Skip to content

Commit 04ec53c

Browse files
committed
Validate column names in more places, refs #86
1 parent 67dd310 commit 04ec53c

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

sqlite_utils/db.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,7 @@ def create_table(
245245
), "defaults set {} includes items not in columns {}".format(
246246
repr(set(defaults)), repr(set(columns.keys()))
247247
)
248-
# Validate no columns contain '[' or ']' - #86
249-
for column in columns.keys():
250-
assert (
251-
"[" not in column and "]" not in column
252-
), "'[' and ']' cannot be used in column names"
248+
validate_column_names(columns.keys())
253249
column_items = list(columns.items())
254250
if column_order is not None:
255251
column_items.sort(
@@ -892,6 +888,7 @@ def update(self, pk_values, updates=None, alter=False, conversions=None):
892888
args = []
893889
sets = []
894890
wheres = []
891+
validate_column_names(updates.keys())
895892
for key, value in updates.items():
896893
sets.append("[{}] = {}".format(key, conversions.get(key, "?")))
897894
args.append(value)
@@ -1026,8 +1023,8 @@ def insert_all(
10261023
all_columns = list(sorted(all_columns))
10271024
if hash_id:
10281025
all_columns.insert(0, hash_id)
1026+
validate_column_names(all_columns)
10291027
first = False
1030-
10311028
# values is the list of insert data that is passed to the
10321029
# .execute() method - but some of them may be replaced by
10331030
# new primary keys if we are extracting any columns.
@@ -1310,3 +1307,11 @@ def resolve_extracts(extracts):
13101307
if isinstance(extracts, (list, tuple)):
13111308
extracts = {item: item for item in extracts}
13121309
return extracts
1310+
1311+
1312+
def validate_column_names(columns):
1313+
# Validate no columns contain '[' or ']' - #86
1314+
for column in columns:
1315+
assert (
1316+
"[" not in column and "]" not in column
1317+
), "'[' and ']' cannot be used in column names"

tests/test_create.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def test_create_table_with_bad_defaults(fresh_db):
7777
)
7878

7979

80-
def test_create_table_with_invalid_column_charactters(fresh_db):
80+
def test_create_table_with_invalid_column_characters(fresh_db):
8181
with pytest.raises(AssertionError):
8282
fresh_db.create_table("players", {"name[foo]": str})
8383

@@ -449,6 +449,13 @@ def test_insert_row_alter_table(
449449
]
450450

451451

452+
def test_insert_row_alter_table_invalid_column_characters(fresh_db):
453+
table = fresh_db["table"]
454+
rowid = table.insert({"foo": "bar"}).last_pk
455+
with pytest.raises(AssertionError):
456+
table.insert({"foo": "baz", "new_col[abc]": 1.2}, alter=True)
457+
458+
452459
@pytest.mark.parametrize("use_table_factory", [True, False])
453460
def test_insert_replace_rows_alter_table(fresh_db, use_table_factory):
454461
first_row = {"id": 1, "title": "Hedgehogs of the world", "author_id": 1}

tests/test_update.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ def test_update_alter(fresh_db):
6666
] == list(table.rows)
6767

6868

69+
def test_update_alter_with_invalid_column_characters(fresh_db):
70+
table = fresh_db["table"]
71+
rowid = table.insert({"foo": "bar"}).last_pk
72+
with pytest.raises(AssertionError):
73+
table.update(rowid, {"new_col[abc]": 1.2}, alter=True)
74+
75+
6976
def test_update_with_no_values_sets_last_pk(fresh_db):
7077
table = fresh_db.table("dogs", pk="id")
7178
table.insert_all([{"id": 1, "name": "Cleo"}, {"id": 2, "name": "Pancakes"}])

0 commit comments

Comments
 (0)