Skip to content

ext/pgsql: pgsql_copy_from to support iterable. #16124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
74 changes: 52 additions & 22 deletions ext/pgsql/pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "php_globals.h"
#include "zend_exceptions.h"
#include "zend_attributes.h"
#include "zend_interfaces.h"
#include "php_network.h"

#ifdef HAVE_PGSQL
Expand Down Expand Up @@ -3357,6 +3358,29 @@ PHP_FUNCTION(pg_copy_to)
}
/* }}} */

static zend_result pgsql_copy_from_query(PGconn *pgsql, PGresult *pgsql_result, zval *value)
{
zend_string *tmp = zval_try_get_string(value);
if (UNEXPECTED(!tmp)) {
return FAILURE;
}
zend_string *zquery = zend_string_alloc(ZSTR_LEN(tmp) + 1, false);
memcpy(ZSTR_VAL(zquery), ZSTR_VAL(tmp), ZSTR_LEN(tmp) + 1);
ZSTR_LEN(zquery) = ZSTR_LEN(tmp);
if (ZSTR_LEN(tmp) > 0 && ZSTR_VAL(zquery)[ZSTR_LEN(tmp)] != '\n') {
ZSTR_VAL(zquery)[ZSTR_LEN(tmp)] = '\n';
ZSTR_LEN(zquery) ++;
}
if (PQputCopyData(pgsql, ZSTR_VAL(zquery), ZSTR_LEN(zquery)) != 1) {
zend_string_release_ex(zquery, false);
zend_string_release(tmp);
return FAILURE;
}
zend_string_release_ex(zquery, false);
zend_string_release(tmp);
return SUCCESS;
}

/* {{{ Copy table from array */
PHP_FUNCTION(pg_copy_from)
{
Expand All @@ -3376,7 +3400,7 @@ PHP_FUNCTION(pg_copy_from)
ZEND_PARSE_PARAMETERS_START(3, 5)
Z_PARAM_OBJECT_OF_CLASS(pgsql_link, pgsql_link_ce)
Z_PARAM_PATH_STR(table_name)
Z_PARAM_ARRAY(pg_rows)
Z_PARAM_ITERABLE(pg_rows)
Z_PARAM_OPTIONAL
Z_PARAM_STR(pg_delimiter)
Z_PARAM_STRING(pg_null_as, pg_null_as_len)
Expand Down Expand Up @@ -3417,38 +3441,44 @@ PHP_FUNCTION(pg_copy_from)
switch (status) {
case PGRES_COPY_IN:
if (pgsql_result) {
int command_failed = 0;
PQclear(pgsql_result);
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pg_rows), value) {
zend_string *tmp = zval_try_get_string(value);
if (UNEXPECTED(!tmp)) {
return;
}
zend_string *zquery = zend_string_alloc(ZSTR_LEN(tmp) + 1, false);
memcpy(ZSTR_VAL(zquery), ZSTR_VAL(tmp), ZSTR_LEN(tmp) + 1);
ZSTR_LEN(zquery) = ZSTR_LEN(tmp);
if (ZSTR_LEN(tmp) > 0 && ZSTR_VAL(zquery)[ZSTR_LEN(tmp)] != '\n') {
ZSTR_VAL(zquery)[ZSTR_LEN(tmp)] = '\n';
ZSTR_LEN(zquery) ++;
bool command_failed = false;
if (Z_TYPE_P(pg_rows) == IS_ARRAY) {
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pg_rows), value) {
if (pgsql_copy_from_query(pgsql, pgsql_result, value) == FAILURE) {
PHP_PQ_ERROR("copy failed: %s", pgsql);
RETURN_FALSE;
}
} ZEND_HASH_FOREACH_END();
} else {
zend_object_iterator *iter = Z_OBJCE_P(pg_rows)->get_iterator(Z_OBJCE_P(pg_rows), pg_rows, 0);
if (UNEXPECTED(EG(exception) || iter == NULL)) {
RETURN_THROWS();
}
if (PQputCopyData(pgsql, ZSTR_VAL(zquery), ZSTR_LEN(zquery)) != 1) {
zend_string_release_ex(zquery, false);
zend_string_release(tmp);
PHP_PQ_ERROR("copy failed: %s", pgsql);
RETURN_FALSE;

if (iter->funcs->rewind) {
iter->funcs->rewind(iter);
}
zend_string_release_ex(zquery, false);
zend_string_release(tmp);
} ZEND_HASH_FOREACH_END();

while (iter->funcs->valid(iter) == SUCCESS && EG(exception) == NULL) {
zval *value = iter->funcs->get_current_data(iter);
if (pgsql_copy_from_query(pgsql, pgsql_result, value) == FAILURE) {
zend_iterator_dtor(iter);
PHP_PQ_ERROR("copy failed: %s", pgsql);
RETURN_FALSE;
}
iter->funcs->move_forward(iter);
}
zend_iterator_dtor(iter);
}
if (PQputCopyEnd(pgsql, NULL) != 1) {
PHP_PQ_ERROR("putcopyend failed: %s", pgsql);
RETURN_FALSE;
}
while ((pgsql_result = PQgetResult(pgsql))) {
if (PGRES_COMMAND_OK != PQresultStatus(pgsql_result)) {
PHP_PQ_ERROR("Copy command failed: %s", pgsql);
command_failed = 1;
command_failed = true;
}
PQclear(pgsql_result);
}
Expand Down
2 changes: 1 addition & 1 deletion ext/pgsql/pgsql.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ function pg_put_line($connection, string $query = UNKNOWN): bool {}
*/
function pg_copy_to(PgSql\Connection $connection, string $table_name, string $separator = "\t", string $null_as = "\\\\N"): array|false {}

function pg_copy_from(PgSql\Connection $connection, string $table_name, array $rows, string $separator = "\t", string $null_as = "\\\\N"): bool {}
function pg_copy_from(PgSql\Connection $connection, string $table_name, array|Traversable $rows, string $separator = "\t", string $null_as = "\\\\N"): bool {}

/**
* @param PgSql\Connection|string $connection
Expand Down
4 changes: 2 additions & 2 deletions ext/pgsql/pgsql_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions ext/pgsql/tests/pg_copy_from_iterable.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
--TEST--
pg_copy_from with an iterable
--EXTENSIONS--
pgsql
--SKIPIF--
<?php include("inc/skipif.inc"); ?>
--FILE--
<?php

include('inc/config.inc');
$table_name = "table_copy_iter";

$db = pg_connect($conn_str);
pg_query($db, "CREATE TABLE {$table_name} (num int)");

$iter = new class implements Iterator {
var $count = 0;
var $values = Array(1,2,3);

public function next(): void {
++$this->count;
}

public function rewind(): void {
$this->count = 0;
}

public function current(): int {
return $this->values[$this->count];
}

public function key(): int {
return $this->count;
}

public function valid(): bool {
return $this->count < count($this->values);
}
};

try {
pg_copy_from($db, $table_name, new stdClass());
} catch (\TypeError $e) {
echo $e->getMessage() . PHP_EOL;
}
var_dump(pg_copy_from($db, $table_name, $iter));
$res = pg_query($db, "SELECT FROM {$table_name}");
var_dump(count(pg_fetch_all($res)) == 3);

?>
--CLEAN--
<?php
include('inc/config.inc');
$table_name = "table_copy_iter";

$db = pg_connect($conn_str);
pg_query($db, "DROP TABLE IF EXISTS {$table_name}");
?>
--EXPECT--
pg_copy_from(): Argument #3 ($rows) must be of type Traversable|array, stdClass given
bool(true)
bool(true)