From e501afad33c9a679afa0bf51440f9867a3c51f4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Wed, 23 Mar 2016 16:39:39 +0200 Subject: [PATCH] new feature added(returning single element) to db service and some minor bug fixes --- opt/ahenk/base/database/AhenkDbService.py | 28 +++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/opt/ahenk/base/database/AhenkDbService.py b/opt/ahenk/base/database/AhenkDbService.py index de134d2..63201d4 100644 --- a/opt/ahenk/base/database/AhenkDbService.py +++ b/opt/ahenk/base/database/AhenkDbService.py @@ -22,6 +22,7 @@ class AhenkDbService(object): self.check_and_create_table('policy',['id INTEGER PRIMARY KEY AUTOINCREMENT','type TEXT','version TEXT','name TEXT']) self.check_and_create_table('profile',['id INTEGER','create_date TEXT','label TEXT','description TEXT','overridable INTEGER','active INTEGER','deleted INTEGER','profile_data BLOB','modify_date TEXT']) self.check_and_create_table('plugin',['version TEXT','name TEXT','description TEXT']) + self.check_and_create_table('registration', ['jid TEXT', 'password TEXT', 'registered INTEGER', 'dn TEXT', 'params TEXT', 'timestamp TEXT']) def connect(self): @@ -75,20 +76,23 @@ class AhenkDbService(object): def findByProperty(self): # Not implemented yet pass + def select(self,table_name, cols="*", criteria="", orderby=""): + print("seleeeeeeect") if self.cursor: try: if not cols == "*": cols = ', '.join([str(x) for x in cols]) sql = "SELECT "+cols+" FROM " + table_name if criteria != "": - sql+=' where ' - sql+=criteria + sql += ' where ' + sql += criteria if orderby != "": - sql+=' order by ' - sql+=orderby + sql += ' order by ' + sql += orderby self.cursor.execute(sql) + rows = self.cursor.fetchall() return rows except Exception as e: @@ -96,6 +100,22 @@ class AhenkDbService(object): else: self.logger.warning("Could not select table cursor is None! Table Name : " + str(table_name)) + def select_one_result(self, table_name, col, criteria=''): + if self.cursor: + try: + sql = 'SELECT ' + col + ' FROM ' + table_name + if criteria != '': + sql += ' where ' + sql += criteria + self.cursor.execute(sql) + row = self.cursor.fetchone() + if row is not None: + return row[0] + except Exception as e: + raise + else: + self.logger.warning('Could not select table cursor is None! Table Name : ' + str(table_name)) + def close(self): try: self.cursor.close()