Formatting fixes

This commit is contained in:
Friedrich Lindenberg 2021-11-17 18:27:48 +01:00
parent 5885680645
commit 4e359f5e01
3 changed files with 29 additions and 20 deletions

View File

@ -21,7 +21,7 @@ def connect(
ensure_schema=True, ensure_schema=True,
row_type=row_type, row_type=row_type,
sqlite_wal_mode=True, sqlite_wal_mode=True,
sqlite_pragmas=None, on_connect_statements=None,
): ):
"""Opens a new connection to a database. """Opens a new connection to a database.
@ -40,12 +40,13 @@ def connect(
the `ensure_schema` argument. It can also be overridden in a lot of the the `ensure_schema` argument. It can also be overridden in a lot of the
data manipulation methods using the `ensure` flag. data manipulation methods using the `ensure` flag.
If you want to run custom SQLite pragmas on database connect, you can add them to sqlite_pragmas If you want to run custom SQLite pragmas on database connect, you can add them
You can view a full list of PRAGMAs here: to on_connect_statements as a set of strings. You can view a full
https://www.sqlite.org/pragma.html `list of PRAGMAs here`_.
.. _SQLAlchemy Engine URL: http://docs.sqlalchemy.org/en/latest/core/engines.html#sqlalchemy.create_engine .. _SQLAlchemy Engine URL: http://docs.sqlalchemy.org/en/latest/core/engines.html#sqlalchemy.create_engine
.. _DB connection timeout: http://docs.sqlalchemy.org/en/latest/core/pooling.html#setting-pool-recycle .. _DB connection timeout: http://docs.sqlalchemy.org/en/latest/core/pooling.html#setting-pool-recycle
.. _list of PRAGMAs here: https://www.sqlite.org/pragma.html
""" """
if url is None: if url is None:
url = os.environ.get("DATABASE_URL", "sqlite://") url = os.environ.get("DATABASE_URL", "sqlite://")
@ -57,5 +58,5 @@ def connect(
ensure_schema=ensure_schema, ensure_schema=ensure_schema,
row_type=row_type, row_type=row_type,
sqlite_wal_mode=sqlite_wal_mode, sqlite_wal_mode=sqlite_wal_mode,
on_connect_statements=sqlite_pragmas, on_connect_statements=on_connect_statements,
) )

View File

@ -59,7 +59,8 @@ class Database(object):
self.is_sqlite = self.engine.dialect.name == "sqlite" self.is_sqlite = self.engine.dialect.name == "sqlite"
if on_connect_statements is None: if on_connect_statements is None:
on_connect_statements = [] on_connect_statements = []
def _run_on_connect_statements(dbapi_con, con_record):
def _run_on_connect(dbapi_con, con_record):
# reference: # reference:
# https://stackoverflow.com/questions/9671490/how-to-set-sqlite-pragma-statements-with-sqlalchemy # https://stackoverflow.com/questions/9671490/how-to-set-sqlite-pragma-statements-with-sqlalchemy
# https://stackoverflow.com/a/7831210/1890086 # https://stackoverflow.com/a/7831210/1890086
@ -70,8 +71,8 @@ class Database(object):
# we only enable WAL mode for sqlite databases that are not in-memory # we only enable WAL mode for sqlite databases that are not in-memory
on_connect_statements.append("PRAGMA journal_mode=WAL") on_connect_statements.append("PRAGMA journal_mode=WAL")
if on_connect_statements: if len(on_connect_statements):
event.listen(self.engine, "connect", _run_on_connect_statements) event.listen(self.engine, "connect", _run_on_connect)
self.types = Types(is_postgres=self.is_postgres) self.types = Types(is_postgres=self.is_postgres)
self.url = url self.url = url

View File

@ -31,7 +31,14 @@ def iter_result_proxy(rp, step=None):
def make_sqlite_url( def make_sqlite_url(
path, cache=None, timeout=None, mode=None, check_same_thread=True, immutable=False, nolock=False): path,
cache=None,
timeout=None,
mode=None,
check_same_thread=True,
immutable=False,
nolock=False,
):
# NOTE: this PR # NOTE: this PR
# https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/1474/ # https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/1474/
# added support for URIs in SQLite # added support for URIs in SQLite
@ -41,24 +48,24 @@ def make_sqlite_url(
# https://www.sqlite.org/uri.html # https://www.sqlite.org/uri.html
params = {} params = {}
if cache: if cache:
assert cache in ('shared', 'private') assert cache in ("shared", "private")
params['cache'] = cache params["cache"] = cache
if timeout: if timeout:
# Note: if timeout is None, it uses the default timeout # Note: if timeout is None, it uses the default timeout
params['timeout'] = timeout params["timeout"] = timeout
if mode: if mode:
assert mode in ('ro', 'rw', 'rwc') assert mode in ("ro", "rw", "rwc")
params['mode'] = mode params["mode"] = mode
if nolock: if nolock:
params['nolock'] = 1 params["nolock"] = 1
if immutable: if immutable:
params['immutable'] = 1 params["immutable"] = 1
if not check_same_thread: if not check_same_thread:
params['check_same_thread'] = 'false' params["check_same_thread"] = "false"
if not params: if not params:
return 'sqlite:///' + path return "sqlite:///" + path
params['uri'] = 'true' params["uri"] = "true"
return 'sqlite:///file:' + path + '?' + urlencode(params) return "sqlite:///file:" + path + "?" + urlencode(params)
class ResultIter(object): class ResultIter(object):