<s> import sqlalchemy as sql <EOL> from sqlalchemy import Column , Integer , String , ForeignKey , Text <EOL> from sqlalchemy . ext . declarative import declarative_base <EOL> from sqlalchemy import create_engine , event <EOL> from sqlalchemy . ext . declarative import declarative_base <EOL> from sqlalchemy . orm import relationship , backref , sessionmaker , session , scoped_session <EOL> from sqlalchemy . engine import Engine <EOL> import json <EOL> from utilities import * <EOL> from dateutil import parser <EOL> import datetime <EOL> import os <EOL> from PySide . QtGui import * <EOL> from PySide . QtCore import * <EOL> Base = declarative_base ( ) <EOL> class Database ( object ) : <EOL> def __init__ ( self , parent ) : <EOL> self . parent = parent <EOL> self . connected = False <EOL> self . filename = "<STR_LIT>" <EOL> @ event . listens_for ( Engine , "<STR_LIT>" ) <EOL> def set_sqlite_pragma ( dbapi_connection , connection_record ) : <EOL> cursor = dbapi_connection . cursor ( ) <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> cursor . close ( ) <EOL> def connect ( self , filename ) : <EOL> try : <EOL> if self . connected : <EOL> self . disconnect ( ) <EOL> self . engine = create_engine ( '<STR_LIT>' % filename , convert_unicode = True ) <EOL> self . session = scoped_session ( sessionmaker ( autocommit = False , autoflush = False , bind = self . engine ) ) <EOL> Base . query = self . session . query_property ( ) <EOL> Base . metadata . create_all ( bind = self . engine ) <EOL> self . filename = filename <EOL> self . connected = True <EOL> except Exception as e : <EOL> self . filename = "<STR_LIT>" <EOL> self . connected = False <EOL> QMessageBox . critical ( self . parent , "<STR_LIT>" , str ( e ) ) <EOL> def disconnect ( self ) : <EOL> if self . connected : <EOL> self . session . close ( ) <EOL> self . filename = "<STR_LIT>" <EOL> self . connected = False <EOL> def createconnect ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> self . disconnect ( ) <EOL> if os . path . isfile ( filename ) : <EOL> os . remove ( filename ) <EOL> self . connect ( filename ) <EOL> def commit ( self ) : <EOL> if self . connected : <EOL> try : <EOL> self . session . commit ( ) <EOL> except Exception as e : <EOL> QMessageBox . critical ( self . parent , "<STR_LIT>" , str ( e ) ) <EOL> else : <EOL> QMessageBox . information ( self . parent , "<STR_LIT>" , "<STR_LIT>" ) <EOL> def rollback ( self ) : <EOL> if self . connected : <EOL> try : <EOL> self . session . rollback ( ) <EOL> except Exception as e : <EOL> QMessageBox . critical ( self . parent , "<STR_LIT>" , str ( e ) ) <EOL> else : <EOL> QMessageBox . information ( self . parent , "<STR_LIT>" , "<STR_LIT>" ) <EOL> class Node ( Base ) : <EOL> """<STR_LIT>""" <EOL> __tablename__ = '<STR_LIT>' <EOL> objectid = Column ( String ) <EOL> objecttype = Column ( String ) <EOL> querystatus = Column ( String ) <EOL> querytype = Column ( String ) <EOL> querytime = Column ( String ) <EOL> queryparams_raw = Column ( "<STR_LIT>" , Text ) <EOL> response_raw = Column ( "<STR_LIT>" , Text ) <EOL> id = Column ( Integer , primary_key = True , index = True ) <EOL> parent_id = Column ( Integer , ForeignKey ( '<STR_LIT>' , ondelete = '<STR_LIT>' ) , index = True ) <EOL> children = relationship ( "<STR_LIT>" , backref = backref ( '<STR_LIT>' , remote_side = '<STR_LIT>' ) ) <EOL> level = Column ( Integer ) <EOL> childcount = Column ( Integer ) <EOL> def __init__ ( self , objectid , parent_id = None ) : <EOL> self . objectid = objectid <EOL> self . parent_id = parent_id <EOL> self . level = <NUM_LIT:0> <EOL> self . childcount = <NUM_LIT:0> <EOL> self . querystatus = '<STR_LIT>' <EOL> self . objecttype = '<STR_LIT>' <EOL> self . sortkey = '<STR_LIT>' <EOL> @ property <EOL> def response ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( self . response_raw == None ) : <EOL> return { } <EOL> else : <EOL> return json . loads ( self . response_raw ) <EOL> @ response . setter <EOL> def response ( self , response_raw ) : <EOL> """<STR_LIT>""" <EOL> self . response_raw = json . dumps ( response_raw ) <EOL> @ property <EOL> def queryparams ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( self . queryparams_raw == None ) : <EOL> return { } <EOL> else : <EOL> return json . loads ( self . queryparams_raw ) <EOL> @ queryparams . setter <EOL> def queryparams ( self , queryparams_raw ) : <EOL> self . queryparams_raw = json . dumps ( queryparams_raw ) <EOL> def getResponseValue ( self , key , encoding = None ) : <EOL> value = getDictValue ( self . response , key ) <EOL> if encoding and isinstance ( value , unicode ) : <EOL> return value . encode ( encoding ) <EOL> else : <EOL> return value <EOL> @ property <EOL> def objectid_encoded ( self ) : <EOL> return self . objectid . encode ( '<STR_LIT:utf-8>' ) </s>
