From 1fc2a27fa5444b9d2ffbd35c61ae41ca0b7f9227 Mon Sep 17 00:00:00 2001 From: Yasuo Ohgaki Date: Mon, 17 Dec 2001 00:24:59 +0000 Subject: [PATCH] Funtion rename: Functions are renamed accoding to coding stanard. Some functions has completely different name now. New functions: pg_copy_to()/pg_copy_from() - by Youichi Iwakiri pg_lo_tell()/pg_lo_seek()/pg_result_error()/pg_result_status() pg_escape_string()/pg_escape_bytea() - Only pgsql 7.2 or later. Fixed: Wrong mode when clean up connection resource. Compiler warning for pg_encoding_to_char(). # Please do not document new functions, yet. # For new functions and other info, please read README # in module dir. --- ext/pgsql/pgsql.c | 968 +++++++++++++++++++++++++++++------------- ext/pgsql/php_pgsql.h | 96 +++-- 2 files changed, 726 insertions(+), 338 deletions(-) diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c index 31ada9165e..154255c5ce 100644 --- a/ext/pgsql/pgsql.c +++ b/ext/pgsql/pgsql.c @@ -13,7 +13,9 @@ | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Zeev Suraski | - | Jouni Ahto (large object interface) | + | Jouni Ahto | + | Yasuo Ohgaki | + | Youichi Iwakiri (pg_copy_*) | +----------------------------------------------------------------------+ */ @@ -43,63 +45,109 @@ #define PGSQL_NUM 1<<1 #define PGSQL_BOTH (PGSQL_ASSOC|PGSQL_NUM) +#define PGSQL_ESCAPE_STRING 1 +#define PGSQL_ESCAPE_BYTEA 2 + #define CHECK_DEFAULT_LINK(x) if (x == -1) { php_error(E_WARNING, "%s: no PostgreSQL link opened yet", get_active_function_name(TSRMLS_C)); } /* {{{ pgsql_functions[] */ function_entry pgsql_functions[] = { + /* connection functions */ PHP_FE(pg_connect, NULL) PHP_FE(pg_pconnect, NULL) PHP_FE(pg_close, NULL) - PHP_FE(pg_cmdtuples, NULL) - PHP_FE(pg_last_notice, NULL) + PHP_FE(pg_connection_status, NULL) + PHP_FE(pg_connection_busy, NULL) + PHP_FE(pg_connection_reset, NULL) + PHP_FE(pg_host, NULL) PHP_FE(pg_dbname, NULL) - PHP_FE(pg_errormessage, NULL) - PHP_FE(pg_trace, NULL) - PHP_FE(pg_untrace, NULL) - PHP_FE(pg_options, NULL) PHP_FE(pg_port, NULL) PHP_FE(pg_tty, NULL) - PHP_FE(pg_host, NULL) - PHP_FE(pg_exec, NULL) - PHP_FE(pg_numrows, NULL) - PHP_FE(pg_numfields, NULL) - PHP_FE(pg_fieldname, NULL) - PHP_FE(pg_fieldsize, NULL) - PHP_FE(pg_fieldtype, NULL) - PHP_FE(pg_fieldnum, NULL) - PHP_FE(pg_result, NULL) + PHP_FE(pg_options, NULL) + /* query functions */ + PHP_FE(pg_query, NULL) + PHP_FE(pg_send_query, NULL) + PHP_FE(pg_cancel_query, NULL) + /* result functions */ + PHP_FE(pg_fetch_result, NULL) PHP_FE(pg_fetch_row, NULL) PHP_FE(pg_fetch_array, NULL) PHP_FE(pg_fetch_object, NULL) - PHP_FE(pg_fieldprtlen, NULL) - PHP_FE(pg_fieldisnull, NULL) - PHP_FE(pg_freeresult, NULL) - PHP_FE(pg_getlastoid, NULL) - PHP_FE(pg_locreate, NULL) - PHP_FE(pg_lounlink, NULL) - PHP_FE(pg_loopen, NULL) - PHP_FE(pg_loclose, NULL) - PHP_FE(pg_loread, NULL) - PHP_FE(pg_lowrite, NULL) - PHP_FE(pg_loreadall, NULL) - PHP_FE(pg_loimport, NULL) - PHP_FE(pg_loexport, NULL) + PHP_FE(pg_affected_rows,NULL) + PHP_FE(pg_get_result, NULL) + PHP_FE(pg_result_status,NULL) + PHP_FE(pg_free_result, NULL) + PHP_FE(pg_last_oid, NULL) + PHP_FE(pg_num_rows, NULL) + PHP_FE(pg_num_fields, NULL) + PHP_FE(pg_field_name, NULL) + PHP_FE(pg_field_num, NULL) + PHP_FE(pg_field_size, NULL) + PHP_FE(pg_field_type, NULL) + PHP_FE(pg_field_prtlen, NULL) + PHP_FE(pg_field_is_null,NULL) + /* error message functions */ + PHP_FE(pg_result_error, NULL) + PHP_FE(pg_last_error, NULL) + PHP_FE(pg_last_notice, NULL) + /* copy functions */ PHP_FE(pg_put_line, NULL) PHP_FE(pg_end_copy, NULL) + PHP_FE(pg_copy_to, NULL) + PHP_FE(pg_copy_from, NULL) + /* debug functions */ + PHP_FE(pg_trace, NULL) + PHP_FE(pg_untrace, NULL) + /* large object functions */ + PHP_FE(pg_lo_create, NULL) + PHP_FE(pg_lo_unlink, NULL) + PHP_FE(pg_lo_open, NULL) + PHP_FE(pg_lo_close, NULL) + PHP_FE(pg_lo_read, NULL) + PHP_FE(pg_lo_write, NULL) + PHP_FE(pg_lo_read_all, NULL) + PHP_FE(pg_lo_import, NULL) + PHP_FE(pg_lo_export, NULL) + PHP_FE(pg_lo_seek, NULL) + PHP_FE(pg_lo_tell, NULL) + /* utility functions */ +#if HAVE_PQESCAPE + PHP_FE(pg_escape_string,NULL) + PHP_FE(pg_escape_bytea, NULL) +#endif #if HAVE_PQCLIENTENCODING PHP_FE(pg_client_encoding, NULL) PHP_FE(pg_set_client_encoding, NULL) - /* for downwards compatibility */ - PHP_FALIAS(pg_clientencoding, pg_client_encoding, NULL) +#endif + /* aliases for downwards compatibility */ + PHP_FALIAS(pg_exec, pg_query, NULL) + PHP_FALIAS(pg_getlastoid, pg_last_oid, NULL) + PHP_FALIAS(pg_cmdtuples, pg_affected_rows, NULL) + PHP_FALIAS(pg_errormessage, pg_last_error, NULL) + PHP_FALIAS(pg_numrows, pg_num_rows, NULL) + PHP_FALIAS(pg_numfields, pg_num_fields, NULL) + PHP_FALIAS(pg_fieldname, pg_field_name, NULL) + PHP_FALIAS(pg_fieldsize, pg_field_size, NULL) + PHP_FALIAS(pg_fieldtype, pg_field_type, NULL) + PHP_FALIAS(pg_fieldnum, pg_field_num, NULL) + PHP_FALIAS(pg_fieldprtlen, pg_field_prtlen, NULL) + PHP_FALIAS(pg_fieldisnull, pg_field_is_null, NULL) + PHP_FALIAS(pg_freeresult, pg_free_result, NULL) + PHP_FALIAS(pg_result, pg_fetch_result, NULL) + PHP_FALIAS(pg_loreadall, pg_lo_read_all, NULL) + PHP_FALIAS(pg_locreate, pg_lo_create, NULL) + PHP_FALIAS(pg_lounlink, pg_lo_unlink, NULL) + PHP_FALIAS(pg_loopen, pg_lo_open, NULL) + PHP_FALIAS(pg_loclse, pg_lo_close, NULL) + PHP_FALIAS(pg_loread, pg_lo_read, NULL) + PHP_FALIAS(pg_lowrite, pg_lo_write, NULL) + PHP_FALIAS(pg_loimport, pg_lo_import, NULL) + PHP_FALIAS(pg_loexport, pg_lo_export, NULL) +#if HAVE_PQCLIENTENCODING + PHP_FALIAS(pg_clientencoding, pg_client_encoding, NULL) PHP_FALIAS(pg_setclientencoding, pg_set_client_encoding, NULL) #endif - PHP_FE(pg_reset, NULL) - PHP_FE(pg_status, NULL) - PHP_FE(pg_send_query, NULL) - PHP_FE(pg_request_cancel, NULL) - PHP_FE(pg_get_result, NULL) - PHP_FE(pg_is_busy, NULL) {NULL, NULL, NULL} }; /* }}} */ @@ -220,7 +268,7 @@ static int _rollback_transactions(zend_rsrc_list_entry *rsrc TSRMLS_DC) link = (PGconn *) rsrc->ptr; - PQsetnonblocking(link,1); + PQsetnonblocking(link,0); /* Just in case */ if (PQisBusy(link)) { if (!PQrequestCancel(link)) { php_error(E_WARNING,"PostgreSQL: failed to cancel qeury. %s", PQerrorMessage(link)); @@ -301,6 +349,22 @@ PHP_MINIT_FUNCTION(pgsql) REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_BAD", CONNECTION_BAD, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_OK", CONNECTION_OK, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_SEEK_SET", SEEK_SET, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_SEEK_CUR", SEEK_CUR, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_SEEK_END", SEEK_END, CONST_CS | CONST_PERSISTENT); + + REGISTER_LONG_CONSTANT("PGSQL_ESCAPE_STRING", PGSQL_ESCAPE_STRING, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_ESCAPE_BYTEA", PGSQL_ESCAPE_BYTEA, CONST_CS | CONST_PERSISTENT); + + REGISTER_LONG_CONSTANT("PGSQL_EMPTY_QUERY", PGRES_EMPTY_QUERY, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PSQL_COMMAND_OK", PGRES_COMMAND_OK, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_TUPLES_OK", PGRES_TUPLES_OK, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_COPY_OUT", PGRES_COPY_OUT, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_COPY_IN", PGRES_COPY_IN, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_BAD_RESPONSE", PGRES_BAD_RESPONSE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_NONFATAL_ERROR", PGRES_NONFATAL_ERROR, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_FATAL_ERROR", PGRES_FATAL_ERROR, CONST_CS | CONST_PERSISTENT); + return SUCCESS; } /* }}} */ @@ -710,9 +774,9 @@ PHP_FUNCTION(pg_dbname) } /* }}} */ -/* {{{ proto string pg_errormessage([resource connection]) +/* {{{ proto string pg_last_error([resource connection]) Get the error message string */ -PHP_FUNCTION(pg_errormessage) +PHP_FUNCTION(pg_last_error) { php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_ERROR_MESSAGE); } @@ -750,9 +814,9 @@ PHP_FUNCTION(pg_host) } /* }}} */ -/* {{{ proto resource pg_exec([resource connection,] string query) +/* {{{ proto resource pg_query([resource connection,] string query) Execute a query */ -PHP_FUNCTION(pg_exec) +PHP_FUNCTION(pg_query) { zval **query, **pgsql_link = NULL; int id = -1; @@ -792,7 +856,8 @@ PHP_FUNCTION(pg_exec) leftover = 1; } if (leftover) { - php_error(E_WARNING,"PostgreSQL: There are results on this connection. Use pg_get_result() to get results"); + php_error(E_WARNING,"%s() found results on this connection. Use pg_get_result() to get results", + get_active_function_name(TSRMLS_C)); } pgsql_result = PQexec(pgsql, Z_STRVAL_PP(query)); @@ -830,83 +895,6 @@ PHP_FUNCTION(pg_exec) } /* }}} */ -/* {{{ proto bool pg_end_copy([resource connection]) - Sync with backend. Completes the Copy command */ -PHP_FUNCTION(pg_end_copy) -{ - zval **pgsql_link = NULL; - int id = -1; - PGconn *pgsql; - int result = 0; - - switch(ZEND_NUM_ARGS()) { - case 0: - id = PGG(default_link); - CHECK_DEFAULT_LINK(id); - break; - case 1: - if (zend_get_parameters_ex(1, &pgsql_link)==FAILURE) { - RETURN_FALSE; - } - break; - default: - WRONG_PARAM_COUNT; - break; - } - - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); - - result = PQendcopy(pgsql); - - if (result!=0) { - php_error(E_WARNING, "PostgreSQL query failed: %s", PQerrorMessage(pgsql)); - RETURN_FALSE; - } - RETURN_TRUE; -} -/* }}} */ - -#define COPYBUFSIZ 8192 - -/* {{{ proto bool pg_put_line([resource connection,] string query) - Send null-terminated string to backend server*/ -PHP_FUNCTION(pg_put_line) -{ - zval **query, **pgsql_link = NULL; - int id = -1; - PGconn *pgsql; - int result = 0; - - switch(ZEND_NUM_ARGS()) { - case 1: - if (zend_get_parameters_ex(1, &query)==FAILURE) { - RETURN_FALSE; - } - id = PGG(default_link); - CHECK_DEFAULT_LINK(id); - break; - case 2: - if (zend_get_parameters_ex(2, &pgsql_link, &query)==FAILURE) { - RETURN_FALSE; - } - break; - default: - WRONG_PARAM_COUNT; - break; - } - - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); - - convert_to_string_ex(query); - result = PQputline(pgsql, Z_STRVAL_PP(query)); - - if (result==EOF) { - php_error(E_WARNING, "PostgreSQL query failed: %s", PQerrorMessage(pgsql)); - RETURN_FALSE; - } - RETURN_TRUE; -} -/* }}} */ #define PHP_PG_NUM_ROWS 1 #define PHP_PG_NUM_FIELDS 2 #define PHP_PG_CMD_TUPLES 3 @@ -926,7 +914,7 @@ void php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type) ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result); pgsql_result = pg_result->result; - + switch (entry_type) { case PHP_PG_NUM_ROWS: Z_LVAL_P(return_value) = PQntuples(pgsql_result); @@ -938,7 +926,8 @@ void php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type) #if HAVE_PQCMDTUPLES Z_LVAL_P(return_value) = atoi(PQcmdTuples(pgsql_result)); #else - php_error(E_WARNING,"This compilation does not support pg_cmdtuples()"); + php_error(E_WARNING,"This compilation does not support %s()", + get_active_function_name(TSRMLS_C); Z_LVAL_P(return_value) = 0; #endif break; @@ -949,25 +938,25 @@ void php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type) } /* }}} */ -/* {{{ proto int pg_numrows(resource result) +/* {{{ proto int pg_num_rows(resource result) Return the number of rows in the result */ -PHP_FUNCTION(pg_numrows) +PHP_FUNCTION(pg_num_rows) { php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_NUM_ROWS); } /* }}} */ -/* {{{ proto int pg_numfields(resource result) +/* {{{ proto int pg_num_fields(resource result) Return the number of fields in the result */ -PHP_FUNCTION(pg_numfields) +PHP_FUNCTION(pg_num_fields) { php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_NUM_FIELDS); } /* }}} */ -/* {{{ proto int pg_cmdtuples(resource result) +/* {{{ proto int pg_affected_rows(resource result) Returns the number of affected tuples */ -PHP_FUNCTION(pg_cmdtuples) +PHP_FUNCTION(pg_affected_rows) { php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_CMD_TUPLES); } @@ -1081,33 +1070,33 @@ void php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type) } /* }}} */ -/* {{{ proto string pg_fieldname(resource result, int field_number) +/* {{{ proto string pg_field_name(resource result, int field_number) Returns the name of the field */ -PHP_FUNCTION(pg_fieldname) +PHP_FUNCTION(pg_field_name) { php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_FIELD_NAME); } /* }}} */ -/* {{{ proto int pg_fieldsize(resource result, int field_number) +/* {{{ proto int pg_field_size(resource result, int field_number) Returns the internal size of the field */ -PHP_FUNCTION(pg_fieldsize) +PHP_FUNCTION(pg_field_size) { php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_FIELD_SIZE); } /* }}} */ -/* {{{ proto string pg_fieldtype(resource result, int field_number) +/* {{{ proto string pg_field_type(resource result, int field_number) Returns the type name for the given field */ -PHP_FUNCTION(pg_fieldtype) +PHP_FUNCTION(pg_field_type) { php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_FIELD_TYPE); } /* }}} */ -/* {{{ proto int pg_fieldnum(resource result, string field_name) +/* {{{ proto int pg_field_num(resource result, string field_name) Returns the field number of the named field */ -PHP_FUNCTION(pg_fieldnum) +PHP_FUNCTION(pg_field_num) { zval **result, **field; PGresult *pgsql_result; @@ -1129,7 +1118,7 @@ PHP_FUNCTION(pg_fieldnum) /* {{{ proto mixed pg_result(resource result, [int row_number,] mixed field_name) Returns values from a result identifier */ -PHP_FUNCTION(pg_result) +PHP_FUNCTION(pg_fetch_result) { zval **result, **row, **field=NULL; PGresult *pgsql_result; @@ -1374,25 +1363,25 @@ void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type) } /* }}} */ -/* {{{ proto int pg_fieldprtlen(resource result, [int row,] mixed field_name_or_number) +/* {{{ proto int pg_field_prtlen(resource result, [int row,] mixed field_name_or_number) Returns the printed length */ -PHP_FUNCTION(pg_fieldprtlen) +PHP_FUNCTION(pg_field_prtlen) { php_pgsql_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_DATA_LENGTH); } /* }}} */ -/* {{{ proto int pg_fieldisnull(resource result, [int row,] mixed field_name_or_number) +/* {{{ proto int pg_field_is_null(resource result, [int row,] mixed field_name_or_number) Test if a field is NULL */ -PHP_FUNCTION(pg_fieldisnull) +PHP_FUNCTION(pg_field_is_null) { php_pgsql_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_DATA_ISNULL); } /* }}} */ -/* {{{ proto bool pg_freeresult(resource result) +/* {{{ proto bool pg_free_result(resource result) Free result memory */ -PHP_FUNCTION(pg_freeresult) +PHP_FUNCTION(pg_free_result) { zval **result; pgsql_result_handle *pg_result; @@ -1410,9 +1399,9 @@ PHP_FUNCTION(pg_freeresult) } /* }}} */ -/* {{{ proto int pg_getlastoid(resource result) +/* {{{ proto int pg_last_oid(resource result) Returns the last object identifier */ -PHP_FUNCTION(pg_getlastoid) +PHP_FUNCTION(pg_last_oid) { zval **result; PGresult *pgsql_result; @@ -1424,7 +1413,14 @@ PHP_FUNCTION(pg_getlastoid) ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result); pgsql_result = pg_result->result; -#ifndef HAVE_PQOIDVALUE +#if HAVE_PQOIDVALUE + Z_LVAL_P(return_value) = (int) PQoidValue(pgsql_result); + if (Z_LVAL_P(return_value) == InvalidOid) { + RETURN_FALSE; + } else { + Z_TYPE_P(return_value) = IS_LONG; + } +#else Z_STRVAL_P(return_value) = (char *) PQoidStatus(pgsql_result); if (Z_STRVAL_P(return_value)) { Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value)); @@ -1433,13 +1429,6 @@ PHP_FUNCTION(pg_getlastoid) } else { Z_STRVAL_P(return_value) = empty_string; } -#else - Z_LVAL_P(return_value) = (int) PQoidValue(pgsql_result); - if (Z_LVAL_P(return_value) == InvalidOid) { - RETURN_FALSE; - } else { - Z_TYPE_P(return_value) = IS_LONG; - } #endif } /* }}} */ @@ -1529,9 +1518,9 @@ PHP_FUNCTION(pg_untrace) } /* }}} */ -/* {{{ proto int pg_locreate(resource connection) +/* {{{ proto int pg_lo_create(resource connection) Create a large object */ -PHP_FUNCTION(pg_locreate) +PHP_FUNCTION(pg_lo_create) { zval **pgsql_link = NULL; PGconn *pgsql; @@ -1573,9 +1562,9 @@ PHP_FUNCTION(pg_locreate) } /* }}} */ -/* {{{ proto bool pg_lounlink([resource connection,] int large_object_oid) +/* {{{ proto bool pg_lo_unlink([resource connection,] int large_object_oid) Delete a large object */ -PHP_FUNCTION(pg_lounlink) +PHP_FUNCTION(pg_lo_unlink) { zval **pgsql_link = NULL, **oid; PGconn *pgsql; @@ -1614,9 +1603,9 @@ PHP_FUNCTION(pg_lounlink) } /* }}} */ -/* {{{ proto resource pg_loopen([resource connection,] int large_object_oid, string mode) +/* {{{ proto resource pg_lo_open([resource connection,] int large_object_oid, string mode) Open a large object and return fd */ -PHP_FUNCTION(pg_loopen) +PHP_FUNCTION(pg_lo_open) { zval **pgsql_link = NULL, **oid, **mode; PGconn *pgsql; @@ -1716,9 +1705,9 @@ PHP_FUNCTION(pg_loopen) } /* }}} */ -/* {{{ proto bool pg_loclose(resource large_object) +/* {{{ proto bool pg_lo_close(resource large_object) Close a large object */ -PHP_FUNCTION(pg_loclose) +PHP_FUNCTION(pg_lo_close) { zval **pgsql_lofp; pgLofp *pgsql; @@ -1748,9 +1737,9 @@ PHP_FUNCTION(pg_loclose) } /* }}} */ -/* {{{ proto string pg_loread(resource large_object [, int len]) +/* {{{ proto string pg_lo_read(resource large_object [, int len]) Read a large object */ -PHP_FUNCTION(pg_loread) +PHP_FUNCTION(pg_lo_read) { zval **pgsql_id, **len; int buf_len = 1024, nbytes; @@ -1780,9 +1769,9 @@ PHP_FUNCTION(pg_loread) } /* }}} */ -/* {{{ proto int pg_lowrite(resource large_object, string buf [, int len]) +/* {{{ proto int pg_lo_write(resource large_object, string buf [, int len]) Write a large object */ -PHP_FUNCTION(pg_lowrite) +PHP_FUNCTION(pg_lo_write) { zval **pgsql_id, **str, **z_len; int nbytes; @@ -1815,9 +1804,9 @@ PHP_FUNCTION(pg_lowrite) } /* }}} */ -/* {{{ proto int pg_loreadall(resource large_object) +/* {{{ proto int pg_lo_read_all(resource large_object) Read a large object and send straight to browser */ -PHP_FUNCTION(pg_loreadall) +PHP_FUNCTION(pg_lo_read_all) { zval **pgsql_id; int i, tbytes; @@ -1846,46 +1835,45 @@ PHP_FUNCTION(pg_loreadall) } tbytes += i; } - Z_LVAL_P(return_value) = tbytes; - Z_TYPE_P(return_value) = IS_LONG; + RETURN_LONG(tbytes); } /* }}} */ -/* {{{ proto int pg_loimport(string filename [, resource connection]) +/* {{{ proto int pg_lo_import([resource connection, ] string filename) Import large object direct from filesystem */ -PHP_FUNCTION(pg_loimport) +PHP_FUNCTION(pg_lo_import) { - zval **pgsql_link = NULL, **file_in; - int id = -1; + zval *pgsql_link = NULL; + char *file_in; + int id = -1, name_len; + int argc = ZEND_NUM_ARGS(); PGconn *pgsql; Oid oid; - - switch (ZEND_NUM_ARGS()) { - case 1: - if (zend_get_parameters_ex(1, &file_in) == FAILURE) { - RETURN_FALSE; - } - id = PGG(default_link); - CHECK_DEFAULT_LINK(id); - break; - case 2: - if (zend_get_parameters_ex(2, &file_in, &pgsql_link) == FAILURE) { - RETURN_FALSE; - } - convert_to_string_ex(file_in); - break; - default: - WRONG_PARAM_COUNT; - break; + + if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, + "rs", &pgsql_link, &file_in, &name_len) == SUCCESS) { + ; + } + else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, + "s", &file_in, &name_len) == SUCCESS) { + id = PGG(default_link); + CHECK_DEFAULT_LINK(id); + } + else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, + "rs", &pgsql_link, &file_in, &name_len) == SUCCESS) { + php_error(E_NOTICE, "Old API for %s() is used.", get_active_function_name(TSRMLS_C)); + } + else { + WRONG_PARAM_COUNT; } - if (PG(safe_mode) &&(!php_checkuid(Z_STRVAL_PP(file_in), NULL, CHECKUID_CHECK_FILE_AND_DIR))) { + if (PG(safe_mode) &&(!php_checkuid(file_in, NULL, CHECKUID_CHECK_FILE_AND_DIR))) { RETURN_FALSE; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); - oid = lo_import(pgsql, Z_STRVAL_PP(file_in)); + oid = lo_import(pgsql, file_in); if (oid > 0) { RETURN_LONG((int)oid); @@ -1895,42 +1883,39 @@ PHP_FUNCTION(pg_loimport) } /* }}} */ -/* {{{ proto bool pg_loexport(int objoid, string filename [, resource connection]) +/* {{{ proto bool pg_lo_export([resource connection, ] int objoid, string filename) Export large object direct to filesystem */ -PHP_FUNCTION(pg_loexport) +PHP_FUNCTION(pg_lo_export) { - zval **pgsql_link = NULL, **oid_id, **file_out; - int id = -1; + zval *pgsql_link = NULL; + char *file_out; + int id = -1, name_len, oid_id; + int argc = ZEND_NUM_ARGS(); Oid oid; PGconn *pgsql; - switch (ZEND_NUM_ARGS()) { - case 2: - if (zend_get_parameters_ex(2, &oid_id, &file_out) == FAILURE) { - RETURN_FALSE; - } - convert_to_long_ex(oid_id); - convert_to_string_ex(file_out); - id = PGG(default_link); - CHECK_DEFAULT_LINK(id); - break; - case 3: - if (zend_get_parameters_ex(3, &oid_id, &file_out, &pgsql_link) == FAILURE) { - RETURN_FALSE; - } - convert_to_long_ex(oid_id); - convert_to_string_ex(file_out); - break; - default: - WRONG_PARAM_COUNT; - break; + if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, + "rls", &pgsql_link, &oid_id, &file_out, &name_len) == SUCCESS) { + ; + } + else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, + "ls", &oid_id, &file_out, &name_len) == SUCCESS) { + id = PGG(default_link); + CHECK_DEFAULT_LINK(id); + } + else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, + "lsr", &oid_id, &file_out, &name_len, &pgsql_link) == SUCCESS) { + php_error(E_NOTICE, "Old API for %s() is used.", get_active_function_name(TSRMLS_C)); + } + else { + WRONG_PARAM_COUNT; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); - oid = (Oid) Z_LVAL_PP(oid_id); + oid = (Oid) oid_id; - if (lo_export(pgsql, oid, Z_STRVAL_PP(file_out))) { + if (lo_export(pgsql, oid, file_out)) { RETURN_TRUE; } else { RETURN_FALSE; @@ -1938,8 +1923,55 @@ PHP_FUNCTION(pg_loexport) } /* }}} */ -#if HAVE_PQCLIENTENCODING +/* {{{ proto bool pg_lo_lseek(resource large_object, int offset[, int whence]) + Seeks position of large object */ +PHP_FUNCTION(pg_lo_seek) +{ + zval *pgsql_id = NULL; + int offset = 0, whence = SEEK_CUR; + pgLofp *pgsql; + int argc = ZEND_NUM_ARGS(); + if (zend_parse_parameters(argc TSRMLS_CC, "rl|l", &pgsql_id, &offset, &whence) == FAILURE) { + return; + } + if (whence != SEEK_SET && whence != SEEK_CUR && whence != SEEK_END) { + php_error(E_WARNING, "Invalid whence parameter for %s()", + get_active_function_name(TSRMLS_C)); + return; + } + + ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp); + + if (lo_lseek((PGconn *)pgsql->conn, pgsql->lofd, offset, whence )) { + RETURN_TRUE; + } else { + RETURN_FALSE; + } +} +/* }}} */ + +/* {{{ proto int pg_lo_tell(resource large_object) + Returns current position of large object */ +PHP_FUNCTION(pg_lo_tell) +{ + zval *pgsql_id = NULL; + int offset = 0; + pgLofp *pgsql; + int argc = ZEND_NUM_ARGS(); + + if (zend_parse_parameters(argc TSRMLS_CC, "r", &pgsql_id) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp); + + offset = lo_tell((PGconn *)pgsql->conn, pgsql->lofd); + RETURN_LONG(offset); +} +/* }}} */ + +#if HAVE_PQCLIENTENCODING /* {{{ proto int pg_set_client_encoding([resource connection,] string encoding) Set client encoding */ PHP_FUNCTION(pg_set_client_encoding) @@ -2016,13 +2048,16 @@ PHP_FUNCTION(pg_client_encoding) #endif -/* {{{ proto int pg_status(resource connnection) - Get connection status */ -PHP_FUNCTION(pg_status) +#define COPYBUFSIZ 8192 + +/* {{{ proto bool pg_end_copy([resource connection]) + Sync with backend. Completes the Copy command */ +PHP_FUNCTION(pg_end_copy) { zval **pgsql_link = NULL; int id = -1; PGconn *pgsql; + int result = 0; switch(ZEND_NUM_ARGS()) { case 0: @@ -2040,27 +2075,37 @@ PHP_FUNCTION(pg_status) } ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); - Z_LVAL_P(return_value) = PQstatus(pgsql); - Z_TYPE_P(return_value) = IS_LONG; -} + result = PQendcopy(pgsql); + + if (result!=0) { + php_error(E_WARNING, "PostgreSQL query failed: %s", PQerrorMessage(pgsql)); + RETURN_FALSE; + } + RETURN_TRUE; +} /* }}} */ -/* {{{ proto bool pg_reset(resource connection) - Reset connection */ -PHP_FUNCTION(pg_reset) + +/* {{{ proto bool pg_put_line([resource connection,] string query) + Send null-terminated string to backend server*/ +PHP_FUNCTION(pg_put_line) { - zval **pgsql_link = NULL; + zval **query, **pgsql_link = NULL; int id = -1; PGconn *pgsql; + int result = 0; switch(ZEND_NUM_ARGS()) { - case 0: + case 1: + if (zend_get_parameters_ex(1, &query)==FAILURE) { + RETURN_FALSE; + } id = PGG(default_link); CHECK_DEFAULT_LINK(id); break; - case 1: - if (zend_get_parameters_ex(1, &pgsql_link)==FAILURE) { + case 2: + if (zend_get_parameters_ex(2, &pgsql_link, &query)==FAILURE) { RETURN_FALSE; } break; @@ -2068,7 +2113,334 @@ PHP_FUNCTION(pg_reset) WRONG_PARAM_COUNT; break; } + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); + + convert_to_string_ex(query); + result = PQputline(pgsql, Z_STRVAL_PP(query)); + + if (result==EOF) { + php_error(E_WARNING, "PostgreSQL query failed: %s", PQerrorMessage(pgsql)); + RETURN_FALSE; + } + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto int pg_copy_to(int connection, string table_name [, string delimiter [, string null_as]]) + Send null-terminated string to backend server*/ +PHP_FUNCTION(pg_copy_to) +{ + zval *pgsql_link; + char *table_name, *pg_delim = NULL, *pg_null_as = NULL; + int table_name_len, pg_delim_len, pg_null_as_len; + char *query; + char *query_template = "COPY \"\" TO STDOUT DELIMITERS ':' WITH NULL AS ''"; + int id = -1; + PGconn *pgsql; + PGresult *pgsql_result; + ExecStatusType status; + int copydone = 0; + char copybuf[COPYBUFSIZ]; + char *csv = (char *)NULL; + int ret; + int argc = ZEND_NUM_ARGS(); + + if (zend_parse_parameters(argc TSRMLS_CC, "rs|ss", + &pgsql_link, &table_name, &table_name_len, + &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len) == FAILURE) { + return; + } + if (!pg_delim) { + pg_delim = "\t"; + } + if (!pg_null_as) { + pg_null_as = safe_estrdup("\\\\N"); + } + + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + + query = (char *)emalloc(strlen(query_template) + strlen(table_name) + strlen(pg_null_as) + 1); + sprintf(query, "COPY \"%s\" TO STDOUT DELIMITERS '%s' WITH NULL AS '%s'", + table_name, pg_delim, pg_null_as); + + while ((pgsql_result = PQgetResult(pgsql))) { + PQclear(pgsql_result); + } + pgsql_result = PQexec(pgsql, query); + + efree(pg_null_as); + efree(query); + + if (pgsql_result) { + status = PQresultStatus(pgsql_result); + } else { + status = (ExecStatusType) PQstatus(pgsql); + } + + switch (status) { + case PGRES_COPY_OUT: + if (pgsql_result) { + PQclear(pgsql_result); + if (array_init(return_value) == FAILURE) { + RETURN_FALSE; + } + while (!copydone) + { + if ((ret = PQgetline(pgsql, copybuf, COPYBUFSIZ))) { + php_error(E_WARNING, "%s() query failed: %s", + get_active_function_name(TSRMLS_C), PQerrorMessage(pgsql)); + RETURN_FALSE; + } + + if (copybuf[0] == '\\' && + copybuf[1] == '.' && + copybuf[2] == '\0') + { + copydone = 1; + } + else + { + if (csv == (char *)NULL) { + csv = estrdup(copybuf); + } else { + csv = (char *)erealloc(csv, strlen(csv) + sizeof(char)*(COPYBUFSIZ+1)); + strcat(csv, copybuf); + } + + switch (ret) + { + case EOF: + copydone = 1; + case 0: + add_next_index_string(return_value, csv, 1); + efree(csv); + csv = (char *)NULL; + break; + case 1: + break; + } + } + } + if (PQendcopy(pgsql)) { + php_error(E_WARNING, "%s() query failed: %s", + get_active_function_name(TSRMLS_C), PQerrorMessage(pgsql)); + RETURN_FALSE; + } + while ((pgsql_result = PQgetResult(pgsql))) { + PQclear(pgsql_result); + } + } else { + PQclear(pgsql_result); + RETURN_FALSE; + } + break; + default: + PQclear(pgsql_result); + php_error(E_WARNING, "%s() query failed: %s", + get_active_function_name(TSRMLS_C), PQerrorMessage(pgsql)); + RETURN_FALSE; + break; + } +} +/* }}} */ + +/* {{{ proto int pg_copy_from(int connection, string table_name , array rows [, string delimiter [, string null_as]]) + */ +PHP_FUNCTION(pg_copy_from) +{ + zval *pgsql_link = NULL, *pg_rows; + zval **tmp; + char *table_name, *pg_delim = NULL, *pg_null_as = NULL; + int table_name_len, pg_delim_len, pg_null_as_len; + char *query; + char *query_template = "COPY \"\" FROM STDIN DELIMITERS ':' WITH NULL AS ''"; + HashPosition pos; + int id = -1; + PGconn *pgsql; + PGresult *pgsql_result; + ExecStatusType status; + int argc = ZEND_NUM_ARGS(); + + if (zend_parse_parameters(argc TSRMLS_CC, "rs/a|ss", + &pgsql_link, &table_name, &table_name_len, &pg_rows, + &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len) == FAILURE) { + return; + } + if (!pg_delim) { + pg_delim = "\t"; + } + if (!pg_null_as) { + pg_null_as = safe_estrdup("\\\\N"); + } + + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + + query = (char *)emalloc(strlen(query_template) + strlen(table_name) + strlen(pg_null_as) + 1); + sprintf(query, "COPY \"%s\" FROM STDIN DELIMITERS '%s' WITH NULL AS '%s'", + table_name, pg_delim, pg_null_as); + while ((pgsql_result = PQgetResult(pgsql))) { + PQclear(pgsql_result); + } + pgsql_result = PQexec(pgsql, query); + + efree(pg_null_as); + efree(query); + + if (pgsql_result) { + status = PQresultStatus(pgsql_result); + } else { + status = (ExecStatusType) PQstatus(pgsql); + } + + switch (status) { + case PGRES_COPY_IN: + if (pgsql_result) { + PQclear(pgsql_result); + zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(pg_rows), &pos); + while (zend_hash_get_current_data_ex(Z_ARRVAL_P(pg_rows), (void **) &tmp, &pos) == SUCCESS) { + convert_to_string_ex(tmp); + query = (char *)emalloc(Z_STRLEN_PP(tmp) +2); + strcpy(query, Z_STRVAL_PP(tmp)); + if(*(query+Z_STRLEN_PP(tmp)-1) != '\n') strcat(query, "\n"); + if (PQputline(pgsql, query)) { + efree(query); + php_error(E_WARNING, "%s() query failed: %s", + get_active_function_name, PQerrorMessage(pgsql)); + RETURN_FALSE; + } + efree(query); + zend_hash_move_forward_ex(Z_ARRVAL_P(pg_rows), &pos); + } + if (PQputline(pgsql, "\\.\n")) { + php_error(E_WARNING, "%s() query failed: %s", + get_active_function_name(TSRMLS_C), PQerrorMessage(pgsql)); + RETURN_FALSE; + } + if (PQendcopy(pgsql)) { + php_error(E_WARNING, "%s() query failed: %s", + get_active_function_name(TSRMLS_C), PQerrorMessage(pgsql)); + RETURN_FALSE; + } + while ((pgsql_result = PQgetResult(pgsql))) { + PQclear(pgsql_result); + } + } else { + PQclear(pgsql_result); + RETURN_FALSE; + } + RETURN_TRUE; + break; + default: + PQclear(pgsql_result); + php_error(E_WARNING, "%s() query failed: %s", + get_active_function_name(TSRMLS_C), PQerrorMessage(pgsql)); + RETURN_FALSE; + break; + } +} +/* }}} */ + +#if HAVE_PQESCAPE +/* {{{ proto string pg_escape_string(string data) + Escape string for text/char type */ +PHP_FUNCTION(pg_escape_string) +{ + char *from = NULL, *to = NULL; + long type = PGSQL_ESCAPE_STRING; + int len; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", + &from, &len) == FAILURE) { + return; + } + + len = (int)PQescapeString(to, from, strlen(from)); + if (len < 0) { + RETURN_FALSE; + } + RETURN_STRINGL(to, len, 0); +} +/* }}} */ + +/* {{{ proto string pg_escape_bytea(string data) + Escape binary for bytea type */ +PHP_FUNCTION(pg_escape_bytea) +{ + char *from = NULL, *to = NULL; + long type = PGSQL_ESCAPE_STRING; + int len; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", + &from, &len) == FAILURE) { + return; + } + + to = (char *)PQescapeBytea((unsigned char*)from, strlen(from), (size_t *)&len); + if (len < 0) { + RETURN_FALSE; + } + RETURN_STRINGL(to, len, 0); +} +/* }}} */ +#endif + +/* {{{ proto string pg_result_error(resource result) + Get error message associated with result */ +PHP_FUNCTION(pg_result_error) +{ + zval *result; + PGresult *pgsql_result; + pgsql_result_handle *pg_result; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", + &result) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result); + + pgsql_result = pg_result->result; + if (!pgsql_result) { + RETURN_FALSE; + } + RETURN_STRING(PQresultErrorMessage(pgsql_result),1); +} +/* }}} */ + +/* {{{ proto int pg_connection_status(resource connnection) + Get connection status */ +PHP_FUNCTION(pg_connection_status) +{ + zval *pgsql_link = NULL; + int id = -1; + PGconn *pgsql; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", + &pgsql_link) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + + RETURN_LONG(PQstatus(pgsql)); +} + +/* }}} */ + +/* {{{ proto bool pg_connection_reset(resource connection) + Reset connection (reconnect) */ +PHP_FUNCTION(pg_connection_reset) +{ + zval *pgsql_link; + int id = -1; + PGconn *pgsql; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", + &pgsql_link) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + PQreset(pgsql); if (PQstatus(pgsql) == CONNECTION_BAD) { RETURN_FALSE; @@ -2078,9 +2450,6 @@ PHP_FUNCTION(pg_reset) /* }}} */ -/* Following functions are for asyncronous query - * Report bugs to yasuo_ohgaki@yahoo.com - */ #define PHP_PG_ASYNC_IS_BUSY 1 #define PHP_PG_ASYNC_REQUEST_CANCEL 2 @@ -2088,27 +2457,17 @@ PHP_FUNCTION(pg_reset) */ void php_pgsql_do_async(INTERNAL_FUNCTION_PARAMETERS, int entry_type) { - zval **pgsql_link = NULL; + zval *pgsql_link; int id = -1; PGconn *pgsql; PGresult *pgsql_result; - switch(ZEND_NUM_ARGS()) { - case 0: - id = PGG(default_link); - CHECK_DEFAULT_LINK(id); - break; - case 1: - if (zend_get_parameters_ex(1, &pgsql_link)==FAILURE) { - RETURN_FALSE; - } - break; - default: - WRONG_PARAM_COUNT; - break; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", + &pgsql_link) == FAILURE) { + return; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); switch(entry_type) { case PHP_PG_ASYNC_IS_BUSY: @@ -2131,58 +2490,48 @@ void php_pgsql_do_async(INTERNAL_FUNCTION_PARAMETERS, int entry_type) } /* }}} */ -/* {{{ proto bool pg_request_cancel([resource connection]) +/* {{{ proto bool pg_cancel_request(resource connection) Cancel request */ -PHP_FUNCTION(pg_request_cancel) +PHP_FUNCTION(pg_cancel_query) { php_pgsql_do_async(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_ASYNC_REQUEST_CANCEL); } /* }}} */ -/* {{{ proto bool pg_is_busy([resource connection]) +/* {{{ proto query bool pg_connection_busy(resource connection) Get connection is busy or not */ -PHP_FUNCTION(pg_is_busy) +PHP_FUNCTION(pg_connection_busy) { php_pgsql_do_async(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_ASYNC_IS_BUSY); } /* }}} */ -/* {{{ proto bool pg_send_query([resource connection], string qeury) +/* {{{ proto bool pg_send_query(resource connection, string qeury) Send asynchronous query */ PHP_FUNCTION(pg_send_query) { - zval **query, **pgsql_link = NULL; + zval *pgsql_link; + char *query; + int len; int id = -1; PGconn *pgsql; PGresult *res; int leftover = 0; - switch(ZEND_NUM_ARGS()) { - case 1: - if (zend_get_parameters_ex(1, &query)==FAILURE) { - RETURN_FALSE; - } - id = PGG(default_link); - CHECK_DEFAULT_LINK(id); - break; - case 2: - if (zend_get_parameters_ex(2, &pgsql_link, &query)==FAILURE) { - RETURN_FALSE; - } - break; - default: - WRONG_PARAM_COUNT; - break; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", + &pgsql_link, &query, &len) == FAILURE) { + return; } - - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); - convert_to_string_ex(query); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + if (PQsetnonblocking(pgsql, 1)) { - php_error(E_NOTICE,"PostgreSQL: Cannot set connection to nonblocking mode in pg_send_query()"); + php_error(E_NOTICE,"%s() cannot set connection to nonblocking mode", + get_active_function_name(TSRMLS_C)); } if (PQisBusy(pgsql)) { - php_error(E_WARNING,"PostgreSQL: Cannot send multiple query using pg_send_query()"); + php_error(E_WARNING,"%s() annot send multiple query", + get_active_function_name(TSRMLS_C)); RETURN_FALSE; } while ((res = PQgetResult(pgsql))) { @@ -2190,47 +2539,41 @@ PHP_FUNCTION(pg_send_query) leftover = 1; } if (leftover) { - php_error(E_NOTICE,"PostgreSQL: There are results on this connection. Call pg_get_result() until it returns false"); + php_error(E_NOTICE,"%s() - There are results on this connection. Call pg_get_result() until it returns false", + get_active_function_name(TSRMLS_C)); } - if (!PQsendQuery(pgsql, Z_STRVAL_PP(query))) { + if (!PQsendQuery(pgsql, query)) { RETURN_FALSE; } if (PQsetnonblocking(pgsql, 0)) { - php_error(E_NOTICE,"PostgreSQL: Cannot set connection to blocking mode in pg_send_query()"); + php_error(E_NOTICE,"%s() cannot set connection to blocking mode", + get_active_function_name(TSRMLS_C)); } RETURN_TRUE; } /* }}} */ -/* {{{ proto query resouce pg_get_result([resource connection]) +/* {{{ proto query resouce pg_get_result(resource connection) Get asynchronous query result */ PHP_FUNCTION(pg_get_result) { - zval **pgsql_link = NULL; + zval *pgsql_link; int id = -1; PGconn *pgsql; PGresult *pgsql_result; pgsql_result_handle *pg_result; - switch(ZEND_NUM_ARGS()) { - case 0: - id = PGG(default_link); - CHECK_DEFAULT_LINK(id); - break; - case 1: - if (zend_get_parameters_ex(1, &pgsql_link)==FAILURE) { - RETURN_FALSE; - } - break; - default: - WRONG_PARAM_COUNT; - break; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", + &pgsql_link) == FAILURE) { + return; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + if (PQsetnonblocking(pgsql, 1)) { - php_error(E_NOTICE,"PostgreSQL: Cannot set connection to nonblocking mode in pg_get_result()"); + php_error(E_NOTICE,"%s() failed to set connection to nonblocking mode", + get_active_function_name(TSRMLS_C)); } pgsql_result = PQgetResult(pgsql); if (!pgsql_result) { @@ -2238,7 +2581,8 @@ PHP_FUNCTION(pg_get_result) RETURN_FALSE; } if (PQsetnonblocking(pgsql, 0)) { - php_error(E_NOTICE,"PostgreSQL: Cannot set connection to blocking mode in pg_get_result()"); + php_error(E_NOTICE,"%s() failed to set connection to blocking mode", + get_active_function_name(TSRMLS_C)); } pg_result = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle)); pg_result->conn = pgsql; @@ -2248,6 +2592,28 @@ PHP_FUNCTION(pg_get_result) } /* }}} */ +/* {{{ proto query int pg_result_status(resource result) + Get status of query result */ +PHP_FUNCTION(pg_result_status) +{ + zval *result; + ExecStatusType status; + PGresult *pgsql_result; + pgsql_result_handle *pg_result; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", + &result) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result); + + pgsql_result = pg_result->result; + status = PQresultStatus(pgsql_result); + RETURN_LONG((int)status); +} +/* }}} */ + #endif /* diff --git a/ext/pgsql/php_pgsql.h b/ext/pgsql/php_pgsql.h index 23e28667de..01a8a8f7df 100644 --- a/ext/pgsql/php_pgsql.h +++ b/ext/pgsql/php_pgsql.h @@ -44,62 +44,84 @@ extern zend_module_entry pgsql_module_entry; #define PHP_PGSQL_API #endif +#ifdef HAVE_PGSQL_WITH_MULTIBYTE_SUPPORT +const char * pg_encoding_to_char(int encoding); +#endif + PHP_MINIT_FUNCTION(pgsql); PHP_MSHUTDOWN_FUNCTION(pgsql); PHP_RINIT_FUNCTION(pgsql); PHP_RSHUTDOWN_FUNCTION(pgsql); PHP_MINFO_FUNCTION(pgsql); +/* connection functions */ PHP_FUNCTION(pg_connect); PHP_FUNCTION(pg_pconnect); PHP_FUNCTION(pg_close); +PHP_FUNCTION(pg_connection_reset); +PHP_FUNCTION(pg_connection_status); +PHP_FUNCTION(pg_connection_busy); +PHP_FUNCTION(pg_host); PHP_FUNCTION(pg_dbname); -PHP_FUNCTION(pg_errormessage); -PHP_FUNCTION(pg_trace); -PHP_FUNCTION(pg_untrace); -PHP_FUNCTION(pg_options); PHP_FUNCTION(pg_port); PHP_FUNCTION(pg_tty); -PHP_FUNCTION(pg_host); -PHP_FUNCTION(pg_exec); -PHP_FUNCTION(pg_numrows); -PHP_FUNCTION(pg_numfields); -PHP_FUNCTION(pg_cmdtuples); -PHP_FUNCTION(pg_last_notice); -PHP_FUNCTION(pg_fieldname); -PHP_FUNCTION(pg_fieldsize); -PHP_FUNCTION(pg_fieldtype); -PHP_FUNCTION(pg_fieldnum); -PHP_FUNCTION(pg_result); -PHP_FUNCTION(pg_fetch_row); +PHP_FUNCTION(pg_options); +/* query functions */ +PHP_FUNCTION(pg_query); +PHP_FUNCTION(pg_send_query); +PHP_FUNCTION(pg_cancel_query); +/* result functions */ PHP_FUNCTION(pg_fetch_array); PHP_FUNCTION(pg_fetch_object); -PHP_FUNCTION(pg_fieldprtlen); -PHP_FUNCTION(pg_fieldisnull); -PHP_FUNCTION(pg_freeresult); -PHP_FUNCTION(pg_getlastoid); -PHP_FUNCTION(pg_locreate); -PHP_FUNCTION(pg_lounlink); -PHP_FUNCTION(pg_loopen); -PHP_FUNCTION(pg_loclose); -PHP_FUNCTION(pg_loread); -PHP_FUNCTION(pg_lowrite); -PHP_FUNCTION(pg_loreadall); -PHP_FUNCTION(pg_loimport); -PHP_FUNCTION(pg_loexport); -PHP_FUNCTION(pg_lolseek); -PHP_FUNCTION(pg_lotell); +PHP_FUNCTION(pg_fetch_result); +PHP_FUNCTION(pg_fetch_row); +PHP_FUNCTION(pg_affected_rows); +PHP_FUNCTION(pg_get_result); +PHP_FUNCTION(pg_result_status); +PHP_FUNCTION(pg_free_result); +PHP_FUNCTION(pg_last_oid); +PHP_FUNCTION(pg_num_rows); +PHP_FUNCTION(pg_num_fields); +PHP_FUNCTION(pg_field_name); +PHP_FUNCTION(pg_field_num); +PHP_FUNCTION(pg_field_size); +PHP_FUNCTION(pg_field_type); +PHP_FUNCTION(pg_field_prtlen); +PHP_FUNCTION(pg_field_is_null); +/* error message functions */ +PHP_FUNCTION(pg_result_error); +PHP_FUNCTION(pg_last_error); +PHP_FUNCTION(pg_last_notice); +/* copy functions */ PHP_FUNCTION(pg_put_line); PHP_FUNCTION(pg_end_copy); +PHP_FUNCTION(pg_copy_to); +PHP_FUNCTION(pg_copy_from); +/* large object functions */ +PHP_FUNCTION(pg_lo_create); +PHP_FUNCTION(pg_lo_unlink); +PHP_FUNCTION(pg_lo_open); +PHP_FUNCTION(pg_lo_close); +PHP_FUNCTION(pg_lo_read); +PHP_FUNCTION(pg_lo_write); +PHP_FUNCTION(pg_lo_read_all); +PHP_FUNCTION(pg_lo_import); +PHP_FUNCTION(pg_lo_export); +PHP_FUNCTION(pg_lo_seek); +PHP_FUNCTION(pg_lo_tell); + +/* debugging functions */ +PHP_FUNCTION(pg_trace); +PHP_FUNCTION(pg_untrace); + +/* utility functions */ #if HAVE_PQCLIENTENCODING PHP_FUNCTION(pg_client_encoding); PHP_FUNCTION(pg_set_client_encoding); #endif -PHP_FUNCTION(pg_reset); -PHP_FUNCTION(pg_status); -PHP_FUNCTION(pg_send_query); -PHP_FUNCTION(pg_request_cancel); -PHP_FUNCTION(pg_get_result); -PHP_FUNCTION(pg_is_busy); +#if HAVE_PQESCAPE +PHP_FUNCTION(pg_escape_string); +PHP_FUNCTION(pg_escape_bytea); +#endif void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS,int persistent); int php_pgsql_get_default_link(INTERNAL_FUNCTION_PARAMETERS); -- 2.40.0