Merge pull request #387 from kapily/sqlite_uri

Add SQLite url builder
This commit is contained in:
Friedrich Lindenberg 2021-11-17 18:10:42 +01:00 committed by GitHub
commit 5885680645
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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."""