new feature added(returning single element) to db service and some minor bug fixes

This commit is contained in:
Volkan Şahin 2016-03-23 16:39:39 +02:00
parent 9790371289
commit e501afad33

View file

@ -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('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('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('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): def connect(self):
@ -75,20 +76,23 @@ class AhenkDbService(object):
def findByProperty(self): def findByProperty(self):
# Not implemented yet # Not implemented yet
pass pass
def select(self,table_name, cols="*", criteria="", orderby=""): def select(self,table_name, cols="*", criteria="", orderby=""):
print("seleeeeeeect")
if self.cursor: if self.cursor:
try: try:
if not cols == "*": if not cols == "*":
cols = ', '.join([str(x) for x in cols]) cols = ', '.join([str(x) for x in cols])
sql = "SELECT "+cols+" FROM " + table_name sql = "SELECT "+cols+" FROM " + table_name
if criteria != "": if criteria != "":
sql+=' where ' sql += ' where '
sql+=criteria sql += criteria
if orderby != "": if orderby != "":
sql+=' order by ' sql += ' order by '
sql+=orderby sql += orderby
self.cursor.execute(sql) self.cursor.execute(sql)
rows = self.cursor.fetchall() rows = self.cursor.fetchall()
return rows return rows
except Exception as e: except Exception as e:
@ -96,6 +100,22 @@ class AhenkDbService(object):
else: else:
self.logger.warning("Could not select table cursor is None! Table Name : " + str(table_name)) 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): def close(self):
try: try:
self.cursor.close() self.cursor.close()