Skip to content

Commit

Permalink
New test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kocsismate committed Apr 2, 2021
1 parent fae9bcf commit 6931655
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 45 deletions.
58 changes: 30 additions & 28 deletions ext/pgsql/pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,20 @@ static void pgsql_link_free(pgsql_link_handle *link)
while ((res = PQgetResult(link->conn))) {
PQclear(res);
}
PQfinish(link->conn);
if (!link->persistent) {
PQfinish(link->conn);
}
PGG(num_links)--;

zend_hash_del(&PGG(regular_list), link->hash);

link->conn = NULL;
zend_string_release(link->hash);

if (link->notices) {
zend_hash_destroy(link->notices);
FREE_HASHTABLE(link->notices);
link->notices = NULL;
}
}

Expand Down Expand Up @@ -293,48 +298,45 @@ static inline char * _php_pgsql_trim_result(PGconn * pgsql, char **buf)

static void php_pgsql_set_default_link(pgsql_link_handle *link)
{
if (PGG(default_link) != NULL) {
pgsql_link_free(FETCH_DEFAULT_LINK());
}

PGG(default_link) = link;
}

static void _close_pgsql_plink(zend_resource *rsrc)
{
PGconn *link = (PGconn *)rsrc->ptr;
PGresult *res;
if (rsrc->ptr) {
PGconn *link = (PGconn *)rsrc->ptr;
PGresult *res;

while ((res = PQgetResult(link))) {
PQclear(res);
while ((res = PQgetResult(link))) {
PQclear(res);
}
PQfinish(link);
PGG(num_persistent)--;
PGG(num_links)--;
rsrc->ptr = NULL;
}
PQfinish(link);
PGG(num_persistent)--;
PGG(num_links)--;
}

static void _php_pgsql_notice_handler(void *link, const char *message)
static void _php_pgsql_notice_handler(void *l, const char *message)
{
HashTable *notices, tmp_notices;
pgsql_link_handle *link;
zval tmp;
char *trimmed_message;
size_t trimmed_message_len;

if (! PGG(ignore_notices)) {
notices = ((pgsql_link_handle *) link)->notices;
if (!notices) {
zend_hash_init(&tmp_notices, 1, NULL, ZVAL_PTR_DTOR, 0);
notices = &tmp_notices;
link = ((pgsql_link_handle *) l);
if (!link->notices) {
link->notices = zend_new_array(1);
}
trimmed_message = _php_pgsql_trim_message(message, &trimmed_message_len);
if (PGG(log_notices)) {
php_error_docref(NULL, E_NOTICE, "%s", trimmed_message);
}

ZVAL_STRINGL(&tmp, trimmed_message, trimmed_message_len);
zend_hash_next_index_insert(notices, &tmp);
zend_hash_next_index_insert(link->notices, &tmp);
efree(trimmed_message);
zval_ptr_dtor(&tmp);
}
}

Expand All @@ -344,8 +346,9 @@ static int _rollback_transactions(zval *el)
PGresult *res;
zend_resource *rsrc = Z_RES_P(el);

if (rsrc->type != le_plink)
return 0;
if (rsrc->type != le_plink) {
return ZEND_HASH_APPLY_KEEP;
}

link = (PGconn *) rsrc->ptr;

Expand All @@ -365,7 +368,7 @@ static int _rollback_transactions(zval *el)
PGG(ignore_notices) = orig;
}

return 0;
return ZEND_HASH_APPLY_KEEP;
}

static void _free_ptr(zend_resource *rsrc)
Expand Down Expand Up @@ -413,7 +416,6 @@ static PHP_GINIT_FUNCTION(pgsql)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
memset(pgsql_globals, 0, sizeof(zend_pgsql_globals));
/* Initialize notice message hash at MINIT only */
zend_hash_init(&pgsql_globals->regular_list, 0, NULL, ZVAL_PTR_DTOR, 1);
}

Expand Down Expand Up @@ -599,8 +601,6 @@ PHP_RINIT_FUNCTION(pgsql)

PHP_RSHUTDOWN_FUNCTION(pgsql)
{
/* clean up notice messages */
zend_hash_clean(&PGG(regular_list));
/* clean up persistent connection */
zend_hash_apply(&EG(persistent_list), (apply_func_t) _rollback_transactions);
return SUCCESS;
Expand Down Expand Up @@ -720,6 +720,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
link->conn = pgsql;
link->hash = zend_string_copy(str.s);
link->notices = NULL;
link->persistent = 1;
} else { /* Non persistent connection */
zval *index_ptr, new_index_ptr;

Expand Down Expand Up @@ -767,6 +768,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
link->conn = pgsql;
link->hash = zend_string_copy(str.s);
link->notices = NULL;
link->persistent = 0;

/* add it to the hash */
ZVAL_COPY(&new_index_ptr, return_value);
Expand Down Expand Up @@ -844,15 +846,16 @@ PHP_FUNCTION(pg_close)
if (!pgsql_link) {
link = FETCH_DEFAULT_LINK();
CHECK_DEFAULT_LINK(link);
zend_hash_del(&PGG(regular_list), link->hash);
PGG(default_link) = NULL;
pgsql_link_free(link);
RETURN_TRUE;
}

link = Z_PGSQL_LINK_P(pgsql_link);
CHECK_PGSQL_LINK(link);

if (link == FETCH_DEFAULT_LINK()) {
zend_hash_del(&PGG(regular_list), link->hash);
PGG(default_link) = NULL;
}
pgsql_link_free(link);
Expand Down Expand Up @@ -3372,7 +3375,6 @@ PHP_FUNCTION(pg_escape_bytea)
RETURN_THROWS();
}
link = FETCH_DEFAULT_LINK();
CHECK_DEFAULT_LINK(link);
break;
default:
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os", &pgsql_link, pgsql_link_ce, &from, &from_len) == FAILURE) {
Expand Down
1 change: 1 addition & 0 deletions ext/pgsql/php_pgsql.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ typedef struct pgsql_link_handle {
zend_string *hash;
HashTable *notices;
zend_object std;
bool persistent;
} pgsql_link_handle;

typedef struct pgLofp {
Expand Down
1 change: 0 additions & 1 deletion ext/pgsql/tests/13pg_select_9.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ $fields = array('num'=>'1234', 'str'=>'ABC', 'bin'=>'XYZ');
$ids = array('num'=>'1234');

$res = pg_select($db, $table_name, $ids) or print "Error\n";
var_dump(pg_last_error($db));
var_dump($res);
echo pg_select($db, $table_name, $ids, PGSQL_DML_STRING)."\n";
echo pg_select($db, $table_name, $ids, PGSQL_DML_STRING|PGSQL_DML_ESCAPE)."\n";
Expand Down
23 changes: 12 additions & 11 deletions ext/pgsql/tests/80_bug32223b.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,41 @@ pgsql.ignore_notice=0
require_once('config.inc');
require_once('lcmess.inc');

define('dbh', pg_connect($conn_str));
if (!dbh) {
die ("Could not connect to the server");
$dbh = pg_connect($conn_str);
if (!$dbh) {
die ("Could not connect to the server");
}

_set_lc_messages();

$res = pg_query(dbh, "CREATE OR REPLACE FUNCTION test_notice() RETURNS boolean AS '
$res = pg_query($dbh, "CREATE OR REPLACE FUNCTION test_notice() RETURNS boolean AS '
begin
RAISE NOTICE ''11111'';
return ''f'';
end;
' LANGUAGE plpgsql;");

$res = pg_query(dbh, 'SET client_min_messages TO NOTICE;');
$res = pg_query($dbh, 'SET client_min_messages TO NOTICE;');
var_dump($res);

function tester() {
$res = pg_query(dbh, 'SELECT test_notice()');
function tester($dbh) {
$res = pg_query($dbh, 'SELECT test_notice()');
$row = pg_fetch_row($res, 0);
var_dump($row);
pg_free_result($res);
if ($row[0] == 'f')
{
var_dump(pg_last_notice(dbh));
var_dump(pg_last_notice($dbh));
}
}
tester();
tester($dbh);

pg_close(dbh);
pg_close($dbh);

?>
--EXPECTF--
resource(%d) of type (pgsql result)
object(PgSqlResult)#%d (0) {
}
array(1) {
[0]=>
string(1) "f"
Expand Down
8 changes: 4 additions & 4 deletions ext/pgsql/tests/bug46408.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Bug #46408 (Locale number format settings can cause pg_query_params to break wit
--SKIPIF--
<?php
require_once('skipif.inc');
if (false === setlocale(LC_ALL, 'de_DE.utf-8', 'de_DE')) {
echo "skip Locale de_DE.utf-8 not present";
if (false === setlocale(LC_ALL, "de", "de_DE", "de_DE.ISO8859-1", "de_DE.ISO_8859-1", "de_DE.UTF-8")) {
echo "skip Locale de-DE not present";
}
?>
--FILE--
Expand All @@ -13,7 +13,7 @@ if (false === setlocale(LC_ALL, 'de_DE.utf-8', 'de_DE')) {
require_once('config.inc');

$dbh = pg_connect($conn_str);
setlocale(LC_ALL, 'de_DE.utf-8', 'de_DE');
setlocale(LC_ALL, "de", "de_DE", "de_DE.ISO8859-1", "de_DE.ISO_8859-1", "de_DE.UTF-8");
echo 3.5 , "\n";
pg_query_params("SELECT $1::numeric", array(3.5));
pg_close($dbh);
Expand All @@ -22,5 +22,5 @@ echo "Done".PHP_EOL;

?>
--EXPECT--
3,5
3.5
Done
2 changes: 1 addition & 1 deletion ext/pgsql/tests/connect_after_close.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ include('config.inc');
$db1 = pg_connect($conn_str);
unset($db1);
var_dump(pg_close());
exit;

$db2 = pg_connect($conn_str);
unset($db2);
var_dump(pg_close());
Expand Down

0 comments on commit 6931655

Please sign in to comment.