def testBFormatting(self): """ Test various formats """ myThread = threading.currentThread() dbformatter = DBFormatter(myThread.logger, myThread.dbi) myThread.transaction.begin() result = myThread.transaction.processData(myThread.select) output = dbformatter.format(result) self.assertEqual(output , [['value1a', 'value2a'], \ ['value1b', 'value2b'], ['value1c', 'value2d']]) result = myThread.transaction.processData(myThread.select) output = dbformatter.formatOne(result) print('test1 '+str(output)) self.assertEqual( output , ['value1a', 'value2a'] ) result = myThread.transaction.processData(myThread.select) output = dbformatter.formatDict(result) self.assertEqual( output , [{'bind2': 'value2a', 'bind1': 'value1a'}, \ {'bind2': 'value2b', 'bind1': 'value1b'},\ {'bind2': 'value2d', 'bind1': 'value1c'}] ) result = myThread.transaction.processData(myThread.select) output = dbformatter.formatOneDict(result) self.assertEqual( output, {'bind2': 'value2a', 'bind1': 'value1a'} )
def testBFormatting(self): """ Test various formats """ myThread = threading.currentThread() dbformatter = DBFormatter(myThread.logger, myThread.dbi) myThread.transaction.begin() result = myThread.transaction.processData(myThread.select) output = dbformatter.format(result) self.assertEqual(output , [['value1a', 'value2a'], \ ['value1b', 'value2b'], ['value1c', 'value2d']]) result = myThread.transaction.processData(myThread.select) output = dbformatter.formatOne(result) print('test1 ' + str(output)) self.assertEqual(output, ['value1a', 'value2a']) result = myThread.transaction.processData(myThread.select) output = dbformatter.formatDict(result) self.assertEqual( output , [{'bind2': 'value2a', 'bind1': 'value1a'}, \ {'bind2': 'value2b', 'bind1': 'value1b'},\ {'bind2': 'value2d', 'bind1': 'value1c'}] ) result = myThread.transaction.processData(myThread.select) output = dbformatter.formatOneDict(result) self.assertEqual(output, {'bind2': 'value2a', 'bind1': 'value1a'})
def testBFormatting(self): """ Test various formats """ myThread = threading.currentThread() dbformatter = DBFormatter(myThread.logger, myThread.dbi) myThread.transaction.begin() result = myThread.transaction.processData(myThread.select) output = dbformatter.format(result) self.assertEqual(output, [["value1a", "value2a"], ["value1b", "value2b"], ["value1c", "value2d"]]) result = myThread.transaction.processData(myThread.select) output = dbformatter.formatOne(result) print("test1 " + str(output)) self.assertEqual(output, ["value1a", "value2a"]) result = myThread.transaction.processData(myThread.select) output = dbformatter.formatDict(result) self.assertEqual( output, [ {"bind2": "value2a", "bind1": "value1a"}, {"bind2": "value2b", "bind1": "value1b"}, {"bind2": "value2d", "bind1": "value1c"}, ], ) result = myThread.transaction.processData(myThread.select) output = dbformatter.formatOneDict(result) self.assertEqual(output, {"bind2": "value2a", "bind1": "value1a"})
def selectOne(self, query): """ execute a query.with only one result expected """ # db connect self.session.connect() # execute query results = self.session.processData(query) if (results.rowcount > 0): formatter = DBFormatter(self.logger, self.session) out = formatter.formatOne(results) else: out = None return out
def selectOne(self, query): """ execute a query.with only one result expected """ # db connect self.session.connect() # execute query results = self.session.processData(query) if (results.rowcount > 0): formatter = DBFormatter(self.logger, self.session) out = formatter.formatOne(results) else : out = None return out
def testBFormatting(self): """ Test various formats """ # fill the database with some initial data self.stuffDB() myThread = threading.currentThread() dbformatter = DBFormatter(myThread.logger, myThread.dbi) result = myThread.dbi.processData(self.selectSQL) output = dbformatter.format(result) self.assertEqual(output, [['value1a', 1, 'value2a'], ['value1b', 2, 'value2b'], ['value1c', 3, 'value2d']]) result = myThread.dbi.processData(self.selectSQL) output = dbformatter.formatOne(result) print('test1 ' + str(output)) self.assertEqual(output, ['value1a', 1, 'value2a']) result = myThread.dbi.processData(self.selectSQL) output = dbformatter.formatDict(result) self.assertEqual(output, [{ 'column3': 'value2a', 'column2': 1, 'column1': 'value1a' }, { 'column3': 'value2b', 'column2': 2, 'column1': 'value1b' }, { 'column3': 'value2d', 'column2': 3, 'column1': 'value1c' }]) result = myThread.dbi.processData(self.selectSQL) output = dbformatter.formatOneDict(result) self.assertEqual(output, { 'column3': 'value2a', 'column2': 1, 'column1': 'value1a' })
class MySQLBase(object): def __init__(self, logger, dbinterface): self.logger = logger self.dbi = dbinterface self.wmformatter = DBFormatter(self.logger, self.dbi) def truefalse(self, value): if value in ('False', 'FALSE', 'n', 'NO', 'No'): value = 0 return bool(value) def convertdatetime(self, t): return int(time.mktime(t.timetuple())) def timestamp(self): """ generate a timestamp """ t = datetime.datetime.now() return self.convertdatetime(t) def format(self, result, dictionary = False): """ Some standard formatting """ if not dictionary: return self.wmformatter.format(result) else: return self.wmformatter.formatDict(result) # out = [] # for r in result: # if dictionary == False: # for i in r.cursor.fetchall(): # out.append(i) # else: # # for i in r.cursor.fetchall(): # row = RowProxy(r,i) # out.append(dict(row.items())) # # # return out def formatOne(self, result, dictionary = False): """ single value format """ if not dictionary: return self.wmformatter.formatOne(result) else: return self.wmformatter.formatOneDict(result) # if len(result) == 0: # return [] # value = result[0].fetchone() # if value == None: # return [] # # if dictionary == True: # row = RowProxy(result[0],value) # value = dict(row.items()) # return value def getBinds(self): """ Return the appropriately formatted binds for the sql """ return {} def execute(self, conn = None, transaction = False): """ A simple select with no binds/arguments is the default """ result = self.dbi.processData(self.sql, self.getBinds(), conn = conn, transaction = transaction) return self.format(result) def restoreCaps(self, dicts, *newkeys): """ Utility function to restore capitalization in dictionary keys lost to conform with Oracle - use of this should be phased out """ for dict in dicts: for key in newkeys: dict[key] = dict.pop(key.lower()) return dicts