<s> from collections import OrderedDict <EOL> from . abstract_asset import AbstractAsset <EOL> from . identifier import Identifier <EOL> class Index ( AbstractAsset ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , columns , is_unique = False , is_primary = False , flags = None , options = None ) : <EOL> """<STR_LIT>""" <EOL> is_unique = is_unique or is_primary <EOL> self . _set_name ( name ) <EOL> self . _is_unique = is_unique <EOL> self . _is_primary = is_primary <EOL> self . _options = options or { } <EOL> self . _columns = OrderedDict ( ) <EOL> self . _flags = OrderedDict ( ) <EOL> for column in columns : <EOL> self . _add_column ( column ) <EOL> flags = flags or OrderedDict ( ) <EOL> for flag in flags : <EOL> self . add_flag ( flag ) <EOL> def _add_column ( self , column ) : <EOL> """<STR_LIT>""" <EOL> self . _columns [ column ] = Identifier ( column ) <EOL> def get_columns ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . _columns . keys ( ) ) <EOL> def get_quoted_columns ( self , platform ) : <EOL> """<STR_LIT>""" <EOL> columns = [ ] <EOL> for column in self . _columns . values ( ) : <EOL> columns . append ( column . get_quoted_name ( platform ) ) <EOL> return columns <EOL> def get_unquoted_columns ( self ) : <EOL> return list ( map ( self . _trim_quotes , self . get_columns ( ) ) ) <EOL> def is_simple_index ( self ) : <EOL> """<STR_LIT>""" <EOL> return not self . _is_primary and not self . _is_unique <EOL> def is_unique ( self ) : <EOL> return self . _is_unique <EOL> def is_primary ( self ) : <EOL> return self . _is_primary <EOL> def has_column_at_position ( self , column_name , pos = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> column_name = self . _trim_quotes ( column_name . lower ( ) ) <EOL> index_columns = [ c . lower ( ) for c in self . get_unquoted_columns ( ) ] <EOL> return index_columns . index ( column_name ) == pos <EOL> def spans_columns ( self , column_names ) : <EOL> """<STR_LIT>""" <EOL> columns = self . get_columns ( ) <EOL> number_of_columns = len ( columns ) <EOL> same_columns = True <EOL> for i in range ( number_of_columns ) : <EOL> column = self . _trim_quotes ( columns [ i ] . lower ( ) ) <EOL> if i >= len ( column_names ) or column != self . _trim_quotes ( column_names [ i ] . lower ( ) ) : <EOL> same_columns = False <EOL> return same_columns <EOL> def is_fullfilled_by ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if len ( other . get_columns ( ) ) != len ( self . get_columns ( ) ) : <EOL> return False <EOL> if not self . spans_columns ( other . get_columns ( ) ) : <EOL> return False <EOL> if not self . same_partial_index ( other ) : <EOL> return False <EOL> if self . is_simple_index ( ) : <EOL> return True <EOL> if other . is_primary ( ) != self . is_primary ( ) : <EOL> return False <EOL> if other . is_unique ( ) != self . is_unique ( ) : <EOL> return False <EOL> return True <EOL> def same_partial_index ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if ( self . has_option ( '<STR_LIT>' ) and other . has_option ( '<STR_LIT>' ) <EOL> and self . get_option ( '<STR_LIT>' ) == other . get_option ( '<STR_LIT>' ) ) : <EOL> return True <EOL> if not self . has_option ( '<STR_LIT>' ) and not other . has_option ( '<STR_LIT>' ) : <EOL> return True <EOL> return False <EOL> def overrules ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if other . is_primary ( ) : <EOL> return False <EOL> elif self . is_simple_index ( ) and other . is_unique ( ) : <EOL> return False <EOL> same_columns = self . spans_columns ( other . get_columns ( ) ) <EOL> if same_columns and ( self . is_primary ( ) or self . is_unique ( ) ) and self . same_partial_index ( other ) : <EOL> return True <EOL> return False <EOL> def get_flags ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . _flags . keys ( ) ) <EOL> def add_flag ( self , flag ) : <EOL> """<STR_LIT>""" <EOL> self . _flags [ flag . lower ( ) ] = True <EOL> return self <EOL> def has_flag ( self , flag ) : <EOL> """<STR_LIT>""" <EOL> return flag . lower ( ) in self . _flags <EOL> def remove_flag ( self , flag ) : <EOL> """<STR_LIT>""" <EOL> if self . has_flag ( flag ) : <EOL> del self . _flags [ flag . lower ( ) ] <EOL> def has_option ( self , name ) : <EOL> return name in self . _options <EOL> def get_option ( self , name ) : <EOL> return self . _options [ name ] <EOL> def get_options ( self ) : <EOL> return self . _options </s>
