Exemple #1
0
    def get_table(self, cursor, table_name):
        table = DBTable(name=table_name)
        cursor.execute("describe %s" % self.connection.ops.quote_name(table_name))
        for row in cursor.fetchall():
            #print 'T:', table_name, row
            column_name = row[0]
            coltype = row[1]
            if row[5]=='auto_increment':
                coltype += ' AUTO_INCREMENT'
            info = DBField(
                name = column_name,
                coltype = get_column_type(coltype), 
                primary_key = False,
                foreign_key = None,
                unique = False,
                allow_null = False
            )

            if row[2]=='YES': 
                info.allow_null = True
            
            if row[3]=='PRI': info.primary_key = True
            #if row[3]=='FOR': info.foreign_key = True
            if row[3]=='UNI': info.unique = True
            #if row[3]=='UNI': print 'unique:', row
            table.fields.append(info)
        # print table_name, column_name, info.traits
        return table
Exemple #2
0
    def get_table(self, cursor, table_name):
        table = DBTable(name = table_name)
        cursor.execute("SELECT a.attname, pg_catalog.format_type(a.atttypid, a.atttypmod), "
                       "(SELECT substring(d.adsrc for 128) FROM pg_catalog.pg_attrdef d "
                       "WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef), "
                       "a.attnotnull, a.attnum, pg_catalog.col_description(a.attrelid, a.attnum) "
                       "FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid "
                       "from pg_catalog.pg_class c where c.relname ~ '^%s$') "
                       "AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum" % table_name)
        for row in cursor.fetchall():
            info = DBField(
                name = row[0], 
                primary_key = False,
                foreign_key = None,
                unique = False,
                coltype = get_column_type(row[1]), 
                allow_null = False
            )
            table.fields.append(info)
            info.allow_null = not row[3]

        cursor.execute("""
            select pg_constraint.conname, pg_constraint.contype, pg_attribute.attname 
            from pg_constraint, pg_attribute, pg_class 
            where pg_constraint.conrelid=pg_class.oid and 
            pg_constraint.conrelid=pg_attribute.attrelid and 
            pg_attribute.attnum=all(pg_constraint.conkey) and 
            not(pg_constraint.contype = 'c') and 
            pg_class.relname = %s
            """, [table_name])
        for row in cursor.fetchall():
            info = table.get_field(row[2])
            if row[1]=='p': info.primary_key = True
            #if row[1]=='f' and not info.primary_key: info.foreign_key = True
            if row[1]=='u': info.unique= True
        # default value check
        cursor.execute("select pg_attribute.attname, adsrc from pg_attrdef, pg_attribute "
                       "WHERE pg_attrdef.adrelid=pg_attribute.attrelid and "
                       "pg_attribute.attnum=pg_attrdef.adnum and "
                       "pg_attrdef.adrelid = (SELECT c.oid "
                       "from pg_catalog.pg_class c where c.relname ~ '^%s$')" % table_name)
        
        for row in cursor.fetchall():
            info = table.get_field(row[0])
            if row[1][0:7] == 'nextval':
                if row[1].startswith("nextval('") and row[1].endswith("'::regclass)"):
                    info.sequence = row[1][9:-12]
        return table