1616 | Jouni Ahto <[email protected] > | 1717 | Yasuo Ohgaki <[email protected] > | 1818 | Youichi Iwakiri <[email protected] > (pg_copy_*) | 19+ | Chris Kings-Lynne <[email protected] > (v3 protocol) | 1920 +----------------------------------------------------------------------+
2021 */
2122
@@ -177,6 +178,9 @@ function_entry pgsql_functions[] = {
177178 PHP_FE (pg_escape_bytea , NULL )
178179 PHP_FE (pg_unescape_bytea , NULL )
179180#endif
181+ #if HAVE_PQSETERRORVERBOSITY
182+ PHP_FE (pg_set_error_verbosity , NULL )
183+ #endif
180184#if HAVE_PQCLIENTENCODING
181185 PHP_FE (pg_client_encoding , NULL )
182186 PHP_FE (pg_set_client_encoding , NULL )
@@ -466,6 +470,12 @@ PHP_MINIT_FUNCTION(pgsql)
466470 REGISTER_LONG_CONSTANT ("PGSQL_TRANSACTION_INTRANS" , PQTRANS_INTRANS , CONST_CS | CONST_PERSISTENT );
467471 REGISTER_LONG_CONSTANT ("PGSQL_TRANSACTION_INERROR" , PQTRANS_INERROR , CONST_CS | CONST_PERSISTENT );
468472 REGISTER_LONG_CONSTANT ("PGSQL_TRANSACTION_UNKNOWN" , PQTRANS_UNKNOWN , CONST_CS | CONST_PERSISTENT );
473+ #endif
474+ #if HAVE_PQSETERRORVERBOSITY
475+ /* For pg_set_error_verbosity() */
476+ REGISTER_LONG_CONSTANT ("PGSQL_ERRORS_TERSE" , PQERRORS_TERSE , CONST_CS | CONST_PERSISTENT );
477+ REGISTER_LONG_CONSTANT ("PGSQL_ERRORS_DEFAULT" , PQERRORS_DEFAULT , CONST_CS | CONST_PERSISTENT );
478+ REGISTER_LONG_CONSTANT ("PGSQL_ERRORS_VERBOSE" , PQERRORS_VERBOSE , CONST_CS | CONST_PERSISTENT );
469479#endif
470480 /* For lo_seek() */
471481 REGISTER_LONG_CONSTANT ("PGSQL_SEEK_SET" , SEEK_SET , CONST_CS | CONST_PERSISTENT );
@@ -2905,6 +2915,51 @@ PHP_FUNCTION(pg_lo_tell)
29052915}
29062916/* }}} */
29072917
2918+ #if HAVE_PQSETERRORVERBOSITY
2919+ /* {{{ proto int pg_set_error_verbosity([resource connection,] int verbosity)
2920+ Set error verbosity */
2921+ PHP_FUNCTION (pg_set_error_verbosity )
2922+ {
2923+ zval * * verbosity , * * pgsql_link = NULL ;
2924+ long val ;
2925+ int id = -1 ;
2926+ PGconn * pgsql ;
2927+
2928+ switch (ZEND_NUM_ARGS ()) {
2929+ case 1 :
2930+ if (zend_get_parameters_ex (1 , & verbosity )== FAILURE ) {
2931+ RETURN_FALSE ;
2932+ }
2933+ id = PGG (default_link );
2934+ CHECK_DEFAULT_LINK (id );
2935+ break ;
2936+ case 2 :
2937+ if (zend_get_parameters_ex (2 , & pgsql_link , & verbosity )== FAILURE ) {
2938+ RETURN_FALSE ;
2939+ }
2940+ break ;
2941+ default :
2942+ WRONG_PARAM_COUNT ;
2943+ break ;
2944+ }
2945+ if (pgsql_link == NULL && id == -1 ) {
2946+ RETURN_FALSE ;
2947+ }
2948+
2949+ ZEND_FETCH_RESOURCE2 (pgsql , PGconn * , pgsql_link , id , "PostgreSQL link" , le_link , le_plink );
2950+
2951+ convert_to_long_ex (verbosity );
2952+ val = Z_LVAL_PP (verbosity );
2953+ if (val & (PQERRORS_TERSE |PQERRORS_DEFAULT |PQERRORS_VERBOSE )) {
2954+ Z_LVAL_P (return_value ) = PQsetErrorVerbosity (pgsql , val );
2955+ Z_TYPE_P (return_value ) = IS_LONG ;
2956+ } else {
2957+ RETURN_FALSE ;
2958+ }
2959+ }
2960+ /* }}} */
2961+ #endif
2962+
29082963#ifdef HAVE_PQCLIENTENCODING
29092964/* {{{ proto int pg_set_client_encoding([resource connection,] string encoding)
29102965 Set client encoding */
@@ -2987,8 +3042,9 @@ PHP_FUNCTION(pg_client_encoding)
29873042/* }}} */
29883043#endif
29893044
2990-
3045+ #if ! HAVE_PQGETCOPYDATA
29913046#define COPYBUFSIZ 8192
3047+ #endif
29923048
29933049/* {{{ proto bool pg_end_copy([resource connection])
29943050 Sync with backend. Completes the Copy command */
@@ -3064,7 +3120,6 @@ PHP_FUNCTION(pg_put_line)
30643120
30653121 convert_to_string_ex (query );
30663122 result = PQputline (pgsql , Z_STRVAL_PP (query ));
3067-
30683123 if (result == EOF ) {
30693124 PHP_PQ_ERROR ("Query failed: %s" , pgsql );
30703125 RETURN_FALSE ;
@@ -3087,7 +3142,9 @@ PHP_FUNCTION(pg_copy_to)
30873142 PGresult * pgsql_result ;
30883143 ExecStatusType status ;
30893144 int copydone = 0 ;
3145+ #if !HAVE_PQGETCOPYDATA
30903146 char copybuf [COPYBUFSIZ ];
3147+ #endif
30913148 char * csv = (char * )NULL ;
30923149 int ret ;
30933150 int argc = ZEND_NUM_ARGS ();
@@ -3128,6 +3185,26 @@ PHP_FUNCTION(pg_copy_to)
31283185 if (pgsql_result ) {
31293186 PQclear (pgsql_result );
31303187 array_init (return_value );
3188+ #if HAVE_PQGETCOPYDATA
3189+ while (!copydone )
3190+ {
3191+ ret = PQgetCopyData (pgsql , & csv , 0 );
3192+ switch (ret ) {
3193+ case -1 :
3194+ copydone = 1 ;
3195+ break ;
3196+ case 0 :
3197+ case -2 :
3198+ PHP_PQ_ERROR ("getline failed: %s" , pgsql );
3199+ RETURN_FALSE ;
3200+ break ;
3201+ default :
3202+ add_next_index_string (return_value , csv , 1 );
3203+ PQfreemem (csv );
3204+ break ;
3205+ }
3206+ }
3207+ #else
31313208 while (!copydone )
31323209 {
31333210 if ((ret = PQgetline (pgsql , copybuf , COPYBUFSIZ ))) {
@@ -3168,6 +3245,7 @@ PHP_FUNCTION(pg_copy_to)
31683245 PHP_PQ_ERROR ("endcopy failed: %s" , pgsql );
31693246 RETURN_FALSE ;
31703247 }
3248+ #endif
31713249 while ((pgsql_result = PQgetResult (pgsql ))) {
31723250 PQclear (pgsql_result );
31733251 }
@@ -3238,13 +3316,33 @@ PHP_FUNCTION(pg_copy_from)
32383316 if (pgsql_result ) {
32393317 PQclear (pgsql_result );
32403318 zend_hash_internal_pointer_reset_ex (Z_ARRVAL_P (pg_rows ), & pos );
3319+ #if HAVE_PQPUTCOPYDATA
32413320 while (zend_hash_get_current_data_ex (Z_ARRVAL_P (pg_rows ), (void * * ) & tmp , & pos ) == SUCCESS ) {
32423321 convert_to_string_ex (tmp );
32433322 query = (char * )emalloc (Z_STRLEN_PP (tmp ) + 2 );
32443323 strcpy (query , Z_STRVAL_PP (tmp ));
32453324 if (* (query + Z_STRLEN_PP (tmp )- 1 ) != '\n' )
32463325 strcat (query , "\n" );
3247- if (PQputline (pgsql , query )) {
3326+ if (PQputCopyData (pgsql , query , strlen (query )) != 1 ) {
3327+ efree (query );
3328+ PHP_PQ_ERROR ("copy failed: %s" , pgsql );
3329+ RETURN_FALSE ;
3330+ }
3331+ efree (query );
3332+ zend_hash_move_forward_ex (Z_ARRVAL_P (pg_rows ), & pos );
3333+ }
3334+ if (PQputCopyEnd (pgsql , NULL ) != 1 ) {
3335+ PHP_PQ_ERROR ("putcopyend failed: %s" , pgsql );
3336+ RETURN_FALSE ;
3337+ }
3338+ #else
3339+ while (zend_hash_get_current_data_ex (Z_ARRVAL_P (pg_rows ), (void * * ) & tmp , & pos ) == SUCCESS ) {
3340+ convert_to_string_ex (tmp );
3341+ query = (char * )emalloc (Z_STRLEN_PP (tmp ) + 2 );
3342+ strcpy (query , Z_STRVAL_PP (tmp ));
3343+ if (* (query + Z_STRLEN_PP (tmp )- 1 ) != '\n' )
3344+ strcat (query , "\n" );
3345+ if (PQputline (pgsql , query )== EOF ) {
32483346 efree (query );
32493347 PHP_PQ_ERROR ("copy failed: %s" , pgsql );
32503348 RETURN_FALSE ;
@@ -3260,6 +3358,7 @@ PHP_FUNCTION(pg_copy_from)
32603358 PHP_PQ_ERROR ("endcopy failed: %s" , pgsql );
32613359 RETURN_FALSE ;
32623360 }
3361+ #endif
32633362 while ((pgsql_result = PQgetResult (pgsql ))) {
32643363 PQclear (pgsql_result );
32653364 }
@@ -3489,8 +3588,11 @@ PHP_FUNCTION(pg_result_error_field)
34893588 }
34903589 if (fieldcode & (PG_DIAG_SEVERITY |PG_DIAG_SQLSTATE |PG_DIAG_MESSAGE_PRIMARY |PG_DIAG_MESSAGE_DETAIL
34913590 |PG_DIAG_MESSAGE_HINT |PG_DIAG_STATEMENT_POSITION
3492- #if defined(PG_DIAG_INTERNAL_POSITION ) && defined (PG_DIAG_INTERNAL_QUERY )
3493- |PG_DIAG_INTERNAL_POSITION |PG_DIAG_INTERNAL_QUERY
3591+ #if PG_DIAG_INTERNAL_POSITION
3592+ |PG_DIAG_INTERNAL_POSITION
3593+ #endif
3594+ #if PG_DIAG_INTERNAL_QUERY
3595+ |PG_DIAG_INTERNAL_QUERY
34943596#endif
34953597 |PG_DIAG_CONTEXT |PG_DIAG_SOURCE_FILE |PG_DIAG_SOURCE_LINE
34963598 |PG_DIAG_SOURCE_FUNCTION )) {
0 commit comments