update the db stuff to use generic insert and select helpers

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



git-svn-id: http://test.kernel.org/svn/autotest/trunk@655 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/tko/db.py b/tko/db.py
index 054fab7..270f077 100644
--- a/tko/db.py
+++ b/tko/db.py
@@ -6,43 +6,65 @@
 		self.cur = self.con.cursor()
 
 
-	def select(self, cmd):
-		print 'select ' + cmd
-		self.cur.execute('select ' + cmd)
+	def select(self, fields, table, where_dict):
+		"""\
+			select fields from table where {dictionary}
+		"""
+		keys = [field + '=%s' for field in where_dict.keys()]
+		values = [where_dict[field] for field in where_dict.keys()]
+
+		where = 'and'.join(keys)
+		cmd = 'select %s from %s where %s' % (fields, table, where)
+		print cmd
+		print values
+		self.cur.execute(cmd, values)
 		return self.cur.fetchall()
 
 
+	def insert(self, table, data):
+		"""\
+			'insert into table (keys) values (%s ... %s)', values
+
+			data:
+				dictionary of fields and data
+		"""
+		fields = data.keys()
+		refs = ['%s' for field in fields]
+		values = [data[field] for field in fields]
+		cmd = 'insert into %s (%s) values (%s)' % \
+				(table, ','.join(fields), ','.join(refs))
+		print cmd
+		print values
+		self.cur.execute(cmd, values)
+		self.con.commit()
+
+
 	def insert_job(self, tag, job):
 		# is kernel version in tree?
-		command = 'insert into jobs ' + \
-			'(tag, machine) ' + \
-			'values (%s, %s) '
-		print command
-		values = (tag, 'UNKNOWN')
-		self.cur.execute(command, values)
-		self.con.commit()
-		# Select it back from the job table and find the uniq key
+		self.insert('jobs', {'tag':tag, 'machine':'UNKNOWN'})
+		job.index = self.find_job(tag)
+		for test in job.tests:
+			self.insert_test(job, test)
+
+	def insert_test(self, job, test):
+		# WE ARE MISSING KVERSION HERE!!!!
+		data = {'job_idx':job.index, 'test':test.testname,
+			'subdir':test.dir, 
+			'status':test.status, 'reason':test.reason}
+		self.insert('tests', data)
 
 
-	def insert_test(self, job, 
-	def lookup_kernel_version(base, patches):
-		command = 'select kversion from kversions where base = %s'
+	def lookup_kernel_version(self, base, patches):
+		return self.select('kversion', 'kversions', {'base':base})[0]
 
 
-	def insert_kernel_version(base, patches):
-		base = re.sub(r'\+.*', '', printable)
-		command = 'select kversion from kversions where printable = %s'
-		self.cur.execute(command, tag)
-		results = self.cur.fetchall()
-		if results:
-			return results[0]
-		command = 'insert into kversions (printable, base) ' + \
-			  'values (%s, %s)'
-		self.cur.execute(command, (printable, base))
-		self.con.commit()
+	def insert_kernel_version(self, base, patches):
+		self.insert('kversions', {'base': base})
 
 
 	def find_job(self, tag):
-		command = 'select * from jobs where tag = %s'
-		self.cur.execute(command, tag)
-		return self.cur.fetchall()
+		rows = self.select('job_idx', 'jobs', {'tag': tag})
+		if rows:
+			return rows[0][0]
+		else:
+			return None