From 5f93c3748a9071c5d70efdd79063175097ee9cec Mon Sep 17 00:00:00 2001 From: Kapil Yedidi Date: Mon, 15 Nov 2021 09:00:33 -0800 Subject: [PATCH 1/3] Add SQLite url builder --- dataset/database.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/dataset/database.py b/dataset/database.py index 22d1a49..7673187 100644 --- a/dataset/database.py +++ b/dataset/database.py @@ -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.""" From eb06d95559c039bdc7c0ef1f5619f29e40b45700 Mon Sep 17 00:00:00 2001 From: Kapil Yedidi Date: Tue, 16 Nov 2021 15:11:55 -0800 Subject: [PATCH 2/3] Bug fix --- dataset/database.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dataset/database.py b/dataset/database.py index 7673187..3da39eb 100644 --- a/dataset/database.py +++ b/dataset/database.py @@ -97,7 +97,7 @@ class Database(object): if not params: return 'sqlite:///' + path params['uri'] = 'true' - return 'sqlite:///' + path + urlencode(params) + return 'sqlite:///file:' + path + '?' + urlencode(params) @property def executable(self): From 5663dc8251ef8443c8bac65854b7a5563c3ec76c Mon Sep 17 00:00:00 2001 From: Kapil Yedidi Date: Wed, 17 Nov 2021 08:46:27 -0800 Subject: [PATCH 3/3] saving state --- dataset/database.py | 28 +--------------------------- dataset/util.py | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/dataset/database.py b/dataset/database.py index 3da39eb..22d1a49 100644 --- a/dataset/database.py +++ b/dataset/database.py @@ -1,6 +1,6 @@ import logging 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.sql import text @@ -73,32 +73,6 @@ 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:///file:' + path + '?' + urlencode(params) - @property def executable(self): """Connection against which statements will be executed.""" diff --git a/dataset/util.py b/dataset/util.py index 1753283..e4ac217 100644 --- a/dataset/util.py +++ b/dataset/util.py @@ -1,5 +1,5 @@ from hashlib import sha1 -from urllib.parse import urlparse +from urllib.parse import urlparse, urlencode from collections import OrderedDict from sqlalchemy.exc import ResourceClosedError @@ -30,6 +30,37 @@ def iter_result_proxy(rp, step=None): 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): """SQLAlchemy ResultProxies are not iterable to get a list of dictionaries. This is to wrap them."""