<s> import sqlite3 <EOL> import logging <EOL> DBFILE = "<STR_LIT>" <EOL> class Bardb ( ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . con = False <EOL> def _connect_to_db ( self ) : <EOL> """<STR_LIT>""" <EOL> self . con = sqlite3 . connect ( DBFILE ) <EOL> self . con . row_factory = self . _dictFactory <EOL> def _disconnect_from_db ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . con : <EOL> self . con . close ( ) <EOL> self . con = False <EOL> def _dictFactory ( self , cursor , row ) : <EOL> """<STR_LIT>""" <EOL> d = { } <EOL> for idx , col in enumerate ( cursor . description ) : <EOL> d [ col [ <NUM_LIT:0> ] ] = row [ idx ] <EOL> return d <EOL> def get_or_create ( self , table , get_where_dict , operator = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> entries = self . select_entries ( table , get_where_dict , operator ) <EOL> if len ( entries ) == <NUM_LIT:0> : <EOL> self . insert_entry ( table , get_where_dict ) <EOL> return self . select_entries ( table , get_where_dict , operator ) [ <NUM_LIT:0> ] <EOL> def update_entries ( self , table , where_dict , set_dict , operator = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> self . _connect_to_db ( ) <EOL> with self . con : <EOL> cur = self . con . cursor ( ) <EOL> first = True <EOL> for key , value in set_dict . iteritems ( ) : <EOL> if first : <EOL> set_part = "<STR_LIT>" % ( key , value ) <EOL> first = False <EOL> else : <EOL> set_part = set_part + "<STR_LIT>" % ( key , value ) <EOL> first = True <EOL> for key , value in where_dict . iteritems ( ) : <EOL> if first : <EOL> where_part = "<STR_LIT>" % ( key , value ) <EOL> first = False <EOL> else : <EOL> where_part = where_part + "<STR_LIT>" % ( operator , key , value ) <EOL> query = "<STR_LIT>" % ( table , set_part , where_part ) <EOL> cur . execute ( query ) <EOL> self . _disconnect_from_db ( ) <EOL> def insert_entry ( self , table , update_dict ) : <EOL> """<STR_LIT>""" <EOL> self . _connect_to_db ( ) <EOL> with self . con : <EOL> cur = self . con . cursor ( ) <EOL> first = True <EOL> for key , value in update_dict . iteritems ( ) : <EOL> if first : <EOL> updatefield_part = "<STR_LIT:%s>" % ( key ) <EOL> setfield_part = "<STR_LIT>" % ( value ) <EOL> first = False <EOL> else : <EOL> updatefield_part = updatefield_part + "<STR_LIT>" % ( key ) <EOL> setfield_part = setfield_part + "<STR_LIT>" % ( value ) <EOL> query = "<STR_LIT>" % ( table , updatefield_part , setfield_part ) <EOL> cur . execute ( query ) <EOL> self . _disconnect_from_db ( ) <EOL> def select_entries ( self , table , where_dict = { "<STR_LIT>" : "<STR_LIT:1>" } , operator = "<STR_LIT>" , order_field = "<STR_LIT:id>" , order = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> self . _connect_to_db ( ) <EOL> with self . con : <EOL> cur = self . con . cursor ( ) <EOL> first = True <EOL> for key , value in where_dict . iteritems ( ) : <EOL> if first : <EOL> where_part = "<STR_LIT>" % ( key , value ) <EOL> first = False <EOL> else : <EOL> where_part = where_part + "<STR_LIT>" % ( operator , key , value ) <EOL> query = "<STR_LIT>" % ( table , where_part , order_field , order ) <EOL> cur . execute ( query ) <EOL> rows = cur . fetchall ( ) <EOL> self . _disconnect_from_db ( ) <EOL> return rows <EOL> def delete_entries ( self , table , where_dict = { "<STR_LIT>" : "<STR_LIT:1>" } ) : <EOL> """<STR_LIT>""" <EOL> self . _connect_to_db ( ) <EOL> with self . con : <EOL> cur = self . con . cursor ( ) <EOL> first = True <EOL> for key , value in where_dict . iteritems ( ) : <EOL> if first : <EOL> where_part = "<STR_LIT>" % ( key , value ) <EOL> first = False <EOL> else : <EOL> where_part = where_part + "<STR_LIT>" % ( key , value ) <EOL> query = "<STR_LIT>" % ( table , where_part ) <EOL> cur . execute ( query ) <EOL> self . _disconnect_from_db ( ) </s>
