<s> import re <EOL> from django . db . backends import BaseDatabaseIntrospection , FieldInfo <EOL> field_size_re = re . compile ( r'<STR_LIT>' ) <EOL> def get_field_size ( name ) : <EOL> """<STR_LIT>""" <EOL> m = field_size_re . search ( name ) <EOL> return int ( m . group ( <NUM_LIT:1> ) ) if m else None <EOL> class FlexibleFieldLookupDict ( object ) : <EOL> base_data_types_reverse = { <EOL> '<STR_LIT:bool>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:int>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:text>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:date>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : '<STR_LIT>' , <EOL> } <EOL> def __getitem__ ( self , key ) : <EOL> key = key . lower ( ) <EOL> try : <EOL> return self . base_data_types_reverse [ key ] <EOL> except KeyError : <EOL> size = get_field_size ( key ) <EOL> if size is not None : <EOL> return ( '<STR_LIT>' , { '<STR_LIT:max_length>' : size } ) <EOL> raise KeyError <EOL> class DatabaseIntrospection ( BaseDatabaseIntrospection ) : <EOL> data_types_reverse = FlexibleFieldLookupDict ( ) <EOL> def get_table_list ( self , cursor ) : <EOL> "<STR_LIT>" <EOL> cursor . execute ( """<STR_LIT>""" ) <EOL> return [ row [ <NUM_LIT:0> ] for row in cursor . fetchall ( ) ] <EOL> def get_table_description ( self , cursor , table_name ) : <EOL> "<STR_LIT>" <EOL> return [ FieldInfo ( info [ '<STR_LIT:name>' ] , info [ '<STR_LIT:type>' ] , None , info [ '<STR_LIT:size>' ] , None , None , <EOL> info [ '<STR_LIT>' ] ) for info in self . _table_info ( cursor , table_name ) ] <EOL> def get_relations ( self , cursor , table_name ) : <EOL> """<STR_LIT>""" <EOL> relations = { } <EOL> cursor . execute ( "<STR_LIT>" , [ table_name , "<STR_LIT>" ] ) <EOL> results = cursor . fetchone ( ) [ <NUM_LIT:0> ] . strip ( ) <EOL> results = results [ results . index ( '<STR_LIT:(>' ) + <NUM_LIT:1> : results . rindex ( '<STR_LIT:)>' ) ] <EOL> for field_index , field_desc in enumerate ( results . split ( '<STR_LIT:U+002C>' ) ) : <EOL> field_desc = field_desc . strip ( ) <EOL> if field_desc . startswith ( "<STR_LIT>" ) : <EOL> continue <EOL> m = re . search ( '<STR_LIT>' , field_desc , re . I ) <EOL> if not m : <EOL> continue <EOL> table , column = [ s . strip ( '<STR_LIT:">' ) for s in m . groups ( ) ] <EOL> cursor . execute ( "<STR_LIT>" , [ table ] ) <EOL> result = cursor . fetchall ( ) [ <NUM_LIT:0> ] <EOL> other_table_results = result [ <NUM_LIT:0> ] . strip ( ) <EOL> li , ri = other_table_results . index ( '<STR_LIT:(>' ) , other_table_results . rindex ( '<STR_LIT:)>' ) <EOL> other_table_results = other_table_results [ li + <NUM_LIT:1> : ri ] <EOL> for other_index , other_desc in enumerate ( other_table_results . split ( '<STR_LIT:U+002C>' ) ) : <EOL> other_desc = other_desc . strip ( ) <EOL> if other_desc . startswith ( '<STR_LIT>' ) : <EOL> continue <EOL> name = other_desc . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] . strip ( '<STR_LIT:">' ) <EOL> if name == column : <EOL> relations [ field_index ] = ( other_index , table ) <EOL> break <EOL> return relations <EOL> def get_key_columns ( self , cursor , table_name ) : <EOL> """<STR_LIT>""" <EOL> key_columns = [ ] <EOL> cursor . execute ( "<STR_LIT>" , [ table_name , "<STR_LIT>" ] ) <EOL> results = cursor . fetchone ( ) [ <NUM_LIT:0> ] . strip ( ) <EOL> results = results [ results . index ( '<STR_LIT:(>' ) + <NUM_LIT:1> : results . rindex ( '<STR_LIT:)>' ) ] <EOL> for field_index , field_desc in enumerate ( results . split ( '<STR_LIT:U+002C>' ) ) : <EOL> field_desc = field_desc . strip ( ) <EOL> if field_desc . startswith ( "<STR_LIT>" ) : <EOL> continue <EOL> m = re . search ( '<STR_LIT>' , field_desc , re . I ) <EOL> if not m : <EOL> continue <EOL> key_columns . append ( tuple ( [ s . strip ( '<STR_LIT:">' ) for s in m . groups ( ) ] ) ) <EOL> return key_columns <EOL> def get_indexes ( self , cursor , table_name ) : <EOL> indexes = { } <EOL> for info in self . _table_info ( cursor , table_name ) : <EOL> if info [ '<STR_LIT>' ] != <NUM_LIT:0> : <EOL> indexes [ info [ '<STR_LIT:name>' ] ] = { '<STR_LIT:primary_key>' : True , <EOL> '<STR_LIT>' : False } <EOL> cursor . execute ( '<STR_LIT>' % self . connection . ops . quote_name ( table_name ) ) <EOL> for index , unique in [ ( field [ <NUM_LIT:1> ] , field [ <NUM_LIT:2> ] ) for field in cursor . fetchall ( ) ] : <EOL> cursor . execute ( '<STR_LIT>' % self . connection . ops . quote_name ( index ) ) <EOL> info = cursor . fetchall ( ) <EOL> if len ( info ) != <NUM_LIT:1> : <EOL> continue <EOL> name = info [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] <EOL> indexes [ name ] = { '<STR_LIT:primary_key>' : False , <EOL> '<STR_LIT>' : unique } <EOL> return indexes <EOL> def get_primary_key_column ( self , cursor , table_name ) : <EOL> """<STR_LIT>""" <EOL> cursor . execute ( "<STR_LIT>" , [ table_name , "<STR_LIT>" ] ) <EOL> results = cursor . fetchone ( ) [ <NUM_LIT:0> ] . strip ( ) <EOL> results = results [ results . index ( '<STR_LIT:(>' ) + <NUM_LIT:1> : results . rindex ( '<STR_LIT:)>' ) ] <EOL> for field_desc in results . split ( '<STR_LIT:U+002C>' ) : <EOL> field_desc = field_desc . strip ( ) <EOL> m = re . search ( '<STR_LIT>' , field_desc ) <EOL> if m : <EOL> return m . groups ( ) [ <NUM_LIT:0> ] <EOL> return None <EOL> def _table_info ( self , cursor , name ) : <EOL> cursor . execute ( '<STR_LIT>' % self . connection . ops . quote_name ( name ) ) <EOL> return [ { '<STR_LIT:name>' : field [ <NUM_LIT:1> ] , <EOL> '<STR_LIT:type>' : field [ <NUM_LIT:2> ] , <EOL> '<STR_LIT:size>' : get_field_size ( field [ <NUM_LIT:2> ] ) , <EOL> '<STR_LIT>' : not field [ <NUM_LIT:3> ] , <EOL> '<STR_LIT>' : field [ <NUM_LIT:5> ] <EOL> } for field in cursor . fetchall ( ) ] </s>
