<s> from __future__ import division <EOL> import os <EOL> import sqlite3 <EOL> class DatabaseAccessSingleton ( object ) : <EOL> def __new__ ( cls , * args , ** kw ) : <EOL> if DatabaseAccess . _instance is None : <EOL> obj = DatabaseAccess ( * args , ** kw ) <EOL> DatabaseAccess . _instance = obj <EOL> return DatabaseAccess . _instance <EOL> class DatabaseAccess ( object ) : <EOL> _instance = None <EOL> def __init__ ( self , db_file ) : <EOL> self . db_file = db_file <EOL> run_schema = False <EOL> if not os . path . exists ( db_file ) : <EOL> run_schema = True <EOL> self . conn = sqlite3 . connect ( db_file ) <EOL> if run_schema : <EOL> cur = self . conn . cursor ( ) <EOL> self . run_sql_file ( cur , os . path . join ( os . path . dirname ( <EOL> os . path . abspath ( __file__ ) ) , '<STR_LIT>' ) ) <EOL> self . ensure_deleted_column_exist ( ) <EOL> self . model = None <EOL> def ensure_deleted_column_exist ( self ) : <EOL> """<STR_LIT>""" <EOL> cur = self . conn . cursor ( ) <EOL> if '<STR_LIT>' not in [ i [ <NUM_LIT:1> ] for i in cur . execute ( <EOL> "<STR_LIT>" ) . fetchall ( ) ] : <EOL> cur . execute ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . conn . commit ( ) <EOL> def set_model ( self , model ) : <EOL> self . model = model <EOL> def run_sql_file ( self , cur , sql_file ) : <EOL> cmd = '<STR_LIT>' <EOL> for line in open ( sql_file ) : <EOL> cmd += line <EOL> if line . strip ( ) . endswith ( '<STR_LIT:;>' ) : <EOL> cur . execute ( cmd . strip ( ) ) <EOL> cmd = '<STR_LIT>' <EOL> def finalize ( self ) : <EOL> self . conn . close ( ) <EOL> def write_database ( self , value_dict ) : <EOL> cur = self . conn . cursor ( ) <EOL> cols , vals = zip ( * value_dict . iteritems ( ) ) <EOL> col_str = '<STR_LIT:U+002CU+0020>' . join ( cols ) <EOL> cur . execute ( "<STR_LIT>" % ( col_str , '<STR_LIT:U+002C>' . join ( [ '<STR_LIT:?>' ] * len ( vals ) ) ) , <EOL> vals ) <EOL> self . conn . commit ( ) <EOL> if self . model : <EOL> self . model . add_data ( value_dict ) <EOL> def read_database ( self , cols = None , where_dict = None ) : <EOL> cur = self . conn . cursor ( ) <EOL> if cols is None : <EOL> cols = [ "<STR_LIT:id>" , "<STR_LIT:name>" , "<STR_LIT>" , "<STR_LIT:user>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:version>" , "<STR_LIT>" ] <EOL> col_str = '<STR_LIT:U+002CU+0020>' . join ( cols ) <EOL> if where_dict is None or len ( where_dict ) <= <NUM_LIT:0> : <EOL> cur . execute ( "<STR_LIT>" + "<STR_LIT:U+002CU+0020>" . join ( cols ) + <EOL> "<STR_LIT>" ) <EOL> else : <EOL> where_cols , where_vals = zip ( * where_dict . iteritems ( ) ) <EOL> where_str = '<STR_LIT>' . join ( where_cols ) + '<STR_LIT>' <EOL> cur . execute ( "<STR_LIT>" + "<STR_LIT:U+002CU+0020>" . join ( cols ) + "<STR_LIT>" <EOL> "<STR_LIT>" % where_str , where_vals ) <EOL> return cur . fetchall ( ) <EOL> def search_by_signature ( self , signature ) : <EOL> cur = self . conn . cursor ( ) <EOL> cur . execute ( "<STR_LIT>" <EOL> "<STR_LIT>" , ( signature , ) ) <EOL> res = cur . fetchone ( ) <EOL> if res : <EOL> return res <EOL> return None <EOL> def get_signature ( self , id , version = None ) : <EOL> cur = self . conn . cursor ( ) <EOL> cur . execute ( "<STR_LIT>" , <EOL> ( id , version ) ) <EOL> res = cur . fetchone ( ) <EOL> if res : <EOL> return res [ <NUM_LIT:0> ] <EOL> return None <EOL> def ref_exists ( self , id , version = None ) : <EOL> cur = self . conn . cursor ( ) <EOL> if version is None : <EOL> cur . execute ( "<STR_LIT>" <EOL> "<STR_LIT>" , ( id , ) ) <EOL> else : <EOL> cur . execute ( "<STR_LIT>" <EOL> "<STR_LIT>" , ( id , version ) ) <EOL> res = cur . fetchone ( ) <EOL> return res is not None <EOL> def delete_from_database ( self , where_dict = None ) : <EOL> cur = self . conn . cursor ( ) <EOL> if where_dict is None or len ( where_dict ) <= <NUM_LIT:0> : <EOL> cur . execute ( "<STR_LIT>" ) <EOL> else : <EOL> where_cols , where_vals = zip ( * where_dict . iteritems ( ) ) <EOL> where_str = '<STR_LIT>' . join ( where_cols ) + '<STR_LIT>' <EOL> cur . execute ( "<STR_LIT>" % <EOL> where_str , where_vals ) <EOL> self . conn . commit ( ) <EOL> if self . model : <EOL> self . model . remove_data ( where_dict ) </s>
