Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion asyncmy/cursors.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ from . import errors
RE_INSERT_VALUES = re.compile(
r"\s*((?:INSERT|REPLACE)\b.+\bVALUES?\s*)"
+ r"(\(\s*(?:%s|%\(.+\)s)\s*(?:,\s*(?:%s|%\(.+\)s)\s*)*\))"
+ r"(\s*(?:ON DUPLICATE.*)?);?\s*\Z",
+ r"(\s*(?:(?:AS|ON DUPLICATE).*)?);?\s*\Z",
re.IGNORECASE | re.DOTALL,
)
logger = logging.getLogger(__package__)
Expand Down
40 changes: 39 additions & 1 deletion tests/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from asyncmy.cursors import DictCursor
from asyncmy.cursors import DictCursor, RE_INSERT_VALUES


@pytest.mark.asyncio
Expand Down Expand Up @@ -140,3 +140,41 @@ async def test_insert_enum(connection):
),
)
assert rows == 1


def test_executemany_regex():
query = "INSERT INTO foo (bar, baz) VALUES (%s, %s)"
match = RE_INSERT_VALUES.match(query)
assert match is not None
assert match.group(1) == "INSERT INTO foo (bar, baz) VALUES "
assert match.group(2) == "(%s, %s)"
assert match.group(3) == ""

# Deprecated VALUES syntax
query = "INSERT INTO foo (bar, baz) VALUES (%s, %s) ON DUPLICATE KEY UPDATE baz=VALUES(baz)"
match = RE_INSERT_VALUES.match(query)
assert match is not None
assert match.group(1) == "INSERT INTO foo (bar, baz) VALUES "
assert match.group(2) == "(%s, %s)"
assert match.group(3) == " ON DUPLICATE KEY UPDATE baz=VALUES(baz)"

# The new syntax
query = "INSERT INTO foo (bar, baz) VALUES (%s, %s) AS new ON DUPLICATE KEY UPDATE baz=new.baz"
match = RE_INSERT_VALUES.match(query)
assert match is not None
assert match.group(1) == "INSERT INTO foo (bar, baz) VALUES "
assert match.group(2) == "(%s, %s)"
assert match.group(3) == " AS new ON DUPLICATE KEY UPDATE baz=new.baz"

# Test REPLACE VALUES
query = "REPLACE INTO foo (bar, baz, aa, bb) VALUES (%s, %s, %s, %s)"
match = RE_INSERT_VALUES.match(query)
assert match is not None
assert match.group(1) == "REPLACE INTO foo (bar, baz, aa, bb) VALUES "
assert match.group(2) == "(%s, %s, %s, %s)"
assert match.group(3) == ""

# Test invalid queries
query = "INSERT INTO foo (bar, baz) VALUES (%s, %s), (%s, %s)"
match = RE_INSERT_VALUES.match(query)
assert match is None