-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbConnection.py
648 lines (506 loc) · 22.4 KB
/
dbConnection.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
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 27 12:07:55 2014
@author: rick.towler
"""
import logging
from PyQt5 import QtCore
from PyQt5 import QtSql
class SQLError(Exception):
def __init__(self, query):
self.QSqlQuery = query
self.error = self.__stripErrorChars(query.lastError().text())
self.SqlText = str(query.lastQuery())
def __str__(self):
return repr(' ::: '.join([self.error, self.SqlText]))
def __stripErrorChars(self, rawMsg):
'''
stripErrorChars is a simple method to extract the readable text from
error messages returned by QSqlQuery.
'''
msg = []
for s in rawMsg:
if (s != "\x00"):
msg.append(s)
else:
break
return ''.join(msg)
class DBError(Exception):
def __init__(self, db):
self.db = db
self.error = self.__stripErrorChars(db.lastError().text())
def __str__(self):
return repr(' ::: '.join([ str(self.db.databaseName()), self.error]))
def __stripErrorChars(self, rawMsg):
'''
stripErrorChars is a simple method to extract the readable text from
error messages returned by QSqlQuery.
'''
msg = []
for s in rawMsg:
if (s != '\x00'):
msg.append(s)
else:
break
return ''.join(msg)
class dbQueryResults:
'''
The dbQueryResults class provides an iterator interface for the qSqlQuery
object. This can be used to provide a more transparent method for iterating
through results returned by SQL SELECT statements since the column values
are returned in a list which can be unpacked in a for-in loop. For example:
sql = ("SELECT device_id, device_name FROM clamsbase2.devices")
results = self.db.dbQuery(sql)
for dev_id, dev_name in results:
print(dev_id)
print(dev_name)
Note that ALL values are returned as Python strings EXCEPT for NULL's which
are returned as None.
The class has the following properties:
columns - a list of column names as lower case python strings ordered
the same as the data.
columnTypes - A list of strings describing the data type of the columns.
Note that this *is not* the data type of the returned data
as all values are returned as strings but the type of the
database column.
nColumns - An integer denoting the number of columns in the returned
result.
query - The reference to the qSQLQuery object used to generate the
query results.
'''
def __init__(self, qSqlQuery, columns, types, dateFormatString=None):
self.query = qSqlQuery
self.columns = columns
self.columnTypes = types
self.nColumns = len(self.columns)
self.rowList = [None] * self.nColumns
if dateFormatString:
self.dateFormat = dateFormatString
else:
self.dateFormat = 'MM/dd/yyyy hh:mm:ss'
def __iter__(self):
return self
def __next__(self):
if self.query.next():
# build the list of items to return
for i in range(self.nColumns):
# NULL values are returned as None
if (self.query.isNull(i)):
val = None
# handle DateTime conversion explicitly
elif (self.columnTypes[i] == 'DateTime'):
val = self.query.value(i).toString(self.dateFormat)
# deal with PyQt4/PyQt5 differences in returning integer values as "floats"
elif (self.columnTypes[i] == 'Double'):
if (self.query.value(i).is_integer()):
val = str(int(self.query.value(i)))
else:
val = str(self.query.value(i))
else:
# try a direct conversion to python string
val = str(self.query.value(i))
self.rowList[i] = val
return self.rowList
else:
self.rowList = [None] * self.nColumns
raise StopIteration
def first(self):
if self.query.first():
# build the list of items to return
for i in range(self.nColumns):
# NULL values are returned as None
if (self.query.isNull(i)):
val = None
# handle DateTime conversion explicitly
elif (self.columnTypes[i] == 'DateTime'):
val = self.query.value(i).toString(self.dateFormat)
else:
val = str(self.query.value(i))
self.rowList[i] = val
else:
# nothing to return as the query is empty
self.rowList = [None] * self.nColumns
return self.rowList
def last(self):
if self.query.last():
# build the list of items to return
for i in range(self.nColumns):
# NULL values are returned as None
if (self.query.isNull(i)):
val = None
# handle DateTime conversion explicitly
elif (self.columnTypes[i] == 'DateTime'):
val = self.query.value(i).toString(self.dateFormat)
else:
val = str(self.query.value(i))
self.rowList[i] = val
else:
# nothing to return as the query is empty
self.rowList = [None] * self.nColumns
return self.rowList
class dbConnection:
'''
The dbConnection class is a simple class that encapsulates much of the common
code we've implemented for SQL interaction in CLAMS and MACEBASE.
driver - set to the QSqlDatabase driver you wish to use for
the connection. See QSqlDatabase docs for details but
common options are:
QPSQL - Postgres SQL driver
QMYSQL - MySQL driver
QSQLITE - SQLite version 3 or greater
QSQLITE2 - SQLite version 2
QODBC3 - ODBC driver (default)
For Oracle connections, use the default ODBC driver. The
full QOCI driver does not ship with Qt.
Note that when using the ODBC driver with NON-Oracle
databases you need to set the isOracle keyword to 'False'
'''
def __init__(self, source, username, password, label='db', driver='QODBC3',
isOracle=True, hostname=None):
# force isOracle keyword for drivers that are obviously *not* oracle
if (driver not in ['QODBC3', 'QODBC', 'QOCI']):
# this is for sure not an oracle driver
isOracle=False
# create an instance of QSqlDatabase - if None is passed as the label
# we'll create the Qt "default" connection.
if (label == None):
self.db = QtSql.QSqlDatabase.addDatabase(driver)
else:
self.db = QtSql.QSqlDatabase.addDatabase(driver, label)
self.db.setDatabaseName(source)
self.db.setUserName(username)
self.db.setPassword(password)
if hostname:
self.db.setHostName(hostname)
self.db.setPort(5432)
self.label = label
self.lastError = ''
if (isOracle):
# set the default Oracle date and timestamp formats
self.NLS_DATE_FORMAT = 'MM/DD/YYYY HH24:MI:SS'
self.NLS_TIMESTAMP_FORMAT = 'MM/DD/YYYY HH24:MI:SS.FF3'
else:
# non-Oracle databases do not support this NLS business
self.NLS_DATE_FORMAT = None
self.NLS_TIMESTAMP_FORMAT = None
self.qtDateFormatString = None
self.loggingEnabled = False
self.logger = None
self.handler = None
self.hasTransactions = False
self.preparedQuery = None
def startTransaction(self):
'''
startTransaction starts a transaction if your driver supports transactions.
Check the state of the hasTransactions property AFTER YOU HAVE CONNECTED TO
YOUR DB to determine if your driver supports transactions.
'''
# start a transaction
ok = self.db.transaction()
# check if we had a problem
if (not ok):
# ooops, there was a problem - report the error
raise DBError(self.db)
def commit(self):
'''
commit will end a transaction and commit data to your database if you have started
a transaction and your driver supports transactions
'''
# commit
ok = self.db.commit()
# check if we had a problem
if (not ok):
# ooops, there was a problem - report the error
raise DBError(self.db)
def rollback(self):
'''
rollback will end a transaction and undo everything you have done after starting
the transaction.
'''
# rollback
ok = self.db.rollback()
# check if we had a problem
if (not ok):
# ooops, there was a problem - report the error
raise DBError(self.db)
def setOracleDateFormat(self, formatString=None, QtFormatString=None):
'''
setOracleDateFormat sets the Oracle NLS_DATE_FORMAT variable for the current session.
The NLS_DATE_FORMAT specifies the format that dates are returned from or inserted
into the database if the TO_CHAR() or TO_DATE() functions are called without a
format string. The default format is:
MM/DD/YYYY HH24:MI:SS
You should rarely need to change this. IF YOU DO CHANGE THE DATE FORMAT
YOU REALLY SHOULD CHANGE THE QT FORMAT STRING TO MATCH. If you fail to
do this, you will either get bogus dates or errors from date conversion
when using the dbQueryResults class since it converts dates to strings
automagically. The format expressions are defined here:
http://qt-project.org/doc/qt-4.8/qdatetime.html#toString
'''
# use the default format string if none provided
if formatString:
self.NLS_DATE_FORMAT = formatString
if QtFormatString:
self.qtDateFormatString = QtFormatString
if self.db.isOpen():
# set the date and time formats for our connections
sql = "alter session set NLS_DATE_FORMAT = '" + self.NLS_DATE_FORMAT + "'"
query = self.db.exec_(sql)
error = query.lastError().isValid()
if (error):
# there was a problem setting the date format - raise an error message
err = DBError(self.db)
self.lastError = err.error
raise err
def setOracleTimestampFormat(self, formatString=None):
'''
setOracleTimestampFormat sets the Oracle NLS_TIMESTAMP_FORMAT variable for the
current session. The NLS_TIMESTAMP_FORMAT specifies the format that timestamps
are returned/inserted from the database if the TO_CHAR() or TO_TIMESTAMP()
functions are called without a format string. The default format is:
MM/DD/YYYY HH24:MI:SS.FF3
Since QtSql drops milliseconds from Timestamp columns, you *must* use the
TO_CHAR() function to convert timestamps to strings on the database side
to preserve them. You then must use the TO_TIMESTAMP() function when inserting
these strings into the database.
'''
# use the default format string if none provided
if formatString:
self.NLS_TIMESTAMP_FORMAT = formatString
if self.db.isOpen():
sql="alter session set NLS_TIMESTAMP_FORMAT='" + self.NLS_TIMESTAMP_FORMAT + "'"
query = self.db.exec_(sql)
error = query.lastError().isValid()
if (error):
# there was a problem setting the timestamp format - raise an error message
err = DBError(self.db)
self.lastError = err.error
raise err
def enableLogging(self, logfile):
'''
enableLogging creates a log file and logs SQL INSERT, UPDATE, DELETE
statements to that log file. This is primarily used to log changes to
the CLAMS database serving as a 3rd line of defense against data loss.
logfile is the full path to the file you wish to log to.
'''
self.logger = logging.getLogger(self.label)
self.logger.setLevel(logging.INFO)
self.handler = logging.handlers.FileHandler(logfile)
formatter = logging.Formatter('%(asctime)s - %(message)s')
self.handler.setFormatter(formatter)
self.logger.addHandler(self.handler)
self.loggingEnabled = True
def disableLogging(self):
'''
disableLogging stops logging if it has been enabled.
'''
if self.logger:
self.logger.removeHandler(self.handler)
self.handler.close()
self.logger = None
self.handler = None
self.loggingEnabled = False
def prepare(self, sql):
'''
prepare creates a sql query object and prepares the provided SQL query
for execution. It will return the prepared query object if successful
and return None if not.
Typically this is used with insert/update statements where you provide
placeholders for binding values and then call dbExecPrepared with the
data you want to insert/update. This allows for more efficient inserts
and updates since the SQL is run through the database optimizer only
once during preparation and this approach should be used when inserting
or updating lots of data.
'''
# define a query object
preparedQuery = QtSql.QSqlQuery(self.db)
# execute the sql
ok = preparedQuery.prepare(sql)
# check if we had a problem
if (not ok):
return None
else:
return preparedQuery
def dbExecPrepared(self, preparedQuery, data):
'''
dbExecPrepared executes a previously prepared query using the data provided.
The data argument must be a dicitonary keyed by the binding values used when
preparing the query. For example, assuming the following SQL:
sql = ("INSERT INTO ships (ship, name, description, active) " +
"VALUES (:ship, :name, :description, :active)"
db.prepare(sql)
you would create a dict in this form:
data = {":ship":[1,2,3],
":name":["a","b","c"],
":description":["x","y","z"],
":active":[0,0,0]}
where the values are lists containing the data you want to insert for
that key. The lists must be the same length and the values must be
ordered appropriately (i.e. values that share an index comprise a
record)
In cases where you are obtaining data to insert on a record by record
basis (i.e. you're querying it out of the database) you can pass a dict
that contains scalar values (not lists) and call dbExecPrepared
once for every record you are inserting. This is still more efficient
than not using bound variables.
data = {":ship":1,
":name":"a",
":description":"x",
":active":0}
'''
# we can't do anything if there isn't a prepared query
if (preparedQuery is None):
return
# get the bind value names
bindNames = list(data.keys())
# bind the data
for name in bindNames:
preparedQuery.bindValue(name, data[name])
# check if we've been a dict of lists - if we have been given lists
# we bind the lists and call execBatch
if (isinstance(data[bindNames[0]], list)):
# we've been give lists containing many records
ok = preparedQuery.execBatch()
else:
# we were only passed one record's worth of data
ok = preparedQuery.exec_()
# check if we had a problem
if (not ok):
# ooops, there was a problem - report the error
raise SQLError(preparedQuery)
def dbOpen(self):
# do nothing if we're already open
if self.db.isOpen():
return
# open up database connection
self.db.open()
if (self.db.isOpenError()) or (not self.db.isValid()):
err = DBError(self.db)
self.lastError = err.error
raise err
# set the Date and Timestamp formats if provided
if (self.NLS_DATE_FORMAT):
self.setOracleDateFormat()
if (self.NLS_TIMESTAMP_FORMAT):
self.setOracleTimestampFormat()
# check if the driver supports transactions.
self.hasTransactions = self.db.driver().hasFeature(QtSql.QSqlDriver.Transactions)
def dbClose(self):
if (self.db.isOpen()):
self.db.close()
#QtSql.QSqlDatabase.removeDatabase(self.db.connectionName())
self.lastError = ''
def dbQuery(self, sql, forwardOnly=True, asDict=False):
'''
dbQuery executes the provided SQL statement and returns either a
dbQueryResults object that can be used to iterate through the result
set created by the SQL OR it can return a dictionary that contains
the full result set. It will raise an error if there is an issue
executing the SQL.
Set the forwardOnly keyword to True to improve performance of the
query, especially for large result sets. This instructs the database
driver to *not* cache the results. Since dbQueryResults cannot iterate
backwards, there is *almost* no reason not to. The only reason would be:
PostgreSQL: While navigating the query results in forward-only mode,
do not execute any other SQL command on the same database connection.
This will cause the query results to be lost.
Calling dbQueryResults.first() multiple times for the same query will
result in an error when forwardOnly=True. Good coding practices can
eliminate the need of calling dbQueryResults.first() multiple times so
this isn't an excuse for setting forwardOnly=False.
By default, this method returns a dbQueryResults instance which you can
use to iterate through the results. Set the asDict keyword to True to
return a dictionary, keyed by query column names that contains the full
results of the query. This results in the entire query data set being
read into RAM and may cause issues with very large data sets. Use your
brain.
'''
# if returning a dict of results force forwardOnly for efficiency
if (asDict):
forwardOnly=True
# create the structures required for our dbSelectResults class
columns = []
types = []
# get a QSqlQuery object
query = self.dbExec(sql, forwardOnly=forwardOnly)
# get a QSqlRecord from the query
dbRecord = query.record()
# if the record is not empty, get the column details
if (not dbRecord.isEmpty()):
for i in range(dbRecord.count()):
columns.append(str(dbRecord.fieldName(i)).lower())
types.append(self.__getQVariantType(dbRecord.field(i)))
if (asDict):
# get an instance of dbQueryREsults
dbq = dbQueryResults(query, columns, types,
dateFormatString=self.qtDateFormatString)
# create the return dictionary with keys mapped to column names
result = {k:[] for k in columns}
# populate the dict with data returned from the query
for vals in dbq:
for i in range(dbq.nColumns):
result[columns[i]].append(vals[i])
# return the dict
return result
else:
# return an instance of our dbSelectResults class
return dbQueryResults(query, columns, types,
dateFormatString=self.qtDateFormatString)
def dbExec(self, sql, forwardOnly=False):
'''
dbExec is a simple wrapper function that checks if your sql statement executed
successfully and raises an error if it didn't.
it returns the qSqlQuery object used to execute the passed sql.
'''
# define a query object
query = QtSql.QSqlQuery(self.db)
# set the forwardOnly property if needed
if (forwardOnly):
query.setForwardOnly(True)
# execute the sql
ok = query.exec_(sql)
# check if we had a problem
if (not ok):
# ooops, there was a problem - report the error
raise SQLError(query)
# check if we're logging DML changes to the database and log if so
if self.loggingEnabled:
# changes are defined here as INSERT, UPDATE and DELETE DML statements
if (sql.split(' ')[0].lower() in ['insert', 'update', 'delete']):
self.logger.info(sql)
return query
def __getQVariantType(self, field):
'''
__getQVariantType is an internal method that determines the python type from
the database column type. These data are stored in the columnTypes property
of the dbQueryResults object returned from a query and can be used to cast
your data from string to the native type if desired.
'''
f= field.type()
if (f == QtCore.QVariant.Int):
t = 'Int'
elif (f == QtCore.QVariant.Double):
t = 'Double'
elif (f == QtCore.QVariant.LongLong):
t = 'LongLong'
elif (f == QtCore.QVariant.Char):
t = 'Char'
elif (f == QtCore.QVariant.Date):
t = 'Date'
elif (f == QtCore.QVariant.DateTime):
t = 'DateTime'
elif (f == QtCore.QVariant.String):
t = 'String'
elif (f == QtCore.QVariant.Time):
t = 'Time'
elif (f == QtCore.QVariant.UInt):
t = 'UInt'
elif (f == QtCore.QVariant.ULongLong):
t = 'ULongLong'
elif (f == QtCore.QVariant.List):
t = 'List'
elif (f == QtCore.QVariant.ByteArray):
t = 'ByteArray'
else:
t = 'Unknown'
return t