fix(django-spanner): backslash-escape string literals in quote_value - #18234
Open
Samin061 wants to merge 1 commit into
Open
fix(django-spanner): backslash-escape string literals in quote_value#18234Samin061 wants to merge 1 commit into
Samin061 wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the quote_value method in django_spanner/schema.py to properly escape backslashes and single quotes for GoogleSQL string literals, and adds corresponding unit tests. The feedback suggests also escaping literal newlines and carriage returns to prevent syntax errors in Spanner.
| # GoogleSQL string literals use backslash escaping; '' quote | ||
| # doubling is not recognized, so escape the backslash first and | ||
| # then the quote (matching the db_default/generated inlining above). | ||
| return "'%s'" % value.replace("\\", "\\\\").replace("'", "\\'") |
Contributor
There was a problem hiding this comment.
In GoogleSQL, single-quoted string literals cannot contain literal newlines (\n) or carriage returns (\r). If a string containing these characters is passed to quote_value, it will result in a syntax error in Spanner. To prevent this, we should also escape newlines and carriage returns.
Suggested change
| return "'%s'" % value.replace("\\", "\\\\").replace("'", "\\'") | |
| return "'%s'" % value.replace("\\", "\\\\").replace("'", "\\'").replace("\n", "\\n").replace("\r", "\\r") |
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.
quote_value inlines a string value into DDL by wrapping it in single quotes and doubling embedded quotes, but Cloud Spanner GoogleSQL does not treat '' as an escaped quote and the routine never escapes backslashes, so a value beginning with a backslash and a quote closes the literal and the trailing text is parsed as DDL. The Spanner backend sets requires_literal_defaults, so column and table comments and altered defaults reach this method. The db_default and generated-column inlining a few lines above already escape by backslashing the backslash and then the quote, so route quote_value through the same escaping.