Skip to content

Commit 9953e50

Browse files
Steve Teahanmethane
Steve Teahan
andauthored
Add Cursor.mogrify(). (#477)
Implements #476 Co-authored-by: Inada Naoki <[email protected]>
1 parent 89c1e0f commit 9953e50

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

Diff for: MySQLdb/cursors.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ def execute(self, query, args=None):
182182
"""
183183
while self.nextset():
184184
pass
185+
186+
mogrified_query = self._mogrify(query, args)
187+
188+
assert isinstance(mogrified_query, (bytes, bytearray))
189+
res = self._query(mogrified_query)
190+
return res
191+
192+
def _mogrify(self, query, args=None):
193+
"""Return query after binding args."""
185194
db = self._get_db()
186195

187196
if isinstance(query, str):
@@ -202,9 +211,21 @@ def execute(self, query, args=None):
202211
except TypeError as m:
203212
raise ProgrammingError(str(m))
204213

205-
assert isinstance(query, (bytes, bytearray))
206-
res = self._query(query)
207-
return res
214+
return query
215+
216+
def mogrify(self, query, args=None):
217+
"""Return query after binding args.
218+
219+
query -- string, query to mogrify
220+
args -- optional sequence or mapping, parameters to use with query.
221+
222+
Note: If args is a sequence, then %s must be used as the
223+
parameter placeholder in the query. If a mapping is used,
224+
%(key)s must be used as the placeholder.
225+
226+
Returns string representing query that would be executed by the server
227+
"""
228+
return self._mogrify(query, args).decode(self._get_db().encoding)
208229

209230
def executemany(self, query, args):
210231
# type: (str, list) -> int

Diff for: tests/test_cursor.py

+36
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,39 @@ def test_dictcursor():
150150
names2 = sorted(rows[1])
151151
for a, b in zip(names1, names2):
152152
assert a is b
153+
154+
155+
def test_mogrify_without_args():
156+
conn = connect()
157+
cursor = conn.cursor()
158+
159+
query = "SELECT VERSION()"
160+
mogrified_query = cursor.mogrify(query)
161+
cursor.execute(query)
162+
163+
assert mogrified_query == query
164+
assert mogrified_query == cursor._executed.decode()
165+
166+
167+
def test_mogrify_with_tuple_args():
168+
conn = connect()
169+
cursor = conn.cursor()
170+
171+
query_with_args = "SELECT %s, %s", (1, 2)
172+
mogrified_query = cursor.mogrify(*query_with_args)
173+
cursor.execute(*query_with_args)
174+
175+
assert mogrified_query == "SELECT 1, 2"
176+
assert mogrified_query == cursor._executed.decode()
177+
178+
179+
def test_mogrify_with_dict_args():
180+
conn = connect()
181+
cursor = conn.cursor()
182+
183+
query_with_args = "SELECT %(a)s, %(b)s", {"a": 1, "b": 2}
184+
mogrified_query = cursor.mogrify(*query_with_args)
185+
cursor.execute(*query_with_args)
186+
187+
assert mogrified_query == "SELECT 1, 2"
188+
assert mogrified_query == cursor._executed.decode()

0 commit comments

Comments
 (0)