Skip to content
This repository
Browse code

Removing old Postgres manager for sdb.db.

  • Loading branch information...
commit 3db8668ed2f08e657ecbde63807b1910047a8b47 1 parent 43647e7
Mitch Garnaat authored June 13, 2012
3  boto/sdb/db/__init__.py
@@ -14,8 +14,7 @@
14 14
 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 15
 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16 16
 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17  
-# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
  17
+# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 18
 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 19
 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 20
 # IN THE SOFTWARE.
21  
-
28  boto/sdb/db/manager/__init__.py
@@ -14,17 +14,18 @@
14 14
 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 15
 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16 16
 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17  
-# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
  17
+# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 18
 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 19
 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 20
 # IN THE SOFTWARE.
21 21
 import boto
22 22
 
  23
+
23 24
 def get_manager(cls):
24 25
     """
25  
-    Returns the appropriate Manager class for a given Model class.  It does this by
26  
-    looking in the boto config for a section like this::
27  
-    
  26
+    Returns the appropriate Manager class for a given Model class.  It
  27
+    does this by looking in the boto config for a section like this::
  28
+
28 29
         [DB]
29 30
         db_type = SimpleDB
30 31
         db_user = <aws access key id>
@@ -36,10 +37,11 @@ def get_manager(cls):
36 37
         db_passwd = <another aws secret access key>
37 38
         db_name = basic_domain
38 39
         db_port = 1111
39  
-    
40  
-    The values in the DB section are "generic values" that will be used if nothing more
41  
-    specific is found.  You can also create a section for a specific Model class that
42  
-    gives the db info for that class.  In the example above, TestBasic is a Model subclass.
  40
+
  41
+    The values in the DB section are "generic values" that will be used
  42
+    if nothing more specific is found.  You can also create a section for
  43
+    a specific Model class that gives the db info for that class.
  44
+    In the example above, TestBasic is a Model subclass.
43 45
     """
44 46
     db_user = boto.config.get('DB', 'db_user', None)
45 47
     db_passwd = boto.config.get('DB', 'db_passwd', None)
@@ -51,7 +53,7 @@ def get_manager(cls):
51 53
     enable_ssl = boto.config.getbool('DB', 'enable_ssl', True)
52 54
     sql_dir = boto.config.get('DB', 'sql_dir', None)
53 55
     debug = boto.config.getint('DB', 'debug', 0)
54  
-    # first see if there is a fully qualified section name in the Boto config file
  56
+    # first see if there is a fully qualified section name in the Boto config
55 57
     module_name = cls.__module__.replace('.', '_')
56 58
     db_section = 'DB_' + module_name + '_' + cls.__name__
57 59
     if not boto.config.has_section(db_section):
@@ -75,17 +77,9 @@ def get_manager(cls):
75 77
         from sdbmanager import SDBManager
76 78
         return SDBManager(cls, db_name, db_user, db_passwd,
77 79
                           db_host, db_port, db_table, sql_dir, enable_ssl)
78  
-    elif db_type == 'PostgreSQL':
79  
-        from pgmanager import PGManager
80  
-        if db_table:
81  
-            return PGManager(cls, db_name, db_user, db_passwd,
82  
-                             db_host, db_port, db_table, sql_dir, enable_ssl)
83  
-        else:
84  
-            return None
85 80
     elif db_type == 'XML':
86 81
         from xmlmanager import XMLManager
87 82
         return XMLManager(cls, db_name, db_user, db_passwd,
88 83
                           db_host, db_port, db_table, sql_dir, enable_ssl)
89 84
     else:
90 85
         raise ValueError('Unknown db_type: %s' % db_type)
