<s> import copy <EOL> import re <EOL> from django . core . exceptions import ObjectDoesNotExist , MultipleObjectsReturned <EOL> from django . db import connection <EOL> from django . db . models import Q <EOL> from django . db . models . sql . query import AND , OR <EOL> from django . utils . tree import Node <EOL> ORDER_PATTERN = re . compile ( r'<STR_LIT>' ) <EOL> FIELD_PATTERN = re . compile ( r'<STR_LIT>' ) <EOL> RAW_FILTER_PATTERN = re . compile ( <EOL> r'<STR_LIT>' , <EOL> re . I ) <EOL> class LazyRawSQLManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , sql_model_class ) : <EOL> self . __sql_model_class = sql_model_class <EOL> self . __manager = None <EOL> def __getattr__ ( self , name ) : <EOL> if not self . __manager : <EOL> self . __manager = RawSQLManager ( self . __sql_model_class ( ) ) <EOL> return getattr ( self . __manager , name ) <EOL> class RawSQLManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , sql_model , base_query = None ) : <EOL> self . sql_model = sql_model <EOL> if not base_query : <EOL> base_query = copy . deepcopy ( sql_model . base_query ( ) ) <EOL> self . base_query = base_query <EOL> if '<STR_LIT>' not in self . base_query : <EOL> self . base_query [ '<STR_LIT>' ] = [ ] <EOL> if '<STR_LIT>' not in self . base_query : <EOL> self . base_query [ '<STR_LIT>' ] = [ ] <EOL> if '<STR_LIT>' not in self . base_query : <EOL> self . base_query [ '<STR_LIT>' ] = [ ] <EOL> if '<STR_LIT>' not in self . base_query : <EOL> self . base_query [ '<STR_LIT>' ] = [ ] <EOL> if '<STR_LIT>' not in self . base_query : <EOL> self . base_query [ '<STR_LIT>' ] = { } <EOL> self . _cursor = None <EOL> self . _record_set = [ ] <EOL> def __iter__ ( self ) : <EOL> self . _build_cursor ( ) <EOL> for row in self . _iter_cursor_results ( ) : <EOL> yield row <EOL> def __getitem__ ( self , key ) : <EOL> if isinstance ( key , slice ) : <EOL> if key . start and key . stop : <EOL> offset = self . _check_limit ( key . start ) <EOL> end = self . _check_limit ( key . stop ) <EOL> row_count = max ( <NUM_LIT:0> , end - offset ) <EOL> self . base_query [ '<STR_LIT>' ] = [ offset , row_count ] <EOL> elif key . start : <EOL> self . base_query [ '<STR_LIT>' ] = [ self . _check_limit ( key . start ) ] <EOL> elif key . stop : <EOL> self . base_query [ '<STR_LIT>' ] = [ <NUM_LIT:0> , self . _check_limit ( key . stop ) ] <EOL> self . _build_cursor ( ) <EOL> self . _build_record_set ( ) <EOL> return self . _record_set <EOL> elif isinstance ( key , int ) : <EOL> if not len ( self . _record_set ) : <EOL> self . _build_cursor ( ) <EOL> self . _build_record_set ( ) <EOL> return self . _record_set [ key ] <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def all ( self ) : <EOL> return self . _clone ( ) <EOL> def count ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _execute ( '<STR_LIT>' % self . as_sql ( ) ) <EOL> return self . _cursor . fetchone ( ) [ <NUM_LIT:0> ] <EOL> def get ( self , ** kw ) : <EOL> clone = self . _clone ( ) <EOL> if kw : <EOL> clone = clone . filter ( ** kw ) <EOL> cnt = clone . count ( ) <EOL> if cnt > <NUM_LIT:1> : <EOL> raise clone . sql_model . MultipleObjectsReturned ( <EOL> '<STR_LIT>' % cnt ) <EOL> elif cnt == <NUM_LIT:0> : <EOL> raise clone . sql_model . DoesNotExist ( <EOL> '<STR_LIT>' % <EOL> self . sql_model . __class__ . __name__ ) <EOL> else : <EOL> return clone [ <NUM_LIT:0> : <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> def exclude ( self , * args , ** kw ) : <EOL> raise NotImplementedError ( ) <EOL> def filter ( self , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> clone = self . _clone ( ) <EOL> for arg in args : <EOL> if isinstance ( arg , Q ) : <EOL> clone . base_query [ '<STR_LIT>' ] . append ( <EOL> '<STR_LIT>' % ( clone . _flatten_q ( arg , clone . _kw_clause_from_q ) ) ) <EOL> else : <EOL> raise TypeError ( <EOL> '<STR_LIT>' % arg ) <EOL> for field , val in kw . items ( ) : <EOL> clone . base_query [ '<STR_LIT>' ] . append ( clone . _kw_filter_to_clause ( field , <EOL> val ) ) <EOL> return clone <EOL> def filter_raw ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> clone = self . _clone ( ) <EOL> specs = [ ] <EOL> for arg in args : <EOL> if isinstance ( arg , Q ) : <EOL> clone . base_query [ '<STR_LIT>' ] . append ( <EOL> '<STR_LIT>' % ( clone . _flatten_q ( arg , clone . _filter_to_clause ) ) ) <EOL> else : <EOL> specs . append ( arg ) <EOL> if len ( specs ) : <EOL> clone . base_query [ '<STR_LIT>' ] . append ( clone . _filter_to_clause ( * specs ) ) <EOL> return clone <EOL> def having ( self , spec , val ) : <EOL> """<STR_LIT>""" <EOL> clone = self . _clone ( ) <EOL> clone . base_query [ '<STR_LIT>' ] . append ( clone . _filter_to_clause ( spec , val ) ) <EOL> return clone <EOL> def latest ( self , column ) : <EOL> """<STR_LIT>""" <EOL> clone = self . _clone ( ) <EOL> clone . order_by ( '<STR_LIT>' % column ) <EOL> if clone . count ( ) == <NUM_LIT:0> : <EOL> raise clone . sql_model . DoesNotExist ( <EOL> '<STR_LIT>' % <EOL> self . sql_model . __class__ . __name__ ) <EOL> return clone [ <NUM_LIT:0> ] <EOL> def order_by ( self , spec ) : <EOL> """<STR_LIT>""" <EOL> if not ORDER_PATTERN . match ( spec ) : <EOL> raise ValueError ( '<STR_LIT>' % spec ) <EOL> if spec . startswith ( '<STR_LIT:->' ) : <EOL> dir = '<STR_LIT>' <EOL> field = spec [ <NUM_LIT:1> : ] <EOL> else : <EOL> dir = '<STR_LIT>' <EOL> field = spec <EOL> clone = self . _clone ( ) <EOL> clone . base_query [ '<STR_LIT>' ] . append ( '<STR_LIT>' % <EOL> ( clone . _resolve_alias ( field ) , dir ) ) <EOL> return clone <EOL> def as_sql ( self ) : <EOL> stmt = self . _compile ( self . base_query ) <EOL> return stmt <EOL> def _clone ( self ) : <EOL> return self . __class__ ( self . sql_model , <EOL> base_query = copy . deepcopy ( self . base_query ) ) <EOL> def _flatten_q ( self , q_object , join_specs , stack = None ) : <EOL> """<STR_LIT>""" <EOL> specs = [ ] <EOL> if stack is None : <EOL> stack = [ None ] <EOL> if q_object . negated : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> connector = q_object . connector <EOL> def add ( specs ) : <EOL> c = join_specs ( * specs , connector = connector ) <EOL> if stack [ - <NUM_LIT:1> ] in ( AND , OR ) : <EOL> c = u'<STR_LIT>' % ( c ) <EOL> elif stack [ - <NUM_LIT:1> ] is not None : <EOL> stack . append ( connector ) <EOL> if c : <EOL> stack . append ( c ) <EOL> for child in q_object . children : <EOL> if isinstance ( child , Node ) : <EOL> add ( specs ) <EOL> specs [ : ] = [ ] <EOL> self . _flatten_q ( child , join_specs , stack = stack ) <EOL> else : <EOL> specs . append ( child ) <EOL> if len ( specs ) : <EOL> add ( specs ) <EOL> return u'<STR_LIT:U+0020>' . join ( [ c for c in stack if c ] ) <EOL> def _kw_clause_from_q ( self , * pairs , ** kw ) : <EOL> """<STR_LIT>""" <EOL> connector = kw . get ( '<STR_LIT>' , AND ) <EOL> stmt = [ ] <EOL> for field , val in pairs : <EOL> stmt . append ( self . _kw_filter_to_clause ( field , val ) ) <EOL> return ( u'<STR_LIT>' % connector ) . join ( stmt ) <EOL> def _kw_filter_to_clause ( self , field , val ) : <EOL> """<STR_LIT>""" <EOL> if not FIELD_PATTERN . match ( field ) : <EOL> raise ValueError ( '<STR_LIT>' % field ) <EOL> param_k = self . _param ( val ) <EOL> field = self . _resolve_alias ( field ) <EOL> return u'<STR_LIT>' % ( field , param_k ) <EOL> def _filter_to_clause ( self , * specs , ** kw ) : <EOL> """<STR_LIT>""" <EOL> connector = kw . get ( '<STR_LIT>' , AND ) <EOL> specs = list ( specs ) <EOL> if ( len ( specs ) % <NUM_LIT:2> ) != <NUM_LIT:0> : <EOL> raise TypeError ( <EOL> "<STR_LIT>" % specs ) <EOL> full_clause = [ ] <EOL> while len ( specs ) : <EOL> spec , val = specs . pop ( <NUM_LIT:0> ) , specs . pop ( <NUM_LIT:0> ) <EOL> clause = RAW_FILTER_PATTERN . match ( spec ) <EOL> if not clause : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % ( <EOL> spec , RAW_FILTER_PATTERN . pattern ) ) <EOL> field = clause . group ( '<STR_LIT>' ) <EOL> field = self . _resolve_alias ( field ) <EOL> if clause . group ( '<STR_LIT>' ) . lower ( ) == '<STR_LIT>' : <EOL> parts = [ '<STR_LIT>' + self . _param ( p ) + '<STR_LIT>' for p in iter ( val ) ] <EOL> param = '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( parts ) <EOL> else : <EOL> param = '<STR_LIT>' % self . _param ( val ) <EOL> full_clause . append ( '<STR_LIT>' % ( field , clause . group ( '<STR_LIT>' ) , param ) ) <EOL> c = ( u'<STR_LIT>' % connector ) . join ( full_clause ) <EOL> if len ( full_clause ) > <NUM_LIT:1> : <EOL> c = u'<STR_LIT>' % c <EOL> return c <EOL> def _resolve_alias ( self , field ) : <EOL> """<STR_LIT>""" <EOL> if field in self . base_query [ '<STR_LIT>' ] : <EOL> field = self . base_query [ '<STR_LIT>' ] [ field ] <EOL> return field <EOL> def _compile ( self , parts ) : <EOL> sep = u"<STR_LIT>" <EOL> and_ = u'<STR_LIT>' % AND <EOL> select = [ u'<STR_LIT>' % ( v , k ) for k , v in parts [ '<STR_LIT>' ] . items ( ) ] <EOL> stmt = u"<STR_LIT>" % ( sep . join ( select ) , <EOL> u"<STR_LIT:\n>" . join ( parts [ '<STR_LIT>' ] ) ) <EOL> if parts . get ( '<STR_LIT>' ) : <EOL> stmt = u"<STR_LIT>" % ( stmt , and_ . join ( parts [ '<STR_LIT>' ] ) ) <EOL> if parts . get ( '<STR_LIT>' ) : <EOL> stmt = u"<STR_LIT>" % ( stmt , parts [ '<STR_LIT>' ] ) <EOL> if parts . get ( '<STR_LIT>' ) : <EOL> stmt = u"<STR_LIT>" % ( stmt , and_ . join ( parts [ '<STR_LIT>' ] ) ) <EOL> if parts . get ( '<STR_LIT>' ) : <EOL> stmt = u"<STR_LIT>" % ( stmt , sep . join ( parts [ '<STR_LIT>' ] ) ) <EOL> if len ( parts [ '<STR_LIT>' ] ) : <EOL> stmt = u"<STR_LIT>" % ( stmt , '<STR_LIT:U+002CU+0020>' . join ( [ str ( i ) for i in <EOL> parts [ '<STR_LIT>' ] ] ) ) <EOL> return stmt <EOL> def _execute ( self , sql ) : <EOL> self . _record_set = [ ] <EOL> self . _cursor = connection . cursor ( ) <EOL> self . _cursor . execute ( sql , self . base_query [ '<STR_LIT>' ] ) <EOL> def _param ( self , val ) : <EOL> param_k = '<STR_LIT>' % len ( self . base_query [ '<STR_LIT>' ] . keys ( ) ) <EOL> self . base_query [ '<STR_LIT>' ] [ param_k ] = val <EOL> return param_k <EOL> def _build_cursor ( self ) : <EOL> self . _execute ( self . as_sql ( ) ) <EOL> def _build_record_set ( self ) : <EOL> self . _record_set = [ ] <EOL> for row in self . _iter_cursor_results ( ) : <EOL> self . _record_set . append ( row ) <EOL> def _iter_cursor_results ( self ) : <EOL> col_names = [ c [ <NUM_LIT:0> ] for c in self . _cursor . description ] <EOL> while <NUM_LIT:1> : <EOL> row = self . _cursor . fetchone ( ) <EOL> if row is None : <EOL> break <EOL> yield self . _make_row ( row , col_names ) <EOL> def _make_row ( self , row , col_names ) : <EOL> values = dict ( zip ( col_names , row ) ) <EOL> return self . sql_model . __class__ ( ** values ) <EOL> def _check_limit ( self , i ) : <EOL> i = int ( i ) <EOL> if i < <NUM_LIT:0> : <EOL> raise IndexError ( "<STR_LIT>" ) <EOL> return i <EOL> class RawSQLModelMeta ( type ) : <EOL> def __new__ ( cls , name , bases , attrs ) : <EOL> super_new = super ( RawSQLModelMeta , cls ) . __new__ <EOL> cls = super_new ( cls , name , bases , attrs ) <EOL> cls . objects = LazyRawSQLManager ( cls ) <EOL> return cls <EOL> class RawSQLModel ( object ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = RawSQLModelMeta <EOL> class DoesNotExist ( ObjectDoesNotExist ) : <EOL> pass <EOL> MultipleObjectsReturned = MultipleObjectsReturned <EOL> def __init__ ( self , ** kwargs ) : <EOL> for key , val in kwargs . items ( ) : <EOL> field = getattr ( self . __class__ , key , None ) <EOL> if field is None : <EOL> raise TypeError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % key ) <EOL> setattr ( self , field . get_attname ( ) or key , field . to_python ( val ) ) <EOL> def base_query ( self ) : <EOL> """<STR_LIT>""" <EOL> return { } <EOL> def _explode_concat ( self , value , sep = '<STR_LIT:U+002C>' , cast = int ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> return [ ] <EOL> return [ cast ( i ) for i in value . split ( sep ) if i ] </s>
