tests and minor fixes for rowcount

This commit is contained in:
Georg Richter
2018-06-28 17:41:54 +02:00
parent d308e38495
commit 2f86da2122
5 changed files with 66 additions and 20 deletions

View File

@ -71,8 +71,8 @@ typedef struct {
PyObject_HEAD
MYSQL_STMT *stmt;
PyObject *data;
uint64_t rowcount;
uint32_t array_size;
uint32_t row_array_size;
uint32_t param_count;
Mariadb_ParamInfo *paraminfo;
Mariadb_ParamValue *value;
@ -83,6 +83,7 @@ typedef struct {
char *statement;
PyObject **values;
uint8_t is_prepared;
uint8_t is_buffered;
} Mariadb_Cursor;
typedef struct {

View File

@ -36,7 +36,7 @@ if sys.version_info[0] < 3:
subprocess.call('git submodule init && git submodule update', shell=True)
assure_path_exists('libmariadb/bld')
subprocess.call('cd libmariadb/bld && cmake .. -DCMAKE_BUILD_TYPE=Release && make -j4', shell=True)
subprocess.call('cd libmariadb/bld && cmake .. -DCMAKE_BUILD_TYPE=Debug && make -j4', shell=True)
if sys.platform != "win32":

View File

@ -83,6 +83,12 @@ static struct PyMemberDef Mariadb_Cursor_Members[] =
offsetof(Mariadb_Cursor, lastrowid),
READONLY,
"row id of the last modified (inserted) row"},
{"arraysize",
T_LONG,
offsetof(Mariadb_Cursor, row_array_size),
0,
"the number of rows to fetch"},
{NULL}
};
PyObject * Mariadb_Cursor_initialize(Mariadb_Connection *self)
@ -98,7 +104,6 @@ PyObject * Mariadb_Cursor_initialize(Mariadb_Connection *self)
return NULL;
}
c->rowcount= -1;
c->array_size= 1;
return (PyObject *) c;
}
@ -294,14 +299,12 @@ PyObject *Mariadb_Cursor_execute(Mariadb_Cursor *self,
if (mariadb_stmt_execute_direct(self->stmt, statement, statement_len))
{
mariadb_throw_exception(self->stmt, Mariadb_InterfaceError, 1, NULL);
self->rowcount= -1;
goto error;
}
} else {
if (mysql_stmt_execute(self->stmt))
{
mariadb_throw_exception(self->stmt, Mariadb_InterfaceError, 1, NULL);
self->rowcount= -1;
goto error;
}
self->lastrowid= mysql_stmt_insert_id(self->stmt);
@ -337,11 +340,10 @@ PyObject *Mariadb_Cursor_rowcount(Mariadb_Cursor *self)
if (PyErr_Occurred())
return NULL;
if (self->fields)
self->rowcount= mysql_stmt_num_rows(self->stmt);
if (mysql_stmt_field_count(self->stmt))
return PyLong_FromLongLong(mysql_stmt_num_rows(self->stmt));
else
self->rowcount= mysql_stmt_affected_rows(self->stmt);
return PyLong_FromLongLong(self->rowcount);
return PyLong_FromLongLong(mysql_stmt_affected_rows(self->stmt));
}
PyObject *Mariadb_Cursor_fieldcount(Mariadb_Cursor *self)
@ -425,7 +427,8 @@ PyObject *Mariadb_Cursor_fetchone(Mariadb_Cursor *self)
static
PyObject *Mariadb_Cursor_fetchmany(Mariadb_Cursor *self, PyObject *Args)
{
PyObject *RowNr, *List;
PyObject *RowNr= NULL,
*List= NULL;
uint32_t i,rows;
MARIADB_CHECK_STMT(self);
@ -439,16 +442,17 @@ PyObject *Mariadb_Cursor_fetchmany(Mariadb_Cursor *self, PyObject *Args)
return NULL;
}
if (!PyArg_ParseTuple(Args, "O!", &PyLong_Type, &RowNr))
if (!PyArg_ParseTuple(Args, "|O!", &PyLong_Type, &RowNr))
return NULL;
rows= PyLong_AsLong(RowNr);
rows= (RowNr) ? PyLong_AsLong(RowNr) : self->row_array_size;
if (!(List= PyList_New(0)))
return NULL;
/* if rows=0, return an empty list */
if (!rows)
goto end;
return List;
for (i=0; i < rows; i++)
{
@ -462,7 +466,6 @@ PyObject *Mariadb_Cursor_fetchmany(Mariadb_Cursor *self, PyObject *Args)
PyTuple_SET_ITEM(Row, j, self->values[j]);
PyList_Append(List, Row);
}
end:
return List;
}

