~lgs/zcadoc/book

« back to all changes in this revision

Viewing changes to zcalib/relationaldatabase.py

  • Committer: Lorenzo Gil Sanchez
  • Date: 2007-11-24 09:57:36 UTC
  • mfrom: (31.1.10 book)
  • Revision ID: lgs@sicem.biz-20071124095736-03lwzlp9uvtvaaud
Merged from Baiju branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
try:
 
3
    import sqlite3
 
4
except ImportError:
 
5
    from pysqlite2 import dbapi2 as sqlite3
 
6
 
 
7
from zope.interface import implements
 
8
 
 
9
from interfaces import IRelationalDatabase
 
10
 
 
11
class RelationalDatabase(object):
 
12
 
 
13
    implements(IRelationalDatabase)
 
14
 
 
15
    def __init__(self):
 
16
        curdir = os.path.abspath(os.path.dirname(__file__))
 
17
        db_file = os.path.join(curdir, 'Data.db')
 
18
        self.conn = sqlite3.connect(db_file)
 
19
        self.conn.row_factory = sqlite3.Row
 
20
 
 
21
    def commit(self):
 
22
        self.conn.commit()
 
23
 
 
24
    def rollback(self):
 
25
        self.conn.rollback()
 
26
 
 
27
    def cursor(self):
 
28
        return self.conn.cursor()
 
29
 
 
30
    def get_next_id(self, table):
 
31
        cr = self.cursor()
 
32
        cr.execute("""SELECT max(id) as max_id FROM %s""" % table)
 
33
        record = cr.fetchone()
 
34
        cr.close()
 
35
        if record['max_id']:
 
36
            return record['max_id'] + 1
 
37
        else:
 
38
            return 1