saving state

This commit is contained in:
Kapil Yedidi 2021-11-17 08:46:27 -08:00
parent eb06d95559
commit 5663dc8251
2 changed files with 33 additions and 28 deletions

View File

@ -1,6 +1,6 @@
import logging import logging
import threading import threading
from urllib.parse import parse_qs, urlparse, urlencode from urllib.parse import parse_qs, urlparse
from sqlalchemy import create_engine, inspect from sqlalchemy import create_engine, inspect
from sqlalchemy.sql import text from sqlalchemy.sql import text
@ -73,32 +73,6 @@ class Database(object):
self.ensure_schema = ensure_schema self.ensure_schema = ensure_schema
self._tables = {} self._tables = {}
@classmethod
def sqlite_url(cls, path, timeout=None, read_only=False, check_same_thread=True, immutable=False, nolock=False):
# NOTE: this PR
# https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/1474/
# added support for URIs in SQLite
# The full list of supported URIs is a combination of:
# https://docs.python.org/3/library/sqlite3.html#sqlite3.connect
# and
# https://www.sqlite.org/uri.html
params = {}
if timeout:
# Note: if timeout is None, it uses the default timeout
params['timeout'] = timeout
if read_only:
params['mode'] = 'ro'
if nolock:
params['nolock'] = 1
if immutable:
params['immutable'] = 1
if not check_same_thread:
params['check_same_thread'] = 'false'
if not params:
return 'sqlite:///' + path
params['uri'] = 'true'
return 'sqlite:///file:' + path + '?' + urlencode(params)
@property @property
def executable(self): def executable(self):
"""Connection against which statements will be executed.""" """Connection against which statements will be executed."""

View File

@ -1,5 +1,5 @@
from hashlib import sha1 from hashlib import sha1
from urllib.parse import urlparse from urllib.parse import urlparse, urlencode
from collections import OrderedDict from collections import OrderedDict
from sqlalchemy.exc import ResourceClosedError from sqlalchemy.exc import ResourceClosedError
@ -30,6 +30,37 @@ def iter_result_proxy(rp, step=None):
yield row yield row
def make_sqlite_url(
path, cache=None, timeout=None, mode=None, check_same_thread=True, immutable=False, nolock=False):
# NOTE: this PR
# https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/1474/
# added support for URIs in SQLite
# The full list of supported URIs is a combination of:
# https://docs.python.org/3/library/sqlite3.html#sqlite3.connect
# and
# https://www.sqlite.org/uri.html
params = {}
if cache:
assert cache in ('shared', 'private')
params['cache'] = cache
if timeout:
# Note: if timeout is None, it uses the default timeout
params['timeout'] = timeout
if mode:
assert mode in ('ro', 'rw', 'rwc')
params['mode'] = mode
if nolock:
params['nolock'] = 1
if immutable:
params['immutable'] = 1
if not check_same_thread:
params['check_same_thread'] = 'false'
if not params:
return 'sqlite:///' + path
params['uri'] = 'true'
return 'sqlite:///file:' + path + '?' + urlencode(params)
class ResultIter(object): class ResultIter(object):
"""SQLAlchemy ResultProxies are not iterable to get a """SQLAlchemy ResultProxies are not iterable to get a
list of dictionaries. This is to wrap them.""" list of dictionaries. This is to wrap them."""