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: