Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / lib / sqlalchemy / dbwrapper.py @ 96090728

History | View | Annotate | Download (3 kB)

1 2e662088 Antony Chazapis
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 2715ade4 Sofia Papagiannaki
#
3 4f917833 Sofia Papagiannaki
# Redistribution and use in source and binary forms, with or
4 4f917833 Sofia Papagiannaki
# without modification, are permitted provided that the following
5 4f917833 Sofia Papagiannaki
# conditions are met:
6 2715ade4 Sofia Papagiannaki
#
7 4f917833 Sofia Papagiannaki
#   1. Redistributions of source code must retain the above
8 4f917833 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
9 4f917833 Sofia Papagiannaki
#      disclaimer.
10 2715ade4 Sofia Papagiannaki
#
11 4f917833 Sofia Papagiannaki
#   2. Redistributions in binary form must reproduce the above
12 4f917833 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
13 4f917833 Sofia Papagiannaki
#      disclaimer in the documentation and/or other materials
14 4f917833 Sofia Papagiannaki
#      provided with the distribution.
15 2715ade4 Sofia Papagiannaki
#
16 4f917833 Sofia Papagiannaki
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 4f917833 Sofia Papagiannaki
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 4f917833 Sofia Papagiannaki
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 4f917833 Sofia Papagiannaki
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 4f917833 Sofia Papagiannaki
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 4f917833 Sofia Papagiannaki
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 4f917833 Sofia Papagiannaki
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 4f917833 Sofia Papagiannaki
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 4f917833 Sofia Papagiannaki
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 4f917833 Sofia Papagiannaki
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 4f917833 Sofia Papagiannaki
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 4f917833 Sofia Papagiannaki
# POSSIBILITY OF SUCH DAMAGE.
28 2715ade4 Sofia Papagiannaki
#
29 4f917833 Sofia Papagiannaki
# The views and conclusions contained in the software and
30 4f917833 Sofia Papagiannaki
# documentation are those of the authors and should not be
31 4f917833 Sofia Papagiannaki
# interpreted as representing official policies, either expressed
32 4f917833 Sofia Papagiannaki
# or implied, of GRNET S.A.
33 4f917833 Sofia Papagiannaki
34 308e0de6 Sofia Papagiannaki
from sqlalchemy import create_engine
35 308e0de6 Sofia Papagiannaki
#from sqlalchemy.event import listen
36 985b9b09 Sofia Papagiannaki
from sqlalchemy.pool import NullPool
37 308e0de6 Sofia Papagiannaki
from sqlalchemy.interfaces import PoolListener
38 4f917833 Sofia Papagiannaki
39 059857e2 Antony Chazapis
40 85a93d09 Antony Chazapis
class DBWrapper(object):
41 85a93d09 Antony Chazapis
    """Database connection wrapper."""
42 2715ade4 Sofia Papagiannaki
43 85a93d09 Antony Chazapis
    def __init__(self, db):
44 a179db1b Sofia Papagiannaki
        if db.startswith('sqlite://'):
45 308e0de6 Sofia Papagiannaki
            class ForeignKeysListener(PoolListener):
46 308e0de6 Sofia Papagiannaki
                def connect(self, dbapi_con, con_record):
47 985b9b09 Sofia Papagiannaki
                    dbapi_con.execute('pragma foreign_keys=ON;')
48 985b9b09 Sofia Papagiannaki
                    dbapi_con.execute('pragma case_sensitive_like=ON;')
49 985b9b09 Sofia Papagiannaki
            self.engine = create_engine(
50 985b9b09 Sofia Papagiannaki
                db, connect_args={'check_same_thread': False},
51 985b9b09 Sofia Papagiannaki
                poolclass=NullPool, listeners=[ForeignKeysListener()],
52 985b9b09 Sofia Papagiannaki
                isolation_level='SERIALIZABLE')
53 413448f1 Sofia Papagiannaki
        #elif db.startswith('mysql://'):
54 413448f1 Sofia Papagiannaki
        #    db = '%s?charset=utf8&use_unicode=0' %db
55 413448f1 Sofia Papagiannaki
        #    self.engine = create_engine(db, convert_unicode=True)
56 a179db1b Sofia Papagiannaki
        else:
57 2715ade4 Sofia Papagiannaki
            #self.engine = create_engine(db, pool_size=0, max_overflow=-1)
58 985b9b09 Sofia Papagiannaki
            self.engine = create_engine(
59 29148653 Sofia Papagiannaki
                db, poolclass=NullPool, isolation_level='READ COMMITTED')
60 7ed99da8 root
        self.engine.echo = False
61 2715ade4 Sofia Papagiannaki
        self.engine.echo_pool = False
62 43be9afd Sofia Papagiannaki
        self.conn = self.engine.connect()
63 85a93d09 Antony Chazapis
        self.trans = None
64 2715ade4 Sofia Papagiannaki
65 d14fe290 Antony Chazapis
    def close(self):
66 d14fe290 Antony Chazapis
        self.conn.close()
67 2715ade4 Sofia Papagiannaki
68 85a93d09 Antony Chazapis
    def execute(self):
69 43be9afd Sofia Papagiannaki
        self.trans = self.conn.begin()
70 2715ade4 Sofia Papagiannaki
71 85a93d09 Antony Chazapis
    def commit(self):
72 85a93d09 Antony Chazapis
        self.trans.commit()
73 85a93d09 Antony Chazapis
        self.trans = None
74 2715ade4 Sofia Papagiannaki
75 85a93d09 Antony Chazapis
    def rollback(self):
76 85a93d09 Antony Chazapis
        self.trans.rollback()
77 85a93d09 Antony Chazapis
        self.trans = None