Revision d50ed8d4 snf-pithos-backend/pithos/backends/lib/sqlalchemy/public.py

b/snf-pithos-backend/pithos/backends/lib/sqlalchemy/public.py
1 1
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2
# 
2
#
3 3
# Redistribution and use in source and binary forms, with or
4 4
# without modification, are permitted provided that the following
5 5
# conditions are met:
6
# 
6
#
7 7
#   1. Redistributions of source code must retain the above
8 8
#      copyright notice, this list of conditions and the following
9 9
#      disclaimer.
10
# 
10
#
11 11
#   2. Redistributions in binary form must reproduce the above
12 12
#      copyright notice, this list of conditions and the following
13 13
#      disclaimer in the documentation and/or other materials
14 14
#      provided with the distribution.
15
# 
15
#
16 16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
......
25 25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 27
# POSSIBILITY OF SUCH DAMAGE.
28
# 
28
#
29 29
# The views and conclusions contained in the software and
30 30
# documentation are those of the authors and should not be
31 31
# interpreted as representing official policies, either expressed
......
37 37
from sqlalchemy.schema import Index
38 38
from sqlalchemy.exc import NoSuchTableError
39 39

  
40

  
40 41
def create_tables(engine):
41 42
    metadata = MetaData()
42
    columns=[]
43
    columns = []
43 44
    columns.append(Column('public_id', Integer, primary_key=True))
44 45
    columns.append(Column('path', String(2048), nullable=False))
45 46
    columns.append(Column('active', Boolean, nullable=False, default=True))
46
    public = Table('public', metadata, *columns, mysql_engine='InnoDB', sqlite_autoincrement=True)
47
    public = Table('public', metadata, *columns, mysql_engine='InnoDB',
48
                   sqlite_autoincrement=True)
47 49
    # place an index on path
48 50
    Index('idx_public_path', public.c.path, unique=True)
49 51
    metadata.create_all(engine)
50 52
    return metadata.sorted_tables
51 53

  
54

  
52 55
class Public(DBWorker):
53 56
    """Paths can be marked as public."""
54
    
57

  
55 58
    def __init__(self, **params):
56 59
        DBWorker.__init__(self, **params)
57 60
        try:
......
60 63
        except NoSuchTableError:
61 64
            tables = create_tables(self.engine)
62 65
            map(lambda t: self.__setattr__(t.name, t), tables)
63
    
66

  
64 67
    def public_set(self, path):
65 68
        s = select([self.public.c.public_id])
66 69
        s = s.where(self.public.c.path == path)
......
75 78
            s = s.values(path=path, active=True)
76 79
        r = self.conn.execute(s)
77 80
        r.close()
78
    
81

  
79 82
    def public_unset(self, path):
80 83
        s = self.public.update()
81 84
        s = s.where(self.public.c.path == path)
82 85
        s = s.values(active=False)
83 86
        r = self.conn.execute(s)
84 87
        r.close()
85
    
88

  
86 89
    def public_unset_bulk(self, paths):
87 90
        s = self.public.update()
88 91
        s = s.where(self.public.c.path.in_(paths))
89 92
        s = s.values(active=False)
90 93
        r = self.conn.execute(s)
91 94
        r.close()
92
    
95

  
93 96
    def public_get(self, path):
94 97
        s = select([self.public.c.public_id])
95 98
        s = s.where(and_(self.public.c.path == path,
......
100 103
        if row:
101 104
            return row[0]
102 105
        return None
103
    
106

  
104 107
    def public_list(self, prefix):
105 108
        s = select([self.public.c.path, self.public.c.public_id])
106
        s = s.where(self.public.c.path.like(self.escape_like(prefix) + '%', escape='\\'))
109
        s = s.where(self.public.c.path.like(
110
            self.escape_like(prefix) + '%', escape='\\'))
107 111
        s = s.where(self.public.c.active == True)
108 112
        r = self.conn.execute(s)
109 113
        rows = r.fetchall()
110 114
        r.close()
111 115
        return rows
112
    
116

  
113 117
    def public_path(self, public):
114 118
        s = select([self.public.c.path])
115 119
        s = s.where(and_(self.public.c.public_id == public,

Also available in: Unified diff