-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcmysql.py
329 lines (303 loc) · 13.1 KB
/
cmysql.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#encoding=utf-8
import time
import pymysql
import sqlalchemy
import const as ct
import pandas as pd
import MySQLdb as db
from base.clog import getLogger
from common import create_redis_obj
from warnings import filterwarnings
from sqlalchemy import create_engine
filterwarnings('error', category = db.Warning)
ALL_DATABASES = 'all_databases'
ALL_TRIGGERS = 'all_triggers'
logger = getLogger(__name__)
class CMySQL:
def __init__(self, dbinfo, dbname = 'stock', iredis = None):
self.dbinfo = dbinfo
self.dbname = dbname
self.redis = create_redis_obj() if iredis is None else iredis
self.engine = create_engine("mysql://%s:%s@%s/%s?charset=utf8" % (self.dbinfo['user'], self.dbinfo['password'], self.dbinfo['host'], self.dbname), pool_size=0 , max_overflow=-1, pool_recycle=20, pool_timeout=5, connect_args={'connect_timeout': 3})
def __del__(self):
self.redis = None
self.engine = None
def changedb(self, dbname = 'stock'):
self.dbname = dbname
self.engine = create_engine("mysql://%s:%s@%s/%s?charset=utf8" % (self.dbinfo['user'], self.dbinfo['password'], self.dbinfo['host'], self.dbname), pool_size=0 , max_overflow=-1, pool_recycle=20, pool_timeout=5, connect_args={'connect_timeout': 3})
def get_all_databases(self):
if self.redis.exists(ALL_DATABASES):
return set(dbname.decode() for dbname in self.redis.smembers(ALL_DATABASES))
else:
all_dbs = self._get_all_databses()
for _db in all_dbs: self.redis.sadd(ALL_DATABASES, _db)
return all_dbs
def is_exists(self, table_name):
if self.redis.exists(self.dbname):
return self.redis.sismember(self.dbname, table_name)
else:
all_tables = self._get('SHOW TABLES', 'Tables_in_{}'.format(self.dbname.lower()))
if table_name in all_tables:
self.redis.sadd(self.dbname, table_name)
return True
return False
def get_all_tables(self):
if self.redis.exists(self.dbname):
return set(table.decode() for table in self.redis.smembers(self.dbname))
else:
all_tables = self._get('SHOW TABLES', 'Tables_in_%s' % self.dbname.lower())
for table in all_tables: self.redis.sadd(self.dbname, table)
return all_tables
def get_all_triggers(self):
if self.redis.exists(ALL_TRIGGERS):
return set(table.decode() for table in self.redis.smembers(ALL_TRIGGERS))
else:
all_triggers = self._get('SHOW TRIGGERS', 'Trigger')
for trigger in all_triggers: self.redis.sadd(ALL_TRIGGERS, trigger)
return all_triggers
def _get(self, sql, key):
res = False
for i in range(ct.RETRY_TIMES):
try:
conn = self.engine.connect()
df = pd.read_sql(sql, conn)
res = True
except sqlalchemy.exc.OperationalError as e:
logger.warning(e)
except Exception as e:
logger.error(e)
finally:
if 'conn' in dir(): conn.close()
if True == res:return set(df[key].tolist()) if not df.empty else set()
logger.error("get all info failed afer try %d times" % ct.RETRY_TIMES)
return set()
def create_update_cols_query(self, table, mdict, kdict):
'''
only update some columns in dataframe in mysql
'''
query = ''
placeholder = ', '.join('{}=%s'.format(k) for k in mdict)
conditon = ', '.join('{}=%s'.format(k) for k in kdict)
query = "UPDATE {} SET {} WHERE {};".format(table, placeholder, condition)
query.split()
query = ' '.join(query.split())
return query
def update_cols(self, df, table, columns, pri_cols):
#only update entire columns
res = True
try:
conn = db.connect(host=self.dbinfo['host'],user=self.dbinfo['user'],passwd=self.dbinfo['password'],db=self.dbname,charset=ct.UTF8,connect_timeout=3)
cur = conn.cursor()
key_values = df[pri_cols].to_dict(orient = 'records')
insert_values = df[columns].to_dict(orient = 'records')
for row in insert_values:
sql = self.create_update_cols_query(table, columns, pri_cols)
cur.execute(sql, row)
conn.commit()
cur.execute(sql, row)
conn.commit()
except Exception as e:
logger.info(e)
if 'conn' in dir(): conn.rollback()
res = False
finally:
if 'curosr' in dir(): cursor.close()
if 'conn' in dir(): conn.close()
return res
def create_upsert_query(self, table, columns, pri_keys):
query = ''
cols = ', '.join(['{}'.format(col) for col in columns])
placeholder = ', '.join(['%({})s'.format(col) for col in columns])
updates = ', '.join(['{}=VALUES({})'.format(col, col) for col in columns])
query = "INSERT INTO {} ({}) VALUES ({}) ON DUPLICATE KEY UPDATE {};".format(table, cols, placeholder, updates)
query.split()
query = ' '.join(query.split())
return query
def upsert(self, df, table, pri_keys = list()):
#if data in mysql, udate to new value,
#if not in mysql, just append to mysql
columns = df.columns.tolist()
insert_items = df.to_dict(orient = 'records')
sql = self.create_upsert_query(table, columns, pri_keys)
return self.executemany(sql, params = insert_items)
def delsert(self, df, table):
if self.exec_sql("truncate table %s;" % table):
logger.debug("delsert %s for %s", table, self.dbname)
return self.set(df, table)
else:
logger.error("delsert %s for db %s failed", table, self.dbname)
return False
def executemany(self, sql, params = None):
res = True
try:
conn = db.connect(host = self.dbinfo['host'], user = self.dbinfo['user'], passwd = self.dbinfo['password'], db = self.dbname, charset = ct.UTF8, connect_timeout = 30)
cur = conn.cursor()
cur.executemany(sql, params)
conn.commit()
except Exception as e:
logger.info(e)
if 'conn' in dir(): conn.rollback()
res = False
finally:
if 'curosr' in dir(): cursor.close()
if 'conn' in dir(): conn.close()
return res
def create_update_query(self, table):
query = ''
cols = ', '.join(['{}'.format(col) for col in columns])
placeholder = ', '.join(['%({})s'.format(col) for col in columns])
updates = ', '.join(['{}=%({})s'.format(col, col) for col in columns])
query = "UPDATE {} SET {} WHERE {} IN ({0});".format(table, cols, placeholder, updates)
query.split()
query = ' '.join(query.split())
return query
def update(self, df, table, columns, pri_keys):
#first remove the duplicated values, then add the new value in df
update_items = df.to_dict(orient = 'records')
query = self.create_update_query(table)
return self.executemany(query, params = update_items)
def set(self, data_frame, table):
res = False
for i in range(ct.RETRY_TIMES):
try:
conn = self.engine.connect()
data_frame.to_sql(table, conn, if_exists = ct.APPEND, index=False)
res = True
except sqlalchemy.exc.OperationalError as e:
logger.debug(e)
except sqlalchemy.exc.ProgrammingError as e:
logger.debug(e)
except sqlalchemy.exc.IntegrityError as e:
logger.debug("duplicated item:%s" % e)
res = True
finally:
if 'conn' in dir(): conn.close()
if True == res: return True
logger.error("write to db:%s, table:%s failed afer try %d times" % (self.dbname, table, ct.RETRY_TIMES))
return res
def get(self, sql, retry_times = ct.RETRY_TIMES):
res = False
for i in range(retry_times):
try:
conn = self.engine.connect()
data = pd.read_sql_query(sql, conn)
res = True
except sqlalchemy.exc.OperationalError as e:
logger.debug(e)
if 'conn' in dir(): conn.close()
except Exception as e:
logger.error(e)
if 'conn' in dir(): conn.close()
if True == res: return data
time.sleep(i)
logger.error("{} {} failed afer try {} times".format(self.dbname, sql, retry_times))
return None
def exec_sql(self, sql, params = None, retry_times = ct.RETRY_TIMES):
hasSucceed = False
for i in range(retry_times):
try:
conn = db.connect(host=self.dbinfo['host'],user=self.dbinfo['user'],passwd=self.dbinfo['password'],db=self.dbname,charset=ct.UTF8,connect_timeout=3)
cur = conn.cursor()
cur.execute(sql, params)
conn.commit()
hasSucceed = True
except db.IntegrityError as s:
logger.debug("warning:%s" % str(s))
hasSucceed = True
except db.Warning as w:
if 'conn' in dir(): conn.rollback()
logger.debug("warning:%s" % str(w))
hasSucceed = True
except db.Error as e:
if 'conn' in dir(): conn.rollback()
logger.debug("error:%s" % str(e))
finally:
if 'cur' in dir(): cur.close()
if 'conn' in dir(): conn.close()
if hasSucceed: return True
if retry_times > 1:
time.sleep(ct.SHORT_SLEEP_TIME * retry_times)
logger.error("%s failed" % sql)
return False
def register(self, sql, register):
if self.exec_sql(sql):
self.redis.sadd(ALL_TRIGGERS, register)
return True
return False
def create(self, sql, table):
if self.exec_sql(sql):
return self.redis.sadd(self.dbname, table)
return False
def delete_item(self, table, cdate):
sql = 'delete from %s where date = "%s"' % (table, cdate)
self.redis.srem(table, cdate)
self.exec_sql(sql)
def delete(self, table):
sql = 'drop table %s' % table
self.exec_sql(sql)
self.redis.srem(self.dbname, table)
self.redis.delete(table)
return True
def _get_all_databses(self):
db_list = list()
try:
conn = db.connect(host=self.dbinfo['host'], user=self.dbinfo['user'], passwd=self.dbinfo['password'],connect_timeout=3,read_timeout=5,write_timeout=10)
cursor = conn.cursor()
cursor.execute("show databases;")
db_tuple = cursor.fetchall()
cursor.close()
conn.commit()
db_list = [tmp_db[0] for tmp_db in db_tuple]
except Exception as e:
if 'conn' in dir(): conn.rollback()
finally:
if 'curosr' in dir(): cursor.close()
if 'conn' in dir(): conn.close()
return db_list
def delete_db(self, dbname = None):
if dbname is None:dbname = self.dbname
try:
conn = db.connect(host=self.dbinfo['host'], user=self.dbinfo['user'], passwd=self.dbinfo['password'], charset=ct.UTF8)
cursor = conn.cursor()
cursor.execute("drop database if exists %s" % dbname)
cursor.close()
conn.commit()
except Exception as e:
if 'conn' in dir(): conn.rollback()
finally:
if 'curosr' in dir(): cursor.close()
if 'conn' in dir(): conn.close()
for tab in set(tdb.decode() for tdb in self.redis.smembers(dbname)):
self.redis.delete(tab)
self.redis.delete(dbname)
self.redis.srem(ALL_DATABASES, dbname)
return True
def create_db(self, dbname = None):
if dbname is None: dbname = self.dbname
if self.redis.exists(ALL_DATABASES) and self.redis.sismember(ALL_DATABASES, dbname):
return True
res = False
try:
conn = db.connect(host=self.dbinfo['host'], user=self.dbinfo['user'], passwd=self.dbinfo['password'], charset=ct.UTF8)
cursor = conn.cursor()
cursor.execute("create database if not exists %s" % dbname)
cursor.close()
conn.commit()
res = True
except Exception as e:
if e.args[0] == 1007 and e.args[1].endswith("database exists"):
res = True
else:
res = False
if 'conn' in dir(): conn.rollback()
finally:
if 'curosr' in dir(): cursor.close()
if 'conn' in dir(): conn.close()
if res == True: self.redis.sadd(ALL_DATABASES, dbname)
return res
if __name__ == '__main__':
cmy = CMySQL(ct.DB_INFO, dbname = 'plate')
sql = 'select * from valuation;'
df = cmy.get(sql)
for cdate, data in df.groupby(df.date):
if len(data) != 6: print(cdate)