root / snf-pithos-backend / pithos / backends / lib / sqlite / xfeatures.py @ d3655326
History | View | Annotate | Download (5.9 kB)
1 | 2e662088 | Antony Chazapis | # Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 2715ade4 | Sofia Papagiannaki | #
|
3 | a9b3f29d | Antony Chazapis | # Redistribution and use in source and binary forms, with or
|
4 | a9b3f29d | Antony Chazapis | # without modification, are permitted provided that the following
|
5 | a9b3f29d | Antony Chazapis | # conditions are met:
|
6 | 2715ade4 | Sofia Papagiannaki | #
|
7 | a9b3f29d | Antony Chazapis | # 1. Redistributions of source code must retain the above
|
8 | a9b3f29d | Antony Chazapis | # copyright notice, this list of conditions and the following
|
9 | a9b3f29d | Antony Chazapis | # disclaimer.
|
10 | 2715ade4 | Sofia Papagiannaki | #
|
11 | a9b3f29d | Antony Chazapis | # 2. Redistributions in binary form must reproduce the above
|
12 | a9b3f29d | Antony Chazapis | # copyright notice, this list of conditions and the following
|
13 | a9b3f29d | Antony Chazapis | # disclaimer in the documentation and/or other materials
|
14 | a9b3f29d | Antony Chazapis | # provided with the distribution.
|
15 | 2715ade4 | Sofia Papagiannaki | #
|
16 | a9b3f29d | Antony Chazapis | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | a9b3f29d | Antony Chazapis | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | a9b3f29d | Antony Chazapis | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | a9b3f29d | Antony Chazapis | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | a9b3f29d | Antony Chazapis | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | a9b3f29d | Antony Chazapis | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | a9b3f29d | Antony Chazapis | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | a9b3f29d | Antony Chazapis | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | a9b3f29d | Antony Chazapis | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | a9b3f29d | Antony Chazapis | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | a9b3f29d | Antony Chazapis | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | a9b3f29d | Antony Chazapis | # POSSIBILITY OF SUCH DAMAGE.
|
28 | 2715ade4 | Sofia Papagiannaki | #
|
29 | a9b3f29d | Antony Chazapis | # The views and conclusions contained in the software and
|
30 | a9b3f29d | Antony Chazapis | # documentation are those of the authors and should not be
|
31 | a9b3f29d | Antony Chazapis | # interpreted as representing official policies, either expressed
|
32 | a9b3f29d | Antony Chazapis | # or implied, of GRNET S.A.
|
33 | a9b3f29d | Antony Chazapis | |
34 | 6f4bce7b | Antony Chazapis | from collections import defaultdict |
35 | 6f4bce7b | Antony Chazapis | |
36 | a9b3f29d | Antony Chazapis | from dbworker import DBWorker |
37 | a9b3f29d | Antony Chazapis | |
38 | a9b3f29d | Antony Chazapis | |
39 | a9b3f29d | Antony Chazapis | class XFeatures(DBWorker): |
40 | a9b3f29d | Antony Chazapis | """XFeatures are path properties that allow non-nested
|
41 | a9b3f29d | Antony Chazapis | inheritance patterns. Currently used for storing permissions.
|
42 | a9b3f29d | Antony Chazapis | """
|
43 | 2715ade4 | Sofia Papagiannaki | |
44 | a9b3f29d | Antony Chazapis | def __init__(self, **params): |
45 | a9b3f29d | Antony Chazapis | DBWorker.__init__(self, **params)
|
46 | a9b3f29d | Antony Chazapis | execute = self.execute
|
47 | 2715ade4 | Sofia Papagiannaki | |
48 | a9b3f29d | Antony Chazapis | execute(""" pragma foreign_keys = on """)
|
49 | 2715ade4 | Sofia Papagiannaki | |
50 | a9b3f29d | Antony Chazapis | execute(""" create table if not exists xfeatures
|
51 | a9b3f29d | Antony Chazapis | ( feature_id integer primary key,
|
52 | a9b3f29d | Antony Chazapis | path text ) """)
|
53 | a9b3f29d | Antony Chazapis | execute(""" create unique index if not exists idx_features_path
|
54 | a9b3f29d | Antony Chazapis | on xfeatures(path) """)
|
55 | a9b3f29d | Antony Chazapis | |
56 | a9b3f29d | Antony Chazapis | execute(""" create table if not exists xfeaturevals
|
57 | a9b3f29d | Antony Chazapis | ( feature_id integer,
|
58 | a9b3f29d | Antony Chazapis | key integer,
|
59 | a9b3f29d | Antony Chazapis | value text,
|
60 | a9b3f29d | Antony Chazapis | primary key (feature_id, key, value)
|
61 | a9b3f29d | Antony Chazapis | foreign key (feature_id) references xfeatures(feature_id)
|
62 | a9b3f29d | Antony Chazapis | on delete cascade ) """)
|
63 | 2715ade4 | Sofia Papagiannaki | |
64 | d83c93c9 | Antony Chazapis | # def xfeature_inherit(self, path):
|
65 | d83c93c9 | Antony Chazapis | # """Return the (path, feature) inherited by the path, or None."""
|
66 | 2715ade4 | Sofia Papagiannaki | #
|
67 | d83c93c9 | Antony Chazapis | # q = ("select path, feature_id from xfeatures "
|
68 | d83c93c9 | Antony Chazapis | # "where path <= ? "
|
69 | d83c93c9 | Antony Chazapis | # "and ? like path || '%' " # XXX: Escape like...
|
70 | d83c93c9 | Antony Chazapis | # "order by path desc")
|
71 | d83c93c9 | Antony Chazapis | # self.execute(q, (path, path))
|
72 | d83c93c9 | Antony Chazapis | # return self.fetchall()
|
73 | 2715ade4 | Sofia Papagiannaki | |
74 | 43763394 | Antony Chazapis | def xfeature_get(self, path): |
75 | 43763394 | Antony Chazapis | """Return feature for path."""
|
76 | 2715ade4 | Sofia Papagiannaki | |
77 | 43763394 | Antony Chazapis | q = "select feature_id from xfeatures where path = ?"
|
78 | 43763394 | Antony Chazapis | self.execute(q, (path,))
|
79 | 43763394 | Antony Chazapis | r = self.fetchone()
|
80 | 43763394 | Antony Chazapis | if r is not None: |
81 | 43763394 | Antony Chazapis | return r[0] |
82 | cf341da4 | Antony Chazapis | return None |
83 | 2715ade4 | Sofia Papagiannaki | |
84 | a9b3f29d | Antony Chazapis | def xfeature_create(self, path): |
85 | a9b3f29d | Antony Chazapis | """Create and return a feature for path.
|
86 | 6f4bce7b | Antony Chazapis | If the path has a feature, return it.
|
87 | a9b3f29d | Antony Chazapis | """
|
88 | 2715ade4 | Sofia Papagiannaki | |
89 | 43763394 | Antony Chazapis | feature = self.xfeature_get(path)
|
90 | 43763394 | Antony Chazapis | if feature is not None: |
91 | 43763394 | Antony Chazapis | return feature
|
92 | a9b3f29d | Antony Chazapis | q = "insert into xfeatures (path) values (?)"
|
93 | a9b3f29d | Antony Chazapis | id = self.execute(q, (path,)).lastrowid
|
94 | a9b3f29d | Antony Chazapis | return id |
95 | 2715ade4 | Sofia Papagiannaki | |
96 | a9b3f29d | Antony Chazapis | def xfeature_destroy(self, path): |
97 | a9b3f29d | Antony Chazapis | """Destroy a feature and all its key, value pairs."""
|
98 | 2715ade4 | Sofia Papagiannaki | |
99 | a9b3f29d | Antony Chazapis | q = "delete from xfeatures where path = ?"
|
100 | a9b3f29d | Antony Chazapis | self.execute(q, (path,))
|
101 | 2715ade4 | Sofia Papagiannaki | |
102 | 4d15c94e | Sofia Papagiannaki | def xfeature_destroy_bulk(self, paths): |
103 | 4d15c94e | Sofia Papagiannaki | """Destroy features and all their key, value pairs."""
|
104 | 2715ade4 | Sofia Papagiannaki | |
105 | 4d15c94e | Sofia Papagiannaki | placeholders = ','.join('?' for path in paths) |
106 | 4d15c94e | Sofia Papagiannaki | q = "delete from xfeatures where path in (%s)" % placeholders
|
107 | 4d15c94e | Sofia Papagiannaki | self.execute(q, paths)
|
108 | 2715ade4 | Sofia Papagiannaki | |
109 | 6f4bce7b | Antony Chazapis | def feature_dict(self, feature): |
110 | 6f4bce7b | Antony Chazapis | """Return a dict mapping keys to list of values for feature."""
|
111 | 2715ade4 | Sofia Papagiannaki | |
112 | 62f915a1 | Antony Chazapis | q = "select key, value from xfeaturevals where feature_id = ?"
|
113 | a9b3f29d | Antony Chazapis | self.execute(q, (feature,))
|
114 | 6f4bce7b | Antony Chazapis | d = defaultdict(list)
|
115 | 6f4bce7b | Antony Chazapis | for key, value in self.fetchall(): |
116 | 6f4bce7b | Antony Chazapis | d[key].append(value) |
117 | 6f4bce7b | Antony Chazapis | return d
|
118 | 2715ade4 | Sofia Papagiannaki | |
119 | a9b3f29d | Antony Chazapis | def feature_set(self, feature, key, value): |
120 | a9b3f29d | Antony Chazapis | """Associate a key, value pair with a feature."""
|
121 | 2715ade4 | Sofia Papagiannaki | |
122 | a9b3f29d | Antony Chazapis | q = "insert or ignore into xfeaturevals (feature_id, key, value) values (?, ?, ?)"
|
123 | a9b3f29d | Antony Chazapis | self.execute(q, (feature, key, value))
|
124 | 2715ade4 | Sofia Papagiannaki | |
125 | a9b3f29d | Antony Chazapis | def feature_setmany(self, feature, key, values): |
126 | a9b3f29d | Antony Chazapis | """Associate the given key, and values with a feature."""
|
127 | 2715ade4 | Sofia Papagiannaki | |
128 | a9b3f29d | Antony Chazapis | q = "insert or ignore into xfeaturevals (feature_id, key, value) values (?, ?, ?)"
|
129 | a9b3f29d | Antony Chazapis | self.executemany(q, ((feature, key, v) for v in values)) |
130 | 2715ade4 | Sofia Papagiannaki | |
131 | a9b3f29d | Antony Chazapis | def feature_unset(self, feature, key, value): |
132 | a9b3f29d | Antony Chazapis | """Disassociate a key, value pair from a feature."""
|
133 | 2715ade4 | Sofia Papagiannaki | |
134 | a9b3f29d | Antony Chazapis | q = ("delete from xfeaturevals where "
|
135 | a9b3f29d | Antony Chazapis | "feature_id = ? and key = ? and value = ?")
|
136 | a9b3f29d | Antony Chazapis | self.execute(q, (feature, key, value))
|
137 | 2715ade4 | Sofia Papagiannaki | |
138 | a9b3f29d | Antony Chazapis | def feature_unsetmany(self, feature, key, values): |
139 | a9b3f29d | Antony Chazapis | """Disassociate the key for the values given, from a feature."""
|
140 | 2715ade4 | Sofia Papagiannaki | |
141 | a9b3f29d | Antony Chazapis | q = ("delete from xfeaturevals where "
|
142 | a9b3f29d | Antony Chazapis | "feature_id = ? and key = ? and value = ?")
|
143 | a9b3f29d | Antony Chazapis | self.executemany(q, ((feature, key, v) for v in values)) |
144 | 2715ade4 | Sofia Papagiannaki | |
145 | a9b3f29d | Antony Chazapis | def feature_get(self, feature, key): |
146 | a9b3f29d | Antony Chazapis | """Return the list of values for a key of a feature."""
|
147 | 2715ade4 | Sofia Papagiannaki | |
148 | a9b3f29d | Antony Chazapis | q = "select value from xfeaturevals where feature_id = ? and key = ?"
|
149 | a9b3f29d | Antony Chazapis | self.execute(q, (feature, key))
|
150 | a9b3f29d | Antony Chazapis | return [r[0] for r in self.fetchall()] |
151 | 2715ade4 | Sofia Papagiannaki | |
152 | a9b3f29d | Antony Chazapis | def feature_clear(self, feature, key): |
153 | a9b3f29d | Antony Chazapis | """Delete all key, value pairs for a key of a feature."""
|
154 | 2715ade4 | Sofia Papagiannaki | |
155 | a9b3f29d | Antony Chazapis | q = "delete from xfeaturevals where feature_id = ? and key = ?"
|
156 | a9b3f29d | Antony Chazapis | self.execute(q, (feature, key)) |