diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2017-08-25 15:26:02 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2017-08-25 16:53:49 -0400 |
| commit | 2392ae1900f112c44ed966783d1dedfb88f13353 (patch) | |
| tree | 405791ed1dacde958b0e5be9f4a36bd66190345e /lib/sqlalchemy | |
| parent | 887fb3ebaad20847edc752f5fcf072ace947d56a (diff) | |
| download | sqlalchemy-2392ae1900f112c44ed966783d1dedfb88f13353.tar.gz | |
Apply percent sign escaping to literal binds, comments
Fixed bug in new percent-sign support (e.g. :ticket:`3740`) where a bound
parameter rendered with literal_binds would fail to escape percent-signs
for relevant dialects. In addition, ensured new table / column comment
support feature also fully makes use of literal-rendered parameters so that
this percent sign support takes place with table / column comment DDL as
well, allowing percent sign support for the mysql / psycopg2 backends that
require escaping of percent signs.
Change-Id: Ia4136a300933e9bc6a01a7b9afd5c7b9a3fee4e3
Fixes: #4054
Fixes: #4052
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/dialects/mysql/base.py | 7 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/sqltypes.py | 4 | ||||
| -rw-r--r-- | lib/sqlalchemy/testing/suite/test_reflection.py | 13 | ||||
| -rw-r--r-- | lib/sqlalchemy/testing/suite/test_types.py | 4 |
4 files changed, 22 insertions, 6 deletions
diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py index 0cffae389..9d6dd7188 100644 --- a/lib/sqlalchemy/dialects/mysql/base.py +++ b/lib/sqlalchemy/dialects/mysql/base.py @@ -1171,7 +1171,9 @@ class MySQLDDLCompiler(compiler.DDLCompiler): ], nonpart_options): arg = opts[opt] if opt in _reflection._options_of_type_string: - arg = "'%s'" % arg.replace("\\", "\\\\").replace("'", "''") + + arg = self.sql_compiler.render_literal_value( + arg, sqltypes.String()) if opt in ('DATA_DIRECTORY', 'INDEX_DIRECTORY', 'DEFAULT_CHARACTER_SET', 'CHARACTER_SET', @@ -1196,7 +1198,8 @@ class MySQLDDLCompiler(compiler.DDLCompiler): ], part_options): arg = opts[opt] if opt in _reflection._options_of_type_string: - arg = "'%s'" % arg.replace("\\", "\\\\").replace("'", "''") + arg = self.sql_compiler.render_literal_value( + arg, sqltypes.String()) opt = opt.replace('_', ' ') joiner = ' ' diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py index 6838baa5f..fe9a56b6e 100644 --- a/lib/sqlalchemy/sql/sqltypes.py +++ b/lib/sqlalchemy/sql/sqltypes.py @@ -205,6 +205,10 @@ class String(Concatenable, TypeEngine): def literal_processor(self, dialect): def process(value): value = value.replace("'", "''") + + if dialect.identifier_preparer._double_percents: + value = value.replace('%', '%%') + return "'%s'" % value return process diff --git a/lib/sqlalchemy/testing/suite/test_reflection.py b/lib/sqlalchemy/testing/suite/test_reflection.py index 54b59a432..7674338b4 100644 --- a/lib/sqlalchemy/testing/suite/test_reflection.py +++ b/lib/sqlalchemy/testing/suite/test_reflection.py @@ -104,9 +104,12 @@ class ComponentReflectionTest(fixtures.TablesTest): ) Table('comment_test', metadata, Column('id', sa.Integer, primary_key=True, comment='id comment'), - Column('data', sa.String(20), comment='data comment'), + Column('data', sa.String(20), comment='data % comment'), + Column( + 'd2', sa.String(20), + comment=r"""Comment types type speedily ' " \ '' Fun!"""), schema=schema, - comment='the test table comment') + comment=r"""the test % ' " \ table comment""") if testing.requires.index_reflection.enabled: cls.define_index(metadata, users) @@ -274,7 +277,7 @@ class ComponentReflectionTest(fixtures.TablesTest): eq_( insp.get_table_comment("comment_test", schema=schema), - {"text": "the test table comment"} + {"text": r"""the test % ' " \ table comment"""} ) eq_( @@ -290,7 +293,9 @@ class ComponentReflectionTest(fixtures.TablesTest): ], [ {'comment': 'id comment', 'name': 'id'}, - {'comment': 'data comment', 'name': 'data'} + {'comment': 'data % comment', 'name': 'data'}, + {'comment': r"""Comment types type speedily ' " \ '' Fun!""", + 'name': 'd2'} ] ) diff --git a/lib/sqlalchemy/testing/suite/test_types.py b/lib/sqlalchemy/testing/suite/test_types.py index a345454be..83aac2850 100644 --- a/lib/sqlalchemy/testing/suite/test_types.py +++ b/lib/sqlalchemy/testing/suite/test_types.py @@ -187,6 +187,10 @@ class TextTest(_LiteralRoundTripFixture, fixtures.TablesTest): data = r'backslash one \ backslash two \\ end' self._literal_round_trip(Text, [data], [data]) + def test_literal_percentsigns(self): + data = r'percent % signs %% percent' + self._literal_round_trip(Text, [data], [data]) + class StringTest(_LiteralRoundTripFixture, fixtures.TestBase): __backend__ = True |
