class GlobalHouseholdCharacteristicsManager: def __init__(self): self.database = Database() def getGlobalHouseholdCharacteristic(self, charid=0, charname=""): char = GlobalHouseholdCharacteristic(charid, charname) return char def addGlobalHouseholdCharacteristic(self, charname, datatype): char = GlobalHouseholdCharacteristic(0, charname, datatype) return char def editGlobalHouseholdCharacteristic(self, charid, charname, datatype): char = GlobalHouseholdCharacteristic(charid) char.setData(charname, datatype) def delGlobalHouseholdCharacteristic(self, charid="", charname=""): query = "DELETE FROM globalhouseholdcharacteristics WHERE id=%i OR characteristic='%s' " % (charid, charname) self.database.open() self.database.execUpdateQuery(query) self.database.close() def getGlobalHouseholdCharacteristics(self): query = "SELECT id FROM globalhouseholdcharacteristics" self.database.open() rows = self.database.execSelectQuery(query) self.database.close() chars = [] for row in rows: charid = row[0] char = GlobalHouseholdCharacteristic(charid) chars.append(char) return chars
def logIhmTransfer(self, pid, pid_access, projectname, startdate, currency ): query = '''INSERT INTO transferlog(pid,pid_access,projectname,datecollected,currency) VALUES(%s,%s,'%s','%s','%s')''' % (pid, pid_access, projectname, startdate, currency) database = Database() database.open() database.execUpdateQuery( query ) database.close()
def test_execDefinitionQuery(self): self.helper.setup_clean_db() database = Database() database.open() database.execDefinitionQuery('create table simples (test int)') database.close() # and just to prove it's there to put something into. database.open() database.execUpdateQuery('insert into simples values (3)') database.close()
def test_execUpdateQuery(self): self.helper.setup_clean_db() database = Database() database.open() database.execUpdateQuery(""" insert into projects (projectname, startdate, enddate, description, currency) values ('test', '2012-06-04', '2013-07-03', 'a simple test', 'GBP')""") query = 'select * from projects' self.assertEqual([(2, u'test', datetime.date(2012, 6, 4), datetime.date(2013, 7, 3), u'a simple test', u'GBP')], database.execSelectQuery(query)) database.close()
def delCorrespondingIhmProject(self, projectname, startdate, currency): ''' Delete if the project was transfered before ''' query = '''SELECT projects.pid FROM projects, transferlog WHERE projects.pid=transferlog.pid AND transferlog.projectname='%s' AND transferlog.datecollected='%s' AND transferlog.currency='%s' ''' % (projectname, startdate, currency) db = Database() db.open() records = db.execSelectQuery( query ) for record in records: pid = record[0] query = '''DELETE FROM projects WHERE pid=%s''' % pid db.execUpdateQuery( query ) db.close()
def importIhmProjectData(self, project, filename): dbfile = file(filename, 'r') contents = dbfile.read() dbfile.close() queries = contents.split('<endl>\n') database = Database() database.open() for index in range(len(queries)): if index != 0: # ignore the first row containing project (already created above) query = queries[index].strip() if ( query.startswith("INSERT") ): query = query.replace("{pid}", str(project.pid) ) print str(index) + " " + query database.execUpdateQuery( query ) database.close()
def deleteProject(self, pid): query = "DELETE FROM projects WHERE pid=%s" % pid database = Database() database.open() database.execUpdateQuery( query ) database.close()
class CurrencyManager( object ): """Manages currencies. Allows adding, editing, deleting and retrieval of currencies. """ def __init__( self ) : self.database = Database() self.currencies = None self.currency_names = None self.getCurrencies() def existsCurrency(self, name): """Check if currency 'name' exists """ currency = Currency(currencyname=name) if not currency.name : return False else: return True def getCurrencyByID(self, currencyid): """return currency currencyid from the database (returns by currency id) """ currency = Currency(currencyid) if not currency.name : raise CurrencyError( "Can't get Currency id %r which does not exist" % currencyid ) return currency def getCurrencyByName(self, name): """return currency currencyname from the database (returns by currency name) """ currency = Currency(currencyname=name) if not currency.name : raise CurrencyError( "Can't get Currency '%r' which does not exist" % name ) return currency def addCurrency(self, currencyname, abbreviation, symbol ): """Adds currency 'currencyname' to the currencies database. """ currency = Currency( 0 , currencyname, abbreviation, symbol ) self.getCurrencies() # update currency list return currency def editCurrency(self, currencyid, currencyname, abbreviation, symbol ): """Edit currency 'currencyid' providing new name, abbreviation, symbol. """ currency = Currency(currencyid) if not currency.name : raise CurrencyError( "Can't find Currency id %r which does not exist" % currencyid ) currency.editData( currencyname, abbreviation, symbol ) self.getCurrencies() # update currency list return def delCurrency(self, currencyid): """Delete currency 'currencyid' from the database. """ currency = Currency(currencyid) if not currency.name : raise CurrencyError( "Can't delete Currency id %r which does not exist" %currencyid ) query = "DELETE FROM currencies WHERE id=%i " % ( currencyid ) self.database.open() self.database.execUpdateQuery( query ) self.database.close() self.getCurrencies() # update currency list return def getCurrencies(self): """Return a list of currencies stored in the database At same time set self.currencies to this list. """ query = "SELECT id FROM currencies" self.database.open() rows = self.database.execSelectQuery( query ) self.database.close() self.currencies = list() for row in rows: curr_id = row[0] currency = Currency(curr_id) self.currencies.append( currency ) self.currency_names = [ x.name for x in self.currencies ] return self.currencies
def delGlobalPersonCharacteristic(self, charid="", charname=""): query = "DELETE FROM globalpersonalcharacteristics WHERE id=%i OR characteristic='%s' " % ( charid, charname ) database = Database() database.open() database.execUpdateQuery( query ) database.close()
def delGlobalCharacteristic(self, charname=""): database = Database() query = "DELETE FROM globalcharacteristics WHERE characteristic='%s' " % ( charname ) database.open() database.execUpdateQuery( query ) database.close()