root / snf-pithos-backend / pithos / backends / lib / sqlalchemy / public.py @ d3655326
History | View | Annotate | Download (4.6 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from dbworker import DBWorker |
35 |
from sqlalchemy import Table, Column, String, Integer, Boolean, MetaData |
36 |
from sqlalchemy.sql import and_, select |
37 |
from sqlalchemy.schema import Index |
38 |
from sqlalchemy.exc import NoSuchTableError |
39 |
|
40 |
|
41 |
def create_tables(engine): |
42 |
metadata = MetaData() |
43 |
columns = [] |
44 |
columns.append(Column('public_id', Integer, primary_key=True)) |
45 |
columns.append(Column('path', String(2048), nullable=False)) |
46 |
columns.append(Column('active', Boolean, nullable=False, default=True)) |
47 |
public = Table('public', metadata, *columns, mysql_engine='InnoDB', |
48 |
sqlite_autoincrement=True)
|
49 |
# place an index on path
|
50 |
Index('idx_public_path', public.c.path, unique=True) |
51 |
metadata.create_all(engine) |
52 |
return metadata.sorted_tables
|
53 |
|
54 |
|
55 |
class Public(DBWorker): |
56 |
"""Paths can be marked as public."""
|
57 |
|
58 |
def __init__(self, **params): |
59 |
DBWorker.__init__(self, **params)
|
60 |
try:
|
61 |
metadata = MetaData(self.engine)
|
62 |
self.public = Table('public', metadata, autoload=True) |
63 |
except NoSuchTableError:
|
64 |
tables = create_tables(self.engine)
|
65 |
map(lambda t: self.__setattr__(t.name, t), tables) |
66 |
|
67 |
def public_set(self, path): |
68 |
s = select([self.public.c.public_id])
|
69 |
s = s.where(self.public.c.path == path)
|
70 |
r = self.conn.execute(s)
|
71 |
row = r.fetchone() |
72 |
r.close() |
73 |
if row:
|
74 |
s = self.public.update().where(self.public.c.public_id == row[0]) |
75 |
s = s.values(active=True)
|
76 |
else:
|
77 |
s = self.public.insert()
|
78 |
s = s.values(path=path, active=True)
|
79 |
r = self.conn.execute(s)
|
80 |
r.close() |
81 |
|
82 |
def public_unset(self, path): |
83 |
s = self.public.update()
|
84 |
s = s.where(self.public.c.path == path)
|
85 |
s = s.values(active=False)
|
86 |
r = self.conn.execute(s)
|
87 |
r.close() |
88 |
|
89 |
def public_unset_bulk(self, paths): |
90 |
if not paths: |
91 |
return
|
92 |
s = self.public.update()
|
93 |
s = s.where(self.public.c.path.in_(paths))
|
94 |
s = s.values(active=False)
|
95 |
r = self.conn.execute(s)
|
96 |
r.close() |
97 |
|
98 |
def public_get(self, path): |
99 |
s = select([self.public.c.public_id])
|
100 |
s = s.where(and_(self.public.c.path == path,
|
101 |
self.public.c.active == True)) |
102 |
r = self.conn.execute(s)
|
103 |
row = r.fetchone() |
104 |
r.close() |
105 |
if row:
|
106 |
return row[0] |
107 |
return None |
108 |
|
109 |
def public_list(self, prefix): |
110 |
s = select([self.public.c.path, self.public.c.public_id]) |
111 |
s = s.where(self.public.c.path.like(
|
112 |
self.escape_like(prefix) + '%', escape='\\')) |
113 |
s = s.where(self.public.c.active == True) |
114 |
r = self.conn.execute(s)
|
115 |
rows = r.fetchall() |
116 |
r.close() |
117 |
return rows
|
118 |
|
119 |
def public_path(self, public): |
120 |
s = select([self.public.c.path])
|
121 |
s = s.where(and_(self.public.c.public_id == public,
|
122 |
self.public.c.active == True)) |
123 |
r = self.conn.execute(s)
|
124 |
row = r.fetchone() |
125 |
r.close() |
126 |
if row:
|
127 |
return row[0] |
128 |
return None |