Skip to content

Commit

Permalink
Simplify internal PostgreSQL driver hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
rudyryk committed Apr 19, 2016
1 parent 4345bce commit 98fd2bd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 43 deletions.
4 changes: 2 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ In current version (0.5.x) new-high level API is introduced while older low-leve
* Works on Python 3.4+
* Has support for PostgreSQL via `aiopg`
* Has support for MySQL via `aiomysql`
* Single point high level async API
* Single point high-level async API
* Drop-in replacement for sync code, sync will remain sync
* Basic operations are supported
* Transactions support is present, yet not heavily tested
Expand Down Expand Up @@ -74,7 +74,7 @@ Quickstart
with objects.allow_sync():
TestModel.drop_table(True)
# Expected output:"
# Expected output:
# Yo, I can do it sync!
# Not bad. Watch this, I'm async!
Expand Down
37 changes: 9 additions & 28 deletions peewee_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ def execute_sql(self, *args, **kwargs):
##############


class AsyncPostgresqlConnectionPool:
class AsyncPostgresqlConnection:
"""Asynchronous database connection pool.
"""
def __init__(self, *, database=None, loop=None, timeout=None, **kwargs):
Expand Down Expand Up @@ -1006,16 +1006,6 @@ def close(self):
yield from self.pool.wait_closed()


class AsyncPostgresqlConnection(AsyncPostgresqlConnectionPool):
"""Asynchronous single database connection.
"""
def __init__(self, *, database=None, loop=None, timeout=None, **kwargs):
kwargs['minsize'] = 1
kwargs['maxsize'] = 1
super().__init__(database=database, loop=loop, timeout=timeout,
**kwargs)


class AsyncPostgresqlMixin(AsyncDatabase):
"""Mixin for `peewee.PostgresqlDatabase` providing extra methods
for managing async connection.
Expand All @@ -1025,11 +1015,9 @@ class AsyncPostgresqlMixin(AsyncDatabase):
Error = psycopg2.Error

def init_async(self, conn_cls=AsyncPostgresqlConnection,
enable_json=False,
enable_hstore=False):
enable_json=False, enable_hstore=False):
if not aiopg:
raise Exception("Error, aiopg is not installed!")

self._async_conn_cls = conn_cls
self._enable_json = enable_json
self._enable_hstore = enable_hstore
Expand All @@ -1040,6 +1028,8 @@ def connect_kwargs_async(self):
"""
kwargs = self.connect_kwargs.copy()
kwargs.update({
'minsize': self.min_connections,
'maxsize': self.max_connections,
'enable_json': self._enable_json,
'enable_hstore': self._enable_hstore,
})
Expand Down Expand Up @@ -1082,6 +1072,8 @@ class PostgresqlDatabase(AsyncPostgresqlMixin, peewee.PostgresqlDatabase):
http://peewee.readthedocs.org/en/latest/peewee/api.html#PostgresqlDatabase
"""
def init(self, database, **kwargs):
self.min_connections = 1
self.max_connections = 1
super().init(database, **kwargs)
self.init_async()

Expand All @@ -1100,21 +1092,10 @@ class PooledPostgresqlDatabase(AsyncPostgresqlMixin, peewee.PostgresqlDatabase):
http://peewee.readthedocs.org/en/latest/peewee/api.html#PostgresqlDatabase
"""
def init(self, database, **kwargs):
self.min_connections = kwargs.pop('min_connections', 1)
self.max_connections = kwargs.pop('max_connections', 20)
super().init(database, **kwargs)
self.init_async(conn_cls=AsyncPostgresqlConnectionPool)
self.min_connections = self.connect_kwargs.pop('min_connections', 1)
self.max_connections = self.connect_kwargs.pop('max_connections', 20)

@property
def connect_kwargs_async(self):
"""Connection parameters for `aiopg.Pool`
"""
kwargs = super().connect_kwargs_async
kwargs.update({
'minsize': self.min_connections,
'maxsize': self.max_connections,
})
return kwargs
self.init_async()


#########
Expand Down
17 changes: 4 additions & 13 deletions peewee_asyncext.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class PostgresqlExtDatabase(AsyncPostgresqlMixin, ext.PostgresqlExtDatabase):
https://peewee.readthedocs.org/en/latest/peewee/playhouse.html#PostgresqlExtDatabase
"""
def init(self, database, **kwargs):
self.min_connections = 1
self.max_connections = 1
super().init(database, **kwargs)
self.init_async(enable_json=True,
enable_hstore=self.register_hstore)
Expand All @@ -55,19 +57,8 @@ class PooledPostgresqlExtDatabase(AsyncPostgresqlMixin, ext.PostgresqlExtDatabas
https://peewee.readthedocs.org/en/latest/peewee/playhouse.html#PostgresqlExtDatabase
"""
def init(self, database, **kwargs):
self.min_connections = kwargs.pop('min_connections', 1)
self.max_connections = kwargs.pop('max_connections', 20)
super().init(database, **kwargs)
self.init_async(enable_json=True,
enable_hstore=self.register_hstore)
self.min_connections = self.connect_kwargs.pop('min_connections', 1)
self.max_connections = self.connect_kwargs.pop('max_connections', 20)

@property
def connect_kwargs_async(self):
"""Connection parameters for `aiopg.Pool`
"""
kwargs = super().connect_kwargs_async
kwargs.update({
'minsize': self.min_connections,
'maxsize': self.max_connections,
})
return kwargs

0 comments on commit 98fd2bd

Please sign in to comment.