]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
patch for [ticket:105], adding "owner" support to oracle...not tested yet
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 19 Jul 2006 23:50:00 +0000 (23:50 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 19 Jul 2006 23:50:00 +0000 (23:50 +0000)
lib/sqlalchemy/databases/oracle.py
lib/sqlalchemy/schema.py

index d184291fd532d6ff371c9e6d34bf7116e56ad544..519f5cb65c9ecb79bdc23b9dc36efe4c6eb5d1d7 100644 (file)
@@ -89,6 +89,7 @@ FROM all_constraints ac,
   all_cons_columns rem
 WHERE ac.table_name = :table_name
 AND ac.constraint_type IN ('R','P')
+AND ac.owner = :owner
 AND ac.owner = loc.owner
 AND ac.constraint_name = loc.constraint_name
 AND ac.r_owner = rem.owner(+)
@@ -166,9 +167,24 @@ class OracleDialect(ansisql.ANSIDialect):
         return bool( cursor.fetchone() is not None )
         
     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()})
+        c = connection.execute ("select distinct OWNER from ALL_TAB_COLUMNS where TABLE_NAME = :table_name", {'table_name':table.name.upper()})
+        rows = c.fetchall()
+        if not rows :
+            raise exceptions.NoSuchTableError(table.name)
+        else:
+            if table.owner is not None:
+                if table.owner.upper() in [r[0] for r in rows]:
+                    owner = table.owner.upper()
+                else:
+                    raise exceptions.AssertionError("Specified owner %s does not own table %s"%(table.owner, table.name))
+            else:
+                if len(rows)==1:
+                    owner = rows[0][0]
+                else:
+                    raise exceptions.AssertionError("There are multiple tables with name %s in the schema, you must specifie owner"%table.name)
+
+        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 and OWNER = :owner", {'table_name':table.name.upper(), 'owner':owner})
         
-        found_table = False
         while True:
             row = c.fetchone()
             if row is None:
@@ -203,10 +219,8 @@ 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()})
+       
+        c = connection.execute(constraintSQL, {'table_name' : table.name.upper(), 'owner' : owner})
         while True:
             row = c.fetchone()
             if row is None:
@@ -216,6 +230,7 @@ class OracleDialect(ansisql.ANSIDialect):
             if cons_type == 'P':
                 table.c[local_column]._set_primary_key()
             elif cons_type == 'R':
+                #table.append_item(ForeignKeyConstraint(value[0], value[1], name=name))
                 table.c[local_column].append_item(
                     schema.ForeignKey(schema.Table(remote_table,
                                             table.metadata,
index 6b44cda2980597532d16a52414d47baf3112a858..002ff2d36c4fa1532da49d2fd26e69b8046b007d 100644 (file)
@@ -140,6 +140,8 @@ class Table(SchemaItem, sql.TableClause):
         the rest of the constructor arguments.  If this flag and the "redefine" flag are not set, constructing 
         the same table twice will result in an exception.
         
+        owner=None : optional owning user of this table.  useful for databases such as Oracle to aid in table
+        reflection.
         """
         super(Table, self).__init__(name)
         self._metadata = metadata
@@ -152,6 +154,7 @@ class Table(SchemaItem, sql.TableClause):
             self.fullname = "%s.%s" % (self.schema, self.name)
         else:
             self.fullname = self.name
+        self.owner = kwargs.pop('owner', None)
         self.kwargs = kwargs
 
     def _set_primary_key(self, pk):