]>
Commit | Line | Data |
---|---|---|
940227cb MT |
1 | #!/usr/bin/python |
2 | ||
3 | import logging | |
4 | import tornado.database | |
5 | ||
6 | from misc import Singleton | |
7 | ||
8 | class Row(tornado.database.Row): | |
9 | pass | |
10 | ||
60024cc8 | 11 | MYSQL_SERVER = "mysql-master.ipfire.org" |
940227cb MT |
12 | |
13 | class Databases(object): | |
14 | __metaclass__ = Singleton | |
15 | ||
16 | def __init__(self): | |
17 | self._connections = {} | |
18 | ||
19 | @property | |
20 | def webapp(self): | |
21 | if not self._connections.has_key("webapp"): | |
22 | self._connections["webapp"] = \ | |
23 | Connection(MYSQL_SERVER, "webapp", user="webapp") | |
24 | ||
25 | return self._connections["webapp"] | |
26 | ||
27 | @property | |
28 | def geoip(self): | |
29 | if not self._connections.has_key("geoip"): | |
30 | self._connections["geoip"] = \ | |
31 | Connection(MYSQL_SERVER, "geoip", user="webapp") | |
32 | ||
33 | return self._connections["geoip"] | |
34 | ||
35 | @property | |
36 | def tracker(self): | |
37 | if not self._connections.has_key("tracker"): | |
38 | self._connections["tracker"] = \ | |
39 | Connection(MYSQL_SERVER, "tracker", user="webapp") | |
40 | ||
41 | return self._connections["tracker"] | |
42 | ||
43 | ||
44 | class Connection(tornado.database.Connection): | |
45 | def __init__(self, *args, **kwargs): | |
46 | logging.debug("Creating new database connection: %s" % args[1]) | |
47 | ||
48 | tornado.database.Connection.__init__(self, *args, **kwargs) | |
49 | ||
50 | def update(self, table, item_id, **items): | |
51 | query = "UPDATE %s SET " % table | |
52 | ||
53 | keys = [] | |
54 | for k, v in items.items(): | |
55 | # Never update id | |
56 | if k == "id": | |
57 | continue | |
58 | ||
59 | keys.append("%s='%s'" % (k, v)) | |
60 | ||
61 | query += ", ".join(keys) | |
62 | query += " WHERE id=%s" % item_id | |
63 | ||
64 | return self.execute(query) | |
65 | ||
66 | def insert(self, table, **items): | |
67 | query = "INSERT INTO %s" % table | |
68 | ||
69 | keys = [] | |
70 | vals = [] | |
71 | ||
72 | for k, v in items.items(): | |
73 | # Never insert id | |
74 | if k == "id": | |
75 | continue | |
76 | ||
77 | keys.append(k) | |
78 | vals.append("'%s'" % v) | |
79 | ||
80 | query += "(%s)"% ", ".join(keys) | |
81 | query += " VALUES(%s)" % ", ".join(vals) | |
82 | ||
83 | return self.execute(query) | |
84 | ||
85 | def _execute(self, cursor, query, parameters): | |
86 | logging.debug("Executing query: %s" % (query % parameters)) | |
87 | ||
88 | return tornado.database.Connection._execute(self, cursor, query, parameters) |