2018-04-07 09:29:09 +02:00
|
|
|
"""Helper Sql performs sql operations."""
|
|
|
|
|
2013-08-29 14:03:45 +02:00
|
|
|
import threading
|
|
|
|
import Queue
|
|
|
|
|
2018-04-07 15:50:29 +02:00
|
|
|
sqlSubmitQueue = Queue.Queue()
|
|
|
|
# SQLITE3 is so thread-unsafe that they won't even let you call it from different threads using your own locks.
|
|
|
|
# SQL objects #can only be called from one thread.
|
2013-08-29 14:03:45 +02:00
|
|
|
sqlReturnQueue = Queue.Queue()
|
|
|
|
sqlLock = threading.Lock()
|
2013-08-27 02:00:30 +02:00
|
|
|
|
2018-04-07 09:29:09 +02:00
|
|
|
|
2013-08-27 02:00:30 +02:00
|
|
|
def sqlQuery(sqlStatement, *args):
|
2018-04-07 15:50:29 +02:00
|
|
|
"""SQLLITE execute statement and return query."""
|
2013-08-29 14:03:45 +02:00
|
|
|
sqlLock.acquire()
|
|
|
|
sqlSubmitQueue.put(sqlStatement)
|
2013-08-27 02:00:30 +02:00
|
|
|
|
|
|
|
if args == ():
|
2013-08-29 14:03:45 +02:00
|
|
|
sqlSubmitQueue.put('')
|
2018-04-07 09:29:09 +02:00
|
|
|
elif isinstance(args[0], (list, tuple)):
|
2015-11-27 00:37:44 +01:00
|
|
|
sqlSubmitQueue.put(args[0])
|
2013-08-27 02:00:30 +02:00
|
|
|
else:
|
2013-08-29 14:03:45 +02:00
|
|
|
sqlSubmitQueue.put(args)
|
2018-04-07 09:29:09 +02:00
|
|
|
queryreturn, _ = sqlReturnQueue.get()
|
2013-08-29 14:03:45 +02:00
|
|
|
sqlLock.release()
|
2013-08-27 02:00:30 +02:00
|
|
|
|
|
|
|
return queryreturn
|
|
|
|
|
2018-01-25 22:04:38 +01:00
|
|
|
|
2017-11-30 19:39:31 +01:00
|
|
|
def sqlExecuteChunked(sqlStatement, idCount, *args):
|
2018-01-25 22:04:38 +01:00
|
|
|
# SQLITE_MAX_VARIABLE_NUMBER,
|
|
|
|
# unfortunately getting/setting isn't exposed to python
|
2017-11-30 19:39:31 +01:00
|
|
|
sqlExecuteChunked.chunkSize = 999
|
|
|
|
|
|
|
|
if idCount == 0 or idCount > len(args):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
totalRowCount = 0
|
|
|
|
with sqlLock:
|
2018-01-25 22:04:38 +01:00
|
|
|
for i in range(
|
|
|
|
len(args) - idCount, len(args),
|
|
|
|
sqlExecuteChunked.chunkSize - (len(args) - idCount)
|
|
|
|
):
|
|
|
|
chunk_slice = args[
|
2018-04-07 09:29:09 +02:00
|
|
|
i:i + sqlExecuteChunked.chunkSize - (len(args) - idCount)
|
2018-01-25 22:04:38 +01:00
|
|
|
]
|
|
|
|
sqlSubmitQueue.put(
|
|
|
|
sqlStatement.format(','.join('?' * len(chunk_slice)))
|
|
|
|
)
|
2017-11-30 19:39:31 +01:00
|
|
|
# first static args, and then iterative chunk
|
2018-01-25 22:04:38 +01:00
|
|
|
sqlSubmitQueue.put(
|
2018-04-07 09:29:09 +02:00
|
|
|
args[0:len(args) - idCount] + chunk_slice
|
2018-01-25 22:04:38 +01:00
|
|
|
)
|
2017-11-30 19:39:31 +01:00
|
|
|
retVal = sqlReturnQueue.get()
|
|
|
|
totalRowCount += retVal[1]
|
|
|
|
sqlSubmitQueue.put('commit')
|
|
|
|
return totalRowCount
|
|
|
|
|
2018-01-25 22:04:38 +01:00
|
|
|
|
2013-08-27 02:00:30 +02:00
|
|
|
def sqlExecute(sqlStatement, *args):
|
2013-08-29 14:03:45 +02:00
|
|
|
sqlLock.acquire()
|
|
|
|
sqlSubmitQueue.put(sqlStatement)
|
2013-08-27 02:00:30 +02:00
|
|
|
|
|
|
|
if args == ():
|
2013-08-29 14:03:45 +02:00
|
|
|
sqlSubmitQueue.put('')
|
2013-08-27 02:00:30 +02:00
|
|
|
else:
|
2013-08-29 14:03:45 +02:00
|
|
|
sqlSubmitQueue.put(args)
|
2018-04-07 09:48:58 +02:00
|
|
|
_, rowcount = sqlReturnQueue.get()
|
2013-08-29 14:03:45 +02:00
|
|
|
sqlSubmitQueue.put('commit')
|
|
|
|
sqlLock.release()
|
2016-03-22 17:17:45 +01:00
|
|
|
return rowcount
|
2013-08-27 02:00:30 +02:00
|
|
|
|
|
|
|
def sqlStoredProcedure(procName):
|
2013-08-29 14:03:45 +02:00
|
|
|
sqlLock.acquire()
|
|
|
|
sqlSubmitQueue.put(procName)
|
|
|
|
sqlLock.release()
|
2013-08-31 16:40:11 +02:00
|
|
|
|
2018-04-07 09:29:09 +02:00
|
|
|
|
2013-08-31 16:40:11 +02:00
|
|
|
class SqlBulkExecute:
|
2018-04-07 15:50:29 +02:00
|
|
|
"""This is used when you have to execute the same statement in a cycle."""
|
|
|
|
|
2013-08-31 16:40:11 +02:00
|
|
|
def __enter__(self):
|
|
|
|
sqlLock.acquire()
|
|
|
|
return self
|
|
|
|
|
2018-04-07 09:29:09 +02:00
|
|
|
def __exit__(self, exc_type, value, traceback):
|
2013-08-31 16:40:11 +02:00
|
|
|
sqlSubmitQueue.put('commit')
|
|
|
|
sqlLock.release()
|
|
|
|
|
2018-04-07 09:29:09 +02:00
|
|
|
@staticmethod
|
|
|
|
def execute(sqlStatement, *args):
|
2018-04-07 15:50:29 +02:00
|
|
|
"""Used for statements that do not return results."""
|
2013-08-31 16:40:11 +02:00
|
|
|
sqlSubmitQueue.put(sqlStatement)
|
2018-04-30 16:55:10 +02:00
|
|
|
|
2013-08-31 16:40:11 +02:00
|
|
|
if args == ():
|
|
|
|
sqlSubmitQueue.put('')
|
|
|
|
else:
|
|
|
|
sqlSubmitQueue.put(args)
|
|
|
|
sqlReturnQueue.get()
|