<s> """<STR_LIT>""" <EOL> from datetime import datetime <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> _classes = { } <EOL> def query_class ( QueryClass ) : <EOL> """<STR_LIT>""" <EOL> global _classes <EOL> try : <EOL> return _classes [ QueryClass ] <EOL> except KeyError : <EOL> pass <EOL> class PyOdbcSSQuery ( QueryClass ) : <EOL> from sql_server . pyodbc import aggregates <EOL> aggregates_module = aggregates <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( PyOdbcSSQuery , self ) . __init__ ( * args , ** kwargs ) <EOL> self . default_reverse_ordering = False <EOL> self . _ord = [ ] <EOL> from django . db . models . sql . subqueries import InsertQuery <EOL> if isinstance ( self , InsertQuery ) : <EOL> self . _orig_as_sql = self . as_sql <EOL> self . as_sql = self . _insert_as_sql <EOL> def _insert_as_sql ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> meta = self . get_meta ( ) <EOL> quoted_table = self . connection . ops . quote_name ( meta . db_table ) <EOL> sql , params = self . _orig_as_sql ( * args , ** kwargs ) <EOL> if meta . has_auto_field and meta . auto_field . column in self . columns : <EOL> if len ( self . columns ) == <NUM_LIT:1> and not params : <EOL> sql = "<STR_LIT>" % quoted_table <EOL> else : <EOL> sql = "<STR_LIT>" % ( quoted_table , sql , quoted_table ) <EOL> return sql , params <EOL> def __reduce__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( QueryClass , '<STR_LIT>' ) : <EOL> assert hasattr ( QueryClass , '<STR_LIT>' ) <EOL> data = self . __getstate__ ( ) <EOL> else : <EOL> data = self . __dict__ <EOL> return ( unpickle_query_class , ( QueryClass , ) , data ) <EOL> def convert_values ( self , value , field ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> return None <EOL> if field and field . get_internal_type ( ) == '<STR_LIT>' : <EOL> return value <EOL> elif field and field . get_internal_type ( ) == '<STR_LIT>' : <EOL> value = value . date ( ) <EOL> elif field and field . get_internal_type ( ) == '<STR_LIT>' or ( isinstance ( value , datetime ) and value . year == <NUM_LIT> and value . month == value . day == <NUM_LIT:1> ) : <EOL> value = value . time ( ) <EOL> elif isinstance ( value , datetime ) and value . hour == value . minute == value . second == value . microsecond == <NUM_LIT:0> : <EOL> value = value . date ( ) <EOL> elif value is not None and field and field . get_internal_type ( ) == '<STR_LIT>' : <EOL> value = float ( value ) <EOL> return value <EOL> def resolve_columns ( self , row , fields = ( ) ) : <EOL> index_start = len ( self . extra_select . keys ( ) ) <EOL> values = [ self . convert_values ( v , None ) for v in row [ : index_start ] ] <EOL> for value , field in map ( None , row [ index_start : ] , fields ) : <EOL> values . append ( self . convert_values ( value , field ) ) <EOL> return tuple ( values ) <EOL> def modify_query ( self , strategy , ordering , out_cols ) : <EOL> """<STR_LIT>""" <EOL> self . default_reverse_ordering = False <EOL> self . _ord = [ ] <EOL> cnt = <NUM_LIT:0> <EOL> extra_select_aliases = [ k . strip ( '<STR_LIT>' ) for k in self . extra_select . keys ( ) ] <EOL> for ord_spec_item in ordering : <EOL> if ord_spec_item . endswith ( '<STR_LIT>' ) or ord_spec_item . endswith ( '<STR_LIT>' ) : <EOL> parts = ord_spec_item . split ( ) <EOL> col , odir = '<STR_LIT:U+0020>' . join ( parts [ : - <NUM_LIT:1> ] ) , parts [ - <NUM_LIT:1> ] <EOL> if col not in self . ordering_aliases and col . strip ( '<STR_LIT>' ) not in extra_select_aliases : <EOL> if col . isdigit ( ) : <EOL> cnt += <NUM_LIT:1> <EOL> n = int ( col ) - <NUM_LIT:1> <EOL> alias = '<STR_LIT>' % cnt <EOL> out_cols [ n ] = '<STR_LIT>' % ( out_cols [ n ] , alias ) <EOL> self . _ord . append ( ( alias , odir ) ) <EOL> elif col in out_cols : <EOL> if strategy == USE_TOP_HMARK : <EOL> cnt += <NUM_LIT:1> <EOL> n = out_cols . index ( col ) <EOL> alias = '<STR_LIT>' % cnt <EOL> out_cols [ n ] = '<STR_LIT>' % ( col , alias ) <EOL> self . _ord . append ( ( alias , odir ) ) <EOL> else : <EOL> self . _ord . append ( ( col , odir ) ) <EOL> elif strategy == USE_TOP_HMARK : <EOL> if col . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] == '<STR_LIT>' and odir == '<STR_LIT>' : <EOL> self . default_reverse_ordering = True <EOL> cnt += <NUM_LIT:1> <EOL> alias = '<STR_LIT>' % cnt <EOL> self . _ord . append ( ( alias , odir ) ) <EOL> self . ordering_aliases . append ( '<STR_LIT>' % ( col , alias ) ) <EOL> else : <EOL> self . _ord . append ( ( col , odir ) ) <EOL> else : <EOL> self . _ord . append ( ( col , odir ) ) <EOL> if strategy == USE_ROW_NUMBER and not self . _ord and '<STR_LIT>' in ordering : <EOL> self . _ord . append ( ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if strategy == USE_TOP_HMARK and not self . _ord : <EOL> meta = self . model . _meta <EOL> qn = self . quote_name_unless_alias <EOL> pk_col = '<STR_LIT>' % ( qn ( meta . db_table ) , qn ( meta . pk . db_column or meta . pk . column ) ) <EOL> if pk_col not in out_cols : <EOL> out_cols . append ( pk_col ) <EOL> def _as_sql ( self , strategy ) : <EOL> """<STR_LIT>""" <EOL> out_cols = self . get_columns ( True ) <EOL> ordering , ordering_group_by = self . get_ordering ( ) <EOL> if strategy == USE_ROW_NUMBER : <EOL> if not ordering : <EOL> meta = self . get_meta ( ) <EOL> qn = self . quote_name_unless_alias <EOL> if '<STR_LIT>' % ( qn ( meta . db_table ) , qn ( meta . pk . db_column or meta . pk . column ) ) not in self . get_columns ( ) : <EOL> ordering = [ '<STR_LIT>' ] <EOL> else : <EOL> ordering = [ '<STR_LIT>' % ( qn ( meta . db_table ) , qn ( meta . pk . db_column or meta . pk . column ) ) ] <EOL> if strategy in ( USE_TOP_HMARK , USE_ROW_NUMBER ) : <EOL> self . modify_query ( strategy , ordering , out_cols ) <EOL> if strategy == USE_ROW_NUMBER : <EOL> ord = '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' % pair for pair in self . _ord ] ) <EOL> self . ordering_aliases . append ( '<STR_LIT>' % ord ) <EOL> from_ , f_params = self . get_from_clause ( ) <EOL> qn = self . quote_name_unless_alias <EOL> where , w_params = self . where . as_sql ( qn = qn ) <EOL> having , h_params = self . having . as_sql ( qn = qn ) <EOL> params = [ ] <EOL> for val in self . extra_select . itervalues ( ) : <EOL> params . extend ( val [ <NUM_LIT:1> ] ) <EOL> result = [ '<STR_LIT>' ] <EOL> if self . distinct : <EOL> result . append ( '<STR_LIT>' ) <EOL> if strategy == USE_TOP_LMARK : <EOL> meta = self . model . _meta <EOL> result . append ( '<STR_LIT>' % ( self . low_mark , self . quote_name_unless_alias ( meta . pk . db_column or meta . pk . column ) ) ) <EOL> else : <EOL> if strategy == USE_TOP_HMARK and self . high_mark is not None : <EOL> result . append ( '<STR_LIT>' % self . high_mark ) <EOL> result . append ( '<STR_LIT:U+002CU+0020>' . join ( out_cols + self . ordering_aliases ) ) <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> if self . extra_where : <EOL> if not where : <EOL> result . append ( '<STR_LIT>' ) <EOL> else : <EOL> result . append ( '<STR_LIT>' ) <EOL> result . append ( '<STR_LIT>' . join ( self . extra_where ) ) <EOL> grouping , gb_params = self . get_grouping ( ) <EOL> if grouping : <EOL> if ordering : <EOL> if not self . connection . features . allows_group_by_pk : <EOL> for col , col_params in ordering_group_by : <EOL> if col not in grouping : <EOL> grouping . append ( str ( col ) ) <EOL> gb_params . extend ( col_params ) <EOL> else : <EOL> ordering = self . connection . ops . force_no_ordering ( ) <EOL> result . append ( '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( grouping ) ) <EOL> params . extend ( gb_params ) <EOL> if having : <EOL> result . append ( '<STR_LIT>' % having ) <EOL> params . extend ( h_params ) <EOL> params . extend ( self . extra_params ) <EOL> return '<STR_LIT:U+0020>' . join ( result ) , tuple ( params ) <EOL> def as_sql ( self , with_limits = True , with_col_aliases = False ) : <EOL> """<STR_LIT>""" <EOL> do_offset = with_limits and ( self . high_mark is not None or self . low_mark != <NUM_LIT:0> ) <EOL> if not do_offset : <EOL> return super ( PyOdbcSSQuery , self ) . as_sql ( with_limits = False , <EOL> with_col_aliases = with_col_aliases ) <EOL> if self . high_mark == <NUM_LIT:0> : <EOL> return "<STR_LIT>" , ( ) <EOL> self . pre_sql_setup ( ) <EOL> meta = self . model . _meta <EOL> qn = self . quote_name_unless_alias <EOL> fallback_ordering = '<STR_LIT>' % ( qn ( meta . db_table ) , qn ( meta . pk . db_column or meta . pk . column ) ) <EOL> if self . connection . ops . sql_server_ver < <NUM_LIT> and self . high_mark is not None : <EOL> orig_sql , params = self . _as_sql ( USE_TOP_HMARK ) <EOL> if self . _ord : <EOL> ord = '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' % pair for pair in self . _ord ] ) <EOL> rev_ord = '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' % ( col , REV_ODIR [ odir ] ) for col , odir in self . _ord ] ) <EOL> else : <EOL> if not self . default_reverse_ordering : <EOL> ord = '<STR_LIT>' % fallback_ordering <EOL> rev_ord = '<STR_LIT>' % fallback_ordering <EOL> else : <EOL> ord = '<STR_LIT>' % fallback_ordering <EOL> rev_ord = '<STR_LIT>' % fallback_ordering <EOL> sql = SQL_SERVER_8_LIMIT_QUERY % { <EOL> '<STR_LIT>' : self . high_mark - self . low_mark , <EOL> '<STR_LIT>' : orig_sql , <EOL> '<STR_LIT>' : ord , <EOL> '<STR_LIT>' : rev_ord , <EOL> '<STR_LIT>' : qn ( meta . db_table ) , <EOL> } <EOL> return sql , params <EOL> if self . connection . ops . sql_server_ver >= <NUM_LIT> : <EOL> sql , params = self . _as_sql ( USE_ROW_NUMBER ) <EOL> result = [ '<STR_LIT>' % sql ] <EOL> if self . high_mark is None : <EOL> self . high_mark = <NUM_LIT> <EOL> result . append ( '<STR_LIT>' % ( self . low_mark + <NUM_LIT:1> , self . high_mark ) ) <EOL> return '<STR_LIT:U+0020>' . join ( result ) , params <EOL> self . get_columns ( with_col_aliases ) <EOL> ordering , ordering_group_by = self . get_ordering ( ) <EOL> if ordering : <EOL> ord = '<STR_LIT:U+002CU+0020>' . join ( ordering ) <EOL> else : <EOL> ord = fallback_ordering <EOL> orig_sql , params = self . _as_sql ( USE_TOP_LMARK ) <EOL> sql = SQL_SERVER_8_NO_LIMIT_QUERY % { <EOL> '<STR_LIT>' : orig_sql , <EOL> '<STR_LIT>' : ord , <EOL> '<STR_LIT>' : qn ( meta . db_table ) , <EOL> '<STR_LIT:key>' : qn ( meta . pk . db_column or meta . pk . column ) , <EOL> } <EOL> return sql , params <EOL> _classes [ QueryClass ] = PyOdbcSSQuery <EOL> return PyOdbcSSQuery <EOL> def unpickle_query_class ( QueryClass ) : <EOL> """<STR_LIT>""" <EOL> klass = query_class ( QueryClass ) <EOL> return klass . __new__ ( klass ) <EOL> unpickle_query_class . __safe_for_unpickling__ = True </s>
