This repository has been archived on 2024-11-30. You can view files and clone it, but cannot push or open issues or pull requests.
PyBitmessage-2024-11-30/src/helper_sql.py

105 lines
2.7 KiB
Python
Raw Normal View History

2018-04-07 09:29:09 +02:00
"""Helper Sql performs sql operations."""
import threading
import Queue
2018-04-07 09:29:09 +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.
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):
sqlLock.acquire()
sqlSubmitQueue.put(sqlStatement)
2013-08-27 02:00:30 +02:00
if args == ():
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:
sqlSubmitQueue.put(args)
2018-04-07 09:29:09 +02:00
queryreturn, _ = sqlReturnQueue.get()
sqlLock.release()
2013-08-27 02:00:30 +02:00
return queryreturn
def sqlExecuteChunked(sqlStatement, idCount, *args):
# SQLITE_MAX_VARIABLE_NUMBER,
# unfortunately getting/setting isn't exposed to python
sqlExecuteChunked.chunkSize = 999
if idCount == 0 or idCount > len(args):
return 0
totalRowCount = 0
with sqlLock:
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)
]
sqlSubmitQueue.put(
sqlStatement.format(','.join('?' * len(chunk_slice)))
)
# first static args, and then iterative chunk
sqlSubmitQueue.put(
2018-04-07 09:29:09 +02:00
args[0:len(args) - idCount] + chunk_slice
)
retVal = sqlReturnQueue.get()
totalRowCount += retVal[1]
sqlSubmitQueue.put('commit')
return totalRowCount
2013-08-27 02:00:30 +02:00
def sqlExecute(sqlStatement, *args):
sqlLock.acquire()
sqlSubmitQueue.put(sqlStatement)
2013-08-27 02:00:30 +02:00
if args == ():
sqlSubmitQueue.put('')
2013-08-27 02:00:30 +02:00
else:
sqlSubmitQueue.put(args)
queryreturn, rowcount = sqlReturnQueue.get()
sqlSubmitQueue.put('commit')
sqlLock.release()
return rowcount
2013-08-27 02:00:30 +02:00
def sqlStoredProcedure(procName):
sqlLock.acquire()
sqlSubmitQueue.put(procName)
sqlLock.release()
2018-04-07 09:29:09 +02:00
class SqlBulkExecute:
def __enter__(self):
sqlLock.acquire()
return self
2018-04-07 09:29:09 +02:00
def __exit__(self, exc_type, value, traceback):
sqlSubmitQueue.put('commit')
sqlLock.release()
2018-04-07 09:29:09 +02:00
@staticmethod
def execute(sqlStatement, *args):
sqlSubmitQueue.put(sqlStatement)
if args == ():
sqlSubmitQueue.put('')
else:
sqlSubmitQueue.put(args)
sqlReturnQueue.get()
2018-04-07 09:29:09 +02:00
@staticmethod
def query(sqlStatement, *args):
sqlSubmitQueue.put(sqlStatement)
if args == ():
sqlSubmitQueue.put('')
else:
sqlSubmitQueue.put(args)
return sqlReturnQueue.get()