Skip to content

Commit c1e4ee2

Browse files
authored
Merge pull request #30 from whellcome/v1.3-dev
Custom file name for exported SQL script
2 parents 169537f + 9f4f729 commit c1e4ee2

File tree

1 file changed

+53
-17
lines changed

1 file changed

+53
-17
lines changed

code/export-msaccess-sql.py

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,13 @@ def __init__(self, render_params=None, *args, **options):
3333
12: "Text"
3434
}}
3535
self.db_path = tk.StringVar(self, "")
36+
self.sql_path = tk.StringVar(self, "")
3637
self.config_path = tk.StringVar(self, "")
37-
self.label1 = ttk.Label(self, text="", font=("Helvetica", 12))
38+
self.lable_frame = ttk.Frame(self, padding=(2, 2))
39+
self.label_db_path = ttk.Label(self.lable_frame, text="MS Access database", font=("Helvetica", 12),
40+
wraplength=650)
41+
self.label_sql_path = ttk.Label(self.lable_frame, text="Exported SQL script:", font=("Helvetica", 12),
42+
wraplength=650)
3843
self.frame0 = ttk.Frame(self, width=240, borderwidth=1, relief="solid", padding=(2, 2))
3944
self.frame1 = ttk.Frame(self, width=100, borderwidth=1, relief="solid", padding=(2, 2))
4045
self.tree = TreeviewDataFrame(self.frame1, columns=("table", "export", "data"), show="headings")
@@ -51,22 +56,26 @@ def create_widgets(self):
5156
self.rgrid(self)
5257
self.rgrid(tk.Label(self, text="MS Access to SQL Export Tool", font=("Helvetica", 14)),
5358
dict(row=0, column=0, columnspan=3, pady=5))
54-
self.rgrid(self.label1, dict(row=1, column=0, columnspan=3))
59+
self.rgrid(self.lable_frame, dict(row=1, column=0, columnspan=3))
60+
self.rgrid(self.label_db_path, dict(row=0, column=0, columnspan=3, pady=5))
61+
self.rgrid(self.label_sql_path, dict(row=1, column=0, columnspan=3, pady=5))
5562
self.rgrid(tk.Button(self, text="MS Access File Open", command=self.btn_openf, font=("Helvetica", 11)),
56-
dict(row=2, column=0, columnspan=2))
63+
dict(row=2, column=0))
64+
self.rgrid(tk.Button(self, text="Save SQL script as...", command=self.btn_sql_path, font=("Helvetica", 11)),
65+
dict(row=2, column=1))
5766
self.rgrid(tk.Button(self, text=" Exit ", command=self.btn_exit, font=("Helvetica", 11)),
5867
dict(row=2, column=2, columnspan=2))
5968
self.rgrid(self.frame1, dict(row=4, column=0, columnspan=3))
6069
self.rgrid(self.tree, dict(row=0, column=0, pady=5))
6170
self.rgrid(self.scrollbar, dict(row=0, column=3, sticky="ns"))
62-
self.rgrid(tk.Button(self, text=" Save config ", command=self.save_config, font=("Helvetica", 11)),
71+
self.rgrid(tk.Button(self, text=" Save default config ", command=self.save_config, font=("Helvetica", 11)),
6372
dict(row=5, column=0, ))
64-
self.rgrid(tk.Button(self, text=" Save config as ", command=self.save_config_as, font=("Helvetica", 11)),
73+
self.rgrid(tk.Button(self, text=" Save config as... ", command=self.save_config_as, font=("Helvetica", 11)),
6574
dict(row=5, column=1, ))
6675
self.rgrid(tk.Button(self, text=" Load config ", command=self.load_config, font=("Helvetica", 11)),
6776
dict(row=5, column=2, ))
68-
self.rgrid(tk.Button(self, text=" Run! ", command=self.btn_run, font=("Helvetica", 12)),
69-
dict(row=6, column=0, columnspan=3, ))
77+
self.rgrid(tk.Button(self, text=" Run! ", command=self.btn_run, font=("Helvetica", 12, "bold")),
78+
dict(row=6, column=0, columnspan=3, pady=5))
7079

7180
def recreate_widgets(self):
7281
self.rgrid(self.tree, dict(row=0, column=0, pady=5))
@@ -132,12 +141,24 @@ def btn_openf(self):
132141
"""
133142
db_path = filedialog.askopenfilename(filetypes=[("MS Access files", "*.mdb, *.accdb")])
134143
self.db_path.set(db_path)
135-
self.update_widgets()
144+
self.update_label_db()
145+
146+
def btn_sql_path(self):
147+
"""
148+
149+
"""
150+
sql_path = filedialog.asksaveasfilename(
151+
filetypes=[("SQL script files", "*.sql")],
152+
initialfile=self.get_output_sql_name()
153+
)
154+
self.sql_path.set(sql_path)
155+
self.update_label_sql()
136156

137157
def save_config(self, file_path='config.json'):
138158
config = {
139159
"info": "MS Access to SQL Export configuration file",
140160
"db_path": self.db_path.get(),
161+
"sql_path": self.sql_path.get(),
141162
"tree": self.tree.df.to_dict()
142163
}
143164
with open(file_path, 'w') as f:
@@ -157,8 +178,10 @@ def load_config(self, fpath='config.json', loadbyinit = False):
157178
try:
158179
with open(fpath, 'r') as f:
159180
config = json.load(f)
181+
160182
if config['info'] and (config['info'] == "MS Access to SQL Export configuration file"):
161183
self.db_path.set(config["db_path"])
184+
self.sql_path.set(config["sql_path"])
162185
self.update_widgets()
163186
self.tree.df = self.tree.df.from_dict(config["tree"])
164187
self.tree.rebuild_tree()
@@ -172,15 +195,20 @@ def load_config(self, fpath='config.json', loadbyinit = False):
172195
if fpath:
173196
self.load_config(fpath)
174197

175-
176-
def update_widgets(self):
198+
def update_label_db(self):
177199
self.db_connect()
178200
if self.check_permissions():
179-
self.label1['text'] = f"MS Access database for export: \"{self.db_path.get().split('/')[-1]}\""
180-
self.label1.update()
201+
self.label_db_path['text'] = f"MS Access database: \"{self.db_path.get().split('/')[-1]}\""
202+
# self.label_db_path.update()
181203
self.make_tree()
182204
self.recreate_widgets()
183205

206+
def update_label_sql(self):
207+
self.label_sql_path['text'] = f"Exported SQL script: {self.sql_path.get()}"
208+
209+
def update_widgets(self):
210+
self.update_label_db()
211+
self.update_label_sql()
184212

185213
def show_permission_warning(self):
186214
def open_link(event):
@@ -251,6 +279,16 @@ def get_referenced_tables(self, table_name):
251279

252280
return referenced_tables
253281

282+
def get_output_sql_name(self):
283+
db_path = self.db_path.get()
284+
if db_path:
285+
expath = db_path.split('/')
286+
fname = expath[-1]
287+
catalog = "/".join(expath[:-1])
288+
return f"{catalog}/{'_'.join(fname.split('.')[:-1])}.sql"
289+
else:
290+
return "AccessExport.sql"
291+
254292
def resolve_dependencies(self, export_list):
255293
export_set = set(export_list)
256294
added_tables = set()
@@ -268,7 +306,7 @@ def resolve_dependencies(self, export_list):
268306

269307
return list(export_set), list(added_tables)
270308

271-
def export_prepare(self):
309+
def export_prepare(self, output_sql_path = ""):
272310
df = self.tree.df
273311
export_list = df[df.iloc[:, 1] == "✔"]["table"].to_list()
274312
upload_list = df[df.iloc[:, 2] == "✔"]["table"].to_list()
@@ -283,10 +321,8 @@ def export_prepare(self):
283321
)
284322
if not messagebox.askyesno("Integrity Check", message):
285323
return False
286-
expath = self.db_path.get().split('/')
287-
fname = expath[-1]
288-
catalog = "/".join(expath[:-1])
289-
output_sql_path = f"{catalog}/{'_'.join(fname.split('.')[:-1])}.sql"
324+
if not output_sql_path:
325+
output_sql_path = self.get_output_sql_name()
290326

291327
return final_list, upload_list, output_sql_path
292328

0 commit comments

Comments
 (0)