1 # $Id: back_sqlite.py,v 1.5 2002-09-24 01:59:28 richard Exp $
2 __doc__ = '''
3 See https://pysqlite.sourceforge.net/ for pysqlite info
4 '''
5 import base64, marshal
6 from roundup.backends.rdbms_common import *
7 import sqlite
9 class Database(Database):
10 # char to use for positional arguments
11 arg = '%s'
13 def open_connection(self):
14 # ensure files are group readable and writable
15 os.umask(0002)
16 db = os.path.join(self.config.DATABASE, 'db')
17 self.conn = sqlite.connect(db=db)
18 self.cursor = self.conn.cursor()
19 try:
20 self.database_schema = self.load_dbschema()
21 except sqlite.DatabaseError, error:
22 if str(error) != 'no such table: schema':
23 raise
24 self.database_schema = {}
25 self.cursor.execute('create table schema (schema varchar)')
26 self.cursor.execute('create table ids (name varchar, num integer)')
28 def __repr__(self):
29 return '<roundlite 0x%x>'%id(self)
31 def sql_fetchone(self):
32 ''' Fetch a single row. If there's nothing to fetch, return None.
33 '''
34 return self.cursor.fetchone()
36 def sql_fetchall(self):
37 ''' Fetch a single row. If there's nothing to fetch, return [].
38 '''
39 return self.cursor.fetchall()
41 def sql_commit(self):
42 ''' Actually commit to the database.
44 Ignore errors if there's nothing to commit.
45 '''
46 try:
47 self.conn.commit()
48 except sqlite.DatabaseError, error:
49 if str(error) != 'cannot commit - no transaction is active':
50 raise
52 def save_dbschema(self, schema):
53 ''' Save the schema definition that the database currently implements
54 '''
55 s = repr(self.database_schema)
56 self.sql('insert into schema values (%s)', (s,))
58 def load_dbschema(self):
59 ''' Load the schema definition that the database currently implements
60 '''
61 self.cursor.execute('select schema from schema')
62 return eval(self.cursor.fetchone()[0])
64 def save_journal(self, classname, cols, nodeid, journaldate,
65 journaltag, action, params):
66 ''' Save the journal entry to the database
67 '''
68 # make the params db-friendly
69 params = repr(params)
70 entry = (nodeid, journaldate, journaltag, action, params)
72 # do the insert
73 a = self.arg
74 sql = 'insert into %s__journal (%s) values (%s,%s,%s,%s,%s)'%(classname,
75 cols, a, a, a, a, a)
76 if __debug__:
77 print >>hyperdb.DEBUG, 'addjournal', (self, sql, entry)
78 self.cursor.execute(sql, entry)
80 def load_journal(self, classname, cols, nodeid):
81 ''' Load the journal from the database
82 '''
83 # now get the journal entries
84 sql = 'select %s from %s__journal where nodeid=%s'%(cols, classname,
85 self.arg)
86 if __debug__:
87 print >>hyperdb.DEBUG, 'getjournal', (self, sql, nodeid)
88 self.cursor.execute(sql, (nodeid,))
89 res = []
90 for nodeid, date_stamp, user, action, params in self.cursor.fetchall():
91 params = eval(params)
92 res.append((nodeid, date.Date(date_stamp), user, action, params))
93 return res
95 def unserialise(self, classname, node):
96 ''' Decode the marshalled node data
98 SQLite stringifies _everything_... so we need to re-numberificate
99 Booleans and Numbers.
100 '''
101 if __debug__:
102 print >>hyperdb.DEBUG, 'unserialise', classname, node
103 properties = self.getclass(classname).getprops()
104 d = {}
105 for k, v in node.items():
106 # if the property doesn't exist, or is the "retired" flag then
107 # it won't be in the properties dict
108 if not properties.has_key(k):
109 d[k] = v
110 continue
112 # get the property spec
113 prop = properties[k]
115 if isinstance(prop, Date) and v is not None:
116 d[k] = date.Date(v)
117 elif isinstance(prop, Interval) and v is not None:
118 d[k] = date.Interval(v)
119 elif isinstance(prop, Password):
120 p = password.Password()
121 p.unpack(v)
122 d[k] = p
123 elif isinstance(prop, Boolean) and v is not None:
124 d[k] = int(v)
125 elif isinstance(prop, Number) and v is not None:
126 # try int first, then assume it's a float
127 try:
128 d[k] = int(v)
129 except ValueError:
130 d[k] = float(v)
131 else:
132 d[k] = v
133 return d