<s> '''<STR_LIT>''' <EOL> import itertools <EOL> import datetime <EOL> def safeunicode ( obj , encoding = '<STR_LIT:utf-8>' ) : <EOL> r"""<STR_LIT>""" <EOL> t = type ( obj ) <EOL> if t is unicode : <EOL> return obj <EOL> elif t is str : <EOL> return obj . decode ( encoding ) <EOL> elif t in [ int , float , bool ] : <EOL> return unicode ( obj ) <EOL> elif hasattr ( obj , '<STR_LIT>' ) or isinstance ( obj , unicode ) : <EOL> return unicode ( obj ) <EOL> else : <EOL> return str ( obj ) . decode ( encoding ) <EOL> def safestr ( obj , encoding = '<STR_LIT:utf-8>' ) : <EOL> r"""<STR_LIT>""" <EOL> if isinstance ( obj , unicode ) : <EOL> return obj . encode ( encoding ) <EOL> elif isinstance ( obj , str ) : <EOL> return obj <EOL> elif hasattr ( obj , '<STR_LIT>' ) : <EOL> return itertools . imap ( safestr , obj ) <EOL> else : <EOL> return str ( obj ) <EOL> def sqlify ( obj ) : <EOL> """<STR_LIT>""" <EOL> if obj is None : <EOL> return '<STR_LIT>' <EOL> elif obj is True : <EOL> return "<STR_LIT>" <EOL> elif obj is False : <EOL> return "<STR_LIT>" <EOL> elif datetime and isinstance ( obj , datetime . datetime ) : <EOL> return repr ( obj . isoformat ( ) ) <EOL> else : <EOL> if isinstance ( obj , unicode ) : obj = obj . encode ( '<STR_LIT:utf8>' ) <EOL> return repr ( obj ) <EOL> def sqllist ( lst ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( lst , basestring ) : <EOL> return lst <EOL> else : <EOL> return '<STR_LIT:U+002CU+0020>' . join ( lst ) <EOL> def _sqllist ( values ) : <EOL> """<STR_LIT>""" <EOL> items = [ ] <EOL> items . append ( '<STR_LIT:(>' ) <EOL> for i , v in enumerate ( values ) : <EOL> if i != <NUM_LIT:0> : <EOL> items . append ( '<STR_LIT:U+002CU+0020>' ) <EOL> items . append ( sqlparam ( v ) ) <EOL> items . append ( '<STR_LIT:)>' ) <EOL> return SQLQuery ( items ) <EOL> def sqlquote ( a ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( a , list ) : <EOL> return _sqllist ( a ) <EOL> else : <EOL> return sqlparam ( a ) . sqlquery ( ) <EOL> def _interpolate ( sformat ) : <EOL> """<STR_LIT>""" <EOL> from tokenize import tokenprog <EOL> tokenprog = tokenprog <EOL> def matchorfail ( text , pos ) : <EOL> match = tokenprog . match ( text , pos ) <EOL> if match is None : <EOL> raise _ItplError ( text , pos ) <EOL> return match , match . end ( ) <EOL> namechars = "<STR_LIT>" "<STR_LIT>" ; <EOL> chunks = [ ] <EOL> pos = <NUM_LIT:0> <EOL> while <NUM_LIT:1> : <EOL> dollar = sformat . find ( "<STR_LIT:$>" , pos ) <EOL> if dollar < <NUM_LIT:0> : <EOL> break <EOL> nextchar = sformat [ dollar + <NUM_LIT:1> ] <EOL> if nextchar == "<STR_LIT:{>" : <EOL> chunks . append ( ( <NUM_LIT:0> , sformat [ pos : dollar ] ) ) <EOL> pos , level = dollar + <NUM_LIT:2> , <NUM_LIT:1> <EOL> while level : <EOL> match , pos = matchorfail ( sformat , pos ) <EOL> tstart , tend = match . regs [ <NUM_LIT:3> ] <EOL> token = sformat [ tstart : tend ] <EOL> if token == "<STR_LIT:{>" : <EOL> level = level + <NUM_LIT:1> <EOL> elif token == "<STR_LIT:}>" : <EOL> level = level - <NUM_LIT:1> <EOL> chunks . append ( ( <NUM_LIT:1> , sformat [ dollar + <NUM_LIT:2> : pos - <NUM_LIT:1> ] ) ) <EOL> elif nextchar in namechars : <EOL> chunks . append ( ( <NUM_LIT:0> , sformat [ pos : dollar ] ) ) <EOL> match , pos = matchorfail ( sformat , dollar + <NUM_LIT:1> ) <EOL> while pos < len ( sformat ) : <EOL> if sformat [ pos ] == "<STR_LIT:.>" and pos + <NUM_LIT:1> < len ( sformat ) and sformat [ pos + <NUM_LIT:1> ] in namechars : <EOL> match , pos = matchorfail ( sformat , pos + <NUM_LIT:1> ) <EOL> elif sformat [ pos ] in "<STR_LIT>" : <EOL> pos , level = pos + <NUM_LIT:1> , <NUM_LIT:1> <EOL> while level : <EOL> match , pos = matchorfail ( sformat , pos ) <EOL> tstart , tend = match . regs [ <NUM_LIT:3> ] <EOL> token = sformat [ tstart : tend ] <EOL> if token [ <NUM_LIT:0> ] in "<STR_LIT>" : <EOL> level = level + <NUM_LIT:1> <EOL> elif token [ <NUM_LIT:0> ] in "<STR_LIT>" : <EOL> level = level - <NUM_LIT:1> <EOL> else : <EOL> break <EOL> chunks . append ( ( <NUM_LIT:1> , sformat [ dollar + <NUM_LIT:1> : pos ] ) ) <EOL> else : <EOL> chunks . append ( ( <NUM_LIT:0> , sformat [ pos : dollar + <NUM_LIT:1> ] ) ) <EOL> pos = dollar + <NUM_LIT:1> + ( nextchar == "<STR_LIT:$>" ) <EOL> if pos < len ( sformat ) : <EOL> chunks . append ( ( <NUM_LIT:0> , sformat [ pos : ] ) ) <EOL> return chunks <EOL> def sqlwhere ( dictionary , grouping = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return SQLQuery . join ( [ k + '<STR_LIT>' + sqlparam ( v ) for k , v in dictionary . items ( ) ] , grouping ) <EOL> def reparam ( string_ , dictionary ) : <EOL> """<STR_LIT>""" <EOL> dictionary = dictionary . copy ( ) <EOL> result = [ ] <EOL> for live , chunk in _interpolate ( string_ ) : <EOL> if live : <EOL> v = eval ( chunk , dictionary ) <EOL> result . append ( sqlquote ( v ) ) <EOL> else : <EOL> result . append ( chunk ) <EOL> return SQLQuery . join ( result , '<STR_LIT>' ) <EOL> class UnknownParamstyle ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class _ItplError ( ValueError ) : <EOL> def __init__ ( self , text , pos ) : <EOL> ValueError . __init__ ( self ) <EOL> self . text = text <EOL> self . pos = pos <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> repr ( self . text ) , self . pos ) <EOL> class SQLParam ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ "<STR_LIT:value>" ] <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> def get_marker ( self , paramstyle = '<STR_LIT>' ) : <EOL> if paramstyle == '<STR_LIT>' : <EOL> return '<STR_LIT:?>' <EOL> elif paramstyle == '<STR_LIT>' : <EOL> return '<STR_LIT>' <EOL> elif paramstyle is None or paramstyle in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return '<STR_LIT:%s>' <EOL> raise UnknownParamstyle , paramstyle <EOL> def sqlquery ( self ) : <EOL> return SQLQuery ( [ self ] ) <EOL> def __add__ ( self , other ) : <EOL> return self . sqlquery ( ) + other <EOL> def __radd__ ( self , other ) : <EOL> return other + self . sqlquery ( ) <EOL> def __str__ ( self ) : <EOL> return str ( self . value ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % repr ( self . value ) <EOL> sqlparam = SQLParam <EOL> class SQLQuery ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ "<STR_LIT>" ] <EOL> def __init__ ( self , items = None ) : <EOL> r"""<STR_LIT>""" <EOL> if items is None : <EOL> self . items = [ ] <EOL> elif isinstance ( items , list ) : <EOL> self . items = items <EOL> elif isinstance ( items , SQLParam ) : <EOL> self . items = [ items ] <EOL> elif isinstance ( items , SQLQuery ) : <EOL> self . items = list ( items . items ) <EOL> else : <EOL> self . items = [ items ] <EOL> for i , item in enumerate ( self . items ) : <EOL> if isinstance ( item , SQLParam ) and isinstance ( item . value , SQLLiteral ) : <EOL> self . items [ i ] = item . value . v <EOL> def append ( self , value ) : <EOL> self . items . append ( value ) <EOL> def __add__ ( self , other ) : <EOL> if isinstance ( other , basestring ) : <EOL> items = [ other ] <EOL> elif isinstance ( other , SQLQuery ) : <EOL> items = other . items <EOL> else : <EOL> return NotImplemented <EOL> return SQLQuery ( self . items + items ) <EOL> def __radd__ ( self , other ) : <EOL> if isinstance ( other , basestring ) : <EOL> items = [ other ] <EOL> else : <EOL> return NotImplemented <EOL> return SQLQuery ( items + self . items ) <EOL> def __iadd__ ( self , other ) : <EOL> if isinstance ( other , ( basestring , SQLParam ) ) : <EOL> self . items . append ( other ) <EOL> elif isinstance ( other , SQLQuery ) : <EOL> self . items . extend ( other . items ) <EOL> else : <EOL> return NotImplemented <EOL> return self <EOL> def __len__ ( self ) : <EOL> return len ( self . query ( ) ) <EOL> def query ( self , paramstyle = None ) : <EOL> """<STR_LIT>""" <EOL> s = [ ] <EOL> for x in self . items : <EOL> if isinstance ( x , SQLParam ) : <EOL> x = x . get_marker ( paramstyle ) <EOL> s . append ( safestr ( x ) ) <EOL> else : <EOL> x = safestr ( x ) <EOL> if paramstyle in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if '<STR_LIT:%>' in x and '<STR_LIT>' not in x : <EOL> x = x . replace ( '<STR_LIT:%>' , '<STR_LIT>' ) <EOL> s . append ( x ) <EOL> return "<STR_LIT>" . join ( s ) <EOL> def values ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ i . value for i in self . items if isinstance ( i , SQLParam ) ] <EOL> def join ( items , sep = '<STR_LIT:U+0020>' , prefix = None , suffix = None , target = None ) : <EOL> """<STR_LIT>""" <EOL> if target is None : <EOL> target = SQLQuery ( ) <EOL> target_items = target . items <EOL> if prefix : <EOL> target_items . append ( prefix ) <EOL> for i , item in enumerate ( items ) : <EOL> if i != <NUM_LIT:0> : <EOL> target_items . append ( sep ) <EOL> if isinstance ( item , SQLQuery ) : <EOL> target_items . extend ( item . items ) <EOL> else : <EOL> target_items . append ( item ) <EOL> if suffix : <EOL> target_items . append ( suffix ) <EOL> return target <EOL> join = staticmethod ( join ) <EOL> def _str ( self ) : <EOL> try : <EOL> return self . query ( ) % tuple ( [ sqlify ( x ) for x in self . values ( ) ] ) <EOL> except ( ValueError , TypeError ) : <EOL> return self . query ( ) <EOL> def __str__ ( self ) : <EOL> return safestr ( self . _str ( ) ) <EOL> def __unicode__ ( self ) : <EOL> return safeunicode ( self . _str ( ) ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % repr ( str ( self ) ) <EOL> class SQLLiteral : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , v ) : <EOL> self . v = v <EOL> def __repr__ ( self ) : <EOL> return self . v <EOL> class SQLProducer : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def query ( self , sql_query , processed = False , svars = None ) : <EOL> """<STR_LIT>""" <EOL> if svars is None : <EOL> svars = { } <EOL> if not processed and not isinstance ( sql_query , SQLQuery ) : <EOL> sql_query = reparam ( sql_query , svars ) <EOL> return sql_query <EOL> def sql_clauses ( self , what , tables , where , group , order , limit , offset ) : <EOL> return ( <EOL> ( '<STR_LIT>' , what ) , <EOL> ( '<STR_LIT>' , sqllist ( tables ) ) , <EOL> ( '<STR_LIT>' , where ) , <EOL> ( '<STR_LIT>' , group ) , <EOL> ( '<STR_LIT>' , order ) , <EOL> ( '<STR_LIT>' , limit ) , <EOL> ( '<STR_LIT>' , offset ) ) <EOL> def gen_clause ( self , sql , val , svars ) : <EOL> if isinstance ( val , ( int , long ) ) : <EOL> if sql == '<STR_LIT>' : <EOL> nout = '<STR_LIT>' + sqlquote ( val ) <EOL> else : <EOL> nout = SQLQuery ( val ) <EOL> elif isinstance ( val , ( list , tuple ) ) and len ( val ) == <NUM_LIT:2> : <EOL> nout = SQLQuery ( val [ <NUM_LIT:0> ] , val [ <NUM_LIT:1> ] ) <EOL> elif isinstance ( val , SQLQuery ) : <EOL> nout = val <EOL> else : <EOL> nout = reparam ( val , svars ) <EOL> def xjoin ( a , b ) : <EOL> if a and b : return a + '<STR_LIT:U+0020>' + b <EOL> else : return a or b <EOL> return xjoin ( sql , nout ) <EOL> def _where ( self , where , svars ) : <EOL> if isinstance ( where , ( int , long ) ) : <EOL> where = "<STR_LIT>" + sqlparam ( where ) <EOL> elif isinstance ( where , ( list , tuple ) ) and len ( where ) == <NUM_LIT:2> : <EOL> where = SQLQuery ( where [ <NUM_LIT:0> ] , where [ <NUM_LIT:1> ] ) <EOL> elif isinstance ( where , SQLQuery ) : <EOL> pass <EOL> else : <EOL> where = reparam ( where , svars ) <EOL> return where <EOL> def select ( self , tables , svars = None , what = '<STR_LIT:*>' , where = None , order = None , group = None , <EOL> limit = None , offset = None , _test = False ) : <EOL> """<STR_LIT>""" <EOL> if svars is None : svars = { } <EOL> sql_clauses = self . sql_clauses ( what , tables , where , group , order , limit , offset ) <EOL> clauses = [ self . gen_clause ( sql , val , svars ) for sql , val in sql_clauses if val is not None ] <EOL> qout = SQLQuery . join ( clauses ) <EOL> if _test : return qout <EOL> return self . query ( qout , processed = True ) <EOL> def insert ( self , tablename , seqname = None , _test = False , ** values ) : <EOL> """<STR_LIT>""" <EOL> def q ( x ) : return "<STR_LIT:(>" + x + "<STR_LIT:)>" <EOL> if values : <EOL> _keys = SQLQuery . join ( values . keys ( ) , '<STR_LIT:U+002CU+0020>' ) <EOL> _values = SQLQuery . join ( [ sqlparam ( v ) for v in values . values ( ) ] , '<STR_LIT:U+002CU+0020>' ) <EOL> sql_query = "<STR_LIT>" % tablename + q ( _keys ) + '<STR_LIT>' + q ( _values ) <EOL> else : <EOL> sql_query = SQLQuery ( self . _get_insert_default_values_query ( tablename ) ) <EOL> return sql_query <EOL> def _get_insert_default_values_query ( self , table ) : <EOL> return "<STR_LIT>" % table <EOL> def multiple_insert ( self , tablename , values , seqname = None , _test = False ) : <EOL> """<STR_LIT>""" <EOL> if not values : <EOL> return [ ] <EOL> if not self . supports_multiple_insert : <EOL> out = [ self . insert ( tablename , seqname = seqname , _test = _test , ** v ) for v in values ] <EOL> if seqname is False : <EOL> return None <EOL> else : <EOL> return out <EOL> keys = values [ <NUM_LIT:0> ] . keys ( ) <EOL> for v in values : <EOL> if v . keys ( ) != keys : <EOL> raise ValueError , '<STR_LIT>' <EOL> sql_query = SQLQuery ( '<STR_LIT>' % ( tablename , '<STR_LIT:U+002CU+0020>' . join ( keys ) ) ) <EOL> for i , row in enumerate ( values ) : <EOL> if i != <NUM_LIT:0> : <EOL> sql_query . append ( "<STR_LIT:U+002CU+0020>" ) <EOL> SQLQuery . join ( [ SQLParam ( row [ k ] ) for k in keys ] , sep = "<STR_LIT:U+002CU+0020>" , target = sql_query , prefix = "<STR_LIT:(>" , suffix = "<STR_LIT:)>" ) <EOL> if _test : return sql_query <EOL> db_cursor = self . _db_cursor ( ) <EOL> if seqname is not False : <EOL> sql_query = self . _process_insert_query ( sql_query , tablename , seqname ) <EOL> if isinstance ( sql_query , tuple ) : <EOL> q1 , q2 = sql_query <EOL> self . _db_execute ( db_cursor , q1 ) <EOL> self . _db_execute ( db_cursor , q2 ) <EOL> else : <EOL> self . _db_execute ( db_cursor , sql_query ) <EOL> try : <EOL> out = db_cursor . fetchone ( ) [ <NUM_LIT:0> ] <EOL> out = range ( out - len ( values ) + <NUM_LIT:1> , out + <NUM_LIT:1> ) <EOL> except Exception : <EOL> out = None <EOL> if not self . ctx . transactions : <EOL> self . ctx . commit ( ) <EOL> return out <EOL> def update ( self , tables , where , svars = None , _test = False , ** values ) : <EOL> """<STR_LIT>""" <EOL> if svars is None : svars = { } <EOL> where = self . _where ( where , svars ) <EOL> query = ( <EOL> "<STR_LIT>" + sqllist ( tables ) + <EOL> "<STR_LIT>" + sqlwhere ( values , '<STR_LIT:U+002CU+0020>' ) + <EOL> "<STR_LIT>" + where ) <EOL> if _test : return query <EOL> db_cursor = self . _db_cursor ( ) <EOL> self . _db_execute ( db_cursor , query ) <EOL> if not self . ctx . transactions : <EOL> self . ctx . commit ( ) <EOL> return db_cursor . rowcount <EOL> def delete ( self , table , where , using = None , svars = None , _test = False ) : <EOL> """<STR_LIT>""" <EOL> if svars is None : <EOL> svars = { } <EOL> where = self . _where ( where , svars ) <EOL> q = '<STR_LIT>' + table <EOL> if using : <EOL> q += '<STR_LIT>' + sqllist ( using ) <EOL> if where : <EOL> q += '<STR_LIT>' + where <EOL> return q <EOL> sqlproducer = SQLProducer ( ) <EOL> print sqlproducer . delete ( "<STR_LIT>" , where = "<STR_LIT>" ) </s>
