]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fix quoting schemas in _get_table_sql for the SQLite backend
authorPhillip Cloud <cpcloud@gmail.com>
Mon, 16 Jul 2018 14:10:55 +0000 (10:10 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 18 Jul 2018 14:48:27 +0000 (10:48 -0400)
Fixed issue where the "schema" name used for a SQLite database within table
reflection would not quote the schema name correctly.  Pull request
courtesy Phillip Cloud.

Change-Id: I2770788c1f094a7743209250ec26b5ef5fb2d9e8
Pull-request: https://github.com/zzzeek/sqlalchemy/pull/463

doc/build/changelog/unreleased_12/pr824.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/sqlite/base.py
test/dialect/test_sqlite.py

diff --git a/doc/build/changelog/unreleased_12/pr824.rst b/doc/build/changelog/unreleased_12/pr824.rst
new file mode 100644 (file)
index 0000000..a78bf24
--- /dev/null
@@ -0,0 +1,6 @@
+.. change::
+    :tag: bug, sqlite
+
+    Fixed issue where the "schema" name used for a SQLite database within table
+    reflection would not quote the schema name correctly.  Pull request
+    courtesy Phillip Cloud.
index c6932be8f3fb6b731bea002b8dee5969e9a79e36..cc17b6c9b89956e817c311a794fd396b80ac6341 100644 (file)
@@ -1567,20 +1567,25 @@ class SQLiteDialect(default.DefaultDialect):
 
     @reflection.cache
     def _get_table_sql(self, connection, table_name, schema=None, **kw):
+        if schema:
+            schema_expr = "%s." % (
+                self.identifier_preparer.quote_identifier(schema))
+        else:
+            schema_expr = ""
         try:
             s = ("SELECT sql FROM "
                  " (SELECT * FROM %(schema)ssqlite_master UNION ALL "
                  "  SELECT * FROM %(schema)ssqlite_temp_master) "
                  "WHERE name = '%(table)s' "
                  "AND type = 'table'" % {
-                     "schema": ("%s." % schema) if schema else "",
+                     "schema": schema_expr,
                      "table": table_name})
             rs = connection.execute(s)
         except exc.DBAPIError:
             s = ("SELECT sql FROM %(schema)ssqlite_master "
                  "WHERE name = '%(table)s' "
                  "AND type = 'table'" % {
-                     "schema": ("%s." % schema) if schema else "",
+                     "schema": schema_expr,
                      "table": table_name})
             rs = connection.execute(s)
         return rs.scalar()
index 5e2535b30c4a2e6439f81fa04ec4f38fd0041b8e..9b1d5d7ea5241d06bcc070a7101a841c73480a5d 100644 (file)
@@ -1127,6 +1127,31 @@ class ReflectHeadlessFKsTest(fixtures.TestBase):
         assert b.c.id.references(a.c.id)
 
 
+class KeywordInDatabaseNameTest(fixtures.TestBase):
+    __only_on__ = 'sqlite'
+
+    @classmethod
+    def setup_class(cls):
+        with testing.db.begin() as conn:
+            conn.execute("ATTACH %r AS \"default\"" % conn.engine.url.database)
+            conn.execute('CREATE TABLE "default".a (id INTEGER PRIMARY KEY)')
+
+    @classmethod
+    def teardown_class(cls):
+        with testing.db.begin() as conn:
+            try:
+                conn.execute('drop table "default".a')
+            except Exception:
+                pass
+            conn.execute('DETACH DATABASE "default"')
+
+    def test_reflect(self):
+        with testing.db.begin() as conn:
+            meta = MetaData(bind=conn, schema='default')
+            meta.reflect()
+            assert 'default.a' in meta.tables
+
+
 class ConstraintReflectionTest(fixtures.TestBase):
     __only_on__ = 'sqlite'