Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / lib / sqlalchemy / public.py @ e7117103

History | View | Annotate | Download (4.6 kB)

1 2e662088 Antony Chazapis
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 d50ed8d4 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 d50ed8d4 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 d50ed8d4 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 d50ed8d4 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 d50ed8d4 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 4f917833 Sofia Papagiannaki
from dbworker import DBWorker
35 af75e8a5 Antony Chazapis
from sqlalchemy import Table, Column, String, Integer, Boolean, MetaData
36 af75e8a5 Antony Chazapis
from sqlalchemy.sql import and_, select
37 bb4eafc6 Antony Chazapis
from sqlalchemy.schema import Index
38 6a82f89f Sofia Papagiannaki
from sqlalchemy.exc import NoSuchTableError
39 bb4eafc6 Antony Chazapis
40 d50ed8d4 Sofia Papagiannaki
41 6a82f89f Sofia Papagiannaki
def create_tables(engine):
42 6a82f89f Sofia Papagiannaki
    metadata = MetaData()
43 d50ed8d4 Sofia Papagiannaki
    columns = []
44 6a82f89f Sofia Papagiannaki
    columns.append(Column('public_id', Integer, primary_key=True))
45 6a82f89f Sofia Papagiannaki
    columns.append(Column('path', String(2048), nullable=False))
46 6a82f89f Sofia Papagiannaki
    columns.append(Column('active', Boolean, nullable=False, default=True))
47 d50ed8d4 Sofia Papagiannaki
    public = Table('public', metadata, *columns, mysql_engine='InnoDB',
48 d50ed8d4 Sofia Papagiannaki
                   sqlite_autoincrement=True)
49 6a82f89f Sofia Papagiannaki
    # place an index on path
50 6a82f89f Sofia Papagiannaki
    Index('idx_public_path', public.c.path, unique=True)
51 6a82f89f Sofia Papagiannaki
    metadata.create_all(engine)
52 6a82f89f Sofia Papagiannaki
    return metadata.sorted_tables
53 4f917833 Sofia Papagiannaki
54 d50ed8d4 Sofia Papagiannaki
55 4f917833 Sofia Papagiannaki
class Public(DBWorker):
56 4f917833 Sofia Papagiannaki
    """Paths can be marked as public."""
57 d50ed8d4 Sofia Papagiannaki
58 4f917833 Sofia Papagiannaki
    def __init__(self, **params):
59 4f917833 Sofia Papagiannaki
        DBWorker.__init__(self, **params)
60 6a82f89f Sofia Papagiannaki
        try:
61 6a82f89f Sofia Papagiannaki
            metadata = MetaData(self.engine)
62 6a82f89f Sofia Papagiannaki
            self.public = Table('public', metadata, autoload=True)
63 6a82f89f Sofia Papagiannaki
        except NoSuchTableError:
64 6a82f89f Sofia Papagiannaki
            tables = create_tables(self.engine)
65 6a82f89f Sofia Papagiannaki
            map(lambda t: self.__setattr__(t.name, t), tables)
66 d50ed8d4 Sofia Papagiannaki
67 4f917833 Sofia Papagiannaki
    def public_set(self, path):
68 af75e8a5 Antony Chazapis
        s = select([self.public.c.public_id])
69 a68d51a4 Sofia Papagiannaki
        s = s.where(self.public.c.path == path)
70 2ce96391 Sofia Papagiannaki
        r = self.conn.execute(s)
71 af75e8a5 Antony Chazapis
        row = r.fetchone()
72 4f917833 Sofia Papagiannaki
        r.close()
73 af75e8a5 Antony Chazapis
        if row:
74 af75e8a5 Antony Chazapis
            s = self.public.update().where(self.public.c.public_id == row[0])
75 af75e8a5 Antony Chazapis
            s = s.values(active=True)
76 af75e8a5 Antony Chazapis
        else:
