Commits

Jacek Bzdak  committed 1547144

Fixes for #16 and #17.

  • Participants
  • Parent commits fde6d7e

Comments (0)

Files changed (2)

     mysql=shove.stores.db:DBStore
     oracle=shove.stores.db:DBStore
     postgres=shove.stores.db:DBStore
+    postgresql+psycopg2=shove.stores.db:DBStore
+    nullpool:postgresql+psycopg2=shove.stores.db:NullPoolDBStore
     redis=shove.stores.redisdb:RedisStore
     s3=shove.stores.s3:S3Store
     simple=shove.store:SimpleStore

File shove/stores/db.py

 from stuf.six import native
 try:
     from sqlalchemy import LargeBinary as Binary
+    from sqlalchemy.pool import NullPool
 except ImportError:
     from sqlalchemy import Binary
 try:
-    from sqlalchemy import MetaData, Table, Column, String, select
+    from sqlalchemy import MetaData, Table, Column, String, select, create_engine
+    from sqlalchemy.pool import NullPool
 except ImportError:
     raise ImportError('requires SQLAlchemy >= 0.4')
 
 
     '''Relational database-based object storage frontend.'''
 
+    def _create_engine(self, engine_url):
+        return engine_url
+
     def __init__(self, engine, **kw):
         super(DBStore, self).__init__(engine, **kw)
         # make store table
+        self._engine = self._create_engine(engine)
         self._store = Table(
             # get tablename
             kw.get('tablename', 'store'),
-            MetaData(engine),
+            MetaData(self._engine),
             Column('key', String(255), primary_key=True, nullable=False),
             Column('value', Binary, nullable=False),
         )
             self._store.create()
 
     def __getitem__(self, key):
-        row = select(
+        row = self._engine.execute(select(
             [self._store.c.value], self._store.c.key == key,
-        ).execute().fetchone()
+        )).fetchone()
         if row is not None:
             return self.loads(native(row.value))
         raise KeyError(key)
 
     def clear(self):
         self._store.delete().execute()
+
+class NullPoolDBStore(DBStore):
+
+    __prefix = "nullpool:"
+
+    def _create_engine(self, engine_url):
+        if engine_url.startswith(self.__prefix):
+            engine_url = engine_url[len(self.__prefix):]
+        return create_engine(engine_url, poolclass=NullPool)