]> git.saurik.com Git - cyql.git/commitdiff
Cache the information about OIDs.
authorJay Freeman (saurik) <saurik@saurik.com>
Tue, 5 Feb 2013 07:34:06 +0000 (07:34 +0000)
committerJay Freeman (saurik) <saurik@saurik.com>
Tue, 5 Feb 2013 07:34:06 +0000 (07:34 +0000)
__init__.py

index f4e6b9fe4773ffd780b564e20314a0cf222217c5..ca9ae63228ae4e0ee95d1d0e851c8b465e2b346f 100644 (file)
@@ -19,10 +19,23 @@ psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
 
 class connect(object):
     def __init__(self, dsn):
+        options = dsn.copy()
+        if 'cache' in options:
+            del options['cache']
+
+        if 'cache' in dsn:
+            cached = True
+            cache = dsn['cache']
+        else:
+            cached = False
+            cache = {
+                'hstore': None,
+            }
+
         attempt = 0
         while True:
             try:
-                self.driver = psycopg2.connect(**dsn)
+                self.driver = psycopg2.connect(**options)
                 break
             except psycopg2.OperationalError, e:
                 if attempt == 2:
@@ -38,10 +51,20 @@ class connect(object):
         #    self.driver.close()
         #    raise
 
-        try:
-            psycopg2.extras.register_hstore(self.driver, globally=False, unicode=True)
-        except psycopg2.ProgrammingError, e:
-            pass
+        hstore = cache['hstore']
+        if hstore == None:
+            hstore = psycopg2.extras.HstoreAdapter.get_oids(self.driver)
+            if hstore != None:
+                hstore = hstore[0]
+
+        if hstore != None:
+            try:
+                psycopg2.extras.register_hstore(self.driver, globally=False, unicode=True, oid=hstore)
+            except psycopg2.ProgrammingError, e:
+                pass
+
+        if not cached:
+            dsn['cache'] = cache
 
     def close(self):
         self.driver.close()