Skip to content

Commit 0d6d9c1

Browse files
committed
FIX: VALUES syntax deprecation regex
1 parent a22f6b3 commit 0d6d9c1

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

asyncmy/cursors.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ from . import errors
1111
RE_INSERT_VALUES = re.compile(
1212
r"\s*((?:INSERT|REPLACE)\b.+\bVALUES?\s*)"
1313
+ r"(\(\s*(?:%s|%\(.+\)s)\s*(?:,\s*(?:%s|%\(.+\)s)\s*)*\))"
14-
+ r"(\s*(?:ON DUPLICATE.*)?);?\s*\Z",
14+
+ r"(\s*(?:(?:AS|ON DUPLICATE).*)?);?\s*\Z",
1515
re.IGNORECASE | re.DOTALL,
1616
)
1717
logger = logging.getLogger(__package__)

tests/test_cursor.py

+39-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pytest
66

7-
from asyncmy.cursors import DictCursor
7+
from asyncmy.cursors import DictCursor, RE_INSERT_VALUES
88

99

1010
@pytest.mark.asyncio
@@ -140,3 +140,41 @@ async def test_insert_enum(connection):
140140
),
141141
)
142142
assert rows == 1
143+
144+
145+
def test_executemany_regex():
146+
query = "INSERT INTO foo (bar, baz) VALUES (%s, %s)"
147+
match = RE_INSERT_VALUES.match(query)
148+
assert match is not None
149+
assert match.group(1) == "INSERT INTO foo (bar, baz) VALUES "
150+
assert match.group(2) == "(%s, %s)"
151+
assert match.group(3) == ""
152+
153+
# Deprecated VALUES syntax
154+
query = "INSERT INTO foo (bar, baz) VALUES (%s, %s) ON DUPLICATE KEY UPDATE baz=VALUES(baz)"
155+
match = RE_INSERT_VALUES.match(query)
156+
assert match is not None
157+
assert match.group(1) == "INSERT INTO foo (bar, baz) VALUES "
158+
assert match.group(2) == "(%s, %s)"
159+
assert match.group(3) == " ON DUPLICATE KEY UPDATE baz=VALUES(baz)"
160+
161+
# The new syntax
162+
query = "INSERT INTO foo (bar, baz) VALUES (%s, %s) AS new ON DUPLICATE KEY UPDATE baz=new.baz"
163+
match = RE_INSERT_VALUES.match(query)
164+
assert match is not None
165+
assert match.group(1) == "INSERT INTO foo (bar, baz) VALUES "
166+
assert match.group(2) == "(%s, %s)"
167+
assert match.group(3) == " AS new ON DUPLICATE KEY UPDATE baz=new.baz"
168+
169+
# Test REPLACE VALUES
170+
query = "REPLACE INTO foo (bar, baz, aa, bb) VALUES (%s, %s, %s, %s)"
171+
match = RE_INSERT_VALUES.match(query)
172+
assert match is not None
173+
assert match.group(1) == "REPLACE INTO foo (bar, baz, aa, bb) VALUES "
174+
assert match.group(2) == "(%s, %s, %s, %s)"
175+
assert match.group(3) == ""
176+
177+
# Test invalid queries
178+
query = "INSERT INTO foo (bar, baz) VALUES (%s, %s), (%s, %s)"
179+
match = RE_INSERT_VALUES.match(query)
180+
assert match is None

0 commit comments

Comments
 (0)