Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support executemany #324

Merged
merged 10 commits into from Dec 1, 2018
Merged

fix: support executemany #324

merged 10 commits into from Dec 1, 2018

Conversation

zeromake
Copy link
Contributor

run example

error: aiomysql doesn't support executemany

import asyncio
import sqlalchemy as sa
from sqlalchemy.sql.ddl import CreateTable, DropTable
from aiomysql.sa import create_engine
from sqlalchemy.sql.expression import bindparam

metadata = sa.MetaData()
User = sa.Table(
    'user',
    metadata,
    sa.Column(
        'id',
        sa.Integer,
        autoincrement=True,
        primary_key=True,
        nullable=False,
    ),
    sa.Column(
        'name',
        sa.String(16),
        nullable=False,
    )
)

async def test_executemany(loop):
    engine = await create_engine(
         user="aiomysql",
         db="test_pymysql",
         host="127.0.0.1",
         password="mypass",
         port=3306,
         loop=loop,
         echo=True,
    )
    async with engine.acquire() as conn:
        await conn.execute(CreateTable(User))
        await conn.execute(User.insert().values([{"name": "test1"}, {"name": "test2"}]))
        async with conn.execute(User.select()) as cursor:
            user = await cursor.fetchone()
            assert user.id == 1
            assert user.name == "test1"
            user = await cursor.fetchone()
            assert user.id == 2
            assert user.name == "test2"
        sql = User.update().where(User.c.id == bindparam("id")).values({"name": bindparam("name")})
        await conn.execute(sql, [{"id": 1, "name": "t1"}, {"id": 2, "name": "t2"}])
        async with conn.execute(User.select()) as cursor:
            user = await cursor.fetchone()
            assert user.id == 1
            assert user.name == "t1"
            user = await cursor.fetchone()
            assert user.id == 2
            assert user.name == "t2"


loop = asyncio.get_event_loop()
loop.run_until_complete(test_executemany(loop))

@codecov
Copy link

codecov bot commented Jul 27, 2018

Codecov Report

Merging #324 into master will increase coverage by 0.11%.
The diff coverage is 93.75%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #324      +/-   ##
==========================================
+ Coverage   92.91%   93.02%   +0.11%     
==========================================
  Files           9        9              
  Lines        1129     1147      +18     
  Branches      161      164       +3     
==========================================
+ Hits         1049     1067      +18     
  Misses         56       56              
  Partials       24       24
Impacted Files Coverage Δ
aiomysql/sa/connection.py 93.26% <93.75%> (+0.69%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5c42790...312b352. Read the comment docs.

@terrycain
Copy link
Collaborator

@jettify your knowledge of sqlalchemy exceeds mine so you probably best suited to review this. I had a look over the diff and it looks sane though.

@ZmingZinnia
Copy link

same requirements

aiomysql/sa/connection.py Outdated Show resolved Hide resolved
aiomysql/sa/connection.py Outdated Show resolved Hide resolved
Copy link
Member

@jettify jettify left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks a lot!

@jettify jettify merged commit 9e48e85 into aio-libs:master Dec 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants