]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
added "NonExistentTable" exception throw to reflection, courtesy lbruno@republico...
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 6 Jun 2006 00:11:54 +0000 (00:11 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 6 Jun 2006 00:11:54 +0000 (00:11 +0000)
lib/sqlalchemy/databases/firebird.py
lib/sqlalchemy/databases/information_schema.py
lib/sqlalchemy/databases/mssql.py
lib/sqlalchemy/databases/mysql.py
lib/sqlalchemy/databases/oracle.py
lib/sqlalchemy/databases/sqlite.py
lib/sqlalchemy/exceptions.py
test/engine/reflection.py

index b25d62c2d20d2ba2fb1891301f3ca144a7bc1a6c..3eb287d9c7021b59041eb81be476c53c364685f3 100644 (file)
@@ -195,9 +195,11 @@ class FireBirdDialect(ansisql.ANSIDialect):
         #import pdb;pdb.set_trace()
         # get all of the fields for this table
         c = connection.execute(tblqry, [table.name.upper()])
+        found_table = False
         while True:
             row = c.fetchone()
             if not row: break
+            found_table = True
             args = [row['FNAME']]
             kw = {}
             # get the data types and lengths
@@ -208,6 +210,9 @@ class FireBirdDialect(ansisql.ANSIDialect):
             # is it a primary key?
             table.append_item(schema.Column(*args, **kw))
             # does the field have indexes
+        
+        if not found_table:
+            raise exceptions.NoSuchTableError(table.name)
 
     def last_inserted_ids(self):
         return self.context.last_inserted_ids
index 55c52255865b2808af82c9130680e8d2412ac050..3dc4a337b718229c358df1957da802394fbd300c 100644 (file)
@@ -3,7 +3,7 @@ import sqlalchemy.engine as engine
 import sqlalchemy.schema as schema
 import sqlalchemy.ansisql as ansisql
 import sqlalchemy.types as sqltypes
-from sqlalchemy.exceptions import *
+import sqlalchemy.exceptions as exceptions
 from sqlalchemy import *
 from sqlalchemy.ansisql import *
 
@@ -119,12 +119,14 @@ def reflecttable(connection, table, ischema_names, use_mysql=False):
         order_by=[columns.c.ordinal_position])
         
     c = connection.execute(s)
+    found_table = False
     while True:
         row = c.fetchone()
         if row is None:
             break
         #print "row! " + repr(row)
  #       continue
+        found_table = True
         (name, type, nullable, charlen, numericprec, numericscale, default) = (
             row[columns.c.column_name], 
             row[columns.c.data_type], 
@@ -146,6 +148,9 @@ def reflecttable(connection, table, ischema_names, use_mysql=False):
         if default is not None:
             colargs.append(PassiveDefault(sql.text(default)))
         table.append_item(schema.Column(name, coltype, nullable=nullable, *colargs))
+    
+    if not found_table:
+        raise exceptions.NoSuchTableError(table.name)
 
     s = select([constraints.c.constraint_name, constraints.c.constraint_type, constraints.c.table_name, key_constraints], use_labels=True, from_obj=[constraints.join(column_constraints, column_constraints.c.constraint_name==constraints.c.constraint_name).join(key_constraints, key_constraints.c.constraint_name==column_constraints.c.constraint_name)])
     if not use_mysql:
index 94fbd622f4386d178f102f76c1e7a7a3cb610b42..03197b00f903a5d80f6be0ecce82b5679ec47b75 100644 (file)
@@ -345,11 +345,12 @@ class MSSQLDialect(ansisql.ANSIDialect):
                    order_by=[columns.c.ordinal_position])
         
         c = connection.execute(s)
+        found_table = False
         while True:
             row = c.fetchone()
             if row is None:
                 break
-
+            found_table = True
             (name, type, nullable, charlen, numericprec, numericscale, default) = (
                 row[columns.c.column_name], 
                 row[columns.c.data_type], 
@@ -371,8 +372,10 @@ class MSSQLDialect(ansisql.ANSIDialect):
                 colargs.append(PassiveDefault(sql.text(default)))
                 
             table.append_item(schema.Column(name, coltype, nullable=nullable, *colargs))
-
-
+        
+        if not found_table:
+            raise exceptions.NoSuchTableError(table.name)
+        
         # We also run an sp_columns to check for identity columns:
         # FIXME: note that this only fetches the existence of an identity column, not it's properties like (seed, increment)
         #        also, add a check to make sure we specify the schema name of the table
index 0a480ec11ba2b58a6d86910195ab6e55cc041bb0..aa05134d0416dcbad644daad820709eb038b42da 100644 (file)
@@ -182,15 +182,18 @@ class MySQLDialect(ansisql.ANSIDialect):
         # to use information_schema:
         #ischema.reflecttable(self, table, ischema_names, use_mysql=True)
         
-        tabletype, foreignkeyD = self.moretableinfo(connection, table=table)
-        table.kwargs['mysql_engine'] = tabletype
-        
         c = connection.execute("describe " + table.name, {})
+        found_table = False
         while True:
             row = c.fetchone()
             if row is None:
                 break
             #print "row! " + repr(row)
+            if not found_table:
+                tabletype, foreignkeyD = self.moretableinfo(connection, table=table)
+                table.kwargs['mysql_engine'] = tabletype
+                found_table = True
+
             (name, type, nullable, primary_key, default) = (row[0], row[1], row[2] == 'YES', row[3] == 'PRI', row[4])
             
             match = re.match(r'(\w+)(\(.*?\))?', type)
@@ -214,6 +217,8 @@ class MySQLDialect(ansisql.ANSIDialect):
                                                    nullable=nullable,
                                                    default=default
                                                    )))
