Skip to content

Commit d6ef88d

Browse files
authored
Expose Cursor.warning_count (#780)
1 parent a417303 commit d6ef88d

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

src/MySQLdb/cursors.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class BaseCursor:
6464

6565
def __init__(self, connection):
6666
self.connection = connection
67+
self.warning_count = 0
6768
self.description = None
6869
self.description_flags = None
6970
self.rowcount = 0
@@ -140,6 +141,7 @@ def _do_get_result(self, db):
140141
self.description = result.describe()
141142
self.description_flags = result.field_flags()
142143

144+
self.warning_count = db.warning_count()
143145
self.rowcount = db.affected_rows()
144146
self.rownumber = 0
145147
self.lastrowid = db.insert_id()
@@ -325,6 +327,7 @@ def callproc(self, procname, args=()):
325327
def _query(self, q):
326328
db = self._get_db()
327329
self._result = None
330+
self.warning_count = 0
328331
self.rowcount = None
329332
self.lastrowid = None
330333
db.query(q)
@@ -435,6 +438,7 @@ def fetchone(self):
435438
self._check_executed()
436439
r = self._fetch_row(1)
437440
if not r:
441+
self.warning_count = self._get_db().warning_count()
438442
return None
439443
self.rownumber = self.rownumber + 1
440444
return r[0]
@@ -443,14 +447,18 @@ def fetchmany(self, size=None):
443447
"""Fetch up to size rows from the cursor. Result set may be smaller
444448
than size. If size is not defined, cursor.arraysize is used."""
445449
self._check_executed()
446-
r = self._fetch_row(size or self.arraysize)
450+
size = size or self.arraysize
451+
r = self._fetch_row(size)
452+
if len(r) < size:
453+
self.warning_count = self._get_db().warning_count()
447454
self.rownumber = self.rownumber + len(r)
448455
return r
449456

450457
def fetchall(self):
451458
"""Fetches all available rows from the cursor."""
452459
self._check_executed()
453460
r = self._fetch_row(0)
461+
self.warning_count = self._get_db().warning_count()
454462
self.rownumber = self.rownumber + len(r)
455463
return r
456464

tests/test_cursor.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
import MySQLdb.cursors
3+
from MySQLdb.constants import ER
34
from configdb import connection_factory
45

56

@@ -243,3 +244,43 @@ def test_binary_prefix():
243244
"INSERT INTO test_binary_prefix (id, json) VALUES (%(id)s, %(json)s)",
244245
({"id": 1, "json": "{}"}, {"id": 2, "json": "{}"}),
245246
)
247+
248+
249+
def test_warning_count():
250+
conn = connect()
251+
cursor = conn.cursor()
252+
253+
cursor.execute("DROP TABLE IF EXISTS `no_exists_table`")
254+
assert cursor.warning_count == 1
255+
256+
cursor.execute("SHOW WARNINGS")
257+
warning = cursor.fetchone()
258+
assert warning[1] == ER.BAD_TABLE_ERROR
259+
assert "no_exists_table" in warning[2]
260+
261+
cursor.execute("SELECT 1")
262+
assert cursor.warning_count == 0
263+
264+
265+
def test_sscursor_warning_count():
266+
conn = connect()
267+
cursor = conn.cursor(MySQLdb.cursors.SSCursor)
268+
269+
cursor.execute("DROP TABLE IF EXISTS `no_exists_table`")
270+
assert cursor.warning_count == 1
271+
272+
cursor.execute("SHOW WARNINGS")
273+
warning = cursor.fetchone()
274+
assert warning[1] == ER.BAD_TABLE_ERROR
275+
assert "no_exists_table" in warning[2]
276+
assert cursor.fetchone() is None
277+
278+
cursor.execute("SELECT 1")
279+
assert cursor.fetchone() == (1,)
280+
assert cursor.fetchone() is None
281+
assert cursor.warning_count == 0
282+
283+
cursor.execute("SELECT CAST('abc' AS SIGNED)")
284+
rows = cursor.fetchmany(2)
285+
assert len(rows) == 1
286+
assert cursor.warning_count == 1

0 commit comments

Comments
 (0)