mirror of
https://github.com/mariadb-corporation/mariadb-connector-python.git
synced 2025-08-04 08:04:45 +00:00
tests and minor fixes for rowcount
This commit is contained in:
@ -71,8 +71,8 @@ typedef struct {
|
|||||||
PyObject_HEAD
|
PyObject_HEAD
|
||||||
MYSQL_STMT *stmt;
|
MYSQL_STMT *stmt;
|
||||||
PyObject *data;
|
PyObject *data;
|
||||||
uint64_t rowcount;
|
|
||||||
uint32_t array_size;
|
uint32_t array_size;
|
||||||
|
uint32_t row_array_size;
|
||||||
uint32_t param_count;
|
uint32_t param_count;
|
||||||
Mariadb_ParamInfo *paraminfo;
|
Mariadb_ParamInfo *paraminfo;
|
||||||
Mariadb_ParamValue *value;
|
Mariadb_ParamValue *value;
|
||||||
@ -83,6 +83,7 @@ typedef struct {
|
|||||||
char *statement;
|
char *statement;
|
||||||
PyObject **values;
|
PyObject **values;
|
||||||
uint8_t is_prepared;
|
uint8_t is_prepared;
|
||||||
|
uint8_t is_buffered;
|
||||||
} Mariadb_Cursor;
|
} Mariadb_Cursor;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
Submodule libmariadb updated: 2ad66ba309...9e1fef0bf2
2
setup.py
2
setup.py
@ -36,7 +36,7 @@ if sys.version_info[0] < 3:
|
|||||||
|
|
||||||
subprocess.call('git submodule init && git submodule update', shell=True)
|
subprocess.call('git submodule init && git submodule update', shell=True)
|
||||||
assure_path_exists('libmariadb/bld')
|
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":
|
if sys.platform != "win32":
|
||||||
|
|
||||||
|
@ -83,6 +83,12 @@ static struct PyMemberDef Mariadb_Cursor_Members[] =
|
|||||||
offsetof(Mariadb_Cursor, lastrowid),
|
offsetof(Mariadb_Cursor, lastrowid),
|
||||||
READONLY,
|
READONLY,
|
||||||
"row id of the last modified (inserted) row"},
|
"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)
|
PyObject * Mariadb_Cursor_initialize(Mariadb_Connection *self)
|
||||||
@ -98,7 +104,6 @@ PyObject * Mariadb_Cursor_initialize(Mariadb_Connection *self)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
c->rowcount= -1;
|
|
||||||
c->array_size= 1;
|
c->array_size= 1;
|
||||||
return (PyObject *) c;
|
return (PyObject *) c;
|
||||||
}
|
}
|
||||||
@ -294,14 +299,12 @@ PyObject *Mariadb_Cursor_execute(Mariadb_Cursor *self,
|
|||||||
if (mariadb_stmt_execute_direct(self->stmt, statement, statement_len))
|
if (mariadb_stmt_execute_direct(self->stmt, statement, statement_len))
|
||||||
{
|
{
|
||||||
mariadb_throw_exception(self->stmt, Mariadb_InterfaceError, 1, NULL);
|
mariadb_throw_exception(self->stmt, Mariadb_InterfaceError, 1, NULL);
|
||||||
self->rowcount= -1;
|
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (mysql_stmt_execute(self->stmt))
|
if (mysql_stmt_execute(self->stmt))
|
||||||
{
|
{
|
||||||
mariadb_throw_exception(self->stmt, Mariadb_InterfaceError, 1, NULL);
|
mariadb_throw_exception(self->stmt, Mariadb_InterfaceError, 1, NULL);
|
||||||
self->rowcount= -1;
|
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
self->lastrowid= mysql_stmt_insert_id(self->stmt);
|
self->lastrowid= mysql_stmt_insert_id(self->stmt);
|
||||||
@ -337,11 +340,10 @@ PyObject *Mariadb_Cursor_rowcount(Mariadb_Cursor *self)
|
|||||||
|
|
||||||
if (PyErr_Occurred())
|
if (PyErr_Occurred())
|
||||||
return NULL;
|
return NULL;
|
||||||
if (self->fields)
|
if (mysql_stmt_field_count(self->stmt))
|
||||||
self->rowcount= mysql_stmt_num_rows(self->stmt);
|
return PyLong_FromLongLong(mysql_stmt_num_rows(self->stmt));
|
||||||
else
|
else
|
||||||
self->rowcount= mysql_stmt_affected_rows(self->stmt);
|
return PyLong_FromLongLong(mysql_stmt_affected_rows(self->stmt));
|
||||||
return PyLong_FromLongLong(self->rowcount);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *Mariadb_Cursor_fieldcount(Mariadb_Cursor *self)
|
PyObject *Mariadb_Cursor_fieldcount(Mariadb_Cursor *self)
|
||||||
@ -425,7 +427,8 @@ PyObject *Mariadb_Cursor_fetchone(Mariadb_Cursor *self)
|
|||||||
static
|
static
|
||||||
PyObject *Mariadb_Cursor_fetchmany(Mariadb_Cursor *self, PyObject *Args)
|
PyObject *Mariadb_Cursor_fetchmany(Mariadb_Cursor *self, PyObject *Args)
|
||||||
{
|
{
|
||||||
PyObject *RowNr, *List;
|
PyObject *RowNr= NULL,
|
||||||
|
*List= NULL;
|
||||||
uint32_t i,rows;
|
uint32_t i,rows;
|
||||||
|
|
||||||
MARIADB_CHECK_STMT(self);
|
MARIADB_CHECK_STMT(self);
|
||||||
@ -439,16 +442,17 @@ PyObject *Mariadb_Cursor_fetchmany(Mariadb_Cursor *self, PyObject *Args)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(Args, "O!", &PyLong_Type, &RowNr))
|
if (!PyArg_ParseTuple(Args, "|O!", &PyLong_Type, &RowNr))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
rows= PyLong_AsLong(RowNr);
|
rows= (RowNr) ? PyLong_AsLong(RowNr) : self->row_array_size;
|
||||||
|
|
||||||
if (!(List= PyList_New(0)))
|
if (!(List= PyList_New(0)))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
/* if rows=0, return an empty list */
|
/* if rows=0, return an empty list */
|
||||||
if (!rows)
|
if (!rows)
|
||||||
goto end;
|
return List;
|
||||||
|
|
||||||
for (i=0; i < rows; i++)
|
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]);
|
PyTuple_SET_ITEM(Row, j, self->values[j]);
|
||||||
PyList_Append(List, Row);
|
PyList_Append(List, Row);
|
||||||
}
|
}
|
||||||
|
|
||||||
end:
|
end:
|
||||||
return List;
|
return List;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
#!/usr/bin/env python -O
|
#!/usr/bin/env python -O
|
||||||
|
|
||||||
from mariadb import indicator
|
|
||||||
import mariadb
|
import mariadb
|
||||||
import datetime
|
import datetime
|
||||||
import unittest
|
import unittest
|
||||||
@ -14,7 +13,6 @@ class CursorTest(unittest.TestCase):
|
|||||||
del self.connection
|
del self.connection
|
||||||
|
|
||||||
def test_date(self):
|
def test_date(self):
|
||||||
print("test_date")
|
|
||||||
cursor= self.connection.cursor()
|
cursor= self.connection.cursor()
|
||||||
cursor.execute("CREATE OR REPLACE TABLE t1(c1 TIMESTAMP(6), c2 TIME(6), c3 DATETIME(6), c4 DATE)")
|
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)
|
t= datetime.datetime(2018,6,20,12,22,31,123456)
|
||||||
@ -33,7 +31,6 @@ class CursorTest(unittest.TestCase):
|
|||||||
cursor.close()
|
cursor.close()
|
||||||
|
|
||||||
def test_numbers(self):
|
def test_numbers(self):
|
||||||
print("test_numbers")
|
|
||||||
cursor= self.connection.cursor()
|
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)")
|
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
|
c1= 4
|
||||||
@ -56,7 +53,6 @@ class CursorTest(unittest.TestCase):
|
|||||||
del cursor
|
del cursor
|
||||||
|
|
||||||
def test_string(self):
|
def test_string(self):
|
||||||
print("test_string")
|
|
||||||
cursor= self.connection.cursor()
|
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)");
|
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;
|
c4= b'd' * 100000;
|
||||||
|
|
||||||
a= (None, None, None, None)
|
a= (None, None, None, None)
|
||||||
cursor.indicators= a
|
|
||||||
cursor.execute("INSERT INTO t1 VALUES (?,?,?,?)", (c1, c2, c3, c4))
|
cursor.execute("INSERT INTO t1 VALUES (?,?,?,?)", (c1, c2, c3, c4))
|
||||||
|
|
||||||
cursor.execute("SELECT * FROM t1")
|
cursor.execute("SELECT * FROM t1")
|
||||||
@ -99,3 +94,50 @@ class CursorTest(unittest.TestCase):
|
|||||||
self.assertEqual(row[2],c3)
|
self.assertEqual(row[2],c3)
|
||||||
self.assertEqual(row[3],c4)
|
self.assertEqual(row[3],c4)
|
||||||
del cursor
|
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())
|
||||||
|
Reference in New Issue
Block a user