Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ext/pdo_pgsql/pgsql_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -712,11 +712,15 @@ void pgsqlCopyFromArray_internal(INTERNAL_FUNCTION_PARAMETERS)
}
} ZEND_HASH_FOREACH_END();
} else {
iter = Z_OBJ_P(pg_rows)->ce->get_iterator(Z_OBJCE_P(pg_rows), pg_rows, 0);
iter = Z_OBJCE_P(pg_rows)->get_iterator(Z_OBJCE_P(pg_rows), pg_rows, 0);
if (iter == NULL || EG(exception)) {
RETURN_THROWS();
}

if (iter->funcs->rewind) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think rewind can throw too, but the exception isn't checked in this case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I forgot about that detail.

Maybe would be nice to have some FOREACH_ITERABLE macros 🤔

iter->funcs->rewind(iter);
}

for (; iter->funcs->valid(iter) == SUCCESS && EG(exception) == NULL; iter->funcs->move_forward(iter)) {
tmp = iter->funcs->get_current_data(iter);
if (!_pdo_pgsql_send_copy_data(H, tmp)) {
Expand Down
7 changes: 7 additions & 0 deletions ext/pdo_pgsql/tests/copy_from_iterator.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ $iterator = new class implements Iterator{
}
};

try {
$db->pgsqlCopyFromArray('test_copy_from_traversable',new stdClass());
} catch (\TypeError $e) {
echo $e->getMessage() . PHP_EOL;
}

$db->pgsqlCopyFromArray('test_copy_from_traversable',$iterator);

$stmt = $db->query("select * from test_copy_from_traversable order by 1");
Expand All @@ -56,6 +62,7 @@ $db = PDOTest::test_factory(__DIR__ . '/common.phpt');
$db->query('DROP TABLE IF EXISTS test_copy_from_traversable CASCADE');
?>
--EXPECT--
PDO::pgsqlCopyFromArray(): Argument #2 ($rows) must be of type array or Traversable
array (
0 => 1,
1 => 1,
Expand Down