-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
|
@@ -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) | ||||||
{ | ||||||
|
@@ -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_ARRAY_OR_OBJECT(pg_rows) | ||||||
Z_PARAM_OPTIONAL | ||||||
Z_PARAM_STR(pg_delimiter) | ||||||
Z_PARAM_STRING(pg_null_as, pg_null_as_len) | ||||||
|
@@ -3392,6 +3416,10 @@ PHP_FUNCTION(pg_copy_from) | |||||
zend_argument_value_error(4, "must be one character"); | ||||||
RETURN_THROWS(); | ||||||
} | ||||||
if (Z_TYPE_P(pg_rows) == IS_OBJECT && !instanceof_function(Z_OBJCE_P(pg_rows), zend_ce_traversable)) { | ||||||
zend_argument_type_error(3, "must be of type Traversable"); | ||||||
RETURN_THROWS(); | ||||||
} | ||||||
if (!pg_null_as) { | ||||||
pg_null_as = estrdup("\\\\N"); | ||||||
pg_null_as_free = true; | ||||||
|
@@ -3417,38 +3445,40 @@ 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) ++; | ||||||
} | ||||||
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; | ||||||
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_OBJ_P(pg_rows)->ce->get_iterator(Z_OBJCE_P(pg_rows), pg_rows, 0); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if (UNEXPECTED(EG(exception) || iter == NULL)) { | ||||||
RETURN_THROWS(); | ||||||
} | ||||||
zend_string_release_ex(zquery, false); | ||||||
zend_string_release(tmp); | ||||||
} ZEND_HASH_FOREACH_END(); | ||||||
|
||||||
while (iter->funcs->valid(iter) == SUCCESS && EG(exception) == NULL) { | ||||||
Girgias marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
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); | ||||||
} | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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|object $rows, string $separator = "\t", string $null_as = "\\\\N"): bool {} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
/** | ||||||
* @param PgSql\Connection|string $connection | ||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
bool(true) | ||
bool(true) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
Z_PARAM_ITERABLE