forked from casual-silva/practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_encap.py
970 lines (857 loc) · 32.3 KB
/
db_encap.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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
# -*- encoding: utf-8 -*-
__author__ = 'qaulau'
import time
import Util as util
#比较式
_comparison = {'eq':'=','neq':'<>','gt':'>','egt':'>=','lt':'<','elt':'<=','notlike':'NOT LIKE','notin':'NOT IN','!like':'NOT LIKE'}
def implode_field_value(array, glue=',', fields=None, encape='`'):
'''
连接数组中的字段及值转为字符串
@param dict or list array :需要转换的数据
@param string glue : 连接字符
@return 处理后的sql字符串语句
2014/09/12 添加exp,可用于直接执行SQL函数和语句如字段递增等; example: insert('order',data = {'num':('exp','num + 1')})
'''
args = []
if isinstance(array, list):
array = dict(array)
sql = comma = ''
for k in array:
if isinstance(array[k], (list, tuple)):
if fields and k not in fields:
continue
opt = str(array[k][0]).strip().lower()
# Fix: {'goods_id': (1,)}
if len(array[k]) == 1:
sql += "%s%s%s%s = %%s" % (comma, encape, k, encape)
args.append(array[k][0])
elif opt == 'exp':
if glue == ',':
sql += "%s%s%s%s = %s" % (comma, encape, k, encape, array[k][1])
else:
sql += "%s(%s%s%s %s)" % (comma, encape, k, encape, array[k][1])
else:
if opt in _comparison:
opt = _comparison[opt]
opt = opt.upper()
if opt in ('NOT IN', 'IN'):
_in_sql = '(' + ', '.join(['%s'] * len(array[k][1])) + ')'
sql += '%s%s%s%s %s %s' % (comma, encape, k, encape, opt, _in_sql)
args.extend(array[k][1])
else:
sql += "%s%s%s%s %s %%s" % (comma, encape, k, encape, opt)
args.append(array[k][1])
else:
if fields and k not in fields:
continue
sql += "%s%s%s%s = %%s" % (comma, encape, k, encape)
args.append(array[k])
comma = glue
return sql, args
def implode_condition(args, encape='`'):
'''
连接条件语句
args : 条件语句
示例 [('id',1),'|',('status','<',1)] 其等价于 id = 1 or status < 1
[('id',1),'|',[('status','<',1),('name','like','qaulau')]] 其等价于 id = '1' or (status < '1' AND name like '%qaulau%')
[('id',1),'|',[('status','<',1),('name','like','qaulau'),[('like','like','computer'),'|',('age','>',18)]]]
其等价于 id = '1' or (status < '1' AND name like '%qaulau%' AND (`like` like '%computer%' or `age` > 18))
[('id',1),('status','<',1)] 其等价于 id = 1 and status < 1
{'id':1,'status':('<',1)} 其等价于 id = 1 and status < 1
'''
global _comparison
def _get_expression(args, params=None, encape='`'):
comma = query = ''
for arg in args:
if isinstance(arg, list):
query += "%s (%s)" % (comma, _get_expression(arg, params=params, encape=encape))
comma = ' AND'
elif isinstance(arg, tuple):
if len(arg) == 2:
query += "%s %s%s%s = %%s" % (comma, encape, arg[0], encape)
params.append(arg[1])
else:
opt = str(arg[1]).strip().lower()
if opt in _comparison:
opt = _comparison[opt]
opt = opt.upper()
_append_args = 1
if 'LIKE' in opt:
if '%' not in arg[2]:
query += "%s %s%s%s %s '%%%%%s%%%%'" % (comma, encape, arg[0], encape, opt, arg[2])
_append_args = 0
else:
query += "%s %s%s%s %s %%s" % (comma, encape, arg[0], encape, opt)
else:
query += "%s %s%s%s %s %%s" % (comma, encape, arg[0], encape, opt)
if _append_args:
if isinstance(arg[2], list):
arg[2] = tuple(arg[2])
params.append(arg[2])
comma = ' AND'
elif arg in ['|', 'or', '!', '', 'OR']:
comma = ' OR'
return query
if isinstance(args, dict):
return implode_field_value(args, ' AND ', encape=encape)
elif isinstance(args, list):
params = []
query = _get_expression(args, params=params, encape=encape)
return query, params
class DBException(Exception):
pass
class DBTableNotExists(DBException):
pass
class db_mysql(object):
'''
基于MySQLdb库封装mysql常用操作,避免繁琐的字符串拼接操作,同时方便处理特殊字符,无需手动转义字符串
2014/07/31
以下方法中codition为限制条件,可以为字典和列表
example : {'name':'qaulau'}
data 为更新或添加的数据,可以为字典或者列表(列表必须为列表或元祖对,即[(key,val),...])
键值对应为字段名
example : insert('order',data = {'name':'qaulau','create_time':121545015})
2014-8-27 添加字段自动匹配,对于数据表中不存在的字段会自动过滤(仅针对更新和插入的数据集,限制条件不在此列)
'''
driver = 'MySQLdb'
rety_count = 3 # 最大重试次数
tablepre = ''
data_type = None
interval = 5 # 重连间隔时间
def __init__(self, *args, **kwargs):
'''
db_fields_cache 为是否缓存数据字段,默认为不缓存,在一次连接中会查询一次字段值,如果选择True如果更新了字段请手动删除缓存字段
'''
if args and isinstance(args[0], dict):
kwargs.update(args[0])
if 'charset' not in kwargs:
kwargs['charset'] = 'utf8'
self._trans_times = 0
self.db_fields_cache = kwargs.get('db_fields_cache', False)
self.tablepre = kwargs.get('tablepre', '')
self.dbname = kwargs.get('db', None)
self.host = kwargs.get('host', None)
self.fields = {}
self.__pks = {}
args = kwargs.copy()
if 'db_fields_cache' in args:
del args['db_fields_cache']
if 'tablepre' in args:
del args['tablepre']
if 'data_type' in args:
del args['data_type']
self.args = args
if kwargs.get('data_type', '') == 'dict':
self.data_type = 'dict'
self._connect()
@property
def dbapi(self):
return __import__(self.driver)
def _connect(self):
'''
连接数据库
:return:
'''
self.conn = self.dbapi.connect(**self.args)
if self.data_type == 'dict':
self.cur = self.conn.cursor(cursorclass=self.dbapi.cursors.DictCursor)
else:
self.cur = self.conn.cursor()
def select_db(self, db):
'''
选择数据库
'''
self.dbname = db
self.args['db'] = db
self.conn.select_db(db)
def table(self, table_name):
'''
获取带前缀的表名
'''
return '%s%s' % (self.tablepre, table_name)
def _check_table_info(self, table_name):
#只在第一次执行记录
if table_name not in self.fields:
#如果数据表字段没有定义则自动获取
if self.db_fields_cache:
self.fields[table_name] = util.file('.fields/%s/%s_%s_%s' % (self.host, self.__class__.__name__, self.dbname, table_name))
if not self.fields[table_name]:
self.flush(table_name)
else:
#每次都会读取数据表信息
self.flush(table_name)
def flush(self, table_name):
'''
刷新字段数据
'''
fields = self.get_fields(table_name)
if not fields:
return False
self.fields[table_name] = fields
if self.db_fields_cache:
util.file('.fields/%s/%s_%s_%s' % (self.host, self.__class__.__name__, self.dbname, table_name), fields)
def get_fields(self, table_name):
'''
获取字段列表
'''
result = self.query('SHOW COLUMNS FROM %s;' % table_name)
info = []
if result:
for val in result:
if isinstance(val, dict):
f = val['Field']
else:
f = val[0]
if isinstance(f, unicode):
f = f.encode('utf-8')
info.append(f)
return info
def get_pk(self, table):
'''
获取表table的主键
:return:
'''
table_name = self.table(table)
if table_name in self.__pks:
return self.__pks[table_name]
result = self.query("SHOW COLUMNS FROM %s WHERE `key` = 'PRI';" % table_name)
pk = ''
if result:
for val in result:
if isinstance(val, dict):
pk = val['Field']
else:
pk = val[0]
break
self.__pks[table_name] = pk
return pk
def query(self, query, args=None):
'''
执行sql语句
'''
self.execute(query, args=args)
return self.fetchall()
def execute(self, query, args=None , _num=0):
'''
执行sql语句
'''
try:
return self.cur.execute(query, args=args)
except self.dbapi.OperationalError as e:
if _num == self.rety_count:
raise self.dbapi.OperationalError(e)
else:
# 延时重连
if self.interval:
print('------ sleep %s sec rety connect ------' % (self.interval,))
time.sleep(self.interval)
self._connect()
self.execute(query, args=args, _num=_num + 1)
def insert(self, table, data={}, return_insert_id=False, replace=False, **kwargs):
'''
插入数据
@param string table:不带前缀的表名
@param dict data : 插入数据库的数据,可以是直接字典数据
@param dict kwargs : 插入数据库的数据,字段名:数值
@param boolean return_insert_id:是否返回插入数据的ID
@param boolean replace:是否替换
@return 如果return_insert_id为真则会返回插入数据ID;否则返回真或假
'''
data.update(kwargs)
table = self.table(table)
self._check_table_info(table)
if isinstance(data, (dict, list)):
try:
query, args = implode_field_value(data, fields=self.fields[table])
except KeyError:
raise DBTableNotExists("Table %s doesn't exist!" % (table,))
else:
query = data
args = None
cmd = 'REPLACE INTO' if replace else 'INSERT INTO'
self.execute("%s `%s` SET %s;" % (cmd, table, query), args)
rs = self.insert_id() if return_insert_id else self.affected_rows()
if self._trans_times == 0:
self.conn.commit()
return rs
def update(self, table, data={}, condition=None, low_priority=False, **kwargs):
'''
更新数据
@param string table : 数据库名称
@param dict data : 更新数据库的数据,可以是直接字典数据
@param dict kwargs : 更新数据库的数据,字段名:数值
@param dict condition : 限制条件, {'id' : '> 10','status':1}
@param tuple args : 可以是直接字典数据
@param dict kwargs : 字段名:数值
@return 返回None
'''
data.update(kwargs)
if not data:
raise DBException("update data doesn't exist empty!")
table = self.table(table)
self._check_table_info(table)
try:
sql, args1 = implode_field_value(data, fields=self.fields[table])
except KeyError:
raise DBTableNotExists("Table %s doesn't exist!" % (table,))
cmd = "UPDATE " + ('LOW_PRIORITY' if low_priority else '')
where = ''
args2 = None
if not condition:
where = '1'
elif isinstance(condition, (dict, list)):
where, args2 = implode_condition(condition)
else:
where = condition
args = None
if args1 is not None:
args = args1
if args2 is not None:
args.extend(args2)
self.execute("%s `%s` SET %s WHERE %s;" % (cmd, table, sql, where), args)
if self._trans_times == 0:
self.conn.commit()
return self.affected_rows()
def select(self, table, fields=None, condition=None, order=None, limit=0):
'''
查询数据
@param string table : 数据库名称
@param list or tuple fields : 查询字段名
@param dict condition : 限制条件, [('id','>','10'),|,('status',1)]
@param string limit : 限制性条件(默认为0)
@return 如果limit为1则返回一个结果字典否则返回列表
'''
table = self.table(table)
if fields is None:
self._check_table_info(table)
try:
fields = self.fields[table]
except KeyError:
raise DBTableNotExists("Table %s doesn't exist!" % (table,))
if isinstance(fields, (list, tuple)):
_fields = ''
for field in fields:
_fields += ',`%s`' % field
fields = _fields[1:]
args = None
if not condition:
where = '1'
elif isinstance(condition, (dict, list)):
where, args = implode_condition(condition)
else:
where = condition
order = '' if order is None else 'ORDER BY %s' % order
sql = "SELECT %s FROM `%s` WHERE %s %s %s;" % (fields, table, where, order, 'LIMIT %s' % limit if limit else '')
self.execute(sql, args)
if self._trans_times == 0:
self.conn.commit()
return self.cur.fetchone() if limit == 1 else self.cur.fetchall()
def delete(self, table, condition=None, limit=0):
'''
删除数据
@param string table : 数据库名称
@param dict condition : 限制条件, {'id' : '> 10','status':1}
@param string limit : 限制性条件(默认为0)
@return 返回影响的条数
'''
args = None
if not condition:
where = '1'
elif isinstance(condition, (dict, list)):
where, args = implode_condition(condition)
else:
where = condition
sql = "DELETE FROM `%s` WHERE %s %s;" % (self.table(table), where, 'LIMIT %s' % limit if limit else '')
self.execute(sql, args)
rs = self.affected_rows()
if self._trans_times == 0:
self.conn.commit()
return rs
def count(self, table, condition=None, pk=None):
'''
获取表中数据
:param table:
:param condition:
:param pk:
:return:
'''
args = None
if not condition:
where = '1'
elif isinstance(condition, (dict, list)):
where, args = implode_condition(condition)
else:
where = condition
if not pk:
pk = self.get_pk(table)
if not pk:
pk = 'id'
sql = "SELECT COUNT(%s) AS c FROM %s WHERE %s LIMIT 1;" % (pk, self.table(table), where)
self.execute(sql, args)
ret = self.fetchone()
if hasattr(ret, 'get'):
return ret['c']
return ret[0]
def fetchone(self):
return self.cur.fetchone()
def fetchall(self):
return self.cur.fetchall()
def fecthmany(self, size=None):
return self.cur.fetchmany(size=size)
def result_first(self, sql):
self.query(sql)
return self.fetchone()
def result_all(self, sql):
self.query(sql)
return self.fetchall()
def insert_id(self):
'''
获取最后一次插入数据的主键ID
'''
return self.conn.insert_id()
def num_rows(self):
'''
取得结果集中行的数目
'''
return self.cur.rownumber
def affected_rows(self):
'''
返回上一步SQL操作受影响的行数
'''
return self.cur.rowcount
def start_trans(self):
'''
启动事物
:return:
'''
self.conn.commit()
if self._trans_times == 0:
self.execute('START TRANSACTION;')
self._trans_times += 1
return True
def commit(self):
'''
提交事物
:return:
'''
if self._trans_times > 0:
self._trans_times = 0
return self.conn.commit()
def rollback(self):
'''
回滚事物
:return:
'''
if self._trans_times > 0:
self.conn.rollback()
self._trans_times = 0
return True
def version(self):
'''
返回mysql版本
'''
res = self.query('SELECT VERSION() AS ver')
if isinstance(res[0],dict):
return res[0]['ver']
return res[0][0]
def get_lastsql(self):
'''
返回上一步操作sql语句
'''
return self.cur._last_executed
def close(self):
'''
关闭mysql
'''
if hasattr(self, 'cur'):
self.cur.close()
if hasattr(self, 'conn'):
self.conn.close()
def __del__(self):
self.close()
class db_sqlite:
pass
class db_mongo:
pass
class db_postgre(db_mysql):
driver = 'psycopg2'
def __init__(self, *args, **kwargs):
if args and isinstance(args[0], dict):
kwargs.update(args[0])
self._trans_times = 0
self.db_fields_cache = kwargs.get('db_fields_cache', False)
self.tablepre = kwargs.get('tablepre', '')
self.dbname = kwargs.get('dbname', None)
self.host = kwargs.get('host', None)
self.fields = {}
_dsn = ''
for p in ('host', 'port', 'user', 'password','dbname'):
_dsn += '%s=%s ' % (p, kwargs.get(p, None))
self.dsn = _dsn
self.conn = None
if kwargs.get('data_type', '') == 'dict':
self.data_type = 'dict'
self._connect()
def _connect(self):
'''
连接数据库
:return:
'''
self.conn = self.dbapi.connect(self.dsn)
if self.data_type == 'dict':
__import__(self.dbapi.__name__ + '.extras')
self.cur = self.conn.cursor(cursor_factory=self.dbapi.extras.DictCursor)
else:
self.cur = self.conn.cursor()
def get_fields(self, table_name):
'''
获取字段列表
:param table_name:
:return:
'''
result = self.query('SELECT column_name FROM information_schema.columns WHERE table_name = %s;', (table_name,))
info = []
if result:
for val in result:
if isinstance(val, dict):
info.append(val['Field'])
else:
info.append(val[0])
return info
def query(self, query, args=None):
'''
执行sql语句
'''
self.execute(query, args)
return self.fetchall()
def execute(self, query, args=None , _num = 0):
'''
执行sql语句
'''
try:
return self.cur.execute(query, args)
except self.dbapi.OperationalError as e:
if _num == self.rety_count:
raise self.dbapi.OperationalError(e)
else:
self._connect()
self.execute(query, args=args,_num = _num + 1)
def insert(self, table, data={}, return_insert_id=False, replace=False, **kwargs):
'''
插入数据
@param string table:不带前缀的表名
@param dict data : 插入数据库的数据,可以是直接字典数据
@param dict kwargs : 插入数据库的数据,字段名:数值
@param boolean return_insert_id:是否返回插入数据的ID
@param boolean replace:是否替换
@return 如果return_insert_id为真则会返回插入数据ID;否则返回真或假
'''
data.update(kwargs)
table = self.table(table)
self._check_table_info(table)
if isinstance(data, (dict, list)):
try:
query, args = implode_field_value(data, fields=self.fields[table], encape='')
except KeyError:
raise DBTableNotExists("Table %s doesn't exist!" % (table,))
else:
query = data
args = None
cmd = 'REPLACE INTO' if replace else 'INSERT INTO'
self.execute("%s %s SET %s" % (cmd, table, query), args)
rs = self.insert_id() if return_insert_id else self.affected_rows()
if self._trans_times == 0:
self.conn.commit()
return rs
def update(self, table, data={}, condition=None, **kwargs):
'''
更新数据
@param string table : 数据库名称
@param dict data : 更新数据库的数据,可以是直接字典数据
@param dict kwargs : 更新数据库的数据,字段名:数值
@param dict condition : 限制条件, {'id' : '> 10','status':1}
@param tuple args : 可以是直接字典数据
@param dict kwargs : 字段名:数值
@return 返回None
'''
data.update(kwargs)
if not data:
raise DBException("update data doesn't exist empty!")
table = self.table(table)
self._check_table_info(table)
try:
sql, args1 = implode_field_value(data, fields=self.fields[table], encape='')
except KeyError:
raise DBTableNotExists("Table %s doesn't exist!" % (table,))
cmd = "UPDATE "
where = ''
args2 = None
if not condition:
where = '1'
elif isinstance(condition, (dict, list)):
where, args2 = implode_condition(condition, encape='')
else:
where = condition
args = None
if args1 is not None:
args = args1
if args2 is not None:
args.extend(args2)
self.execute("%s %s SET %s WHERE %s" % (cmd, table, sql, where), args)
if self._trans_times == 0:
self.conn.commit()
return self.affected_rows()
def select(self, table, fields=None, condition=None, order=None, limit=0):
'''
查询数据
@param string table : 数据库名称
@param list or tuple fields : 查询字段名
@param dict condition : 限制条件, [('id','>','10'),|,('status',1)]
@param string limit : 限制性条件(默认为0)
@return 如果limit为1则返回一个结果字典否则返回列表
'''
table = self.table(table)
if fields is None:
self._check_table_info(table)
try:
fields = self.fields[table]
except KeyError:
raise DBTableNotExists("Table %s doesn't exist!" % (table,))
if isinstance(fields, (list, tuple)):
_fields = ''
for field in fields:
_fields += ',%s' % field
fields = _fields[1:]
args = None
if not condition:
where = '1 = 1'
elif isinstance(condition, (dict, list)):
where, args = implode_condition(condition, encape='')
else:
where = condition
order = '' if order is None else 'ORDER BY %s' % order
sql = "SELECT %s FROM %s WHERE %s %s %s" % (fields, table, where, order, 'LIMIT %s' % limit if limit else '')
self.execute(sql, args)
if self._trans_times == 0:
self.conn.commit()
return self.cur.fetchone() if limit == 1 else self.cur.fetchall()
def delete(self, table, condition=None, limit=0):
'''
删除数据
@param string table : 数据库名称
@param dict condition : 限制条件, {'id' : '> 10','status':1}
@param string limit : 限制性条件(默认为0)
@return 返回影响的条数
'''
args = None
if not condition:
where = '1'
elif isinstance(condition, (dict, list)):
where, args = implode_condition(condition, encape='')
else:
where = condition
sql = "DELETE FROM %s WHERE %s %s" % (self.table(table), where, 'LIMIT %s' % limit if limit else '')
self.execute(sql, args)
rs = self.affected_rows()
if self._trans_times == 0:
self.conn.commit()
return rs
def get_lastsql(self):
return self.cur.query
def insert_id(self):
return self.query('SELECT LASTVAL();')[0][0]
class db_mssql(db_mysql):
"""SQL Server"""
driver = 'pymssql'
def __init__(self, *args, **kwargs):
super(db_mssql, self).__init__(*args, **kwargs)
self._last_executed = None
def _connect(self):
'''
连接数据库
:return:
'''
self.conn = self.dbapi.connect(**self.args)
if self.data_type == 'dict':
self.cur = self.conn.cursor(as_dict=True)
else:
self.cur = self.conn.cursor()
def get_fields(self, table_name):
'''
获取字段列表
:param table_name:
:return:
'''
result = self.query("SELECT COLUMN_NAME AS Field,DATA_TYPE FROM INFORMATION_SCHEMA.columns \
WHERE TABLE_NAME= '%s';" % (table_name,))
info = []
if result:
for val in result:
if isinstance(val, dict):
info.append(val['Field'].lower())
else:
info.append(val[0].lower())
return info
def execute(self, query, args=None , _num = 0):
'''
执行sql语句
'''
try:
self._last_executed = (query, args)
if isinstance(args, list):
args = tuple(args)
return self.cur.execute(query, args)
except self.dbapi.OperationalError as e:
if _num == self.rety_count:
raise self.dbapi.OperationalError(e)
else:
self._connect()
self.execute(query, args=args, _num =_num +1)
def insert(self, table, data={}, return_insert_id=False, replace=False, **kwargs):
'''
插入数据
@param string table:不带前缀的表名
@param dict data : 插入数据库的数据,可以是直接字典数据
@param dict kwargs : 插入数据库的数据,字段名:数值
@param boolean return_insert_id:是否返回插入数据的ID
@param boolean replace:是否替换
@return 如果return_insert_id为真则会返回插入数据ID;否则返回真或假
'''
data.update(kwargs)
table = self.table(table)
self._check_table_info(table)
if isinstance(data, (dict, list)):
try:
query, args = implode_field_value(data, fields=self.fields[table], encape='')
except KeyError:
raise DBTableNotExists("Table %s doesn't exist!" % (table,))
else:
query = data
args = None
cmd = 'REPLACE INTO' if replace else 'INSERT INTO'
self.execute("%s %s SET %s" % (cmd, table, query), args)
rs = self.insert_id() if return_insert_id else self.affected_rows()
if self._trans_times == 0:
self.conn.commit()
return rs
def update(self, table, data={}, condition=None, **kwargs):
'''
更新数据
@param string table : 数据库名称
@param dict data : 更新数据库的数据,可以是直接字典数据
@param dict kwargs : 更新数据库的数据,字段名:数值
@param dict condition : 限制条件, {'id' : '> 10','status':1}
@param tuple args : 可以是直接字典数据
@param dict kwargs : 字段名:数值
@return 返回None
'''
data.update(kwargs)
if not data:
raise DBException("update data doesn't exist empty!")
table = self.table(table)
self._check_table_info(table)
try:
sql, args1 = implode_field_value(data, fields=self.fields[table], encape='')
except KeyError:
raise DBTableNotExists("Table %s doesn't exist!" % (table,))
cmd = "UPDATE "
where = ''
args2 = None
if not condition:
where = '1'
elif isinstance(condition, (dict, list)):
where, args2 = implode_condition(condition, encape='')
else:
where = condition
args = None
if args1 is not None:
args = args1
if args2 is not None:
args.extend(args2)
self.execute("%s %s SET %s WHERE %s" % (cmd, table, sql, where), args)
if self._trans_times == 0:
self.conn.commit()
return self.affected_rows()
def select(self, table, fields=None, condition=None, order=None, limit=0):
'''
查询数据
@param string table : 数据库名称
@param list or tuple fields : 查询字段名
@param dict condition : 限制条件, [('id','>','10'),|,('status',1)]
@param string limit : 限制性条件(默认为0)
@return 如果limit为1则返回一个结果字典否则返回列表
'''
table = self.table(table)
if fields is None:
self._check_table_info(table)
try:
fields = self.fields[table]
except KeyError:
raise DBTableNotExists("Table %s doesn't exist!" % (table,))
if isinstance(fields, (list, tuple)):
_fields = ''
for field in fields:
_fields += ',%s' % field
fields = _fields[1:]
args = None
if not condition:
where = '1 = 1'
elif isinstance(condition, (dict, list)):
where, args = implode_condition(condition, encape='')
else:
where = condition
order = '' if order is None else 'ORDER BY %s' % order
sql = "SELECT %s %s FROM %s WHERE %s %s" % ('TOP %s' % limit if limit else '', fields, table, where, order)
self.execute(sql, args)
return self.cur.fetchone() if limit == 1 else self.cur.fetchall()
def delete(self, table, condition=None, limit=0):
'''
删除数据
@param string table : 数据库名称
@param dict condition : 限制条件, {'id' : '> 10','status':1}
@param string limit : 限制性条件(默认为0)
@return 返回影响的条数
'''
args = None
if not condition:
where = '1'
elif isinstance(condition, (dict, list)):
where, args = implode_condition(condition, encape='')
else:
where = condition
sql = "DELETE FROM %s WHERE %s %s" % (self.table(table), where, 'LIMIT %s' % limit if limit else '')
self.execute(sql, args)
rs = self.affected_rows()
if self._trans_times == 0:
self.conn.commit()
return rs
def get_lastsql(self):
'''
返回上一步操作sql语句
'''
return self._last_executed
def count(self, table, condition=None, pk=None):
'''
获取表中数据
:param table:
:param condition:
:param pk:
:return:
'''
args = None
if not condition:
where = '1'
elif isinstance(condition, (dict, list)):
where, args = implode_condition(condition, '')
else:
where = condition
if not pk:
pk = self.get_pk(table)
if not pk:
pk = 'id'
sql = "SELECT COUNT(%s) AS c FROM %s WHERE %s;" % (pk, self.table(table), where)
self.execute(sql, args)
ret = self.fetchone()
if hasattr(ret, 'get'):
return ret['c']
return ret[0]