helper_sql formating
This commit is contained in:
parent
5a2bac7e8c
commit
21922251e2
|
@ -1,22 +1,26 @@
|
||||||
|
"""Helper Sql performs sql operations."""
|
||||||
|
|
||||||
import threading
|
import threading
|
||||||
import Queue
|
import Queue
|
||||||
|
|
||||||
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.
|
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.
|
||||||
sqlReturnQueue = Queue.Queue()
|
sqlReturnQueue = Queue.Queue()
|
||||||
sqlLock = threading.Lock()
|
sqlLock = threading.Lock()
|
||||||
|
|
||||||
|
|
||||||
def sqlQuery(sqlStatement, *args):
|
def sqlQuery(sqlStatement, *args):
|
||||||
sqlLock.acquire()
|
sqlLock.acquire()
|
||||||
sqlSubmitQueue.put(sqlStatement)
|
sqlSubmitQueue.put(sqlStatement)
|
||||||
|
|
||||||
if args == ():
|
if args == ():
|
||||||
sqlSubmitQueue.put('')
|
sqlSubmitQueue.put('')
|
||||||
elif type(args[0]) in [list, tuple]:
|
elif isinstance(args[0], (list, tuple)):
|
||||||
sqlSubmitQueue.put(args[0])
|
sqlSubmitQueue.put(args[0])
|
||||||
else:
|
else:
|
||||||
sqlSubmitQueue.put(args)
|
sqlSubmitQueue.put(args)
|
||||||
|
queryreturn, _ = sqlReturnQueue.get()
|
||||||
queryreturn, rowcount = sqlReturnQueue.get()
|
|
||||||
sqlLock.release()
|
sqlLock.release()
|
||||||
|
|
||||||
return queryreturn
|
return queryreturn
|
||||||
|
@ -37,14 +41,14 @@ def sqlExecuteChunked(sqlStatement, idCount, *args):
|
||||||
sqlExecuteChunked.chunkSize - (len(args) - idCount)
|
sqlExecuteChunked.chunkSize - (len(args) - idCount)
|
||||||
):
|
):
|
||||||
chunk_slice = args[
|
chunk_slice = args[
|
||||||
i:i+sqlExecuteChunked.chunkSize - (len(args) - idCount)
|
i:i + sqlExecuteChunked.chunkSize - (len(args) - idCount)
|
||||||
]
|
]
|
||||||
sqlSubmitQueue.put(
|
sqlSubmitQueue.put(
|
||||||
sqlStatement.format(','.join('?' * len(chunk_slice)))
|
sqlStatement.format(','.join('?' * len(chunk_slice)))
|
||||||
)
|
)
|
||||||
# first static args, and then iterative chunk
|
# first static args, and then iterative chunk
|
||||||
sqlSubmitQueue.put(
|
sqlSubmitQueue.put(
|
||||||
args[0:len(args)-idCount] + chunk_slice
|
args[0:len(args) - idCount] + chunk_slice
|
||||||
)
|
)
|
||||||
retVal = sqlReturnQueue.get()
|
retVal = sqlReturnQueue.get()
|
||||||
totalRowCount += retVal[1]
|
totalRowCount += retVal[1]
|
||||||
|
@ -60,7 +64,6 @@ def sqlExecute(sqlStatement, *args):
|
||||||
sqlSubmitQueue.put('')
|
sqlSubmitQueue.put('')
|
||||||
else:
|
else:
|
||||||
sqlSubmitQueue.put(args)
|
sqlSubmitQueue.put(args)
|
||||||
|
|
||||||
queryreturn, rowcount = sqlReturnQueue.get()
|
queryreturn, rowcount = sqlReturnQueue.get()
|
||||||
sqlSubmitQueue.put('commit')
|
sqlSubmitQueue.put('commit')
|
||||||
sqlLock.release()
|
sqlLock.release()
|
||||||
|
@ -71,25 +74,27 @@ def sqlStoredProcedure(procName):
|
||||||
sqlSubmitQueue.put(procName)
|
sqlSubmitQueue.put(procName)
|
||||||
sqlLock.release()
|
sqlLock.release()
|
||||||
|
|
||||||
|
|
||||||
class SqlBulkExecute:
|
class SqlBulkExecute:
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
sqlLock.acquire()
|
sqlLock.acquire()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __exit__(self, type, value, traceback):
|
def __exit__(self, exc_type, value, traceback):
|
||||||
sqlSubmitQueue.put('commit')
|
sqlSubmitQueue.put('commit')
|
||||||
sqlLock.release()
|
sqlLock.release()
|
||||||
|
|
||||||
def execute(self, sqlStatement, *args):
|
@staticmethod
|
||||||
|
def execute(sqlStatement, *args):
|
||||||
sqlSubmitQueue.put(sqlStatement)
|
sqlSubmitQueue.put(sqlStatement)
|
||||||
|
|
||||||
if args == ():
|
if args == ():
|
||||||
sqlSubmitQueue.put('')
|
sqlSubmitQueue.put('')
|
||||||
else:
|
else:
|
||||||
sqlSubmitQueue.put(args)
|
sqlSubmitQueue.put(args)
|
||||||
sqlReturnQueue.get()
|
sqlReturnQueue.get()
|
||||||
|
|
||||||
def query(self, sqlStatement, *args):
|
@staticmethod
|
||||||
|
def query(sqlStatement, *args):
|
||||||
sqlSubmitQueue.put(sqlStatement)
|
sqlSubmitQueue.put(sqlStatement)
|
||||||
|
|
||||||
if args == ():
|
if args == ():
|
||||||
|
@ -97,4 +102,3 @@ class SqlBulkExecute:
|
||||||
else:
|
else:
|
||||||
sqlSubmitQueue.put(args)
|
sqlSubmitQueue.put(args)
|
||||||
return sqlReturnQueue.get()
|
return sqlReturnQueue.get()
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user