]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed bug where values within an ENUM weren't escaped for single
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 30 Nov 2013 18:53:26 +0000 (13:53 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 30 Nov 2013 18:53:26 +0000 (13:53 -0500)
quote signs.   Note that this is backwards-incompatible for existing
workarounds that manually escape the single quotes. [ticket:2878]

doc/build/changelog/changelog_09.rst
doc/build/changelog/migration_09.rst
lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/postgresql/test_compiler.py

index 08b815a11f8e35a6ed843adf860cc6a876aa9cf6..158a14058494ed0b5adff9acfa200a729e017f9f 100644 (file)
 .. changelog::
     :version: 0.9.0b2
 
+    .. change::
+        :tags: bug, postgresql
+        :tickets: 2878
+
+        Fixed bug where values within an ENUM weren't escaped for single
+        quote signs.   Note that this is backwards-incompatible for existing
+        workarounds that manually escape the single quotes.
+
+        .. seealso::
+
+            :ref:`migration_2878`
+
     .. change::
         :tags: bug, orm, declarative
 
index d0d901626f1809ce0c716a6eb5d10fe44e624707..1b0a4fc25b528dde2a16b0cc9ef6c1d33818c895 100644 (file)
@@ -692,6 +692,24 @@ an ``__lt__()`` method has been added::
 
 :ticket:`2848`
 
+.. _migration_2878:
+
+Postgresql CREATE TYPE <x> AS ENUM now applies quoting to values
+----------------------------------------------------------------
+
+The :class:`.postgresql.ENUM` type will now apply escaping to single quote
+signs within the enumerated values::
+
+    >>> from sqlalchemy.dialects import postgresql
+    >>> type = postgresql.ENUM('one', 'two', "three's", name="myenum")
+    >>> from sqlalchemy.dialects.postgresql import base
+    >>> print base.CreateEnumType(type).compile(dialect=postgresql.dialect())
+    CREATE TYPE myenum AS ENUM ('one','two','three''s')
+
+Existing workarounds which already escape single quote signs will need to be
+modified, else they will now double-escape.
+
+:ticket:`2878`
 
 New Features
 ============
index 8bcfcbf7cfe851d11dc7d9ab41f5f7ba674cf380..b80f269c149398d8fae499bf69f79896e78a250c 100644 (file)
@@ -1090,7 +1090,9 @@ class PGDDLCompiler(compiler.DDLCompiler):
 
         return "CREATE TYPE %s AS ENUM (%s)" % (
             self.preparer.format_type(type_),
-            ",".join("'%s'" % e for e in type_.enums)
+            ", ".join(
+                self.sql_compiler.process(sql.literal(e), literal_binds=True)
+                        for e in type_.enums)
         )
 
     def visit_drop_enum_type(self, drop):
index 409d6f03a28d5774769a61540d464daf82d7c1ce..dd7df2be37107c65ab974056180fad4ab351a322 100644 (file)
@@ -16,6 +16,7 @@ from sqlalchemy.dialects.postgresql import base as postgresql
 from sqlalchemy.dialects.postgresql import TSRANGE
 from sqlalchemy.orm import mapper, aliased, Session
 from sqlalchemy.sql import table, column, operators
+from sqlalchemy.util import u
 
 class SequenceTest(fixtures.TestBase, AssertsCompiledSQL):
 
@@ -106,6 +107,21 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
                             'AS length_1', dialect=dialect)
 
 
+    def test_create_enum(self):
+        # test escaping and unicode within CREATE TYPE for ENUM
+        typ = postgresql.ENUM(
+                    "val1", "val2", "val's 3", u('méil'), name="myname")
+        self.assert_compile(postgresql.CreateEnumType(typ),
+            u("CREATE TYPE myname AS ENUM ('val1', 'val2', 'val''s 3', 'méil')")
+        )
+
+        typ = postgresql.ENUM(
+                    "val1", "val2", "val's 3", name="PleaseQuoteMe")
+        self.assert_compile(postgresql.CreateEnumType(typ),
+            "CREATE TYPE \"PleaseQuoteMe\" AS ENUM "
+                "('val1', 'val2', 'val''s 3')"
+        )
+
     def test_create_partial_index(self):
         m = MetaData()
         tbl = Table('testtbl', m, Column('data', Integer))