Skip to content

Commit 7f6f89d

Browse files
author
boonhapus
committed
programmatically fetch sqlite MAX_VARIABLE_NUMBER
1 parent ca2377f commit 7f6f89d

File tree

2 files changed

+15
-28
lines changed

2 files changed

+15
-28
lines changed

cs_tools/sync/sqlite/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# SQLITE DOCS:
22
# https://www.sqlite.org/limits.html
3-
SQLITE_MAX_VARIABLES = 32_766
3+
SQLITE_MAX_VARIABLES = 999

cs_tools/sync/sqlite/syncer.py

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,23 @@ def __init__(self, **kwargs):
3838
super().__init__(**kwargs)
3939
self._engine = sa.create_engine(f"sqlite:///{self.database_path}", future=True)
4040

41+
def __finalize__(self):
42+
super().__finalize__()
43+
self._set_sqlite_max_vars()
44+
4145
def __repr__(self):
4246
return f"<SQLiteSyncer conn_string='{self.engine.url}'>"
4347

44-
def insert_on_conflict(self, data: TableRows, *, table: sa.Table) -> Union[sa.Insert, sa.Update]:
45-
"""UPSERT."""
46-
stmt = insert(table).values(data)
47-
48-
if table.columns == table.primary_key:
49-
set_ = {c.key: getattr(stmt.excluded, c.key) for c in table.columns}
50-
else:
51-
set_ = {c.key: getattr(stmt.excluded, c.key) for c in table.columns if c.key not in table.primary_key}
52-
53-
stmt = stmt.on_conflict_do_update(
54-
index_elements=table.primary_key,
55-
set_=set_,
56-
)
57-
return stmt
58-
59-
# @contextlib.contextmanager
60-
# def pragma_speedy_insert(self):
61-
# """ """
62-
# self.session.execute("PRAGMA journal_mode = OFF;")
63-
# self.session.execute("PRAGMA synchronous = 0;")
64-
# self.session.execute("PRAGMA locking_mode = EXCLUSIVE;")
65-
# self.session.execute("PRAGMA temp_store = MEMORY;")
66-
# yield
67-
# self.session.execute("PRAGMA journal_mode = ON;")
68-
# self.session.execute("PRAGMA synchronous = 0;")
69-
# self.session.execute("PRAGMA locking_mode = EXCLUSIVE;")
70-
# self.session.execute("PRAGMA temp_store = MEMORY;")
48+
def _set_sqlite_max_vars(self) -> None:
49+
"""Fetch the maximum number of variables that SQLite supports."""
50+
r = self.session.execute(sa.text("PRAGMA compile_options;"))
51+
52+
for option in r.mappings().all():
53+
override = option["compile_options"]
54+
name, _, value = override.partition("=")
55+
56+
if name == "MAX_VARIABLE_NUMBER":
57+
const.SQLITE_MAX_VARIABLES = int(value)
7158

7259
def read_stream(self, tablename: str, *, batch: int = 100_000) -> Iterator[TableRows]:
7360
"""Read rows from a SQLite database."""

0 commit comments

Comments
 (0)