Skip to content

Commit

Permalink
inserttable: test for errors and return number of tuples as str (#73)
Browse files Browse the repository at this point in the history
Contributed by: Justin Pryzby <justin@telsasoft.com>
  • Loading branch information
justinpryzby committed Aug 26, 2023
1 parent 373e1a9 commit e142a51
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
3 changes: 2 additions & 1 deletion docs/contents/pg/connection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ inserttable -- insert an iterable into a table
:param str table: the table name
:param list values: iterable of row values, which must be lists or tuples
:param list columns: list or tuple of column names
:rtype: None
:rtype: int
:raises TypeError: invalid connection, bad argument type, or too many arguments
:raises MemoryError: insert buffer could not be allocated
:raises ValueError: unsupported values
Expand All @@ -506,6 +506,7 @@ of the same size, containing the values for each inserted row.
These may contain string, integer, long or double (real) values.
``columns`` is an optional tuple or list of column names to be passed on
to the COPY command.
The number of rows affected is returned.

.. warning::

Expand Down
17 changes: 13 additions & 4 deletions pgconn.c
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ conn_inserttable(connObject *self, PyObject *args)

Py_DECREF(iter_row);
if (PyErr_Occurred()) {
PQerrorMessage(self->cnx); PyMem_Free(buffer);
PyMem_Free(buffer);
return NULL; /* pass the iteration error */
}

Expand All @@ -1026,9 +1026,18 @@ conn_inserttable(connObject *self, PyObject *args)

PyMem_Free(buffer);

/* no error : returns nothing */
Py_INCREF(Py_None);
return Py_None;
Py_BEGIN_ALLOW_THREADS
result = PQgetResult(self->cnx);
Py_END_ALLOW_THREADS
if (PQresultStatus(result) != PGRES_COMMAND_OK) {
PyErr_SetString(PyExc_ValueError, PQerrorMessage(self->cnx));
PQclear(result);
return NULL;
} else {
long ntuples = atol(PQcmdTuples(result));
PQclear(result);
return PyInt_FromLong(ntuples);
}
}

/* Get transaction state. */
Expand Down
5 changes: 5 additions & 0 deletions tests/test_classic_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2023,6 +2023,11 @@ def testInserttableWithHugeListOfColumnNames(self):
cols *= 2
self.assertRaises(MemoryError, self.c.inserttable, 'test', data, cols)

def testInserttableWithOutOfRangeData(self):
# try inserting data out of range for the column type
# Should raise a value error because of smallint out of range
self.assertRaises(ValueError, self.c.inserttable, 'test', [[33000]], ['i2'])

def testInserttableMaxValues(self):
data = [(2 ** 15 - 1, int(2 ** 31 - 1), long(2 ** 31 - 1),
True, '2999-12-31', '11:59:59', 1e99,
Expand Down
3 changes: 2 additions & 1 deletion tests/test_classic_dbwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4227,10 +4227,11 @@ def testInserttableFromQuery(self):
self.createTable('test_table_to', 'n integer, t timestamp')
for i in range(1, 4):
query("insert into test_table_from values ($1, now())", i)
self.db.inserttable(
n = self.db.inserttable(
'test_table_to', query("select n, t::text from test_table_from"))
data_from = query("select * from test_table_from").getresult()
data_to = query("select * from test_table_to").getresult()
self.assertEqual(n, 3)
self.assertEqual([row[0] for row in data_from], [1, 2, 3])
self.assertEqual(data_from, data_to)

Expand Down
3 changes: 2 additions & 1 deletion tests/test_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def test_all_steps(self):
self.assertEqual(r, {'name': 'banana', 'id': 2})
more_fruits = 'cherimaya durian eggfruit fig grapefruit'.split()
data = list(enumerate(more_fruits, start=3))
db.inserttable('fruits', data)
n = db.inserttable('fruits', data)
self.assertEqual(n, 5)
q = db.query('select * from fruits')
r = str(q).splitlines()
self.assertEqual(r[0], 'id| name ')
Expand Down

0 comments on commit e142a51

Please sign in to comment.