Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
klass, funcname=funcname.split(".",1)
else:
klass="apsw"
if klass not in classes:
classes[klass]=[]
classes[klass].append(funcname)
# ok, so we know what was documented. Now lets see what exists
con=apsw.Connection(":memory:")
cur=con.cursor()
cur.execute("create table x(y); insert into x values(x'abcdef1012');select * from x")
blob=con.blobopen("main", "x", "y", con.last_insert_rowid(), 0)
vfs=apsw.VFS("aname", "")
vfsfile=apsw.VFSFile("", ":memory:", [apsw.SQLITE_OPEN_MAIN_DB|apsw.SQLITE_OPEN_CREATE|apsw.SQLITE_OPEN_READWRITE, 0])
# virtual tables aren't real - just check their size hasn't changed
assert len(classes['VTModule'])==2
del classes['VTModule']
assert len(classes['VTTable'])==13
del classes['VTTable']
assert len(classes['VTCursor'])==6
del classes['VTCursor']
for name, obj in ( ('Connection', con),
('Cursor', cur),
('blob', blob),
('VFS', vfs),
('VFSFile', vfsfile),
('apsw', apsw),
):
def db_connect(path, type):
# Connect to new database
if type == 'peewee':
db = APSWDatabase(path, autorollback=True, journal_mode='WAL', timeout=BUSY_TIMEOUT)
elif type == 'raw':
db = apsw.Connection(path, flags=apsw.SQLITE_OPEN_READWRITE | apsw.SQLITE_OPEN_CREATE | apsw.SQLITE_OPEN_WAL)
db.setbusytimeout(BUSY_TIMEOUT)
else:
raise ValueError('Unknown database type: %r' % type)
log.debug('Connected to database at %r', path)
return db
def __init__(self, path=None, read_only=False):
if path is None:
path = config.database_path
if read_only:
flags = apsw.SQLITE_OPEN_READONLY
else:
flags = apsw.SQLITE_OPEN_CREATE | apsw.SQLITE_OPEN_READWRITE
# The global lock is acquired in the constructor, so you must never instantiate a DbCursor
# object without actually using it.
global_database_lock.acquire()
try:
# The connection setup must be done in the constructor, NOT in __enter__.
# If __enter__ raises an exception, then the __exit__ method will also be called.
self.connection = apsw.Connection(path, flags)
self.connection.setbusytimeout(5000)
for module in apply_filters("database-vtmodules", [self.get_assignments_vtmodule()]):
module.registerWithConnection(self.connection)
except Exception:
global_database_lock.release()
raise
def __call__(self):
from sqlite3 import connect
if (self.readonly or (self.vfs is not None)) and self.APSW_IS_AVAILABLE:
# and not self.lockable
import apsw
if self.readonly: flags = apsw.SQLITE_OPEN_READONLY #1
else: flags = apsw.SQLITE_OPEN_READWRITE | apsw.SQLITE_OPEN_CREATE #2|4
apsw_con = apsw.Connection(self.filename, vfs=self.vfs, flags=flags)
return connect(apsw_con)
return connect(self.filename)
def open(self):
self.db = apsw.Connection(
self._db_path,
flags=(
apsw.SQLITE_OPEN_READWRITE |
apsw.SQLITE_OPEN_CREATE |
apsw.SQLITE_OPEN_URI
)
)
def exec_factory(cursor, statement, bindings):
tpl = namedtuple('row', (d[0] for d in cursor.getdescription()))
cursor.setrowtrace(lambda cursor, row: tpl(*row))
return True
self.db.setexectrace(exec_factory)
self.execute(self.PRAGMAS)
self.execute(self.CREATE_TABLES_QUERY)
register_canonical_functions(self.db)
register_trending_functions(self.db)