-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
1143 lines (732 loc) · 29.6 KB
/
database.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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env/python3
# -*- coding: utf-8 -*-
__version__ = (0, 22)
import sys, time, datetime, threading, queue, socket
if sys.version_info[0] < 3:
import cPickle as pickle
import Queue as queue
else:
import pickle, queue
'''
0.22 : - generato un FileNotFoundError in connessione per sqlite
0.20 : - aggiunto pyodbc
0.18 : - aggiunto firebirdsql, in sola lettura
- aggiunto PARSE_DECLTYPES per sqlite3
0.17.1 : - timeout a 60 secondi per Sqlite3Connection
0.17 : - Sqlite3Connection come contextmanager
0.16.1 : - aggiunta la gestione di SELECT ... FOR UPDATE per sqlite3 facendo
- un BEGIN IMMEDIATE TRANSACTION
0.14 : - aggiunto OperationalError per sqlite3 e dbproxy (per intercettare create table
di tabelle esistenti). Al momento non so cosa va per postgres
0.11 : - modifiche per compatibilita' con python2
0.10 : - creazione degli indici in create_table(), corretti alcuni bug
0.9 : - aggiunto metodo insert(), che per praticità costruisce da solo l'sql.
- aggiunto metodo now_dtstr()
'''
# questi moduli li carico quando servono, e quando succede
# li rendo globali
postgres = None
sqlite3 = None
firebirdsql = None
pyodbc = None
def connect(dbstring, **kwargs):
if dbstring.startswith('postgres:'):
return use_postgresql(dbstring)
elif dbstring.startswith('sqlite3:'):
return use_sqlite3(dbstring[8:], **kwargs)
elif dbstring.startswith('proxy:'):
return db_proxy_server_connection(dbstring[6:])
elif dbstring.startswith('firebirdsql:'):
return use_firebirdsql(dbstring[12:], **kwargs)
elif dbstring.startswith('odbc:'):
return use_pyodbc(dbstring.partition(':')[2])
else:
raise AssertionError(dbstring)
def use_postgresql(dbstring):
global postgres
assert dbstring.startswith('postgres:') or dbstring.startswith('psql:')
if postgres is None:
try:
## python-postgres
import postgresql.driver.dbapi20 as postgres
import postgresql.exceptions as pg_exceptions
# aggiungo alla classe esposta all'applicazione
# gli errori di postgres
PostgresConnection.Error = pg_exceptions.Error
PostgresConnection.IntegrityError = pg_exceptions.UniqueError
except ImportError:
## psycopg2
import psycopg2 as postgres
PostgresConnection.Error = postgres.Error
PostgresConnection.IntegrityError = postgres.IntegrityError
_, _, connstring = dbstring.partition(':')
conn_params = PostgresConnection.read_connstring(connstring)
db = PostgresConnection(**conn_params)
db.dbstring = dbstring
return db
def use_sqlite3(dbstring, **kwargs):
global sqlite3
if sqlite3 is None:
import sqlite3
Sqlite3Connection.Error = sqlite3.Error
Sqlite3Connection.OperationalError = sqlite3.OperationalError
Sqlite3Connection.IntegrityError = sqlite3.IntegrityError
return Sqlite3Connection(dbstring, **kwargs)
def use_firebirdsql(dbstring, **kwargs):
global firebirdsql
if firebirdsql is None:
import firebirdsql
FirebirdsqlConnection.Error = firebirdsql.Error
FirebirdsqlConnection.OperationalError = firebirdsql.OperationalError
FirebirdsqlConnection.IntegrityError = firebirdsql.IntegrityError
conn_params = FirebirdsqlConnection.read_connstring(dbstring)
db = FirebirdsqlConnection(**conn_params)
db.dbstring = dbstring
return db
def db_proxy_server_connection(dbstring):
return DBProxyServerConnection(dbstring)
def use_pyodbc(dsn):
global pyodbc
if pyodbc is None:
import pyodbc
PyodbcConnection.Error = pyodbc.Error
PyodbcConnection.OperationalError = pyodbc.OperationalError
PyodbcConnection.IntegrityError = pyodbc.IntegrityError
return PyodbcConnection(dsn)
#----------------------------------------------------------------------
class Base:
@staticmethod
def dtstr_to_datetime(dtstr):
return datetime.datetime.strptime(dtstr, '%Y%m%d%H%M%S')
@staticmethod
def datetime_to_dtstr(d):
return d.strftime('%Y%m%d%H%M%S')
@staticmethod
def now_dtstr():
return time.strftime('%Y%m%d%H%M%S')
@staticmethod
def dtstr_to_date(dtstr):
return datetime.date(int(dtstr[0:4]), int(dtstr[4:6]), int(dtstr[6:8]))
@staticmethod
def date_to_dstr(d):
return d.strftime('%Y%m%d')
@staticmethod
def dstr_to_date(dstr):
return datetime.date(int(dstr[0:4]), int(dstr[4:6]), int(dstr[6:8]))
class PostgresConnection(Base):
paramstyle = 'format' # http://www.python.org/dev/peps/pep-0249/
class UnsupportedError(Exception): pass
def __init__(self, user, password, host, database, port=5432):
self.db = postgres.connect(user=user, host=host, password=password,
database=database, port=port)
#~ db = postgres.connect(user='x', database='y', host=None, port=None, unix='/var/run/postgresql/.s.PGSQL.5432')
def __del__(self):
self.close()
def execute(self, sql, bind=()):
cur = self.db.cursor()
cur.execute(sql, bind)
return cur
def execute_dict(self, *args, **kw):
return CursorWrapperDict(self.execute(*args, **kw))
def cursor(self):
return self.db.cursor()
def close(self):
if self.db:
self.db.close()
self.db = None
def savepoint(self, name):
self.db.cursor().execute('savepoint "{}"'.format(name))
def commit(self):
self.db.commit()
def rollback(self):
self.db.rollback()
def rollback_savepoint(self, name):
self.db.cursor().execute('rollback to savepoint "{}"'.format(name))
def insert(self, table, **data):
cols, values = zip(*data.items())
sql = """insert into %s (%s) values (%s)""" % (table, ','.join(cols), ','.join(['%s']*len(values)))
self.db.cursor().execute(sql, tuple(values))
def drop_table(self, name, if_exists=False):
self.db.cursor().execute("""drop table {} {}""".format('if exists' if if_exists else '', name))
self.db.commit() # a quanto pare con postgres si committano anche i drop table
def create_table(self, name, columns, primary_key=None, indexes=None,
if_not_exists=False):
global COLUMN_TYPE_ACCEPTED
if not all(c[1].lower() in COLUMN_TYPE_ACCEPTED for c in columns):
return self._build_error(
sqlite3.OperationalError('column type not in ' + ', '.join(self.COLUMN_TYPE_ACCEPTED))
)
if if_not_exists:
cur = self.db.cursor()
try:
cur.execute("""select 1 from {} where 1 = 2""".format(name))
return # nessun errore, tabella esistente
except self.db.Error as err:
pass
# cambio di tipi da quelli generici a quelli di postgres
type_map = {
'text': 'text', 'integer': 'integer', 'float': 'float', 'binary': 'bytea',
'bigint': 'bigint'
}
new_col_def = []
for c in columns:
c = list(c)
c[1] = type_map[c[1]]
new_col_def.append(c)
sql_commands = build_sql_create_table(
name=name, columns=new_col_def, primary_key=primary_key,
indexes=indexes
)
for sql in sql_commands:
try:
cur = self.db.cursor()
cur.execute(sql)
self.db.commit() # a quanto pare con postgres va committato il create table
except self.db.Error as err:
return self._build_error(err)
@staticmethod
def read_connstring(connstring):
out = {}
user_pwd, sep, connstring = connstring.partition('@')
if sep != '@':
raise ValueError('wrong connstring ' + init_string)
out['user'], sep, out['password'] = user_pwd.partition('/')
if sep != '/':
raise ValueError('wrong connstring ' + init_string)
out['database'], sep, host = connstring.partition('/')
if ':' in host:
out['host'], _, port = host.partition(':')
out['port'] = int(port)
else:
out['host'] = host
return out
class Sqlite3Connection(Base):
paramstyle = 'format' # http://www.python.org/dev/peps/pep-0249/
class UnsupportedError(Exception): pass
def __init__(self, database, **kwargs):
database, _, params = database.partition('::')
if params:
params = params.split(',')
if 'PARSE_DECLTYPES' in params:
kwargs['detect_types'] = sqlite3.PARSE_DECLTYPES
else:
params = []
kwargs.setdefault('timeout', 60)
try:
self.db = sqlite3.connect(database, **kwargs)
except sqlite3.OperationalError as err:
raise FileNotFoundError('{} / file:{}'.format(err, database))
if 'ENABLE_DATETIME' in params:
# ATTENZIONE: QUESTA IMPOSTAZIONE VALE A LIVELLO DI MODULO
# E PER ESSERE USATA DEVE ESSERE ATTIVATO ANCHE PARSE_DECLTYPES
sqlite3.register_converter(
'DATETIME', lambda v: datetime.datetime.strptime(v.decode('ascii'), '%Y%m%d%H%M%S')
)
sqlite3.register_adapter(
'DATETIME', lambda v: v.strftime('%Y%m%d%H%M%S')
)
# disabilito l'autocommit e me lo gestisco a mano. Senza di questo
# non funzionano i savepoint (non ho approfondito sul perche')
self.db.isolation_level = None
# attributo presente direttamente sulla connessione a sqlite3, ma
# solo a partire da python 3.2. lo gestisco a mano per compatibilità
# con python 2
self.in_transaction = False
def __del__(self):
self.close()
def execute(self, sql, bind=()):
sql = self._check_transaction(sql)
return self.db.execute(self._change_placeholders(sql), bind)
def execute_dict(self, *args, **kw):
return CursorWrapperDict(self.execute(*args, **kw))
def cursor(self):
return NotImplementedError('DA FARE: gestione della transazione automatica per i cursori espliciti')
def close(self):
if self.db:
self.db.close()
self.db = None
def savepoint(self, name):
if not self.in_transaction:
self.db.execute('begin')
self.in_transaction = True
self.db.execute('savepoint {}'.format(name))
def commit(self):
self.db.commit()
self.in_transaction = False
def rollback(self):
self.db.rollback()
self.in_transaction = False
def rollback_savepoint(self, name):
self.db.execute('rollback to savepoint {}'.format(name))
def insert(self, table, **data):
cols, values = zip(*data.items())
sql = """insert into %s (%s) values (%s)""" % (table, ','.join(cols), ','.join(['?']*len(values)))
self.execute(sql, tuple(values))
def drop_table(self, name, if_exists=False):
self.db.execute("""drop table {} {}""".format('if exists' if if_exists else '',
name))
self.in_transaction = False # non sono sicuro che sqlite annulli la transazione
def create_table(self, name, columns, primary_key=None, indexes=None,
if_not_exists=False):
if not all(c[1].lower() in COLUMN_TYPE_ACCEPTED for c in columns):
return self._build_error(
sqlite3.OperationalError('column type not in ' + ', '.join(COLUMN_TYPE_ACCEPTED))
)
if if_not_exists:
cur = self.db.cursor()
try:
cur.execute("""select 1 from {} where 1 = 2""".format(name))
return # nessun errore, tabella esistente
except self.db.Error as err:
pass
sql_commands = build_sql_create_table(
name=name, columns=columns, primary_key=primary_key,
indexes=indexes
)
for sql in sql_commands:
self.db.execute(sql)
self.in_transaction = False
def _check_transaction(self, sql):
# apro una transazione se la query non e' una select
if not self.in_transaction:
if sql.lstrip()[:7].lower() != 'select ':
self.db.execute('begin')
self.in_transaction = True
return sql
elif sql.rstrip()[-10:].lower() == 'for update':
self.db.execute('begin immediate transaction')
self.in_transaction = True
return sql.rstrip()[:-10]
else:
return sql
else:
if sql.rstrip()[-10:].lower() == 'for update':
raise AssertionError('already in transaction')
return sql
def schema(self):
cur = self.db.execute("""
select type, name, tbl_name, sql from sqlite_master
order by name
""")
col_names = [r[0] for r in cur.description]
return [dict(zip(col_names, row)) for row in cur]
@staticmethod
def _change_placeholders(sql):
return sql.replace('%s', '?')
class FirebirdsqlConnection:
paramstyle = 'format' # http://www.python.org/dev/peps/pep-0249/
class UnsupportedError(Exception): pass
def __init__(self, dsn, user, password):
self.db = firebirdsql.connect(dsn=dsn, user=user, password=password)
self.db.charset = 'latin-1'
def __del__(self):
self.close()
def execute(self, sql, bind=()):
assert sql.lstrip().upper().startswith('SELECT ')
return self.db.cursor().execute(self._change_placeholders(sql), bind)
def execute_dict(self, *args, **kw):
return CursorWrapperDict(self.execute(*args, **kw))
def close(self):
if self.db:
self.db.close()
self.db = None
def savepoint(self, name):
raise NotImplementedError
def commit(self):
self.db.commit()
def rollback(self):
self.db.rollback()
def rollback_savepoint(self, name):
raise NotImplementedError
def insert(self, table, **data):
cols, values = zip(*data.items())
sql = """insert into %s (%s) values (%s)""" % (table, ','.join(cols), ','.join(['?']*len(values)))
self.execute(sql, tuple(values))
def drop_table(self, name, if_exists=False):
raise NotImplementedError
self.db.execute("""drop table {} {}""".format('if exists' if if_exists else '',
name))
self.in_transaction = False # non sono sicuro che sqlite annulli la transazione
def create_table(self, name, columns, primary_key=None, indexes=None,
if_not_exists=False):
raise NotImplementedError
def schema(self):
raise NotImplementedError
@staticmethod
def _change_placeholders(sql):
return sql.replace('%s', '?')
@staticmethod
def read_connstring(connstring):
out = {}
user_pwd, sep, connstring = connstring.partition('@')
if sep != '@':
raise ValueError('wrong connstring ' + init_string)
out['user'], sep, out['password'] = user_pwd.partition('/')
if sep != '/':
raise ValueError('wrong connstring ' + init_string)
host, sep, database = connstring.partition('/')
if sep != '/':
raise ValueError('wrong connstring ' + init_string)
out['dsn'] = host + ':' + database
return out
class PyodbcConnection(Base):
# https://www.connectionstrings.com
paramstyle = 'format' # http://www.python.org/dev/peps/pep-0249/
def __init__(self, dsn, **kwargs):
self.db = pyodbc.connect(dsn, timeout=10) # timeout per il login
self.db_brand = self.guess_brand(dsn)
kwargs.setdefault('timeout', 60)
self.db.timeout = kwargs['timeout'] # timeout per le query.
def __del__(self):
self.close()
def guess_brand(self, dsn):
return None
def execute(self, sql, bind=()):
return self.db.execute(self._change_placeholders(sql), bind)
def execute_dict(self, *args, **kw):
return CursorWrapperDict(self.execute(*args, **kw))
def cursor(self):
return NotImplementedError
def close(self):
if self.db:
self.db.close()
self.db = None
def savepoint(self, name):
self.db.execute('savepoint {}'.format(name))
def commit(self):
self.db.commit()
def rollback(self):
self.db.rollback()
def rollback_savepoint(self, name):
self.db.execute('rollback to savepoint {}'.format(name))
def insert(self, table, **data):
cols, values = zip(*data.items())
sql = """insert into %s (%s) values (%s)""" % (table, ','.join(cols), ','.join(['?']*len(values)))
self.execute(sql, tuple(values))
def drop_table(self, name, if_exists=False):
self.db.execute("""drop table {} {}""".format('if exists' if if_exists else '',
name))
def create_table(self, name, columns, primary_key=None, indexes=None,
if_not_exists=False):
# non l'ho mai provata su SQLServer. C'è da guardare i tipi di dato
if not all(c[1].lower() in COLUMN_TYPE_ACCEPTED for c in columns):
return self._build_error(
sqlite3.OperationalError('column type not in ' + ', '.join(COLUMN_TYPE_ACCEPTED))
)
if if_not_exists:
cur = self.db.cursor()
try:
cur.execute("""select 1 from {} where 1 = 2""".format(name))
return # nessun errore, tabella esistente
except self.db.Error as err:
pass
sql_commands = build_sql_create_table(
name=name, columns=columns, primary_key=primary_key,
indexes=indexes
)
for sql in sql_commands:
self.db.execute(sql)
self.in_transaction = False
def schema(self):
cur = self.db.cursor().tables()
return [{'name': r[2], 'type':r[3]} for r in cur]
@staticmethod
def _change_placeholders(sql):
return sql.replace('%s', '?')
class DBProxyServerConnection:
class UnsupportedError(Exception): pass # funzionalità non disponibili
class Error(Exception): pass
class OperationalError(Error): pass
class Warning(Exception): pass
class IntegrityError(Error): pass
DEFAULT_SERVER_ADDR = '127.0.0.1:1555'
def __init__(self, connstring):
server, sep, dbstring = connstring.partition(',')
if not sep:
dbstring = server
server = '127.0.0.1'
self.server = server
self.dbstring = dbstring
self._connect_to_server(server, dbstring)
def _send(self, d, flush=True):
pickle.dump(d, self.wfile, protocol=self.pickle_protocol)
if flush:
self.wfile.flush()
def _recv(self):
try:
out = pickle.load(self.rfile)
return out
#~ return pickle.load(self.rfile, errors='replace')
except EOFError:
self.rfile.close()
self.wfile.close()
self.rfile = self.wfile = None
raise EOFError('connection terminated')
def _connect_to_server(self, server, dbstring):
if ':' in server:
addr, port = server.split(':')
server_addr = (addr, int(port))
else:
server_addr = (server, int(self.DEFAULT_SERVER_ADDR.split(':')[1]))
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(server_addr)
self.rfile = sock.makefile('rb')
self.wfile = sock.makefile('wb')
sock.close(); del sock
self.wfile.write(dbstring.encode('ascii') + b'\n')
self.wfile.flush()
# attesa della conferma
rs = self.rfile.readline(1024)
values = rs.decode('ascii').strip().split(',')
if values[0] != 'OK':
raise self.Error('connection rejected: ' + repr(rs))
self.pickle_protocol = 2 if 'P2' in values else 3
self.cursors_to_delete = []
def reconnect(self):
self.wfile.close()
self.rfile.close()
self._connect_to_server(self.server, self.dbstring)
def _com(self, com, **data):
if self.cursors_to_delete:
data['_del_cursors'] = self.cursors_to_delete
self.cursors_to_delete = []
data['com'] = com
self._send(data)
rs = self._recv()
# la risposta è una tra None (per quei comandi che non prevedono dati
# di ritorno) oppure un dizionario con _necessariamente_ la chiave 'rs'
if rs and rs['rs'] == 'err':
raise self._build_exception(rs)
return rs
def _fetch_cursor(self, cursor_id, count):
rs = self._com('fetchmany', id=cursor_id, count=count)
assert rs['rs'] == 'rows'
return rs['d']
def _del_cursor(self, cursor_id):
rs = self._com('del_cursor', id=cursor_id)
assert rs is None
def _defer_del_cursor(self, cursor_id):
self.cursors_to_delete.append(cursor_id)
def _build_exception(self, r):
if 'type' in r:
err = {
'Error': self.Error,
'OperationalError': self.OperationalError,
'Warning': self.Warning,
'IntegrityError': self.IntegrityError,
'UnsupportedError': self.UnsupportedError
}[r['type']]
else:
err = self.Error # errore non-sqlite
return err(r['text'])
def schema(self):
r = self._com('schema')
assert r['rs'] == 'schema'
return r['obj_list'] # controlla che esca come PyodbcConnection.schema
def execute(self, sql, params=None):
rs = self._com('exe', sql=sql, p=params)
assert rs['rs'] == 'cursor'
return DBProxyServerCursor(self, rs['id'], description=rs['descr'], rowcount=rs['rowcount'])
def execute_dict(self, *args, **kw):
return CursorWrapperDict(self.execute(*args, **kw))
def executemany(self, sql, data_sequence):
self._send({'com': 'exemany', 'sql': sql}, flush=False)
data_block = []
for data in data_sequence:
data_block.append(data)
if len(b) == 16:
self._send(data_block, flush=False)
data_block = []
if data_block:
self._send(data_block, flush=False)
self._send(None) # segnala il termine dei dati e flush
rs = self._recv()
assert isinstance(r, dict) and 'rs' in r
if r['rs'] == 'cursor':
return Cursor(self, rs['id'], description=rs['descr'], rowcount=rs['rowcount'])
elif r['rs'] == 'err':
raise self._build_exception(r)
else:
raise AssertionError
def savepoint(self, name):
r = self._com('savepoint', name=name)
assert r is None
def commit(self):
r = self._com('commit')
assert r is None
def rollback(self):
r = self._com('rollback')
assert r is None
def rollback_savepoint(self, name):
r = self._com('rollback_savepoint', name=name)
assert r is None
def insert(self, table, **data):
cols, values = zip(*data.items())
sql = """insert into %s (%s) values (%s)""" % (table, ','.join(cols), ','.join(['%s']*len(values)))
self.execute(sql, tuple(values))
def drop_table(self, name, if_exists=False):
r = self._com('drop_table', name=name, if_exists=if_exists)
if r is not None:
raise self._build_exception(r)
def create_table(self, name, columns, primary_key=None, indexes=None,
if_not_exists=False):
r = self._com(
'create_table', name=name, columns=columns, primary_key=primary_key,
indexes=indexes, if_not_exists=if_not_exists
)
if r is not None:
raise self._build_exception(r)
def close(self):
self.rfile.close()
self.wfile.close()
del self.rfile, self.wfile
self.dbstring = None
class DBProxyServerCursor:
paramstyle = 'format' # http://www.python.org/dev/peps/pep-0249/
defer_cursor_delete = True
def __init__(self, connection, cursor_id, description, rowcount):
self.conn = connection
self.id = cursor_id
self.description = description
self.rowcount = rowcount
self.prefetched_rows = []
def __del__(self):
if self.defer_cursor_delete:
self.conn._defer_del_cursor(self.id)
else:
try:
self.conn._del_cursor(self.id)
except EOFError:
pass
del self.conn, self.id
def __iter__(self):
while True:
if self.prefetched_rows:
yield self.prefetched_rows.pop(0)
else:
block = self.conn._fetch_cursor(self.id, 200)
if not block:
break
self.prefetched_rows = block
def fetchone(self):
if self.prefetched_rows:
return self.prefetched_rows.pop(0)
else:
row = self.conn._fetch_cursor(self.id, 1)
return row[0] if row else None
def fetchmany(self, count):
assert count >= 0
if self.prefetched_rows:
rows, self.prefetched_rows = (self.prefetched_rows[:count],
self.prefetched_rows[count:])
else:
rows = []
while len(rows) < count:
block = self.conn._fetch_cursor(self.id, min(count - len(rows), 2048))
rows.extend(block)
if not block:
# righe finite
break
return rows
def fetchall(self):
rows = self.prefetched_rows
self.prefetched_rows = []
while True:
block = self.conn._fetch_cursor(self.id, 2048)
if not block:
break
rows.extend(block)
if len(block) < 2048:
# sono arrivate meno righe di quelle richieste; è
# chiaro che non ce ne sono più
break
return rows
class CursorWrapperDict:
def __init__(self, cursor):
self._cursor = cursor