Fix #17: quote SQL table/column/index identifiers#52
Merged
Conversation
Coverage Report for CI Build 27778225547Warning Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes. Coverage increased (+0.1%) to 73.451%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
Wrap every table, column, and index name in double quotes when building SQL in database.py, via a new quote_identifier() helper that also escapes embedded double quotes. This means identifiers containing spaces or punctuation, or which collide with SQL keywords (e.g. "order", "group"), no longer have to be sanitized down to alphanumeric + underscore. Adds a regression test that creates and queries a table whose name and columns contain spaces and reserved words. Claude-Session: https://claude.ai/code/session_011bzfZPTzWnhAMVD7msyMg1
a98f640 to
24ced29
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Closes #17.
datacache interpolated table, column, and index names directly into SQL strings without quoting. That forces names to be sanitized to a restricted alphanumeric + underscore character set, and breaks outright for identifiers that are SQL keywords (
order,group, ...) or contain spaces/punctuation.Fix
Add a
quote_identifier()helper indatacache/database.pythat wraps an identifier in double quotes (the SQL standard) and escapes embedded double quotes by doubling them. Apply it to every identifier interpolated into SQL:CREATE TABLE(table + column names)INSERT INTO(table name)DROP TABLE(table name)CREATE INDEX(index name, table name, indexed columns)CREATE/INSERT/SELECTValues continue to be bound via
?placeholders. Quoting is transparent for ordinary names (SQLite stores the unquoted form in its catalog), so existing databases keep working.Added a regression test that creates and queries a table whose name and columns contain spaces and reserved words — invalid SQL before this change.
https://claude.ai/code/session_011bzfZPTzWnhAMVD7msyMg1