-
Notifications
You must be signed in to change notification settings - Fork 150
Fixed #14, implemented prepared statement #219
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| from datetime import datetime | ||
|
|
||
| import pytest | ||
| from .models import db, User | ||
|
|
||
| pytestmark = pytest.mark.asyncio | ||
|
|
||
|
|
||
| async def test_compiled_and_bindparam(bind): | ||
| async with db.acquire() as conn: | ||
| # noinspection PyArgumentList | ||
| ins = await conn.prepare(User.insert().returning( | ||
| *User).execution_options(loader=User)) | ||
| users = {} | ||
| for name in '12345': | ||
| u = await ins.first(nickname=name) | ||
| assert u.nickname == name | ||
| users[u.id] = u | ||
| get = await conn.prepare( | ||
| User.query.where(User.id == db.bindparam('uid'))) | ||
| for key in users: | ||
| u = await get.first(uid=key) | ||
| assert u.nickname == users[key].nickname | ||
| assert (await get.all(uid=key))[0].nickname == u.nickname | ||
|
|
||
| assert await get.scalar(uid=-1) is None | ||
|
|
||
| with pytest.raises(ValueError, match='does not support multiple'): | ||
| await get.all([dict(uid=1), dict(uid=2)]) | ||
|
|
||
| delete = await conn.prepare( | ||
| User.delete.where(User.nickname == db.bindparam('name'))) | ||
| for name in '12345': | ||
| msg = await delete.status(name=name) | ||
| assert msg == 'DELETE 1' | ||
|
|
||
|
|
||
| async def test_statement(engine): | ||
| async with engine.acquire() as conn: | ||
| stmt = await conn.prepare('SELECT now()') | ||
| last = None | ||
| for i in range(5): | ||
| now = await stmt.scalar() | ||
| assert isinstance(now, datetime) | ||
| assert last != now | ||
| last = now |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is there such limitation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because asyncpg doesn't have it on prepared statement. User who wants to use
executemany()with prepared statement has to fall back to sequentialall()calls, or not to use prepared statement to save RTT.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thx for the info. I looked up, and it seems to be due to PostgreSQL.
(I saw some "workaround" https://stackoverflow.com/a/14903130/238634 but it'd make it too complex)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm it's more complicated than I thought.
First of all, current
executemany()is not saving RTT, it is only saving some slow Python time: MagicStack/asyncpg#36And yes, extended query protocol (video) does not support multiple SQL statements, but what we need is actually multiple
BindandExecutecommands, ideally issued in a batch. Under the hood prepared statement is actually named statement of extended query, so it should be possible to use prepared statement withexecutemany(), asyncpg just didn't expose the interface.So for the first issue, I tried to tweak asyncpg a bit to send
BindandExecutein one packet, it just worked fine. And for the second issue, it won't be hard to addexecutemany()in prepared statement I suppose (commit).As for this PR, guess I'll leave as it is now, and note about upstream.