<s> from itertools import izip <EOL> from django . db . models . query import sql <EOL> from django . db . models . fields . related import ForeignKey <EOL> from django . contrib . gis . db . backend import SpatialBackend <EOL> from django . contrib . gis . db . models . fields import GeometryField <EOL> from django . contrib . gis . db . models . sql import aggregates as gis_aggregates_module <EOL> from django . contrib . gis . db . models . sql . conversion import AreaField , DistanceField , GeomField <EOL> from django . contrib . gis . db . models . sql . where import GeoWhereNode <EOL> from django . contrib . gis . measure import Area , Distance <EOL> ALL_TERMS = sql . constants . QUERY_TERMS . copy ( ) <EOL> ALL_TERMS . update ( SpatialBackend . gis_terms ) <EOL> TABLE_NAME = sql . constants . TABLE_NAME <EOL> get_proxied_model = sql . query . get_proxied_model <EOL> class GeoQuery ( sql . Query ) : <EOL> """<STR_LIT>""" <EOL> query_terms = ALL_TERMS <EOL> aggregates_module = gis_aggregates_module <EOL> def __init__ ( self , model , conn ) : <EOL> super ( GeoQuery , self ) . __init__ ( model , conn , where = GeoWhereNode ) <EOL> self . custom_select = { } <EOL> self . transformed_srid = None <EOL> self . extra_select_fields = { } <EOL> if SpatialBackend . oracle : <EOL> def __reduce__ ( self ) : <EOL> callable , args , data = super ( GeoQuery , self ) . __reduce__ ( ) <EOL> return ( unpickle_geoquery , ( ) , data ) <EOL> def clone ( self , * args , ** kwargs ) : <EOL> obj = super ( GeoQuery , self ) . clone ( * args , ** kwargs ) <EOL> obj . custom_select = self . custom_select . copy ( ) <EOL> obj . transformed_srid = self . transformed_srid <EOL> obj . extra_select_fields = self . extra_select_fields . copy ( ) <EOL> return obj <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 self . extra_select . iteritems ( ) ] <EOL> aliases = set ( self . extra_select . keys ( ) ) <EOL> if with_aliases : <EOL> col_aliases = aliases . copy ( ) <EOL> else : <EOL> col_aliases = set ( ) <EOL> if self . select : <EOL> only_load = self . deferred_to_columns ( ) <EOL> for col , field in izip ( self . select , self . select_fields ) : <EOL> if isinstance ( col , ( list , tuple ) ) : <EOL> alias , column = col <EOL> table = self . alias_map [ alias ] [ TABLE_NAME ] <EOL> if table in only_load and col 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> result . append ( col . as_sql ( quote_func = qn ) ) <EOL> if hasattr ( col , '<STR_LIT>' ) : <EOL> aliases . add ( col . alias ) <EOL> col_aliases . add ( col . alias ) <EOL> elif self . 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> result . extend ( [ <EOL> '<STR_LIT>' % ( <EOL> self . get_extra_select_format ( alias ) % aggregate . as_sql ( quote_func = qn ) , <EOL> alias is not None and '<STR_LIT>' % alias or '<STR_LIT>' <EOL> ) <EOL> for alias , aggregate in self . aggregate_select . items ( ) <EOL> ] ) <EOL> for ( table , col ) , field in izip ( self . related_select_cols , self . related_select_fields ) : <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 <EOL> def get_default_columns ( self , with_aliases = False , col_aliases = None , <EOL> start_alias = None , opts = None , as_pairs = False ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> if opts is None : <EOL> opts = self . model . _meta <EOL> aliases = set ( ) <EOL> only_load = self . deferred_to_columns ( ) <EOL> proxied_model = get_proxied_model ( opts ) <EOL> if start_alias : <EOL> seen = { None : start_alias } <EOL> for field , model in opts . get_fields_with_model ( ) : <EOL> if start_alias : <EOL> try : <EOL> alias = seen [ model ] <EOL> except KeyError : <EOL> if model is proxied_model : <EOL> alias = start_alias <EOL> else : <EOL> link_field = opts . get_ancestor_link ( model ) <EOL> alias = self . join ( ( start_alias , model . _meta . db_table , <EOL> link_field . column , model . _meta . pk . column ) ) <EOL> seen [ model ] = alias <EOL> else : <EOL> alias = self . included_inherited_models [ model ] <EOL> table = self . 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 . column ) ) <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 = self . extra_select . keys ( ) <EOL> if self . aggregates : <EOL> aliases . extend ( [ None for i in xrange ( len ( self . aggregates ) ) ] ) <EOL> rn_offset = <NUM_LIT:0> <EOL> if SpatialBackend . oracle : <EOL> if self . high_mark is not None or self . low_mark : rn_offset = <NUM_LIT:1> <EOL> index_start = rn_offset + len ( aliases ) <EOL> values = [ self . convert_values ( v , self . extra_select_fields . get ( a , None ) ) <EOL> for v , a in izip ( row [ rn_offset : index_start ] , aliases ) ] <EOL> if SpatialBackend . oracle or getattr ( self , '<STR_LIT>' , False ) : <EOL> for value , field in izip ( row [ index_start : ] , fields ) : <EOL> values . append ( self . convert_values ( value , field ) ) <EOL> else : <EOL> values . extend ( row [ index_start : ] ) <EOL> return tuple ( values ) <EOL> def convert_values ( self , value , field ) : <EOL> """<STR_LIT>""" <EOL> if SpatialBackend . oracle : <EOL> value = super ( GeoQuery , self ) . convert_values ( value , field or GeomField ( ) ) <EOL> if isinstance ( field , DistanceField ) : <EOL> value = Distance ( ** { field . distance_att : value } ) <EOL> elif isinstance ( field , AreaField ) : <EOL> value = Area ( ** { field . area_att : value } ) <EOL> elif isinstance ( field , ( GeomField , GeometryField ) ) and value : <EOL> value = SpatialBackend . Geometry ( value ) <EOL> return value <EOL> def resolve_aggregate ( self , value , aggregate ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( aggregate , self . aggregates_module . GeoAggregate ) : <EOL> if aggregate . is_extent : <EOL> return self . aggregates_module . convert_extent ( value ) <EOL> else : <EOL> return self . aggregates_module . convert_geom ( value , aggregate . source ) <EOL> else : <EOL> return super ( GeoQuery , self ) . resolve_aggregate ( value , aggregate ) <EOL> def get_extra_select_format ( self , alias ) : <EOL> sel_fmt = '<STR_LIT:%s>' <EOL> if alias in self . custom_select : <EOL> sel_fmt = sel_fmt % self . 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 . custom_select : <EOL> field_sel = sel_fmt % self . 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 SpatialBackend . select and hasattr ( fld , '<STR_LIT>' ) : <EOL> sel_fmt = SpatialBackend . select <EOL> if self . transformed_srid and ( SpatialBackend . oracle or <EOL> SpatialBackend . spatialite ) : <EOL> sel_fmt = "<STR_LIT>" % ( self . 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 . model . _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> def _geo_field ( self , field_name = None ) : <EOL> """<STR_LIT>""" <EOL> if field_name is None : <EOL> for fld in self . model . _meta . fields : <EOL> if isinstance ( fld , GeometryField ) : return fld <EOL> return False <EOL> else : <EOL> return GeoWhereNode . _check_geo_field ( self . model . _meta , field_name ) <EOL> if SpatialBackend . oracle : <EOL> def unpickle_geoquery ( ) : <EOL> """<STR_LIT>""" <EOL> return GeoQuery . __new__ ( GeoQuery ) <EOL> unpickle_geoquery . __safe_for_unpickling__ = True </s>
