Skip to content

Refactor to (a) get rid of cursors, (b) have a nicer interface #2

Open
@smurfix

Description

@smurfix

The problem is that mysql doesn't have cursors, so why would you want to fake them?

In an async world you don't need to expose cursors to the user anyway. You want to simply call

    async with trio_mysql.connect("localhost") as db:
        async with db.query("select a,b,c …") as result
            async for a,b,c in result:
                await process_record(a,b,c)

instead. SO, none of that fetch_one, fetch_many or fetch_dict stuff – let the record be a named tuple instead, and/or add some options that describe how the result should look like.

If process needs to run a query (like an update), well, you need to save the not-yet-read results in memory – which you should not do when that is not necessary.

Also, cancelling a query requires sending a "kill" command to the server if not all results have been read (if any). The mysql command line client can do that: if you interrupt a query before it returns any result it asks you whether you want to.

Also, mysql now supports nested transactions. This adapter should support them. It should also refuse to accept queries on the connection (or outer transactions) when a(n inner) transaction is active, i.e.

    async with trio_mysql.connect("localhost") as db:
        await db.query("insert into test values(1)")  # works, auto-commits
        async with db.transaction() as tr:
            await tr.query("insert into test values(2)")  # works
            await db.query("insert into test values(3)")  # raises an error

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions