execsql2 2.38.0__py3-none-any.whl → 2.40.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. execsql/cli/lint.py +40 -8
  2. execsql/cli/run.py +123 -56
  3. execsql/data/execsql.conf.template +5 -2
  4. execsql/db/base.py +9 -0
  5. execsql/db/dsn.py +2 -3
  6. execsql/db/firebird.py +31 -22
  7. execsql/db/mysql.py +14 -20
  8. execsql/db/oracle.py +6 -4
  9. execsql/db/postgres.py +13 -18
  10. execsql/exceptions.py +5 -0
  11. execsql/gui/__init__.py +13 -10
  12. execsql/gui/base.py +131 -8
  13. execsql/gui/console.py +77 -30
  14. execsql/gui/desktop.py +290 -120
  15. execsql/gui/tui.py +384 -132
  16. execsql/importers/base.py +6 -12
  17. execsql/importers/csv.py +3 -5
  18. execsql/metacommands/control.py +40 -27
  19. execsql/metacommands/data.py +11 -9
  20. execsql/metacommands/io_fileops.py +1 -1
  21. execsql/metacommands/io_write.py +0 -9
  22. execsql/metacommands/prompt.py +94 -42
  23. execsql/metacommands/script_ext.py +6 -2
  24. execsql/metacommands/system.py +0 -2
  25. execsql/parser.py +118 -8
  26. execsql/script/engine.py +1 -1
  27. execsql/script/executor.py +31 -3
  28. execsql/script/parser.py +31 -0
  29. execsql/utils/auth.py +11 -6
  30. execsql/utils/gui.py +162 -53
  31. {execsql2-2.38.0.dist-info → execsql2-2.40.0.dist-info}/METADATA +2 -2
  32. {execsql2-2.38.0.dist-info → execsql2-2.40.0.dist-info}/RECORD +49 -49
  33. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/README.md +0 -0
  34. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/config_settings.sqlite +0 -0
  35. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/example_config_prompt.sql +0 -0
  36. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/make_config_db.sql +0 -0
  37. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/md_compare.sql +0 -0
  38. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/md_glossary.sql +0 -0
  39. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/md_upsert.sql +0 -0
  40. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/pg_compare.sql +0 -0
  41. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/pg_glossary.sql +0 -0
  42. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/script_template.sql +0 -0
  43. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/ss_compare.sql +0 -0
  44. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/ss_glossary.sql +0 -0
  45. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/ss_upsert.sql +0 -0
  46. {execsql2-2.38.0.dist-info → execsql2-2.40.0.dist-info}/WHEEL +0 -0
  47. {execsql2-2.38.0.dist-info → execsql2-2.40.0.dist-info}/entry_points.txt +0 -0
  48. {execsql2-2.38.0.dist-info → execsql2-2.40.0.dist-info}/licenses/LICENSE.txt +0 -0
  49. {execsql2-2.38.0.dist-info → execsql2-2.40.0.dist-info}/licenses/NOTICE +0 -0
execsql/gui/desktop.py CHANGED
@@ -15,8 +15,7 @@ import time
15
15
  from typing import Any, cast
16
16
  from collections.abc import Callable
17
17
 
18
- from execsql.exceptions import ScriptExit
19
- from execsql.gui.base import GuiBackend
18
+ from execsql.gui.base import GuiBackend, _key_column_indices, normalize_text_entry, selected_row_values
20
19
 
21
20
  # ---------------------------------------------------------------------------
22
21
  # Tkinter import guard
@@ -225,6 +224,7 @@ class PauseDialog:
225
224
  class DisplayDialog:
226
225
  def __init__(self, root: tk.Tk, args: dict) -> None:
227
226
  self.result: dict = {}
227
+ self._args = args
228
228
 
229
229
  win = tk.Toplevel(root)
230
230
  win.title(args.get("title", ""))
@@ -283,10 +283,20 @@ class DisplayDialog:
283
283
  root.wait_window(win)
284
284
 
285
285
  def _close(self, win: tk.Toplevel, value: int | None) -> None:
286
- self.result = {
287
- "button": value,
288
- "return_value": self._text_var.get() if self._text_var else None,
289
- }
286
+ return_value = self._text_var.get() if self._text_var else None
287
+ if value is not None and return_value is not None:
288
+ try:
289
+ return_value = normalize_text_entry(
290
+ return_value,
291
+ self._args.get("textentrytype"),
292
+ self._args.get("textentrycase"),
293
+ )
294
+ except ValueError as exc:
295
+ from tkinter import messagebox
296
+
297
+ messagebox.showerror("Invalid entry", str(exc), parent=win)
298
+ return
299
+ self.result = {"button": value, "return_value": return_value}
290
300
  if win.winfo_exists():
291
301
  win.destroy()
292
302
 
@@ -521,14 +531,13 @@ class CompareDialog:
521
531
 
522
532
  tree1 = _add_tree(tables_frame, "Table 1", headers1, rows1)
523
533
  tree2 = _add_tree(tables_frame, "Table 2", headers2, rows2)
534
+ iids1 = tree1.get_children()
535
+ iids2 = tree2.get_children()
524
536
 
525
537
  # Cross-table row highlighting based on key columns
526
- if keylist:
527
- headers1_str = [str(h) for h in headers1]
528
- headers2_str = [str(h) for h in headers2]
529
- key_idx1 = [i for i, h in enumerate(headers1_str) if h in keylist]
530
- key_idx2 = [i for i, h in enumerate(headers2_str) if h in keylist]
531
-
538
+ key_idx1 = _key_column_indices([str(h) for h in headers1], keylist)
539
+ key_idx2 = _key_column_indices([str(h) for h in headers2], keylist)
540
+ if key_idx1 and key_idx2:
532
541
  # Syncing uses display-consistent string normalization (treeview
533
542
  # values are strings). The diff engine in base.py has its own
534
543
  # _pk_tuple with native-equality semantics — it does not share
@@ -536,8 +545,6 @@ class CompareDialog:
536
545
  def _kv(row, idxs):
537
546
  return tuple(str(row[i]) if row[i] is not None else "" for i in idxs)
538
547
 
539
- iids1 = tree1.get_children()
540
- iids2 = tree2.get_children()
541
548
  iid_to_kv1 = {iid: _kv(rows1[i], key_idx1) for i, iid in enumerate(iids1)}
542
549
  iid_to_kv2 = {iid: _kv(rows2[i], key_idx2) for i, iid in enumerate(iids2)}
543
550
  # First occurrence wins for duplicate PKs (consistent with compute_row_diffs).
@@ -675,6 +682,7 @@ class CompareDialog:
675
682
  class SelectRowsDialog:
676
683
  def __init__(self, root: tk.Tk, args: dict) -> None:
677
684
  self.result: dict = {}
685
+ self._selected_rows: list[list[Any]] = []
678
686
 
679
687
  win = tk.Toplevel(root)
680
688
  win.title(args.get("title", "Select rows"))
@@ -690,7 +698,10 @@ class SelectRowsDialog:
690
698
  anchor="w",
691
699
  pady=(0, 4),
692
700
  )
693
- ttk.Label(frame, text="Double-click a row to copy it to the destination table.").pack(
701
+ ttk.Label(
702
+ frame,
703
+ text="Double-click a source row to add it; double-click an added destination row to remove that copy. Existing rows are kept.",
704
+ ).pack(
694
705
  anchor="w",
695
706
  pady=(0, 8),
696
707
  )
@@ -714,19 +725,40 @@ class SelectRowsDialog:
714
725
  tree.configure(yscrollcommand=vsb.set)
715
726
  tree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
716
727
  vsb.pack(side=tk.RIGHT, fill=tk.Y)
717
- ttk.Label(lf, text=_row_count_text(len(rows))).pack(anchor="w")
718
- return tree
728
+ count = ttk.Label(lf, text=_row_count_text(len(rows)))
729
+ count.pack(anchor="w")
730
+ return tree, count
719
731
 
720
- src_tree = _add_tree_frame(tables_frame, "Source", headers1, rows1)
721
- dst_tree = _add_tree_frame(tables_frame, "Destination", headers2, rows2)
732
+ src_tree, _ = _add_tree_frame(tables_frame, "Source", headers1, rows1)
733
+ dst_tree, dst_count = _add_tree_frame(tables_frame, "Destination", headers2, rows2)
734
+ source_rows = {key: list(row) for key, row in zip(src_tree.get_children(), rows1)}
735
+
736
+ pending_rows = {}
722
737
 
723
738
  def _on_double_click(event):
724
739
  sel = src_tree.selection()
725
740
  if sel:
726
741
  row_id = sel[0]
727
- values = src_tree.item(row_id, "values")
728
- dst_tree.insert("", "end", values=values)
729
-
742
+ values = source_rows[row_id]
743
+ selected = list(values)
744
+ self._selected_rows.append(selected)
745
+ display = selected_row_values(headers1, values, headers2)
746
+ key = dst_tree.insert("", "end", values=[str(value) if value is not None else "" for value in display])
747
+ pending_rows[key] = selected
748
+ dst_count.configure(text=_row_count_text(len(rows2) + len(self._selected_rows)))
749
+
750
+ def _remove_row(event=None):
751
+ selection = dst_tree.selection()
752
+ key = dst_tree.identify_row(event.y) if event is not None else selection[0] if selection else None
753
+ selected = pending_rows.pop(key, None)
754
+ if selected is not None:
755
+ self._selected_rows = [row for row in self._selected_rows if row is not selected]
756
+ dst_tree.delete(key)
757
+ dst_count.configure(text=_row_count_text(len(rows2) + len(self._selected_rows)))
758
+ return "break"
759
+
760
+ dst_tree.bind("<Double-1>", _remove_row)
761
+ dst_tree.bind("<Return>", lambda event: _remove_row())
730
762
  src_tree.bind("<Double-1>", _on_double_click)
731
763
  src_tree.bind("<Return>", _on_double_click)
732
764
 
@@ -735,12 +767,13 @@ class SelectRowsDialog:
735
767
  btn_frame.pack(pady=8, anchor="e")
736
768
  _add_buttons(btn_frame, button_list, lambda v: self._close(win, v))
737
769
  win.bind("<Escape>", lambda e: self._close(win, None))
770
+ win.protocol("WM_DELETE_WINDOW", lambda: self._close(win, None))
738
771
 
739
772
  _center_window(win, 900, 500)
740
773
  root.wait_window(win)
741
774
 
742
775
  def _close(self, win: tk.Toplevel, value: int | None) -> None:
743
- self.result = {"button": value}
776
+ self.result = {"button": value, "rows": self._selected_rows if value is not None else None}
744
777
  if win.winfo_exists():
745
778
  win.destroy()
746
779
 
@@ -776,26 +809,33 @@ class SelectSubDialog:
776
809
  tree_frame.pack(fill=tk.BOTH, expand=True)
777
810
  tree = ttk.Treeview(tree_frame, height=min(10, len(rows)))
778
811
  _populate_treeview(tree, headers, rows)
812
+ source_rows = {key: list(row) for key, row in zip(tree.get_children(), rows)}
779
813
  vsb = ttk.Scrollbar(tree_frame, orient=tk.VERTICAL, command=tree.yview)
780
814
  tree.configure(yscrollcommand=vsb.set)
781
815
  tree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
782
816
  vsb.pack(side=tk.RIGHT, fill=tk.Y)
783
817
  ttk.Label(frame, text=_row_count_text(len(rows))).pack(anchor="w")
784
818
 
785
- def _select():
786
- sel = tree.selection()
787
- if sel:
788
- vals = tree.item(sel[0], "values")
789
- row_dict = dict(zip(headers, vals))
790
- self.result = {"button": 1, "row": row_dict}
791
- win.destroy()
819
+ def _select(value=1):
820
+ selected_row = None
821
+ if value == 1:
822
+ sel = tree.selection()
823
+ if not sel:
824
+ return
825
+ selected_row = source_rows[sel[0]]
826
+ self.result = {"button": value, "row": selected_row}
827
+ win.destroy()
792
828
 
793
829
  btn_frame = ttk.Frame(frame)
794
830
  btn_frame.pack(pady=8, anchor="e")
795
- ttk.Button(btn_frame, text="OK", command=_select).pack(side=tk.LEFT, padx=4)
831
+ button_list = args.get("button_list", [("OK", 1, "<Return>")])
832
+ _add_buttons(btn_frame, button_list, _select)
796
833
  ttk.Button(btn_frame, text="Cancel", command=lambda: self._close(win)).pack(side=tk.LEFT, padx=4)
797
834
  tree.bind("<Double-1>", lambda e: _select())
835
+ if not any(len(button) > 2 and button[2] == "<Return>" for button in button_list):
836
+ win.bind("<Return>", lambda e: _select())
798
837
  win.bind("<Escape>", lambda e: self._close(win))
838
+ win.protocol("WM_DELETE_WINDOW", lambda: self._close(win))
799
839
 
800
840
  _center_window(win, 600, 350)
801
841
  root.wait_window(win)
@@ -821,46 +861,135 @@ class ActionDialog:
821
861
 
822
862
  frame = ttk.Frame(win, padding=12)
823
863
  frame.pack(fill=tk.BOTH, expand=True)
864
+ frame.columnconfigure(0, weight=1)
865
+ frame.rowconfigure(0, weight=1)
824
866
  _add_help_button(frame, args.get("help_url"))
825
867
 
868
+ canvas = tk.Canvas(frame, width=600, height=1, highlightthickness=0, yscrollincrement=20)
869
+ canvas.grid(row=0, column=0, sticky="nsew")
870
+ scrollbar = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=canvas.yview)
871
+ scrollbar.grid(row=0, column=1, sticky="ns")
872
+ canvas.configure(yscrollcommand=scrollbar.set)
873
+ content = ttk.Frame(canvas)
874
+ content_window = canvas.create_window((0, 0), window=content, anchor="nw")
875
+ content.bind("<Configure>", lambda event: canvas.configure(scrollregion=canvas.bbox("all")))
876
+
877
+ wrapped_labels = []
826
878
  message = args.get("message", "")
827
879
  if message:
828
- ttk.Label(frame, text=message, wraplength=580, anchor="w", justify="left").pack(
829
- anchor="w",
830
- pady=(0, 8),
831
- )
880
+ message_label = ttk.Label(content, text=message, wraplength=580, anchor="w", justify="left")
881
+ message_label.pack(fill=tk.X, pady=(0, 8))
882
+ wrapped_labels.append(message_label)
883
+
884
+ def fit_width(event):
885
+ canvas.itemconfigure(content_window, width=event.width)
886
+ for label in wrapped_labels:
887
+ label.configure(wraplength=max(1, event.width))
832
888
 
889
+ canvas.bind("<Configure>", fit_width)
833
890
  headers = args.get("column_headers")
834
891
  rows = args.get("rowset")
892
+ tree = None
893
+ source_rows = {}
835
894
  if headers and rows:
836
- tree = ttk.Treeview(frame, height=min(6, len(rows)))
895
+ tree = ttk.Treeview(content, height=min(6, len(rows)), selectmode="browse")
837
896
  _populate_treeview(tree, headers, rows)
838
- tree.pack(fill=tk.BOTH, expand=True, pady=(0, 4))
839
- ttk.Label(frame, text=_row_count_text(len(rows))).pack(anchor="w", pady=(0, 8))
897
+ source_rows = {key: list(row) for key, row in zip(tree.get_children(), rows)}
898
+ tree.pack(fill=tk.X, pady=(0, 4))
899
+ ttk.Label(content, text=_row_count_text(len(rows))).pack(anchor="w", pady=(0, 8))
840
900
 
841
901
  button_specs = args.get("button_specs", [])
842
- for i, spec in enumerate(button_specs):
843
- btn_text = f"{spec.label}\n{spec.prompt}"
902
+ action_buttons: list[ttk.Button] = []
844
903
 
845
- def _button_command(v: int = i + 1) -> None:
846
- self._close(win, v)
904
+ def selected_row():
905
+ selected = tree.selection() if tree is not None else ()
906
+ return source_rows.get(selected[0]) if selected else None
847
907
 
848
- ttk.Button(
849
- frame,
850
- text=btn_text,
851
- command=_button_command,
852
- ).pack(fill=tk.X, pady=2)
908
+ def select_action(index):
909
+ row = selected_row() if button_specs[index].data_required else None
910
+ if button_specs[index].data_required and row is None:
911
+ return
912
+ self.result = {"button": index + 1, "row": row}
913
+ win.destroy()
914
+
915
+ def update_buttons(event):
916
+ has_row = selected_row() is not None
917
+ for button, spec in zip(action_buttons, button_specs):
918
+ button.configure(state="disabled" if spec.data_required and not has_row else "normal")
919
+
920
+ def reveal_action(event):
921
+ top = canvas.canvasy(0)
922
+ y = event.widget.winfo_rooty() - content.winfo_rooty()
923
+ bottom = y + event.widget.winfo_height()
924
+ height = max(1, content.winfo_height())
925
+ if y < top:
926
+ canvas.yview_moveto(y / height)
927
+ elif bottom > top + canvas.winfo_height():
928
+ canvas.yview_moveto((bottom - canvas.winfo_height()) / height)
929
+
930
+ for i, spec in enumerate(button_specs):
931
+ action_row = ttk.Frame(content)
932
+ action_row.pack(fill=tk.X, pady=4)
933
+ action_row.columnconfigure(1, weight=1)
853
934
 
854
- include_continue = args.get("include_continue_button")
855
- if include_continue:
856
- ttk.Button(frame, text="Continue", command=lambda: self._close(win, 1)).pack(fill=tk.X, pady=2)
935
+ def _button_command(index: int = i) -> None:
936
+ select_action(index)
857
937
 
938
+ button = ttk.Button(
939
+ action_row,
940
+ text=spec.label,
941
+ command=_button_command,
942
+ state="disabled" if spec.data_required else "normal",
943
+ )
944
+ button.grid(row=0, column=0, sticky="nw", padx=(0, 8))
945
+ button.bind("<FocusIn>", reveal_action)
946
+ description = ttk.Label(action_row, text=spec.prompt, wraplength=360, anchor="w", justify="left")
947
+ description.grid(row=0, column=1, sticky="ew")
948
+ description.bind("<Configure>", lambda event: event.widget.configure(wraplength=max(1, event.width)))
949
+ action_buttons.append(button)
950
+
951
+ if tree is not None:
952
+ tree.bind("<<TreeviewSelect>>", update_buttons)
953
+
954
+ footer = ttk.Frame(frame)
955
+ footer.grid(row=1, column=0, columnspan=2, sticky="ew", pady=(8, 0))
956
+ ttk.Button(footer, text="Cancel", command=lambda: self._close(win, None)).pack(side=tk.RIGHT, padx=4)
957
+ if args.get("include_continue_button"):
958
+ ttk.Button(footer, text="Continue", command=lambda: self._close(win, 0)).pack(side=tk.RIGHT, padx=4)
959
+
960
+ def scroll(event):
961
+ if content.winfo_height() > canvas.winfo_height():
962
+ if event.num in (4, 5):
963
+ units = -1 if event.num == 4 else 1
964
+ else:
965
+ units = -int(event.delta / 120) if abs(event.delta) >= 120 else -event.delta
966
+ canvas.yview_scroll(units, "units")
967
+ return "break"
968
+
969
+ win.bind("<MouseWheel>", scroll)
970
+ win.bind("<Button-4>", scroll)
971
+ win.bind("<Button-5>", scroll)
972
+ win.bind("<Prior>", lambda event: canvas.yview_scroll(-1, "pages"))
973
+ win.bind("<Next>", lambda event: canvas.yview_scroll(1, "pages"))
858
974
  win.bind("<Escape>", lambda e: self._close(win, None))
859
- _center_window(win, 400, 100 + len(button_specs) * 50)
975
+ win.protocol("WM_DELETE_WINDOW", lambda: self._close(win, None))
976
+
977
+ width = min(640, win.winfo_screenwidth() - 80)
978
+ win.update_idletasks()
979
+ canvas.configure(width=max(1, width - 24 - scrollbar.winfo_reqwidth()))
980
+ win.update_idletasks()
981
+ canvas.itemconfigure(content_window, width=canvas.winfo_reqwidth())
982
+ win.update_idletasks()
983
+ available_height = max(80, win.winfo_screenheight() - 150 - footer.winfo_reqheight())
984
+ canvas.configure(height=min(content.winfo_reqheight(), available_height))
985
+ win.update_idletasks()
986
+ height = min(win.winfo_reqheight(), win.winfo_screenheight() - 100)
987
+ _center_window(win, width, height)
988
+ win.minsize(min(width, 400), min(height, 180))
860
989
  root.wait_window(win)
861
990
 
862
991
  def _close(self, win: tk.Toplevel, value: int | None) -> None:
863
- self.result = {"button": value}
992
+ self.result = {"button": value, "row": None}
864
993
  if win.winfo_exists():
865
994
  win.destroy()
866
995
 
@@ -1057,31 +1186,39 @@ class CredentialsDialog:
1057
1186
  pady=(0, 8),
1058
1187
  )
1059
1188
 
1189
+ form = ttk.Frame(frame)
1190
+ form.pack(fill=tk.BOTH, expand=True)
1191
+
1060
1192
  user_var = tk.StringVar()
1061
1193
  pw_var = tk.StringVar()
1062
1194
 
1063
- ttk.Label(frame, text="Username:").grid(row=0, column=0, sticky="e", padx=4, pady=4)
1064
- ttk.Entry(frame, textvariable=user_var, width=30).grid(row=0, column=1, sticky="ew", pady=4)
1195
+ ttk.Label(form, text="Username:").grid(row=0, column=0, sticky="e", padx=4, pady=4)
1196
+ ttk.Entry(form, textvariable=user_var, width=30).grid(row=0, column=1, sticky="ew", pady=4)
1065
1197
 
1066
- ttk.Label(frame, text="Password:").grid(row=1, column=0, sticky="e", padx=4, pady=4)
1067
- ttk.Entry(frame, textvariable=pw_var, show="*", width=30).grid(row=1, column=1, sticky="ew", pady=4)
1068
- frame.columnconfigure(1, weight=1)
1198
+ ttk.Label(form, text="Password:").grid(row=1, column=0, sticky="e", padx=4, pady=4)
1199
+ ttk.Entry(form, textvariable=pw_var, show="*", width=30).grid(row=1, column=1, sticky="ew", pady=4)
1200
+ form.columnconfigure(1, weight=1)
1069
1201
 
1070
- btn_frame = ttk.Frame(frame)
1202
+ btn_frame = ttk.Frame(form)
1071
1203
  btn_frame.grid(row=2, column=0, columnspan=2, pady=8, sticky="e")
1072
1204
  ttk.Button(btn_frame, text="OK", command=lambda: self._close(win, user_var.get(), pw_var.get())).pack(
1073
1205
  side=tk.LEFT,
1074
1206
  padx=4,
1075
1207
  )
1076
- ttk.Button(btn_frame, text="Cancel", command=lambda: self._close(win, "", "")).pack(side=tk.LEFT, padx=4)
1208
+ ttk.Button(btn_frame, text="Cancel", command=lambda: self._close(win, "", "", cancelled=True)).pack(
1209
+ side=tk.LEFT,
1210
+ padx=4,
1211
+ )
1077
1212
  win.bind("<Return>", lambda e: self._close(win, user_var.get(), pw_var.get()))
1078
- win.bind("<Escape>", lambda e: self._close(win, "", ""))
1213
+ win.bind("<Escape>", lambda e: self._close(win, "", "", cancelled=True))
1214
+
1215
+ win.protocol("WM_DELETE_WINDOW", lambda: self._close(win, "", "", cancelled=True))
1079
1216
 
1080
1217
  _center_window(win, 380, 180)
1081
1218
  root.wait_window(win)
1082
1219
 
1083
- def _close(self, win: tk.Toplevel, username: str, password: str) -> None:
1084
- self.result = {"username": username, "password": password}
1220
+ def _close(self, win: tk.Toplevel, username: str, password: str, *, cancelled: bool = False) -> None:
1221
+ self.result = {"button": None if cancelled else 1, "username": username, "password": password}
1085
1222
  if win.winfo_exists():
1086
1223
  win.destroy()
1087
1224
 
@@ -1122,56 +1259,88 @@ class ConnectDialog:
1122
1259
  pady=(0, 8),
1123
1260
  )
1124
1261
 
1125
- type_var = tk.StringVar(value="p")
1126
- server_var = tk.StringVar()
1127
- db_var = tk.StringVar()
1128
- user_var = tk.StringVar()
1129
-
1130
- def _make_row(parent, row, label, widget_factory):
1131
- ttk.Label(parent, text=label, anchor="e").grid(row=row, column=0, sticky="e", padx=4, pady=4)
1132
- w = widget_factory(parent)
1133
- w.grid(row=row, column=1, sticky="ew", pady=4)
1134
- return w
1262
+ from execsql.gui.base import CONNECTION_ENCODINGS, FILE_DATABASES, connection_result
1135
1263
 
1264
+ variables = {
1265
+ name: tk.StringVar() for name in ("server", "database", "username", "password", "port", "encoding")
1266
+ }
1136
1267
  form = ttk.Frame(frame)
1137
1268
  form.pack(fill=tk.BOTH, expand=True)
1138
1269
  form.columnconfigure(1, weight=1)
1139
-
1140
- type_labels = [f"{k} — {v}" for k, v in _DB_TYPE_OPTIONS]
1141
1270
  type_values = [k for k, v in _DB_TYPE_OPTIONS]
1142
- type_combo = ttk.Combobox(form, textvariable=type_var, values=type_labels, state="readonly", width=30)
1271
+ ttk.Label(form, text="Database type:").grid(row=0, column=0, sticky="e", padx=4, pady=4)
1272
+ type_combo = ttk.Combobox(form, values=[f"{k} — {v}" for k, v in _DB_TYPE_OPTIONS], state="readonly", width=35)
1143
1273
  type_combo.current(0)
1144
- type_var.trace_add("write", lambda *a: None) # placeholder
1145
- _make_row(form, 0, "Database type:", lambda p: type_combo)
1146
- ttk.Label(form, text="Server:").grid(row=1, column=0, sticky="e", padx=4, pady=4)
1147
- ttk.Entry(form, textvariable=server_var, width=35).grid(row=1, column=1, sticky="ew", pady=4)
1148
- ttk.Label(form, text="Database/File:").grid(row=2, column=0, sticky="e", padx=4, pady=4)
1149
- ttk.Entry(form, textvariable=db_var, width=35).grid(row=2, column=1, sticky="ew", pady=4)
1150
- ttk.Label(form, text="Username:").grid(row=3, column=0, sticky="e", padx=4, pady=4)
1151
- ttk.Entry(form, textvariable=user_var, width=35).grid(row=3, column=1, sticky="ew", pady=4)
1274
+ type_combo.grid(row=0, column=1, sticky="ew", pady=4)
1275
+ rows = {}
1276
+ for row, (name, label) in enumerate(
1277
+ (
1278
+ ("server", "Server:"),
1279
+ ("database", "Database:"),
1280
+ ("username", "Username:"),
1281
+ ("password", "Password:"),
1282
+ ("port", "Port (optional):"),
1283
+ ("encoding", "Encoding (optional):"),
1284
+ ),
1285
+ 1,
1286
+ ):
1287
+ caption = ttk.Label(form, text=label)
1288
+ caption.grid(row=row, column=0, sticky="e", padx=4, pady=4)
1289
+ entry: ttk.Entry
1290
+ if name == "encoding":
1291
+ entry = ttk.Combobox(form, textvariable=variables[name], values=CONNECTION_ENCODINGS, width=35)
1292
+ else:
1293
+ entry = ttk.Entry(form, textvariable=variables[name], width=35, show="*" if name == "password" else "")
1294
+ entry.grid(row=row, column=1, sticky="ew", pady=4)
1295
+ rows[name] = (caption, entry)
1296
+
1297
+ def kind():
1298
+ idx = type_combo.current()
1299
+ return type_values[idx] if idx >= 0 else "p"
1300
+
1301
+ def browse():
1302
+ selected = filedialog.askopenfilename(parent=win, title="Select database file")
1303
+ if selected:
1304
+ variables["database"].set(selected)
1305
+
1306
+ browse_button = ttk.Button(form, text="Browse…", command=browse)
1307
+ browse_button.grid(row=2, column=2, padx=4)
1308
+
1309
+ def update_fields(*_):
1310
+ db_type = kind()
1311
+ file_based = db_type in FILE_DATABASES
1312
+ for name in ("server", "username", "password", "port", "encoding"):
1313
+ visible = not file_based and not (db_type == "d" and name in ("server", "port"))
1314
+ for widget in rows[name]:
1315
+ widget.grid() if visible else widget.grid_remove()
1316
+ rows["database"][0].configure(
1317
+ text="File path:" if file_based else "DSN:" if db_type == "d" else "Database:",
1318
+ )
1319
+ browse_button.grid() if file_based else browse_button.grid_remove()
1152
1320
 
1321
+ type_combo.bind("<<ComboboxSelected>>", update_fields)
1322
+ update_fields()
1323
+ ttk.Label(frame, text="Blank password: no additional password prompt.").pack(anchor="w", pady=4)
1324
+ error = ttk.Label(frame, text="", wraplength=440)
1325
+ error.pack(anchor="w")
1153
1326
  btn_frame = ttk.Frame(frame)
1154
1327
  btn_frame.pack(pady=8, anchor="e")
1155
1328
 
1156
1329
  def _on_connect():
1157
- idx = type_combo.current()
1158
- db_type = type_values[idx] if idx >= 0 else "p"
1159
- db = db_var.get().strip() or None
1160
- self.result = {
1161
- "db_type": db_type,
1162
- "server": server_var.get().strip() or None,
1163
- "database": db,
1164
- "db_file": db if db_type in ("l", "k", "a") else None,
1165
- "username": user_var.get().strip() or None,
1166
- }
1330
+ try:
1331
+ self.result = dict(connection_result(kind(), **{name: var.get() for name, var in variables.items()}))
1332
+ except ValueError as exc:
1333
+ error.configure(text=str(exc))
1334
+ rows["port"][1].focus_set()
1335
+ return
1167
1336
  win.destroy()
1168
1337
 
1169
1338
  ttk.Button(btn_frame, text="Connect", command=_on_connect).pack(side=tk.LEFT, padx=4)
1170
1339
  ttk.Button(btn_frame, text="Cancel", command=lambda: self._close(win)).pack(side=tk.LEFT, padx=4)
1171
1340
  win.bind("<Return>", lambda e: _on_connect())
1172
1341
  win.bind("<Escape>", lambda e: self._close(win))
1173
-
1174
- _center_window(win, 440, 240)
1342
+ win.protocol("WM_DELETE_WINDOW", lambda: self._close(win))
1343
+ _center_window(win, 560, 450)
1175
1344
  root.wait_window(win)
1176
1345
 
1177
1346
  def _close(self, win: tk.Toplevel) -> None:
@@ -1241,7 +1410,20 @@ class ConsoleWindow:
1241
1410
  )
1242
1411
 
1243
1412
  self._running = True
1244
- _center_window(self._win, self._width * 8, self._height * 20)
1413
+ self._win.update_idletasks()
1414
+ _center_window(self._win, self._win.winfo_reqwidth(), self._win.winfo_reqheight())
1415
+
1416
+ def resize(self, width: int, height: int) -> None:
1417
+ """Apply requested text dimensions and let Tk fit the window around them.
1418
+
1419
+ Clearing the explicit pixel size also handles windows resized manually.
1420
+ The existing position and hidden/visible state are preserved.
1421
+ """
1422
+ self._width, self._height = width, height
1423
+ if self._text is not None and self._win is not None:
1424
+ self._text.configure(width=width, height=height)
1425
+ self._win.geometry("")
1426
+ self._win.update_idletasks()
1245
1427
 
1246
1428
  def _on_close(self) -> None:
1247
1429
  self._running = False
@@ -1334,15 +1516,24 @@ class TkinterBackend(GuiBackend):
1334
1516
  # ------------------------------------------------------------------
1335
1517
 
1336
1518
  def console_on(self) -> None:
1519
+ from execsql.utils.gui import gui_console_height, gui_console_width
1520
+
1337
1521
  root = self._root_or_raise()
1522
+ width, height = gui_console_width(), gui_console_height()
1338
1523
  if self._console is None:
1339
- self._console = ConsoleWindow(root)
1524
+ self._console = ConsoleWindow(root, width=width, height=height)
1525
+ else:
1526
+ self._console.resize(width, height)
1340
1527
  self._console.start()
1341
1528
  import execsql.state as _state
1342
1529
 
1343
1530
  _state.output.redir_stdout(self._console.write)
1344
1531
  _state.output.redir_stderr(self._console.write)
1345
1532
 
1533
+ def console_resize(self, width: int, height: int) -> None:
1534
+ if self._console is not None:
1535
+ self._console.resize(width, height)
1536
+
1346
1537
  def console_off(self) -> None:
1347
1538
  if self._console:
1348
1539
  self._console.stop()
@@ -1455,15 +1646,14 @@ class TkinterBackend(GuiBackend):
1455
1646
 
1456
1647
 
1457
1648
  # ---------------------------------------------------------------------------
1458
- # _TkinterSyncQueue — synchronous gui_manager_queue replacement for level 3
1649
+ # _TkinterSyncQueue — synchronous gui_manager_queue replacement
1459
1650
  # ---------------------------------------------------------------------------
1460
1651
 
1461
1652
 
1462
1653
  class _TkinterSyncQueue:
1463
1654
  """Drop-in replacement for ``_state.gui_manager_queue`` on the Tkinter path.
1464
1655
 
1465
- gui_level == 3 with tkinter available: instead of a background manager
1466
- thread, ``put()`` dispatches the dialog synchronously in the calling
1656
+ ``put()`` dispatches native Tk dialogs synchronously in the calling
1467
1657
  (main) thread. After ``put()`` returns, the result is already on
1468
1658
  ``spec.return_queue`` so the calling metacommand's ``get()`` unblocks
1469
1659
  immediately.
@@ -1477,24 +1667,6 @@ class _TkinterSyncQueue:
1477
1667
  def __init__(self, backend: TkinterBackend) -> None:
1478
1668
  self._backend = backend
1479
1669
 
1480
- # GUI types for which a None/cancelled result means the user wants to exit.
1481
- # File/directory/credentials/connect dialogs are intentionally excluded
1482
- # because cancelling those just means "no selection" rather than "quit".
1483
- _EXIT_ON_CANCEL = frozenset(
1484
- [
1485
- "halt",
1486
- "msg",
1487
- "pause",
1488
- "display",
1489
- "entry",
1490
- "compare",
1491
- "selectrows",
1492
- "selectsub",
1493
- "action",
1494
- "map",
1495
- ],
1496
- )
1497
-
1498
1670
  def put(self, spec: Any, block: bool = True, timeout: Any = None) -> None:
1499
1671
  if spec is None:
1500
1672
  return
@@ -1513,8 +1685,6 @@ class _TkinterSyncQueue:
1513
1685
  pass # Tk event loop may be torn down.
1514
1686
  except Exception as exc:
1515
1687
  result = {"error": str(exc), "button": None}
1516
- if result is not None and spec.gui_type in self._EXIT_ON_CANCEL and result.get("button") is None:
1517
- raise ScriptExit(2)
1518
1688
  spec.return_queue.put(result if result is not None else {})
1519
1689
 
1520
1690
  def get_nowait(self) -> Any: