]> git.ipfire.org Git - people/ms/ipfire-3.x.git/blob - pkgs/core/pakfire/src/python/db.py
Remove legacy build system.
[people/ms/ipfire-3.x.git] / pkgs / core / pakfire / src / python / db.py
1
2
3 from pysqlite2 import dbapi2 as sqlite
4
5 DATABASE_PATH = "."
6
7 class Database(object):
8 def __init__(self, filename):
9 self.filename = filename
10
11 self.connection = sqlite.connect(self.filename)
12
13 def add(self, table):
14 c = self.cursor
15 c.executescript("CREATE TABLE IF NOT EXISTS %s(id, key, value);" % table)
16 c.close()
17
18 def commit(self):
19 self.connection.commit()
20
21 def destroy(self, table):
22 c = self.cursor
23 c.execute("DELETE FROM %s" % table)
24 c.close()
25 self.commit()
26
27 def get(self, table, id, key):
28 ret = None
29 c = self.cursor
30 c.execute("SELECT value FROM %s WHERE id='%s' AND key='%s';" % \
31 (table, id, key,))
32 try:
33 ret = c.fetchone()[0]
34 except TypeError:
35 pass
36 c.close()
37 return ret
38
39 def set(self, table, id, key, value):
40 c = self.cursor
41 if not self.get(id, key):
42 c.execute("INSERT INTO %s(id, key, value) VALUES('%s', '%s', '%s');" \
43 % (table, id, key, value,))
44 else:
45 c.execute("UPDATE %s SET value='%s' WHERE id='%s' AND key='%s';" \
46 % (table, value, id, key,))
47 c.close()
48 self.commit()
49
50 @property
51 def cursor(self):
52 return self.connection.cursor()