Updated Norwegian Bokmål translation
[gpodder.git] / src / gpodder / minidb.py
blob6026c9f592c81df84b1575c8d890fa85038fc270
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
4 # gPodder - A media aggregator and podcast client
5 # Copyright (c) 2005-2010 Thomas Perl and the gPodder Team
7 # gPodder is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # gPodder is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 # gpodder.minidb - A simple SQLite store for Python objects
22 # Thomas Perl, 2010-01-28
24 # based on: "ORM wie eine Kirchenmaus - a very poor ORM implementation
25 # by thp, 2009-11-29 (thpinfo.com/about)"
28 # For Python 2.5, we need to request the "with" statement
29 from __future__ import with_statement
31 try:
32 import sqlite3.dbapi2 as sqlite
33 except ImportError:
34 try:
35 from pysqlite2 import dbapi2 as sqlite
36 except ImportError:
37 raise Exception('Please install SQLite3 support.')
40 import threading
42 class Store(object):
43 def __init__(self, filename=':memory:'):
44 self.db = sqlite.connect(filename, check_same_thread=False)
45 self.lock = threading.RLock()
47 def _schema(self, class_):
48 return class_.__name__, list(sorted(class_.__slots__))
50 def _set(self, o, slot, value):
51 # Set a slot on the given object to value, doing a cast if
52 # necessary. The value None is special-cased and never cast.
53 cls = o.__class__.__slots__[slot]
54 if value is not None:
55 if isinstance(value, unicode):
56 value = value.decode('utf-8')
57 value = cls(value)
58 setattr(o, slot, value)
60 def commit(self):
61 with self.lock:
62 self.db.commit()
64 def close(self):
65 with self.lock:
66 self.db.close()
68 def _register(self, class_):
69 with self.lock:
70 table, slots = self._schema(class_)
71 cur = self.db.execute('PRAGMA table_info(%s)' % table)
72 available = cur.fetchall()
74 if available:
75 available = [row[1] for row in available]
76 missing_slots = (s for s in slots if s not in available)
77 for slot in missing_slots:
78 self.db.execute('ALTER TABLE %s ADD COLUMN %s TEXT' % (table,
79 slot))
80 else:
81 self.db.execute('CREATE TABLE %s (%s)' % (table,
82 ', '.join('%s TEXT'%s for s in slots)))
84 def update(self, o, **kwargs):
85 self.remove(o)
86 for k, v in kwargs.items():
87 setattr(o, k, v)
88 self.save(o)
90 def save(self, o):
91 if hasattr(o, '__iter__'):
92 for child in o:
93 self.save(child)
94 return
96 with self.lock:
97 self._register(o.__class__)
98 table, slots = self._schema(o.__class__)
100 # Only save values that have values set (non-None values)
101 slots = [s for s in slots if getattr(o, s, None) is not None]
103 def convert(v):
104 if isinstance(v, str) or isinstance(v, unicode):
105 return v
106 else:
107 return str(v)
108 values = [convert(getattr(o, slot)) for slot in slots]
109 self.db.execute('INSERT INTO %s (%s) VALUES (%s)' % (table,
110 ', '.join(slots), ', '.join('?'*len(slots))), values)
112 def delete(self, class_, **kwargs):
113 with self.lock:
114 self._register(class_)
115 table, slots = self._schema(class_)
116 sql = 'DELETE FROM %s' % (table,)
117 if kwargs:
118 sql += ' WHERE %s' % (' AND '.join('%s=?' % k for k in kwargs))
119 try:
120 self.db.execute(sql, kwargs.values())
121 return True
122 except Exception, e:
123 return False
125 def remove(self, o):
126 if hasattr(o, '__iter__'):
127 for child in o:
128 self.remove(child)
129 return
131 with self.lock:
132 self._register(o.__class__)
133 table, slots = self._schema(o.__class__)
135 # Use "None" as wildcard selector in remove actions
136 slots = [s for s in slots if getattr(o, s, None) is not None]
138 values = [getattr(o, slot) for slot in slots]
139 self.db.execute('DELETE FROM %s WHERE %s' % (table,
140 ' AND '.join('%s=?'%s for s in slots)), values)
142 def load(self, class_, **kwargs):
143 with self.lock:
144 self._register(class_)
145 table, slots = self._schema(class_)
146 sql = 'SELECT %s FROM %s' % (', '.join(slots), table)
147 if kwargs:
148 sql += ' WHERE %s' % (' AND '.join('%s=?' % k for k in kwargs))
149 try:
150 cur = self.db.execute(sql, kwargs.values())
151 except Exception, e:
152 raise
153 def apply(row):
154 o = class_.__new__(class_)
155 for attr, value in zip(slots, row):
156 try:
157 self._set(o, attr, value)
158 except ValueError, ve:
159 return None
160 return o
161 return filter(lambda x: x is not None, [apply(row) for row in cur])
163 def get(self, class_, **kwargs):
164 result = self.load(class_, **kwargs)
165 if result:
166 return result[0]
167 else:
168 return None
170 if __name__ == '__main__':
171 class Person(object):
172 __slots__ = {'username': str, 'id': int}
174 def __init__(self, username, id):
175 self.username = username
176 self.id = id
178 def __repr__(self):
179 return '<Person "%s" (%d)>' % (self.username, self.id)
181 m = Store()
182 m.save(Person('User %d' % x, x*20) for x in range(50))
184 p = m.get(Person, id=200)
185 print p
186 m.remove(p)
187 p = m.get(Person, id=200)
189 # Remove some persons again (deletion by value!)
190 m.remove(Person('User %d' % x, x*20) for x in range(40))
192 class Person(object):
193 __slots__ = {'username': str, 'id': int, 'mail': str}
195 def __init__(self, username, id, mail):
196 self.username = username
197 self.id = id
198 self.mail = mail
200 def __repr__(self):
201 return '<Person "%s" (%s)>' % (self.username, self.mail)
203 # A schema update takes place here
204 m.save(Person('User %d' % x, x*20, 'user@home.com') for x in range(50))
205 print m.load(Person)