Skip to content

Commit

Permalink
Fix for CONPY-214:
Browse files Browse the repository at this point in the history
Replace cursor iterator by native python __iter__() method.
  • Loading branch information
9EOR9 committed Jul 6, 2022
1 parent c4955f1 commit 275aaf3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
3 changes: 3 additions & 0 deletions mariadb/cursors.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@ def fetchall(self):
rows.append((row))
return rows

def __iter__(self):
return iter(self.fetchone, None)

def scroll(self, value: int, mode="relative"):
"""
Scroll the cursor in the result set to a new position according to mode.
Expand Down
31 changes: 2 additions & 29 deletions mariadb/mariadb_cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ PyObject *MrdbCursor_clear_result(MrdbCursor *self);
void
field_fetch_callback(void *data, unsigned int column, unsigned char **row);
static PyObject *mariadb_get_sequence_or_tuple(MrdbCursor *self);
static PyObject * MrdbCursor_iter(PyObject *self);
static PyObject * MrdbCursor_iternext(PyObject *self);

/* todo: write more documentation, this is just a placeholder */
static char mariadb_cursor_documentation[] =
Expand Down Expand Up @@ -377,8 +375,8 @@ PyTypeObject MrdbCursor_Type =
0, /* (long) tp_weaklistoffset */

/* Iterators */
(getiterfunc)MrdbCursor_iter,
(iternextfunc)MrdbCursor_iternext,
0,
0,

/* Attribute descriptor and subclassing stuff */
(struct PyMethodDef *)MrdbCursor_Methods, /* tp_methods */
Expand Down Expand Up @@ -948,31 +946,6 @@ MrdbCursor_warnings(MrdbCursor *self)
return PyLong_FromLong((long)CURSOR_WARNING_COUNT(self));
}

/* iterator protocol */

static PyObject *
MrdbCursor_iter(PyObject *self)
{
MARIADB_CHECK_STMT(((MrdbCursor *)self));
Py_INCREF(self);
return self;
}

static PyObject *
MrdbCursor_iternext(PyObject *self)
{
PyObject *res;

res= MrdbCursor_fetchone((MrdbCursor *)self);

if (res && res == Py_None)
{
Py_DECREF(res);
res= NULL;
}
return res;
}

static PyObject
*MrdbCursor_closed(MrdbCursor *self)
{
Expand Down
32 changes: 31 additions & 1 deletion testing/test/integration/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,41 @@ def test_format(self):
row = cursor.fetchone()
self.assertEqual(row[0], "Andrey")

def test_conpy214(self):
cursor= self.connection.cursor(named_tuple=True)
cursor.execute("SELECT 1 as foo")
rows= cursor.fetchall()
self.assertEqual(rows[0].foo, 1)
del cursor
cursor= self.connection.cursor(dictionary=True)
cursor.execute("SELECT 1 as foo")
rows= cursor.fetchall()
self.assertEqual(rows[0]["foo"], 1)
del cursor

def test_named_tuple(self):
if is_maxscale():
self.skipTest("MAXSCALE doesn't support BULK yet")

cursor = self.connection.cursor(named_tuple=1)
cursor = self.connection.cursor(named_tuple=True)
cursor.execute(
"CREATE TEMPORARY TABLE test_named_tuple (id int, name varchar(64), city varchar(64))");
params = [(1, u"Jack", u"Boston"),
(2, u"Martin", u"Ohio"),
(3, u"James", u"Washington"),
(4, u"Rasmus", u"Helsinki"),
(5, u"Andrey", u"Sofia")]
cursor.executemany("INSERT INTO test_named_tuple VALUES (?,?,?)", params);
cursor.execute("SELECT * FROM test_named_tuple ORDER BY id")
row = cursor.fetchone()

self.assertEqual(cursor.statement, "SELECT * FROM test_named_tuple ORDER BY id")

def test_named_tuple(self):
if is_maxscale():
self.skipTest("MAXSCALE doesn't support BULK yet")

cursor = self.connection.cursor(named_tuple=True)
cursor.execute(
"CREATE TEMPORARY TABLE test_named_tuple (id int, name varchar(64), city varchar(64))");
params = [(1, u"Jack", u"Boston"),
Expand Down

0 comments on commit 275aaf3

Please sign in to comment.