CanopyTax / asyncpgsa

A wrapper around asyncpg for use with sqlalchemy
Apache License 2.0
417 stars 46 forks source link

Cursors are not supported(?) with the pool.transaction() interface #68

Open thedrow opened 6 years ago

thedrow commented 6 years ago

I have declared a connection pool and tried to call cursor like so:

pool = asyncpgsa.create_pool()
async with pool.transaction() as t:
  async with t.query("SELECT * FROM whatever") as cursor:
    # ...

The query() method is currently missing. It might be a documentation problem though.

nhumrich commented 6 years ago

oh. Ya query got removed as it has weird behavior and didnt work as people thought. So this is mostly a documentation issue.

thedrow commented 6 years ago

So how do I create a cursor?

nhumrich commented 6 years ago
async with pool.transaction() as t:
    async with t.cursor("SELECT  ...") as cursor:
        ...

https://github.com/CanopyTax/asyncpgsa/blob/master/asyncpgsa/connection.py#L110

thedrow commented 6 years ago

For some reason I'm getting a Cursor object and not a CursorIterator so it errors on missing __aiter__ when I do async for row in cursor. The example you gave errors with AttributeError: __aexit__...

nhumrich commented 6 years ago

@thedrow Hey. Sorry about this, it looks likes cursors changed how they work in asyncpg. Can you reference this: https://github.com/MagicStack/asyncpg/blob/master/docs/api/index.rst#cursors

and use cursors that way? I will try adding tests for cursor support soon when I get time. But hopefully that should work.

basically


cur = await con.cursor('select ...')
await cur.forward(10) # move cursor ahead 10 rows
print(await cur.fetchrow()) # get a single row
print(await cur.fetch(5))  # get 5 rows```
nhumrich commented 6 years ago

actually it looks like this was my fault in telling you async with but it should actually be async for

async with pool.transaction() as t:
    async for record in t.cursor("SELECT  ..."):
        ...
nhumrich commented 6 years ago

I will gladly accept PR's that fix the documentation on this subject :)

thedrow commented 6 years ago

I did exactly as you said. I get a Cursor. Not a CursorIterator for some reason.

nhumrich commented 6 years ago

@thedrow can you try this on just asyncpg? what object does it give you? a cursoriterator?

thedrow commented 6 years ago

I'll do so shortly :)