#NEWLINE# def get_relations( self , cursor , table_name ) : #NEWLINE# #INDENT# '\n        Returns a dictionary of {field_index: (field_index_other_table, other_table)}\n        representing all relationships to the given table. Indexes are 0-based.\n        ' #NEWLINE# relations = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# cursor . execute ( 'SELECT sql FROM sqlite_master WHERE tbl_name = %s AND type = %s' , [ table_name , 'table' ] ) #NEWLINE# results = cursor . fetchone ( ) [ 0 ] . strip ( ) #NEWLINE# results = column [ ( results . index ( '(' ) + 1 ) : results . rindex ( ')' ) ] #NEWLINE# for ( field_index , field_desc ) in enumerate ( results . split ( ',' ) ) : #NEWLINE# #INDENT# field_desc = field_desc . strip ( ) #NEWLINE# if field_desc . startswith ( 'UNIQUE' ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# m = re . search ( 'references (.*) \\(["|](.*)["|]\\)' , field_desc , re . I ) #NEWLINE# if ( not m ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# ( table , column ) = [ s . strip ( '"' ) for s in m . groups ( ) ] #NEWLINE# cursor . execute ( 'SELECT sql FROM sqlite_master WHERE tbl_name = %s' , [ table ] ) #NEWLINE# result = cursor . fetchall ( ) [ 0 ] #NEWLINE# other_table_results = result [ 0 ] . strip ( ) #NEWLINE# ( li , ri ) = ( other_table_results . index ( '(' ) , other_table_results . rindex ( ')' ) ) #NEWLINE# other_table_results = other_table_results [ ( li + 1 ) : ri ] #NEWLINE# for ( other_index , other_desc ) in enumerate ( other_table_results . split ( ',' ) ) : #NEWLINE# #INDENT# other_desc = other_desc . strip ( ) #NEWLINE# if other_desc . startswith ( 'UNIQUE' ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# name = other_desc . split ( ' ' , 1 ) [ 0 ] . strip ( '"' ) #NEWLINE# if ( name == column ) : #NEWLINE# #INDENT# relations [ field_index ] = ( other_index , table ) #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return relations