Skip to content

Commit

Permalink
some extra checking and a bug fix around URL arguments to sqlite3.con…
Browse files Browse the repository at this point in the history
…nect()
  • Loading branch information
Nicholas FitzRoy-Dale committed Oct 18, 2023
1 parent d14adf4 commit 9dffbee
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions rime/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,25 @@ def sqlite3_connect(db_path, uri=False):
# We current do not, but in future may, support caching connections across threads.
check_same_thread = False

if uri is False and db_path.startswith('file:'):
raise ValueError(f"Supplied a URI-looking db_path of {db_path} but uri is False")

if not _sqlite3_is_threadsafe():
raise RuntimeError("RIME requires a thread-safe sqlite3 module (sqlite3.threadsafety == 3)")

conn = sqlite3.connect(db_path, uri=uri, check_same_thread=check_same_thread)
conn.create_function('REGEXP', 2, _sqlite3_regexp_search)
try:
conn = sqlite3.connect(db_path, uri=uri, check_same_thread=check_same_thread)
conn.create_function('REGEXP', 2, _sqlite3_regexp_search)
except sqlite3.OperationalError as e:
print(f"Error connecting to database {db_path} (uri={uri}): {e}", file=sys.stderr)
raise

return conn


def sqlite3_connect_filename(path, read_only=True):
params = "?mode=ro&immutable=1" if read_only else ""
return sqlite3_connect(f"file:{path}{params}", uri=True)
return sqlite3_connect(f"file://{path}{params}", uri=True)


def get_field_indices(query):
Expand Down

0 comments on commit 9dffbee

Please sign in to comment.