View File

@ -1,6 +1,5 @@
#!/usr/bin/env python -O
from mariadb import indicator
import mariadb
import datetime
import unittest
@ -14,7 +13,6 @@ class CursorTest(unittest.TestCase):
del self.connection
def test_date(self):
print("test_date")
cursor= self.connection.cursor()
cursor.execute("CREATE OR REPLACE TABLE t1(c1 TIMESTAMP(6), c2 TIME(6), c3 DATETIME(6), c4 DATE)")
t= datetime.datetime(2018,6,20,12,22,31,123456)
@ -33,7 +31,6 @@ class CursorTest(unittest.TestCase):
cursor.close()
def test_numbers(self):
print("test_numbers")
cursor= self.connection.cursor()
cursor.execute("CREATE OR REPLACE TABLE t1 (a tinyint unsigned, b smallint unsigned, c mediumint unsigned, d int unsigned, e bigint unsigned, f double)")
c1= 4
@ -56,7 +53,6 @@ class CursorTest(unittest.TestCase):
del cursor
def test_string(self):
print("test_string")
cursor= self.connection.cursor()
cursor.execute("CREATE OR REPLACE TABLE t1 (a char(5), b varchar(100), c tinytext, d mediumtext, e text, f longtext)");
@ -89,7 +85,6 @@ class CursorTest(unittest.TestCase):
c4= b'd' * 100000;
a= (None, None, None, None)
cursor.indicators= a
cursor.execute("INSERT INTO t1 VALUES (?,?,?,?)", (c1, c2, c3, c4))
cursor.execute("SELECT * FROM t1")
@ -99,3 +94,50 @@ class CursorTest(unittest.TestCase):
self.assertEqual(row[2],c3)
self.assertEqual(row[3],c4)
del cursor
def test_fetchmany(self):
cursor= self.connection.cursor()
cursor.execute("CREATE OR REPLACE TABLE t1 (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 t1 VALUES (?,?,?)", params);
#test Errors
# # a) if no select was executed
self.assertRaises(mariadb.Error, cursor.fetchall)
#b ) if cursor was not executed
del cursor
cursor= self.connection.cursor()
self.assertRaises(mariadb.Error, cursor.fetchall)
cursor.execute("SELECT id, name, city FROM t1 ORDER BY id")
self.assertEqual(0, cursor.rowcount())
row = cursor.fetchall()
self.assertEqual(row, params)
self.assertEqual(5, cursor.rowcount())
cursor.execute("SELECT id, name, city FROM t1 ORDER BY id")
self.assertEqual(0, cursor.rowcount())
print("row count 3: ", cursor.rowcount())
row= cursor.fetchmany()
print("row count 4: ", cursor.rowcount())
self.assertEqual(row,[])
row= cursor.fetchmany(1)
print("row count: ", cursor.rowcount())
self.assertEqual(row,[params[0]])
row= cursor.fetchmany(2)
self.assertEqual(row,([params[1], params[2]]))
cursor.arraysize= 1
row= cursor.fetchmany()
self.assertEqual(row,[params[3]])
cursor.arraysize= 2
row= cursor.fetchmany()
self.assertEqual(row,[params[4]])
print("row count: ", cursor.rowcount())