Skip to content

Commit

Permalink
Cleaner implementation for select(), raw_query() and AsyncQueryWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
rudyryk committed Apr 18, 2016
1 parent 37a49a4 commit 1b7344f
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions peewee_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,9 @@ def select(query):
("Error, trying to run select coroutine"
"with wrong query class %s" % str(query))

result = AsyncQueryWrapper(query)
cursor = yield from result.execute()
cursor = yield from _execute_query_async(query)

result = AsyncQueryWrapper(cursor=cursor, query=query)
try:
while True:
yield from result.fetchone()
Expand Down Expand Up @@ -585,7 +585,6 @@ def scalar(query, as_tuple=False):
row = yield from cursor.fetchone()

cursor.release()

if row and not as_tuple:
return row[0]
else:
Expand All @@ -598,9 +597,9 @@ def raw_query(query):
("Error, trying to run delete coroutine"
"with wrong query class %s" % str(query))

result = AsyncRawQueryWrapper(query)
cursor = yield from result.execute()
cursor = yield from _execute_query_async(query)

result = AsyncRawQueryWrapper(cursor=cursor, query=query)
try:
while True:
yield from result.fetchone()
Expand Down Expand Up @@ -678,10 +677,9 @@ class AsyncQueryWrapper:
To retrieve results after async fetching just iterate over this class
instance, like you generally iterate over sync results wrapper.
"""
def __init__(self, query):
def __init__(self, *, cursor=None, query=None):
self._initialized = False
self._cursor = None
self._query = query
self._cursor = cursor
self._result = []
self._result_wrapper = self._get_result_wrapper(query)

Expand Down Expand Up @@ -711,11 +709,6 @@ def _get_result_wrapper(self, query):

return QRW(query.model_class, None, query.get_query_meta())

@asyncio.coroutine
def execute(self):
self._cursor = yield from _execute_query_async(self._query)
return self._cursor

@asyncio.coroutine
def fetchone(self):
row = yield from self._cursor.fetchone()
Expand Down

0 comments on commit 1b7344f

Please sign in to comment.