escape_sqlite_table_name => escape_sqlite, handles reserved words

It can be used for column names as well as table names.

Reserved word list from https://www.sqlite.org/lang_keywords.html
This commit is contained in:
Simon Willison 2018-04-03 06:39:50 -07:00
commit 8f0d44d646
No known key found for this signature in database
GPG key ID: 17E2DEA2588B7F52
7 changed files with 68 additions and 25 deletions

View file

@ -116,6 +116,13 @@ CREATE TABLE "complex_foreign_keys" (
FOREIGN KEY ("f3") REFERENCES [simple_primary_key](id)
);
CREATE TABLE [select] (
[group] text,
[having] text,
[and] text
);
INSERT INTO [select] VALUES ('group', 'having', 'and');
INSERT INTO simple_primary_key VALUES (1, 'hello');
INSERT INTO simple_primary_key VALUES (2, 'world');
INSERT INTO simple_primary_key VALUES (3, '');

View file

@ -13,7 +13,7 @@ def test_homepage(app_client):
assert response.json.keys() == {'test_tables': 0}.keys()
d = response.json['test_tables']
assert d['name'] == 'test_tables'
assert d['tables_count'] == 8
assert d['tables_count'] == 9
def test_database_page(app_client):
@ -77,6 +77,13 @@ def test_database_page(app_client):
'hidden': False,
'foreign_keys': {'incoming': [], 'outgoing': []},
'label_column': None,
}, {
'columns': ['group', 'having', 'and'],
'name': 'select',
'count': 1,
'hidden': False,
'foreign_keys': {'incoming': [], 'outgoing': []},
'label_column': None,
}, {
'columns': ['pk', 'content'],
'name': 'simple_primary_key',
@ -190,6 +197,18 @@ def test_table_with_slashes_in_name(app_client):
}]
def test_table_with_reserved_word_name(app_client):
response = app_client.get('/test_tables/select.jsono', gather_request=False)
assert response.status == 200
data = response.json
assert data['rows'] == [{
'rowid': 1,
'group': 'group',
'having': 'having',
'and': 'and',
}]
@pytest.mark.parametrize('path,expected_rows,expected_pages', [
('/test_tables/no_primary_key.jsono', 201, 5),
('/test_tables/paginated_view.jsono', 201, 5),

View file

@ -227,16 +227,16 @@ def test_temporary_docker_directory_uses_copy_if_hard_link_fails(mock_link):
def test_compound_keys_after_sql():
assert '(([a] > :p0))' == utils.compound_keys_after_sql(['a'])
assert '((a > :p0))' == utils.compound_keys_after_sql(['a'])
assert '''
(([a] > :p0)
((a > :p0)
or
([a] = :p0 and [b] > :p1))
(a = :p0 and b > :p1))
'''.strip() == utils.compound_keys_after_sql(['a', 'b'])
assert '''
(([a] > :p0)
((a > :p0)
or
([a] = :p0 and [b] > :p1)
(a = :p0 and b > :p1)
or
([a] = :p0 and [b] = :p1 and [c] > :p2))
(a = :p0 and b = :p1 and c > :p2))
'''.strip() == utils.compound_keys_after_sql(['a', 'b', 'c'])