]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- boolean, int, and float arguments count as "cache key" values for inspector info_ca...
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 1 Oct 2009 23:00:02 +0000 (23:00 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 1 Oct 2009 23:00:02 +0000 (23:00 +0000)
- added awareness of sqlite implicit auto indexes [ticket:1551]

lib/sqlalchemy/dialects/sqlite/base.py
lib/sqlalchemy/engine/reflection.py
test/dialect/test_sqlite.py

index 8dea91d0abf316be4b9a650da455702d893a0057..c25e75f2c9e9b5d5a6bf363ebb63b59f6dc1ab8b 100644 (file)
@@ -498,6 +498,7 @@ class SQLiteDialect(default.DefaultDialect):
             pragma = "PRAGMA %s." % quote(schema)
         else:
             pragma = "PRAGMA "
+        include_auto_indexes = kw.pop('include_auto_indexes', False)
         qtable = quote(table_name)
         c = _pragma_cursor(connection.execute("%sindex_list(%s)" % (pragma, qtable)))
         indexes = []
@@ -505,6 +506,11 @@ class SQLiteDialect(default.DefaultDialect):
             row = c.fetchone()
             if row is None:
                 break
+            # ignore implicit primary key index.
+            # http://www.mail-archive.com/sqlite-users@sqlite.org/msg30517.html
+            elif not include_auto_indexes and row[1].startswith('sqlite_autoindex'):
+                continue
+
             indexes.append(dict(name=row[1], column_names=[], unique=row[2]))
         # loop thru unique indexes to get the column names.
         for idx in indexes:
index 173e0fab0070498823a800709ae2b9dd5599942b..d88a0a3d29341ad26f482378bbce3ecde329f56f 100644 (file)
@@ -33,7 +33,7 @@ def cache(fn, self, con, *args, **kw):
     key = (
             fn.__name__, 
             tuple(a for a in args if isinstance(a, basestring)), 
-            tuple((k, v) for k, v in kw.iteritems() if isinstance(v, basestring))
+            tuple((k, v) for k, v in kw.iteritems() if isinstance(v, (basestring, int, float)))
         )
     ret = info_cache.get(key)
     if ret is None:
@@ -207,6 +207,10 @@ class Inspector(object):
         referred_columns
           a list of column names in the referred table that correspond to
           constrained_columns
+
+        \**kw
+          other options passed to the dialect's get_foreign_keys() method.
+
         """
 
         fk_defs = self.dialect.get_foreign_keys(self.conn, table_name, schema,
@@ -214,7 +218,7 @@ class Inspector(object):
                                                 **kw)
         return fk_defs
 
-    def get_indexes(self, table_name, schema=None):
+    def get_indexes(self, table_name, schema=None, **kw):
         """Return information about indexes in `table_name`.
 
         Given a string `table_name` and an optional string `schema`, return
@@ -228,11 +232,14 @@ class Inspector(object):
 
         unique
           boolean
+          
+        \**kw
+          other options passed to the dialect's get_indexes() method.
         """
 
         indexes = self.dialect.get_indexes(self.conn, table_name,
                                                   schema,
-                                            info_cache=self.info_cache)
+                                            info_cache=self.info_cache, **kw)
         return indexes
 
     def reflecttable(self, table, include_columns):
index 95d14e9a05c2bc68c31fc91ec0d7489bf055b9c8..040397f4c3d98f436e2846f1d06b9fb221eea13a 100644 (file)
@@ -289,7 +289,25 @@ class DialectTest(TestBase, AssertsExecutionResults):
             except exc.DBAPIError:
                 pass
             raise
-
+    
+    
+    def test_dont_reflect_autoindex(self):
+        meta = MetaData(testing.db)
+        t = Table('foo', meta, Column('bar', String, primary_key=True))
+        meta.create_all()
+        
+        from sqlalchemy.engine.reflection import Inspector
+        try:
+            inspector = Inspector(testing.db)
+            eq_(inspector.get_indexes('foo'), [])
+            eq_(
+                inspector.get_indexes('foo', include_auto_indexes=True), 
+                [{'unique': 1, 'name': u'sqlite_autoindex_foo_1', 'column_names': [u'bar']}]
+            )
+        finally:
+            meta.drop_all()
+        
+        
     def test_set_isolation_level(self):
         """Test setting the read uncommitted/serializable levels"""
         eng = create_engine(testing.db.url)