-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_sql_connector.py
305 lines (247 loc) · 9.39 KB
/
my_sql_connector.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
import mysql.connector
def query_database(config, query, *parameter_tuple):
"""
Generator for queries to a database in MySQL server
:param config: dict
configs required for mysql.connector
For instance, {'user': 'my_user',
'password': 'my_pass',
'host': 'localhost',
'use_pure': False,
'database': 'english'}
:param query: String
A query e.g. "SELECT * FROM table"
:param parameter_tuple: tuple (optional)
Parameters which should replace %s in queries.
Number of parameters should be equal to number of %s in queries
:return:
generator
"""
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor()
cursor.execute(query, parameter_tuple)
for c in cursor:
yield c
cnx.commit()
cursor.close()
def modify_database(config, query):
"""
Function for queries to a database in MySQL server
It allows do all possible queries like ALTER, UPDATE, CREATE, etc
:param config: dict
see query_database(config, query) for details
:param query: String
a query like "ALTER...", "UPDATE...", "CREATE...", etc
:return: True if done
"""
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor()
cursor.execute(query)
cnx.commit()
cursor.close()
return True
# The functions for creation database 'english' and table 'en_voc' (The names are not specified!!!)
def create_database(config, database_name):
"""
Create a database in MySQL Server
:param config: dict
see query_database(config, query) for details
:param database_name: String
a single word, e.g. 'english'
:return: True if done
"""
try:
del config['database']
except KeyError:
pass
db_creation_query = ("CREATE DATABASE IF NOT EXISTS {} "
"CHARACTER SET utf8 COLLATE utf8_general_ci;").format(database_name)
return modify_database(config, db_creation_query)
def create_table(config, table_name):
"""
Create a table in database
:param config: dict
see query_database(config, query) for details
:param table_name: String
a single word, e.g. 'en_voc'
:return: True if done
"""
tab_creation_query = ("CREATE TABLE IF NOT EXISTS `{}` "
"(`id` INT(11) AUTO_INCREMENT, PRIMARY KEY (`id`), "
"`Eng` TEXT, "
"`engT` TEXT, "
"`EngEx` TEXT, "
"`Rus` TEXT, "
"`RusEx` TEXT, "
"`wellknown` TINYINT(1) NOT NULL DEFAULT 0, "
"`marked` DATE NOT NULL DEFAULT '1980-01-01', "
"`unmarked` DATE NOT NULL DEFAULT '1980-01-01', "
"`added` DATE NOT NULL,"
"`visible` TINYINT(1) NOT NULL DEFAULT 1);").format(table_name)
return modify_database(config, tab_creation_query)
# The functions for insert data:
def insert_rows(config, rows):
"""
Insert one or more rows into a table
:param config:
see query_database(config, query) for details
:param rows: a list or tuple of format
('Eng', 'engT', 'EngEx', 'Rus', 'RusEx', 'added')
:return: True if done
"""
values = ''
for row in rows:
eng, eng_t, eng_ex, rus, rus_ex = row
values = values + '("{}", "{}", "{}", "{}", "{}", CURDATE()), '.format(eng, eng_t, eng_ex, rus, rus_ex)
query = ("INSERT INTO en_voc(Eng, engT, EngEx, Rus, RusEx, added) "
"VALUES ") + values
query = query[:-2] + ";"
return modify_database(config, query)
def delete_doubles(config):
query = ("DELETE t1 FROM en_voc t1 "
"INNER JOIN en_voc t2 "
"WHERE "
"t1.id < t2.id AND "
"t1.Eng = t2.Eng;"
)
return modify_database(config, query)
# The functions for the second radio button field of main window
def learn_new():
"""
Specify a query for new words
:return: String
MySQL query
"""
return ("SELECT Eng, engT, EngEx, Rus, RusEx FROM en_voc "
"WHERE wellknown = 0 AND visible = 1 "
"AND Eng is NOT NULL "
"AND EngT IS NOT NULL "
"AND EngEx IS NOT NULL "
"AND Rus IS NOT NULL "
"AND RusEx IS NOT NULL;")
def from_within_last_n_days(n):
"""
Specify a query for words which marked as learned within past n days
:param n: int
number of days
:return: String
MySQL query
"""
return ("SELECT Eng, engT, EngEx, Rus, RusEx FROM en_voc "
"WHERE wellknown = 1 AND visible = 1 AND DATE_SUB(CURDATE(), "
"INTERVAL {} DAY) <= marked;").format(n)
def learn_all():
"""
Specify a query for all words
:return: String
MySQL query
"""
return ("SELECT Eng, engT, EngEx, Rus, RusEx FROM en_voc WHERE visible = 1 "
"AND Eng is NOT NULL "
"AND EngT IS NOT NULL "
"AND EngEx IS NOT NULL "
"AND Rus IS NOT NULL "
"AND RusEx IS NOT NULL;")
def repeat_within_arbitrary_interval():
"""
Obsoleted
Required to pass a tuple (start_date, end_date) to query_database()
as a *parameter_tuple. It is rather complicated
:return: MySQL query
"""
return ("SELECT Eng, engT, EngEx, Rus, RusEx FROM en_voc "
"WHERE wellknown = 1 AND visible = 1 AND marked BETWEEN %s AND %s;")
def random_rows(query, limit):
"""
Return random rows in table limited by a number.
:param query: String
specific MySQL query
:param limit: int
number of rows required
:return: String
MySQL query
"""
return query.replace(";", " ") + "ORDER BY RAND() LIMIT {};".format(limit)
# Functions for StatLabel
def count_wellknown(config):
"""
Count wellknown words in table
:param config: dict
see query_database(config, query) for details
:return: int
Number of wellknown words in table except NULL values.
"""
query = ("SELECT COUNT(id) FROM en_voc WHERE wellknown = 1 "
"AND visible = 1 "
"AND Eng is NOT NULL "
"AND EngT IS NOT NULL "
"AND EngEx IS NOT NULL "
"AND Rus IS NOT NULL "
"AND RusEx IS NOT NULL;")
return next(query_database(config, query))[0]
def count_new(config):
"""
Count new words in table
:param config: dict
see query_database(config, query) for details
:return: int
Number of new words in table except NULL values.
"""
query = ("SELECT COUNT(id) FROM en_voc WHERE wellknown = 0 "
"AND visible = 1 "
"AND Eng is NOT NULL "
"AND EngT IS NOT NULL "
"AND EngEx IS NOT NULL "
"AND Rus IS NOT NULL "
"AND RusEx IS NOT NULL;")
return next(query_database(config, query))[0]
# Functions for mark and unmark words
def is_marked(config, row):
"""
Check whether the word is marked
:param config: dict
see query_database(config, query) for details
:param row: tuple
(Eng, EngT, EngEx, Rus, RusEx)
:return: True if word is marked as wellknown. False otherwise
"""
query = ('SELECT wellknown FROM en_voc WHERE Eng = "{}" '
'AND Rus = "{}" AND visible = 1;').format(row[0], row[3])
return next(query_database(config, query))[0] == 1
def mark_as_learned(config, row):
query = ('UPDATE en_voc SET wellknown = 1, marked = CURDATE() '
'WHERE Eng = "{}" AND Rus = "{}" AND visible = 1;').format(row[0], row[3])
return modify_database(config, query)
def mark_as_unlearned(config, row):
query = ('UPDATE en_voc SET wellknown = 0, unmarked = CURDATE() '
'WHERE Eng = "{}" AND Rus = "{}" AND visible = 1;').format(row[0], row[3])
return modify_database(config, query)
def hide_word_forever(config, row):
query = ('UPDATE en_voc SET visible = 0 '
'WHERE Eng = "{}" AND Rus = "{}";').format(row[0], row[3])
return modify_database(config, query)
def undo_hiding_forever_for_all(config):
return modify_database(config, "UPDATE en_voc SET visible = 1;")
if __name__ == '__main__':
tmp_config = {'user': 'admin',
'password': 'Zvezda12/',
'host': 'localhost',
'use_pure': False,
'database': 'english'}
tmp_query = ("SELECT * FROM en_voc "
"WHERE Eng is NOT NULL "
"AND EngT IS NOT NULL "
"AND EngEx IS NOT NULL "
"AND Rus IS NOT NULL "
"AND RusEx IS NOT NULL;")
query1 = ("SELECT Eng, engT, EngEx, Rus, RusEx FROM en_voc "
"WHERE wellknown = 0 and added BETWEEN %s AND %s;")
query2 = ("SELECT Eng, engT, EngEx, Rus, RusEx FROM en_voc "
"LIMIT 5;")
rows_gen = query_database(tmp_config, query2)
rows_ = [a for a in rows_gen]
create_database(tmp_config, 'test')
tmp_config['database'] = 'test'
modify_database(tmp_config, "DROP TABLE en_voc;")
create_table(tmp_config, 'en_voc')
print(insert_rows(tmp_config, rows_))