<s> """<STR_LIT>""" <EOL> import copy <EOL> from django . utils . datastructures import SortedDict <EOL> from django . utils . encoding import force_text <EOL> from django . utils . tree import Node <EOL> from django . utils import six <EOL> from django . db import connections , DEFAULT_DB_ALIAS <EOL> from django . db . models import signals <EOL> from django . db . models . constants import LOOKUP_SEP <EOL> from django . db . models . expressions import ExpressionNode <EOL> from django . db . models . fields import FieldDoesNotExist <EOL> from django . db . models . sql import aggregates as base_aggregates_module <EOL> from django . db . models . sql . constants import ( QUERY_TERMS , ORDER_DIR , SINGLE , <EOL> ORDER_PATTERN , JoinInfo ) <EOL> from django . db . models . sql . datastructures import EmptyResultSet , Empty , MultiJoin <EOL> from django . db . models . sql . expressions import SQLEvaluator <EOL> from django . db . models . sql . where import ( WhereNode , Constraint , EverythingNode , <EOL> ExtraWhere , AND , OR ) <EOL> from django . core . exceptions import FieldError <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> class RawQuery ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , sql , using , params = None ) : <EOL> self . params = params or ( ) <EOL> self . sql = sql <EOL> self . using = using <EOL> self . cursor = None <EOL> self . low_mark , self . high_mark = <NUM_LIT:0> , None <EOL> self . extra_select = { } <EOL> self . aggregate_select = { } <EOL> def clone ( self , using ) : <EOL> return RawQuery ( self . sql , using , params = self . params ) <EOL> def convert_values ( self , value , field , connection ) : <EOL> """<STR_LIT>""" <EOL> return connection . ops . convert_values ( value , field ) <EOL> def get_columns ( self ) : <EOL> if self . cursor is None : <EOL> self . _execute_query ( ) <EOL> converter = connections [ self . using ] . introspection . table_name_converter <EOL> return [ converter ( column_meta [ <NUM_LIT:0> ] ) <EOL> for column_meta in self . cursor . description ] <EOL> def __iter__ ( self ) : <EOL> self . _execute_query ( ) <EOL> if not connections [ self . using ] . features . can_use_chunked_reads : <EOL> result = list ( self . cursor ) <EOL> else : <EOL> result = self . cursor <EOL> return iter ( result ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . sql % tuple ( self . params ) ) <EOL> def _execute_query ( self ) : <EOL> self . cursor = connections [ self . using ] . cursor ( ) <EOL> self . cursor . execute ( self . sql , self . params ) <EOL> class Query ( object ) : <EOL> """<STR_LIT>""" <EOL> INNER = '<STR_LIT>' <EOL> LOUTER = '<STR_LIT>' <EOL> alias_prefix = '<STR_LIT:T>' <EOL> query_terms = QUERY_TERMS <EOL> aggregates_module = base_aggregates_module <EOL> compiler = '<STR_LIT>' <EOL> def __init__ ( self , model , where = WhereNode ) : <EOL> self . model = model <EOL> self . alias_refcount = { } <EOL> self . alias_map = { } <EOL> self . table_map = { } <EOL> self . join_map = { } <EOL> self . default_cols = True <EOL> self . default_ordering = True <EOL> self . standard_ordering = True <EOL> self . ordering_aliases = [ ] <EOL> self . related_select_fields = [ ] <EOL> self . dupe_avoidance = { } <EOL> self . used_aliases = set ( ) <EOL> self . filter_is_sticky = False <EOL> self . included_inherited_models = { } <EOL> self . select = [ ] <EOL> self . select_fields = [ ] <EOL> self . tables = [ ] <EOL> self . where = where ( ) <EOL> self . where_class = where <EOL> self . group_by = None <EOL> self . having = where ( ) <EOL> self . order_by = [ ] <EOL> self . low_mark , self . high_mark = <NUM_LIT:0> , None <EOL> self . distinct = False <EOL> self . distinct_fields = [ ] <EOL> self . select_for_update = False <EOL> self . select_for_update_nowait = False <EOL> self . select_related = False <EOL> self . related_select_cols = [ ] <EOL> self . aggregates = SortedDict ( ) <EOL> self . aggregate_select_mask = None <EOL> self . _aggregate_select_cache = None <EOL> self . max_depth = <NUM_LIT:5> <EOL> self . extra = SortedDict ( ) <EOL> self . extra_select_mask = None <EOL> self . _extra_select_cache = None <EOL> self . extra_tables = ( ) <EOL> self . extra_order_by = ( ) <EOL> self . deferred_loading = ( set ( ) , True ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> sql , params = self . sql_with_params ( ) <EOL> return sql % params <EOL> def sql_with_params ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . get_compiler ( DEFAULT_DB_ALIAS ) . as_sql ( ) <EOL> def __deepcopy__ ( self , memo ) : <EOL> result = self . clone ( memo = memo ) <EOL> memo [ id ( self ) ] = result <EOL> return result <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> obj_dict = self . __dict__ . copy ( ) <EOL> obj_dict [ '<STR_LIT>' ] = [ ] <EOL> obj_dict [ '<STR_LIT>' ] = [ ] <EOL> obj_dict [ '<STR_LIT>' ] = [ <EOL> f is not None and f . name or None <EOL> for f in obj_dict [ '<STR_LIT>' ] <EOL> ] <EOL> return obj_dict <EOL> def __setstate__ ( self , obj_dict ) : <EOL> """<STR_LIT>""" <EOL> opts = obj_dict [ '<STR_LIT>' ] . _meta <EOL> obj_dict [ '<STR_LIT>' ] = [ <EOL> name is not None and opts . get_field ( name ) or None <EOL> for name in obj_dict [ '<STR_LIT>' ] <EOL> ] <EOL> self . __dict__ . update ( obj_dict ) <EOL> def prepare ( self ) : <EOL> return self <EOL> def get_compiler ( self , using = None , connection = None ) : <EOL> if using is None and connection is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if using : <EOL> connection = connections [ using ] <EOL> for alias , aggregate in self . aggregate_select . items ( ) : <EOL> connection . ops . check_aggregate_support ( aggregate ) <EOL> return connection . ops . compiler ( self . compiler ) ( self , connection , using ) <EOL> def get_meta ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . model . _meta <EOL> def clone ( self , klass = None , memo = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> obj = Empty ( ) <EOL> obj . __class__ = klass or self . __class__ <EOL> obj . model = self . model <EOL> obj . alias_refcount = self . alias_refcount . copy ( ) <EOL> obj . alias_map = self . alias_map . copy ( ) <EOL> obj . table_map = self . table_map . copy ( ) <EOL> obj . join_map = self . join_map . copy ( ) <EOL> obj . default_cols = self . default_cols <EOL> obj . default_ordering = self . default_ordering <EOL> obj . standard_ordering = self . standard_ordering <EOL> obj . included_inherited_models = self . included_inherited_models . copy ( ) <EOL> obj . ordering_aliases = [ ] <EOL> obj . select_fields = self . select_fields [ : ] <EOL> obj . related_select_fields = self . related_select_fields [ : ] <EOL> obj . dupe_avoidance = self . dupe_avoidance . copy ( ) <EOL> obj . select = self . select [ : ] <EOL> obj . tables = self . tables [ : ] <EOL> obj . where = copy . deepcopy ( self . where , memo = memo ) <EOL> obj . where_class = self . where_class <EOL> if self . group_by is None : <EOL> obj . group_by = None <EOL> else : <EOL> obj . group_by = self . group_by [ : ] <EOL> obj . having = copy . deepcopy ( self . having , memo = memo ) <EOL> obj . order_by = self . order_by [ : ] <EOL> obj . low_mark , obj . high_mark = self . low_mark , self . high_mark <EOL> obj . distinct = self . distinct <EOL> obj . distinct_fields = self . distinct_fields [ : ] <EOL> obj . select_for_update = self . select_for_update <EOL> obj . select_for_update_nowait = self . select_for_update_nowait <EOL> obj . select_related = self . select_related <EOL> obj . related_select_cols = [ ] <EOL> obj . aggregates = copy . deepcopy ( self . aggregates , memo = memo ) <EOL> if self . aggregate_select_mask is None : <EOL> obj . aggregate_select_mask = None <EOL> else : <EOL> obj . aggregate_select_mask = self . aggregate_select_mask . copy ( ) <EOL> obj . _aggregate_select_cache = None <EOL> obj . max_depth = self . max_depth <EOL> obj . extra = self . extra . copy ( ) <EOL> if self . extra_select_mask is None : <EOL> obj . extra_select_mask = None <EOL> else : <EOL> obj . extra_select_mask = self . extra_select_mask . copy ( ) <EOL> if self . _extra_select_cache is None : <EOL> obj . _extra_select_cache = None <EOL> else : <EOL> obj . _extra_select_cache = self . _extra_select_cache . copy ( ) <EOL> obj . extra_tables = self . extra_tables <EOL> obj . extra_order_by = self . extra_order_by <EOL> obj . deferred_loading = copy . deepcopy ( self . deferred_loading , memo = memo ) <EOL> if self . filter_is_sticky and self . used_aliases : <EOL> obj . used_aliases = self . used_aliases . copy ( ) <EOL> else : <EOL> obj . used_aliases = set ( ) <EOL> obj . filter_is_sticky = False <EOL> obj . __dict__ . update ( kwargs ) <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> obj . _setup_query ( ) <EOL> return obj <EOL> def convert_values ( self , value , field , connection ) : <EOL> """<STR_LIT>""" <EOL> return connection . ops . convert_values ( value , field ) <EOL> def resolve_aggregate ( self , value , aggregate , connection ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> if aggregate . is_ordinal : <EOL> return <NUM_LIT:0> <EOL> return value <EOL> elif aggregate . is_ordinal : <EOL> return int ( value ) <EOL> elif aggregate . is_computed : <EOL> return float ( value ) <EOL> else : <EOL> return self . convert_values ( value , aggregate . field , connection ) <EOL> def get_aggregation ( self , using ) : <EOL> """<STR_LIT>""" <EOL> if not self . aggregate_select : <EOL> return { } <EOL> if self . group_by is not None : <EOL> from django . db . models . sql . subqueries import AggregateQuery <EOL> query = AggregateQuery ( self . model ) <EOL> obj = self . clone ( ) <EOL> for alias , aggregate in self . aggregate_select . items ( ) : <EOL> if aggregate . is_summary : <EOL> query . aggregate_select [ alias ] = aggregate <EOL> del obj . aggregate_select [ alias ] <EOL> try : <EOL> query . add_subquery ( obj , using ) <EOL> except EmptyResultSet : <EOL> return dict ( <EOL> ( alias , None ) <EOL> for alias in query . aggregate_select <EOL> ) <EOL> else : <EOL> query = self <EOL> self . select = [ ] <EOL> self . default_cols = False <EOL> self . extra = { } <EOL> self . remove_inherited_models ( ) <EOL> query . clear_ordering ( True ) <EOL> query . clear_limits ( ) <EOL> query . select_for_update = False <EOL> query . select_related = False <EOL> query . related_select_cols = [ ] <EOL> query . related_select_fields = [ ] <EOL> result = query . get_compiler ( using ) . execute_sql ( SINGLE ) <EOL> if result is None : <EOL> result = [ None for q in query . aggregate_select . items ( ) ] <EOL> return dict ( [ <EOL> ( alias , self . resolve_aggregate ( val , aggregate , connection = connections [ using ] ) ) <EOL> for ( alias , aggregate ) , val <EOL> in zip ( query . aggregate_select . items ( ) , result ) <EOL> ] ) <EOL> def get_count ( self , using ) : <EOL> """<STR_LIT>""" <EOL> obj = self . clone ( ) <EOL> if len ( self . select ) > <NUM_LIT:1> or self . aggregate_select or ( self . distinct and self . distinct_fields ) : <EOL> from django . db . models . sql . subqueries import AggregateQuery <EOL> subquery = obj <EOL> subquery . clear_ordering ( True ) <EOL> subquery . clear_limits ( ) <EOL> obj = AggregateQuery ( obj . model ) <EOL> try : <EOL> obj . add_subquery ( subquery , using = using ) <EOL> except EmptyResultSet : <EOL> return <NUM_LIT:0> <EOL> obj . add_count_column ( ) <EOL> number = obj . get_aggregation ( using = using ) [ None ] <EOL> number = max ( <NUM_LIT:0> , number - self . low_mark ) <EOL> if self . high_mark is not None : <EOL> number = min ( number , self . high_mark - self . low_mark ) <EOL> return number <EOL> def has_results ( self , using ) : <EOL> q = self . clone ( ) <EOL> q . clear_select_clause ( ) <EOL> q . add_extra ( { '<STR_LIT:a>' : <NUM_LIT:1> } , None , None , None , None , None ) <EOL> q . set_extra_mask ( [ '<STR_LIT:a>' ] ) <EOL> q . clear_ordering ( True ) <EOL> q . set_limits ( high = <NUM_LIT:1> ) <EOL> compiler = q . get_compiler ( using = using ) <EOL> return bool ( compiler . execute_sql ( SINGLE ) ) <EOL> def combine ( self , rhs , connector ) : <EOL> """<STR_LIT>""" <EOL> assert self . model == rhs . model , "<STR_LIT>" <EOL> assert self . can_filter ( ) , "<STR_LIT>" <EOL> assert self . distinct == rhs . distinct , "<STR_LIT>" <EOL> assert self . distinct_fields == rhs . distinct_fields , "<STR_LIT>" <EOL> self . remove_inherited_models ( ) <EOL> change_map = { } <EOL> used = set ( ) <EOL> conjunction = ( connector == AND ) <EOL> first = True <EOL> for alias in rhs . tables : <EOL> if not rhs . alias_refcount [ alias ] : <EOL> continue <EOL> table , _ , join_type , lhs , lhs_col , col , _ = rhs . alias_map [ alias ] <EOL> promote = join_type == self . LOUTER <EOL> lhs = change_map . get ( lhs , lhs ) <EOL> new_alias = self . join ( ( lhs , table , lhs_col , col ) , <EOL> conjunction and not first , used , promote , not conjunction ) <EOL> used . add ( new_alias ) <EOL> change_map [ alias ] = new_alias <EOL> first = False <EOL> if not conjunction : <EOL> l_tables = set ( self . tables ) <EOL> r_tables = set ( rhs . tables ) <EOL> for alias in change_map : <EOL> if alias in r_tables : <EOL> if rhs . alias_refcount [ alias ] : <EOL> r_tables . remove ( alias ) <EOL> r_tables . add ( change_map [ alias ] ) <EOL> outer_tables = ( l_tables | r_tables ) - ( l_tables & r_tables ) <EOL> for alias in outer_tables : <EOL> if self . alias_refcount . get ( alias ) or rhs . alias_refcount . get ( alias ) : <EOL> self . promote_joins ( [ alias ] , True ) <EOL> if rhs . where : <EOL> w = copy . deepcopy ( rhs . where ) <EOL> w . relabel_aliases ( change_map ) <EOL> if not self . where : <EOL> self . where . add ( EverythingNode ( ) , AND ) <EOL> elif self . where : <EOL> w = self . where_class ( ) <EOL> w . add ( EverythingNode ( ) , AND ) <EOL> else : <EOL> w = self . where_class ( ) <EOL> self . where . add ( w , connector ) <EOL> self . select = [ ] <EOL> for col in rhs . select : <EOL> if isinstance ( col , ( list , tuple ) ) : <EOL> self . select . append ( ( change_map . get ( col [ <NUM_LIT:0> ] , col [ <NUM_LIT:0> ] ) , col [ <NUM_LIT:1> ] ) ) <EOL> else : <EOL> item = copy . deepcopy ( col ) <EOL> item . relabel_aliases ( change_map ) <EOL> self . select . append ( item ) <EOL> self . select_fields = rhs . select_fields [ : ] <EOL> if connector == OR : <EOL> if self . extra and rhs . extra : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . extra . update ( rhs . extra ) <EOL> extra_select_mask = set ( ) <EOL> if self . extra_select_mask is not None : <EOL> extra_select_mask . update ( self . extra_select_mask ) <EOL> if rhs . extra_select_mask is not None : <EOL> extra_select_mask . update ( rhs . extra_select_mask ) <EOL> if extra_select_mask : <EOL> self . set_extra_mask ( extra_select_mask ) <EOL> self . extra_tables += rhs . extra_tables <EOL> self . order_by = rhs . order_by and rhs . order_by [ : ] or self . order_by <EOL> self . extra_order_by = rhs . extra_order_by or self . extra_order_by <EOL> def deferred_to_data ( self , target , callback ) : <EOL> """<STR_LIT>""" <EOL> field_names , defer = self . deferred_loading <EOL> if not field_names : <EOL> return <EOL> orig_opts = self . model . _meta <EOL> seen = { } <EOL> must_include = { orig_opts . concrete_model : set ( [ orig_opts . pk ] ) } <EOL> for field_name in field_names : <EOL> parts = field_name . split ( LOOKUP_SEP ) <EOL> cur_model = self . model <EOL> opts = orig_opts <EOL> for name in parts [ : - <NUM_LIT:1> ] : <EOL> old_model = cur_model <EOL> source = opts . get_field_by_name ( name ) [ <NUM_LIT:0> ] <EOL> if is_reverse_o2o ( source ) : <EOL> cur_model = source . model <EOL> else : <EOL> cur_model = source . rel . to <EOL> opts = cur_model . _meta <EOL> if not is_reverse_o2o ( source ) : <EOL> must_include [ old_model ] . add ( source ) <EOL> add_to_dict ( must_include , cur_model , opts . pk ) <EOL> field , model , _ , _ = opts . get_field_by_name ( parts [ - <NUM_LIT:1> ] ) <EOL> if model is None : <EOL> model = cur_model <EOL> if not is_reverse_o2o ( field ) : <EOL> add_to_dict ( seen , model , field ) <EOL> if defer : <EOL> workset = { } <EOL> for model , values in six . iteritems ( seen ) : <EOL> for field , m in model . _meta . get_fields_with_model ( ) : <EOL> if field in values : <EOL> continue <EOL> add_to_dict ( workset , m or model , field ) <EOL> for model , values in six . iteritems ( must_include ) : <EOL> if model in workset : <EOL> workset [ model ] . update ( values ) <EOL> for model , values in six . iteritems ( workset ) : <EOL> callback ( target , model , values ) <EOL> else : <EOL> for model , values in six . iteritems ( must_include ) : <EOL> if model in seen : <EOL> seen [ model ] . update ( values ) <EOL> else : <EOL> seen [ model ] = values <EOL> for model in orig_opts . get_parent_list ( ) : <EOL> if model not in seen : <EOL> seen [ model ] = set ( ) <EOL> for model , values in six . iteritems ( seen ) : <EOL> callback ( target , model , values ) <EOL> def deferred_to_columns_cb ( self , target , model , fields ) : <EOL> """<STR_LIT>""" <EOL> table = model . _meta . db_table <EOL> if table not in target : <EOL> target [ table ] = set ( ) <EOL> for field in fields : <EOL> target [ table ] . add ( field . column ) <EOL> def table_alias ( self , table_name , create = False ) : <EOL> """<STR_LIT>""" <EOL> current = self . table_map . get ( table_name ) <EOL> if not create and current : <EOL> alias = current [ <NUM_LIT:0> ] <EOL> self . alias_refcount [ alias ] += <NUM_LIT:1> <EOL> return alias , False <EOL> if current : <EOL> alias = '<STR_LIT>' % ( self . alias_prefix , len ( self . alias_map ) + <NUM_LIT:1> ) <EOL> current . append ( alias ) <EOL> else : <EOL> alias = table_name <EOL> self . table_map [ alias ] = [ alias ] <EOL> self . alias_refcount [ alias ] = <NUM_LIT:1> <EOL> self . tables . append ( alias ) <EOL> return alias , True <EOL> def ref_alias ( self , alias ) : <EOL> """<STR_LIT>""" <EOL> self . alias_refcount [ alias ] += <NUM_LIT:1> <EOL> def unref_alias ( self , alias , amount = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . alias_refcount [ alias ] -= amount <EOL> def promote_joins ( self , aliases , unconditional = False ) : <EOL> """<STR_LIT>""" <EOL> aliases = list ( aliases ) <EOL> while aliases : <EOL> alias = aliases . pop ( <NUM_LIT:0> ) <EOL> if self . alias_map [ alias ] . rhs_join_col is None : <EOL> continue <EOL> parent_alias = self . alias_map [ alias ] . lhs_alias <EOL> parent_louter = ( parent_alias <EOL> and self . alias_map [ parent_alias ] . join_type == self . LOUTER ) <EOL> already_louter = self . alias_map [ alias ] . join_type == self . LOUTER <EOL> if ( ( unconditional or self . alias_map [ alias ] . nullable <EOL> or parent_louter ) and not already_louter ) : <EOL> data = self . alias_map [ alias ] . _replace ( join_type = self . LOUTER ) <EOL> self . alias_map [ alias ] = data <EOL> aliases . extend ( <EOL> join for join in self . alias_map . keys ( ) <EOL> if ( self . alias_map [ join ] . lhs_alias == alias <EOL> and join not in aliases ) ) <EOL> def reset_refcounts ( self , to_counts ) : <EOL> """<STR_LIT>""" <EOL> for alias , cur_refcount in self . alias_refcount . copy ( ) . items ( ) : <EOL> unref_amount = cur_refcount - to_counts . get ( alias , <NUM_LIT:0> ) <EOL> self . unref_alias ( alias , unref_amount ) <EOL> def promote_unused_aliases ( self , initial_refcounts , used_aliases ) : <EOL> """<STR_LIT>""" <EOL> for alias in self . tables : <EOL> if alias in used_aliases and ( alias not in initial_refcounts or <EOL> self . alias_refcount [ alias ] == initial_refcounts [ alias ] ) : <EOL> self . promote_joins ( [ alias ] ) <EOL> def change_aliases ( self , change_map ) : <EOL> """<STR_LIT>""" <EOL> assert set ( change_map . keys ( ) ) . intersection ( set ( change_map . values ( ) ) ) == set ( ) <EOL> self . where . relabel_aliases ( change_map ) <EOL> self . having . relabel_aliases ( change_map ) <EOL> for columns in [ self . select , self . group_by or [ ] ] : <EOL> for pos , col in enumerate ( columns ) : <EOL> if isinstance ( col , ( list , tuple ) ) : <EOL> old_alias = col [ <NUM_LIT:0> ] <EOL> columns [ pos ] = ( change_map . get ( old_alias , old_alias ) , col [ <NUM_LIT:1> ] ) <EOL> else : <EOL> col . relabel_aliases ( change_map ) <EOL> for mapping in [ self . aggregates ] : <EOL> for key , col in mapping . items ( ) : <EOL> if isinstance ( col , ( list , tuple ) ) : <EOL> old_alias = col [ <NUM_LIT:0> ] <EOL> mapping [ key ] = ( change_map . get ( old_alias , old_alias ) , col [ <NUM_LIT:1> ] ) <EOL> else : <EOL> col . relabel_aliases ( change_map ) <EOL> for k , aliases in self . join_map . items ( ) : <EOL> aliases = tuple ( [ change_map . get ( a , a ) for a in aliases ] ) <EOL> self . join_map [ k ] = aliases <EOL> for old_alias , new_alias in six . iteritems ( change_map ) : <EOL> alias_data = self . alias_map [ old_alias ] <EOL> alias_data = alias_data . _replace ( rhs_alias = new_alias ) <EOL> self . alias_refcount [ new_alias ] = self . alias_refcount [ old_alias ] <EOL> del self . alias_refcount [ old_alias ] <EOL> self . alias_map [ new_alias ] = alias_data <EOL> del self . alias_map [ old_alias ] <EOL> table_aliases = self . table_map [ alias_data . table_name ] <EOL> for pos , alias in enumerate ( table_aliases ) : <EOL> if alias == old_alias : <EOL> table_aliases [ pos ] = new_alias <EOL> break <EOL> for pos , alias in enumerate ( self . tables ) : <EOL> if alias == old_alias : <EOL> self . tables [ pos ] = new_alias <EOL> break <EOL> for key , alias in self . included_inherited_models . items ( ) : <EOL> if alias in change_map : <EOL> self . included_inherited_models [ key ] = change_map [ alias ] <EOL> for alias , data in six . iteritems ( self . alias_map ) : <EOL> lhs = data . lhs_alias <EOL> if lhs in change_map : <EOL> data = data . _replace ( lhs_alias = change_map [ lhs ] ) <EOL> self . alias_map [ alias ] = data <EOL> def bump_prefix ( self , exceptions = ( ) ) : <EOL> """<STR_LIT>""" <EOL> current = ord ( self . alias_prefix ) <EOL> assert current < ord ( '<STR_LIT>' ) <EOL> prefix = chr ( current + <NUM_LIT:1> ) <EOL> self . alias_prefix = prefix <EOL> change_map = SortedDict ( ) <EOL> for pos , alias in enumerate ( self . tables ) : <EOL> if alias in exceptions : <EOL> continue <EOL> new_alias = '<STR_LIT>' % ( prefix , pos ) <EOL> change_map [ alias ] = new_alias <EOL> self . tables [ pos ] = new_alias <EOL> self . change_aliases ( change_map ) <EOL> def get_initial_alias ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . tables : <EOL> alias = self . tables [ <NUM_LIT:0> ] <EOL> self . ref_alias ( alias ) <EOL> else : <EOL> alias = self . join ( ( None , self . model . _meta . db_table , None , None ) ) <EOL> return alias <EOL> def count_active_tables ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( [ <NUM_LIT:1> for count in self . alias_refcount . values ( ) if count ] ) <EOL> def join ( self , connection , always_create = False , exclusions = ( ) , <EOL> promote = False , outer_if_first = False , nullable = False , reuse = None ) : <EOL> """<STR_LIT>""" <EOL> lhs , table , lhs_col , col = connection <EOL> if lhs in self . alias_map : <EOL> lhs_table = self . alias_map [ lhs ] . table_name <EOL> else : <EOL> lhs_table = lhs <EOL> if reuse and always_create and table in self . table_map : <EOL> exclusions = set ( self . table_map [ table ] ) . difference ( reuse ) . union ( set ( exclusions ) ) <EOL> always_create = False <EOL> t_ident = ( lhs_table , table , lhs_col , col ) <EOL> if not always_create : <EOL> for alias in self . join_map . get ( t_ident , ( ) ) : <EOL> if alias not in exclusions : <EOL> if lhs_table and not self . alias_refcount [ self . alias_map [ alias ] . lhs_alias ] : <EOL> continue <EOL> if self . alias_map [ alias ] . lhs_alias != lhs : <EOL> continue <EOL> self . ref_alias ( alias ) <EOL> if promote or ( lhs and self . alias_map [ lhs ] . join_type == self . LOUTER ) : <EOL> self . promote_joins ( [ alias ] ) <EOL> return alias <EOL> alias , _ = self . table_alias ( table , True ) <EOL> if not lhs : <EOL> join_type = None <EOL> elif ( promote or outer_if_first <EOL> or self . alias_map [ lhs ] . join_type == self . LOUTER ) : <EOL> join_type = self . LOUTER <EOL> else : <EOL> join_type = self . INNER <EOL> join = JoinInfo ( table , alias , join_type , lhs , lhs_col , col , nullable ) <EOL> self . alias_map [ alias ] = join <EOL> if t_ident in self . join_map : <EOL> self . join_map [ t_ident ] += ( alias , ) <EOL> else : <EOL> self . join_map [ t_ident ] = ( alias , ) <EOL> return alias <EOL> def setup_inherited_models ( self ) : <EOL> """<STR_LIT>""" <EOL> opts = self . model . _meta . concrete_model . _meta <EOL> root_alias = self . tables [ <NUM_LIT:0> ] <EOL> seen = { None : root_alias } <EOL> for field , model in opts . get_fields_with_model ( ) : <EOL> if model not in seen : <EOL> link_field = opts . get_ancestor_link ( model ) <EOL> seen [ model ] = self . join ( ( root_alias , model . _meta . db_table , <EOL> link_field . column , model . _meta . pk . column ) ) <EOL> self . included_inherited_models = seen <EOL> def remove_inherited_models ( self ) : <EOL> """<STR_LIT>""" <EOL> for key , alias in self . included_inherited_models . items ( ) : <EOL> if key : <EOL> self . unref_alias ( alias ) <EOL> self . included_inherited_models = { } <EOL> def need_force_having ( self , q_object ) : <EOL> """<STR_LIT>""" <EOL> for child in q_object . children : <EOL> if isinstance ( child , Node ) : <EOL> if self . need_force_having ( child ) : <EOL> return True <EOL> else : <EOL> if child [ <NUM_LIT:0> ] . split ( LOOKUP_SEP ) [ <NUM_LIT:0> ] in self . aggregates : <EOL> return True <EOL> return False <EOL> def add_aggregate ( self , aggregate , model , alias , is_summary ) : <EOL> """<STR_LIT>""" <EOL> opts = model . _meta <EOL> field_list = aggregate . lookup . split ( LOOKUP_SEP ) <EOL> if len ( field_list ) == <NUM_LIT:1> and aggregate . lookup in self . aggregates : <EOL> field_name = field_list [ <NUM_LIT:0> ] <EOL> col = field_name <EOL> source = self . aggregates [ field_name ] <EOL> if not is_summary : <EOL> raise FieldError ( "<STR_LIT>" % ( <EOL> aggregate . name , field_name , field_name ) ) <EOL> elif ( ( len ( field_list ) > <NUM_LIT:1> ) or <EOL> ( field_list [ <NUM_LIT:0> ] not in [ i . name for i in opts . fields ] ) or <EOL> self . group_by is None or <EOL> not is_summary ) : <EOL> field , source , opts , join_list , last , _ = self . setup_joins ( <EOL> field_list , opts , self . get_initial_alias ( ) , False ) <EOL> col , _ , join_list = self . trim_joins ( source , join_list , last , False ) <EOL> self . promote_joins ( join_list , True ) <EOL> col = ( join_list [ - <NUM_LIT:1> ] , col ) <EOL> else : <EOL> field_name = field_list [ <NUM_LIT:0> ] <EOL> source = opts . get_field ( field_name ) <EOL> col = field_name <EOL> aggregate . add_to_query ( self , alias , col = col , source = source , is_summary = is_summary ) <EOL> def add_filter ( self , filter_expr , connector = AND , negate = False , trim = False , <EOL> can_reuse = None , process_extras = True , force_having = False ) : <EOL> """<STR_LIT>""" <EOL> arg , value = filter_expr <EOL> parts = arg . split ( LOOKUP_SEP ) <EOL> if not parts : <EOL> raise FieldError ( "<STR_LIT>" % arg ) <EOL> lookup_type = '<STR_LIT>' <EOL> num_parts = len ( parts ) <EOL> if ( len ( parts ) > <NUM_LIT:1> and parts [ - <NUM_LIT:1> ] in self . query_terms <EOL> and arg not in self . aggregates ) : <EOL> lookup_model = self . model <EOL> for counter , field_name in enumerate ( parts ) : <EOL> try : <EOL> lookup_field = lookup_model . _meta . get_field ( field_name ) <EOL> except FieldDoesNotExist : <EOL> lookup_type = parts . pop ( ) <EOL> break <EOL> if ( counter + <NUM_LIT:1> ) < num_parts : <EOL> try : <EOL> lookup_model = lookup_field . rel . to <EOL> except AttributeError : <EOL> lookup_type = parts . pop ( ) <EOL> break <EOL> having_clause = False <EOL> if value is None : <EOL> if lookup_type != '<STR_LIT>' : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> lookup_type = '<STR_LIT>' <EOL> value = True <EOL> elif callable ( value ) : <EOL> value = value ( ) <EOL> elif isinstance ( value , ExpressionNode ) : <EOL> value = SQLEvaluator ( value , self , reuse = can_reuse ) <EOL> having_clause = value . contains_aggregate <EOL> for alias , aggregate in self . aggregates . items ( ) : <EOL> if alias in ( parts [ <NUM_LIT:0> ] , LOOKUP_SEP . join ( parts ) ) : <EOL> entry = self . where_class ( ) <EOL> entry . add ( ( aggregate , lookup_type , value ) , AND ) <EOL> if negate : <EOL> entry . negate ( ) <EOL> self . having . add ( entry , connector ) <EOL> return <EOL> opts = self . get_meta ( ) <EOL> alias = self . get_initial_alias ( ) <EOL> allow_many = trim or not negate <EOL> try : <EOL> field , target , opts , join_list , last , extra_filters = self . setup_joins ( <EOL> parts , opts , alias , True , allow_many , allow_explicit_fk = True , <EOL> can_reuse = can_reuse , negate = negate , <EOL> process_extras = process_extras ) <EOL> except MultiJoin as e : <EOL> self . split_exclude ( filter_expr , LOOKUP_SEP . join ( parts [ : e . level ] ) , <EOL> can_reuse ) <EOL> return <EOL> table_promote = False <EOL> join_promote = False <EOL> if ( lookup_type == '<STR_LIT>' and value is True and not negate and <EOL> len ( join_list ) > <NUM_LIT:1> ) : <EOL> self . promote_joins ( join_list ) <EOL> join_promote = True <EOL> nonnull_comparison = ( lookup_type == '<STR_LIT>' and value is False ) <EOL> col , alias , join_list = self . trim_joins ( target , join_list , last , trim , <EOL> nonnull_comparison ) <EOL> if connector == OR : <EOL> join_it = iter ( join_list ) <EOL> table_it = iter ( self . tables ) <EOL> next ( join_it ) , next ( table_it ) <EOL> unconditional = False <EOL> for join in join_it : <EOL> table = next ( table_it ) <EOL> unconditional = ( unconditional or <EOL> self . alias_map [ join ] . join_type == self . LOUTER ) <EOL> if join == table and self . alias_refcount [ join ] > <NUM_LIT:1> : <EOL> continue <EOL> join_promote = join_promote or self . promote_joins ( [ join ] , unconditional ) <EOL> if table != join : <EOL> table_promote = self . promote_joins ( [ table ] ) <EOL> break <EOL> self . promote_joins ( join_it , join_promote ) <EOL> self . promote_joins ( table_it , table_promote or join_promote ) <EOL> if having_clause or force_having : <EOL> if ( alias , col ) not in self . group_by : <EOL> self . group_by . append ( ( alias , col ) ) <EOL> self . having . add ( ( Constraint ( alias , col , field ) , lookup_type , value ) , <EOL> connector ) <EOL> else : <EOL> self . where . add ( ( Constraint ( alias , col , field ) , lookup_type , value ) , <EOL> connector ) <EOL> if negate : <EOL> self . promote_joins ( join_list ) <EOL> if lookup_type != '<STR_LIT>' : <EOL> if len ( join_list ) > <NUM_LIT:1> : <EOL> for j_alias in join_list : <EOL> if self . alias_map [ j_alias ] . join_type == self . LOUTER : <EOL> j_col = self . alias_map [ j_alias ] . rhs_join_col <EOL> assert j_col is not None <EOL> entry = self . where_class ( ) <EOL> entry . add ( <EOL> ( Constraint ( j_alias , j_col , None ) , '<STR_LIT>' , True ) , <EOL> AND <EOL> ) <EOL> entry . negate ( ) <EOL> self . where . add ( entry , AND ) <EOL> break <EOL> if self . is_nullable ( field ) : <EOL> self . where . add ( ( Constraint ( alias , col , None ) , '<STR_LIT>' , False ) , AND ) <EOL> if can_reuse is not None : <EOL> can_reuse . update ( join_list ) <EOL> if process_extras : <EOL> for filter in extra_filters : <EOL> self . add_filter ( filter , negate = negate , can_reuse = can_reuse , <EOL> process_extras = False ) <EOL> def add_q ( self , q_object , used_aliases = None , force_having = False ) : <EOL> """<STR_LIT>""" <EOL> if used_aliases is None : <EOL> used_aliases = self . used_aliases <EOL> if hasattr ( q_object , '<STR_LIT>' ) : <EOL> q_object . add_to_query ( self , used_aliases ) <EOL> else : <EOL> if self . where and q_object . connector != AND and len ( q_object ) > <NUM_LIT:1> : <EOL> self . where . start_subtree ( AND ) <EOL> subtree = True <EOL> else : <EOL> subtree = False <EOL> connector = AND <EOL> if q_object . connector == OR and not force_having : <EOL> force_having = self . need_force_having ( q_object ) <EOL> for child in q_object . children : <EOL> if connector == OR : <EOL> refcounts_before = self . alias_refcount . copy ( ) <EOL> if force_having : <EOL> self . having . start_subtree ( connector ) <EOL> else : <EOL> self . where . start_subtree ( connector ) <EOL> if isinstance ( child , Node ) : <EOL> self . add_q ( child , used_aliases , force_having = force_having ) <EOL> else : <EOL> self . add_filter ( child , connector , q_object . negated , <EOL> can_reuse = used_aliases , force_having = force_having ) <EOL> if force_having : <EOL> self . having . end_subtree ( ) <EOL> else : <EOL> self . where . end_subtree ( ) <EOL> if connector == OR : <EOL> self . promote_unused_aliases ( refcounts_before , used_aliases ) <EOL> connector = q_object . connector <EOL> if q_object . negated : <EOL> self . where . negate ( ) <EOL> if subtree : <EOL> self . where . end_subtree ( ) <EOL> if self . filter_is_sticky : <EOL> self . used_aliases = used_aliases <EOL> def setup_joins ( self , names , opts , alias , dupe_multis , allow_many = True , <EOL> allow_explicit_fk = False , can_reuse = None , negate = False , <EOL> process_extras = True ) : <EOL> """<STR_LIT>""" <EOL> joins = [ alias ] <EOL> last = [ <NUM_LIT:0> ] <EOL> dupe_set = set ( ) <EOL> exclusions = set ( ) <EOL> extra_filters = [ ] <EOL> int_alias = None <EOL> for pos , name in enumerate ( names ) : <EOL> if int_alias is not None : <EOL> exclusions . add ( int_alias ) <EOL> exclusions . add ( alias ) <EOL> last . append ( len ( joins ) ) <EOL> if name == '<STR_LIT>' : <EOL> name = opts . pk . name <EOL> try : <EOL> field , model , direct , m2m = opts . get_field_by_name ( name ) <EOL> except FieldDoesNotExist : <EOL> for f in opts . fields : <EOL> if allow_explicit_fk and name == f . attname : <EOL> field , model , direct , m2m = opts . get_field_by_name ( f . name ) <EOL> break <EOL> else : <EOL> names = opts . get_all_field_names ( ) + list ( self . aggregate_select ) <EOL> raise FieldError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( name , "<STR_LIT:U+002CU+0020>" . join ( names ) ) ) <EOL> if not allow_many and ( m2m or not direct ) : <EOL> for alias in joins : <EOL> self . unref_alias ( alias ) <EOL> raise MultiJoin ( pos + <NUM_LIT:1> ) <EOL> if model : <EOL> proxied_model = opts . concrete_model <EOL> for int_model in opts . get_base_chain ( model ) : <EOL> if int_model is proxied_model : <EOL> opts = int_model . _meta <EOL> else : <EOL> lhs_col = opts . parents [ int_model ] . column <EOL> dedupe = lhs_col in opts . duplicate_targets <EOL> if dedupe : <EOL> exclusions . update ( self . dupe_avoidance . get ( <EOL> ( id ( opts ) , lhs_col ) , ( ) ) ) <EOL> dupe_set . add ( ( opts , lhs_col ) ) <EOL> opts = int_model . _meta <EOL> alias = self . join ( ( alias , opts . db_table , lhs_col , <EOL> opts . pk . column ) , exclusions = exclusions ) <EOL> joins . append ( alias ) <EOL> exclusions . add ( alias ) <EOL> for ( dupe_opts , dupe_col ) in dupe_set : <EOL> self . update_dupe_avoidance ( dupe_opts , dupe_col , <EOL> alias ) <EOL> cached_data = opts . _join_cache . get ( name ) <EOL> orig_opts = opts <EOL> dupe_col = direct and field . column or field . field . column <EOL> dedupe = dupe_col in opts . duplicate_targets <EOL> if dupe_set or dedupe : <EOL> if dedupe : <EOL> dupe_set . add ( ( opts , dupe_col ) ) <EOL> exclusions . update ( self . dupe_avoidance . get ( ( id ( opts ) , dupe_col ) , <EOL> ( ) ) ) <EOL> if process_extras and hasattr ( field , '<STR_LIT>' ) : <EOL> extra_filters . extend ( field . extra_filters ( names , pos , negate ) ) <EOL> if direct : <EOL> if m2m : <EOL> if cached_data : <EOL> ( table1 , from_col1 , to_col1 , table2 , from_col2 , <EOL> to_col2 , opts , target ) = cached_data <EOL> else : <EOL> table1 = field . m2m_db_table ( ) <EOL> from_col1 = opts . get_field_by_name ( <EOL> field . m2m_target_field_name ( ) ) [ <NUM_LIT:0> ] . column <EOL> to_col1 = field . m2m_column_name ( ) <EOL> opts = field . rel . to . _meta <EOL> table2 = opts . db_table <EOL> from_col2 = field . m2m_reverse_name ( ) <EOL> to_col2 = opts . get_field_by_name ( <EOL> field . m2m_reverse_target_field_name ( ) ) [ <NUM_LIT:0> ] . column <EOL> target = opts . pk <EOL> orig_opts . _join_cache [ name ] = ( table1 , from_col1 , <EOL> to_col1 , table2 , from_col2 , to_col2 , opts , <EOL> target ) <EOL> int_alias = self . join ( ( alias , table1 , from_col1 , to_col1 ) , <EOL> dupe_multis , exclusions , nullable = True , <EOL> reuse = can_reuse ) <EOL> if int_alias == table2 and from_col2 == to_col2 : <EOL> joins . append ( int_alias ) <EOL> alias = int_alias <EOL> else : <EOL> alias = self . join ( <EOL> ( int_alias , table2 , from_col2 , to_col2 ) , <EOL> dupe_multis , exclusions , nullable = True , <EOL> reuse = can_reuse ) <EOL> joins . extend ( [ int_alias , alias ] ) <EOL> elif field . rel : <EOL> if cached_data : <EOL> ( table , from_col , to_col , opts , target ) = cached_data <EOL> else : <EOL> opts = field . rel . to . _meta <EOL> target = field . rel . get_related_field ( ) <EOL> table = opts . db_table <EOL> from_col = field . column <EOL> to_col = target . column <EOL> orig_opts . _join_cache [ name ] = ( table , from_col , to_col , <EOL> opts , target ) <EOL> alias = self . join ( ( alias , table , from_col , to_col ) , <EOL> exclusions = exclusions , <EOL> nullable = self . is_nullable ( field ) ) <EOL> joins . append ( alias ) <EOL> else : <EOL> target = field <EOL> break <EOL> else : <EOL> orig_field = field <EOL> field = field . field <EOL> if m2m : <EOL> if cached_data : <EOL> ( table1 , from_col1 , to_col1 , table2 , from_col2 , <EOL> to_col2 , opts , target ) = cached_data <EOL> else : <EOL> table1 = field . m2m_db_table ( ) <EOL> from_col1 = opts . get_field_by_name ( <EOL> field . m2m_reverse_target_field_name ( ) ) [ <NUM_LIT:0> ] . column <EOL> to_col1 = field . m2m_reverse_name ( ) <EOL> opts = orig_field . opts <EOL> table2 = opts . db_table <EOL> from_col2 = field . m2m_column_name ( ) <EOL> to_col2 = opts . get_field_by_name ( <EOL> field . m2m_target_field_name ( ) ) [ <NUM_LIT:0> ] . column <EOL> target = opts . pk <EOL> orig_opts . _join_cache [ name ] = ( table1 , from_col1 , <EOL> to_col1 , table2 , from_col2 , to_col2 , opts , <EOL> target ) <EOL> int_alias = self . join ( ( alias , table1 , from_col1 , to_col1 ) , <EOL> dupe_multis , exclusions , nullable = True , <EOL> reuse = can_reuse ) <EOL> alias = self . join ( ( int_alias , table2 , from_col2 , to_col2 ) , <EOL> dupe_multis , exclusions , nullable = True , <EOL> reuse = can_reuse ) <EOL> joins . extend ( [ int_alias , alias ] ) <EOL> else : <EOL> if cached_data : <EOL> ( table , from_col , to_col , opts , target ) = cached_data <EOL> else : <EOL> local_field = opts . get_field_by_name ( <EOL> field . rel . field_name ) [ <NUM_LIT:0> ] <EOL> opts = orig_field . opts <EOL> table = opts . db_table <EOL> from_col = local_field . column <EOL> to_col = field . column <EOL> if orig_field . model is local_field . model : <EOL> target = opts . get_field_by_name ( <EOL> field . rel . field_name ) [ <NUM_LIT:0> ] <EOL> else : <EOL> target = opts . pk <EOL> orig_opts . _join_cache [ name ] = ( table , from_col , to_col , <EOL> opts , target ) <EOL> alias = self . join ( ( alias , table , from_col , to_col ) , <EOL> dupe_multis , exclusions , nullable = True , <EOL> reuse = can_reuse ) <EOL> joins . append ( alias ) <EOL> for ( dupe_opts , dupe_col ) in dupe_set : <EOL> if int_alias is None : <EOL> to_avoid = alias <EOL> else : <EOL> to_avoid = int_alias <EOL> self . update_dupe_avoidance ( dupe_opts , dupe_col , to_avoid ) <EOL> if pos != len ( names ) - <NUM_LIT:1> : <EOL> if pos == len ( names ) - <NUM_LIT:2> : <EOL> raise FieldError ( "<STR_LIT>" % ( name , names [ pos + <NUM_LIT:1> ] ) ) <EOL> else : <EOL> raise FieldError ( "<STR_LIT>" % name ) <EOL> return field , target , opts , joins , last , extra_filters <EOL> def trim_joins ( self , target , join_list , last , trim , nonnull_check = False ) : <EOL> """<STR_LIT>""" <EOL> final = len ( join_list ) <EOL> penultimate = last . pop ( ) <EOL> if penultimate == final : <EOL> penultimate = last . pop ( ) <EOL> if trim and final > <NUM_LIT:1> : <EOL> extra = join_list [ penultimate : ] <EOL> join_list = join_list [ : penultimate ] <EOL> final = penultimate <EOL> penultimate = last . pop ( ) <EOL> col = self . alias_map [ extra [ <NUM_LIT:0> ] ] . lhs_join_col <EOL> for alias in extra : <EOL> self . unref_alias ( alias ) <EOL> else : <EOL> col = target . column <EOL> alias = join_list [ - <NUM_LIT:1> ] <EOL> while final > <NUM_LIT:1> : <EOL> join = self . alias_map [ alias ] <EOL> if ( col != join . rhs_join_col or join . join_type != self . INNER or <EOL> nonnull_check ) : <EOL> break <EOL> self . unref_alias ( alias ) <EOL> alias = join . lhs_alias <EOL> col = join . lhs_join_col <EOL> join_list . pop ( ) <EOL> final -= <NUM_LIT:1> <EOL> if final == penultimate : <EOL> penultimate = last . pop ( ) <EOL> return col , alias , join_list <EOL> def update_dupe_avoidance ( self , opts , col , alias ) : <EOL> """<STR_LIT>""" <EOL> ident = id ( opts ) <EOL> for name in opts . duplicate_targets [ col ] : <EOL> try : <EOL> self . dupe_avoidance [ ident , name ] . add ( alias ) <EOL> except KeyError : <EOL> self . dupe_avoidance [ ident , name ] = set ( [ alias ] ) <EOL> def split_exclude ( self , filter_expr , prefix , can_reuse ) : <EOL> """<STR_LIT>""" <EOL> query = Query ( self . model ) <EOL> query . add_filter ( filter_expr ) <EOL> query . bump_prefix ( ) <EOL> query . clear_ordering ( True ) <EOL> query . set_start ( prefix ) <EOL> alias , col = query . select [ <NUM_LIT:0> ] <EOL> query . where . add ( ( Constraint ( alias , col , None ) , '<STR_LIT>' , False ) , AND ) <EOL> self . add_filter ( ( '<STR_LIT>' % prefix , query ) , negate = True , trim = True , <EOL> can_reuse = can_reuse ) <EOL> active_positions = len ( [ count for count <EOL> in query . alias_refcount . items ( ) if count ] ) <EOL> if active_positions > <NUM_LIT:1> : <EOL> self . add_filter ( ( '<STR_LIT>' % prefix , False ) , negate = True , <EOL> trim = True , can_reuse = can_reuse ) <EOL> def set_limits ( self , low = None , high = None ) : <EOL> """<STR_LIT>""" <EOL> if high is not None : <EOL> if self . high_mark is not None : <EOL> self . high_mark = min ( self . high_mark , self . low_mark + high ) <EOL> else : <EOL> self . high_mark = self . low_mark + high <EOL> if low is not None : <EOL> if self . high_mark is not None : <EOL> self . low_mark = min ( self . high_mark , self . low_mark + low ) <EOL> else : <EOL> self . low_mark = self . low_mark + low <EOL> def clear_limits ( self ) : <EOL> """<STR_LIT>""" <EOL> self . low_mark , self . high_mark = <NUM_LIT:0> , None <EOL> def can_filter ( self ) : <EOL> """<STR_LIT>""" <EOL> return not self . low_mark and self . high_mark is None <EOL> def clear_select_clause ( self ) : <EOL> """<STR_LIT>""" <EOL> self . select = [ ] <EOL> self . select_fields = [ ] <EOL> self . default_cols = False <EOL> self . select_related = False <EOL> self . set_extra_mask ( ( ) ) <EOL> self . set_aggregate_mask ( ( ) ) <EOL> def clear_select_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> self . select = [ ] <EOL> self . select_fields = [ ] <EOL> def add_distinct_fields ( self , * field_names ) : <EOL> """<STR_LIT>""" <EOL> self . distinct_fields = field_names <EOL> self . distinct = True <EOL> def add_fields ( self , field_names , allow_m2m = True ) : <EOL> """<STR_LIT>""" <EOL> alias = self . get_initial_alias ( ) <EOL> opts = self . get_meta ( ) <EOL> try : <EOL> for name in field_names : <EOL> field , target , u2 , joins , u3 , u4 = self . setup_joins ( <EOL> name . split ( LOOKUP_SEP ) , opts , alias , False , allow_m2m , <EOL> True ) <EOL> final_alias = joins [ - <NUM_LIT:1> ] <EOL> col = target . column <EOL> if len ( joins ) > <NUM_LIT:1> : <EOL> join = self . alias_map [ final_alias ] <EOL> if col == join . rhs_join_col : <EOL> self . unref_alias ( final_alias ) <EOL> final_alias = join . lhs_alias <EOL> col = join . lhs_join_col <EOL> joins = joins [ : - <NUM_LIT:1> ] <EOL> self . promote_joins ( joins [ <NUM_LIT:1> : ] ) <EOL> self . select . append ( ( final_alias , col ) ) <EOL> self . select_fields . append ( field ) <EOL> except MultiJoin : <EOL> raise FieldError ( "<STR_LIT>" % name ) <EOL> except FieldError : <EOL> if LOOKUP_SEP in name : <EOL> raise <EOL> else : <EOL> names = sorted ( opts . get_all_field_names ( ) + list ( self . extra ) <EOL> + list ( self . aggregate_select ) ) <EOL> raise FieldError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( name , "<STR_LIT:U+002CU+0020>" . join ( names ) ) ) <EOL> self . remove_inherited_models ( ) <EOL> def add_ordering ( self , * ordering ) : <EOL> """<STR_LIT>""" <EOL> errors = [ ] <EOL> for item in ordering : <EOL> if not ORDER_PATTERN . match ( item ) : <EOL> errors . append ( item ) <EOL> if errors : <EOL> raise FieldError ( '<STR_LIT>' % errors ) <EOL> if ordering : <EOL> self . order_by . extend ( ordering ) <EOL> else : <EOL> self . default_ordering = False <EOL> def clear_ordering ( self , force_empty = False ) : <EOL> """<STR_LIT>""" <EOL> self . order_by = [ ] <EOL> self . extra_order_by = ( ) <EOL> if force_empty : <EOL> self . default_ordering = False <EOL> def set_group_by ( self ) : <EOL> """<STR_LIT>""" <EOL> self . group_by = [ ] <EOL> for sel in self . select : <EOL> self . group_by . append ( sel ) <EOL> def add_count_column ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . distinct : <EOL> if not self . select : <EOL> count = self . aggregates_module . Count ( '<STR_LIT:*>' , is_summary = True ) <EOL> else : <EOL> assert len ( self . select ) == <NUM_LIT:1> , "<STR_LIT>" % self . select <EOL> count = self . aggregates_module . Count ( self . select [ <NUM_LIT:0> ] ) <EOL> else : <EOL> opts = self . model . _meta <EOL> if not self . select : <EOL> count = self . aggregates_module . Count ( ( self . join ( ( None , opts . db_table , None , None ) ) , opts . pk . column ) , <EOL> is_summary = True , distinct = True ) <EOL> else : <EOL> assert len ( self . select ) == <NUM_LIT:1> , "<STR_LIT>" <EOL> count = self . aggregates_module . Count ( self . select [ <NUM_LIT:0> ] , distinct = True ) <EOL> self . distinct = False <EOL> self . aggregates = { None : count } <EOL> self . set_aggregate_mask ( None ) <EOL> self . group_by = None <EOL> def add_select_related ( self , fields ) : <EOL> """<STR_LIT>""" <EOL> field_dict = { } <EOL> for field in fields : <EOL> d = field_dict <EOL> for part in field . split ( LOOKUP_SEP ) : <EOL> d = d . setdefault ( part , { } ) <EOL> self . select_related = field_dict <EOL> self . related_select_cols = [ ] <EOL> self . related_select_fields = [ ] <EOL> def add_extra ( self , select , select_params , where , params , tables , order_by ) : <EOL> """<STR_LIT>""" <EOL> if select : <EOL> select_pairs = SortedDict ( ) <EOL> if select_params : <EOL> param_iter = iter ( select_params ) <EOL> else : <EOL> param_iter = iter ( [ ] ) <EOL> for name , entry in select . items ( ) : <EOL> entry = force_text ( entry ) <EOL> entry_params = [ ] <EOL> pos = entry . find ( "<STR_LIT:%s>" ) <EOL> while pos != - <NUM_LIT:1> : <EOL> entry_params . append ( next ( param_iter ) ) <EOL> pos = entry . find ( "<STR_LIT:%s>" , pos + <NUM_LIT:2> ) <EOL> select_pairs [ name ] = ( entry , entry_params ) <EOL> self . extra . update ( select_pairs ) <EOL> if where or params : <EOL> self . where . add ( ExtraWhere ( where , params ) , AND ) <EOL> if tables : <EOL> self . extra_tables += tuple ( tables ) <EOL> if order_by : <EOL> self . extra_order_by = order_by <EOL> def clear_deferred_loading ( self ) : <EOL> """<STR_LIT>""" <EOL> self . deferred_loading = ( set ( ) , True ) <EOL> def add_deferred_loading ( self , field_names ) : <EOL> """<STR_LIT>""" <EOL> existing , defer = self . deferred_loading <EOL> if defer : <EOL> self . deferred_loading = existing . union ( field_names ) , True <EOL> else : <EOL> self . deferred_loading = existing . difference ( field_names ) , False <EOL> def add_immediate_loading ( self , field_names ) : <EOL> """<STR_LIT>""" <EOL> existing , defer = self . deferred_loading <EOL> field_names = set ( field_names ) <EOL> if '<STR_LIT>' in field_names : <EOL> field_names . remove ( '<STR_LIT>' ) <EOL> field_names . add ( self . model . _meta . pk . name ) <EOL> if defer : <EOL> self . deferred_loading = field_names . difference ( existing ) , False <EOL> else : <EOL> self . deferred_loading = field_names , False <EOL> def get_loaded_field_names ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _loaded_field_names_cache <EOL> except AttributeError : <EOL> collection = { } <EOL> self . deferred_to_data ( collection , self . get_loaded_field_names_cb ) <EOL> self . _loaded_field_names_cache = collection <EOL> return collection <EOL> def get_loaded_field_names_cb ( self , target , model , fields ) : <EOL> """<STR_LIT>""" <EOL> target [ model ] = set ( [ f . name for f in fields ] ) <EOL> def set_aggregate_mask ( self , names ) : <EOL> "<STR_LIT>" <EOL> if names is None : <EOL> self . aggregate_select_mask = None <EOL> else : <EOL> self . aggregate_select_mask = set ( names ) <EOL> self . _aggregate_select_cache = None <EOL> def set_extra_mask ( self , names ) : <EOL> """<STR_LIT>""" <EOL> if names is None : <EOL> self . extra_select_mask = None <EOL> else : <EOL> self . extra_select_mask = set ( names ) <EOL> self . _extra_select_cache = None <EOL> def _aggregate_select ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _aggregate_select_cache is not None : <EOL> return self . _aggregate_select_cache <EOL> elif self . aggregate_select_mask is not None : <EOL> self . _aggregate_select_cache = SortedDict ( [ <EOL> ( k , v ) for k , v in self . aggregates . items ( ) <EOL> if k in self . aggregate_select_mask <EOL> ] ) <EOL> return self . _aggregate_select_cache <EOL> else : <EOL> return self . aggregates <EOL> aggregate_select = property ( _aggregate_select ) <EOL> def _extra_select ( self ) : <EOL> if self . _extra_select_cache is not None : <EOL> return self . _extra_select_cache <EOL> elif self . extra_select_mask is not None : <EOL> self . _extra_select_cache = SortedDict ( [ <EOL> ( k , v ) for k , v in self . extra . items ( ) <EOL> if k in self . extra_select_mask <EOL> ] ) <EOL> return self . _extra_select_cache <EOL> else : <EOL> return self . extra <EOL> extra_select = property ( _extra_select ) <EOL> def set_start ( self , start ) : <EOL> """<STR_LIT>""" <EOL> opts = self . model . _meta <EOL> alias = self . get_initial_alias ( ) <EOL> field , col , opts , joins , last , extra = self . setup_joins ( <EOL> start . split ( LOOKUP_SEP ) , opts , alias , False ) <EOL> select_col = self . alias_map [ joins [ <NUM_LIT:1> ] ] . lhs_join_col <EOL> select_alias = alias <EOL> for alias in joins : <EOL> self . unref_alias ( alias ) <EOL> for alias in joins [ <NUM_LIT:1> : ] : <EOL> join_info = self . alias_map [ alias ] <EOL> if ( join_info . lhs_join_col != select_col <EOL> or join_info . join_type != self . INNER ) : <EOL> break <EOL> self . unref_alias ( select_alias ) <EOL> select_alias = join_info . rhs_alias <EOL> select_col = join_info . rhs_join_col <EOL> self . select = [ ( select_alias , select_col ) ] <EOL> self . remove_inherited_models ( ) <EOL> def is_nullable ( self , field ) : <EOL> """<STR_LIT>""" <EOL> if ( ( connections [ DEFAULT_DB_ALIAS ] . features . interprets_empty_strings_as_nulls ) <EOL> and field . empty_strings_allowed ) : <EOL> return True <EOL> else : <EOL> return field . null <EOL> def get_order_dir ( field , default = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> dirn = ORDER_DIR [ default ] <EOL> if field [ <NUM_LIT:0> ] == '<STR_LIT:->' : <EOL> return field [ <NUM_LIT:1> : ] , dirn [ <NUM_LIT:1> ] <EOL> return field , dirn [ <NUM_LIT:0> ] <EOL> def setup_join_cache ( sender , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> sender . _meta . _join_cache = { } <EOL> signals . class_prepared . connect ( setup_join_cache ) <EOL> def add_to_dict ( data , key , value ) : <EOL> """<STR_LIT>""" <EOL> if key in data : <EOL> data [ key ] . add ( value ) <EOL> else : <EOL> data [ key ] = set ( [ value ] ) <EOL> def is_reverse_o2o ( field ) : <EOL> """<STR_LIT>""" <EOL> return not hasattr ( field , '<STR_LIT>' ) and field . field . unique </s>
