]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
(no commit message)
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 29 Oct 2005 19:31:45 +0000 (19:31 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 29 Oct 2005 19:31:45 +0000 (19:31 +0000)
lib/sqlalchemy/databases/postgres.py
lib/sqlalchemy/schema.py

index ce81cd7d6701dd4cdca01df04ccfd7ba7e1ec372..6a20e1704bc895aac0cf2db13c6f56c06931ce9a 100644 (file)
@@ -77,6 +77,30 @@ ischema_names = {
     'bytea' : PGBinary,
 }
 
+generic_engine = ansisql.engine()
+gen_columns = schema.Table("columns", generic_engine,
+    Column("table_schema", String),
+    Column("table_name", String),
+    Column("column_name", String),
+    Column("is_nullable", Integer),
+    Column("data_type", String),
+    Column("ordinal_position", Integer),
+    schema="information_schema")
+    
+gen_constraints = schema.Table("table_constraints", generic_engine,
+    Column("table_schema", String),
+    Column("table_name", String),
+    Column("constraint_name", String),
+    Column("constraint_type", String),
+    schema="information_schema")
+
+gen_column_constraints = schema.Table("constraint_column_usage", generic_engine,
+    Column("table_schema", String),
+    Column("table_name", String),
+    Column("column_name", String),
+    Column("constraint_name", String),
+    schema="information_schema")
+
 def engine(opts, **params):
     return PGSQLEngine(opts, **params)
 
@@ -178,29 +202,9 @@ class PGSQLEngine(ansisql.ANSISQLEngine):
         return self.module
 
     def reflecttable(self, table):
-        
-        columns = schema.Table("columns", table.engine,
-            Column("table_schema", String),
-            Column("table_name", String),
-            Column("column_name", String),
-            Column("is_nullable", Integer),
-            Column("data_type", String),
-            Column("ordinal_position", Integer),
-            schema="information_schema")
-            
-        constraints = schema.Table("table_constraints", table.engine,
-            Column("table_schema", String),
-            Column("table_name", String),
-            Column("constraint_name", String),
-            Column("constraint_type", String),
-            schema="information_schema")
-
-        column_constraints = schema.Table("constraint_column_usage", table.engine,
-            Column("table_schema", String),
-            Column("table_name", String),
-            Column("column_name", String),
-            Column("constraint_name", String),
-            schema="information_schema")
+        columns = gen_columns.toengine(table.engine)
+        constraints = gen_constraints.toengine(table.engine)
+        column_constraints = gen_column_constraints.toengine(table.engine)
         
         s = columns.select(columns.c.table_name==table.name, order_by=[columns.c.ordinal_position])
 
@@ -231,20 +235,16 @@ class PGSQLEngine(ansisql.ANSISQLEngine):
             if row is None:
                 break
             print "row! " + repr(row)
-            continue
-            (name, type, nullable, primary_key) = (row[1], row[2].upper(), not row[3], row[5])
+            (name, type, nullable, primary_key) = (row[columns.c.column_name], row[columns.c.data_type], not row[columns.c.is_nullable], row[constraints.c.constraint_type] is not None)
 
-            match = re.match(r'(\w+)(\(.*?\))?', type)
-            coltype = match.group(1)
-            args = match.group(2)
+            #match = re.match(r'(\w+)(\(.*?\))?', type)
+            #coltype = match.group(1)
+            #args = match.group(2)
 
             #print "coltype: " + repr(coltype) + " args: " + repr(args)
-            coltype = pragma_names[coltype]
-            if args is not None:
-                args = re.findall(r'(\d+)', args)
-                #print "args! " +repr(args)
-                coltype = coltype(*args)
+            coltype = ischema_names[type]
             table.append_item(schema.Column(name, coltype, primary_key = primary_key, nullable = nullable))
+        return
         c = self.execute("PRAGMA foreign_key_list(" + table.name + ")", {})
         while True:
             row = c.fetchone()
index 17755676b0a988d87045bd5c1499766462648d31..825fbe4a79508ed664fa5638c759a89ef0009948 100644 (file)
@@ -120,7 +120,7 @@ class Table(SchemaItem):
             args = []
             for c in self.columns:
                 args.append(c.copy())
-            return Table(self.name, engine, *args)
+            return Table(self.name, engine, schema=self.schema, *args)
 
 class Column(SchemaItem):
     """represents a column in a database table."""
@@ -155,7 +155,11 @@ class Column(SchemaItem):
 
     def copy(self):
         """creates a copy of this Column, unitialized"""
-        return Column(self.name, self.type, key = self.key, primary_key = self.primary_key, foreign_key = self.foreign_key.copy(), sequence = self.sequence)
+        if self.foreign_key is None:
+            fk = None
+        else:
+            fk = self.foreign_key.copy()
+        return Column(self.name, self.type, key = self.key, primary_key = self.primary_key, foreign_key = fk, sequence = self.sequence)
         
     def _make_proxy(self, selectable, name = None):
         """creates a copy of this Column, initialized the way this Column is"""