mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-08-03 16:04:13 +02:00
Handle db.table(table_name).insert({}), closes #759
This commit is contained in:
parent
401fb6949c
commit
b5d0080cd1
2 changed files with 40 additions and 1 deletions
|
|
@ -3179,6 +3179,20 @@ class Table(Queryable):
|
||||||
return a list of ``(sql, parameters)`` 2-tuples which, when executed in
|
return a list of ``(sql, parameters)`` 2-tuples which, when executed in
|
||||||
order, perform the desired INSERT / UPSERT / REPLACE operation.
|
order, perform the desired INSERT / UPSERT / REPLACE operation.
|
||||||
"""
|
"""
|
||||||
|
# Dict-mode insert({}) has no explicit columns; SQLite spells that as
|
||||||
|
# DEFAULT VALUES. List mode with no columns is a different input shape.
|
||||||
|
if not list_mode and not all_columns:
|
||||||
|
or_clause = ""
|
||||||
|
if replace:
|
||||||
|
or_clause = " OR REPLACE"
|
||||||
|
elif ignore:
|
||||||
|
or_clause = " OR IGNORE"
|
||||||
|
sql = (
|
||||||
|
f"INSERT{or_clause} INTO {quote_identifier(self.name)} "
|
||||||
|
"DEFAULT VALUES"
|
||||||
|
)
|
||||||
|
return [(sql, []) for _ in chunk]
|
||||||
|
|
||||||
if hash_id_columns and hash_id is None:
|
if hash_id_columns and hash_id is None:
|
||||||
hash_id = "id"
|
hash_id = "id"
|
||||||
|
|
||||||
|
|
@ -3603,7 +3617,11 @@ class Table(Queryable):
|
||||||
assert (
|
assert (
|
||||||
num_columns <= SQLITE_MAX_VARS
|
num_columns <= SQLITE_MAX_VARS
|
||||||
), "Rows can have a maximum of {} columns".format(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))
|
batch_size = (
|
||||||
|
1
|
||||||
|
if num_columns == 0
|
||||||
|
else max(1, min(batch_size, SQLITE_MAX_VARS // num_columns))
|
||||||
|
)
|
||||||
self.last_rowid = None
|
self.last_rowid = None
|
||||||
self.last_pk = None
|
self.last_pk = None
|
||||||
if truncate and self.exists():
|
if truncate and self.exists():
|
||||||
|
|
|
||||||
|
|
@ -31,3 +31,24 @@ def test_quote_default_value(fresh_db, column_def, initial_value, expected_value
|
||||||
assert expected_value == fresh_db.quote_default_value(
|
assert expected_value == fresh_db.quote_default_value(
|
||||||
fresh_db["foo"].columns[0].default_value
|
fresh_db["foo"].columns[0].default_value
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_insert_empty_record_uses_default_values(fresh_db):
|
||||||
|
fresh_db.execute("""
|
||||||
|
CREATE TABLE has_defaults (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
name TEXT,
|
||||||
|
timestamp TEXT DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
is_active INTEGER NOT NULL DEFAULT 1
|
||||||
|
)
|
||||||
|
""")
|
||||||
|
|
||||||
|
table = fresh_db["has_defaults"]
|
||||||
|
table.insert({})
|
||||||
|
|
||||||
|
rows = list(table.rows)
|
||||||
|
assert len(rows) == 1
|
||||||
|
assert rows[0]["id"] == 1
|
||||||
|
assert rows[0]["name"] is None
|
||||||
|
assert rows[0]["timestamp"] is not None
|
||||||
|
assert rows[0]["is_active"] == 1
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue