示例#1
0
文件: sqlite3.py 项目: buriy/deseb2
    def get_table(self, cursor, table_name):
        table = DBTable(name = table_name)
        qn = self.connection.ops.quote_name
        
        cursor.execute("PRAGMA table_info(%s)" % qn(table_name))
        for row in cursor.fetchall():
            info = DBField(
                name = row[1], 
                primary_key = False, 
                foreign_key = None,
                unique = False, 
                allow_null = False,
                coltype = get_column_type(row[2])
            )
            table.fields.append(info)
            col = info.traits

            # f_default flag check goes here
            col['allow_null'] = (row[3]==0)
                
        cursor.execute("select sql from sqlite_master where name=%s;" % qn(table_name))
        
        for row in cursor.fetchall():
            table_description = [ s.strip() for s in row[0].split('\n')[1:-1] ]
            for column_description in table_description:
                #print table_name, "::", column_description
                if not column_description.startswith('"'): continue
                colname = column_description.split('"',2)[1]
                col = table.get_field(colname).traits
                col['primary_key'] = ' PRIMARY KEY' in column_description
                #col['foreign_key'] = ' REFERENCES ' in column_description or None
                col['unique'] = ' UNIQUE' in column_description or ' UNIQUE' in column_description  
        return table
示例#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