<s> import re <EOL> from django . db . models . sql import compiler <EOL> import django <EOL> from datetime import datetime <EOL> from django import VERSION as DjangoVersion <EOL> from django_pyodbc . compat import zip_longest <EOL> REV_ODIR = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> SQL_SERVER_8_LIMIT_QUERY = """<STR_LIT>""" <EOL> SQL_SERVER_8_NO_LIMIT_QUERY = """<STR_LIT>""" <EOL> USE_ROW_NUMBER = <NUM_LIT:0> <EOL> USE_TOP_HMARK = <NUM_LIT:1> <EOL> USE_TOP_LMARK = <NUM_LIT:2> <EOL> _re_data_type_terminator = re . compile ( <EOL> r'<STR_LIT>' + <EOL> r'<STR_LIT>' + <EOL> r'<STR_LIT>' + <EOL> '<STR_LIT:)>' , <EOL> re . IGNORECASE , <EOL> ) <EOL> _re_order_limit_offset = re . compile ( <EOL> r'<STR_LIT>' ) <EOL> _re_col_placeholder = re . compile ( r'<STR_LIT>' ) <EOL> _re_find_order_direction = re . compile ( r'<STR_LIT>' , re . IGNORECASE ) <EOL> def _remove_order_limit_offset ( sql ) : <EOL> return _re_order_limit_offset . sub ( '<STR_LIT>' , sql ) . split ( None , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> def _break ( s , find ) : <EOL> """<STR_LIT>""" <EOL> i = s . find ( find ) <EOL> return s [ : i ] , s [ i : ] <EOL> def _get_order_limit_offset ( sql ) : <EOL> return _re_order_limit_offset . search ( sql ) . groups ( ) <EOL> class SQLCompiler ( compiler . SQLCompiler ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( SQLCompiler , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _re_pat_col = re . compile ( <EOL> r"<STR_LIT>" . format ( <EOL> left_sql_quote = self . connection . ops . left_sql_quote , <EOL> right_sql_quote = self . connection . ops . right_sql_quote ) ) <EOL> def resolve_columns ( self , row , fields = ( ) ) : <EOL> if getattr ( self , '<STR_LIT>' , False ) : <EOL> row = row [ <NUM_LIT:1> : ] <EOL> values = [ ] <EOL> index_extra_select = len ( self . query . extra_select ) <EOL> for value , field in zip_longest ( row [ index_extra_select : ] , fields ) : <EOL> if field : <EOL> try : <EOL> value = self . connection . ops . convert_values ( value , field ) <EOL> except ValueError : <EOL> pass <EOL> values . append ( value ) <EOL> return row [ : index_extra_select ] + tuple ( values ) <EOL> def _fix_aggregates ( self ) : <EOL> """<STR_LIT>""" <EOL> for alias , aggregate in self . query . aggregate_select . items ( ) : <EOL> if aggregate . sql_function == '<STR_LIT>' : <EOL> self . query . aggregate_select [ alias ] . sql_template = '<STR_LIT>' <EOL> elif aggregate . sql_function == '<STR_LIT>' : <EOL> self . query . aggregate_select [ alias ] . sql_function = '<STR_LIT>' <EOL> elif aggregate . sql_function == '<STR_LIT>' : <EOL> self . query . aggregate_select [ alias ] . sql_function = '<STR_LIT>' <EOL> elif aggregate . sql_function == '<STR_LIT>' : <EOL> self . query . aggregate_select [ alias ] . sql_function = '<STR_LIT>' <EOL> elif aggregate . sql_function == '<STR_LIT>' : <EOL> self . query . aggregate_select [ alias ] . sql_function = '<STR_LIT>' <EOL> def as_sql ( self , with_limits = True , with_col_aliases = False ) : <EOL> if with_limits and self . query . low_mark == self . query . high_mark : <EOL> return '<STR_LIT>' , ( ) <EOL> self . _fix_aggregates ( ) <EOL> self . _using_row_number = False <EOL> check_limits = with_limits and ( self . query . low_mark or self . query . high_mark is not None ) <EOL> if not check_limits : <EOL> try : <EOL> setattr ( self . query , '<STR_LIT>' , with_col_aliases ) <EOL> result = super ( SQLCompiler , self ) . as_sql ( with_limits , with_col_aliases ) <EOL> finally : <EOL> delattr ( self . query , '<STR_LIT>' ) <EOL> return result <EOL> raw_sql , fields = super ( SQLCompiler , self ) . as_sql ( False , with_col_aliases ) <EOL> if self . query . high_mark is not None and not self . query . low_mark : <EOL> if self . connection . ops . is_db2 : <EOL> sql = self . _select_top ( '<STR_LIT>' , raw_sql , self . query . high_mark ) <EOL> else : <EOL> _select = '<STR_LIT>' <EOL> if self . query . distinct : <EOL> _select += '<STR_LIT>' <EOL> sql = re . sub ( r'<STR_LIT>' . format ( _select ) , '<STR_LIT>' . format ( _select , self . query . high_mark ) , raw_sql , <NUM_LIT:1> ) <EOL> return sql , fields <EOL> self . _using_row_number = True <EOL> inner_select = _remove_order_limit_offset ( raw_sql ) <EOL> outer_fields , inner_select = self . _alias_columns ( inner_select ) <EOL> order = _get_order_limit_offset ( raw_sql ) [ <NUM_LIT:0> ] <EOL> qn = self . connection . ops . quote_name <EOL> inner_table_name = qn ( '<STR_LIT>' ) <EOL> outer_fields , inner_select , order = self . _fix_slicing_order ( outer_fields , inner_select , order , inner_table_name ) <EOL> f = [ ] <EOL> for x in outer_fields . split ( '<STR_LIT:U+002C>' ) : <EOL> i = x . upper ( ) . find ( '<STR_LIT>' ) <EOL> if i != - <NUM_LIT:1> : <EOL> x = x [ i + <NUM_LIT:4> : ] <EOL> if x . find ( '<STR_LIT:.>' ) != - <NUM_LIT:1> : <EOL> tbl , col = x . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> else : <EOL> col = x <EOL> f . append ( '<STR_LIT>' . format ( inner_table_name , col . strip ( ) ) ) <EOL> inner_select = '<STR_LIT>' . format ( <EOL> fields = '<STR_LIT:U+002CU+0020>' . join ( f ) , <EOL> inner = inner_select , <EOL> inner_as = inner_table_name , <EOL> ) <EOL> row_num_col = '<STR_LIT>' if self . connection . ops . is_db2 else '<STR_LIT>' <EOL> where_row_num = '<STR_LIT>' . format ( self . query . low_mark , row_num_col = row_num_col ) <EOL> if self . query . high_mark : <EOL> where_row_num += '<STR_LIT>' . format ( self . query . high_mark , row_num_col = row_num_col ) <EOL> if self . connection . ops . sql_server_ver < <NUM_LIT> and not self . connection . ops . is_db2 : <EOL> num_to_select = self . query . high_mark - self . query . low_mark <EOL> order_by_col_with_prefix , order_direction = order . rsplit ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> order_by_col = order_by_col_with_prefix . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] <EOL> opposite_order_direction = REV_ODIR [ order_direction ] <EOL> sql = r'''<STR_LIT>''' . format ( <EOL> inner = inner_select , <EOL> num_to_select = num_to_select , <EOL> high_mark = self . query . high_mark , <EOL> order_by_col = order_by_col , <EOL> order_direction = order_direction , <EOL> opposite_order_direction = opposite_order_direction , <EOL> left_sql_quote = self . connection . ops . left_sql_quote , <EOL> right_sql_quote = self . connection . ops . right_sql_quote , <EOL> ) <EOL> else : <EOL> sql = "<STR_LIT>" . format ( <EOL> outer = outer_fields , <EOL> order = order , <EOL> inner = inner_select , <EOL> where = where_row_num , <EOL> row_num_col = row_num_col <EOL> ) <EOL> return sql , fields <EOL> def _select_top ( self , select , inner_sql , number_to_fetch ) : <EOL> if self . connection . ops . is_db2 : <EOL> return "<STR_LIT>" . format ( <EOL> select = select , inner_sql = inner_sql , number_to_fetch = number_to_fetch ) <EOL> else : <EOL> return "<STR_LIT>" . format ( <EOL> select = select , inner_sql = inner_sql , number_to_fetch = number_to_fetch ) <EOL> def _fix_slicing_order ( self , outer_fields , inner_select , order , inner_table_name ) : <EOL> """<STR_LIT>""" <EOL> if order is None : <EOL> meta = self . query . get_meta ( ) <EOL> column = meta . pk . db_column or meta . pk . get_attname ( ) <EOL> order = '<STR_LIT>' . format ( <EOL> inner_table_name , <EOL> self . connection . ops . quote_name ( column ) , <EOL> ) <EOL> else : <EOL> alias_id = <NUM_LIT:0> <EOL> new_order = [ ] <EOL> for x in order . split ( '<STR_LIT:U+002C>' ) : <EOL> m = _re_find_order_direction . search ( x ) <EOL> if m : <EOL> direction = m . groups ( ) [ <NUM_LIT:0> ] <EOL> else : <EOL> direction = '<STR_LIT>' <EOL> x = _re_find_order_direction . sub ( '<STR_LIT>' , x ) <EOL> col = x . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] <EOL> if x not in inner_select : <EOL> alias_id += <NUM_LIT:1> <EOL> col = '<STR_LIT>' . format ( <EOL> col . strip ( self . connection . ops . left_sql_quote + self . connection . ops . right_sql_quote ) , <EOL> alias_id , <EOL> left_sql_quote = self . connection . ops . left_sql_quote , <EOL> right_sql_quote = self . connection . ops . right_sql_quote , <EOL> ) <EOL> inner_select = '<STR_LIT>' . format ( x , col , inner_select ) <EOL> new_order . append ( '<STR_LIT>' . format ( inner_table_name , col , direction ) ) <EOL> order = '<STR_LIT:U+002CU+0020>' . join ( new_order ) <EOL> return outer_fields , inner_select , order <EOL> def _alias_columns ( self , sql ) : <EOL> """<STR_LIT>""" <EOL> qn = self . connection . ops . quote_name <EOL> outer = list ( ) <EOL> inner = list ( ) <EOL> names_seen = list ( ) <EOL> paren_depth , paren_buf = <NUM_LIT:0> , [ '<STR_LIT>' ] <EOL> parens , i = { } , <NUM_LIT:0> <EOL> for ch in sql : <EOL> if ch == '<STR_LIT:(>' : <EOL> i += <NUM_LIT:1> <EOL> paren_depth += <NUM_LIT:1> <EOL> paren_buf . append ( '<STR_LIT>' ) <EOL> elif ch == '<STR_LIT:)>' : <EOL> paren_depth -= <NUM_LIT:1> <EOL> key = '<STR_LIT>' . format ( i ) <EOL> buf = paren_buf . pop ( ) <EOL> buf = re . sub ( r'<STR_LIT>' , r'<STR_LIT>' , buf ) <EOL> parens [ key ] = buf % parens <EOL> parens [ key ] = re . sub ( r'<STR_LIT>' , r'<STR_LIT>' , parens [ key ] ) <EOL> paren_buf [ paren_depth ] += '<STR_LIT>' + key + '<STR_LIT>' <EOL> else : <EOL> paren_buf [ paren_depth ] += ch <EOL> def _replace_sub ( col ) : <EOL> """<STR_LIT>""" <EOL> while _re_col_placeholder . search ( col ) : <EOL> col = col . format ( ** parens ) <EOL> return col <EOL> temp_sql = '<STR_LIT>' . join ( paren_buf ) <EOL> placeholder_data = { "<STR_LIT:i>" : i } <EOL> def _alias_placeholders ( val ) : <EOL> i = placeholder_data [ "<STR_LIT:i>" ] <EOL> i += <NUM_LIT:1> <EOL> placeholder_data [ "<STR_LIT:i>" ] = i <EOL> key = "<STR_LIT>" . format ( i ) <EOL> parens [ key ] = "<STR_LIT:%s>" <EOL> return "<STR_LIT>" + key + "<STR_LIT>" <EOL> temp_sql = re . sub ( "<STR_LIT:%s>" , _alias_placeholders , temp_sql ) <EOL> select_list , from_clause = _break ( temp_sql , '<STR_LIT>' + self . connection . ops . left_sql_quote ) <EOL> for col in [ x . strip ( ) for x in select_list . split ( '<STR_LIT:U+002C>' ) ] : <EOL> match = self . _re_pat_col . search ( col ) <EOL> if match : <EOL> col_name = match . group ( <NUM_LIT:1> ) <EOL> col_key = col_name . lower ( ) <EOL> if col_key in names_seen : <EOL> alias = qn ( '<STR_LIT>' . format ( col_name , names_seen . count ( col_key ) ) ) <EOL> outer . append ( alias ) <EOL> inner . append ( '<STR_LIT>' . format ( _replace_sub ( col ) , alias ) ) <EOL> else : <EOL> outer . append ( qn ( col_name ) ) <EOL> inner . append ( _replace_sub ( col ) ) <EOL> names_seen . append ( col_key ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' . format ( col ) ) <EOL> return '<STR_LIT:U+002CU+0020>' . join ( outer ) , '<STR_LIT:U+002CU+0020>' . join ( inner ) + ( from_clause % parens ) <EOL> def get_ordering ( self ) : <EOL> if getattr ( self . query , '<STR_LIT>' , False ) : <EOL> if django . VERSION [ <NUM_LIT:0> ] == <NUM_LIT:1> and django . VERSION [ <NUM_LIT:1> ] < <NUM_LIT:6> : <EOL> return ( None , [ ] ) <EOL> return ( None , [ ] , [ ] ) <EOL> return super ( SQLCompiler , self ) . get_ordering ( ) <EOL> class SQLInsertCompiler ( compiler . SQLInsertCompiler , SQLCompiler ) : <EOL> _re_values_sub = re . compile ( r'<STR_LIT>' , re . IGNORECASE ) <EOL> _values_repl = r'<STR_LIT>' <EOL> def as_sql ( self , * args , ** kwargs ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . return_id = False <EOL> result = super ( SQLInsertCompiler , self ) . as_sql ( * args , ** kwargs ) <EOL> if isinstance ( result , list ) : <EOL> return [ self . _fix_insert ( x [ <NUM_LIT:0> ] , x [ <NUM_LIT:1> ] ) for x in result ] <EOL> sql , params = result <EOL> return self . _fix_insert ( sql , params ) <EOL> def _fix_insert ( self , sql , params ) : <EOL> """<STR_LIT>""" <EOL> meta = self . query . get_meta ( ) <EOL> if meta . has_auto_field : <EOL> if hasattr ( self . query , '<STR_LIT>' ) : <EOL> fields = self . query . fields <EOL> auto_field = meta . auto_field <EOL> else : <EOL> fields = self . query . columns <EOL> auto_field = meta . auto_field . db_column or meta . auto_field . column <EOL> auto_in_fields = auto_field in fields <EOL> quoted_table = self . connection . ops . quote_name ( meta . db_table ) <EOL> if not fields or ( auto_in_fields and len ( fields ) == <NUM_LIT:1> and not params ) : <EOL> sql = '<STR_LIT>' . format ( <EOL> quoted_table <EOL> ) <EOL> params = [ ] <EOL> elif auto_in_fields : <EOL> sql = '<STR_LIT>' . format ( <EOL> table = quoted_table , <EOL> sql = sql , <EOL> ) <EOL> if self . return_id and self . connection . features . can_return_id_from_insert : <EOL> col = self . connection . ops . quote_name ( meta . pk . db_column or meta . pk . get_attname ( ) ) <EOL> pk_db_type = _re_data_type_terminator . split ( meta . pk . db_type ( self . connection ) ) [ <NUM_LIT:0> ] <EOL> sql = '<STR_LIT>' . format ( <EOL> sql = sql , <EOL> declare_table_var = "<STR_LIT>" . format ( <EOL> col_name = col , <EOL> pk_type = pk_db_type , <EOL> ) , <EOL> select_return_id = "<STR_LIT>" , <EOL> ) <EOL> output = self . _values_repl . format ( col = col ) <EOL> sql = self . _re_values_sub . sub ( output , sql ) <EOL> return sql , params <EOL> class SQLInsertCompiler2 ( compiler . SQLInsertCompiler , SQLCompiler ) : <EOL> def as_sql_legacy ( self ) : <EOL> qn = self . connection . ops . quote_name <EOL> opts = self . query . model . _meta <EOL> returns_id = bool ( self . return_id and <EOL> self . connection . features . can_return_id_from_insert ) <EOL> result = [ '<STR_LIT>' % qn ( opts . db_table ) ] <EOL> result . append ( '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( [ qn ( c ) for c in self . query . columns ] ) ) <EOL> if returns_id : <EOL> result . append ( '<STR_LIT>' % qn ( opts . pk . column ) ) <EOL> values = [ self . placeholder ( * v ) for v in self . query . values ] <EOL> result . append ( '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( values ) ) <EOL> params = self . query . params <EOL> sql = '<STR_LIT:U+0020>' . join ( result ) <EOL> meta = self . query . get_meta ( ) <EOL> if meta . has_auto_field : <EOL> auto_field_column = meta . auto_field . db_column or meta . auto_field . column <EOL> if auto_field_column in self . query . columns : <EOL> quoted_table = self . connection . ops . quote_name ( meta . db_table ) <EOL> if len ( self . query . columns ) == <NUM_LIT:1> and not params : <EOL> result = [ '<STR_LIT>' % quoted_table ] <EOL> if returns_id : <EOL> result . append ( '<STR_LIT>' % qn ( opts . pk . column ) ) <EOL> result . append ( '<STR_LIT>' ) <EOL> sql = '<STR_LIT:U+0020>' . join ( result ) <EOL> else : <EOL> sql = "<STR_LIT>" % ( quoted_table , sql , quoted_table ) <EOL> return sql , params <EOL> def as_sql ( self ) : <EOL> if self . connection . _DJANGO_VERSION < <NUM_LIT> : <EOL> return self . as_sql_legacy ( ) <EOL> qn = self . connection . ops . quote_name <EOL> opts = self . query . model . _meta <EOL> result = [ '<STR_LIT>' % qn ( opts . db_table ) ] <EOL> has_fields = bool ( self . query . fields ) <EOL> fields = self . query . fields if has_fields else [ opts . pk ] <EOL> columns = [ f . column for f in fields ] <EOL> result . append ( '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( [ qn ( c ) for c in columns ] ) ) <EOL> if has_fields : <EOL> params = values = [ <EOL> [ <EOL> f . get_db_prep_save ( getattr ( obj , f . attname ) if self . query . raw else f . pre_save ( obj , True ) , connection = self . connection ) <EOL> for f in fields <EOL> ] <EOL> for obj in self . query . objs <EOL> ] <EOL> else : <EOL> values = [ [ self . connection . ops . pk_default_value ( ) ] for obj in self . query . objs ] <EOL> params = [ [ ] ] <EOL> fields = [ None ] <EOL> placeholders = [ <EOL> [ self . placeholder ( field , v ) for field , v in zip ( fields , val ) ] <EOL> for val in values <EOL> ] <EOL> if self . return_id and self . connection . features . can_return_id_from_insert : <EOL> params = params [ <NUM_LIT:0> ] <EOL> output = '<STR_LIT>' % qn ( opts . pk . column ) <EOL> result . append ( output ) <EOL> result . append ( "<STR_LIT>" % "<STR_LIT:U+002CU+0020>" . join ( placeholders [ <NUM_LIT:0> ] ) ) <EOL> return [ ( "<STR_LIT:U+0020>" . join ( result ) , tuple ( params ) ) ] <EOL> items = [ <EOL> ( "<STR_LIT:U+0020>" . join ( result + [ "<STR_LIT>" % "<STR_LIT:U+002CU+0020>" . join ( p ) ] ) , vals ) <EOL> for p , vals in zip ( placeholders , params ) <EOL> ] <EOL> meta = self . query . get_meta ( ) <EOL> if meta . has_auto_field : <EOL> auto_field_column = meta . auto_field . db_column or meta . auto_field . column <EOL> out = [ ] <EOL> for sql , params in items : <EOL> if auto_field_column in columns : <EOL> quoted_table = self . connection . ops . quote_name ( meta . db_table ) <EOL> if not has_fields : <EOL> sql = "<STR_LIT>" % quoted_table <EOL> else : <EOL> sql = "<STR_LIT>" % ( quoted_table , sql , quoted_table ) <EOL> out . append ( [ sql , params ] ) <EOL> items = out <EOL> return items <EOL> class SQLDeleteCompiler ( compiler . SQLDeleteCompiler , SQLCompiler ) : <EOL> pass <EOL> class SQLUpdateCompiler ( compiler . SQLUpdateCompiler , SQLCompiler ) : <EOL> pass <EOL> class SQLAggregateCompiler ( compiler . SQLAggregateCompiler , SQLCompiler ) : <EOL> def as_sql ( self , qn = None ) : <EOL> self . _fix_aggregates ( ) <EOL> return super ( SQLAggregateCompiler , self ) . as_sql ( qn = qn ) <EOL> if DjangoVersion [ <NUM_LIT:0> ] >= <NUM_LIT:1> and DjangoVersion [ <NUM_LIT:1> ] >= <NUM_LIT:8> : <EOL> import warnings <EOL> class DeprecatedMeta ( type ) : <EOL> def __new__ ( cls , name , bases , attrs ) : <EOL> if attrs . get ( '<STR_LIT>' ) is not cls : <EOL> msg = ( '<STR_LIT>' + <EOL> '<STR_LIT>' + name + <EOL> '<STR_LIT>' + name + '<STR_LIT>' ) <EOL> raise ImportError ( msg ) <EOL> return super ( DeprecatedMeta , cls ) . __new__ ( cls , name , bases , attrs ) <EOL> class SQLDateCompiler ( object ) : <EOL> __metaclass__ = DeprecatedMeta <EOL> class SQLDateTimeCompiler ( object ) : <EOL> __metaclass__ = DeprecatedMeta <EOL> else : <EOL> class SQLDateCompiler ( compiler . SQLDateCompiler , SQLCompiler ) : <EOL> pass <EOL> class SQLDateTimeCompiler ( compiler . SQLDateCompiler , SQLCompiler ) : <EOL> pass </s>
