summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntanas Uršulis <antanas.ursulis@gmail.com>2013-07-29 21:55:51 +0300
committerAntanas Uršulis <antanas.ursulis@gmail.com>2013-07-29 21:55:51 +0300
commite5bdc5bc099096186e00e61604484fc6a1ecdeb2 (patch)
tree0e31d855e720459ad438216c47898b5080fb8c92 /database.py
parentMore basename() defensiveness (diff)
downloadlog-analysis-e5bdc5bc099096186e00e61604484fc6a1ecdeb2.tar.gz
log-analysis-e5bdc5bc099096186e00e61604484fc6a1ecdeb2.tar.bz2
log-analysis-e5bdc5bc099096186e00e61604484fc6a1ecdeb2.zip
Database (SQL) class for functionality common across all processors
Currently uses MySQLdb. Schema included.
Diffstat (limited to 'database.py')
-rw-r--r--database.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/database.py b/database.py
new file mode 100644
index 0000000..5202876
--- /dev/null
+++ b/database.py
@@ -0,0 +1,22 @@
+from contextlib import closing
+import MySQLdb
+
+class DatabaseConnection:
+ def __init__(self, conn):
+ self.conn = conn
+
+ def insert_file(self, path, group_id):
+ with closing(self.conn.cursor()) as c:
+ c.execute("insert into `files` (`path`, `group_id`) values (%s, %s)", (path, group_id))
+ self.conn.commit()
+ return c.lastrowid
+
+ def insert_group(self, name, provider, date):
+ with closing(self.conn.cursor()) as c:
+ c.execute("insert into `groups` (`name`, `provider`, `date`) values (%s, %s, %s)", (name, provider, date))
+ self.conn.commit()
+ return c.lastrowid
+
+def get_connection(user, passwd, db):
+ conn = MySQLdb.connect(user=user, passwd=passwd, db=db)
+ return DatabaseConnection(conn)