~abentley/mailtyrant/trunk

« back to all changes in this revision

Viewing changes to mailclient/db_cache.py

  • Committer: Aaron Bentley
  • Date: 2009-05-24 08:31:50 UTC
  • Revision ID: aaron@aaronbentley.com-20090524083150-s4w8dfa7rrtna7k6
Tweak dbcache and its tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
 
8
8
    def __init__(self, connection):
9
9
        self.connection = connection
 
10
        self.reset_cache()
 
11
 
 
12
    @classmethod
 
13
    def from_path(klass, path):
 
14
        connection = sqlite3.connect(path,
 
15
            detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
 
16
        return klass(connection)
 
17
 
 
18
    def reset_cache(self):
10
19
        c = self.connection.cursor()
11
20
        try:
12
21
            c.execute('''drop table mailcache''')
13
 
        except sqlite3.OperationalError:
14
 
            pass
 
22
        except sqlite3.OperationalError, e:
 
23
            if e[0] != 'no such table: mailcache':
 
24
                raise
15
25
        c.execute('''create table mailcache (key text, date timestamp,
16
26
                     'from' text, subject text)''')
17
27
 
27
37
        c.execute('select key, date as "date [timestamp]", "from", subject'
28
38
            ' from mailcache where key in (?)', keys)
29
39
        result = c.fetchall()
30
 
        return result
 
40
        return [(x[0], x[1:]) for x in result]
31
41
 
32
42
    def set(self, key, values):
33
43
        c = self.connection.cursor()
34
44
        c.execute('insert into mailcache values (?, ?, ?, ?)',
35
45
                  (key,) + values)
36
46
 
 
47
    def save(self):
 
48
        self.connection.commit()
 
49
 
37
50
 
38
51
def get_cache():
39
 
    return Cache(sqlite3.connect('cache.sqlite', detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES))
 
52
    return Cache.from_path('cache.sqlite')