@ -23,19 +23,27 @@ sqlSubmitQueue = Queue.Queue()
""" the queue for SQL """
sqlReturnQueue = Queue . Queue ( )
""" the queue for results """
sqlLock = threading . Lock ( )
sql_lock = threading . Lock ( )
""" lock to prevent queueing a new request until the previous response
is available """
sql_available = False
""" set to True by `.threads.sqlThread` immediately upon start """
sql_ready = threading . Event ( )
""" set by `.threads.sqlThread` when ready for processing (after
initialization is done ) """
def sqlQuery ( sqlStatement , * args ) :
def sqlQuery ( sql _s tatement, * args ) :
"""
Query sqlite and return results
: param str sqlStatement : SQL statement string
: param str sql _s tatement: SQL statement string
: param list args : SQL query parameters
: rtype : list
"""
sqlLock . acquire ( )
sqlSubmitQueue . put ( sqlStatement )
assert sql_available
sql_lock . acquire ( )
sqlSubmitQueue . put ( sql_statement )
if args == ( ) :
sqlSubmitQueue . put ( ' ' )
@ -44,46 +52,48 @@ def sqlQuery(sqlStatement, *args):
else :
sqlSubmitQueue . put ( args )
queryreturn , _ = sqlReturnQueue . get ( )
sql L ock. release ( )
sql _l ock. release ( )
return queryreturn
def sqlExecuteChunked ( sql S tatement, idCount , * args ) :
def sqlExecuteChunked ( sql _s tatement, idCount , * args ) :
""" Execute chunked SQL statement to avoid argument limit """
# SQLITE_MAX_VARIABLE_NUMBER,
# unfortunately getting/setting isn't exposed to python
assert sql_available
sqlExecuteChunked . chunkSize = 999
if idCount == 0 or idCount > len ( args ) :
return 0
total RowC ount = 0
with sql L ock:
total _row_c ount = 0
with sql _l ock:
for i in range (
len ( args ) - idCount , len ( args ) ,
sqlExecuteChunked . chunkSize - ( len ( args ) - idCount )
len ( args ) - idCount , len ( args ) ,
sqlExecuteChunked . chunkSize - ( len ( args ) - idCount )
) :
chunk_slice = args [
i : i + sqlExecuteChunked . chunkSize - ( len ( args ) - idCount )
]
sqlSubmitQueue . put (
sql S tatement. format ( ' , ' . join ( ' ? ' * len ( chunk_slice ) ) )
sql _s tatement. format ( ' , ' . join ( ' ? ' * len ( chunk_slice ) ) )
)
# first static args, and then iterative chunk
sqlSubmitQueue . put (
args [ 0 : len ( args ) - idCount ] + chunk_slice
)
ret V al = sqlReturnQueue . get ( )
total RowCount + = retV al[ 1 ]
ret _v al = sqlReturnQueue . get ( )
total _row_count + = ret_v al[ 1 ]
sqlSubmitQueue . put ( ' commit ' )
return total RowC ount
return total _row_c ount
def sqlExecute ( sql S tatement, * args ) :
def sqlExecute ( sql _s tatement, * args ) :
""" Execute SQL statement (optionally with arguments) """
sqlLock . acquire ( )
sqlSubmitQueue . put ( sqlStatement )
assert sql_available
sql_lock . acquire ( )
sqlSubmitQueue . put ( sql_statement )
if args == ( ) :
sqlSubmitQueue . put ( ' ' )
@ -91,32 +101,34 @@ def sqlExecute(sqlStatement, *args):
sqlSubmitQueue . put ( args )
_ , rowcount = sqlReturnQueue . get ( )
sqlSubmitQueue . put ( ' commit ' )
sql L ock. release ( )
sql _l ock. release ( )
return rowcount
def sqlStoredProcedure ( procName ) :
""" Schedule procName to be run """
sqlLock . acquire ( )
assert sql_available
sql_lock . acquire ( )
sqlSubmitQueue . put ( procName )
sql L ock. release ( )
sql _l ock. release ( )
class SqlBulkExecute ( object ) :
""" This is used when you have to execute the same statement in a cycle. """
def __enter__ ( self ) :
sql L ock. acquire ( )
sql _l ock. acquire ( )
return self
def __exit__ ( self , exc_type , value , traceback ) :
sqlSubmitQueue . put ( ' commit ' )
sql L ock. release ( )
sql _l ock. release ( )
@staticmethod
def execute ( sql S tatement, * args ) :
def execute ( sql _s tatement, * args ) :
""" Used for statements that do not return results. """
sqlSubmitQueue . put ( sqlStatement )
assert sql_available
sqlSubmitQueue . put ( sql_statement )
if args == ( ) :
sqlSubmitQueue . put ( ' ' )