@@ -167,15 +167,20 @@ static void pgsql_link_free(pgsql_link_handle *link)
167167 while ((res = PQgetResult (link -> conn ))) {
168168 PQclear (res );
169169 }
170- PQfinish (link -> conn );
170+ if (!link -> persistent ) {
171+ PQfinish (link -> conn );
172+ }
171173 PGG (num_links )-- ;
172174
173175 zend_hash_del (& PGG (regular_list ), link -> hash );
174176
175177 link -> conn = NULL ;
176178 zend_string_release (link -> hash );
179+
177180 if (link -> notices ) {
178181 zend_hash_destroy (link -> notices );
182+ FREE_HASHTABLE (link -> notices );
183+ link -> notices = NULL ;
179184 }
180185}
181186
@@ -293,48 +298,45 @@ static inline char * _php_pgsql_trim_result(PGconn * pgsql, char **buf)
293298
294299static void php_pgsql_set_default_link (pgsql_link_handle * link )
295300{
296- if (PGG (default_link ) != NULL ) {
297- pgsql_link_free (FETCH_DEFAULT_LINK ());
298- }
299-
300301 PGG (default_link ) = link ;
301302}
302303
303304static void _close_pgsql_plink (zend_resource * rsrc )
304305{
305- PGconn * link = (PGconn * )rsrc -> ptr ;
306- PGresult * res ;
306+ if (rsrc -> ptr ) {
307+ PGconn * link = (PGconn * )rsrc -> ptr ;
308+ PGresult * res ;
307309
308- while ((res = PQgetResult (link ))) {
309- PQclear (res );
310+ while ((res = PQgetResult (link ))) {
311+ PQclear (res );
312+ }
313+ PQfinish (link );
314+ PGG (num_persistent )-- ;
315+ PGG (num_links )-- ;
316+ rsrc -> ptr = NULL ;
310317 }
311- PQfinish (link );
312- PGG (num_persistent )-- ;
313- PGG (num_links )-- ;
314318}
315319
316- static void _php_pgsql_notice_handler (void * link , const char * message )
320+ static void _php_pgsql_notice_handler (void * l , const char * message )
317321{
318- HashTable * notices , tmp_notices ;
322+ pgsql_link_handle * link ;
319323 zval tmp ;
320324 char * trimmed_message ;
321325 size_t trimmed_message_len ;
322326
323327 if (! PGG (ignore_notices )) {
324- notices = ((pgsql_link_handle * ) link )-> notices ;
325- if (!notices ) {
326- zend_hash_init (& tmp_notices , 1 , NULL , ZVAL_PTR_DTOR , 0 );
327- notices = & tmp_notices ;
328+ link = ((pgsql_link_handle * ) l );
329+ if (!link -> notices ) {
330+ link -> notices = zend_new_array (1 );
328331 }
329332 trimmed_message = _php_pgsql_trim_message (message , & trimmed_message_len );
330333 if (PGG (log_notices )) {
331334 php_error_docref (NULL , E_NOTICE , "%s" , trimmed_message );
332335 }
333336
334337 ZVAL_STRINGL (& tmp , trimmed_message , trimmed_message_len );
335- zend_hash_next_index_insert (notices , & tmp );
338+ zend_hash_next_index_insert (link -> notices , & tmp );
336339 efree (trimmed_message );
337- zval_ptr_dtor (& tmp );
338340 }
339341}
340342
@@ -344,8 +346,9 @@ static int _rollback_transactions(zval *el)
344346 PGresult * res ;
345347 zend_resource * rsrc = Z_RES_P (el );
346348
347- if (rsrc -> type != le_plink )
348- return 0 ;
349+ if (rsrc -> type != le_plink ) {
350+ return ZEND_HASH_APPLY_KEEP ;
351+ }
349352
350353 link = (PGconn * ) rsrc -> ptr ;
351354
@@ -365,7 +368,7 @@ static int _rollback_transactions(zval *el)
365368 PGG (ignore_notices ) = orig ;
366369 }
367370
368- return 0 ;
371+ return ZEND_HASH_APPLY_KEEP ;
369372}
370373
371374static void _free_ptr (zend_resource * rsrc )
@@ -413,7 +416,6 @@ static PHP_GINIT_FUNCTION(pgsql)
413416 ZEND_TSRMLS_CACHE_UPDATE ();
414417#endif
415418 memset (pgsql_globals , 0 , sizeof (zend_pgsql_globals ));
416- /* Initialize notice message hash at MINIT only */
417419 zend_hash_init (& pgsql_globals -> regular_list , 0 , NULL , ZVAL_PTR_DTOR , 1 );
418420}
419421
@@ -599,8 +601,6 @@ PHP_RINIT_FUNCTION(pgsql)
599601
600602PHP_RSHUTDOWN_FUNCTION (pgsql )
601603{
602- /* clean up notice messages */
603- zend_hash_clean (& PGG (regular_list ));
604604 /* clean up persistent connection */
605605 zend_hash_apply (& EG (persistent_list ), (apply_func_t ) _rollback_transactions );
606606 return SUCCESS ;
@@ -720,6 +720,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
720720 link -> conn = pgsql ;
721721 link -> hash = zend_string_copy (str .s );
722722 link -> notices = NULL ;
723+ link -> persistent = 1 ;
723724 } else { /* Non persistent connection */
724725 zval * index_ptr , new_index_ptr ;
725726
@@ -767,6 +768,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
767768 link -> conn = pgsql ;
768769 link -> hash = zend_string_copy (str .s );
769770 link -> notices = NULL ;
771+ link -> persistent = 0 ;
770772
771773 /* add it to the hash */
772774 ZVAL_COPY (& new_index_ptr , return_value );
@@ -844,15 +846,16 @@ PHP_FUNCTION(pg_close)
844846 if (!pgsql_link ) {
845847 link = FETCH_DEFAULT_LINK ();
846848 CHECK_DEFAULT_LINK (link );
849+ zend_hash_del (& PGG (regular_list ), link -> hash );
847850 PGG (default_link ) = NULL ;
848- pgsql_link_free (link );
849851 RETURN_TRUE ;
850852 }
851853
852854 link = Z_PGSQL_LINK_P (pgsql_link );
853855 CHECK_PGSQL_LINK (link );
854856
855857 if (link == FETCH_DEFAULT_LINK ()) {
858+ zend_hash_del (& PGG (regular_list ), link -> hash );
856859 PGG (default_link ) = NULL ;
857860 }
858861 pgsql_link_free (link );
@@ -3372,7 +3375,6 @@ PHP_FUNCTION(pg_escape_bytea)
33723375 RETURN_THROWS ();
33733376 }
33743377 link = FETCH_DEFAULT_LINK ();
3375- CHECK_DEFAULT_LINK (link );
33763378 break ;
33773379 default :
33783380 if (zend_parse_parameters (ZEND_NUM_ARGS (), "Os" , & pgsql_link , pgsql_link_ce , & from , & from_len ) == FAILURE ) {
0 commit comments