<s> import datetime <EOL> from django . conf import settings <EOL> from django . db . backends . util import truncate_name , typecast_date , typecast_timestamp <EOL> from django . db . models . sql import compiler <EOL> from django . db . models . sql . constants import MULTI <EOL> from django . utils import six <EOL> from django . utils . six . moves import zip , zip_longest <EOL> from django . utils import timezone <EOL> SQLCompiler = compiler . SQLCompiler <EOL> class GeoSQLCompiler ( compiler . SQLCompiler ) : <EOL> def get_columns ( self , with_aliases = False ) : <EOL> """<STR_LIT>""" <EOL> qn = self . quote_name_unless_alias <EOL> qn2 = self . connection . ops . quote_name <EOL> result = [ '<STR_LIT>' % ( self . get_extra_select_format ( alias ) % col [ <NUM_LIT:0> ] , qn2 ( alias ) ) <EOL> for alias , col in six . iteritems ( self . query . extra_select ) ] <EOL> params = [ ] <EOL> aliases = set ( self . query . extra_select . keys ( ) ) <EOL> if with_aliases : <EOL> col_aliases = aliases . copy ( ) <EOL> else : <EOL> col_aliases = set ( ) <EOL> if self . query . select : <EOL> only_load = self . deferred_to_columns ( ) <EOL> for col , field in self . query . select : <EOL> if isinstance ( col , ( list , tuple ) ) : <EOL> alias , column = col <EOL> table = self . query . alias_map [ alias ] . table_name <EOL> if table in only_load and column not in only_load [ table ] : <EOL> continue <EOL> r = self . get_field_select ( field , alias , column ) <EOL> if with_aliases : <EOL> if col [ <NUM_LIT:1> ] in col_aliases : <EOL> c_alias = '<STR_LIT>' % len ( col_aliases ) <EOL> result . append ( '<STR_LIT>' % ( r , c_alias ) ) <EOL> aliases . add ( c_alias ) <EOL> col_aliases . add ( c_alias ) <EOL> else : <EOL> result . append ( '<STR_LIT>' % ( r , qn2 ( col [ <NUM_LIT:1> ] ) ) ) <EOL> aliases . add ( r ) <EOL> col_aliases . add ( col [ <NUM_LIT:1> ] ) <EOL> else : <EOL> result . append ( r ) <EOL> aliases . add ( r ) <EOL> col_aliases . add ( col [ <NUM_LIT:1> ] ) <EOL> else : <EOL> col_sql , col_params = col . as_sql ( qn , self . connection ) <EOL> result . append ( col_sql ) <EOL> params . extend ( col_params ) <EOL> if hasattr ( col , '<STR_LIT>' ) : <EOL> aliases . add ( col . alias ) <EOL> col_aliases . add ( col . alias ) <EOL> elif self . query . default_cols : <EOL> cols , new_aliases = self . get_default_columns ( with_aliases , <EOL> col_aliases ) <EOL> result . extend ( cols ) <EOL> aliases . update ( new_aliases ) <EOL> max_name_length = self . connection . ops . max_name_length ( ) <EOL> for alias , aggregate in self . query . aggregate_select . items ( ) : <EOL> agg_sql , agg_params = aggregate . as_sql ( qn , self . connection ) <EOL> if alias is None : <EOL> result . append ( agg_sql ) <EOL> else : <EOL> result . append ( '<STR_LIT>' % ( agg_sql , qn ( truncate_name ( alias , max_name_length ) ) ) ) <EOL> params . extend ( agg_params ) <EOL> for ( table , col ) , field in self . query . related_select_cols : <EOL> r = self . get_field_select ( field , table , col ) <EOL> if with_aliases and col in col_aliases : <EOL> c_alias = '<STR_LIT>' % len ( col_aliases ) <EOL> result . append ( '<STR_LIT>' % ( r , c_alias ) ) <EOL> aliases . add ( c_alias ) <EOL> col_aliases . add ( c_alias ) <EOL> else : <EOL> result . append ( r ) <EOL> aliases . add ( r ) <EOL> col_aliases . add ( col ) <EOL> self . _select_aliases = aliases <EOL> return result , params <EOL> def get_default_columns ( self , with_aliases = False , col_aliases = None , <EOL> start_alias = None , opts = None , as_pairs = False , from_parent = None ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> if opts is None : <EOL> opts = self . query . get_meta ( ) <EOL> aliases = set ( ) <EOL> only_load = self . deferred_to_columns ( ) <EOL> seen = self . query . included_inherited_models . copy ( ) <EOL> if start_alias : <EOL> seen [ None ] = start_alias <EOL> for field , model in opts . get_concrete_fields_with_model ( ) : <EOL> if from_parent and model is not None and issubclass ( from_parent , model ) : <EOL> continue <EOL> alias = self . query . join_parent_model ( opts , model , start_alias , seen ) <EOL> table = self . query . alias_map [ alias ] . table_name <EOL> if table in only_load and field . column not in only_load [ table ] : <EOL> continue <EOL> if as_pairs : <EOL> result . append ( ( alias , field ) ) <EOL> aliases . add ( alias ) <EOL> continue <EOL> field_sel = self . get_field_select ( field , alias ) <EOL> if with_aliases and field . column in col_aliases : <EOL> c_alias = '<STR_LIT>' % len ( col_aliases ) <EOL> result . append ( '<STR_LIT>' % ( field_sel , c_alias ) ) <EOL> col_aliases . add ( c_alias ) <EOL> aliases . add ( c_alias ) <EOL> else : <EOL> r = field_sel <EOL> result . append ( r ) <EOL> aliases . add ( r ) <EOL> if with_aliases : <EOL> col_aliases . add ( field . column ) <EOL> return result , aliases <EOL> def resolve_columns ( self , row , fields = ( ) ) : <EOL> """<STR_LIT>""" <EOL> values = [ ] <EOL> aliases = list ( self . query . extra_select ) <EOL> rn_offset = <NUM_LIT:0> <EOL> if self . connection . ops . oracle : <EOL> if self . query . high_mark is not None or self . query . low_mark : rn_offset = <NUM_LIT:1> <EOL> index_start = rn_offset + len ( aliases ) <EOL> values = [ self . query . convert_values ( v , <EOL> self . query . extra_select_fields . get ( a , None ) , <EOL> self . connection ) <EOL> for v , a in zip ( row [ rn_offset : index_start ] , aliases ) ] <EOL> if self . connection . ops . oracle or getattr ( self . query , '<STR_LIT>' , False ) : <EOL> for value , field in zip_longest ( row [ index_start : ] , fields ) : <EOL> values . append ( self . query . convert_values ( value , field , self . connection ) ) <EOL> else : <EOL> values . extend ( row [ index_start : ] ) <EOL> return tuple ( values ) <EOL> def get_extra_select_format ( self , alias ) : <EOL> sel_fmt = '<STR_LIT:%s>' <EOL> if hasattr ( self . query , '<STR_LIT>' ) and alias in self . query . custom_select : <EOL> sel_fmt = sel_fmt % self . query . custom_select [ alias ] <EOL> return sel_fmt <EOL> def get_field_select ( self , field , alias = None , column = None ) : <EOL> """<STR_LIT>""" <EOL> sel_fmt = self . get_select_format ( field ) <EOL> if field in self . query . custom_select : <EOL> field_sel = sel_fmt % self . query . custom_select [ field ] <EOL> else : <EOL> field_sel = sel_fmt % self . _field_column ( field , alias , column ) <EOL> return field_sel <EOL> def get_select_format ( self , fld ) : <EOL> """<STR_LIT>""" <EOL> if self . connection . ops . select and hasattr ( fld , '<STR_LIT>' ) : <EOL> sel_fmt = self . connection . ops . select <EOL> if self . query . transformed_srid and ( self . connection . ops . oracle or <EOL> self . connection . ops . spatialite ) : <EOL> sel_fmt = "<STR_LIT>" % ( self . query . transformed_srid , sel_fmt ) <EOL> else : <EOL> sel_fmt = '<STR_LIT:%s>' <EOL> return sel_fmt <EOL> def _field_column ( self , field , table_alias = None , column = None ) : <EOL> """<STR_LIT>""" <EOL> if table_alias is None : table_alias = self . query . get_meta ( ) . db_table <EOL> return "<STR_LIT>" % ( self . quote_name_unless_alias ( table_alias ) , <EOL> self . connection . ops . quote_name ( column or field . column ) ) <EOL> class SQLInsertCompiler ( compiler . SQLInsertCompiler , GeoSQLCompiler ) : <EOL> pass <EOL> class SQLDeleteCompiler ( compiler . SQLDeleteCompiler , GeoSQLCompiler ) : <EOL> pass <EOL> class SQLUpdateCompiler ( compiler . SQLUpdateCompiler , GeoSQLCompiler ) : <EOL> pass <EOL> class SQLAggregateCompiler ( compiler . SQLAggregateCompiler , GeoSQLCompiler ) : <EOL> pass <EOL> class SQLDateCompiler ( compiler . SQLDateCompiler , GeoSQLCompiler ) : <EOL> """<STR_LIT>""" <EOL> def results_iter ( self ) : <EOL> if self . connection . ops . oracle : <EOL> from django . db . models . fields import DateTimeField <EOL> fields = [ DateTimeField ( ) ] <EOL> else : <EOL> needs_string_cast = self . connection . features . needs_datetime_string_cast <EOL> offset = len ( self . query . extra_select ) <EOL> for rows in self . execute_sql ( MULTI ) : <EOL> for row in rows : <EOL> date = row [ offset ] <EOL> if self . connection . ops . oracle : <EOL> date = self . resolve_columns ( row , fields ) [ offset ] <EOL> elif needs_string_cast : <EOL> date = typecast_date ( str ( date ) ) <EOL> if isinstance ( date , datetime . datetime ) : <EOL> date = date . date ( ) <EOL> yield date <EOL> class SQLDateTimeCompiler ( compiler . SQLDateTimeCompiler , GeoSQLCompiler ) : <EOL> """<STR_LIT>""" <EOL> def results_iter ( self ) : <EOL> if self . connection . ops . oracle : <EOL> from django . db . models . fields import DateTimeField <EOL> fields = [ DateTimeField ( ) ] <EOL> else : <EOL> needs_string_cast = self . connection . features . needs_datetime_string_cast <EOL> offset = len ( self . query . extra_select ) <EOL> for rows in self . execute_sql ( MULTI ) : <EOL> for row in rows : <EOL> datetime = row [ offset ] <EOL> if self . connection . ops . oracle : <EOL> datetime = self . resolve_columns ( row , fields ) [ offset ] <EOL> elif needs_string_cast : <EOL> datetime = typecast_timestamp ( str ( datetime ) ) <EOL> if settings . USE_TZ : <EOL> datetime = datetime . replace ( tzinfo = None ) <EOL> datetime = timezone . make_aware ( datetime , self . query . tzinfo ) <EOL> yield datetime </s>