+        if not found_table:
+            raise exceptions.NoSuchTableError(table.name)
     
     def moretableinfo(self, connection, table):
         """Return (tabletype, {colname:foreignkey,...})
@@ -286,4 +291,4 @@ class MySQLSchemaDropper(ansisql.ANSISchemaDropper):
         self.append("\nDROP INDEX " + index.name + " ON " + index.table.name)
         self.execute()
 
-dialect = MySQLDialect
\ No newline at end of file
+dialect = MySQLDialect
index 167059ed20f4d9d8a2196c9e9dcba05571568cc1..bf6c1fd8d4b3c304c20af4a477ab5d5b94fdd37e 100644 (file)
@@ -168,10 +168,12 @@ class OracleDialect(ansisql.ANSIDialect):
     def reflecttable(self, connection, table):
         c = connection.execute ("select COLUMN_NAME, DATA_TYPE, DATA_LENGTH, DATA_PRECISION, DATA_SCALE, NULLABLE, DATA_DEFAULT from ALL_TAB_COLUMNS where TABLE_NAME = :table_name", {'table_name':table.name.upper()})
         
+        found_table = False
         while True:
             row = c.fetchone()
             if row is None:
                 break
+            found_table = True
 
             #print "ROW:" , row
             (name, coltype, length, precision, scale, nullable, default) = (row[0], row[1], row[2], row[3], row[4], row[5]=='Y', row[6])
@@ -201,7 +203,9 @@ class OracleDialect(ansisql.ANSIDialect):
             
             table.append_item (schema.Column(name, coltype, nullable=nullable, *colargs))
 
-   
+        if not found_table:
+            raise exceptions.NoSuchTableError(table.name)
+        
         c = connection.execute(constraintSQL, {'table_name' : table.name.upper()})
         while True:
             row = c.fetchone()
index f074d02fa9f071da9e479e00059019435c508586..b2aeb75fd590bf383fa90a499b97c7120f00cf5b 100644 (file)
@@ -158,11 +158,13 @@ class SQLiteDialect(ansisql.ANSIDialect):
 
     def reflecttable(self, connection, table):
         c = connection.execute("PRAGMA table_info(" + table.name + ")", {})
+        found_table = False
         while True:
             row = c.fetchone()
             if row is None:
                 break
             #print "row! " + repr(row)
+            found_table = True
             (name, type, nullable, primary_key) = (row[1], row[2].upper(), not row[3], row[5])
             
             match = re.match(r'(\w+)(\(.*?\))?', type)
@@ -176,6 +178,10 @@ class SQLiteDialect(ansisql.ANSIDialect):
                 #print "args! " +repr(args)
                 coltype = coltype(*[int(a) for a in args])
             table.append_item(schema.Column(name, coltype, primary_key = primary_key, nullable = nullable))
+        
+        if not found_table:
+            raise exceptions.NoSuchTableError(table.name)
+        
         c = connection.execute("PRAGMA foreign_key_list(" + table.name + ")", {})
         while True:
             row = c.fetchone()
index c942ab4c2d741912bd1888e4ec9cb6546814dfd0..2713fb7543b50f4babd262ada66b51e6ce5d4084 100644 (file)
@@ -34,6 +34,11 @@ class InvalidRequestError(SQLAlchemyError):
     This error generally corresponds to runtime state errors."""
     pass
 
+class NoSuchTableError(InvalidRequestError):
+    """sqlalchemy was asked to load a table's definition from the database,
+    but the table doesn't exist."""
+    pass
+
 class AssertionError(SQLAlchemyError):
     """corresponds to internal state being detected in an invalid state"""
     pass
index 85c97d704f47d801239c8f6867d12ad4a225e4d2..bb0fd95898088ec4e9714a97a8f7ae9380aee622 100644 (file)
@@ -3,6 +3,7 @@ import sqlalchemy.ansisql as ansisql
 import sqlalchemy.databases.postgres as postgres
 
 from sqlalchemy import *
+from sqlalchemy.exceptions import *
 
 from testbase import PersistTest
 import testbase
@@ -141,6 +142,11 @@ class ReflectionTest(PersistTest):
         assert not table2.c.name.nullable 
         assert table2.c.description.nullable 
         
+    def test_nonexistent(self):
+        self.assertRaises(NoSuchTableError, Table,
+                          'fake_table',
+                          testbase.db, autoload=True)
+        
     def testoverride(self):
         table = Table(
             'override_test', testbase.db,