From 5f93c3748a9071c5d70efdd79063175097ee9cec Mon Sep 17 00:00:00 2001 From: Kapil Yedidi Date: Mon, 15 Nov 2021 09:00:33 -0800 Subject: [PATCH] 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."""