<s> from sqlalchemy import Column , Table , select <EOL> from sqlalchemy . types import Integer , PickleType , DateTime , Unicode <EOL> from webtools . database import Base <EOL> from webtools . utils import timezone <EOL> from . base import BaseSessionEngine <EOL> session = Table ( "<STR_LIT>" , Base . metadata , <EOL> Column ( "<STR_LIT:id>" , Integer , primary_key = True , autoincrement = False ) , <EOL> Column ( "<STR_LIT>" , DateTime ( timezone = True ) , index = True ) , <EOL> Column ( "<STR_LIT:key>" , Unicode ( length = <NUM_LIT:100> ) , unique = True , index = True ) , <EOL> Column ( "<STR_LIT:data>" , PickleType , unique = True ) , <EOL> ) <EOL> class DatabaseEngine ( BaseSessionEngine ) : <EOL> @ property <EOL> def db ( self ) : <EOL> return self . _application . db <EOL> def load ( self ) : <EOL> if self . _current_session_key is None : <EOL> self . _current_session_key = self . random_session_key ( ) <EOL> sql = session . insert ( ) . values ( <EOL> last_modify = timezone . now ( ) , <EOL> key = self . _current_session_key , <EOL> data = { } <EOL> ) <EOL> self . db . execute ( sql ) <EOL> self . _session_data = { } <EOL> self . _modified = False <EOL> else : <EOL> sql = select ( [ session . c . key , session . c . data ] ) . where ( session . c . key == self . _current_session_key ) <EOL> res_proxy = self . db . execute ( sql ) . first ( ) <EOL> if res_proxy : <EOL> self . _session_data = res_proxy [ '<STR_LIT:data>' ] <EOL> self . _modified = False <EOL> def save ( self ) : <EOL> sql = session . update ( ) . where ( session . c . key == self . _current_session_key ) . values ( data = self . _session_data ) <EOL> self . db . execute ( sql ) <EOL> self . db . commit ( ) <EOL> self . _modified = False <EOL> def delete ( self ) : <EOL> if self . _current_session_key is None : <EOL> return <EOL> self . db . execute ( session . delete ( ) . where ( session . c . key == self . _current_session_key ) ) <EOL> self . db . commit ( ) <EOL> self . _current_session_key = None <EOL> self . _session_data = None </s>
