@@ -164,15 +164,20 @@ static void pgsql_link_free(pgsql_link_handle *link)
164164 while ((res = PQgetResult (link -> conn ))) {
165165 PQclear (res );
166166 }
167- PQfinish (link -> conn );
167+ if (!link -> persistent ) {
168+ PQfinish (link -> conn );
169+ }
168170 PGG (num_links )-- ;
169171
170172 zend_hash_del (& PGG (regular_list ), link -> hash );
171173
172174 link -> conn = NULL ;
173175 zend_string_release (link -> hash );
176+
174177 if (link -> notices ) {
175178 zend_hash_destroy (link -> notices );
179+ FREE_HASHTABLE (link -> notices );
180+ link -> notices = NULL ;
176181 }
177182}
178183
@@ -290,48 +295,45 @@ static inline char * _php_pgsql_trim_result(PGconn * pgsql, char **buf)
290295
291296static void php_pgsql_set_default_link (pgsql_link_handle * link )
292297{
293- if (PGG (default_link ) != NULL ) {
294- pgsql_link_free (FETCH_DEFAULT_LINK ());
295- }
296-
297298 PGG (default_link ) = link ;
298299}
299300
300301static void _close_pgsql_plink (zend_resource * rsrc )
301302{
302- PGconn * link = (PGconn * )rsrc -> ptr ;
303- PGresult * res ;
303+ if (rsrc -> ptr ) {
304+ PGconn * link = (PGconn * )rsrc -> ptr ;
305+ PGresult * res ;
304306
305- while ((res = PQgetResult (link ))) {
306- PQclear (res );
307+ while ((res = PQgetResult (link ))) {
308+ PQclear (res );
309+ }
310+ PQfinish (link );
311+ PGG (num_persistent )-- ;
312+ PGG (num_links )-- ;
313+ rsrc -> ptr = NULL ;
307314 }
308- PQfinish (link );
309- PGG (num_persistent )-- ;
310- PGG (num_links )-- ;
311315}
312316
313- static void _php_pgsql_notice_handler (void * link , const char * message )
317+ static void _php_pgsql_notice_handler (void * l , const char * message )
314318{
315- HashTable * notices , tmp_notices ;
319+ pgsql_link_handle * link ;
316320 zval tmp ;
317321 char * trimmed_message ;
318322 size_t trimmed_message_len ;
319323
320324 if (! PGG (ignore_notices )) {
321- notices = ((pgsql_link_handle * ) link )-> notices ;
322- if (!notices ) {
323- zend_hash_init (& tmp_notices , 1 , NULL , ZVAL_PTR_DTOR , 0 );
324- notices = & tmp_notices ;
325+ link = ((pgsql_link_handle * ) l );
326+ if (!link -> notices ) {
327+ link -> notices = zend_new_array (1 );
325328 }
326329 trimmed_message = _php_pgsql_trim_message (message , & trimmed_message_len );
327330 if (PGG (log_notices )) {
328331 php_error_docref (NULL , E_NOTICE , "%s" , trimmed_message );
329332 }
330333
331334 ZVAL_STRINGL (& tmp , trimmed_message , trimmed_message_len );
332- zend_hash_next_index_insert (notices , & tmp );
335+ zend_hash_next_index_insert (link -> notices , & tmp );
333336 efree (trimmed_message );
334- zval_ptr_dtor (& tmp );
335337 }
336338}
337339
@@ -341,8 +343,9 @@ static int _rollback_transactions(zval *el)
341343 PGresult * res ;
342344 zend_resource * rsrc = Z_RES_P (el );
343345
344- if (rsrc -> type != le_plink )
345- return 0 ;
346+ if (rsrc -> type != le_plink ) {
347+ return ZEND_HASH_APPLY_KEEP ;
348+ }
346349
347350 link = (PGconn * ) rsrc -> ptr ;
348351
@@ -362,7 +365,7 @@ static int _rollback_transactions(zval *el)
362365 PGG (ignore_notices ) = orig ;
363366 }
364367
365- return 0 ;
368+ return ZEND_HASH_APPLY_KEEP ;
366369}
367370
368371static void _free_ptr (zend_resource * rsrc )
@@ -410,7 +413,6 @@ static PHP_GINIT_FUNCTION(pgsql)
410413 ZEND_TSRMLS_CACHE_UPDATE ();
411414#endif
412415 memset (pgsql_globals , 0 , sizeof (zend_pgsql_globals ));
413- /* Initialize notice message hash at MINIT only */
414416 zend_hash_init (& pgsql_globals -> regular_list , 0 , NULL , ZVAL_PTR_DTOR , 1 );
415417}
416418
@@ -596,8 +598,6 @@ PHP_RINIT_FUNCTION(pgsql)
596598
597599PHP_RSHUTDOWN_FUNCTION (pgsql )
598600{
599- /* clean up notice messages */
600- zend_hash_clean (& PGG (regular_list ));
601601 /* clean up persistent connection */
602602 zend_hash_apply (& EG (persistent_list ), (apply_func_t ) _rollback_transactions );
603603 return SUCCESS ;
@@ -717,6 +717,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
717717 link -> conn = pgsql ;
718718 link -> hash = zend_string_copy (str .s );
719719 link -> notices = NULL ;
720+ link -> persistent = 1 ;
720721 } else { /* Non persistent connection */
721722 zval * index_ptr , new_index_ptr ;
722723
@@ -764,6 +765,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
764765 link -> conn = pgsql ;
765766 link -> hash = zend_string_copy (str .s );
766767 link -> notices = NULL ;
768+ link -> persistent = 0 ;
767769
768770 /* add it to the hash */
769771 ZVAL_COPY (& new_index_ptr , return_value );
@@ -841,15 +843,16 @@ PHP_FUNCTION(pg_close)
841843 if (!pgsql_link ) {
842844 link = FETCH_DEFAULT_LINK ();
843845 CHECK_DEFAULT_LINK (link );
846+ zend_hash_del (& PGG (regular_list ), link -> hash );
844847 PGG (default_link ) = NULL ;
845- pgsql_link_free (link );
846848 RETURN_TRUE ;
847849 }
848850
849851 link = Z_PGSQL_LINK_P (pgsql_link );
850852 CHECK_PGSQL_LINK (link );
851853
852854 if (link == FETCH_DEFAULT_LINK ()) {
855+ zend_hash_del (& PGG (regular_list ), link -> hash );
853856 PGG (default_link ) = NULL ;
854857 }
855858 pgsql_link_free (link );
@@ -3369,7 +3372,6 @@ PHP_FUNCTION(pg_escape_bytea)
33693372 RETURN_THROWS ();
33703373 }
33713374 link = FETCH_DEFAULT_LINK ();
3372- CHECK_DEFAULT_LINK (link );
33733375 break ;
33743376 default :
33753377 if (zend_parse_parameters (ZEND_NUM_ARGS (), "OS" , & pgsql_link , pgsql_link_ce , & from ) == FAILURE ) {
@@ -5683,7 +5685,6 @@ PHP_FUNCTION(pg_update)
56835685 zval * pgsql_link , * values , * ids ;
56845686 pgsql_link_handle * link ;
56855687 zend_string * table ;
5686- size_t table_len ;
56875688 zend_ulong option = PGSQL_DML_EXEC ;
56885689 PGconn * pg_link ;
56895690 zend_string * sql = NULL ;
0 commit comments