Repository navigation
Expand file tree
/
Copy pathui.py
More file actions
393 lines (349 loc) · 15.7 KB
/
Copy pathui.py
File metadata and controls
393 lines (349 loc) · 15.7 KB
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
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 10 10:46:28 2017
@author: jdubbeldam
"""
import sys
from PyQt5.QtWidgets import (QMainWindow, QDesktopWidget, QFileDialog, QDialog,
QRadioButton, QPushButton, QCheckBox, QTextEdit,
QButtonGroup, QLineEdit, QApplication)
from classes import Query
#Global constants defining the amount of vertical space between those specific items.
PUSHBUTTONHEIGHT = 40
RADIOBUTTONHEIGHT = 20
CHECKBOXHEIGHT = 20
class CreateQueryInterface(QMainWindow, Query):
"""
This class provides a GUI to the Query class imported from classes.py.
Upon initialization creates a filedialog to select a JSON file to serve as
context (Universe). These files have their extension changed to .uni to make
filtering easier. The generated files will be in one specific directory, so
this location is hardcoded.
"""
def __init__(self):
try:
super().__init__(filename=QFileDialog.getOpenFileName(
None,
'Select universe',
'R:/NL/Database Marketing/R library/SQL builder/Universes',
'Universes (*.uni)')[0])
except FileNotFoundError:
sys.exit()
self.init_ui()
def init_ui(self):
"""
Handles generating the main interface. This consists of a column of
buttons, which get enabled when they become relevant, and a textbox
from where the output from compile_query can be copied.
"""
self.table_button = QPushButton('Select tables', self)
self.table_button.move(50, 50)
self.table_button.clicked.connect(self.pick_tables)
self.table_button.setEnabled(True)
self.column_button = QPushButton('Select columns', self)
self.column_button.move(50, 100)
self.column_button.clicked.connect(self.pick_table_for_columns)
self.column_button.setEnabled(False)
self.join_button = QPushButton('Specify joins', self)
self.join_button.move(50, 150)
self.join_button.clicked.connect(self.specify_joins)
self.join_button.setEnabled(False)
self.where_button = QPushButton('Specify wheres', self)
self.where_button.move(50, 200)
self.where_button.clicked.connect(self.specify_where)
self.where_button.setEnabled(False)
self.preset_button = QPushButton('Presets', self)
self.preset_button.move(50, 250)
self.preset_button.clicked.connect(self.select_presets)
self.preset_button.setEnabled(True)
self.compile_button = QPushButton('Compile', self)
self.compile_button.move(50, 300)
self.compile_button.clicked.connect(self.print_query)
self.compile_button.setEnabled(False)
self.output = QTextEdit(self)
self.output.move(200, 50)
self.output.resize(400, 400)
self.output.setReadOnly(True)
self.resize(650, 500)
self.setWindowTitle('SQL Builder')
self.center()
self.raise_()
self.activateWindow()
self.show()
def center(self):
"""
Handles the position of the main window.
"""
screen = QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width()-size.width())/2,
(screen.height()-size.height())/2)
def pick_tables(self):
"""
Creates a dialog where tables can be activated. self.active_tables
contains a list of tables already activated; the checked state is imported
from there. If the table has been activated by a preset, its button will
be disabled, to prevent messing with the preset.
"""
dialog = QDialog()
buttons = []
for index, table in zip(range(len(self.tables)), self.tables):
buttons.append(QCheckBox(dialog))
buttons[index].setText(table)
buttons[index].move(10, 10 + index * CHECKBOXHEIGHT)
buttons[index].clicked[bool].connect(self.activate_table)
buttons[index].setChecked(table in self.active_tables)
try:
buttons[index].setEnabled(table not in self.tables_added_by_preset)
except KeyError:
pass
dialog.setWindowTitle('Pick tables')
dialog.exec_()
def activate_table(self, pressed):
"""
When a checkbox created by pick_tables is changed, the related table is
added or removed from self.active_tables. This also activates more
relevant buttons in the main window.
"""
source = self.sender()
if pressed:
self.add_tables(source.text())
else:
self.remove_tables(source.text())
self.column_button.setEnabled(True)
self.compile_button.setEnabled(True)
if len(self.active_tables) > 1:
self.join_button.setEnabled(True)
def pick_table_for_columns(self):
"""
Selecting columns goes in two steps, first the table is selected, then
another dialog is displayed with checkboxes for the columns. This method
handles the table selection.
"""
dialog = QDialog()
buttons = []
tables = self.active_tables
for index, table in zip(range(len(tables)), tables):
buttons.append(QPushButton(table, dialog))
buttons[index].move(10, 10 + index * PUSHBUTTONHEIGHT)
buttons[index].clicked.connect(self.pick_columns)
dialog.setWindowTitle('Select tables')
dialog.exec_()
def pick_columns(self):
"""
When a table is selected, its name is stored in an attribute so it can
be called in subsequent functions. This method enumerates all columns
available in the selected table and creates checkboxes.
"""
source = self.sender()
selected_table = source.text()
dialog = QDialog()
buttons = []
columns = self.tables[selected_table]['Columns']
maximal_name_length = max([len(column) for column in columns])
for index, column in zip(range(len(columns)),
sorted(columns)):
buttons.append(QCheckBox(dialog))
buttons[index].setText(column)
buttons[index].move(10 + (CHECKBOXHEIGHT + 6 * maximal_name_length) * (index//45),
10 + index%45 * CHECKBOXHEIGHT)
buttons[index].clicked[bool].connect(self.activate_columns)
buttons[index].selected_table = selected_table
buttons[index].setChecked(column in self.active_columns[selected_table])
dialog.setWindowTitle('Pick columns')
dialog.resize(20 + (CHECKBOXHEIGHT + 6 * maximal_name_length) * (len(columns)//45 + 1),
20 + min(len(columns), 45) * CHECKBOXHEIGHT)
dialog.exec_()
def activate_columns(self, pressed):
"""
When a checkbox created by pick_columns is changed, it is added or removed
from the dictionary of activated columns. Enables another button.
"""
source = self.sender()
selected_table = source.selected_table
if pressed:
self.add_columns(selected_table, source.text())
self.where_button.setEnabled(True)
else:
self.remove_columns(selected_table, source.text())
def specify_joins(self):
"""
Dialog to set the way tables are joined. First the required joins are
computed by calling the find_joins method. Each of the joins gets a button
which creates another dialog.
"""
joins = self.find_joins()
dialog = QDialog()
options = []
max_join_string = max([len(join[0] + ' on ' + join[1]) for join in joins])
for index, join in zip(range(len(joins)), joins):
options.append(QPushButton(join[0] + ' on ' + join[1], dialog))
options[index].move(10, 10 + index * PUSHBUTTONHEIGHT)
options[index].clicked.connect(self.pick_join_settings)
options[index].joinTag = join
dialog.setWindowTitle('Select a join')
dialog.resize(20 + 6 * max_join_string, 20 + PUSHBUTTONHEIGHT * len(joins))
dialog.exec_()
def pick_join_settings(self):
"""
Given a join, the default join setting is imported from the Universe.
Then the join_settings dictionary is checked to see if another join has
already been set or not. If not, the default is used. The four options
are shown as radiobuttons with the current setting activated.
"""
source = self.sender()
table_tuple = source.joinTag
try:
how = self.tables[table_tuple[0]]['Joins'][table_tuple[1]][1]
except TypeError:
table_tuple = (table_tuple[1], table_tuple[0])
how = self.tables[table_tuple[0]]['Joins'][table_tuple[1]][1]
selected_join = table_tuple
try:
how = self.how_to_join[table_tuple]
except KeyError:
pass
dialog = QDialog()
buttongroup = QButtonGroup(dialog)
buttons = []
for index, setting in zip(range(4), ['inner', 'left', 'right', 'full']):
buttons.append(QRadioButton(dialog))
buttons[index].setText(setting)
buttons[index].setChecked(setting == how)
buttons[index].move(20, 20 + index * RADIOBUTTONHEIGHT)
buttons[index].clicked.connect(self.adjust_join_settings)
buttons[index].selected_join = selected_join
buttongroup.addButton(buttons[index])
dialog.setWindowTitle('Pick a join type')
dialog.exec_()
def adjust_join_settings(self):
"""
Sets the join setting to the selected radiobutton.
"""
source = self.sender()
selected_join = source.selected_join
self.how_to_join[selected_join] = source.text()
def specify_where(self):
"""
Series of dialogs to set where-statements. Where-statements can be
configured on columns, so there first is a dialog to select one of the
activated tables, and then one to select one of the activated columns.
"""
dialog3 = QDialog()
buttons = []
for index, table in zip(range(len(self.active_tables)),
self.active_tables):
buttons.append(QPushButton(table, dialog3))
buttons[index].move(10, 10 + index * PUSHBUTTONHEIGHT)
buttons[index].clicked.connect(self.pick_column_for_where)
buttons[index].setEnabled(len(self.active_columns[table]))
dialog3.setWindowTitle('Select a table')
dialog3.exec_()
def pick_column_for_where(self):
"""
After a table has been selected, this enumerates the activated columns
and allows column selection.
"""
source = self.sender()
selected_table = source.text()
dialog = QDialog()
buttons = []
columns = self.active_columns[selected_table]
maximal_name_length = max([len(column) for column in columns])
for index, column in zip(range(len(columns)),
sorted(columns)):
buttons.append(QPushButton(dialog))
buttons[index].setText(column)
buttons[index].move(10 + 6 * maximal_name_length * (index//45),
10 + index%45 * PUSHBUTTONHEIGHT)
buttons[index].clicked.connect(self.specify_where_text)
buttons[index].selected_table = selected_table
buttons[index].parent_dialog = dialog
dialog.setWindowTitle('Select a column')
dialog.resize(20 + (CHECKBOXHEIGHT + 6 * maximal_name_length) * (len(columns)//45 + 1),
20 + min(len(columns), 45) * CHECKBOXHEIGHT)
dialog.exec_()
def specify_where_text(self):
"""
Creates a line-edit dialog where "table.column = " is already filled in.
This is (very) vulnerable to SQL-injection, but 1) this is for internal
use only and 2) the generated SQL is printed, not sent to the database.
"""
source = self.sender()
selected_column = source.text()
selected_table = source.selected_table
dialog = QDialog()
where_editor = QLineEdit(dialog)
try:
where_editor.setText(self.where[(selected_table, selected_column)])
except KeyError:
where_editor.setText(selected_table + '.' + selected_column + ' = ')
where_editor.move(20, 20)
where_editor.resize(6 * len(where_editor.text()) + 200, 20)
confirm_button = QPushButton('Confirm', dialog)
confirm_button.move(20, 60)
confirm_button.clicked.connect(self.submit_where_text)
confirm_button.selected_column = selected_column
confirm_button.selected_table = selected_table
confirm_button.linked_editor = where_editor
confirm_button.parent_dialog = [dialog, source.parent_dialog]
dialog.setWindowTitle('Specify where statement')
dialog.resize(6 * len(where_editor.text()) + 240, 100)
dialog.exec_()
def submit_where_text(self):
"""
Handles the submit button, stores the where-string and closes the dialogs.
"""
source = self.sender()
editor = source.linked_editor
selected_table = source.selected_table
selected_column = source.selected_column
self.add_where(editor.text(), selected_table, selected_column)
for dialog in source.parent_dialog:
dialog.close()
def select_presets(self):
"""
Presets are predefined where-statements that can be added in a few clicks.
They add the relevant table to the list of activated tables and add
a where statement. For simplicity, when added, a preset can't be disabled.
"""
presets = self.presets.keys()
dialog = QDialog()
buttons = []
for index, preset in zip(range(len(presets)), presets):
buttons.append(QCheckBox(dialog))
buttons[index].setText(preset)
buttons[index].setEnabled(preset not in self.active_presets)
buttons[index].setChecked(preset in self.active_presets)
buttons[index].clicked[bool].connect(self.activate_preset)
buttons[index].move(10, 10 + index * CHECKBOXHEIGHT)
dialog.setWindowTitle('Select presets')
dialog.exec_()
def activate_preset(self):
"""
Activates preset. Enables buttons similar to activating a table, as this
also activates presets. Keeps track of tables added by presets.
"""
source = self.sender()
source.setEnabled(False)
self.add_preset(source.text())
self.column_button.setEnabled(True)
self.compile_button.setEnabled(True)
if len(self.active_tables) > 1:
self.join_button.setEnabled(True)
def print_query(self):
"""
Compiles the query. The activated elements are added to the query and
the compile_query method is called. The result is printed in the Textdisplay.
"""
self.output.setText(self.compile_query())
def main():
"""
Deploys the app
"""
app = QApplication([])
interface = CreateQueryInterface()
sys.exit(app.exec_())
print(interface)
if __name__ == '__main__':
main()