mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-11-22 18:09:41 +01:00
Use assertion to enforce <=999 columns
This commit is contained in:
parent
62d2922528
commit
0c1b8b7f96
2 changed files with 7 additions and 2 deletions
|
|
@ -6,6 +6,8 @@ import itertools
|
||||||
import json
|
import json
|
||||||
import pathlib
|
import pathlib
|
||||||
|
|
||||||
|
SQLITE_MAX_VARS = 999
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import numpy as np
|
import numpy as np
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
|
@ -854,7 +856,10 @@ class Table:
|
||||||
# Peek at first record to count its columns:
|
# Peek at first record to count its columns:
|
||||||
first_record = next(records)
|
first_record = next(records)
|
||||||
num_columns = len(first_record.keys())
|
num_columns = len(first_record.keys())
|
||||||
batch_size = max(1, min(batch_size, 999 // num_columns))
|
assert (
|
||||||
|
num_columns <= SQLITE_MAX_VARS
|
||||||
|
), "Rows can have a maximum of {} columns".format(SQLITE_MAX_VARS)
|
||||||
|
batch_size = max(1, min(batch_size, SQLITE_MAX_VARS // num_columns))
|
||||||
for chunk in chunks(itertools.chain([first_record], records), batch_size):
|
for chunk in chunks(itertools.chain([first_record], records), batch_size):
|
||||||
chunk = list(chunk)
|
chunk = list(chunk)
|
||||||
if first:
|
if first:
|
||||||
|
|
|
||||||
|
|
@ -530,7 +530,7 @@ def test_bulk_insert_more_than_999_values(fresh_db):
|
||||||
def test_error_if_more_than_999_columns(fresh_db, num_columns, should_error):
|
def test_error_if_more_than_999_columns(fresh_db, num_columns, should_error):
|
||||||
record = dict([("c{}".format(i), i) for i in range(num_columns)])
|
record = dict([("c{}".format(i), i) for i in range(num_columns)])
|
||||||
if should_error:
|
if should_error:
|
||||||
with pytest.raises(Exception):
|
with pytest.raises(AssertionError):
|
||||||
fresh_db["big"].insert(record)
|
fresh_db["big"].insert(record)
|
||||||
else:
|
else:
|
||||||
fresh_db["big"].insert(record)
|
fresh_db["big"].insert(record)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue