PyBitmessage/src/helper_sql.py

69 lines
1.7 KiB
Python
Raw Normal View History

import threading
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.
sqlReturnQueue = Queue.Queue()
sqlLock = threading.Lock()
2013-08-27 02:00:30 +02:00
def sqlQuery(sqlStatement, *args):
sqlLock.acquire()
2013-09-03 00:24:22 +02:00
print 'sqlQuery args are:', args
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)
2013-08-27 02:00:30 +02:00
queryreturn = sqlReturnQueue.get()
sqlLock.release()
2013-08-27 02:00:30 +02:00
return queryreturn
def sqlExecute(sqlStatement, *args):
sqlLock.acquire()
2013-09-03 00:24:22 +02:00
print 'sqlExecute args are:', args
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)
2013-08-27 02:00:30 +02:00
sqlReturnQueue.get()
sqlSubmitQueue.put('commit')
sqlLock.release()
2013-08-27 02:00:30 +02:00
def sqlStoredProcedure(procName):
sqlLock.acquire()
sqlSubmitQueue.put(procName)
sqlLock.release()
class SqlBulkExecute:
def __enter__(self):
sqlLock.acquire()
return self
def __exit__(self, type, value, traceback):
sqlSubmitQueue.put('commit')
sqlLock.release()
def execute(self, sqlStatement, *args):
sqlSubmitQueue.put(sqlStatement)
if args == ():
sqlSubmitQueue.put('')
else:
sqlSubmitQueue.put(args)
sqlReturnQueue.get()
def query(self, sqlStatement, *args):
sqlSubmitQueue.put(sqlStatement)
if args == ():
sqlSubmitQueue.put('')
else:
sqlSubmitQueue.put(args)
return sqlReturnQueue.get()