<s> import abc <EOL> from django . db . models . base import ModelBase <EOL> from six import string_types , with_metaclass <EOL> import querybuilder <EOL> from querybuilder . fields import FieldFactory <EOL> class TableFactory ( object ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , table , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> table_type = type ( table ) <EOL> if table_type is dict : <EOL> kwargs . update ( alias = list ( table . keys ( ) ) [ <NUM_LIT:0> ] ) <EOL> table = list ( table . values ( ) ) [ <NUM_LIT:0> ] <EOL> table_type = type ( table ) <EOL> if isinstance ( table , string_types ) : <EOL> return SimpleTable ( table , ** kwargs ) <EOL> elif table_type is ModelBase : <EOL> return ModelTable ( table , ** kwargs ) <EOL> elif table_type is querybuilder . query . Query : <EOL> return QueryTable ( table , ** kwargs ) <EOL> elif isinstance ( table , Table ) : <EOL> for key , value in kwargs . items ( ) : <EOL> setattr ( table , key , value ) <EOL> return table <EOL> return None <EOL> class Table ( with_metaclass ( abc . ABCMeta , object ) ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , table = None , fields = None , schema = None , extract_fields = False , prefix_fields = False , <EOL> field_prefix = None , owner = None , alias = None ) : <EOL> """<STR_LIT>""" <EOL> self . table = table <EOL> self . owner = owner <EOL> self . name = None <EOL> self . alias = alias <EOL> self . auto_alias = None <EOL> self . fields = [ ] <EOL> self . schema = schema <EOL> self . extract_fields = extract_fields <EOL> self . prefix_fields = prefix_fields <EOL> self . field_prefix = field_prefix <EOL> self . init_defaults ( ) <EOL> if fields : <EOL> self . set_fields ( fields ) <EOL> def init_defaults ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def get_sql ( self ) : <EOL> """<STR_LIT>""" <EOL> alias = self . get_alias ( ) <EOL> if alias : <EOL> return '<STR_LIT>' . format ( self . get_from_name ( ) , alias ) <EOL> return self . get_identifier ( ) <EOL> def get_alias ( self ) : <EOL> """<STR_LIT>""" <EOL> alias = None <EOL> if self . alias : <EOL> alias = self . alias <EOL> elif self . auto_alias : <EOL> alias = self . auto_alias <EOL> return alias <EOL> def get_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . name <EOL> def get_from_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . name <EOL> def get_identifier ( self ) : <EOL> """<STR_LIT>""" <EOL> alias = self . get_alias ( ) <EOL> if alias : <EOL> return alias <EOL> return self . get_name ( ) <EOL> def add_field ( self , field ) : <EOL> """<STR_LIT>""" <EOL> field = FieldFactory ( <EOL> field , <EOL> ) <EOL> field . set_table ( self ) <EOL> field_name = field . get_name ( ) <EOL> for existing_field in self . fields : <EOL> if existing_field . get_name ( ) == field_name : <EOL> return None <EOL> self . before_add_field ( field ) <EOL> field . before_add ( ) <EOL> if field . ignore is False : <EOL> self . fields . append ( field ) <EOL> return field <EOL> def remove_field ( self , field ) : <EOL> """<STR_LIT>""" <EOL> new_field = FieldFactory ( <EOL> field , <EOL> ) <EOL> new_field . set_table ( self ) <EOL> new_field_identifier = new_field . get_identifier ( ) <EOL> for field in self . fields : <EOL> if field . get_identifier ( ) == new_field_identifier : <EOL> self . fields . remove ( field ) <EOL> return field <EOL> return None <EOL> def before_add_field ( self , field ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def set_fields ( self , fields ) : <EOL> """<STR_LIT>""" <EOL> self . fields = [ ] <EOL> self . add_fields ( fields ) <EOL> def add_fields ( self , fields ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( fields , string_types ) : <EOL> fields = [ fields ] <EOL> elif type ( fields ) is tuple : <EOL> fields = list ( fields ) <EOL> field_objects = [ self . add_field ( field ) for field in fields ] <EOL> return field_objects <EOL> def get_field_sql ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ field . get_sql ( ) for field in self . fields ] <EOL> def get_field_names ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ field . get_name ( ) for field in self . fields ] <EOL> def get_field_identifiers ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ field . get_identifier ( ) for field in self . fields ] <EOL> def get_field_prefix ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . field_prefix or self . get_identifier ( ) <EOL> def find_field ( self , field = None , alias = None ) : <EOL> """<STR_LIT>""" <EOL> if alias : <EOL> field = alias <EOL> field = FieldFactory ( field , table = self , alias = alias ) <EOL> identifier = field . get_identifier ( ) <EOL> for field in self . fields : <EOL> if field . get_identifier ( ) == identifier : <EOL> return field <EOL> return None <EOL> class SimpleTable ( Table ) : <EOL> """<STR_LIT>""" <EOL> def init_defaults ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( SimpleTable , self ) . init_defaults ( ) <EOL> self . name = self . table <EOL> class ModelTable ( Table ) : <EOL> """<STR_LIT>""" <EOL> def init_defaults ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( ModelTable , self ) . init_defaults ( ) <EOL> self . model = self . table <EOL> self . name = self . model . _meta . db_table <EOL> def before_add_field ( self , field ) : <EOL> """<STR_LIT>""" <EOL> if self . extract_fields and field . name == '<STR_LIT:*>' : <EOL> field . ignore = True <EOL> fields = [ model_field . column for model_field in self . model . _meta . fields ] <EOL> self . add_fields ( fields ) <EOL> class QueryTable ( Table ) : <EOL> """<STR_LIT>""" <EOL> def init_defaults ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( QueryTable , self ) . init_defaults ( ) <EOL> self . query = self . table <EOL> self . query . is_inner = True <EOL> def get_sql ( self ) : <EOL> return self . get_identifier ( ) <EOL> def get_from_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . format ( self . query . get_sql ( ) ) <EOL> def get_with_sql ( self ) : <EOL> return '<STR_LIT>' . format ( self . get_identifier ( ) , self . query . get_sql ( ) ) </s>
