Add SQLite url builder

This commit is contained in:
Kapil Yedidi 2021-11-15 09:00:33 -08:00
parent b1638a41d6
commit 5f93c3748a

View File

@ -1,6 +1,6 @@
import logging
import threading
from urllib.parse import parse_qs, urlparse
from urllib.parse import parse_qs, urlparse, urlencode
from sqlalchemy import create_engine, inspect
from sqlalchemy.sql import text
@ -73,6 +73,32 @@ class Database(object):
self.ensure_schema = ensure_schema
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:///' + path + urlencode(params)
@property
def executable(self):
"""Connection against which statements will be executed."""