-
Notifications
You must be signed in to change notification settings - Fork 0
/
KQL-DB-MANAGER.py
610 lines (460 loc) · 26.1 KB
/
KQL-DB-MANAGER.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
import sqlite3
from datetime import datetime
import tkinter as tk
from tkinter import messagebox, ttk, filedialog
import csv
import json
import pyperclip
class QueryManagerApp:
def __init__(self, root):
self.root = root
self.root.title("Query Manager")
self.root.geometry("1700x800")
# Initialize database
self.init_db()
# Create menu
self.create_menu()
# Create navigation pane
self.create_navigation_pane()
# Create display area
self.create_display_area()
# Load queries
self.load_queries()
def init_db(self):
conn = sqlite3.connect('queries.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS queries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
author TEXT NOT NULL,
date_added TEXT NOT NULL,
description TEXT,
query_text TEXT NOT NULL,
is_favorite BOOLEAN NOT NULL CHECK (is_favorite IN (0, 1)),
tags TEXT
)
''')
conn.commit()
conn.close()
def create_menu(self):
menubar = tk.Menu(self.root)
self.root.config(menu=menubar)
file_menu = tk.Menu(menubar, tearoff=0)
# Removed Add command
file_menu.add_command(label="Export", command=self.export_data)
file_menu.add_command(label="Import", command=self.import_data)
file_menu.add_command(label="Exit", command=self.root.quit)
menubar.add_cascade(label="File", menu=file_menu)
edit_menu = tk.Menu(menubar, tearoff=0)
edit_menu.add_command(label="Add", command=self.add_query)
edit_menu.add_command(label="Edit", command=self.edit_query)
edit_menu.add_command(label="Delete", command=self.delete_query)
menubar.add_cascade(label="Edit", menu=edit_menu)
search_menu = tk.Menu(menubar, tearoff=0)
search_menu.add_command(label="Search", command=self.search_query)
menubar.add_cascade(label="Search", menu=search_menu)
#####WORKINGGGGGGGGGG DISPLAYINGGGGG TITLES NOT SHARMUTA ID'ssssss
# def create_navigation_pane(self):
# nav_frame = ttk.Frame(self.root, width=4000)
# nav_frame.pack(side=tk.LEFT, fill=tk.Y, expand=True)
# tk.Label(nav_frame, text="Navigation", font=('Arial', 16)).pack(expand=True, padx=50,)
# self.nav_tree = ttk.Treeview(nav_frame, padding=70)
# self.nav_tree.bind("<<TreeviewSelect>>", self.display_query_details)
# self.nav_tree.pack(fill=tk.BOTH, expand=True, ipadx=60)
def display_query_details(self, event):
selected_item = self.nav_tree.selection()
if selected_item:
query_id = self.nav_tree.item(selected_item[0], 'values')[0]
conn = sqlite3.connect('queries.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM queries WHERE id = ?', (query_id,))
query = cursor.fetchone()
conn.close()
if query:
self.title_label.config(text=query[1])
self.author_label.config(text=query[2])
self.date_added_label.config(text=query[3])
self.description_label.config(text=query[4])
# Temporarily enable the text widget to update its content
self.query_text_display.config(state=tk.NORMAL)
self.query_text_display.delete(1.0, tk.END)
self.query_text_display.insert(tk.END, query[5])
self.query_text_display.config(state=tk.DISABLED)
self.tags_label.config(text=query[7])
def create_navigation_pane(self):
# Create a frame for the navigation pane
nav_frame = ttk.Frame(self.root)
nav_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# Add a label for the navigation section
tk.Label(nav_frame, text="Navigation", font=('Arial', 16)).pack(padx=50)
# Add a Treeview widget for displaying query titles
self.nav_tree = ttk.Treeview(nav_frame)
#self.nav_tree.heading('ID', text='ID')
# self.nav_tree.heading('Title', text='KQL Query Titles', anchor='center')
#self.nav_tree.column('ID', width=50, anchor='w')
#self.nav_tree.column('Title', width=400, anchor='w')
self.nav_tree.pack(fill=tk.BOTH, expand=True)
# Bind the selection event to the display_query_details method
self.nav_tree.bind("<<TreeviewSelect>>", self.display_query_details)
# def create_navigation_pane(self):
# #done
# nav_frame = ttk.Frame(self.root)
# nav_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# #done
# tk.Label(nav_frame, text="Navigation", font=('Arial', 16)).pack(expand=True, padx=50,)
# self.nav_tree = ttk.Treeview(nav_frame, padding=70)
# self.nav_tree.bind("<<TreeviewSelect>>", self.display_query_details)
# self.nav_tree.pack(fill=tk.BOTH, expand=True, ipadx=60)
# def create_navigation_pane(self):
# # Create a frame for the navigation pane
# nav_frame = ttk.Frame(self.root)
# nav_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# # Add a label for the navigation section
# tk.Label(nav_frame, text="Navigation", font=('Arial', 16)).pack(expand=True, padx=50,)
# # Add a Treeview widget for displaying query titles
# self.nav_tree = ttk.Treeview(nav_frame, padding=70)
# self.nav_tree = ttk.Treeview(nav_frame, columns=('Title'),show='headings')
# self.nav_tree.heading('Title', text='KQL Query Titles')
# self.nav_tree.pack(fill=tk.BOTH, expand=True)
# # Configure the Treeview column to auto-adjust width
# self.nav_tree.bind("<<TreeviewSelect>>", self.display_query_details)
#self.nav_tree.column('Title', anchor='w', minwidth=100, width=400, stretch=True)
# Bind the selection event to the display_query_details method
# def create_navigation_pane(self):
# #done
# nav_frame = ttk.Frame(self.root)
# nav_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# #done
# tk.Label(nav_frame, text="Navigation", font=('Arial', 16)).pack(expand=True, padx=50,)
# self.nav_tree = ttk.Treeview(nav_frame, padding=70)
# self.nav_tree.bind("<<TreeviewSelect>>", self.display_query_details)
# self.nav_tree.pack(fill=tk.BOTH, expand=True, ipadx=60)
# def create_navigation_pane(self):
# # Create a frame for the navigation pane
# nav_frame = ttk.Frame(self.root)
# nav_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# # Add a label for the navigation section
# tk.Label(nav_frame, text="Navigation", font=('Arial', 16)).pack(expand=True, padx=50)
# # Add a Treeview widget for displaying query titles
# self.nav_tree = ttk.Treeview(nav_frame, columns=('Title'), show='headings')
# self.nav_tree.heading('Title', text='KQL Query Titles')
# self.nav_tree.column('Title', anchor='w', minwidth=100, width=400, stretch=True)
# self.nav_tree.pack(fill=tk.BOTH, expand=True)
# # Add a vertical scrollbar to the Treeview
# self.tree_scrollbar = ttk.Scrollbar(nav_frame, orient=tk.VERTICAL, command=self.nav_tree.yview)
# self.nav_tree.configure(yscroll=self.tree_scrollbar.set)
# self.tree_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# # Bind the selection event to the display_query_details method
# self.nav_tree.bind("<<TreeviewSelect>>", self.display_query_details)
def create_display_area(self):
self.display_frame = ttk.Frame(self.root)
self.display_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)
tk.Label(self.display_frame, text="Query Details", font=('Arial', 16)).grid(row=0, column=0, columnspan=2, pady=10)
tk.Label(self.display_frame, text="Title:", font=('Arial', 12, 'bold')).grid(row=1, column=0, sticky='w', padx=10, pady=5)
self.title_label = tk.Label(self.display_frame, text="", font=('Arial', 12))
self.title_label.grid(row=1, column=1, padx=10, pady=5, sticky='w')
tk.Label(self.display_frame, text="Author:", font=('Arial', 12, 'bold')).grid(row=2, column=0, sticky='w', padx=10, pady=5)
self.author_label = tk.Label(self.display_frame, text="", font=('Arial', 12))
self.author_label.grid(row=2, column=1, padx=10, pady=5, sticky='w')
tk.Label(self.display_frame, text="Date Added:", font=('Arial', 12, 'bold')).grid(row=3, column=0, sticky='w', padx=10, pady=5)
self.date_added_label = tk.Label(self.display_frame, text="", font=('Arial', 12))
self.date_added_label.grid(row=3, column=1, padx=10, pady=5, sticky='w')
tk.Label(self.display_frame, text="Description:", font=('Arial', 12, 'bold')).grid(row=4, column=0, sticky='w', padx=10, pady=5)
self.description_label = tk.Label(self.display_frame, text="", font=('Arial', 12))
self.description_label.grid(row=4, column=1, padx=10, pady=5, sticky='w')
tk.Label(self.display_frame, text="Query Text:", font=('Arial', 12, 'bold')).grid(row=5, column=0, sticky='w', padx=10, pady=5)
# Add the Copy button
self.copy_button = tk.Button(self.display_frame, text="Copy Query", command=self.copy_to_clipboard)
self.copy_button.grid(row=5, column=1, padx=(10, 0), pady=5, sticky='w')
self.query_text_display = tk.Text(self.display_frame, wrap=tk.WORD, height=10, width=60, state=tk.DISABLED)
self.query_text_display.grid(row=6, column=0, columnspan=2, padx=10, pady=5, sticky='nsew')
tk.Label(self.display_frame, text="Tags:", font=('Arial', 12, 'bold')).grid(row=7, column=0, sticky='w', padx=10, pady=5)
self.tags_label = tk.Label(self.display_frame, text="", font=('Arial', 12))
self.tags_label.grid(row=7, column=1, padx=10, pady=5, sticky='w')
# Update column and row weights to ensure resizing
self.display_frame.grid_columnconfigure(1, weight=1)
self.display_frame.grid_rowconfigure(6, weight=1)
def copy_to_clipboard(self):
query_text = self.query_text_display.get("1.0", tk.END).strip()
pyperclip.copy(query_text)
def new_query(self):
messagebox.showinfo("New Query", "Create a new query.")
def add_query(self):
self.new_window = tk.Toplevel(self.root)
self.new_window.title("Add New Query")
self.new_window.geometry("500x400")
tk.Label(self.new_window, text="Title:").grid(row=0, column=0, pady=5)
self.title_entry = tk.Entry(self.new_window)
self.title_entry.grid(row=0, column=1, pady=5, sticky='ew')
tk.Label(self.new_window, text="Author:").grid(row=1, column=0, pady=5)
self.author_entry = tk.Entry(self.new_window)
self.author_entry.grid(row=1, column=1, pady=5, sticky='ew')
tk.Label(self.new_window, text="Description:").grid(row=2, column=0, pady=5)
self.desc_text = tk.Text(self.new_window, height=5)
self.desc_text.grid(row=2, column=1, pady=5, sticky='ew')
tk.Label(self.new_window, text="Query:").grid(row=3, column=0, pady=5)
self.query_text = tk.Text(self.new_window, height=10)
self.query_text.grid(row=3, column=1, pady=5, sticky='ew')
tk.Label(self.new_window, text="Tags (comma separated):").grid(row=4, column=0, pady=5)
self.tags_entry = tk.Entry(self.new_window)
self.tags_entry.grid(row=4, column=1, pady=5, sticky='ew')
tk.Button(self.new_window, text="Save", command=self.save_query).grid(row=5, column=1, pady=10, sticky='e')
# Make the new window resizable
self.new_window.grid_columnconfigure(1, weight=1)
self.new_window.grid_rowconfigure(2, weight=1)
self.new_window.grid_rowconfigure(3, weight=2)
def save_query(self):
title = self.title_entry.get().strip()
author = self.author_entry.get().strip()
description = self.desc_text.get("1.0", tk.END).strip()
query_text = self.query_text.get("1.0", tk.END).strip()
tags = self.tags_entry.get().strip()
date_added = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
is_favorite = 0
# Check for duplicate title (case-insensitive)
conn = sqlite3.connect('queries.db')
cursor = conn.cursor()
cursor.execute('''
SELECT COUNT(*) FROM queries WHERE LOWER(title) = LOWER(?)
''', (title,))
count = cursor.fetchone()[0]
if count > 0:
messagebox.showwarning("Duplicate Title",
"A KQL Query with the same title exists. You can paste your similar KQL Query to the Query Text field of the existing KQL Query Title, add few lines and paste :) Ostrich Algorithm Moment.")
conn.close()
return
cursor.execute('''
INSERT INTO queries (title, author, date_added, description, query_text, is_favorite, tags)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', (title, author, date_added, description, query_text, is_favorite, tags))
conn.commit()
conn.close()
self.new_window.destroy()
self.load_queries()
def load_queries(self):
# Clear the tree view
for item in self.nav_tree.get_children():
self.nav_tree.delete(item)
# Load queries from the database and populate the navigation pane
conn = sqlite3.connect('queries.db')
cursor = conn.cursor()
cursor.execute('SELECT id, title FROM queries WHERE title IS NOT NULL AND title != ""')
rows = cursor.fetchall()
for row in rows:
self.nav_tree.insert('', 'end', row[0], text=row[1], values=(row[0],))
conn.close()
def export_data(self):
file_types = [("CSV files", "*.csv"), ("JSON files", "*.json")]
file_path = filedialog.asksaveasfilename(filetypes=file_types, defaultextension=file_types)
if not file_path:
return
conn = sqlite3.connect('queries.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM queries')
rows = cursor.fetchall()
conn.close()
if file_path.endswith('.csv'):
self.export_to_csv(file_path, rows)
elif file_path.endswith('.json'):
self.export_to_json(file_path, rows)
def export_to_csv(self, file_path, data):
fieldnames = ['id', 'title', 'author', 'date_added', 'description', 'query_text', 'is_favorite', 'tags']
with open(file_path, 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for row in data:
writer.writerow({
'id': row[0], 'title': row[1], 'author': row[2],
'date_added': row[3], 'description': row[4],
'query_text': row[5], 'is_favorite': row[6], 'tags': row[7]
})
def export_to_json(self, file_path, data):
export_data = [
{
'id': row[0], 'title': row[1], 'author': row[2],
'date_added': row[3], 'description': row[4],
'query_text': row[5], 'is_favorite': row[6], 'tags': row[7]
} for row in data
]
with open(file_path, 'w', encoding='utf-8') as jsonfile:
json.dump(export_data, jsonfile, indent=4)
def import_data(self):
file_types = [("CSV files", "*.csv"), ("JSON files", "*.json")]
file_path = filedialog.askopenfilename(filetypes=file_types)
if not file_path:
return
if file_path.endswith('.csv'):
self.import_from_csv(file_path)
elif file_path.endswith('.json'):
self.import_from_json(file_path)
def import_from_csv(self, file_path):
with open(file_path, 'r', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
data = [row for row in reader]
self.validate_and_insert_data(data)
def import_from_json(self, file_path):
with open(file_path, 'r', encoding='utf-8') as jsonfile:
data = json.load(jsonfile)
self.validate_and_insert_data(data)
def validate_and_insert_data(self, data):
expected_fields = {'title', 'author', 'date_added', 'description', 'query_text', 'is_favorite', 'tags'}
for entry in data:
if not expected_fields.issubset(entry.keys()):
messagebox.showerror("Import Error", "Invalid data format. Ensure all required fields are present.")
return
conn = sqlite3.connect('queries.db')
cursor = conn.cursor()
for entry in data:
try:
cursor.execute('''
INSERT INTO queries (title, author, date_added, description, query_text, is_favorite, tags)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', (entry['title'], entry['author'], entry['date_added'], entry['description'], entry['query_text'], entry['is_favorite'], entry['tags']))
except sqlite3.IntegrityError:
messagebox.showerror("Import Error", f"Failed to import query '{entry.get('title')}' due to integrity constraints.")
conn.commit()
conn.close()
self.load_queries()
def edit_query(self):
selected_item = self.nav_tree.selection()
if selected_item:
query_id = self.nav_tree.item(selected_item[0], 'values')[0]
self.edit_window = tk.Toplevel(self.root)
self.edit_window.title("Edit Query")
self.edit_window.geometry("500x400")
conn = sqlite3.connect('queries.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM queries WHERE id = ?', (query_id,))
query = cursor.fetchone()
conn.close()
if query:
tk.Label(self.edit_window, text="Title:").grid(row=0, column=0, pady=5)
self.edit_title_entry = tk.Entry(self.edit_window)
self.edit_title_entry.grid(row=0, column=1, pady=5, sticky='ew')
self.edit_title_entry.insert(0, query[1])
tk.Label(self.edit_window, text="Author:").grid(row=1, column=0, pady=5)
self.edit_author_entry = tk.Entry(self.edit_window)
self.edit_author_entry.grid(row=1, column=1, pady=5, sticky='ew')
self.edit_author_entry.insert(0, query[2])
tk.Label(self.edit_window, text="Description:").grid(row=2, column=0, pady=5)
self.edit_desc_text = tk.Text(self.edit_window, height=5)
self.edit_desc_text.grid(row=2, column=1, pady=5, sticky='ew')
self.edit_desc_text.insert(tk.END, query[4])
tk.Label(self.edit_window, text="Query:").grid(row=3, column=0, pady=5)
self.edit_query_text = tk.Text(self.edit_window, height=10)
self.edit_query_text.grid(row=3, column=1, pady=5, sticky='ew')
self.edit_query_text.insert(tk.END, query[5])
tk.Label(self.edit_window, text="Tags (comma separated):").grid(row=4, column=0, pady=5)
self.edit_tags_entry = tk.Entry(self.edit_window)
self.edit_tags_entry.grid(row=4, column=1, pady=5, sticky='ew')
self.edit_tags_entry.insert(0, query[7])
tk.Button(self.edit_window, text="Save Changes", command=lambda: self.save_edited_query(query_id)).grid(row=5, column=1, pady=10, sticky='e')
# Make the edit window resizable
self.edit_window.grid_columnconfigure(1, weight=1)
self.edit_window.grid_rowconfigure(2, weight=1)
self.edit_window.grid_rowconfigure(3, weight=2)
else:
messagebox.showwarning("No Selection", "Please select a query to edit.")
def save_edited_query(self, query_id):
title = self.edit_title_entry.get()
author = self.edit_author_entry.get()
description = self.edit_desc_text.get("1.0", tk.END).strip()
query_text = self.edit_query_text.get("1.0", tk.END).strip()
tags = self.edit_tags_entry.get().strip()
conn = sqlite3.connect('queries.db')
cursor = conn.cursor()
cursor.execute('''
UPDATE queries
SET title = ?, author = ?, description = ?, query_text = ?, tags = ?
WHERE id = ?
''', (title, author, description, query_text, tags, query_id))
conn.commit()
conn.close()
self.edit_window.destroy()
self.load_queries()
def delete_query(self):
selected_item = self.nav_tree.selection()
if selected_item:
query_id = self.nav_tree.item(selected_item[0], 'values')[0]
query_title = self.nav_tree.item(selected_item[0], 'text')
# Confirmation dialog
confirm = messagebox.askyesno("Delete Query", f"Are you sure you want to delete the query '{query_title}'?")
if confirm:
conn = sqlite3.connect('queries.db')
cursor = conn.cursor()
cursor.execute('DELETE FROM queries WHERE id = ?', (query_id,))
conn.commit()
conn.close()
self.load_queries()
else:
messagebox.showwarning("No Selection", "Please select a query to delete.")
def search_query(self):
self.search_window = tk.Toplevel(self.root)
self.search_window.title("Search Queries")
tk.Label(self.search_window, text="Search Option:").grid(row=0, column=0, pady=5)
self.search_option = ttk.Combobox(self.search_window, values=["Title", "Query Text", "Tags", "All"], state='readonly')
self.search_option.set("All")
self.search_option.grid(row=0, column=1, pady=5)
tk.Label(self.search_window, text="Search Term:").grid(row=1, column=0, pady=5)
self.search_entry = tk.Entry(self.search_window)
self.search_entry.grid(row=1, column=1, pady=5)
self.time_filter_var = tk.BooleanVar()
tk.Checkbutton(self.search_window, text="Enable Time Range Filter", variable=self.time_filter_var, command=self.toggle_time_filter).grid(row=2, column=0, columnspan=2, pady=5)
tk.Label(self.search_window, text="Start Date (YYYY-MM-DD HH:MM:SS):").grid(row=3, column=0, pady=5)
self.start_date_entry = tk.Entry(self.search_window)
self.start_date_entry.grid(row=3, column=1, pady=5)
tk.Label(self.search_window, text="End Date (YYYY-MM-DD HH:MM:SS):").grid(row=4, column=0, pady=5)
self.end_date_entry = tk.Entry(self.search_window)
self.end_date_entry.grid(row=4, column=1, pady=5)
tk.Button(self.search_window, text="Search", command=self.perform_search).grid(row=5, column=1, pady=10)
# Initially disable the time filter fields
self.toggle_time_filter()
def toggle_time_filter(self):
state = 'normal' if self.time_filter_var.get() else 'disabled'
self.start_date_entry.config(state=state)
self.end_date_entry.config(state=state)
def perform_search(self):
search_term = self.search_entry.get()
search_option = self.search_option.get()
start_date = self.start_date_entry.get().strip()
end_date = self.end_date_entry.get().strip()
query = 'SELECT id, title FROM queries WHERE'
params = []
if search_option == "Title":
query += ' title LIKE ?'
params.append(f'%{search_term}%')
elif search_option == "Query Text":
query += ' query_text LIKE ?'
params.append(f'%{search_term}%')
elif search_option == "Tags":
query += ' tags LIKE ?'
params.append(f'%{search_term}%')
else: # All
query += ' title LIKE ? OR query_text LIKE ? OR tags LIKE ?'
params.extend([f'%{search_term}%'] * 3)
if self.time_filter_var.get():
if not start_date or not end_date:
messagebox.showwarning("Time Filter Error", "Please enter both start and end dates.")
return
try:
datetime.strptime(start_date, "%Y-%m-%d %H:%M:%S")
datetime.strptime(end_date, "%Y-%m-%d %H:%M:%S")
except ValueError:
messagebox.showwarning("Time Filter Error", "Invalid date format. Use YYYY-MM-DD HH:MM:SS.")
return
query += ' AND date_added BETWEEN ? AND ?'
params.extend([start_date, end_date])
conn = sqlite3.connect('queries.db')
cursor = conn.cursor()
cursor.execute(query, params)
rows = cursor.fetchall()
conn.close()
self.search_window.destroy()
self.nav_tree.delete(*self.nav_tree.get_children())
for row in rows:
self.nav_tree.insert('', 'end', row[0], text=row[1], values=(row[0],))
if __name__ == "__main__":
root = tk.Tk()
app = QueryManagerApp(root)
root.mainloop()