<s> """<STR_LIT>""" <EOL> import re <EOL> from django . db import models <EOL> from django . db . models . sql import compiler , query , where , constants , AND , OR <EOL> from django . db . models . sql . datastructures import EmptyResultSet <EOL> from . import subselect <EOL> from salesforce import DJANGO_18_PLUS , DJANGO_19_PLUS <EOL> DJANGO_17_EXACT = not DJANGO_18_PLUS <EOL> class SQLCompiler ( compiler . SQLCompiler ) : <EOL> """<STR_LIT>""" <EOL> soql_trans = None <EOL> def resolve_columns ( self , row , fields ) : <EOL> return [ row [ field . column ] for field in fields ] <EOL> def get_columns ( self , with_aliases = False ) : <EOL> """<STR_LIT>""" <EOL> soql_trans = self . query_topology ( ) <EOL> cols , col_params = compiler . SQLCompiler . get_columns ( self , with_aliases ) <EOL> out = [ ] <EOL> for col in cols : <EOL> if soql_trans and re . match ( r'<STR_LIT>' , col ) : <EOL> tab_name , col_name = col . split ( '<STR_LIT:.>' ) <EOL> out . append ( '<STR_LIT>' % ( soql_trans [ tab_name ] , col_name ) ) <EOL> else : <EOL> out . append ( col ) <EOL> cols = out <EOL> result = [ x . replace ( '<STR_LIT>' , '<STR_LIT:U+0020>' ) for x in cols ] <EOL> return ( result , col_params ) <EOL> def get_from_clause ( self ) : <EOL> """<STR_LIT>""" <EOL> self . query_topology ( ) <EOL> root_table = self . soql_trans [ self . root_alias ] <EOL> return [ root_table ] , [ ] <EOL> def quote_name_unless_alias ( self , name ) : <EOL> """<STR_LIT>""" <EOL> r = self . connection . ops . quote_name ( name ) <EOL> self . quote_cache [ name ] = r <EOL> return r <EOL> def execute_sql ( self , result_type = constants . MULTI ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> sql , params = self . as_sql ( ) <EOL> if not sql : <EOL> raise EmptyResultSet <EOL> except EmptyResultSet : <EOL> if result_type == constants . MULTI : <EOL> return iter ( [ ] ) <EOL> else : <EOL> return <EOL> cursor = self . connection . cursor ( self . query ) <EOL> cursor . execute ( sql , params ) <EOL> if not result_type or result_type == '<STR_LIT>' : <EOL> return cursor <EOL> if DJANGO_18_PLUS : <EOL> ordering_aliases = None <EOL> else : <EOL> ordering_aliases = self . ordering_aliases <EOL> if result_type == constants . SINGLE : <EOL> if ordering_aliases : <EOL> return cursor . fetchone ( ) [ : - len ( ordering_aliases ) ] <EOL> return cursor . fetchone ( ) <EOL> if ordering_aliases : <EOL> result = compiler . order_modified_iter ( cursor , len ( ordering_aliases ) , <EOL> self . connection . features . empty_fetchmany_value ) <EOL> else : <EOL> result = iter ( ( lambda : cursor . fetchmany ( constants . GET_ITERATOR_CHUNK_SIZE ) ) , <EOL> self . connection . features . empty_fetchmany_value ) <EOL> if not self . connection . features . can_use_chunked_reads : <EOL> return list ( result ) <EOL> return result <EOL> if DJANGO_18_PLUS : <EOL> def as_sql ( self , with_limits = True , with_col_aliases = False , subquery = False ) : <EOL> """<STR_LIT>""" <EOL> if DJANGO_19_PLUS : <EOL> if with_limits and self . query . low_mark == self . query . high_mark : <EOL> return '<STR_LIT>' , ( ) <EOL> self . subquery = subquery <EOL> refcounts_before = self . query . alias_refcount . copy ( ) <EOL> soql_trans = self . query_topology ( ) <EOL> try : <EOL> extra_select , order_by , group_by = self . pre_sql_setup ( ) <EOL> if with_limits and self . query . low_mark == self . query . high_mark : <EOL> return '<STR_LIT>' , ( ) <EOL> distinct_fields = self . get_distinct ( ) <EOL> from_ , f_params = self . get_from_clause ( ) <EOL> if DJANGO_19_PLUS : <EOL> where , w_params = self . compile ( self . where ) if self . where is not None else ( "<STR_LIT>" , [ ] ) <EOL> having , h_params = self . compile ( self . having ) if self . having is not None else ( "<STR_LIT>" , [ ] ) <EOL> else : <EOL> where , w_params = self . compile ( self . query . where ) <EOL> having , h_params = self . compile ( self . query . having ) <EOL> params = [ ] <EOL> result = [ '<STR_LIT>' ] <EOL> if self . query . distinct : <EOL> result . append ( self . connection . ops . distinct_sql ( distinct_fields ) ) <EOL> out_cols = [ ] <EOL> col_idx = <NUM_LIT:1> <EOL> for _ , ( s_sql , s_params ) , alias in self . select + extra_select : <EOL> if alias : <EOL> s_sql = '<STR_LIT>' % ( s_sql , self . connection . ops . quote_name ( alias ) ) <EOL> elif with_col_aliases : <EOL> s_sql = '<STR_LIT>' % ( s_sql , '<STR_LIT>' % col_idx ) <EOL> col_idx += <NUM_LIT:1> <EOL> if soql_trans and re . match ( r'<STR_LIT>' , s_sql ) : <EOL> tab_name , col_name = s_sql . split ( '<STR_LIT:.>' ) <EOL> s_sql = '<STR_LIT>' % ( soql_trans [ tab_name ] , col_name ) <EOL> params . extend ( s_params ) <EOL> out_cols . append ( s_sql ) <EOL> result . append ( '<STR_LIT:U+002CU+0020>' . join ( out_cols ) ) <EOL> result . append ( '<STR_LIT>' ) <EOL> result . extend ( from_ ) <EOL> params . extend ( f_params ) <EOL> if where : <EOL> result . append ( '<STR_LIT>' % where ) <EOL> params . extend ( w_params ) <EOL> grouping = [ ] <EOL> for g_sql , g_params in group_by : <EOL> grouping . append ( g_sql ) <EOL> params . extend ( g_params ) <EOL> if grouping : <EOL> if distinct_fields : <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" ) <EOL> if not order_by : <EOL> order_by = self . connection . ops . force_no_ordering ( ) <EOL> result . append ( '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( grouping ) ) <EOL> if having : <EOL> result . append ( '<STR_LIT>' % having ) <EOL> params . extend ( h_params ) <EOL> if order_by : <EOL> ordering = [ ] <EOL> for _ , ( o_sql , o_params , _ ) in order_by : <EOL> ordering . append ( o_sql ) <EOL> params . extend ( o_params ) <EOL> result . append ( '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( ordering ) ) <EOL> if with_limits : <EOL> if self . query . high_mark is not None : <EOL> result . append ( '<STR_LIT>' % ( self . query . high_mark - self . query . low_mark ) ) <EOL> if self . query . low_mark : <EOL> if self . query . high_mark is None : <EOL> val = self . connection . ops . no_limit_value ( ) <EOL> if val : <EOL> result . append ( '<STR_LIT>' % val ) <EOL> result . append ( '<STR_LIT>' % self . query . low_mark ) <EOL> if self . query . select_for_update and self . connection . features . has_select_for_update : <EOL> if self . connection . get_autocommit ( ) : <EOL> raise TransactionManagementError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> nowait = self . query . select_for_update_nowait <EOL> if nowait and not self . connection . features . has_select_for_update_nowait : <EOL> raise DatabaseError ( '<STR_LIT>' ) <EOL> result . append ( self . connection . ops . for_update_sql ( nowait = nowait ) ) <EOL> return '<STR_LIT:U+0020>' . join ( result ) , tuple ( params ) <EOL> finally : <EOL> self . query . reset_refcounts ( refcounts_before ) <EOL> def query_topology ( self , _alias_map_items = None ) : <EOL> if self . soql_trans is not None : <EOL> return self . soql_trans <EOL> if not _alias_map_items and not self . query . alias_map : <EOL> assert DJANGO_18_PLUS , "<STR_LIT>" <EOL> return [ ] <EOL> query = self . query <EOL> if _alias_map_items : <EOL> alias_map_items = _alias_map_items <EOL> elif DJANGO_18_PLUS : <EOL> alias_map_items = [ ( getattr ( v , '<STR_LIT>' , None ) , v . table_name , <EOL> getattr ( v , '<STR_LIT>' , None ) , v . table_alias <EOL> ) for v in query . alias_map . values ( ) ] <EOL> else : <EOL> alias_map_items = [ ( v . lhs_alias , v . table_name , v . join_cols , v . rhs_alias ) <EOL> for v in query . alias_map . values ( ) ] <EOL> alias2table = { } <EOL> side_l , side_r = set ( ) , set ( ) <EOL> for ( lhs , table , join_cols_ , rhs ) in alias_map_items : <EOL> alias2table [ rhs ] = table <EOL> if lhs is not None : <EOL> ( join_cols , ) = join_cols_ <EOL> assert len ( join_cols ) == <NUM_LIT:2> <EOL> if join_cols [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> assert join_cols [ <NUM_LIT:1> ] != '<STR_LIT>' <EOL> lhs , rhs = rhs , lhs <EOL> join_cols = join_cols [ <NUM_LIT:1> ] , join_cols [ <NUM_LIT:0> ] <EOL> assert join_cols [ <NUM_LIT:1> ] == '<STR_LIT>' <EOL> side_l . add ( lhs ) <EOL> side_r . add ( rhs ) <EOL> else : <EOL> side_l . add ( rhs ) <EOL> assert len ( alias2table ) == len ( alias_map_items ) <EOL> assert len ( side_l . union ( side_r ) ) == len ( alias_map_items ) <EOL> top_lhs_set = set ( side_l ) . difference ( side_r ) <EOL> assert len ( top_lhs_set ) == <NUM_LIT:1> , ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ( top_lhs , ) = top_lhs_set <EOL> self . root_alias = top_lhs <EOL> soql_trans = { top_lhs : alias2table [ top_lhs ] } <EOL> work_lhses = set ( [ top_lhs ] ) <EOL> while work_lhses : <EOL> new_work = set ( ) <EOL> for ( lhs , table , join_cols_ , rhs ) in alias_map_items : <EOL> if lhs is not None : <EOL> ( join_cols , ) = join_cols_ <EOL> if join_cols [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> lhs , rhs = rhs , lhs <EOL> join_cols = join_cols [ <NUM_LIT:1> ] , join_cols [ <NUM_LIT:0> ] <EOL> if lhs in work_lhses : <EOL> assert not rhs in soql_trans <EOL> if join_cols [ <NUM_LIT:0> ] . endswith ( '<STR_LIT>' ) : <EOL> fkey = re . sub ( '<STR_LIT>' , '<STR_LIT>' , join_cols [ <NUM_LIT:0> ] ) <EOL> else : <EOL> assert join_cols [ <NUM_LIT:0> ] . endswith ( '<STR_LIT>' ) <EOL> fkey = re . sub ( '<STR_LIT>' , '<STR_LIT>' , join_cols [ <NUM_LIT:0> ] ) <EOL> soql_trans [ rhs ] = '<STR_LIT>' % ( soql_trans [ lhs ] , fkey ) <EOL> new_work . add ( rhs ) <EOL> work_lhses = new_work <EOL> assert len ( soql_trans ) == len ( alias_map_items ) <EOL> self . soql_trans = soql_trans <EOL> return self . soql_trans <EOL> def late_fix ( self , sql ) : <EOL> """<STR_LIT>""" <EOL> assert not re . search ( r'<STR_LIT>' , sql ) <EOL> replacements = self . query_topology ( ) <EOL> assert all ( re . match ( r'<STR_LIT>' , x ) for x in replacements . keys ( ) ) <EOL> pattern_replaced = re . compile ( r'<STR_LIT>' % '<STR_LIT:|>' . join ( replacements . keys ( ) ) ) <EOL> def func ( sql ) : <EOL> start = <NUM_LIT:0> <EOL> out = [ ] <EOL> for match in pattern_replaced . finditer ( sql ) : <EOL> out . append ( sql [ start : match . start ( ) ] ) <EOL> out . append ( replacements [ match . group ( <NUM_LIT:1> ) ] ) <EOL> start = match . end ( ) <EOL> out . append ( sql [ start : len ( sql ) ] ) <EOL> return '<STR_LIT>' . join ( out ) <EOL> return subselect . transform_except_subselect ( sql , func ) <EOL> class SalesforceWhereNode ( where . WhereNode ) : <EOL> overridden_types = [ '<STR_LIT>' ] <EOL> def make_atom ( self , child , qn , connection ) : <EOL> lvalue , lookup_type , value_annot , params_or_value = child <EOL> result = super ( SalesforceWhereNode , self ) . make_atom ( child , qn , connection ) <EOL> if ( lookup_type in self . overridden_types ) : <EOL> if hasattr ( lvalue , '<STR_LIT>' ) : <EOL> try : <EOL> lvalue , params = lvalue . process ( lookup_type , params_or_value , connection ) <EOL> except where . EmptyShortCircuit : <EOL> raise EmptyResultSet <EOL> if isinstance ( lvalue , tuple ) : <EOL> field_sql = self . sql_for_columns ( lvalue , qn , connection ) <EOL> else : <EOL> field_sql = lvalue . as_sql ( qn , connection ) <EOL> if lookup_type == '<STR_LIT>' : <EOL> return ( '<STR_LIT>' % ( field_sql , <EOL> ( not value_annot and '<STR_LIT>' or '<STR_LIT>' ) ) , ( ) ) <EOL> else : <EOL> return result <EOL> def as_sql ( self , qn , connection ) : <EOL> """<STR_LIT>""" <EOL> soql_trans = qn . query_topology ( ) <EOL> result = [ ] <EOL> result_params = [ ] <EOL> everything_childs , nothing_childs = <NUM_LIT:0> , <NUM_LIT:0> <EOL> non_empty_childs = len ( self . children ) <EOL> for child in self . children : <EOL> try : <EOL> if hasattr ( child , '<STR_LIT>' ) : <EOL> sql , params = qn . compile ( child ) <EOL> else : <EOL> sql , params = self . make_atom ( child , qn , connection ) <EOL> except EmptyResultSet : <EOL> nothing_childs += <NUM_LIT:1> <EOL> else : <EOL> if sql : <EOL> x_match = re . match ( r'<STR_LIT>' , sql ) <EOL> if x_match : <EOL> x_table , x_field = x_match . groups ( ) <EOL> sql = '<STR_LIT>' % ( soql_trans [ x_table ] , x_field ) <EOL> result . append ( sql ) <EOL> result_params . extend ( params ) <EOL> else : <EOL> if sql is None : <EOL> non_empty_childs -= <NUM_LIT:1> <EOL> continue <EOL> everything_childs += <NUM_LIT:1> <EOL> if self . connector == AND : <EOL> full_needed , empty_needed = non_empty_childs , <NUM_LIT:1> <EOL> else : <EOL> full_needed , empty_needed = <NUM_LIT:1> , non_empty_childs <EOL> if empty_needed - nothing_childs <= <NUM_LIT:0> : <EOL> if self . negated : <EOL> return '<STR_LIT>' , [ ] <EOL> else : <EOL> raise EmptyResultSet <EOL> if full_needed - everything_childs <= <NUM_LIT:0> : <EOL> if self . negated : <EOL> raise EmptyResultSet <EOL> else : <EOL> return '<STR_LIT>' , [ ] <EOL> if non_empty_childs == <NUM_LIT:0> : <EOL> return None , [ ] <EOL> conn = '<STR_LIT>' % self . connector <EOL> sql_string = conn . join ( result ) <EOL> if sql_string : <EOL> if self . negated : <EOL> sql_string = '<STR_LIT>' % sql_string <EOL> elif len ( result ) > <NUM_LIT:1> : <EOL> sql_string = '<STR_LIT>' % sql_string <EOL> return sql_string , result_params <EOL> def add ( self , data , conn_type , ** kwargs ) : <EOL> cond = isinstance ( data , models . lookups . IsNull ) and not isinstance ( data , IsNull ) <EOL> if cond : <EOL> data = IsNull ( data . lhs , data . rhs ) <EOL> return super ( SalesforceWhereNode , self ) . add ( data , conn_type , ** kwargs ) <EOL> as_salesforce = as_sql <EOL> del as_sql <EOL> class SQLInsertCompiler ( compiler . SQLInsertCompiler , SQLCompiler ) : <EOL> def execute_sql ( self , return_id = False ) : <EOL> assert not ( return_id and len ( self . query . objs ) != <NUM_LIT:1> ) <EOL> self . return_id = return_id <EOL> cursor = self . connection . cursor ( query = self . query ) <EOL> for sql , params in self . as_sql ( ) : <EOL> cursor . execute ( sql , params ) <EOL> if not return_id : <EOL> return <EOL> return self . connection . ops . last_insert_id ( cursor , <EOL> self . query . model . _meta . db_table , self . query . model . _meta . pk . column ) <EOL> class SQLDeleteCompiler ( compiler . SQLDeleteCompiler , SQLCompiler ) : <EOL> pass <EOL> class SQLUpdateCompiler ( compiler . SQLUpdateCompiler , SQLCompiler ) : <EOL> pass <EOL> class SQLAggregateCompiler ( compiler . SQLAggregateCompiler , SQLCompiler ) : <EOL> pass <EOL> if DJANGO_17_EXACT : <EOL> class SQLDateCompiler ( compiler . SQLDateCompiler , SQLCompiler ) : <EOL> pass <EOL> class IsNull ( models . lookups . IsNull ) : <EOL> lookup_name = '<STR_LIT>' <EOL> def as_sql ( self , qn , connection ) : <EOL> if connection . vendor == '<STR_LIT>' : <EOL> sql , params = qn . compile ( self . lhs ) <EOL> return ( '<STR_LIT>' % ( sql , ( '<STR_LIT:=>' if self . rhs else '<STR_LIT>' ) ) ) , params <EOL> else : <EOL> return super ( IsNull , self ) . as_sql ( qn , connection ) <EOL> models . Field . register_lookup ( IsNull ) <EOL> class Range ( models . lookups . Range ) : <EOL> lookup_name = '<STR_LIT>' <EOL> def as_sql ( self , qn , connection ) : <EOL> if connection . vendor == '<STR_LIT>' : <EOL> lhs , lhs_params = self . process_lhs ( qn , connection ) <EOL> rhs , rhs_params = self . process_rhs ( qn , connection ) <EOL> if DJANGO_17_EXACT : <EOL> rhs = [ rhs , rhs ] <EOL> assert rhs == [ '<STR_LIT:%s>' , '<STR_LIT:%s>' ] <EOL> params = lhs_params + rhs_params [ : <NUM_LIT:1> ] + lhs_params + rhs_params [ <NUM_LIT:1> : <NUM_LIT:2> ] <EOL> return '<STR_LIT>' % ( lhs , rhs [ <NUM_LIT:0> ] , lhs , rhs [ <NUM_LIT:1> ] ) , params <EOL> else : <EOL> return super ( Range , self ) . as_sql ( qn , connection ) <EOL> models . Field . register_lookup ( Range ) <EOL> if DJANGO_18_PLUS : <EOL> from django . db . models . aggregates import Count <EOL> def count_as_salesforce ( self , * args , ** kwargs ) : <EOL> if ( len ( self . source_expressions ) == <NUM_LIT:1> and <EOL> isinstance ( self . source_expressions [ <NUM_LIT:0> ] , models . expressions . Value ) and <EOL> self . source_expressions [ <NUM_LIT:0> ] . value == '<STR_LIT:*>' ) : <EOL> return '<STR_LIT>' , [ ] <EOL> else : <EOL> return self . as_sql ( * args , ** kwargs ) <EOL> setattr ( Count , '<STR_LIT>' , count_as_salesforce ) </s>
