sqlite search: Rename connection
to get_connection
`connection` could cause confusion with `django.db.connection` and `get_connection` is a better callable name.
This commit is contained in:
parent
e0f8eeeaa7
commit
adb9f0ecc9
1 changed files with 7 additions and 7 deletions
|
@ -15,15 +15,15 @@ FTS_COLUMN = "texts"
|
||||||
|
|
||||||
if FTS_SEPARATE_DATABASE:
|
if FTS_SEPARATE_DATABASE:
|
||||||
database = sqlite3.connect("search.sqlite3")
|
database = sqlite3.connect("search.sqlite3")
|
||||||
# Make connection callable, because `django.db.connection.cursor()`
|
# Make get_connection callable, because `django.db.connection.cursor()`
|
||||||
# has to be called to get a context manager, but sqlite3.Connection
|
# has to be called to get a context manager, but sqlite3.Connection
|
||||||
# is a context manager without being called.
|
# is a context manager without being called.
|
||||||
def connection():
|
def get_connection():
|
||||||
return database
|
return database
|
||||||
SQLITE_BIND = "?"
|
SQLITE_BIND = "?"
|
||||||
else:
|
else:
|
||||||
from django.db import connection as database
|
from django.db import connection as database
|
||||||
connection = database.cursor
|
get_connection = database.cursor
|
||||||
SQLITE_BIND = "%s"
|
SQLITE_BIND = "%s"
|
||||||
|
|
||||||
# Only Python >= 3.11 supports sqlite3.Connection.getlimit(),
|
# Only Python >= 3.11 supports sqlite3.Connection.getlimit(),
|
||||||
|
@ -71,7 +71,7 @@ def _create_tables():
|
||||||
tokenizers = _escape_sqlite3_value(FTS_TOKENIZERS)
|
tokenizers = _escape_sqlite3_value(FTS_TOKENIZERS)
|
||||||
trigger_name = _escape_sqlite3_identifier(f"{FTS_ID_TABLE}_ad")
|
trigger_name = _escape_sqlite3_identifier(f"{FTS_ID_TABLE}_ad")
|
||||||
|
|
||||||
with connection() as cursor:
|
with get_connection() as cursor:
|
||||||
# Create a contentless-delete FTS5 table that indexes
|
# Create a contentless-delete FTS5 table that indexes
|
||||||
# but does not store the texts of snapshots
|
# but does not store the texts of snapshots
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
|
@ -107,7 +107,7 @@ def index(snapshot_id: str, texts: List[str]):
|
||||||
column = _escape_sqlite3_identifier(FTS_COLUMN)
|
column = _escape_sqlite3_identifier(FTS_COLUMN)
|
||||||
id_table = _escape_sqlite3_identifier(FTS_ID_TABLE)
|
id_table = _escape_sqlite3_identifier(FTS_ID_TABLE)
|
||||||
|
|
||||||
with connection() as cursor:
|
with get_connection() as cursor:
|
||||||
retries = 2
|
retries = 2
|
||||||
while retries > 0:
|
while retries > 0:
|
||||||
retries -= 1
|
retries -= 1
|
||||||
|
@ -144,7 +144,7 @@ def search(text: str) -> List[str]:
|
||||||
table = _escape_sqlite3_identifier(FTS_TABLE)
|
table = _escape_sqlite3_identifier(FTS_TABLE)
|
||||||
id_table = _escape_sqlite3_identifier(FTS_ID_TABLE)
|
id_table = _escape_sqlite3_identifier(FTS_ID_TABLE)
|
||||||
|
|
||||||
with connection() as cursor:
|
with get_connection() as cursor:
|
||||||
res = cursor.execute(
|
res = cursor.execute(
|
||||||
f"SELECT snapshot_id FROM {table}"
|
f"SELECT snapshot_id FROM {table}"
|
||||||
f" INNER JOIN {id_table}"
|
f" INNER JOIN {id_table}"
|
||||||
|
@ -160,7 +160,7 @@ def flush(snapshot_ids: Generator[str, None, None]):
|
||||||
|
|
||||||
id_table = _escape_sqlite3_identifier(FTS_ID_TABLE)
|
id_table = _escape_sqlite3_identifier(FTS_ID_TABLE)
|
||||||
|
|
||||||
with connection() as cursor:
|
with get_connection() as cursor:
|
||||||
cursor.executemany(
|
cursor.executemany(
|
||||||
f"DELETE FROM {id_table} WHERE snapshot_id={SQLITE_BIND}",
|
f"DELETE FROM {id_table} WHERE snapshot_id={SQLITE_BIND}",
|
||||||
[snapshot_ids])
|
[snapshot_ids])
|
||||||
|
|
Loading…
Reference in a new issue