-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconView.py
More file actions
352 lines (320 loc) · 10.5 KB
/
Copy pathconView.py
File metadata and controls
352 lines (320 loc) · 10.5 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
import datetime
from PyInquirer import prompt
from view import View
def create_add_cols_prompt(columns_array):
add_pr = [
]
for element in columns_array:
add_pr.append({
'type': 'input',
'name': element,
'message': 'Enter value for {0}'.format(element)
})
return add_pr
def text_to_word_search_arr(text_piece):
word_str = '('
for word in text_piece:
word_str += word
if word != text_piece[-1]:
word_str += ' & '
else:
word_str += ')'
print word_str
return word_str
class concrete_view(View):
def __init__(self):
pass
db_schema = 'none'
def launch_joint_search_prompt(self):
is_active = prompt([{
'type': 'input',
'name': 'is_active',
'message': 'Is player active'
}])['is_active']
team_name = prompt([{
'type': 'input',
'name': 'team_name',
'message': 'Team name'
}])['team_name']
print is_active
print team_name
return {'player_is_active': is_active, 'team_name': team_name}
def set_data(self, schema_data):
self.db_schema = schema_data
self.fill_table_prompt()
def fill_table_prompt(self):
for element in self.db_schema:
self.tables[0]['choices'].insert(0, element)
def launch_insert_prompt(self, table_chosen):
add_prompt = self.create_add_prompt(table_chosen)
data_dict = prompt(add_prompt)
return data_dict
def launch_update_prompt(self, table_chosen):
upd_selection_prompt = self.create_select_prompt(table_chosen)
upd_by = prompt(upd_selection_prompt)['sel_pr']
if upd_by != 'All':
condition = prompt([{
'type': 'input',
'name': 'cond',
'message': 'enter the condition for selecting by {0}'.format(upd_by)
}])['cond']
condition = upd_by + condition
else:
condition = 'True'
upd_cols_prompt = self.create_columns_chk_prompt(table_chosen)
cols = prompt(upd_cols_prompt)['col_pr']
new_data_prompt = create_add_cols_prompt(cols)
columns = prompt(new_data_prompt)
return {'columns': columns, 'condition': condition}
def launch_delete_prompt(self, table_chosen):
delete_prompt = self.create_delete_prompt(table_chosen)
del_by = prompt(delete_prompt)['del_pr']
if del_by != 'All':
condition = prompt([{
'type': 'input',
'name': 'cond',
'message': 'enter the condition for deleting by {0}'.format(del_by)
}])['cond']
data = del_by + condition
else:
data = 'True'
return data
def launch_select_prompt(self, table_chosen):
select_prompt = self.create_select_prompt(table_chosen)
sel_by = prompt(select_prompt)['sel_pr']
print sel_by
cond_data = []
if not sel_by:
condition_string = 'False'
elif 'All' not in sel_by:
for x in range(0, len(sel_by), 1):
condition = prompt([{
'type': 'input',
'name': 'cond',
'message': 'enter the condition for selecting by {0}'.format(sel_by[x])
}])['cond']
cond_data.append({'row': sel_by[x], 'cond': condition})
condition_string = '('
for cond in cond_data:
condition_string += cond['row'] + " " + cond['cond']
if cond != cond_data[-1]:
condition_string += ' AND '
condition_string += ')'
else:
condition_string = 'True'
limit = prompt([{
'type': 'input',
'name': 'limit',
'message': 'limit number of rows read'
}])['limit']
end_data = {
'cond': condition_string,
'limit': limit
}
return end_data
def launch_text_search_prompt(self, table_chosen):
col_pr = self.create_columns_prompt(table_chosen)
column_to_search = prompt(col_pr)['col_pr']
type = prompt([{
'type': 'list',
'name': 'type',
'message': 'Choose full text search type',
'choices': [
{
'name': 'Is not in text '
},
{
'name': 'Is in'
}
]
}])['type']
print type
text_piece = None
if type == 'Is not in text':
text_piece = prompt([{
'type': 'input',
'name': 'txt',
'message': 'Enter text to not include in search'
}])['txt']
else:
text_piece = prompt([{
'type': 'input',
'name': 'txt',
'message': 'Enter text to search'
}])['txt']
text_piece = text_piece.split()
text_piece = text_to_word_search_arr(text_piece)
data = {'in': type == 'Is in', 'col': column_to_search, 'textp': text_piece}
return data
def launch_operation_prompt(self):
return prompt(self.operations)['operation']
def launch_table_prompt(self):
return prompt(self.tables)['table']
def create_delete_prompt(self, table_name):
schema = self.db_schema[table_name]
del_pr = [
{
'type': 'list',
'message': 'choose deletion method',
'name': 'del_pr',
'choices': [
{
'name': 'All'
},
{
'name': 'id'
}
]
}
]
for col in schema:
obj = {
'name': col
}
del_pr[0]['choices'].append(obj)
return del_pr
def create_add_prompt(self, table_name):
schema = self.db_schema[table_name]
add_pr = [
]
for element in schema:
add_pr.append({
'type': 'input',
'name': element,
'message': 'Enter value for {0}'.format(element)
})
return add_pr
def create_columns_chk_prompt(self, table_name):
schema = self.db_schema[table_name]
col_pr = [
{
'type': 'checkbox',
'message': 'select columns to update',
'name': 'col_pr',
'choices': [
]
}
]
for elem in schema:
col_pr[0]['choices'].append({
'name': elem
})
return col_pr
def create_columns_prompt(self, table_name):
schema = self.db_schema[table_name]
col_pr = [
{
'type': 'list',
'message': 'select column for fulltext search',
'name': 'col_pr',
'choices': [
]
}
]
for elem in schema:
col_pr[0]['choices'].append({
'name': elem
})
return col_pr
def create_select_prompt(self, table_name):
schema = self.db_schema[table_name]
sel_pr = [
{
'type': 'checkbox',
'message': 'choose selection method',
'name': 'sel_pr',
'choices': [
{
'name': 'All',
'data': ''
},
{
'name': 'id'
}
]
}
]
for col in schema:
obj = {
'name': col
}
sel_pr[0]['choices'].append(obj)
return sel_pr
tables = [
{
'type': 'list',
'message': 'Select table',
'name': 'table',
'choices': [
{
'name': 'Exit'
}
]
}
]
operations = [
{
'type': 'list',
'message': 'Select operation',
'name': 'operation',
'choices': [
{
'name': 'Preview'
},
{
'name': 'Joint search'
},
{
'name': 'Text search'
},
{
'name': 'Add'
},
{
'name': 'Add Random'
},
{
'name': 'Update'
},
{
'name': 'Delete'
},
{
'name': 'Back'
}
]
}
]
def display_select_data(self, data, table_name):
print('Data from table: {0}'.format(table_name))
print('-----------------------------')
columns_str = 'id '
for elem in self.db_schema[table_name]:
columns_str += elem + ' '
print columns_str
print('-----------------------------')
for item in data:
itemstr = ''
for col in item:
if type(col) == datetime.date:
textdata = col.strftime("%d-%m-%Y")
else:
textdata = col.__repr__()
itemstr += textdata + ' | '
print itemstr
print('-----------------------------')
def display_delete_data(self, data, table_name):
print('-----------------------------')
print('Deleted items where {0} from table: {1}'.format(data, table_name))
print('-----------------------------')
def display_insert_data(self, data, table_name):
print('-----------------------------')
print('Inserted {0} into table: {1}'.format(data, table_name))
print('-----------------------------')
def display_update_data(self, columns, condition, table_name):
print('-----------------------------')
print('Updated items {0} to {1} where {2} in table: {3}'.format(columns.keys(),
columns.values(),
condition,
table_name))
print('-----------------------------')