<s> """<STR_LIT>""" <EOL> import copy <EOL> import sqlite3 <EOL> import sys <EOL> import threading <EOL> from . util import sqlite_escape <EOL> encoding_type = '<STR_LIT:utf8>' <EOL> def u ( r ) : <EOL> def f ( x ) : <EOL> if sys . version < '<STR_LIT:3>' and isinstance ( x , unicode ) : <EOL> return x . encode ( encoding_type ) <EOL> return x <EOL> return list ( map ( f , r ) ) <EOL> class Field ( object ) : <EOL> name = '<STR_LIT>' <EOL> @ property <EOL> def sql ( self ) : <EOL> pass <EOL> class IntegerField ( Field ) : <EOL> @ property <EOL> def sql ( self ) : <EOL> return '<STR_LIT>' % ( self . name , "<STR_LIT>" ) <EOL> class CharField ( Field ) : <EOL> def __init__ ( self , max_length = <NUM_LIT:255> ) : <EOL> self . max_length = max_length <EOL> @ property <EOL> def sql ( self ) : <EOL> return '<STR_LIT>' % ( self . name , "<STR_LIT>" , self . max_length ) <EOL> class TextField ( Field ) : <EOL> @ property <EOL> def sql ( self ) : <EOL> return '<STR_LIT>' % ( self . name , "<STR_LIT>" ) <EOL> class RealField ( Field ) : <EOL> @ property <EOL> def sql ( self ) : <EOL> return '<STR_LIT>' % ( self . name , "<STR_LIT>" ) <EOL> class DateField ( Field ) : <EOL> @ property <EOL> def sql ( self ) : <EOL> return '<STR_LIT>' % ( self . name , "<STR_LIT>" ) <EOL> class PrimaryKeyField ( IntegerField ) : <EOL> @ property <EOL> def sql ( self ) : <EOL> return '<STR_LIT>' % ( self . name , "<STR_LIT>" ) <EOL> class ForeignKeyField ( IntegerField ) : <EOL> def __init__ ( self , to_table ) : <EOL> self . to_table = to_table <EOL> @ property <EOL> def sql ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . name , '<STR_LIT>' , self . to_table , '<STR_LIT:id>' <EOL> ) <EOL> class ForeignKeyReverseField ( object ) : <EOL> def __init__ ( self , from_table ) : <EOL> self . from_table = from_table <EOL> self . name = None <EOL> self . tablename = None <EOL> self . id = None <EOL> self . db = None <EOL> self . from_class = None <EOL> self . re = None <EOL> def update ( self , name , tablename , db ) : <EOL> self . name = name <EOL> self . tablename = tablename <EOL> self . db = db <EOL> self . from_class = self . db . __tabledict__ [ self . from_table ] <EOL> for name , attr in self . from_class . __dict__ . items ( ) : <EOL> if isinstance ( attr , ForeignKeyField ) and attr . to_table == self . tablename : <EOL> self . re = name <EOL> def all ( self ) : <EOL> return self . from_class . select ( '<STR_LIT:*>' ) . where ( '<STR_LIT:=>' . join ( [ self . re , str ( self . id ) ] ) ) . all ( ) <EOL> def count ( self ) : <EOL> return self . from_class . select ( '<STR_LIT:*>' ) . where ( '<STR_LIT:=>' . join ( [ self . re , str ( self . id ) ] ) ) . count ( ) <EOL> class ManyToManyField ( object ) : <EOL> def __init__ ( self , rel = None , to_table = None ) : <EOL> if not rel or not to_table : <EOL> raise DatabaseException ( <EOL> "<STR_LIT>" ) <EOL> self . rel = rel <EOL> self . to_table = to_table <EOL> self . id = None <EOL> self . self_id = None <EOL> self . db = None <EOL> self . name = None <EOL> def update ( self , name , tablename , db ) : <EOL> self . name = name <EOL> self . tablename = tablename <EOL> self . db = db <EOL> def _get_rel_names ( self ) : <EOL> rel_class = self . db . __tabledict__ [ self . rel ] <EOL> self_id = None <EOL> rel_id = None <EOL> for name , field in rel_class . __fields__ . items ( ) : <EOL> if isinstance ( field , ForeignKeyField ) : <EOL> if field . to_table == self . tablename : <EOL> self_id = name <EOL> elif field . to_table == self . to_table : <EOL> rel_id = name <EOL> if not self_id or not rel_id : <EOL> raise DatabaseException ( "<STR_LIT>" ) <EOL> return self_id , rel_id <EOL> def _select ( self ) : <EOL> self_id , rel_id = self . _get_rel_names ( ) <EOL> c = self . db . execute ( <EOL> '<STR_LIT>' % ( <EOL> rel_id , self . rel , self_id , self . id ) <EOL> ) <EOL> rs = c . fetchall ( ) <EOL> if len ( rs ) == <NUM_LIT:0> : <EOL> return self . db . __tabledict__ [ self . to_table ] . select ( ) . where ( '<STR_LIT>' ) <EOL> return self . db . __tabledict__ [ self . to_table ] . select ( ) . where ( <EOL> '<STR_LIT>' . join ( [ '<STR_LIT>' % c [ <NUM_LIT:0> ] for c in rs ] ) <EOL> ) <EOL> def all ( self ) : <EOL> return self . _select ( ) . all ( ) <EOL> def append ( self , ins ) : <EOL> """<STR_LIT>""" <EOL> self_id , rel_id = self . _get_rel_names ( ) <EOL> c = self . db . execute ( <EOL> '<STR_LIT>' % ( <EOL> self . rel , self_id , rel_id , self . id , ins . id <EOL> ) , commit = True <EOL> ) <EOL> return c <EOL> def remove ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> if not self . self_id : <EOL> self . self_id , _ = self . _get_rel_names ( ) <EOL> c = self . db . execute ( <EOL> '<STR_LIT>' % ( <EOL> self . rel , self . self_id , self . id , <EOL> '<STR_LIT>' . join ( list ( args ) ) <EOL> ) , commit = True <EOL> ) <EOL> return c <EOL> def count ( self ) : <EOL> return self . _select ( ) . count ( ) <EOL> class MetaModel ( type ) : <EOL> def __new__ ( cls , name , bases , attrs ) : <EOL> cls = super ( MetaModel , cls ) . __new__ ( cls , name , bases , attrs ) <EOL> fields = { } <EOL> refed_fields = { } <EOL> cls_dict = cls . __dict__ <EOL> if '<STR_LIT>' in cls_dict . keys ( ) : <EOL> setattr ( cls , '<STR_LIT>' , cls_dict [ '<STR_LIT>' ] ) <EOL> else : <EOL> setattr ( cls , '<STR_LIT>' , cls . __name__ . lower ( ) ) <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> getattr ( cls , '<STR_LIT>' ) . __tabledict__ [ cls . __tablename__ ] = cls <EOL> has_primary_key = False <EOL> setattr ( cls , '<STR_LIT>' , False ) <EOL> for name , attr in cls . __dict__ . items ( ) : <EOL> if isinstance ( attr , ForeignKeyReverseField ) or isinstance ( attr , ManyToManyField ) : <EOL> setattr ( cls , '<STR_LIT>' , True ) <EOL> attr . update ( name , cls . __tablename__ , cls . db ) <EOL> refed_fields [ name ] = attr <EOL> if isinstance ( attr , Field ) : <EOL> attr . name = name <EOL> fields [ name ] = attr <EOL> if isinstance ( attr , PrimaryKeyField ) : <EOL> has_primary_key = True <EOL> if not has_primary_key : <EOL> pk = PrimaryKeyField ( ) <EOL> pk . name = '<STR_LIT:id>' <EOL> fields [ '<STR_LIT:id>' ] = pk <EOL> setattr ( cls , '<STR_LIT>' , fields ) <EOL> setattr ( cls , '<STR_LIT>' , refed_fields ) <EOL> return cls <EOL> class Model ( MetaModel ( '<STR_LIT>' , ( object , ) , { } ) ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> max_id = self . select ( '<STR_LIT:id>' ) . max ( ) <EOL> if not max_id : <EOL> max_id = <NUM_LIT:0> <EOL> self . id = max_id + <NUM_LIT:1> <EOL> for k , v in self . __refed_fields__ . items ( ) : <EOL> if isinstance ( v , ForeignKeyReverseField ) or isinstance ( v , ManyToManyField ) : <EOL> v . id = self . id <EOL> t = copy . deepcopy ( v ) <EOL> setattr ( t , '<STR_LIT>' , v . db ) <EOL> setattr ( self , k , t ) <EOL> for k , v in kwargs . items ( ) : <EOL> setattr ( self , k , v ) <EOL> @ classmethod <EOL> def get ( cls , * args , ** kwargs ) : <EOL> return SelectQuery ( cls , args ) . where ( ** kwargs ) . first ( ) <EOL> @ classmethod <EOL> def select ( cls , * args ) : <EOL> return SelectQuery ( cls , args ) <EOL> @ classmethod <EOL> def update ( cls , * args , ** kwargs ) : <EOL> return UpdateQuery ( cls , args , ** kwargs ) <EOL> @ classmethod <EOL> def delete ( cls , * args , ** kwargs ) : <EOL> return DeleteQuery ( cls , args , ** kwargs ) <EOL> class Database ( threading . local ) : <EOL> pass <EOL> class DatabaseException ( Exception ) : <EOL> pass <EOL> class Sqlite ( Database ) : <EOL> def __init__ ( self , database ) : <EOL> self . database = database <EOL> self . conn = sqlite3 . connect ( <EOL> self . database , detect_types = sqlite3 . PARSE_DECLTYPES | sqlite3 . PARSE_COLNAMES ) <EOL> self . __tabledict__ = { } <EOL> self . __db__ = self <EOL> setattr ( self , '<STR_LIT>' , Model ) <EOL> c = getattr ( self , '<STR_LIT>' ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> setattr ( c , '<STR_LIT>' , getattr ( self , '<STR_LIT>' ) ) <EOL> def create_all ( self ) : <EOL> for k , v in self . __tabledict__ . items ( ) : <EOL> if issubclass ( v , self . Model ) : <EOL> print ( "<STR_LIT>" % k ) <EOL> self . create_table ( v ) <EOL> def create_table ( self , model ) : <EOL> c = [ ] <EOL> for field in model . __fields__ . values ( ) : <EOL> c . append ( field . sql ) <EOL> cursor = self . conn . cursor ( ) <EOL> cursor . execute ( <EOL> '<STR_LIT>' % ( model . __tablename__ , '<STR_LIT:U+002CU+0020>' . join ( c ) ) <EOL> ) <EOL> if not model . __tablename__ in self . __tabledict__ . keys ( ) : <EOL> self . __tabledict__ [ model . __tablename__ ] = model <EOL> self . commit ( ) <EOL> def drop_table ( self , model ) : <EOL> cursor = self . conn . cursor ( ) <EOL> cursor . execute ( '<STR_LIT>' % model . __tablename__ ) <EOL> del self . __tabledict__ [ model . __tablename__ ] <EOL> self . commit ( ) <EOL> def add ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> cofk = [ ] <EOL> cofv = [ ] <EOL> for k , v in instance . __dict__ . items ( ) : <EOL> if isinstance ( v , ManyToManyField ) or isinstance ( v , ForeignKeyReverseField ) : <EOL> continue <EOL> cofk . append ( k ) <EOL> cofv . append ( '<STR_LIT>' + sqlite_escape ( str ( v ) ) + '<STR_LIT>' ) <EOL> cursor = self . conn . cursor ( ) <EOL> cursor . execute ( <EOL> '<STR_LIT>' % ( instance . __class__ . __tablename__ , <EOL> '<STR_LIT:U+002CU+0020>' . join ( cofk ) , '<STR_LIT:U+002CU+0020>' . join ( cofv ) ) <EOL> ) <EOL> return cursor <EOL> def commit ( self ) : <EOL> self . conn . commit ( ) <EOL> def rollback ( self ) : <EOL> self . conn . rollback ( ) <EOL> def close ( self ) : <EOL> self . conn . close ( ) <EOL> def execute ( self , sql , commit = False ) : <EOL> cursor = self . conn . cursor ( ) <EOL> cursor . execute ( sql ) <EOL> if commit : <EOL> self . commit ( ) <EOL> return cursor <EOL> class BaseQuery ( object ) : <EOL> base_statement = '<STR_LIT>' <EOL> @ property <EOL> def sql ( self ) : <EOL> pass <EOL> class QueryException ( DatabaseException ) : <EOL> pass <EOL> class SelectQuery ( BaseQuery ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , klass , * args ) : <EOL> self . base_statement = '<STR_LIT>' <EOL> self . klass = klass <EOL> self . query = [ '<STR_LIT:*>' ] <EOL> if args != ( ( ) , ) : <EOL> self . query = list ( * args ) <EOL> self . like_pattern = None <EOL> @ property <EOL> def sql ( self ) : <EOL> if self . like_pattern : <EOL> return self . base_statement % ( <EOL> '<STR_LIT:U+002CU+0020>' . join ( [ str ( i ) . strip ( '<STR_LIT>' ) . strip ( '<STR_LIT:">' ) <EOL> for i in self . query ] ) . strip ( '<STR_LIT:U+002C>' ) , self . klass . __tablename__ , self . like_pattern <EOL> ) <EOL> return self . base_statement % ( <EOL> '<STR_LIT:U+002CU+0020>' . join ( [ str ( i ) . strip ( '<STR_LIT>' ) . strip ( '<STR_LIT:">' ) <EOL> for i in self . query ] ) . strip ( '<STR_LIT:U+002C>' ) , self . klass . __tablename__ <EOL> ) <EOL> def _make_instance ( self , descriptor , r ) : <EOL> try : <EOL> ins = self . klass ( ** dict ( zip ( descriptor , r ) ) ) <EOL> except TypeError : <EOL> return None <EOL> for _ , rf in ins . __dict__ . items ( ) : <EOL> if isinstance ( rf , ManyToManyField ) or isinstance ( rf , ForeignKeyReverseField ) : <EOL> rf . id = ins . id <EOL> return ins <EOL> def all ( self ) : <EOL> c = self . klass . db . execute ( self . sql ) <EOL> descriptor = list ( i [ <NUM_LIT:0> ] for i in c . description ) <EOL> rs = c . fetchall ( ) <EOL> ins_list = [ self . _make_instance ( descriptor , u ( r ) ) for r in rs ] <EOL> return ins_list <EOL> def first ( self ) : <EOL> c = self . klass . db . execute ( self . sql ) <EOL> rs = c . fetchone ( ) <EOL> if not rs : <EOL> return None <EOL> return self . _make_instance ( list ( i [ <NUM_LIT:0> ] for i in c . description ) , u ( rs ) ) <EOL> def where ( self , * args , ** kwargs ) : <EOL> c = [ '<STR_LIT>' % ( k , str ( v ) ) for k , v in kwargs . items ( ) ] <EOL> if c != ( ( ) , ) : <EOL> c . extend ( list ( args ) ) <EOL> self . base_statement = "<STR_LIT>" + '<STR_LIT>' . join ( c ) + "<STR_LIT:;>" <EOL> return self <EOL> def _base_function ( self , func ) : <EOL> sql = self . base_statement % ( <EOL> func + '<STR_LIT>' + '<STR_LIT:U+002CU+0020>' . join ( [ str ( i ) . strip ( '<STR_LIT>' ) . strip ( '<STR_LIT:">' ) <EOL> for i in self . query ] ) . strip ( '<STR_LIT:U+002C>' ) + '<STR_LIT>' , self . klass . __tablename__ <EOL> ) <EOL> c = self . klass . db . execute ( sql ) <EOL> rs = c . fetchone ( ) <EOL> return rs [ <NUM_LIT:0> ] <EOL> def count ( self ) : <EOL> return self . _base_function ( '<STR_LIT:count>' ) <EOL> def max ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _base_function ( '<STR_LIT>' ) <EOL> def min ( self ) : <EOL> return self . _base_function ( '<STR_LIT>' ) <EOL> def avg ( self ) : <EOL> return self . _base_function ( '<STR_LIT>' ) <EOL> def sum ( self ) : <EOL> return self . _base_function ( '<STR_LIT>' ) <EOL> def orderby ( self , order = None , by = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . base_statement = '<STR_LIT:U+0020>' . join ( <EOL> [ self . base_statement . strip ( '<STR_LIT:;>' ) , '<STR_LIT>' , order , by , '<STR_LIT:;>' ] ) <EOL> return self <EOL> def like ( self , pattern ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' not in self . base_statement : <EOL> raise QueryException ( "<STR_LIT>" ) <EOL> self . like_pattern = pattern or '<STR_LIT>' <EOL> self . base_statement = '<STR_LIT>' . join ( <EOL> [ self . base_statement . strip ( '<STR_LIT:;>' ) , '<STR_LIT>' ] ) <EOL> return self <EOL> def notlike ( self , pattern ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' not in self . base_statement : <EOL> raise QueryException ( "<STR_LIT>" ) <EOL> self . like_pattern = pattern or '<STR_LIT>' <EOL> self . base_statement = '<STR_LIT>' . join ( <EOL> [ self . base_statement . strip ( '<STR_LIT:;>' ) , '<STR_LIT>' ] ) <EOL> return self <EOL> class UpdateQuery ( BaseQuery ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , klass , * args , ** kwargs ) : <EOL> self . klass = klass <EOL> self . base_statement = "<STR_LIT>" <EOL> self . where = None <EOL> self . update_value = None <EOL> if args != ( ( ) , ) or kwargs : <EOL> c = [ '<STR_LIT>' % ( k , str ( v ) ) for k , v in kwargs . items ( ) ] <EOL> if args != ( ( ) , ) : <EOL> c . extend ( list ( * args ) ) <EOL> self . where = "<STR_LIT>" + '<STR_LIT>' . join ( c ) <EOL> @ property <EOL> def sql ( self ) : <EOL> if self . where : <EOL> self . base_statement = '<STR_LIT:U+0020>' . join ( <EOL> [ '<STR_LIT>' , self . where , '<STR_LIT:;>' ] ) <EOL> return self . base_statement % ( self . klass . __tablename__ , self . update_value ) <EOL> def set ( self , * args , ** kwargs ) : <EOL> self . update_value = '<STR_LIT>' . join ( <EOL> [ '<STR_LIT>' % ( k , v ) for k , v in kwargs . items ( ) ] ) <EOL> return self <EOL> def commit ( self ) : <EOL> c = self . klass . db . execute ( self . sql , commit = True ) <EOL> return c <EOL> class DeleteQuery ( BaseQuery ) : <EOL> def __init__ ( self , klass , * args , ** kwargs ) : <EOL> self . base_statement = '<STR_LIT>' <EOL> self . c = None <EOL> if args != ( ( ) , ) or kwargs : <EOL> self . base_statement = '<STR_LIT>' <EOL> self . c = [ '<STR_LIT>' % ( k , str ( v ) ) for k , v in kwargs . items ( ) ] <EOL> if args != ( ( ) , ) : <EOL> self . c . extend ( list ( * args ) ) <EOL> self . klass = klass <EOL> @ property <EOL> def sql ( self ) : <EOL> if self . c : <EOL> return self . base_statement % ( self . klass . __tablename__ , '<STR_LIT>' . join ( self . c ) ) <EOL> return self . base_statement % self . klass . __tablename__ <EOL> def commit ( self ) : <EOL> c = self . klass . db . execute ( self . sql , commit = True ) <EOL> return c </s>
