Abstract table entry creation.
authorCMDR furrycat <elite@furrycat.net>
Sun, 23 Oct 2016 11:39:15 +0000 (12:39 +0100)
committerCMDR furrycat <elite@furrycat.net>
Sun, 23 Oct 2016 11:39:15 +0000 (12:39 +0100)
db.py

diff --git a/db.py b/db.py
index 7c07d7a..3572a47 100644 (file)
--- a/db.py
+++ b/db.py
@@ -128,6 +128,9 @@ class DBConnection(object):
     self.close_db()
 
   def create_announcement(self, client, **args):
+    return self.insert_into_table(client, 'announcements', **args)
+
+  def insert_into_table(self, client, table, **args):
     id = self.uuid()
 
     values = []
@@ -135,13 +138,13 @@ class DBConnection(object):
     for k, v in args.items():
       values.append(k)
       params.append(v)
-    sql = 'insert into announcements (id, bot_id, {}) values (?, ?, {})'.format(', '.join(values), ', '.join(['?'] * len(values)))
+    sql = 'insert into {} (id, bot_id, {}) values (?, ?, {})'.format(table, ', '.join(values), ', '.join(['?'] * len(values)))
     cursor = self.query(sql, params)
     if cursor.rowcount:
       self.dbh.commit()
       ret = id
     else:
-      log.error('Failed to create announcement {}: {}'.format(id, args))
+      log.error('Failed to create entry {} in table {}: {}'.format(id, table, args))
       self.dbh.rollback()
       ret = None
     self.close_db()