@@ -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
@@ -281,46 +286,46 @@ static zend_string *_php_pgsql_trim_message(const char *message)
281286
282287static void php_pgsql_set_default_link (pgsql_link_handle * link )
283288{
284- if (PGG (default_link ) != NULL ) {
285- pgsql_link_free (FETCH_DEFAULT_LINK ());
286- }
287-
288289 PGG (default_link ) = link ;
289290}
290291
291292static void _close_pgsql_plink (zend_resource * rsrc )
292293{
293- PGconn * link = (PGconn * )rsrc -> ptr ;
294- PGresult * res ;
294+ if (rsrc -> ptr ) {
295+ PGconn * link = (PGconn * )rsrc -> ptr ;
296+ PGresult * res ;
295297
296- while ((res = PQgetResult (link ))) {
297- PQclear (res );
298+ while ((res = PQgetResult (link ))) {
299+ PQclear (res );
300+ }
301+ PQfinish (link );
302+ PGG (num_persistent )-- ;
303+ PGG (num_links )-- ;
304+ rsrc -> ptr = NULL ;
298305 }
299- PQfinish (link );
300- PGG (num_persistent )-- ;
301- PGG (num_links )-- ;
302306}
303307
304- static void _php_pgsql_notice_handler (void * link , const char * message )
308+ static void _php_pgsql_notice_handler (void * l , const char * message )
305309{
306310 if (PGG (ignore_notices )) {
307311 return ;
308312 }
309313
314+ pgsql_link_handle * link ;
310315 HashTable * notices , tmp_notices ;
311316 zval tmp ;
312- zval * notices = notices = ((pgsql_link_handle * ) link )-> notices ;
313- if (!notices ) {
314- array_init (& tmp );
315- notices = & tmp ;
316- zend_hash_index_update (& PGG (notices ), (zend_ulong )resource_id , notices );
317+
318+ link = ((pgsql_link_handle * ) l );
319+ if (!link -> notices ) {
320+ link -> notices = zend_new_array (1 );
317321 }
318322
319323 zend_string * trimmed_message = _php_pgsql_trim_message (message );
320324 if (PGG (log_notices )) {
321325 php_error_docref (NULL , E_NOTICE , "%s" , ZSTR_VAL (trimmed_message ));
322326 }
323- add_next_index_str (notices , trimmed_message );
327+
328+ add_next_index_str (link -> notices , trimmed_message );
324329}
325330
326331static int _rollback_transactions (zval * el )
@@ -329,8 +334,9 @@ static int _rollback_transactions(zval *el)
329334 PGresult * res ;
330335 zend_resource * rsrc = Z_RES_P (el );
331336
332- if (rsrc -> type != le_plink )
333- return 0 ;
337+ if (rsrc -> type != le_plink ) {
338+ return ZEND_HASH_APPLY_KEEP ;
339+ }
334340
335341 link = (PGconn * ) rsrc -> ptr ;
336342
@@ -350,7 +356,7 @@ static int _rollback_transactions(zval *el)
350356 PGG (ignore_notices ) = orig ;
351357 }
352358
353- return 0 ;
359+ return ZEND_HASH_APPLY_KEEP ;
354360}
355361
356362static void _free_ptr (zend_resource * rsrc )
@@ -403,7 +409,6 @@ static PHP_GINIT_FUNCTION(pgsql)
403409 ZEND_TSRMLS_CACHE_UPDATE ();
404410#endif
405411 memset (pgsql_globals , 0 , sizeof (zend_pgsql_globals ));
406- /* Initialize notice message hash at MINIT only */
407412 zend_hash_init (& pgsql_globals -> regular_list , 0 , NULL , ZVAL_PTR_DTOR , 1 );
408413}
409414
@@ -590,11 +595,8 @@ PHP_RINIT_FUNCTION(pgsql)
590595
591596PHP_RSHUTDOWN_FUNCTION (pgsql )
592597{
593- /* clean up notice messages */
594- zend_hash_clean (& PGG (hashes ));
595598 zend_hash_destroy (& PGG (field_oids ));
596599 zend_hash_destroy (& PGG (table_oids ));
597- zend_hash_clean (& PGG (regular_list ));
598600 /* clean up persistent connection */
599601 zend_hash_apply (& EG (persistent_list ), (apply_func_t ) _rollback_transactions );
600602 return SUCCESS ;
@@ -714,6 +716,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
714716 link -> conn = pgsql ;
715717 link -> hash = zend_string_copy (str .s );
716718 link -> notices = NULL ;
719+ link -> persistent = 1 ;
717720 } else { /* Non persistent connection */
718721 zval * index_ptr , new_index_ptr ;
719722
@@ -761,6 +764,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
761764 link -> conn = pgsql ;
762765 link -> hash = zend_string_copy (str .s );
763766 link -> notices = NULL ;
767+ link -> persistent = 0 ;
764768
765769 /* add it to the hash */
766770 ZVAL_COPY (& new_index_ptr , return_value );
@@ -838,15 +842,16 @@ PHP_FUNCTION(pg_close)
838842 if (!pgsql_link ) {
839843 link = FETCH_DEFAULT_LINK ();
840844 CHECK_DEFAULT_LINK (link );
845+ zend_hash_del (& PGG (regular_list ), link -> hash );
841846 PGG (default_link ) = NULL ;
842- pgsql_link_free (link );
843847 RETURN_TRUE ;
844848 }
845849
846850 link = Z_PGSQL_LINK_P (pgsql_link );
847851 CHECK_PGSQL_LINK (link );
848852
849853 if (link == FETCH_DEFAULT_LINK ()) {
854+ zend_hash_del (& PGG (regular_list ), link -> hash );
850855 PGG (default_link ) = NULL ;
851856 }
852857 pgsql_link_free (link );
@@ -3328,7 +3333,6 @@ PHP_FUNCTION(pg_escape_bytea)
33283333 RETURN_THROWS ();
33293334 }
33303335 link = FETCH_DEFAULT_LINK ();
3331- CHECK_DEFAULT_LINK (link );
33323336 break ;
33333337 default :
33343338 if (zend_parse_parameters (ZEND_NUM_ARGS (), "OS" , & pgsql_link , pgsql_link_ce , & from ) == FAILURE ) {
@@ -5643,7 +5647,6 @@ PHP_FUNCTION(pg_update)
56435647 zval * pgsql_link , * values , * ids ;
56445648 pgsql_link_handle * link ;
56455649 zend_string * table ;
5646- size_t table_len ;
56475650 zend_ulong option = PGSQL_DML_EXEC ;
56485651 PGconn * pg_link ;
56495652 zend_string * sql = NULL ;
0 commit comments