Mike Bayer avatar Mike Bayer committed 96d8826

patch for [ticket:105], adding "owner" support to oracle...not tested yet

Comments (0)

Files changed (2)

lib/sqlalchemy/databases/oracle.py

   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(+)
         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:
             
             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:
             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,

lib/sqlalchemy/schema.py

         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
             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):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.