Revision af75e8a5 pithos/backends/lib/sqlalchemy/public.py
b/pithos/backends/lib/sqlalchemy/public.py | ||
---|---|---|
32 | 32 |
# or implied, of GRNET S.A. |
33 | 33 |
|
34 | 34 |
from dbworker import DBWorker |
35 |
from sqlalchemy import Table, Column, String, Integer, MetaData |
|
36 |
from sqlalchemy.sql import select |
|
35 |
from sqlalchemy import Table, Column, String, Integer, Boolean, MetaData
|
|
36 |
from sqlalchemy.sql import and_, select
|
|
37 | 37 |
from sqlalchemy.schema import Index |
38 | 38 |
|
39 | 39 |
|
... | ... | |
45 | 45 |
metadata = MetaData() |
46 | 46 |
columns=[] |
47 | 47 |
columns.append(Column('public_id', Integer, primary_key=True)) |
48 |
columns.append(Column('path', String(2048))) |
|
48 |
columns.append(Column('path', String(2048), nullable=False)) |
|
49 |
columns.append(Column('active', Boolean, nullable=False, default=True)) |
|
49 | 50 |
self.public = Table('public', metadata, *columns, mysql_engine='InnoDB', sqlite_autoincrement=True) |
50 | 51 |
# place an index on path |
51 | 52 |
Index('idx_public_path', self.public.c.path, unique=True) |
52 | 53 |
metadata.create_all(self.engine) |
53 | 54 |
|
54 | 55 |
def public_set(self, path): |
55 |
s = self.public.select()
|
|
56 |
s = select([self.public.c.public_id])
|
|
56 | 57 |
s = s.where(self.public.c.path == path) |
57 | 58 |
r = self.conn.execute(s) |
58 |
public = r.fetchall()
|
|
59 |
row = r.fetchone()
|
|
59 | 60 |
r.close() |
60 |
if len(public) == 0: |
|
61 |
if row: |
|
62 |
s = self.public.update().where(self.public.c.public_id == row[0]) |
|
63 |
s = s.values(active=True) |
|
64 |
else: |
|
61 | 65 |
s = self.public.insert() |
62 |
r = self.conn.execute(s, path = path) |
|
63 |
r.close() |
|
66 |
s = s.values(path=path, active=True) |
|
67 |
r = self.conn.execute(s) |
|
68 |
r.close() |
|
64 | 69 |
|
65 | 70 |
def public_unset(self, path): |
66 |
s = self.public.delete().where(self.public.c.path == path) |
|
71 |
s = self.public.update() |
|
72 |
s = s.where(self.public.c.path == path) |
|
73 |
s = s.values(active=False) |
|
67 | 74 |
r = self.conn.execute(s) |
68 | 75 |
r.close() |
69 | 76 |
|
70 | 77 |
def public_get(self, path): |
71 |
s = select([self.public.c.public_id], self.public.c.path == path) |
|
78 |
s = select([self.public.c.public_id]) |
|
79 |
s = s.where(and_(self.public.c.path == path, |
|
80 |
self.public.c.active == True)) |
|
72 | 81 |
r = self.conn.execute(s) |
73 | 82 |
row = r.fetchone() |
74 | 83 |
r.close() |
... | ... | |
77 | 86 |
return None |
78 | 87 |
|
79 | 88 |
def public_path(self, public): |
80 |
s = select([self.public.c.path], self.public.c.public_id == public) |
|
89 |
s = select([self.public.c.path]) |
|
90 |
s = s.where(and_(self.public.c.public_id == public, |
|
91 |
self.public.c.active == True)) |
|
81 | 92 |
r = self.conn.execute(s) |
82 | 93 |
row = r.fetchone() |
83 | 94 |
r.close() |
Also available in: Unified diff