<s> from django . db . backends import BaseDatabaseIntrospection <EOL> from pymysql import ProgrammingError , OperationalError <EOL> from pymysql . constants import FIELD_TYPE <EOL> import re <EOL> from . py3 import iteritems <EOL> foreign_key_re = re . compile ( r"<STR_LIT>" ) <EOL> class DatabaseIntrospection ( BaseDatabaseIntrospection ) : <EOL> data_types_reverse = { <EOL> FIELD_TYPE . BLOB : '<STR_LIT>' , <EOL> FIELD_TYPE . CHAR : '<STR_LIT>' , <EOL> FIELD_TYPE . DECIMAL : '<STR_LIT>' , <EOL> FIELD_TYPE . NEWDECIMAL : '<STR_LIT>' , <EOL> FIELD_TYPE . DATE : '<STR_LIT>' , <EOL> FIELD_TYPE . DATETIME : '<STR_LIT>' , <EOL> FIELD_TYPE . DOUBLE : '<STR_LIT>' , <EOL> FIELD_TYPE . FLOAT : '<STR_LIT>' , <EOL> FIELD_TYPE . INT24 : '<STR_LIT>' , <EOL> FIELD_TYPE . LONG : '<STR_LIT>' , <EOL> FIELD_TYPE . LONGLONG : '<STR_LIT>' , <EOL> FIELD_TYPE . SHORT : '<STR_LIT>' , <EOL> FIELD_TYPE . STRING : '<STR_LIT>' , <EOL> FIELD_TYPE . TIMESTAMP : '<STR_LIT>' , <EOL> FIELD_TYPE . TINY : '<STR_LIT>' , <EOL> FIELD_TYPE . TINY_BLOB : '<STR_LIT>' , <EOL> FIELD_TYPE . MEDIUM_BLOB : '<STR_LIT>' , <EOL> FIELD_TYPE . LONG_BLOB : '<STR_LIT>' , <EOL> FIELD_TYPE . VAR_STRING : '<STR_LIT>' , <EOL> } <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> cursor . execute ( "<STR_LIT>" % self . connection . ops . quote_name ( table_name ) ) <EOL> return cursor . description <EOL> def _name_to_index ( self , cursor , table_name ) : <EOL> """<STR_LIT>""" <EOL> return dict ( [ ( d [ <NUM_LIT:0> ] , i ) for i , d in enumerate ( self . get_table_description ( cursor , table_name ) ) ] ) <EOL> def get_relations ( self , cursor , table_name ) : <EOL> """<STR_LIT>""" <EOL> my_field_dict = self . _name_to_index ( cursor , table_name ) <EOL> constraints = self . get_key_columns ( cursor , table_name ) <EOL> relations = { } <EOL> for my_fieldname , other_table , other_field in constraints : <EOL> other_field_index = self . _name_to_index ( cursor , other_table ) [ other_field ] <EOL> my_field_index = my_field_dict [ my_fieldname ] <EOL> relations [ my_field_index ] = ( other_field_index , other_table ) <EOL> return relations <EOL> def get_key_columns ( self , cursor , table_name ) : <EOL> """<STR_LIT>""" <EOL> key_columns = [ ] <EOL> try : <EOL> cursor . execute ( """<STR_LIT>""" , [ table_name ] ) <EOL> key_columns . extend ( cursor . fetchall ( ) ) <EOL> except ( ProgrammingError , OperationalError ) : <EOL> cursor . execute ( "<STR_LIT>" % self . connection . ops . quote_name ( table_name ) ) <EOL> for row in cursor . fetchall ( ) : <EOL> pos = <NUM_LIT:0> <EOL> while True : <EOL> match = foreign_key_re . search ( row [ <NUM_LIT:1> ] , pos ) <EOL> if match == None : <EOL> break <EOL> pos = match . end ( ) <EOL> key_columns . append ( match . groups ( ) ) <EOL> return key_columns <EOL> def get_primary_key_column ( self , cursor , table_name ) : <EOL> """<STR_LIT>""" <EOL> for column in iteritems ( self . get_indexes ( cursor , table_name ) ) : <EOL> if column [ <NUM_LIT:1> ] [ '<STR_LIT:primary_key>' ] : <EOL> return column [ <NUM_LIT:0> ] <EOL> return None <EOL> def get_indexes ( self , cursor , table_name ) : <EOL> """<STR_LIT>""" <EOL> cursor . execute ( "<STR_LIT>" % self . connection . ops . quote_name ( table_name ) ) <EOL> indexes = { } <EOL> for row in cursor . fetchall ( ) : <EOL> indexes [ row [ <NUM_LIT:4> ] ] = { '<STR_LIT:primary_key>' : ( row [ <NUM_LIT:2> ] == '<STR_LIT>' ) , '<STR_LIT>' : not bool ( row [ <NUM_LIT:1> ] ) } <EOL> return indexes </s>
