forked from tjg-global/xlsxlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql.py
522 lines (408 loc) · 14.4 KB
/
sql.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
import os, sys
import csv
import logging
import re
import urllib.parse as urlparse
#import pyodbc
#Connection = pyodbc.Connection
try:
import snowflake.connector as snowflake
except ImportError:
snowflake = None
'''
import win32api
import win32con
import win32net
import win32netcon
'''
import row
class sql_exception(Exception):
pass
class sql_unimplemented(sql_exception):
pass
class sql_could_not_connect(sql_exception):
pass
log = logging.getLogger("sql")
DEFAULT_DATABASES = {}
DATABASE_STARTUP = {}
class Database(object):
def __init__(self, dbtype, connection):
self.type = dbtype
self._connection = connection
def __repr__(self):
return "<%s database connection %s>" % (self.type, self._connection)
def __getattr__(self, attr):
return getattr(self._connection, attr)
def mysql_connection(server, database, username, password):
return pyodbc.connect(
"DRIVER={MySQL ODBC 5.3 Unicode Driver};OPTION=3;SERVER=%s;DATABASE=%s;USER=%s;PASSWORD=%s;"
% (server, database, username, password)
)
def snowflake_connection(database="warehouses", username=None, password=None):
return snowflake.connect(
user=username or os.environ["DBT_PROFILES_USER"],
password=password or os.environ["DBT_PROFILES_PASSWORD"],
account="global.eu-west-1",
database=database,
warehouse="DEV_DWH_ETL_XSMALL",
role="reader_all",
)
def database(database_name):
try:
server, database, username, password = DEFAULT_DATABASES[database_name]
except KeyError:
server, database, username, password = parse_dburi(database_name)
db = pyodbc_connection(server, database, username, password)
startup = DATABASE_STARTUP.get(database_name)
if startup:
q = db.cursor()
q.execute(startup)
q.close()
return db
def database_ex(database_name):
driver, server, database, username, password = parse_dburi_ex(database_name)
if driver == "mssql":
return Database("mssql", pyodbc_connection(server, database, username, password)), driver
pass
elif driver == "mysql":
return Database("mysql", mysql_connection(server, database, username, password)), driver
pass
elif driver == "snowflake":
return Database("snowflake", snowflake_connection(database, username, password)), driver
else:
raise RuntimeError("Unknown driver: %s" % driver)
ERROR = "User %s is not allowed to access database %s.%s"
def pyodbc_connection(server, database, username="", password="", **kwargs):
connectors = ["Driver={ODBC Driver 17 for SQL Server}"]
connectors.append("Server=%s" % server)
connectors.append("Database=%s" % database)
if username:
connectors.append("UID=%s" % username)
connectors.append("PWD=%s" % password)
else:
connectors.append("Trusted_Connection=Yes")
try:
return pyodbc.connect(";".join(connectors), **kwargs)
except pyodbc.Error as info:
code, message = info
if code == "28000":
win32api.MessageBox(
0,
ERROR
% (
username or win32api.GetUserNameEx(win32con.NameSamCompatible),
server,
database,
),
"Problem connecting to %s.%s" % (server, database),
)
raise sql_could_not_connect
else:
raise
def parse_dburi(dburi):
if not dburi.startswith("mssql://"):
dburi = "mssql://" + dburi
parsed = urlparse.urlparse(dburi)
return (
parsed.hostname or "",
(parsed.path or "").lstrip("/"),
parsed.username or "",
parsed.password or "",
)
def parse_dburi_ex(dburi):
if not re.match("[^:]+://", dburi):
dburi = "mssql://" + dburi
parsed = urlparse.urlparse(dburi)
return (
parsed.scheme or "mssql",
parsed.hostname or "",
(parsed.path or "").lstrip("/"),
parsed.username or "",
parsed.password or "",
)
def connection_from_oledb_dsn(oledb_dsn, **kwargs):
"""An OLEDB DSN looks something like this:
Data Source=SERVER;User ID=USERNAME;Password=PWD;Initial Catalog=DBNAME;Provider=SQLOLEDB.1
Data Source=SVR-DWSQLDEV;Integrated Security=SSPI;Initial Catalog=WALRUS;Provider=SQLOLEDB.1;Persist Security Info=True;
Parse this data and extract the elements we need pass to pyodbc_connection (along with
any arbitrary keyword args)
"""
info = {}
items = (i.strip() for i in oledb_dsn.split(";") if i.strip())
for item in items:
k, v = item.split("=")
info[k.lower()] = v
if "data source" not in info:
raise RuntimeError("No data source supplied in DSN %s" % oledb_dsn)
else:
server = info["data source"]
database = info.get("initial catalog", "")
username = info.get("user id", "")
password = info.get("password", "")
return pyodbc_connection(server, database, username, password, **kwargs)
def servers(domain=None):
hResume = 0
while True:
info, total, hResume = win32net.NetServerEnum(
None, 100, win32netcon.SV_TYPE_SQLSERVER, domain, hResume
)
for server in info:
yield server["name"]
if hResume == 0:
break
def get_credentials(server="", database="", username="", password=""):
from winsys import dialogs
if isinstance(server, list):
possible_servers = server
else:
possible_servers = sorted(s for s in servers() if s != server)
if server:
possible_servers = [server] + possible_servers
return dialogs.dialog(
"Database Logon",
("Server", possible_servers),
("Database", database or ""),
("Username", username or ""),
("Password", password or ""),
)
def query_to_csv(db, query, csv_filename):
header, rows = fetch_query(db, query, with_headers=1)
write_csv(csv_filename, rows, header)
def fetchall(q):
"""fetchall - return a described list of rows from a cursor resultset
Uses dtuple to return a more convenient version of a cursor's resultset.
Rather than struggling with tuple indices, you can reference the rows
as a dictionary or even as a class.
Example:
import sql
q = db.cursor ()
q.execute ("SELECT * FROM wb_booking_types")
booking_types = sql.fetchall (q)
for booking_type in booking_types:
print "id = %(id)d, code = %(booking_type_code)s, name = %(booking_type_name)s, active = %(active)d" % booking_type.asMapping ()
q.close ()
"""
Row = row.Row([d[0] for d in q.description])
return [Row(i) for i in q.fetchall()]
def fetchone(q):
"""fetchone - return one described row from a cursor resultset
Uses dtuple to return a more convenient version of a cursor's resultset.
Rather than struggling with tuple indices, you can reference the rows
as a dictionary or even as a class.
Example:
import sql
q = db.cursor ()
q.execute ("SELECT * FROM wb_booking_types")
booking_type = sql.fetchone (q)
print "id = %(id)d, code = %(booking_type_code)s, name = %(booking_type_name)s, active = %(active)d" % booking_type.asMapping ()
q.close ()
"""
Row = row.Row([d[0] for d in q.description])
return Row(q.fetchone())
def fetch_value(db, q_sql):
q = db.cursor()
row = []
try:
q.execute(q_sql, ())
row = fetchone(q)
finally:
q.close()
if len(row) == 1:
return row[0]
else:
raise sql_exception("fetch_value expects a one-column row")
def fetch_query(db, q_sql, params=None, with_headers=0):
"""
fetch_query - execute a query against a database and return the resultset
=> [header,] rows
"""
q = db.cursor()
log.debug(
"%s\n%s\n%s",
("-" * 80),
re.sub("[\r\n]{2,}", "\n", q_sql),
params or "<No params>",
)
if params is None:
q.execute(q_sql)
else:
q.execute(q_sql, params)
#
# Bit of a fudge to avoid issues with adodbapi
# when the result set is empty
#
if q.description is None:
rows = header = []
else:
rows = fetchall(q)
header = [f[0] for f in q.description]
q.close()
if with_headers:
return (header, rows)
else:
return rows
def fetch_queries(db, q_sql, params=None):
"""fetch_queries - fetch one or more resultsets from a database"""
results = []
q = db.cursor()
if params is None:
q.execute(q_sql)
else:
q.execute(q_sql, params)
while 1:
results.append(fetchall(q))
if q.nextset() is None:
break
q.close()
return results
def fetch_query_with_header(db, q_sql, params=None):
"fetch_query_with_header - return the resultset and the header"
return fetch_query(db=db, q_sql=q_sql, params=params, with_headers=1)
def execute_script(db, script, params=None, delimiter="\nGO", debug=0):
"""execute_script - take a SQL file and run it
Splits the script into batches at the GO statements (which aren't
part of SQL).
Designed for DDL, so makes no attempt to return resultsets
"""
execute_sql(
db=db, sql=open(script).read(), params=params, delimiter=delimiter, debug=debug
)
def execute_sql(db, sql, params=None, delimiter=r"\bGO\b", debug=0):
"""execute_sql - take a series of SQL statements and run them
Splits the script into batches at the GO statements (which aren't
part of SQL.
Designed for DDL, so makes no attempt to return resultsets
"""
re_delimiter = re.compile(delimiter, re.IGNORECASE)
q = db.cursor()
# split at GO on a linebreak to avoid problems with line-commented
# GO in a comment block.
try:
for s in re_delimiter.split(sql):
try:
if s:
log.debug(
"***%s\n%s\n%s",
("-" * 80),
re.sub("[\r\n]{2,}", "\n", s),
params or "<No params>",
)
if params is None:
q.execute(s)
else:
q.execute(s, params)
except:
raise
finally:
q.close()
class csv_writer:
def __init__(self, csv_file):
self.csv_file = csv_file
self.parser = csv.parser()
def writerow(self, row):
self.csv_file.write("%s\n" % self.parser.join(row))
def query2csv(db, q_sql, csv_filename):
header, rows = fetch_query_with_header(db, q_sql)
write_csv(csv_filename, rows, header)
#
# If the item is of type (key) then convert item
# using factory (value)
#
def nuller(i):
return None
TYPE_MAPPER = {type(True): int, type(None): nuller}
def write_csv(csv_filename, rows, headers):
f = open(csv_filename, "wb")
try:
if sys.version_info[:2] < (2, 3):
writer = csv_writer(f)
else:
writer = csv.writer(f)
if headers:
writer.writerow(headers)
for row in rows:
for item in row:
writer.writerow([TYPE_MAPPER.get(type(i), type(i))(i) for i in row])
finally:
f.close()
def csv_reader(csv_file):
p = csv.parser(ms_double_quote=1)
rows = []
while 1:
line = csv_file.readline()
if not line:
break
fields = p.parse(line)
if not fields:
continue
rows.append(fields)
return rows
def fetch_csv(csv_file, with_header=0, ignore_blanks=1):
"""
fetch rows from a csv file as though from a database,
optionally ignoring wholly blank lines.
NB the first row is *always* assumed to be a header
csv_file - name of file to be read; no assumptions
with_header - return the header list before the data?
ignore_blanks - ignore entirely blank lines?
=> [header,] rows
"""
f = open(csv_file)
try:
if sys.version_info[:2] < (2, 3):
reader = csv_reader(f)
else:
reader = csv.reader(f)
rows = []
first = 1
for fields in reader:
if first:
header = fields
Row = row.Row(header)
first = 0
else:
total_field = "".join(field.strip() for field in fields)
if not ignore_blanks or total_field > "":
try:
for i in range(len(fields), len(header)):
fields.append("")
rows.append(Row(fields))
except ValueError:
print("Unable to process row:")
pprint(fields)
break
finally:
f.close()
if with_header:
return (header, rows)
else:
return rows
#
# Shortcut class to give an object-oriented
# feel to the data fetches
#
class sql_database:
def __init__(self, db):
self.db = db
def execute(self, query, *args, **kwargs):
return execute_sql(self.db, query, *args, **kwargs)
execute_sql = execute
def fetch(self, query, *args, **kwargs):
return fetch_query(self.db, query, *args, **kwargs)
fetch_query = fetch
def fetch_queries(self, query, *args, **kwargs):
return fetch_queries(self.db, query, *args, **kwargs)
def begin(self):
return execute_sql(self.db, "BEGIN TRANSACTION")
def commit(self):
return execute_sql(self.db, "COMMIT TRANSACTION")
def rollback(self):
return execute_sql(self.db, "ROLLBACK TRANSACTION")
class csv_database:
def __init__(self, path="."):
self.path = path
def fetch(self, table, *args):
root, ext = os.path.splitext(table)
if not ext:
ext = ".csv"
return fetch_csv(os.path.join(self.path, root + ext), *args)