X-Git-Url: https://code.grnet.gr/git/pithos/blobdiff_plain/26f81fe9e4fe838a0d75ee532ebd456d7b7f3f33..4732ed316c3512ab41b710514c373f10b95110ca:/pithos/backends/lib/sqlalchemy/public.py diff --git a/pithos/backends/lib/sqlalchemy/public.py b/pithos/backends/lib/sqlalchemy/public.py index a9f121c..3618961 100644 --- a/pithos/backends/lib/sqlalchemy/public.py +++ b/pithos/backends/lib/sqlalchemy/public.py @@ -1,4 +1,4 @@ -# Copyright 2011 GRNET S.A. All rights reserved. +# Copyright 2011-2012 GRNET S.A. All rights reserved. # # Redistribution and use in source and binary forms, with or # without modification, are permitted provided that the following @@ -32,8 +32,10 @@ # or implied, of GRNET S.A. from dbworker import DBWorker -from sqlalchemy import Table, Column, String, MetaData -from sqlalchemy.sql import select +from sqlalchemy import Table, Column, String, Integer, Boolean, MetaData +from sqlalchemy.sql import and_, select +from sqlalchemy.schema import Index + class Public(DBWorker): """Paths can be marked as public.""" @@ -42,30 +44,54 @@ class Public(DBWorker): DBWorker.__init__(self, **params) metadata = MetaData() columns=[] - columns.append(Column('path', String(2048), index=True)) - self.public = Table('public', metadata, *columns, mysql_engine='InnoDB') + columns.append(Column('public_id', Integer, primary_key=True)) + columns.append(Column('path', String(2048), nullable=False)) + columns.append(Column('active', Boolean, nullable=False, default=True)) + self.public = Table('public', metadata, *columns, mysql_engine='InnoDB', sqlite_autoincrement=True) + # place an index on path + Index('idx_public_path', self.public.c.path, unique=True) metadata.create_all(self.engine) - def public_set(self, path): - s = self.public.select() + s = select([self.public.c.public_id]) s = s.where(self.public.c.path == path) r = self.conn.execute(s) - public = r.fetchall() + row = r.fetchone() r.close() - if len(public) == 0: + if row: + s = self.public.update().where(self.public.c.public_id == row[0]) + s = s.values(active=True) + else: s = self.public.insert() - r = self.conn.execute(s, path = path) - r.close() + s = s.values(path=path, active=True) + r = self.conn.execute(s) + r.close() def public_unset(self, path): - s = self.public.delete().where(self.public.c.path == path) + s = self.public.update() + s = s.where(self.public.c.path == path) + s = s.values(active=False) + r = self.conn.execute(s) + r.close() + + def public_get(self, path): + s = select([self.public.c.public_id]) + s = s.where(and_(self.public.c.path == path, + self.public.c.active == True)) r = self.conn.execute(s) + row = r.fetchone() r.close() + if row: + return row[0] + return None - def public_check(self, path): - s = select([self.public.c.path], self.public.c.path == path) + def public_path(self, public): + s = select([self.public.c.path]) + s = s.where(and_(self.public.c.public_id == public, + self.public.c.active == True)) r = self.conn.execute(s) - l = r.fetchone() + row = r.fetchone() r.close() - return bool(l) \ No newline at end of file + if row: + return row[0] + return None