91  
-
389  boto/sdb/db/manager/pgmanager.py
... ...
@@ -1,389 +0,0 @@
1  
-# Copyright (c) 2006,2007,2008 Mitch Garnaat http://garnaat.org/
2  
-#
3  
-# Permission is hereby granted, free of charge, to any person obtaining a
4  
-# copy of this software and associated documentation files (the
5  
-# "Software"), to deal in the Software without restriction, including
6  
-# without limitation the rights to use, copy, modify, merge, publish, dis-
7  
-# tribute, sublicense, and/or sell copies of the Software, and to permit
8  
-# persons to whom the Software is furnished to do so, subject to the fol-
9  
-# lowing conditions:
10  
-#
11  
-# The above copyright notice and this permission notice shall be included
12  
-# in all copies or substantial portions of the Software.
13  
-#
14  
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  
-# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16  
-# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17  
-# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
18  
-# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  
-# IN THE SOFTWARE.
21  
-from boto.sdb.db.key import Key
22  
-from boto.sdb.db.model import Model
23  
-import psycopg2
24  
-import psycopg2.extensions
25  
-import uuid
26  
-import os
27  
-import string
28  
-from boto.exception import SDBPersistenceError
29  
-
30  
-psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
31  
-
32  
-class PGConverter:
33  
-    
34  
-    def __init__(self, manager):
35  
-        self.manager = manager
36  
-        self.type_map = {Key : (self.encode_reference, self.decode_reference),
37  
-                         Model : (self.encode_reference, self.decode_reference)}
38  
-
39  
-    def encode(self, type, value):
40  
-        if type in self.type_map:
41  
-            encode = self.type_map[type][0]
42  
-            return encode(value)
43  
-        return value
44  
-
45  
-    def decode(self, type, value):
46  
-        if type in self.type_map:
47  
-            decode = self.type_map[type][1]
48  
-            return decode(value)
49  
-        return value
50  
-
51  
-    def encode_prop(self, prop, value):
52  
-        if isinstance(value, list):
53  
-            if hasattr(prop, 'item_type'):
54  
-                s = "{"
55  
-                new_value = []
56  
-                for v in value:
57  
-                    item_type = getattr(prop, 'item_type')
58  
-                    if Model in item_type.mro():
59  
-                        item_type = Model
60  
-                    new_value.append('%s' % self.encode(item_type, v))
61  
-                s += ','.join(new_value)
62  
-                s += "}"
63  
-                return s
64  
-            else:
65  
-                return value
66  
-        return self.encode(prop.data_type, value)
67  
-
68  
-    def decode_prop(self, prop, value):
69  
-        if prop.data_type == list:
70  
-            if value != None:
71  
-                if not isinstance(value, list):
72  
-                    value = [value]
73  
-                if hasattr(prop, 'item_type'):
74  
-                    item_type = getattr(prop, "item_type")
75  
-                    if Model in item_type.mro():
76  
-                        if item_type != self.manager.cls:
77  
-                            return item_type._manager.decode_value(prop, value)
78  
-                        else:
79  
-                            item_type = Model
80  
-                    return [self.decode(item_type, v) for v in value]
81  
-            return value
82  
-        elif hasattr(prop, 'reference_class'):
83  
-            ref_class = getattr(prop, 'reference_class')
84  
-            if ref_class != self.manager.cls:
85  
-                return ref_class._manager.decode_value(prop, value)
86  
-            else:
87  
-                return self.decode(prop.data_type, value)
88  
-        elif hasattr(prop, 'calculated_type'):
89  
-            calc_type = getattr(prop, 'calculated_type')
90  
-            return self.decode(calc_type, value)
91  
-        else:
92  
-            return self.decode(prop.data_type, value)
93  
-
94  
-    def encode_reference(self, value):
95  
-        if isinstance(value, str) or isinstance(value, unicode):
96  
-            return value
97  
-        if value == None:
98  
-            return ''
99  
-        else:
100  
-            return value.id
101  
-
102  
-    def decode_reference(self, value):
103  
-        if not value:
104  
-            return None
105  
-        try:
106  
-            return self.manager.get_object_from_id(value)
107  
-        except:
108  
-            raise ValueError('Unable to convert %s to Object' % value)
109  
-
110  
-class PGManager(object):
111  
-
112  
-    def __init__(self, cls, db_name, db_user, db_passwd,
113  
-                 db_host, db_port, db_table, sql_dir, enable_ssl):
114  
-        self.cls = cls
115  
-        self.db_name = db_name
116  
-        self.db_user = db_user
117  
-        self.db_passwd = db_passwd
118  
-        self.db_host = db_host
119  
-        self.db_port = db_port
120  
-        self.db_table = db_table
121  
-        self.sql_dir = sql_dir
122  
-        self.in_transaction = False
123  
-        self.converter = PGConverter(self)
124  
-        self._connect()
125  
-
126  
-    def _build_connect_string(self):
127  
-        cs = 'dbname=%s user=%s password=%s host=%s port=%d'
128  
-        return cs % (self.db_name, self.db_user, self.db_passwd,
129  
-                     self.db_host, self.db_port)
130  
-
131  
-    def _connect(self):
132  
-        self.connection = psycopg2.connect(self._build_connect_string())
133  
-        self.connection.set_client_encoding('UTF8')
134  
-        self.cursor = self.connection.cursor()
135  
-
136  
-    def _object_lister(self, cursor):
137  
-        try:
138  
-            for row in cursor:
139  
-                yield self._object_from_row(row, cursor.description)
140  
-        except StopIteration:
141  
-            cursor.close()
142  
-            raise StopIteration
143  
-                
144  
-    def _dict_from_row(self, row, description):
145  
-        d = {}
146  
-        for i in range(0, len(row)):
147  
-            d[description[i][0]] = row[i]
148  
-        return d
149  
-
150  
-    def _object_from_row(self, row, description=None):
151  
-        if not description:
152  
-            description = self.cursor.description
153  
-        d = self._dict_from_row(row, description)
154  
-        obj = self.cls(d['id'])
155  
-        obj._manager = self
156  
-        obj._auto_update = False
157  
-        for prop in obj.properties(hidden=False):
158  
-            if prop.data_type != Key:
159  
-                v = self.decode_value(prop, d[prop.name])
160  
-                v = prop.make_value_from_datastore(v)
161  
-                if hasattr(prop, 'calculated_type'):
162  
-                    prop._set_direct(obj, v)
163  
-                elif not prop.empty(v):
164  
-                    setattr(obj, prop.name, v)
165  
-                else:
166  
-                    setattr(obj, prop.name, prop.default_value())
167  
-        return obj
168  
-
169  
-    def _build_insert_qs(self, obj, calculated):
170  
-        fields = []
171  
-        values = []
172  
-        templs = []
173  
-        id_calculated = [p for p in calculated if p.name == 'id']
174  
-        for prop in obj.properties(hidden=False):
175  
-            if prop not in calculated:
176  
-                value = prop.get_value_for_datastore(obj)
177  
-                if value != prop.default_value() or prop.required:
178  
-                    value = self.encode_value(prop, value)
179  
-                    values.append(value)
180  
-                    fields.append('"%s"' % prop.name)
181  
-                    templs.append('%s')
182  
-        qs = 'INSERT INTO "%s" (' % self.db_table
183  
-        if len(id_calculated) == 0:
184  
-            qs += '"id",'
185  
-        qs += ','.join(fields)
186  
-        qs += ") VALUES ("
187  
-        if len(id_calculated) == 0:
188  
-            qs += "'%s'," % obj.id
189  
-        qs += ','.join(templs)
190  
-        qs += ')'
191  
-        if calculated:
192  
-            qs += ' RETURNING '
193  
-            calc_values = ['"%s"' % p.name for p in calculated]
194  
-            qs += ','.join(calc_values)
195  
-        qs += ';'
196  
-        return qs, values
197  
-
198  
-    def _build_update_qs(self, obj, calculated):
199  
-        fields = []
200  
-        values = []
201  
-        for prop in obj.properties(hidden=False):
202  
-            if prop not in calculated:
203  
-                value = prop.get_value_for_datastore(obj)
204  
-                if value != prop.default_value() or prop.required:
205  
-                    value = self.encode_value(prop, value)
206  
-                    values.append(value)
207  
-                    field = '"%s"=' % prop.name
208  
-                    field += '%s'
209  
-                    fields.append(field)
210  
-        qs = 'UPDATE "%s" SET ' % self.db_table
211  
-        qs += ','.join(fields)
212  
-        qs += """ WHERE "id" = '%s'""" % obj.id
213  
-        if calculated:
214  
-            qs += ' RETURNING '
215  
-            calc_values = ['"%s"' % p.name for p in calculated]
216  
-            qs += ','.join(calc_values)
217  
-        qs += ';'
218  
-        return qs, values
219  
-
220  
-    def _get_sql(self, mapping=None):
221  
-        print '_get_sql'
222  
-        sql = None
223  
-        if self.sql_dir:
224  
-            path = os.path.join(self.sql_dir, self.cls.__name__ + '.sql')
225  
-            print path
226  
-            if os.path.isfile(path):
227  
-                fp = open(path)
228  
-                sql = fp.read()
229  
-                fp.close()
230  
-                t = string.Template(sql)
231  
-                sql = t.safe_substitute(mapping)
232  
-        return sql
233  
-
234  
-    def start_transaction(self):
235  
-        print 'start_transaction'
236  
-        self.in_transaction = True
237  
-
238  
-    def end_transaction(self):
239  
-        print 'end_transaction'
240  
-        self.in_transaction = False
241  
-        self.commit()
242  
-
243  
-    def commit(self):
244  
-        if not self.in_transaction:
245  
-            print '!!commit on %s' % self.db_table
246  
-            try:
247  
-                self.connection.commit()
248  
-                
249  
-            except psycopg2.ProgrammingError, err:
250  
-                self.connection.rollback()
251  
-                raise err
252  
-
253  
-    def rollback(self):
254  
-        print '!!rollback on %s' % self.db_table
255  
-        self.connection.rollback()
256  
-
257  
-    def delete_table(self):
258  
-        self.cursor.execute('DROP TABLE "%s";' % self.db_table)
259  
-        self.commit()
260  
-
261  
-    def create_table(self, mapping=None):
262  
-        self.cursor.execute(self._get_sql(mapping))
263  
-        self.commit()
264  
-
265  
-    def encode_value(self, prop, value):
266  
-        return self.converter.encode_prop(prop, value)
267  
-
268  
-    def decode_value(self, prop, value):
269  
-        return self.converter.decode_prop(prop, value)
270  
-
271  
-    def execute_sql(self, query):
272  
-        self.cursor.execute(query, None)
273  
-        self.commit()
274  
-
275  
-    def query_sql(self, query, vars=None):
276  
-        self.cursor.execute(query, vars)
277  
-        return self.cursor.fetchall()
278  
-
279  
-    def lookup(self, cls, name, value):
280  
-        values = []
281  
-        qs = 'SELECT * FROM "%s" WHERE ' % self.db_table
282  
-        found = False
283  
-        for property in cls.properties(hidden=False):
284  
-            if property.name == name:
285  
-                found = True
286  
-                value = self.encode_value(property, value)
287  
-                values.append(value)
288  
-                qs += "%s=" % name
289  
-                qs += "%s"
290  
-        if not found:
291  
-            raise SDBPersistenceError('%s is not a valid field' % name)
292  
-        qs += ';'
293  
-        print qs
294  
-        self.cursor.execute(qs, values)
295  
-        if self.cursor.rowcount == 1:
296  
-            row = self.cursor.fetchone()
297  
-            return self._object_from_row(row, self.cursor.description)
298  
-        elif self.cursor.rowcount == 0:
299  
-            raise KeyError('Object not found')
300  
-        else:
301  
-            raise LookupError('Multiple Objects Found')
302  
-
303  
-    def query(self, cls, filters, limit=None, order_by=None):
304  
-        parts = []
305  
-        qs = 'SELECT * FROM "%s"' % self.db_table
306  
-        if filters:
307  
-            qs += ' WHERE '
308  
-            properties = cls.properties(hidden=False)
309  
-            for filter, value in filters:
310  
-                name, op = filter.strip().split()
311  
-                found = False
312  
-                for property in properties:
313  
-                    if property.name == name:
314  
-                        found = True
315  
-                        value = self.encode_value(property, value)
316  
-                        parts.append(""""%s"%s'%s'""" % (name, op, value))
317  
-                if not found:
318  
-                    raise SDBPersistenceError('%s is not a valid field' % name)
319  
-            qs += ','.join(parts)
320  
-        qs += ';'
321  
-        print qs
322  
-        cursor = self.connection.cursor()
323  
-        cursor.execute(qs)
324  
-        return self._object_lister(cursor)
325  
-
326  
-    def get_property(self, prop, obj, name):
327  
-        qs = """SELECT "%s" FROM "%s" WHERE id='%s';""" % (name, self.db_table, obj.id)
328  
-        print qs
329  
-        self.cursor.execute(qs, None)
330  
-        if self.cursor.rowcount == 1:
331  
-            rs = self.cursor.fetchone()
332  
-            for prop in obj.properties(hidden=False):
333  
-                if prop.name == name:
334  
-                    v = self.decode_value(prop, rs[0])
335  
-                    return v
336  
-        raise AttributeError('%s not found' % name)
337  
-
338  
-    def set_property(self, prop, obj, name, value):
339  
-        pass
340  
-        value = self.encode_value(prop, value)
341  
-        qs = 'UPDATE "%s" SET ' % self.db_table
342  
-        qs += "%s='%s'" % (name, self.encode_value(prop, value))
343  
-        qs += " WHERE id='%s'" % obj.id
344  
-        qs += ';'
345  
-        print qs
346  
-        self.cursor.execute(qs)
347  
-        self.commit()
348  
-
349  
-    def get_object(self, cls, id):
350  
-        qs = """SELECT * FROM "%s" WHERE id='%s';""" % (self.db_table, id)
351  
-        self.cursor.execute(qs, None)
352  
-        if self.cursor.rowcount == 1:
353  
-            row = self.cursor.fetchone()
354  
-            return self._object_from_row(row, self.cursor.description)
355  
-        else:
356  
-            raise SDBPersistenceError('%s object with id=%s does not exist' % (cls.__name__, id))
357  
-        
358  
-    def get_object_from_id(self, id):
359  
-        return self.get_object(self.cls, id)
360  
-
361  
-    def _find_calculated_props(self, obj):
362  
-        return [p for p in obj.properties() if hasattr(p, 'calculated_type')]
363  
-
364  
-    def save_object(self, obj, expected_value=None):
365  
-        obj._auto_update = False
366  
-        calculated = self._find_calculated_props(obj)
367  
-        if not obj.id:
368  
-            obj.id = str(uuid.uuid4())
369  
-            qs, values = self._build_insert_qs(obj, calculated)
370  
-        else:
371  
-            qs, values = self._build_update_qs(obj, calculated)
372  
-        print qs
373  
-        self.cursor.execute(qs, values)
374  
-        if calculated:
375  
-            calc_values = self.cursor.fetchone()
376  
-            print calculated
377  
-            print calc_values
378  
-            for i in range(0, len(calculated)):
379  
-                prop = calculated[i]
380  
-                prop._set_direct(obj, calc_values[i])
381  
-        self.commit()
382  
-
383  
-    def delete_object(self, obj):
384  
-        qs = """DELETE FROM "%s" WHERE id='%s';""" % (self.db_table, obj.id)
385  
-        print qs
386  
-        self.cursor.execute(qs)
387  
-        self.commit()
388  
-
389  
-            

0 notes on commit 3db8668

Please sign in to comment.
Something went wrong with that request. Please try again.