@@ -167,15 +167,20 @@ static void pgsql_link_free(pgsql_link_handle *link)
167
167
while ((res = PQgetResult (link -> conn ))) {
168
168
PQclear (res );
169
169
}
170
- PQfinish (link -> conn );
170
+ if (!link -> persistent ) {
171
+ PQfinish (link -> conn );
172
+ }
171
173
PGG (num_links )-- ;
172
174
173
175
zend_hash_del (& PGG (regular_list ), link -> hash );
174
176
175
177
link -> conn = NULL ;
176
178
zend_string_release (link -> hash );
179
+
177
180
if (link -> notices ) {
178
181
zend_hash_destroy (link -> notices );
182
+ FREE_HASHTABLE (link -> notices );
183
+ link -> notices = NULL ;
179
184
}
180
185
}
181
186
@@ -293,48 +298,45 @@ static inline char * _php_pgsql_trim_result(PGconn * pgsql, char **buf)
293
298
294
299
static void php_pgsql_set_default_link (pgsql_link_handle * link )
295
300
{
296
- if (PGG (default_link ) != NULL ) {
297
- pgsql_link_free (FETCH_DEFAULT_LINK ());
298
- }
299
-
300
301
PGG (default_link ) = link ;
301
302
}
302
303
303
304
static void _close_pgsql_plink (zend_resource * rsrc )
304
305
{
305
- PGconn * link = (PGconn * )rsrc -> ptr ;
306
- PGresult * res ;
306
+ if (rsrc -> ptr ) {
307
+ PGconn * link = (PGconn * )rsrc -> ptr ;
308
+ PGresult * res ;
307
309
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 ;
310
317
}
311
- PQfinish (link );
312
- PGG (num_persistent )-- ;
313
- PGG (num_links )-- ;
314
318
}
315
319
316
- static void _php_pgsql_notice_handler (void * link , const char * message )
320
+ static void _php_pgsql_notice_handler (void * l , const char * message )
317
321
{
318
- HashTable * notices , tmp_notices ;
322
+ pgsql_link_handle * link ;
319
323
zval tmp ;
320
324
char * trimmed_message ;
321
325
size_t trimmed_message_len ;
322
326
323
327
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 );
328
331
}
329
332
trimmed_message = _php_pgsql_trim_message (message , & trimmed_message_len );
330
333
if (PGG (log_notices )) {
331
334
php_error_docref (NULL , E_NOTICE , "%s" , trimmed_message );
332
335
}
333
336
334
337
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 );
336
339
efree (trimmed_message );
337
- zval_ptr_dtor (& tmp );
338
340
}
339
341
}
340
342
@@ -344,8 +346,9 @@ static int _rollback_transactions(zval *el)
344
346
PGresult * res ;
345
347
zend_resource * rsrc = Z_RES_P (el );
346
348
347
- if (rsrc -> type != le_plink )
348
- return 0 ;
349
+ if (rsrc -> type != le_plink ) {
350
+ return ZEND_HASH_APPLY_KEEP ;
351
+ }
349
352
350
353
link = (PGconn * ) rsrc -> ptr ;
351
354
@@ -365,7 +368,7 @@ static int _rollback_transactions(zval *el)
365
368
PGG (ignore_notices ) = orig ;
366
369
}
367
370
368
- return 0 ;
371
+ return ZEND_HASH_APPLY_KEEP ;
369
372
}
370
373
371
374
static void _free_ptr (zend_resource * rsrc )
@@ -413,7 +416,6 @@ static PHP_GINIT_FUNCTION(pgsql)
413
416
ZEND_TSRMLS_CACHE_UPDATE ();
414
417
#endif
415
418
memset (pgsql_globals , 0 , sizeof (zend_pgsql_globals ));
416
- /* Initialize notice message hash at MINIT only */
417
419
zend_hash_init (& pgsql_globals -> regular_list , 0 , NULL , ZVAL_PTR_DTOR , 1 );
418
420
}
419
421
@@ -599,8 +601,6 @@ PHP_RINIT_FUNCTION(pgsql)
599
601
600
602
PHP_RSHUTDOWN_FUNCTION (pgsql )
601
603
{
602
- /* clean up notice messages */
603
- zend_hash_clean (& PGG (regular_list ));
604
604
/* clean up persistent connection */
605
605
zend_hash_apply (& EG (persistent_list ), (apply_func_t ) _rollback_transactions );
606
606
return SUCCESS ;
@@ -720,6 +720,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
720
720
link -> conn = pgsql ;
721
721
link -> hash = zend_string_copy (str .s );
722
722
link -> notices = NULL ;
723
+ link -> persistent = 1 ;
723
724
} else { /* Non persistent connection */
724
725
zval * index_ptr , new_index_ptr ;
725
726
@@ -767,6 +768,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
767
768
link -> conn = pgsql ;
768
769
link -> hash = zend_string_copy (str .s );
769
770
link -> notices = NULL ;
771
+ link -> persistent = 0 ;
770
772
771
773
/* add it to the hash */
772
774
ZVAL_COPY (& new_index_ptr , return_value );
@@ -844,15 +846,16 @@ PHP_FUNCTION(pg_close)
844
846
if (!pgsql_link ) {
845
847
link = FETCH_DEFAULT_LINK ();
846
848
CHECK_DEFAULT_LINK (link );
849
+ zend_hash_del (& PGG (regular_list ), link -> hash );
847
850
PGG (default_link ) = NULL ;
848
- pgsql_link_free (link );
849
851
RETURN_TRUE ;
850
852
}
851
853
852
854
link = Z_PGSQL_LINK_P (pgsql_link );
853
855
CHECK_PGSQL_LINK (link );
854
856
855
857
if (link == FETCH_DEFAULT_LINK ()) {
858
+ zend_hash_del (& PGG (regular_list ), link -> hash );
856
859
PGG (default_link ) = NULL ;
857
860
}
858
861
pgsql_link_free (link );
@@ -3372,7 +3375,6 @@ PHP_FUNCTION(pg_escape_bytea)
3372
3375
RETURN_THROWS ();
3373
3376
}
3374
3377
link = FETCH_DEFAULT_LINK ();
3375
- CHECK_DEFAULT_LINK (link );
3376
3378
break ;
3377
3379
default :
3378
3380
if (zend_parse_parameters (ZEND_NUM_ARGS (), "Os" , & pgsql_link , pgsql_link_ce , & from , & from_len ) == FAILURE ) {
0 commit comments