From: Mike Bayer Date: Tue, 6 Jun 2006 00:11:54 +0000 (+0000) Subject: added "NonExistentTable" exception throw to reflection, courtesy lbruno@republico... X-Git-Tag: rel_0_2_3~26 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bdb41655d14caed41e93225f79e5554925a15e20;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git added "NonExistentTable" exception throw to reflection, courtesy lbruno@republico.estv.ipv.pt, for [ticket:138] --- diff --git a/lib/sqlalchemy/databases/firebird.py b/lib/sqlalchemy/databases/firebird.py index b25d62c2d2..3eb287d9c7 100644 --- a/lib/sqlalchemy/databases/firebird.py +++ b/lib/sqlalchemy/databases/firebird.py @@ -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 diff --git a/lib/sqlalchemy/databases/information_schema.py b/lib/sqlalchemy/databases/information_schema.py index 55c5225586..3dc4a337b7 100644 --- a/lib/sqlalchemy/databases/information_schema.py +++ b/lib/sqlalchemy/databases/information_schema.py @@ -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: diff --git a/lib/sqlalchemy/databases/mssql.py b/lib/sqlalchemy/databases/mssql.py index 94fbd622f4..03197b00f9 100644 --- a/lib/sqlalchemy/databases/mssql.py +++ b/lib/sqlalchemy/databases/mssql.py @@ -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 diff --git a/lib/sqlalchemy/databases/mysql.py b/lib/sqlalchemy/databases/mysql.py index 0a480ec11b..aa05134d04 100644 --- a/lib/sqlalchemy/databases/mysql.py +++ b/lib/sqlalchemy/databases/mysql.py @@ -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 diff --git a/lib/sqlalchemy/databases/oracle.py b/lib/sqlalchemy/databases/oracle.py index 167059ed20..bf6c1fd8d4 100644 --- a/lib/sqlalchemy/databases/oracle.py +++ b/lib/sqlalchemy/databases/oracle.py @@ -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() diff --git a/lib/sqlalchemy/databases/sqlite.py b/lib/sqlalchemy/databases/sqlite.py index f074d02fa9..b2aeb75fd5 100644 --- a/lib/sqlalchemy/databases/sqlite.py +++ b/lib/sqlalchemy/databases/sqlite.py @@ -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() diff --git a/lib/sqlalchemy/exceptions.py b/lib/sqlalchemy/exceptions.py index c942ab4c2d..2713fb7543 100644 --- a/lib/sqlalchemy/exceptions.py +++ b/lib/sqlalchemy/exceptions.py @@ -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 diff --git a/test/engine/reflection.py b/test/engine/reflection.py index 85c97d704f..bb0fd95898 100644 --- a/test/engine/reflection.py +++ b/test/engine/reflection.py @@ -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,