Extensions to SQL abstractions

Signed-off-by: Martin J. Bligh <mbligh@google.com>

Add more generalized select statement, allow where to be a
string as instead of a dictionary, add an update function.



git-svn-id: http://test.kernel.org/svn/autotest/trunk@755 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/tko/db.py b/tko/db.py
index acc909d..a696c3a 100644
--- a/tko/db.py
+++ b/tko/db.py
@@ -57,17 +57,29 @@
 		cmd += [fields, 'from', table]
 
 		values = []
-		if where:
+		if where and isinstance(where, types.DictionaryType):
 			keys = [field + '=%s' for field in where.keys()]
 			values = [where[field] for field in where.keys()]
 
 			cmd.append(' where ' + ' and '.join(keys))
+		elif where and isinstance(where, types.StringTypes):
+			cmd.append(' where ' + where)
 
 		self.dprint('%s %s' % (' '.join(cmd),values))
 		self.cur.execute(' '.join(cmd), values)
 		return self.cur.fetchall()
 
 
+	def select_sql(self, fields, table, sql, values):
+		"""\
+			select fields from table "sql"
+		"""
+		cmd = 'select %s from %s %s' % (fields, table, sql)
+		self.dprint(cmd)
+		self.cur.execute(cmd, values)
+		return self.cur.fetchall()
+
+
 	def insert(self, table, data):
 		"""\
 			'insert into table (keys) values (%s ... %s)', values
@@ -86,6 +98,28 @@
 		self.con.commit()
 
 
+	def update(self, table, data, where):
+		"""\
+			'update table set data values (%s ... %s) where ...'
+
+			data:
+				dictionary of fields and data
+		"""
+		cmd = 'update %s ' % table
+		fields = data.keys()
+		data_refs = [field + '=%s' for field in fields]
+		data_values = [data[field] for field in fields]
+		cmd += ' set ' + ' and '.join(data_refs)
+
+		where_keys = [field + '=%s' for field in where.keys()]
+		where_values = [where[field] for field in where.keys()]
+		cmd += ' where ' + ' and '.join(where_keys)
+
+		print '%s %s' % (cmd, data_values + where_values)
+		self.cur.execute(cmd, data_values + where_values)
+		self.con.commit()
+
+
 	def insert_job(self, tag, job):
 		job.machine_idx = self.lookup_machine(job.machine)
 		if not job.machine_idx:
diff --git a/tko/frontend.py b/tko/frontend.py
index c48c5a1..7ffe8d2 100755
--- a/tko/frontend.py
+++ b/tko/frontend.py
@@ -64,6 +64,15 @@
 		return tests
 
 
+	@classmethod
+	def select_sql(klass, db, sql, values):
+		fields = ['test_idx', 'job_idx', 'test', 'subdir', 
+			  'kernel_idx', 'status', 'reason', 'machine_idx']
+		fields = ['t.'+field for field in fields]
+		rows = db.select_sql(','.join(fields), 'tests', sql, values)
+		return [klass(db, *row) for row in rows]
+
+		
 	def __init__(self, db, test_idx, job_idx, testname, subdir, kernel_idx, status_num, reason, machine_idx):
 		self.idx = test_idx
 		self.job = job(db, job_idx)