77 2ce96391 Sofia Papagiannaki
            s = self.public.insert()
78 af75e8a5 Antony Chazapis
            s = s.values(path=path, active=True)
79 af75e8a5 Antony Chazapis
        r = self.conn.execute(s)
80 af75e8a5 Antony Chazapis
        r.close()
81 d50ed8d4 Sofia Papagiannaki
82 4f917833 Sofia Papagiannaki
    def public_unset(self, path):
83 af75e8a5 Antony Chazapis
        s = self.public.update()
84 af75e8a5 Antony Chazapis
        s = s.where(self.public.c.path == path)
85 af75e8a5 Antony Chazapis
        s = s.values(active=False)
86 4f917833 Sofia Papagiannaki
        r = self.conn.execute(s)
87 4f917833 Sofia Papagiannaki
        r.close()
88 d50ed8d4 Sofia Papagiannaki
89 8221c89d Sofia Papagiannaki
    def public_unset_bulk(self, paths):
90 fe3252ab Sofia Papagiannaki
        if not paths:
91 fe3252ab Sofia Papagiannaki
            return
92 8221c89d Sofia Papagiannaki
        s = self.public.update()
93 8221c89d Sofia Papagiannaki
        s = s.where(self.public.c.path.in_(paths))
94 8221c89d Sofia Papagiannaki
        s = s.values(active=False)
95 8221c89d Sofia Papagiannaki
        r = self.conn.execute(s)
96 8221c89d Sofia Papagiannaki
        r.close()
97 d50ed8d4 Sofia Papagiannaki
98 bb4eafc6 Antony Chazapis
    def public_get(self, path):
99 af75e8a5 Antony Chazapis
        s = select([self.public.c.public_id])
100 af75e8a5 Antony Chazapis
        s = s.where(and_(self.public.c.path == path,
101 4732ed31 Antony Chazapis
                         self.public.c.active == True))
102 bb4eafc6 Antony Chazapis
        r = self.conn.execute(s)
103 bb4eafc6 Antony Chazapis
        row = r.fetchone()
104 bb4eafc6 Antony Chazapis
        r.close()
105 bb4eafc6 Antony Chazapis
        if row:
106 bb4eafc6 Antony Chazapis
            return row[0]
107 bb4eafc6 Antony Chazapis
        return None
108 d50ed8d4 Sofia Papagiannaki
109 15a96c3e Antony Chazapis
    def public_list(self, prefix):
110 15a96c3e Antony Chazapis
        s = select([self.public.c.path, self.public.c.public_id])
111 d50ed8d4 Sofia Papagiannaki
        s = s.where(self.public.c.path.like(
112 d50ed8d4 Sofia Papagiannaki
            self.escape_like(prefix) + '%', escape='\\'))
113 15a96c3e Antony Chazapis
        s = s.where(self.public.c.active == True)
114 15a96c3e Antony Chazapis
        r = self.conn.execute(s)
115 15a96c3e Antony Chazapis
        rows = r.fetchall()
116 15a96c3e Antony Chazapis
        r.close()
117 15a96c3e Antony Chazapis
        return rows
118 d50ed8d4 Sofia Papagiannaki
119 bb4eafc6 Antony Chazapis
    def public_path(self, public):
120 af75e8a5 Antony Chazapis
        s = select([self.public.c.path])
121 af75e8a5 Antony Chazapis
        s = s.where(and_(self.public.c.public_id == public,
122 4732ed31 Antony Chazapis
                         self.public.c.active == True))
123 4f917833 Sofia Papagiannaki
        r = self.conn.execute(s)
124 bb4eafc6 Antony Chazapis
        row = r.fetchone()
125 4f917833 Sofia Papagiannaki
        r.close()
126 bb4eafc6 Antony Chazapis
        if row:
127 bb4eafc6 Antony Chazapis
            return row[0]
128 bb4eafc6 Antony Chazapis
        return None