mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-16 13:34:10 +02:00
Fix _decode_default_value to unescape doubled single quotes in string defaults (#811)
* Fix _decode_default_value to unescape doubled single quotes in string defaults SQLite stores string defaults with single quotes doubled (e.g. DEFAULT 'O''Brien' is stored as the literal "'O''Brien'" in sqlite_master). The previous code stripped the outer quotes with value[1:-1] but never converted '' back to ', so default_values returned the raw escaped form instead of the true string value. * Test for doubled single quotes in string defaults
This commit is contained in:
parent
a7b734946f
commit
6a456830ca
2 changed files with 13 additions and 2 deletions
|
|
@ -5108,8 +5108,8 @@ def resolve_extracts(
|
||||||
|
|
||||||
def _decode_default_value(value: str) -> object:
|
def _decode_default_value(value: str) -> object:
|
||||||
if value.startswith("'") and value.endswith("'"):
|
if value.startswith("'") and value.endswith("'"):
|
||||||
# It's a string
|
# It's a string; unescape doubled single quotes
|
||||||
return value[1:-1]
|
return value[1:-1].replace("''", "'")
|
||||||
if value.isdigit():
|
if value.isdigit():
|
||||||
# It's an integer
|
# It's an integer
|
||||||
return int(value)
|
return int(value)
|
||||||
|
|
|
||||||
|
|
@ -312,6 +312,7 @@ def test_table_strict(fresh_db, create_table, expected_strict):
|
||||||
1,
|
1,
|
||||||
1.3,
|
1.3,
|
||||||
"foo",
|
"foo",
|
||||||
|
"O'Brien",
|
||||||
True,
|
True,
|
||||||
b"binary",
|
b"binary",
|
||||||
),
|
),
|
||||||
|
|
@ -324,6 +325,16 @@ def test_table_default_values(fresh_db, value):
|
||||||
assert default_values == {"value": value}
|
assert default_values == {"value": value}
|
||||||
|
|
||||||
|
|
||||||
|
def test_table_default_values_escaped_quotes(fresh_db):
|
||||||
|
# SQLite stores string defaults with single quotes doubled, so
|
||||||
|
# introspection needs to unescape them again
|
||||||
|
fresh_db.execute(
|
||||||
|
"create table t (id integer primary key, name text default 'O''Brien')"
|
||||||
|
)
|
||||||
|
assert "default 'O''Brien'" in fresh_db["t"].schema
|
||||||
|
assert fresh_db["t"].default_values == {"name": "O'Brien"}
|
||||||
|
|
||||||
|
|
||||||
def test_pks_use_primary_key_declaration_order(fresh_db):
|
def test_pks_use_primary_key_declaration_order(fresh_db):
|
||||||
# PRIMARY KEY (a, b) declared against columns stored in order (b, a) -
|
# PRIMARY KEY (a, b) declared against columns stored in order (b, a) -
|
||||||
# pks must follow the declaration order, which is what SQLite uses to
|
# pks must follow the declaration order, which is what SQLite uses to
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue