Skip to content

Commit

Permalink
Merge pull request #4 from horpto/feature/playhouse
Browse files Browse the repository at this point in the history
Draft playhouse extension support
  • Loading branch information
rudyryk committed Jun 6, 2015
2 parents 89bfd74 + 322c703 commit 4004839
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
28 changes: 26 additions & 2 deletions peewee_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def fetchone(self):
self._result.append(obj)


class PostgresqlDatabase(peewee.PostgresqlDatabase):
class _PostgresqlDatabase:
""" PosgreSQL database driver providing **single drop-in sync** connection
and **single async connection** interface.
Expand Down Expand Up @@ -414,8 +414,22 @@ def last_insert_id_async(self, cursor, model):
result = yield from cursor.fetchone()
return result

def PostgresqlDatabase(database, threadlocals=True, autocommit=True,
fields=None, ops=None, autorollback=True,
database_cls=peewee.PostgresqlDatabase, **connect_kwargs):
if not issubclass(database_cls, peewee.PostgresqlDatabase):
raise TypeError("database_cls parameter should be peewee.PostgresqlDatabase instance")

class PooledPostgresqlDatabase(PostgresqlDatabase):
class PostgresqlDatabase(_PostgresqlDatabase, database_cls):
pass

return PostgresqlDatabase(database, threadlocals=True, autocommit=True,
fields=None, ops=None, autorollback=True,
**connect_kwargs)



class _PooledPostgresqlDatabase(_PostgresqlDatabase):
""" PosgreSQL database driver providing **single drop-in sync** connection
and **async connections pool** interface.
Expand Down Expand Up @@ -452,6 +466,16 @@ def connect_async(self, loop=None, timeout=None):
yield from self.async_conn.connect()


def PooledPostgresqlDatabase(*args, database_cls=peewee.PostgresqlDatabase, **kwargs):
if not issubclass(database_cls, peewee.PostgresqlDatabase):
raise TypeError("database_cls parameter should be peewee.PostgresqlDatabase instance")

class PooledPostgresqlDatabase(_PooledPostgresqlDatabase, database_cls):
pass

return PostgresqlDatabase(*args, **kwargs)


class AsyncPostgresDatabase:
"""
Asynchronous single connection database interface.
Expand Down
8 changes: 6 additions & 2 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@
config = {}

config.setdefault('db', 'test')
config.setdefault('user', 'postgres')
config.setdefault('password', '')

if 'pool_size' in config:
max_connections = int(config['pool_size'])
database = peewee_async.PooledPostgresqlDatabase(config['db'], max_connections=max_connections)
database = peewee_async.PooledPostgresqlDatabase(config['db'], max_connections=max_connections,
user=config['user'], password=config['password'])
else:
database = peewee_async.PostgresqlDatabase(config['db'])
database = peewee_async.PostgresqlDatabase(config['db'],
user=config['user'], password=config['password'])


#
Expand Down

0 comments on commit 4004839

Please sign in to comment.