comparison roundup/backends/back_sqlite.py @ 3728:b476fef16ccc

fixed support for pysqlite2 (version 2.1.0 is the minimum version supported)
author Richard Jones <richard@users.sourceforge.net>
date Tue, 10 Oct 2006 03:55:31 +0000
parents 0d561b24ceff
children c92687dce135
comparison
equal deleted inserted replaced
3727:04dee2ac29e2 3728:b476fef16ccc
1 # $Id: back_sqlite.py,v 1.47 2006-10-04 01:12:00 richard Exp $ 1 # $Id: back_sqlite.py,v 1.48 2006-10-10 03:55:31 richard Exp $
2 '''Implements a backend for SQLite. 2 '''Implements a backend for SQLite.
3 3
4 See https://pysqlite.sourceforge.net/ for pysqlite info 4 See https://pysqlite.sourceforge.net/ for pysqlite info
5 5
6 6
11 11
12 import os, base64, marshal, shutil, time, logging 12 import os, base64, marshal, shutil, time, logging
13 13
14 from roundup import hyperdb, date, password 14 from roundup import hyperdb, date, password
15 from roundup.backends import rdbms_common 15 from roundup.backends import rdbms_common
16 is_sqlite3 = False 16 sqlite_version = None
17 try: 17 try:
18 import sqlite 18 import sqlite
19 sqlite_version = 1
19 except ImportError: 20 except ImportError:
20 try: 21 try:
21 from pysqlite2 import dbapi2 as sqlite 22 from pysqlite2 import dbapi2 as sqlite
23 sqlite_version = 2
22 except ImportError: 24 except ImportError:
23 import sqlite3 as sqlite 25 import sqlite3 as sqlite
24 is_sqlite3 = True 26 sqlite_version = 3
25 27
26 def db_exists(config): 28 def db_exists(config):
27 return os.path.exists(os.path.join(config.DATABASE, 'db')) 29 return os.path.exists(os.path.join(config.DATABASE, 'db'))
28 30
29 def db_nuke(config): 31 def db_nuke(config):
30 shutil.rmtree(config.DATABASE) 32 shutil.rmtree(config.DATABASE)
31 33
32 class Database(rdbms_common.Database): 34 class Database(rdbms_common.Database):
33 # char to use for positional arguments 35 # char to use for positional arguments
34 if is_sqlite3: 36 if sqlite_version in (2,3):
35 arg = '?' 37 arg = '?'
36 else: 38 else:
37 arg = '%s' 39 arg = '%s'
38 40
39 # used by some code to switch styles of query 41 # used by some code to switch styles of query
96 98
97 db = os.path.join(self.config.DATABASE, 'db') 99 db = os.path.join(self.config.DATABASE, 'db')
98 logging.getLogger('hyperdb').info('open database %r'%db) 100 logging.getLogger('hyperdb').info('open database %r'%db)
99 # set a 30 second timeout (extraordinarily generous) for handling 101 # set a 30 second timeout (extraordinarily generous) for handling
100 # locked database 102 # locked database
101 if is_sqlite3: 103 if sqlite_version == 1:
102 conn = sqlite.connect(db, 30)
103 conn.row_factory = sqlite.Row
104 else:
105 conn = sqlite.connect(db=db) 104 conn = sqlite.connect(db=db)
106 conn.db.sqlite_busy_handler(self.sqlite_busy_handler) 105 conn.db.sqlite_busy_handler(self.sqlite_busy_handler)
106 else:
107 conn = sqlite.connect(db, timeout=30)
108 conn.row_factory = sqlite.Row
107 cursor = conn.cursor() 109 cursor = conn.cursor()
108 return (conn, cursor) 110 return (conn, cursor)
109 111
110 def open_connection(self): 112 def open_connection(self):
111 # ensure files are group readable and writable 113 # ensure files are group readable and writable
262 d = [] 264 d = []
263 for name in inscols: 265 for name in inscols:
264 # generate the new value for the Interval int column 266 # generate the new value for the Interval int column
265 if name.endswith('_int__'): 267 if name.endswith('_int__'):
266 name = name[2:-6] 268 name = name[2:-6]
267 if is_sqlite3: 269 if sqlite_version in (2,3):
268 try: 270 try:
269 v = hyperdb.Interval(entry[name]).as_seconds() 271 v = hyperdb.Interval(entry[name]).as_seconds()
270 except IndexError: 272 except IndexError:
271 v = None 273 v = None
272 elif entry.has_key(name): 274 elif entry.has_key(name):
273 v = hyperdb.Interval(entry[name]).as_seconds() 275 v = hyperdb.Interval(entry[name]).as_seconds()
274 else: 276 else:
275 v = None 277 v = None
276 elif is_sqlite3: 278 elif sqlite_version in (2,3):
277 try: 279 try:
278 v = entry[name] 280 v = entry[name]
279 except IndexError: 281 except IndexError:
280 v = None 282 v = None
281 elif (not is_sqlite3 and entry.has_key(name)): 283 elif (sqlite_version == 1 and entry.has_key(name)):
282 v = entry[name] 284 v = entry[name]
283 else: 285 else:
284 v = None 286 v = None
285 d.append(v) 287 d.append(v)
286 self.sql(sql, tuple(d)) 288 self.sql(sql, tuple(d))
366 rdbms_common.Database.create_class(self, spec) 368 rdbms_common.Database.create_class(self, spec)
367 sql = 'insert into ids (name, num) values (%s, %s)'%(self.arg, self.arg) 369 sql = 'insert into ids (name, num) values (%s, %s)'%(self.arg, self.arg)
368 vals = (spec.classname, 1) 370 vals = (spec.classname, 1)
369 self.sql(sql, vals) 371 self.sql(sql, vals)
370 372
371 if is_sqlite3: 373 if sqlite_version in (2,3):
372 def load_journal(self, classname, cols, nodeid): 374 def load_journal(self, classname, cols, nodeid):
373 '''We need to turn the sqlite3.Row into a tuple so it can be 375 '''We need to turn the sqlite3.Row into a tuple so it can be
374 unpacked''' 376 unpacked'''
375 l = rdbms_common.Database.load_journal(self, 377 l = rdbms_common.Database.load_journal(self,
376 classname, cols, nodeid) 378 classname, cols, nodeid)

Roundup Issue Tracker: http://roundup-tracker.org/