1999-04-22 10:48:28 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2001-12-11 23:32:16 +08:00
|
|
|
| PHP Version 4 |
|
1999-04-22 10:48:28 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2001-12-11 23:32:16 +08:00
|
|
|
| Copyright (c) 1997-2002 The PHP Group |
|
1999-04-22 10:48:28 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2000-05-18 23:34:45 +08:00
|
|
|
| This source file is subject to version 2.02 of the PHP license, |
|
1999-07-16 21:13:16 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available at through the world-wide-web at |
|
2000-05-18 23:34:45 +08:00
|
|
|
| http://www.php.net/license/2_02.txt. |
|
1999-07-16 21:13:16 +08:00
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
1999-04-22 10:48:28 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Zeev Suraski <zeev@zend.com> |
|
2001-12-17 08:24:59 +08:00
|
|
|
| Jouni Ahto <jouni.ahto@exdec.fi> |
|
|
|
|
| Yasuo Ohgaki <yohgaki@php.net> |
|
|
|
|
| Youichi Iwakiri <yiwakiri@st.rim.or.jp> (pg_copy_*) |
|
1999-04-22 10:48:28 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2000-05-02 13:27:35 +08:00
|
|
|
#define PHP_PGSQL_PRIVATE 1
|
|
|
|
|
2001-05-24 18:07:29 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
1999-04-22 10:48:28 +08:00
|
|
|
#include "php.h"
|
2000-02-06 01:26:35 +08:00
|
|
|
#include "php_ini.h"
|
1999-12-05 03:19:57 +08:00
|
|
|
#include "ext/standard/php_standard.h"
|
2000-02-06 00:12:23 +08:00
|
|
|
#include "php_pgsql.h"
|
1999-04-22 10:48:28 +08:00
|
|
|
#include "php_globals.h"
|
|
|
|
|
|
|
|
#if HAVE_PGSQL
|
|
|
|
|
2001-02-20 00:31:17 +08:00
|
|
|
#ifndef InvalidOid
|
|
|
|
#define InvalidOid ((Oid) 0)
|
|
|
|
#endif
|
1999-04-22 10:48:28 +08:00
|
|
|
|
1999-07-17 01:19:11 +08:00
|
|
|
#define PGSQL_ASSOC 1<<0
|
|
|
|
#define PGSQL_NUM 1<<1
|
|
|
|
#define PGSQL_BOTH (PGSQL_ASSOC|PGSQL_NUM)
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
#define PGSQL_ESCAPE_STRING 1
|
|
|
|
#define PGSQL_ESCAPE_BYTEA 2
|
|
|
|
|
2002-03-29 19:24:54 +08:00
|
|
|
#if HAVE_PQSETNONBLOCKING
|
|
|
|
#define PQ_SETNONBLOCKING(pg_link, flag) PQsetnonblocking(pg_link, flag)
|
|
|
|
#else
|
|
|
|
#define PQ_SETNONBLOCKING(pg_link, flag) 0
|
|
|
|
#endif
|
|
|
|
|
2002-03-26 16:43:57 +08:00
|
|
|
#define CHECK_DEFAULT_LINK(x) if (x == -1) { php_error(E_WARNING, "%s() no PostgreSQL link opened yet", get_active_function_name(TSRMLS_C)); }
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ pgsql_functions[]
|
|
|
|
*/
|
1999-04-22 10:48:28 +08:00
|
|
|
function_entry pgsql_functions[] = {
|
2001-12-17 08:24:59 +08:00
|
|
|
/* connection functions */
|
1999-12-18 19:38:04 +08:00
|
|
|
PHP_FE(pg_connect, NULL)
|
|
|
|
PHP_FE(pg_pconnect, NULL)
|
|
|
|
PHP_FE(pg_close, NULL)
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FE(pg_connection_status, NULL)
|
|
|
|
PHP_FE(pg_connection_busy, NULL)
|
|
|
|
PHP_FE(pg_connection_reset, NULL)
|
|
|
|
PHP_FE(pg_host, NULL)
|
1999-12-18 19:38:04 +08:00
|
|
|
PHP_FE(pg_dbname, NULL)
|
|
|
|
PHP_FE(pg_port, NULL)
|
|
|
|
PHP_FE(pg_tty, NULL)
|
2001-12-17 08:24:59 +08:00
|
|
|
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)
|
1999-12-18 19:38:04 +08:00
|
|
|
PHP_FE(pg_fetch_row, NULL)
|
|
|
|
PHP_FE(pg_fetch_array, NULL)
|
|
|
|
PHP_FE(pg_fetch_object, NULL)
|
2001-12-17 08:24:59 +08:00
|
|
|
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 */
|
2000-09-12 05:28:47 +08:00
|
|
|
PHP_FE(pg_put_line, NULL)
|
|
|
|
PHP_FE(pg_end_copy, NULL)
|
2001-12-17 08:24:59 +08:00
|
|
|
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
|
2000-07-02 16:59:44 +08:00
|
|
|
#if HAVE_PQCLIENTENCODING
|
2000-09-13 12:13:36 +08:00
|
|
|
PHP_FE(pg_client_encoding, NULL)
|
|
|
|
PHP_FE(pg_set_client_encoding, NULL)
|
2001-12-17 08:24:59 +08:00
|
|
|
#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)
|
2002-02-06 15:25:51 +08:00
|
|
|
PHP_FALIAS(pg_loclose, pg_lo_close, NULL)
|
2001-12-17 08:24:59 +08:00
|
|
|
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)
|
2000-09-13 12:13:36 +08:00
|
|
|
PHP_FALIAS(pg_setclientencoding, pg_set_client_encoding, NULL)
|
2000-07-02 16:59:44 +08:00
|
|
|
#endif
|
2002-02-06 15:25:51 +08:00
|
|
|
{NULL, NULL, NULL}
|
1999-04-22 10:48:28 +08:00
|
|
|
};
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ pgsql_module_entry
|
|
|
|
*/
|
1999-12-18 04:55:31 +08:00
|
|
|
zend_module_entry pgsql_module_entry = {
|
2001-10-12 07:33:59 +08:00
|
|
|
STANDARD_MODULE_HEADER,
|
2000-05-24 14:48:04 +08:00
|
|
|
"pgsql",
|
|
|
|
pgsql_functions,
|
|
|
|
PHP_MINIT(pgsql),
|
|
|
|
PHP_MSHUTDOWN(pgsql),
|
|
|
|
PHP_RINIT(pgsql),
|
2001-02-14 02:28:24 +08:00
|
|
|
PHP_RSHUTDOWN(pgsql),
|
2000-05-24 14:48:04 +08:00
|
|
|
PHP_MINFO(pgsql),
|
2001-10-12 07:33:59 +08:00
|
|
|
NO_VERSION_YET,
|
2000-05-24 14:48:04 +08:00
|
|
|
STANDARD_MODULE_PROPERTIES
|
1999-04-22 10:48:28 +08:00
|
|
|
};
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2000-05-23 17:33:51 +08:00
|
|
|
#ifdef COMPILE_DL_PGSQL
|
2000-05-02 08:30:36 +08:00
|
|
|
ZEND_GET_MODULE(pgsql)
|
1999-04-22 10:48:28 +08:00
|
|
|
#endif
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
static int le_link, le_plink, le_result, le_lofp, le_string;
|
1999-11-20 09:04:45 +08:00
|
|
|
|
1999-11-20 08:30:08 +08:00
|
|
|
#ifdef ZTS
|
|
|
|
int pgsql_globals_id;
|
|
|
|
#else
|
2002-02-06 15:25:51 +08:00
|
|
|
php_pgsql_globals pgsql_globals;
|
1999-11-20 08:30:08 +08:00
|
|
|
#endif
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ php_pgsql_set_default_link
|
|
|
|
*/
|
2002-02-06 15:16:10 +08:00
|
|
|
static void php_pgsql_set_default_link(int id TSRMLS_DC)
|
2000-10-11 21:27:21 +08:00
|
|
|
{
|
2001-05-28 06:54:54 +08:00
|
|
|
zend_list_addref(id);
|
2001-04-05 20:29:24 +08:00
|
|
|
|
2001-05-28 06:54:54 +08:00
|
|
|
if (PGG(default_link) != -1) {
|
|
|
|
zend_list_delete(PGG(default_link));
|
|
|
|
}
|
2001-02-14 00:54:48 +08:00
|
|
|
|
2001-05-28 06:54:54 +08:00
|
|
|
PGG(default_link) = id;
|
2000-10-11 21:27:21 +08:00
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
2000-10-11 21:27:21 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ _close_pgsql_link
|
|
|
|
*/
|
2001-07-31 13:44:11 +08:00
|
|
|
static void _close_pgsql_link(zend_rsrc_list_entry *rsrc TSRMLS_DC)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-10-21 02:25:16 +08:00
|
|
|
PGconn *link = (PGconn *)rsrc->ptr;
|
2001-12-10 12:02:08 +08:00
|
|
|
PGresult *res;
|
1999-11-20 08:30:08 +08:00
|
|
|
|
2001-12-10 12:02:08 +08:00
|
|
|
while ((res = PQgetResult(link))) {
|
|
|
|
PQclear(res);
|
|
|
|
}
|
1999-04-22 10:48:28 +08:00
|
|
|
PQfinish(link);
|
1999-11-20 08:30:08 +08:00
|
|
|
PGG(num_links)--;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ _close_pgsql_plink
|
|
|
|
*/
|
2001-07-31 13:44:11 +08:00
|
|
|
static void _close_pgsql_plink(zend_rsrc_list_entry *rsrc TSRMLS_DC)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-10-21 02:25:16 +08:00
|
|
|
PGconn *link = (PGconn *)rsrc->ptr;
|
2001-12-10 12:02:08 +08:00
|
|
|
PGresult *res;
|
1999-11-20 08:30:08 +08:00
|
|
|
|
2001-12-10 12:02:08 +08:00
|
|
|
while ((res = PQgetResult(link))) {
|
|
|
|
PQclear(res);
|
|
|
|
}
|
1999-04-22 10:48:28 +08:00
|
|
|
PQfinish(link);
|
1999-11-20 08:30:08 +08:00
|
|
|
PGG(num_persistent)--;
|
|
|
|
PGG(num_links)--;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ _notice_handler
|
|
|
|
*/
|
2001-09-27 05:44:48 +08:00
|
|
|
static void _notice_handler(void *arg, const char *message)
|
2001-02-14 04:13:14 +08:00
|
|
|
{
|
2001-07-28 19:36:37 +08:00
|
|
|
TSRMLS_FETCH();
|
2001-04-05 05:40:41 +08:00
|
|
|
|
|
|
|
if (! PGG(ignore_notices)) {
|
2001-08-06 00:21:33 +08:00
|
|
|
php_log_err((char *) message TSRMLS_CC);
|
2001-09-27 05:44:48 +08:00
|
|
|
if (PGG(last_notice)) {
|
2001-05-06 09:34:56 +08:00
|
|
|
efree(PGG(last_notice));
|
|
|
|
}
|
2001-09-27 05:44:48 +08:00
|
|
|
PGG(last_notice_len) = strlen(message);
|
|
|
|
PGG(last_notice) = estrndup(message, PGG(last_notice_len));
|
2001-04-05 05:40:41 +08:00
|
|
|
}
|
2001-02-14 04:13:14 +08:00
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
2001-02-14 04:13:14 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ _rollback_transactions
|
|
|
|
*/
|
2001-07-31 12:53:54 +08:00
|
|
|
static int _rollback_transactions(zend_rsrc_list_entry *rsrc TSRMLS_DC)
|
2001-02-14 02:28:24 +08:00
|
|
|
{
|
2001-05-28 07:26:58 +08:00
|
|
|
PGconn *link;
|
2001-12-10 12:02:08 +08:00
|
|
|
PGresult *res;
|
2001-02-14 02:28:24 +08:00
|
|
|
|
2001-09-26 05:58:48 +08:00
|
|
|
if (Z_TYPE_P(rsrc) != le_plink)
|
2001-05-28 11:30:11 +08:00
|
|
|
return 0;
|
2001-05-28 07:26:58 +08:00
|
|
|
|
2001-05-28 11:30:11 +08:00
|
|
|
link = (PGconn *) rsrc->ptr;
|
2002-03-29 19:24:54 +08:00
|
|
|
|
|
|
|
if (PQ_SETNONBLOCKING(link, 0)) {
|
|
|
|
php_error(E_NOTICE,"PostgreSQL cannot set connection to blocking mode");
|
|
|
|
return -1;
|
|
|
|
}
|
2001-05-28 11:30:11 +08:00
|
|
|
|
2001-12-10 12:02:08 +08:00
|
|
|
while ((res = PQgetResult(link))) {
|
|
|
|
PQclear(res);
|
|
|
|
}
|
2001-04-05 05:40:41 +08:00
|
|
|
PGG(ignore_notices) = 1;
|
2002-03-29 19:24:54 +08:00
|
|
|
res = PQexec(link,"BEGIN;ROLLBACK;");
|
|
|
|
PQclear(res);
|
2001-04-05 05:40:41 +08:00
|
|
|
PGG(ignore_notices) = 0;
|
2001-02-14 02:28:24 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
2001-02-14 02:28:24 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ _free_ptr
|
|
|
|
*/
|
2001-07-31 13:44:11 +08:00
|
|
|
static void _free_ptr(zend_rsrc_list_entry *rsrc TSRMLS_DC)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-10-21 02:25:16 +08:00
|
|
|
pgLofp *lofp = (pgLofp *)rsrc->ptr;
|
1999-04-22 10:48:28 +08:00
|
|
|
efree(lofp);
|
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ _free_result
|
|
|
|
*/
|
2001-07-31 13:44:11 +08:00
|
|
|
static void _free_result(zend_rsrc_list_entry *rsrc TSRMLS_DC)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-10-21 02:25:16 +08:00
|
|
|
pgsql_result_handle *pg_result = (pgsql_result_handle *)rsrc->ptr;
|
2001-07-31 13:44:11 +08:00
|
|
|
|
1999-04-22 10:48:28 +08:00
|
|
|
PQclear(pg_result->result);
|
|
|
|
efree(pg_result);
|
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ PHP_INI
|
|
|
|
*/
|
2000-02-06 01:26:35 +08:00
|
|
|
PHP_INI_BEGIN()
|
2002-03-11 15:23:07 +08:00
|
|
|
STD_PHP_INI_BOOLEAN("pgsql.allow_persistent", "1", PHP_INI_SYSTEM, OnUpdateBool, allow_persistent, php_pgsql_globals, pgsql_globals)
|
|
|
|
STD_PHP_INI_ENTRY_EX("pgsql.max_persistent", "-1", PHP_INI_SYSTEM, OnUpdateInt, max_persistent, php_pgsql_globals, pgsql_globals, display_link_numbers)
|
|
|
|
STD_PHP_INI_ENTRY_EX("pgsql.max_links", "-1", PHP_INI_SYSTEM, OnUpdateInt, max_links, php_pgsql_globals, pgsql_globals, display_link_numbers)
|
|
|
|
STD_PHP_INI_BOOLEAN("pgsql.auto_reset_persistent", "0", PHP_INI_SYSTEM, OnUpdateBool, auto_reset_persistent, php_pgsql_globals, pgsql_globals)
|
2000-02-06 01:26:35 +08:00
|
|
|
PHP_INI_END()
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
2000-02-06 01:26:35 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ php_pgsql_init_globals
|
|
|
|
*/
|
2001-07-28 19:36:37 +08:00
|
|
|
static void php_pgsql_init_globals(php_pgsql_globals *pgsql_globals_p TSRMLS_DC)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-11-20 09:04:45 +08:00
|
|
|
PGG(num_persistent) = 0;
|
2001-04-05 05:40:41 +08:00
|
|
|
PGG(ignore_notices) = 0;
|
2002-02-12 18:01:08 +08:00
|
|
|
PGG(auto_reset_persistent) = 0;
|
1999-11-20 09:04:45 +08:00
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-11-20 09:04:45 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ PHP_MINIT_FUNCTION
|
|
|
|
*/
|
1999-11-20 09:04:45 +08:00
|
|
|
PHP_MINIT_FUNCTION(pgsql)
|
|
|
|
{
|
|
|
|
#ifdef ZTS
|
2001-07-27 18:16:41 +08:00
|
|
|
ts_allocate_id(&pgsql_globals_id, sizeof(php_pgsql_globals), (ts_allocate_ctor) php_pgsql_init_globals, NULL);
|
1999-11-20 09:04:45 +08:00
|
|
|
#else
|
2001-07-28 19:36:37 +08:00
|
|
|
php_pgsql_init_globals(&pgsql_globals TSRMLS_CC);
|
1999-11-20 09:04:45 +08:00
|
|
|
#endif
|
2000-02-06 01:26:35 +08:00
|
|
|
|
|
|
|
REGISTER_INI_ENTRIES();
|
1999-11-20 09:04:45 +08:00
|
|
|
|
2000-10-26 01:44:02 +08:00
|
|
|
le_link = zend_register_list_destructors_ex(_close_pgsql_link, NULL, "pgsql link", module_number);
|
|
|
|
le_plink = zend_register_list_destructors_ex(NULL, _close_pgsql_plink, "pgsql link persistent", module_number);
|
|
|
|
le_result = zend_register_list_destructors_ex(_free_result, NULL, "pgsql result", module_number);
|
|
|
|
le_lofp = zend_register_list_destructors_ex(_free_ptr, NULL, "pgsql large object", module_number);
|
|
|
|
le_string = zend_register_list_destructors_ex(_free_ptr, NULL, "pgsql string", module_number);
|
2002-03-11 15:19:57 +08:00
|
|
|
/* For pg_fetch_array() */
|
1999-07-17 01:27:42 +08:00
|
|
|
REGISTER_LONG_CONSTANT("PGSQL_ASSOC", PGSQL_ASSOC, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("PGSQL_NUM", PGSQL_NUM, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("PGSQL_BOTH", PGSQL_BOTH, CONST_CS | CONST_PERSISTENT);
|
2002-03-11 15:19:57 +08:00
|
|
|
/* For pg_connection_status() */
|
2001-12-10 12:02:08 +08:00
|
|
|
REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_BAD", CONNECTION_BAD, CONST_CS | CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_OK", CONNECTION_OK, CONST_CS | CONST_PERSISTENT);
|
2002-03-11 15:19:57 +08:00
|
|
|
/* For lo_seek() */
|
2001-12-17 08:24:59 +08:00
|
|
|
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);
|
2002-03-11 15:19:57 +08:00
|
|
|
/* For pg_result_status() */
|
2001-12-17 08:24:59 +08:00
|
|
|
REGISTER_LONG_CONSTANT("PGSQL_EMPTY_QUERY", PGRES_EMPTY_QUERY, CONST_CS | CONST_PERSISTENT);
|
2002-01-21 08:36:22 +08:00
|
|
|
REGISTER_LONG_CONSTANT("PGSQL_COMMAND_OK", PGRES_COMMAND_OK, CONST_CS | CONST_PERSISTENT);
|
2001-12-17 08:24:59 +08:00
|
|
|
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);
|
|
|
|
|
1999-04-22 10:48:28 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ PHP_MSHUTDOWN_FUNCTION
|
|
|
|
*/
|
2000-02-06 01:26:35 +08:00
|
|
|
PHP_MSHUTDOWN_FUNCTION(pgsql)
|
|
|
|
{
|
|
|
|
UNREGISTER_INI_ENTRIES();
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
2000-02-06 01:26:35 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ PHP_RINIT_FUNCTION
|
|
|
|
*/
|
1999-12-18 19:38:04 +08:00
|
|
|
PHP_RINIT_FUNCTION(pgsql)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-11-20 08:30:08 +08:00
|
|
|
PGG(default_link)=-1;
|
|
|
|
PGG(num_links) = PGG(num_persistent);
|
2001-09-27 05:44:48 +08:00
|
|
|
PGG(last_notice) = NULL;
|
1999-04-22 10:48:28 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ PHP_RSHUTDOWN_FUNCTION
|
|
|
|
*/
|
2001-02-14 02:28:24 +08:00
|
|
|
PHP_RSHUTDOWN_FUNCTION(pgsql)
|
|
|
|
{
|
2001-09-27 05:44:48 +08:00
|
|
|
if (PGG(last_notice)) {
|
|
|
|
efree(PGG(last_notice));
|
|
|
|
}
|
2001-07-31 12:53:54 +08:00
|
|
|
zend_hash_apply(&EG(persistent_list), (apply_func_t) _rollback_transactions TSRMLS_CC);
|
2001-02-14 02:28:24 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
2001-02-14 02:28:24 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ PHP_MINFO_FUNCTION
|
|
|
|
*/
|
2000-05-24 14:48:04 +08:00
|
|
|
PHP_MINFO_FUNCTION(pgsql)
|
|
|
|
{
|
|
|
|
char buf[32];
|
|
|
|
|
|
|
|
php_info_print_table_start();
|
|
|
|
php_info_print_table_header(2, "PostgreSQL Support", "enabled");
|
2002-03-31 09:45:22 +08:00
|
|
|
#if HAVE_PG_CONFIG_H
|
2002-03-31 09:18:32 +08:00
|
|
|
php_info_print_table_row(2, "PostgreSQL(libpq) Version", PG_VERSION);
|
2002-03-31 09:45:22 +08:00
|
|
|
#ifdef MULTIBYTE
|
|
|
|
php_info_print_table_row(2, "Multibyte charater support", "enabled");
|
|
|
|
#else
|
|
|
|
php_info_print_table_row(2, "Multibyte charater support", "disabled");
|
|
|
|
#endif
|
|
|
|
#ifdef USE_SSL
|
|
|
|
php_info_print_table_row(2, "SSL support", "enabled");
|
|
|
|
#else
|
|
|
|
php_info_print_table_row(2, "SSL support", "disabled");
|
|
|
|
#endif
|
|
|
|
#endif /* HAVE_PG_CONFIG_H */
|
2000-05-24 14:48:04 +08:00
|
|
|
sprintf(buf, "%ld", PGG(num_persistent));
|
|
|
|
php_info_print_table_row(2, "Active Persistent Links", buf);
|
|
|
|
sprintf(buf, "%ld", PGG(num_links));
|
|
|
|
php_info_print_table_row(2, "Active Links", buf);
|
|
|
|
php_info_print_table_end();
|
|
|
|
|
|
|
|
DISPLAY_INI_ENTRIES();
|
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
2001-04-05 05:40:41 +08:00
|
|
|
|
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ php_pgsql_do_connect
|
|
|
|
*/
|
2002-02-06 15:16:10 +08:00
|
|
|
static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
|
|
|
char *host=NULL,*port=NULL,*options=NULL,*tty=NULL,*dbname=NULL,*connstring=NULL;
|
|
|
|
char *hashed_details;
|
|
|
|
int hashed_details_length;
|
|
|
|
PGconn *pgsql;
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
switch(ZEND_NUM_ARGS()) {
|
1999-04-22 10:48:28 +08:00
|
|
|
case 1: { /* new style, using connection string */
|
2002-03-11 15:23:07 +08:00
|
|
|
zval **yyconnstring;
|
|
|
|
if (zend_get_parameters_ex(1, &yyconnstring) == FAILURE) {
|
|
|
|
RETURN_FALSE;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
2002-03-11 15:23:07 +08:00
|
|
|
convert_to_string_ex(yyconnstring);
|
|
|
|
connstring = Z_STRVAL_PP(yyconnstring);
|
|
|
|
hashed_details_length = Z_STRLEN_PP(yyconnstring)+5+1;
|
|
|
|
hashed_details = (char *) emalloc(hashed_details_length+1);
|
|
|
|
sprintf(hashed_details, "pgsql_%s", connstring); /* SAFE */
|
|
|
|
}
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
case 3: { /* host, port, dbname */
|
2002-03-11 15:23:07 +08:00
|
|
|
zval **yyhost, **yyport, **yydbname;
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2002-03-11 15:23:07 +08:00
|
|
|
if (zend_get_parameters_ex(3, &yyhost, &yyport, &yydbname) == FAILURE) {
|
|
|
|
RETURN_FALSE;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
2002-03-11 15:23:07 +08:00
|
|
|
convert_to_string_ex(yyhost);
|
|
|
|
convert_to_string_ex(yyport);
|
|
|
|
convert_to_string_ex(yydbname);
|
|
|
|
host = Z_STRVAL_PP(yyhost);
|
|
|
|
port = Z_STRVAL_PP(yyport);
|
|
|
|
dbname = Z_STRVAL_PP(yydbname);
|
|
|
|
options=tty=NULL;
|
|
|
|
hashed_details_length = Z_STRLEN_PP(yyhost) + Z_STRLEN_PP(yyport) + Z_STRLEN_PP(yydbname) + 5 + 5;
|
|
|
|
hashed_details = (char *) emalloc(hashed_details_length+1);
|
|
|
|
sprintf(hashed_details, "pgsql_%s_%s___%s", host, port, dbname); /* SAFE */
|
|
|
|
}
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
case 4: { /* host, port, options, dbname */
|
2002-03-11 15:23:07 +08:00
|
|
|
zval **yyhost, **yyport, **yyoptions, **yydbname;
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2002-03-11 15:23:07 +08:00
|
|
|
if (zend_get_parameters_ex(4, &yyhost, &yyport, &yyoptions, &yydbname) == FAILURE) {
|
|
|
|
RETURN_FALSE;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
2002-03-11 15:23:07 +08:00
|
|
|
convert_to_string_ex(yyhost);
|
|
|
|
convert_to_string_ex(yyport);
|
|
|
|
convert_to_string_ex(yyoptions);
|
|
|
|
convert_to_string_ex(yydbname);
|
|
|
|
host = Z_STRVAL_PP(yyhost);
|
|
|
|
port = Z_STRVAL_PP(yyport);
|
|
|
|
options = Z_STRVAL_PP(yyoptions);
|
|
|
|
dbname = Z_STRVAL_PP(yydbname);
|
|
|
|
tty=NULL;
|
|
|
|
hashed_details_length = Z_STRLEN_PP(yyhost) + Z_STRLEN_PP(yyport) + Z_STRLEN_PP(yyoptions) + Z_STRLEN_PP(yydbname) + 5 + 5;
|
|
|
|
hashed_details = (char *) emalloc(hashed_details_length+1);
|
|
|
|
sprintf(hashed_details, "pgsql_%s_%s_%s__%s", host, port, options, dbname); /* SAFE */
|
|
|
|
}
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
case 5: { /* host, port, options, tty, dbname */
|
2002-03-11 15:23:07 +08:00
|
|
|
zval **yyhost, **yyport, **yyoptions, **yytty, **yydbname;
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2002-03-11 15:23:07 +08:00
|
|
|
if (zend_get_parameters_ex(5, &yyhost, &yyport, &yyoptions, &yytty, &yydbname) == FAILURE) {
|
|
|
|
RETURN_FALSE;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
2002-03-11 15:23:07 +08:00
|
|
|
convert_to_string_ex(yyhost);
|
|
|
|
convert_to_string_ex(yyport);
|
|
|
|
convert_to_string_ex(yyoptions);
|
|
|
|
convert_to_string_ex(yytty);
|
|
|
|
convert_to_string_ex(yydbname);
|
|
|
|
host = Z_STRVAL_PP(yyhost);
|
|
|
|
port = Z_STRVAL_PP(yyport);
|
|
|
|
options = Z_STRVAL_PP(yyoptions);
|
|
|
|
tty = Z_STRVAL_PP(yytty);
|
|
|
|
dbname = Z_STRVAL_PP(yydbname);
|
|
|
|
hashed_details_length = Z_STRLEN_PP(yyhost) + Z_STRLEN_PP(yyport) + Z_STRLEN_PP(yyoptions) + Z_STRLEN_PP(yytty) + Z_STRLEN_PP(yydbname) + 5 + 5;
|
|
|
|
hashed_details = (char *) emalloc(hashed_details_length+1);
|
|
|
|
sprintf(hashed_details, "pgsql_%s_%s_%s_%s_%s", host, port, options, tty, dbname); /* SAFE */
|
|
|
|
}
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-07-14 01:13:00 +08:00
|
|
|
if (persistent && PGG(allow_persistent)) {
|
1999-04-22 10:48:28 +08:00
|
|
|
list_entry *le;
|
|
|
|
|
|
|
|
/* try to find if we already have this link in our persistent list */
|
2000-02-06 00:12:23 +08:00
|
|
|
if (zend_hash_find(&EG(persistent_list), hashed_details, hashed_details_length+1, (void **) &le)==FAILURE) { /* we don't */
|
1999-04-22 10:48:28 +08:00
|
|
|
list_entry new_le;
|
|
|
|
|
1999-11-20 08:30:08 +08:00
|
|
|
if (PGG(max_links)!=-1 && PGG(num_links)>=PGG(max_links)) {
|
2002-03-26 16:43:57 +08:00
|
|
|
php_error(E_WARNING,"%s() cannot create new link. Too many open links (%d)",
|
|
|
|
get_active_function_name(TSRMLS_C), PGG(num_links));
|
1999-04-22 10:48:28 +08:00
|
|
|
efree(hashed_details);
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
1999-11-20 08:30:08 +08:00
|
|
|
if (PGG(max_persistent)!=-1 && PGG(num_persistent)>=PGG(max_persistent)) {
|
2002-03-26 16:43:57 +08:00
|
|
|
php_error(E_WARNING,"%s() cannot create new link. Too many open persistent links (%d)",
|
|
|
|
get_active_function_name(TSRMLS_C), PGG(num_persistent));
|
1999-04-22 10:48:28 +08:00
|
|
|
efree(hashed_details);
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create the link */
|
|
|
|
if (connstring) {
|
|
|
|
pgsql=PQconnectdb(connstring);
|
|
|
|
} else {
|
|
|
|
pgsql=PQsetdb(host,port,options,tty,dbname);
|
|
|
|
}
|
|
|
|
if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) {
|
2002-03-26 16:43:57 +08:00
|
|
|
php_error(E_WARNING,"%s() unable to connect to PostgreSQL server: %s",
|
|
|
|
get_active_function_name(TSRMLS_C), PQerrorMessage(pgsql));
|
2001-04-30 01:33:11 +08:00
|
|
|
if (pgsql) {
|
|
|
|
PQfinish(pgsql);
|
|
|
|
}
|
1999-04-22 10:48:28 +08:00
|
|
|
efree(hashed_details);
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2001-04-05 05:40:41 +08:00
|
|
|
PQsetNoticeProcessor(pgsql, _notice_handler, NULL);
|
|
|
|
|
1999-04-22 10:48:28 +08:00
|
|
|
/* hash it up */
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_TYPE(new_le) = le_plink;
|
1999-04-22 10:48:28 +08:00
|
|
|
new_le.ptr = pgsql;
|
2000-02-06 00:12:23 +08:00
|
|
|
if (zend_hash_update(&EG(persistent_list), hashed_details, hashed_details_length+1, (void *) &new_le, sizeof(list_entry), NULL)==FAILURE) {
|
1999-04-22 10:48:28 +08:00
|
|
|
efree(hashed_details);
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
1999-11-20 08:30:08 +08:00
|
|
|
PGG(num_links)++;
|
|
|
|
PGG(num_persistent)++;
|
1999-04-22 10:48:28 +08:00
|
|
|
} else { /* we do */
|
2001-09-26 05:58:48 +08:00
|
|
|
if (Z_TYPE_P(le) != le_plink) {
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
/* ensure that the link did not die */
|
2002-01-25 08:43:50 +08:00
|
|
|
if (PGG(auto_reset_persistent)) {
|
2002-01-24 17:27:43 +08:00
|
|
|
/* need to send & get something from backend to
|
|
|
|
make sure we catch CONNECTION_BAD everytime */
|
|
|
|
PGresult *pg_result;
|
|
|
|
pg_result = PQexec(le->ptr, "select 1");
|
|
|
|
PQclear(pg_result);
|
|
|
|
}
|
1999-04-22 10:48:28 +08:00
|
|
|
if (PQstatus(le->ptr)==CONNECTION_BAD) { /* the link died */
|
2002-01-24 17:27:43 +08:00
|
|
|
if (le->ptr == NULL) {
|
|
|
|
if (connstring) {
|
|
|
|
le->ptr=PQconnectdb(connstring);
|
|
|
|
} else {
|
|
|
|
le->ptr=PQsetdb(host,port,options,tty,dbname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PQreset(le->ptr);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
if (le->ptr==NULL || PQstatus(le->ptr)==CONNECTION_BAD) {
|
2002-03-26 16:43:57 +08:00
|
|
|
php_error(E_WARNING,"%s() PostgreSQL link lost, unable to reconnect",
|
|
|
|
get_active_function_name(TSRMLS_C));
|
2000-02-06 00:12:23 +08:00
|
|
|
zend_hash_del(&EG(persistent_list),hashed_details,hashed_details_length+1);
|
1999-04-22 10:48:28 +08:00
|
|
|
efree(hashed_details);
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pgsql = (PGconn *) le->ptr;
|
2001-04-05 05:40:41 +08:00
|
|
|
|
|
|
|
PQsetNoticeProcessor(pgsql, _notice_handler, NULL);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
2000-05-25 15:51:40 +08:00
|
|
|
ZEND_REGISTER_RESOURCE(return_value, pgsql, le_plink);
|
1999-04-22 10:48:28 +08:00
|
|
|
} else {
|
|
|
|
list_entry *index_ptr,new_index_ptr;
|
|
|
|
|
|
|
|
/* first we check the hash for the hashed_details key. if it exists,
|
|
|
|
* it should point us to the right offset where the actual pgsql link sits.
|
|
|
|
* if it doesn't, open a new pgsql link, add it to the resource list,
|
|
|
|
* and add a pointer to it with hashed_details as the key.
|
|
|
|
*/
|
2000-02-06 00:12:23 +08:00
|
|
|
if (zend_hash_find(&EG(regular_list),hashed_details,hashed_details_length+1,(void **) &index_ptr)==SUCCESS) {
|
1999-04-22 10:48:28 +08:00
|
|
|
int type,link;
|
|
|
|
void *ptr;
|
|
|
|
|
2001-09-26 05:58:48 +08:00
|
|
|
if (Z_TYPE_P(index_ptr) != le_index_ptr) {
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
1999-12-05 03:19:57 +08:00
|
|
|
link = (int) (long) index_ptr->ptr;
|
1999-12-18 03:51:39 +08:00
|
|
|
ptr = zend_list_find(link,&type); /* check if the link is still there */
|
1999-11-20 09:04:45 +08:00
|
|
|
if (ptr && (type==le_link || type==le_plink)) {
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_LVAL_P(return_value) = link;
|
2001-02-15 10:15:36 +08:00
|
|
|
zend_list_addref(link);
|
2002-02-06 15:16:10 +08:00
|
|
|
php_pgsql_set_default_link(link TSRMLS_CC);
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_TYPE_P(return_value) = IS_RESOURCE;
|
1999-04-22 10:48:28 +08:00
|
|
|
efree(hashed_details);
|
|
|
|
return;
|
|
|
|
} else {
|
2000-02-06 00:12:23 +08:00
|
|
|
zend_hash_del(&EG(regular_list),hashed_details,hashed_details_length+1);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
}
|
1999-11-20 08:30:08 +08:00
|
|
|
if (PGG(max_links)!=-1 && PGG(num_links)>=PGG(max_links)) {
|
2002-03-26 16:43:57 +08:00
|
|
|
php_error(E_WARNING,"%s() cannot create new link. Too many open links (%d)",
|
|
|
|
get_active_function_name(TSRMLS_C), PGG(num_links));
|
1999-04-22 10:48:28 +08:00
|
|
|
efree(hashed_details);
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
if (connstring) {
|
2000-05-25 15:51:40 +08:00
|
|
|
pgsql = PQconnectdb(connstring);
|
1999-04-22 10:48:28 +08:00
|
|
|
} else {
|
2000-05-25 15:51:40 +08:00
|
|
|
pgsql = PQsetdb(host,port,options,tty,dbname);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) {
|
2002-03-26 16:43:57 +08:00
|
|
|
php_error(E_WARNING,"%s() unable to connect to PostgreSQL server: %s",
|
|
|
|
get_active_function_name(TSRMLS_C), PQerrorMessage(pgsql));
|
1999-04-22 10:48:28 +08:00
|
|
|
efree(hashed_details);
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2001-04-05 05:40:41 +08:00
|
|
|
PQsetNoticeProcessor(pgsql, _notice_handler, NULL);
|
|
|
|
|
1999-04-22 10:48:28 +08:00
|
|
|
/* add it to the list */
|
2000-05-25 15:51:40 +08:00
|
|
|
ZEND_REGISTER_RESOURCE(return_value, pgsql, le_link);
|
1999-04-22 10:48:28 +08:00
|
|
|
|
|
|
|
/* add it to the hash */
|
2001-09-26 05:58:48 +08:00
|
|
|
new_index_ptr.ptr = (void *) Z_LVAL_P(return_value);
|
|
|
|
Z_TYPE(new_index_ptr) = le_index_ptr;
|
2000-02-06 00:12:23 +08:00
|
|
|
if (zend_hash_update(&EG(regular_list),hashed_details,hashed_details_length+1,(void *) &new_index_ptr, sizeof(list_entry), NULL)==FAILURE) {
|
1999-04-22 10:48:28 +08:00
|
|
|
efree(hashed_details);
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
1999-11-20 08:30:08 +08:00
|
|
|
PGG(num_links)++;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
efree(hashed_details);
|
2002-02-06 15:16:10 +08:00
|
|
|
php_pgsql_set_default_link(Z_LVAL_P(return_value) TSRMLS_CC);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2002-02-06 15:16:10 +08:00
|
|
|
#if 0
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ php_pgsql_get_default_link
|
|
|
|
*/
|
2002-02-06 15:16:10 +08:00
|
|
|
static int php_pgsql_get_default_link(INTERNAL_FUNCTION_PARAMETERS)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-11-20 08:30:08 +08:00
|
|
|
if (PGG(default_link)==-1) { /* no link opened yet, implicitly open one */
|
1999-07-23 07:54:54 +08:00
|
|
|
ht = 0;
|
1999-12-18 19:38:04 +08:00
|
|
|
php_pgsql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,0);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
1999-11-20 08:30:08 +08:00
|
|
|
return PGG(default_link);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
2002-02-06 15:16:10 +08:00
|
|
|
#endif
|
1999-07-23 07:54:54 +08:00
|
|
|
|
2001-12-10 14:14:03 +08:00
|
|
|
/* {{{ proto resource pg_connect([string connection_string] | [string host, string port [, string options [, string tty,]] string database)
|
1999-04-22 10:48:28 +08:00
|
|
|
Open a PostgreSQL connection */
|
1999-12-20 06:44:55 +08:00
|
|
|
PHP_FUNCTION(pg_connect)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-12-18 19:38:04 +08:00
|
|
|
php_pgsql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,0);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-10 14:14:03 +08:00
|
|
|
/* {{{ proto resource pg_pconnect([string connection_string] | [string host, string port [, string options [, string tty,]] string database)
|
1999-04-22 10:48:28 +08:00
|
|
|
Open a persistent PostgreSQL connection */
|
1999-12-20 06:44:55 +08:00
|
|
|
PHP_FUNCTION(pg_pconnect)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-12-18 19:38:04 +08:00
|
|
|
php_pgsql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,1);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-10 14:14:03 +08:00
|
|
|
/* {{{ proto bool pg_close([resource connection])
|
1999-04-22 10:48:28 +08:00
|
|
|
Close a PostgreSQL connection */
|
1999-12-20 06:44:55 +08:00
|
|
|
PHP_FUNCTION(pg_close)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-07-29 14:07:26 +08:00
|
|
|
zval **pgsql_link = NULL;
|
2001-04-05 20:29:24 +08:00
|
|
|
int id;
|
1999-04-22 10:48:28 +08:00
|
|
|
PGconn *pgsql;
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
switch (ZEND_NUM_ARGS()) {
|
1999-04-22 10:48:28 +08:00
|
|
|
case 0:
|
1999-11-20 08:30:08 +08:00
|
|
|
id = PGG(default_link);
|
2000-07-29 14:07:26 +08:00
|
|
|
CHECK_DEFAULT_LINK(id);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
2000-05-25 15:51:40 +08:00
|
|
|
if (zend_get_parameters_ex(1, &pgsql_link)==FAILURE) {
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2000-11-03 10:46:49 +08:00
|
|
|
id = -1;
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);
|
2000-11-03 10:46:49 +08:00
|
|
|
|
|
|
|
if (id==-1) { /* explicit resource number */
|
|
|
|
zend_list_delete(Z_RESVAL_PP(pgsql_link));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id!=-1
|
|
|
|
|| (pgsql_link && Z_RESVAL_PP(pgsql_link)==PGG(default_link))) {
|
2001-03-26 16:00:43 +08:00
|
|
|
zend_list_delete(PGG(default_link));
|
2000-11-03 10:46:49 +08:00
|
|
|
PGG(default_link) = -1;
|
2000-11-03 08:51:53 +08:00
|
|
|
}
|
|
|
|
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
|
1999-12-18 19:38:04 +08:00
|
|
|
#define PHP_PG_DBNAME 1
|
|
|
|
#define PHP_PG_ERROR_MESSAGE 2
|
|
|
|
#define PHP_PG_OPTIONS 3
|
|
|
|
#define PHP_PG_PORT 4
|
|
|
|
#define PHP_PG_TTY 5
|
|
|
|
#define PHP_PG_HOST 6
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ php_pgsql_get_link_info
|
|
|
|
*/
|
2002-02-06 15:16:10 +08:00
|
|
|
static void php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-07-29 14:07:26 +08:00
|
|
|
zval **pgsql_link = NULL;
|
2000-05-25 15:51:40 +08:00
|
|
|
int id = -1;
|
1999-04-22 10:48:28 +08:00
|
|
|
PGconn *pgsql;
|
1999-11-20 08:30:08 +08:00
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
switch(ZEND_NUM_ARGS()) {
|
1999-04-22 10:48:28 +08:00
|
|
|
case 0:
|
1999-11-20 08:30:08 +08:00
|
|
|
id = PGG(default_link);
|
2000-07-29 14:07:26 +08:00
|
|
|
CHECK_DEFAULT_LINK(id);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
2000-05-25 15:51:40 +08:00
|
|
|
if (zend_get_parameters_ex(1, &pgsql_link)==FAILURE) {
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);
|
|
|
|
|
1999-04-22 10:48:28 +08:00
|
|
|
switch(entry_type) {
|
1999-12-18 19:38:04 +08:00
|
|
|
case PHP_PG_DBNAME:
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_STRVAL_P(return_value) = PQdb(pgsql);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
1999-12-18 19:38:04 +08:00
|
|
|
case PHP_PG_ERROR_MESSAGE:
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_STRVAL_P(return_value) = PQerrorMessage(pgsql);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
1999-12-18 19:38:04 +08:00
|
|
|
case PHP_PG_OPTIONS:
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_STRVAL_P(return_value) = PQoptions(pgsql);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
1999-12-18 19:38:04 +08:00
|
|
|
case PHP_PG_PORT:
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_STRVAL_P(return_value) = PQport(pgsql);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
1999-12-18 19:38:04 +08:00
|
|
|
case PHP_PG_TTY:
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_STRVAL_P(return_value) = PQtty(pgsql);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
1999-12-18 19:38:04 +08:00
|
|
|
case PHP_PG_HOST:
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_STRVAL_P(return_value) = PQhost(pgsql);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
|
|
|
|
Z_STRVAL_P(return_value) = (char *) estrdup(Z_STRVAL_P(return_value));
|
|
|
|
Z_TYPE_P(return_value) = IS_STRING;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-12-10 14:14:03 +08:00
|
|
|
/* {{{ proto string pg_dbname([resource connection])
|
1999-04-22 10:48:28 +08:00
|
|
|
Get the database name */
|
1999-12-20 06:44:55 +08:00
|
|
|
PHP_FUNCTION(pg_dbname)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-12-18 19:38:04 +08:00
|
|
|
php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_DBNAME);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto string pg_last_error([resource connection])
|
1999-04-22 10:48:28 +08:00
|
|
|
Get the error message string */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_last_error)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-12-18 19:38:04 +08:00
|
|
|
php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_ERROR_MESSAGE);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-10 14:14:03 +08:00
|
|
|
/* {{{ proto string pg_options([resource connection])
|
1999-04-22 10:48:28 +08:00
|
|
|
Get the options associated with the connection */
|
1999-12-20 06:44:55 +08:00
|
|
|
PHP_FUNCTION(pg_options)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-12-18 19:38:04 +08:00
|
|
|
php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_OPTIONS);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-10 14:14:03 +08:00
|
|
|
/* {{{ proto int pg_port([resource connection])
|
1999-04-22 10:48:28 +08:00
|
|
|
Return the port number associated with the connection */
|
1999-12-20 06:44:55 +08:00
|
|
|
PHP_FUNCTION(pg_port)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-12-18 19:38:04 +08:00
|
|
|
php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_PORT);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-10 14:14:03 +08:00
|
|
|
/* {{{ proto string pg_tty([resource connection])
|
1999-04-22 10:48:28 +08:00
|
|
|
Return the tty name associated with the connection */
|
1999-12-20 06:44:55 +08:00
|
|
|
PHP_FUNCTION(pg_tty)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-12-18 19:38:04 +08:00
|
|
|
php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_TTY);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-10 14:14:03 +08:00
|
|
|
/* {{{ proto string pg_host([resource connection])
|
1999-04-22 10:48:28 +08:00
|
|
|
Returns the host name associated with the connection */
|
1999-12-20 06:44:55 +08:00
|
|
|
PHP_FUNCTION(pg_host)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-12-18 19:38:04 +08:00
|
|
|
php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_HOST);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto resource pg_query([resource connection,] string query)
|
1999-04-22 10:48:28 +08:00
|
|
|
Execute a query */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_query)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-07-29 14:07:26 +08:00
|
|
|
zval **query, **pgsql_link = NULL;
|
2000-05-25 15:51:40 +08:00
|
|
|
int id = -1;
|
2001-12-10 12:02:08 +08:00
|
|
|
int leftover = 0;
|
1999-04-22 10:48:28 +08:00
|
|
|
PGconn *pgsql;
|
|
|
|
PGresult *pgsql_result;
|
2000-05-25 15:51:40 +08:00
|
|
|
ExecStatusType status;
|
1999-04-22 10:48:28 +08:00
|
|
|
pgsql_result_handle *pg_result;
|
2001-12-10 12:02:08 +08:00
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
switch(ZEND_NUM_ARGS()) {
|
1999-04-22 10:48:28 +08:00
|
|
|
case 1:
|
2000-05-25 15:51:40 +08:00
|
|
|
if (zend_get_parameters_ex(1, &query)==FAILURE) {
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
1999-11-20 08:30:08 +08:00
|
|
|
id = PGG(default_link);
|
2000-07-29 14:07:26 +08:00
|
|
|
CHECK_DEFAULT_LINK(id);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
2000-05-25 15:51:40 +08:00
|
|
|
if (zend_get_parameters_ex(2, &pgsql_link, &query)==FAILURE) {
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);
|
|
|
|
|
|
|
|
convert_to_string_ex(query);
|
2002-03-29 19:24:54 +08:00
|
|
|
if (PQ_SETNONBLOCKING(pgsql, 0)) {
|
|
|
|
php_error(E_NOTICE,"%s() cannot set connection to blocking mode",
|
|
|
|
get_active_function_name(TSRMLS_C));
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2001-12-10 12:02:08 +08:00
|
|
|
while ((pgsql_result = PQgetResult(pgsql))) {
|
|
|
|
PQclear(pgsql_result);
|
|
|
|
leftover = 1;
|
|
|
|
}
|
|
|
|
if (leftover) {
|
2001-12-19 13:41:26 +08:00
|
|
|
php_error(E_NOTICE,"%s() found results on this connection. Use pg_get_result() to get results",
|
2001-12-17 08:24:59 +08:00
|
|
|
get_active_function_name(TSRMLS_C));
|
2001-12-10 12:02:08 +08:00
|
|
|
}
|
2000-05-25 15:51:40 +08:00
|
|
|
pgsql_result = PQexec(pgsql, Z_STRVAL_PP(query));
|
1999-04-22 10:48:28 +08:00
|
|
|
|
|
|
|
if (pgsql_result) {
|
|
|
|
status = PQresultStatus(pgsql_result);
|
|
|
|
} else {
|
|
|
|
status = (ExecStatusType) PQstatus(pgsql);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
case PGRES_EMPTY_QUERY:
|
|
|
|
case PGRES_BAD_RESPONSE:
|
|
|
|
case PGRES_NONFATAL_ERROR:
|
|
|
|
case PGRES_FATAL_ERROR:
|
2002-03-26 16:43:57 +08:00
|
|
|
php_error(E_WARNING, "%s() query failed: %s",
|
|
|
|
get_active_function_name(TSRMLS_C), PQerrorMessage(pgsql));
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
break;
|
|
|
|
case PGRES_COMMAND_OK: /* successful command that did not return rows */
|
|
|
|
default:
|
|
|
|
if (pgsql_result) {
|
|
|
|
pg_result = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle));
|
|
|
|
pg_result->conn = pgsql;
|
|
|
|
pg_result->result = pgsql_result;
|
2001-06-20 02:10:08 +08:00
|
|
|
pg_result->row = -1;
|
2000-05-25 15:51:40 +08:00
|
|
|
ZEND_REGISTER_RESOURCE(return_value, pg_result, le_result);
|
1999-04-22 10:48:28 +08:00
|
|
|
} else {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
2001-06-05 21:12:10 +08:00
|
|
|
|
1999-12-18 19:38:04 +08:00
|
|
|
#define PHP_PG_NUM_ROWS 1
|
|
|
|
#define PHP_PG_NUM_FIELDS 2
|
|
|
|
#define PHP_PG_CMD_TUPLES 3
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ php_pgsql_get_result_info
|
|
|
|
*/
|
2002-02-06 15:16:10 +08:00
|
|
|
static void php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-05-25 15:51:40 +08:00
|
|
|
zval **result;
|
1999-04-22 10:48:28 +08:00
|
|
|
PGresult *pgsql_result;
|
|
|
|
pgsql_result_handle *pg_result;
|
1999-11-20 08:30:08 +08:00
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &result)==FAILURE) {
|
1999-04-22 10:48:28 +08:00
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result);
|
1999-04-22 10:48:28 +08:00
|
|
|
|
|
|
|
pgsql_result = pg_result->result;
|
2001-12-17 08:24:59 +08:00
|
|
|
|
1999-04-22 10:48:28 +08:00
|
|
|
switch (entry_type) {
|
1999-12-18 19:38:04 +08:00
|
|
|
case PHP_PG_NUM_ROWS:
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_LVAL_P(return_value) = PQntuples(pgsql_result);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
1999-12-18 19:38:04 +08:00
|
|
|
case PHP_PG_NUM_FIELDS:
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_LVAL_P(return_value) = PQnfields(pgsql_result);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
1999-12-18 19:38:04 +08:00
|
|
|
case PHP_PG_CMD_TUPLES:
|
1999-04-22 10:48:28 +08:00
|
|
|
#if HAVE_PQCMDTUPLES
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_LVAL_P(return_value) = atoi(PQcmdTuples(pgsql_result));
|
1999-04-22 10:48:28 +08:00
|
|
|
#else
|
2001-12-17 08:24:59 +08:00
|
|
|
php_error(E_WARNING,"This compilation does not support %s()",
|
2002-03-11 15:09:12 +08:00
|
|
|
get_active_function_name(TSRMLS_C));
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_LVAL_P(return_value) = 0;
|
1999-04-22 10:48:28 +08:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_TYPE_P(return_value) = IS_LONG;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto int pg_num_rows(resource result)
|
1999-04-22 10:48:28 +08:00
|
|
|
Return the number of rows in the result */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_num_rows)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-12-18 19:38:04 +08:00
|
|
|
php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_NUM_ROWS);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto int pg_num_fields(resource result)
|
1999-04-22 10:48:28 +08:00
|
|
|
Return the number of fields in the result */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_num_fields)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-12-18 19:38:04 +08:00
|
|
|
php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_NUM_FIELDS);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto int pg_affected_rows(resource result)
|
1999-04-22 10:48:28 +08:00
|
|
|
Returns the number of affected tuples */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_affected_rows)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-12-18 19:38:04 +08:00
|
|
|
php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_CMD_TUPLES);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-10 14:14:03 +08:00
|
|
|
/* {{{ proto string pg_last_notice(resource connection)
|
2001-05-06 14:33:48 +08:00
|
|
|
Returns the last notice set by the backend */
|
2001-06-04 16:06:07 +08:00
|
|
|
PHP_FUNCTION(pg_last_notice)
|
|
|
|
{
|
2001-09-27 05:44:48 +08:00
|
|
|
if (PGG(last_notice)) {
|
|
|
|
RETURN_STRINGL(PGG(last_notice), PGG(last_notice_len), 0);
|
2001-05-06 09:34:56 +08:00
|
|
|
} else {
|
2001-09-27 05:44:48 +08:00
|
|
|
RETURN_FALSE;
|
2001-05-06 09:34:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ get_field_name
|
|
|
|
*/
|
2002-02-06 15:16:10 +08:00
|
|
|
static char *get_field_name(PGconn *pgsql, Oid oid, HashTable *list TSRMLS_DC)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
|
|
|
PGresult *result;
|
|
|
|
char hashed_oid_key[32];
|
|
|
|
list_entry *field_type;
|
|
|
|
char *ret=NULL;
|
1999-11-20 08:30:08 +08:00
|
|
|
|
1999-04-22 10:48:28 +08:00
|
|
|
/* try to lookup the type in the resource list */
|
|
|
|
snprintf(hashed_oid_key,31,"pgsql_oid_%d",(int) oid);
|
|
|
|
hashed_oid_key[31]=0;
|
|
|
|
|
1999-08-03 03:17:14 +08:00
|
|
|
if (zend_hash_find(list,hashed_oid_key,strlen(hashed_oid_key)+1,(void **) &field_type)==SUCCESS) {
|
1999-04-22 10:48:28 +08:00
|
|
|
ret = estrdup((char *)field_type->ptr);
|
|
|
|
} else { /* hash all oid's */
|
|
|
|
int i,num_rows;
|
|
|
|
int oid_offset,name_offset;
|
|
|
|
char *tmp_oid, *tmp_name;
|
|
|
|
list_entry new_oid_entry;
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
if ((result = PQexec(pgsql,"select oid,typname from pg_type")) == NULL) {
|
1999-04-22 10:48:28 +08:00
|
|
|
return empty_string;
|
|
|
|
}
|
2000-05-25 15:51:40 +08:00
|
|
|
num_rows = PQntuples(result);
|
1999-04-22 10:48:28 +08:00
|
|
|
oid_offset = PQfnumber(result,"oid");
|
|
|
|
name_offset = PQfnumber(result,"typname");
|
|
|
|
|
|
|
|
for (i=0; i<num_rows; i++) {
|
2000-05-25 15:51:40 +08:00
|
|
|
if ((tmp_oid = PQgetvalue(result,i,oid_offset))==NULL) {
|
1999-04-22 10:48:28 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
snprintf(hashed_oid_key,31,"pgsql_oid_%s",tmp_oid);
|
2000-05-25 15:51:40 +08:00
|
|
|
if ((tmp_name = PQgetvalue(result,i,name_offset))==NULL) {
|
1999-04-22 10:48:28 +08:00
|
|
|
continue;
|
|
|
|
}
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_TYPE(new_oid_entry) = le_string;
|
1999-04-22 10:48:28 +08:00
|
|
|
new_oid_entry.ptr = estrdup(tmp_name);
|
1999-08-03 03:17:14 +08:00
|
|
|
zend_hash_update(list,hashed_oid_key,strlen(hashed_oid_key)+1,(void *) &new_oid_entry, sizeof(list_entry), NULL);
|
1999-04-22 10:48:28 +08:00
|
|
|
if (!ret && atoi(tmp_oid)==oid) {
|
|
|
|
ret = estrdup(tmp_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
1999-12-18 19:38:04 +08:00
|
|
|
#define PHP_PG_FIELD_NAME 1
|
|
|
|
#define PHP_PG_FIELD_SIZE 2
|
|
|
|
#define PHP_PG_FIELD_TYPE 3
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ php_pgsql_get_field_info
|
|
|
|
*/
|
2002-02-06 15:16:10 +08:00
|
|
|
static void php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-05-25 15:51:40 +08:00
|
|
|
zval **result, **field;
|
1999-04-22 10:48:28 +08:00
|
|
|
PGresult *pgsql_result;
|
|
|
|
pgsql_result_handle *pg_result;
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &result, &field)==FAILURE) {
|
1999-04-22 10:48:28 +08:00
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result);
|
|
|
|
|
1999-04-22 10:48:28 +08:00
|
|
|
pgsql_result = pg_result->result;
|
2000-05-25 15:51:40 +08:00
|
|
|
convert_to_long_ex(field);
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
if (Z_LVAL_PP(field) < 0 || Z_LVAL_PP(field) >= PQnfields(pgsql_result)) {
|
2002-03-11 22:53:59 +08:00
|
|
|
php_error(E_WARNING,"%s() bad field offset specified",
|
|
|
|
get_active_function_name(TSRMLS_C));
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (entry_type) {
|
1999-12-18 19:38:04 +08:00
|
|
|
case PHP_PG_FIELD_NAME:
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_STRVAL_P(return_value) = PQfname(pgsql_result, Z_LVAL_PP(field));
|
|
|
|
Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
|
|
|
|
Z_STRVAL_P(return_value) = estrndup(Z_STRVAL_P(return_value),Z_STRLEN_P(return_value));
|
|
|
|
Z_TYPE_P(return_value) = IS_STRING;
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
1999-12-18 19:38:04 +08:00
|
|
|
case PHP_PG_FIELD_SIZE:
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_LVAL_P(return_value) = PQfsize(pgsql_result, Z_LVAL_PP(field));
|
|
|
|
Z_TYPE_P(return_value) = IS_LONG;
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
1999-12-18 19:38:04 +08:00
|
|
|
case PHP_PG_FIELD_TYPE:
|
2002-02-06 15:16:10 +08:00
|
|
|
Z_STRVAL_P(return_value) = get_field_name(pg_result->conn, PQftype(pgsql_result, Z_LVAL_PP(field)), &EG(regular_list) TSRMLS_CC);
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
|
|
|
|
Z_TYPE_P(return_value) = IS_STRING;
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto string pg_field_name(resource result, int field_number)
|
1999-04-22 10:48:28 +08:00
|
|
|
Returns the name of the field */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_field_name)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-12-18 19:38:04 +08:00
|
|
|
php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_FIELD_NAME);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto int pg_field_size(resource result, int field_number)
|
1999-04-22 10:48:28 +08:00
|
|
|
Returns the internal size of the field */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_field_size)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-12-18 19:38:04 +08:00
|
|
|
php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_FIELD_SIZE);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto string pg_field_type(resource result, int field_number)
|
1999-04-22 10:48:28 +08:00
|
|
|
Returns the type name for the given field */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_field_type)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-12-18 19:38:04 +08:00
|
|
|
php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_FIELD_TYPE);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto int pg_field_num(resource result, string field_name)
|
1999-04-22 10:48:28 +08:00
|
|
|
Returns the field number of the named field */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_field_num)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-05-25 15:51:40 +08:00
|
|
|
zval **result, **field;
|
1999-04-22 10:48:28 +08:00
|
|
|
PGresult *pgsql_result;
|
|
|
|
pgsql_result_handle *pg_result;
|
1999-11-20 08:30:08 +08:00
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &result, &field)==FAILURE) {
|
1999-04-22 10:48:28 +08:00
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result);
|
|
|
|
|
1999-04-22 10:48:28 +08:00
|
|
|
pgsql_result = pg_result->result;
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
convert_to_string_ex(field);
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_LVAL_P(return_value) = PQfnumber(pgsql_result, Z_STRVAL_PP(field));
|
|
|
|
Z_TYPE_P(return_value) = IS_LONG;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 19:53:38 +08:00
|
|
|
/* {{{ proto mixed pg_fetch_result(resource result, [int row_number,] mixed field_name)
|
1999-04-22 10:48:28 +08:00
|
|
|
Returns values from a result identifier */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_fetch_result)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-05-25 15:51:40 +08:00
|
|
|
zval **result, **row, **field=NULL;
|
1999-04-22 10:48:28 +08:00
|
|
|
PGresult *pgsql_result;
|
|
|
|
pgsql_result_handle *pg_result;
|
2001-06-20 02:10:08 +08:00
|
|
|
int field_offset, pgsql_row;
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-20 02:10:08 +08:00
|
|
|
if ((ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &result, &row, &field)==FAILURE) &&
|
|
|
|
(ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &result, &field)==FAILURE)) {
|
1999-04-22 10:48:28 +08:00
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result);
|
|
|
|
|
1999-04-22 10:48:28 +08:00
|
|
|
pgsql_result = pg_result->result;
|
2001-06-20 02:10:08 +08:00
|
|
|
if (ZEND_NUM_ARGS() == 2) {
|
|
|
|
if (pg_result->row < 0)
|
|
|
|
pg_result->row = 0;
|
|
|
|
pgsql_row = pg_result->row;
|
|
|
|
if (pgsql_row >= PQntuples(pgsql_result)) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
convert_to_long_ex(row);
|
|
|
|
pgsql_row = Z_LVAL_PP(row);
|
|
|
|
if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
|
2002-03-21 08:06:45 +08:00
|
|
|
php_error(E_WARNING,"%s() unable to jump to row %d on PostgreSQL result index %d",
|
|
|
|
get_active_function_name(TSRMLS_C), Z_LVAL_PP(row), Z_LVAL_PP(result));
|
2001-06-20 02:10:08 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
2000-05-25 15:51:40 +08:00
|
|
|
switch(Z_TYPE_PP(field)) {
|
1999-04-22 10:48:28 +08:00
|
|
|
case IS_STRING:
|
2000-05-25 15:51:40 +08:00
|
|
|
field_offset = PQfnumber(pgsql_result, Z_STRVAL_PP(field));
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
default:
|
2000-05-25 15:51:40 +08:00
|
|
|
convert_to_long_ex(field);
|
|
|
|
field_offset = Z_LVAL_PP(field);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (field_offset<0 || field_offset>=PQnfields(pgsql_result)) {
|
2002-03-21 08:06:45 +08:00
|
|
|
php_error(E_WARNING,"%s() bad column offset specified",
|
|
|
|
get_active_function_name(TSRMLS_C));
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2001-06-20 02:10:08 +08:00
|
|
|
if (PQgetisnull(pgsql_result, pgsql_row, field_offset)) {
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_TYPE_P(return_value) = IS_NULL;
|
2000-08-16 09:03:20 +08:00
|
|
|
} else {
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_STRVAL_P(return_value) = PQgetvalue(pgsql_result, pgsql_row, field_offset);
|
|
|
|
Z_STRLEN_P(return_value) = (Z_STRVAL_P(return_value) ? strlen(Z_STRVAL_P(return_value)) : 0);
|
|
|
|
Z_STRVAL_P(return_value) = safe_estrndup(Z_STRVAL_P(return_value),Z_STRLEN_P(return_value));
|
|
|
|
Z_TYPE_P(return_value) = IS_STRING;
|
2000-08-16 09:03:20 +08:00
|
|
|
}
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2002-03-03 03:53:11 +08:00
|
|
|
/* {{{ void php_pgsql_fetch_hash */
|
1999-12-18 19:38:04 +08:00
|
|
|
static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int result_type)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-05-25 15:51:40 +08:00
|
|
|
zval **result, **row, **arg3;
|
1999-04-22 10:48:28 +08:00
|
|
|
PGresult *pgsql_result;
|
|
|
|
pgsql_result_handle *pg_result;
|
2001-06-20 02:10:08 +08:00
|
|
|
int i, num_fields, pgsql_row;
|
2000-05-25 15:51:40 +08:00
|
|
|
char *element, *field_name;
|
1999-04-22 10:48:28 +08:00
|
|
|
uint element_len;
|
1999-07-17 01:19:11 +08:00
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
switch (ZEND_NUM_ARGS()) {
|
2001-06-20 02:10:08 +08:00
|
|
|
case 1:
|
2002-03-03 03:53:11 +08:00
|
|
|
if (zend_get_parameters_ex(1, &result) == FAILURE) {
|
2001-06-20 02:10:08 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
if (!result_type) {
|
|
|
|
result_type = PGSQL_BOTH;
|
|
|
|
}
|
|
|
|
break;
|
1999-07-17 01:19:11 +08:00
|
|
|
case 2:
|
2002-03-03 03:53:11 +08:00
|
|
|
if (zend_get_parameters_ex(2, &result, &row) == FAILURE) {
|
1999-07-17 01:19:11 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
if (!result_type) {
|
|
|
|
result_type = PGSQL_BOTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
2002-03-03 03:53:11 +08:00
|
|
|
if (zend_get_parameters_ex(3, &result, &row, &arg3) == FAILURE) {
|
1999-07-17 01:19:11 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2000-05-25 15:51:40 +08:00
|
|
|
convert_to_long_ex(arg3);
|
|
|
|
result_type = Z_LVAL_PP(arg3);
|
1999-07-17 01:19:11 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
break;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result);
|
|
|
|
|
1999-04-22 10:48:28 +08:00
|
|
|
pgsql_result = pg_result->result;
|
2001-06-20 02:10:08 +08:00
|
|
|
|
|
|
|
if (ZEND_NUM_ARGS() == 1) {
|
|
|
|
pg_result->row++;
|
|
|
|
pgsql_row = pg_result->row;
|
|
|
|
if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
2002-03-03 03:53:11 +08:00
|
|
|
if (Z_TYPE_PP(row) != IS_NULL) {
|
|
|
|
convert_to_long_ex(row);
|
|
|
|
pgsql_row = Z_LVAL_PP(row);
|
|
|
|
pg_result->row = pgsql_row;
|
|
|
|
if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
|
2002-03-21 08:06:45 +08:00
|
|
|
php_error(E_WARNING, "%s() unable to jump to row %d on PostgreSQL result index %d",
|
|
|
|
get_active_function_name(TSRMLS_C), Z_LVAL_PP(row), Z_LVAL_PP(result));
|
2002-03-03 03:53:11 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* If 2nd param is NULL, ignore it and use the normal way of accessing the next row */
|
|
|
|
pg_result->row++;
|
|
|
|
pgsql_row = pg_result->row;
|
|
|
|
if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2001-06-20 02:10:08 +08:00
|
|
|
}
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
array_init(return_value);
|
2002-03-03 03:53:11 +08:00
|
|
|
for (i = 0, num_fields = PQnfields(pgsql_result); i < num_fields; i++) {
|
2001-06-20 02:10:08 +08:00
|
|
|
if (PQgetisnull(pgsql_result, pgsql_row, i)) {
|
2000-08-16 09:03:20 +08:00
|
|
|
if (result_type & PGSQL_NUM) {
|
2001-02-01 05:53:30 +08:00
|
|
|
add_index_null(return_value, i);
|
2000-09-09 17:40:16 +08:00
|
|
|
}
|
|
|
|
if (result_type & PGSQL_ASSOC) {
|
2000-08-16 09:03:20 +08:00
|
|
|
field_name = PQfname(pgsql_result, i);
|
2001-02-01 05:53:30 +08:00
|
|
|
add_assoc_null(return_value, field_name);
|
1999-07-17 01:19:11 +08:00
|
|
|
}
|
2000-08-16 09:03:20 +08:00
|
|
|
} else {
|
2001-06-20 02:10:08 +08:00
|
|
|
element = PQgetvalue(pgsql_result, pgsql_row, i);
|
2000-08-16 09:03:20 +08:00
|
|
|
element_len = (element ? strlen(element) : 0);
|
|
|
|
if (element) {
|
|
|
|
char *data;
|
|
|
|
int data_len;
|
|
|
|
int should_copy=0;
|
|
|
|
|
|
|
|
if (PG(magic_quotes_runtime)) {
|
2001-08-06 11:50:52 +08:00
|
|
|
data = php_addslashes(element, element_len, &data_len, 0 TSRMLS_CC);
|
2000-08-16 09:03:20 +08:00
|
|
|
} else {
|
|
|
|
data = safe_estrndup(element, element_len);
|
|
|
|
data_len = element_len;
|
|
|
|
}
|
1999-07-17 01:19:11 +08:00
|
|
|
|
2000-08-16 09:03:20 +08:00
|
|
|
if (result_type & PGSQL_NUM) {
|
|
|
|
add_index_stringl(return_value, i, data, data_len, should_copy);
|
|
|
|
should_copy=1;
|
|
|
|
}
|
1999-07-17 01:19:11 +08:00
|
|
|
|
2000-08-16 09:03:20 +08:00
|
|
|
if (result_type & PGSQL_ASSOC) {
|
|
|
|
field_name = PQfname(pgsql_result, i);
|
|
|
|
add_assoc_stringl(return_value, field_name, data, data_len, should_copy);
|
|
|
|
}
|
1999-07-17 01:19:11 +08:00
|
|
|
}
|
2000-08-16 09:03:20 +08:00
|
|
|
}
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-07-17 01:19:11 +08:00
|
|
|
|
2002-03-03 03:53:11 +08:00
|
|
|
/* {{{ proto array pg_fetch_row(resource result [, int row])
|
1999-07-17 01:26:16 +08:00
|
|
|
Get a row as an enumerated array */
|
1999-12-20 06:44:55 +08:00
|
|
|
PHP_FUNCTION(pg_fetch_row)
|
1999-07-17 01:26:16 +08:00
|
|
|
{
|
1999-12-18 19:38:04 +08:00
|
|
|
php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_NUM);
|
1999-07-17 01:26:16 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2002-03-03 03:53:11 +08:00
|
|
|
/* {{{ proto array pg_fetch_array(resource result [, int row [, int result_type]])
|
1999-04-22 10:48:28 +08:00
|
|
|
Fetch a row as an array */
|
1999-12-20 06:44:55 +08:00
|
|
|
PHP_FUNCTION(pg_fetch_array)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-12-18 19:38:04 +08:00
|
|
|
php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2002-03-03 03:53:11 +08:00
|
|
|
/* {{{ proto object pg_fetch_object(resource result [, int row [, int result_type]])
|
1999-04-22 10:48:28 +08:00
|
|
|
Fetch a row as an object */
|
1999-12-20 06:44:55 +08:00
|
|
|
PHP_FUNCTION(pg_fetch_object)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-12-18 19:38:04 +08:00
|
|
|
php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
|
2001-09-26 05:58:48 +08:00
|
|
|
if (Z_TYPE_P(return_value)==IS_ARRAY) {
|
2002-03-13 04:18:01 +08:00
|
|
|
object_and_properties_init(return_value, ZEND_STANDARD_CLASS_DEF_PTR, Z_ARRVAL_P(return_value));
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
1999-12-18 19:38:04 +08:00
|
|
|
#define PHP_PG_DATA_LENGTH 1
|
|
|
|
#define PHP_PG_DATA_ISNULL 2
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ php_pgsql_data_info
|
|
|
|
*/
|
2002-02-06 15:16:10 +08:00
|
|
|
static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-05-25 15:51:40 +08:00
|
|
|
zval **result, **row, **field;
|
1999-04-22 10:48:28 +08:00
|
|
|
PGresult *pgsql_result;
|
|
|
|
pgsql_result_handle *pg_result;
|
2001-06-20 02:10:08 +08:00
|
|
|
int field_offset, pgsql_row;
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-20 02:10:08 +08:00
|
|
|
if ((ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &result, &row, &field)==FAILURE) &&
|
|
|
|
(ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &result, &field)==FAILURE)) {
|
1999-04-22 10:48:28 +08:00
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result);
|
|
|
|
|
1999-04-22 10:48:28 +08:00
|
|
|
pgsql_result = pg_result->result;
|
2001-06-20 02:10:08 +08:00
|
|
|
if (ZEND_NUM_ARGS() == 2) {
|
|
|
|
if (pg_result->row < 0)
|
|
|
|
pg_result->row = 0;
|
|
|
|
pgsql_row = pg_result->row;
|
|
|
|
if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
convert_to_long_ex(row);
|
|
|
|
pgsql_row = Z_LVAL_PP(row);
|
|
|
|
if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
|
2002-03-21 08:06:45 +08:00
|
|
|
php_error(E_WARNING,"%s() unable to jump to row %d on PostgreSQL result index %d",
|
|
|
|
get_active_function_name(TSRMLS_C), Z_LVAL_PP(row), Z_LVAL_PP(result));
|
2001-06-20 02:10:08 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
2001-06-20 02:10:08 +08:00
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
switch(Z_TYPE_PP(field)) {
|
1999-04-22 10:48:28 +08:00
|
|
|
case IS_STRING:
|
2000-05-25 15:51:40 +08:00
|
|
|
convert_to_string_ex(field);
|
|
|
|
field_offset = PQfnumber(pgsql_result, Z_STRVAL_PP(field));
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
default:
|
2000-05-25 15:51:40 +08:00
|
|
|
convert_to_long_ex(field);
|
|
|
|
field_offset = Z_LVAL_PP(field);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
}
|
2000-05-25 15:51:40 +08:00
|
|
|
if (field_offset < 0 || field_offset >= PQnfields(pgsql_result)) {
|
2002-03-26 16:43:57 +08:00
|
|
|
php_error(E_WARNING, "%s() bad column offset specified",
|
|
|
|
get_active_function_name(TSRMLS_C));
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (entry_type) {
|
1999-12-18 19:38:04 +08:00
|
|
|
case PHP_PG_DATA_LENGTH:
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_LVAL_P(return_value) = PQgetlength(pgsql_result, pgsql_row, field_offset);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
1999-12-18 19:38:04 +08:00
|
|
|
case PHP_PG_DATA_ISNULL:
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_LVAL_P(return_value) = PQgetisnull(pgsql_result, pgsql_row, field_offset);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
}
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_TYPE_P(return_value) = IS_LONG;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto int pg_field_prtlen(resource result, [int row,] mixed field_name_or_number)
|
1999-04-22 10:48:28 +08:00
|
|
|
Returns the printed length */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_field_prtlen)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-12-18 19:38:04 +08:00
|
|
|
php_pgsql_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_DATA_LENGTH);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto int pg_field_is_null(resource result, [int row,] mixed field_name_or_number)
|
1999-04-22 10:48:28 +08:00
|
|
|
Test if a field is NULL */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_field_is_null)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-12-18 19:38:04 +08:00
|
|
|
php_pgsql_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_DATA_ISNULL);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto bool pg_free_result(resource result)
|
1999-04-22 10:48:28 +08:00
|
|
|
Free result memory */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_free_result)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-05-25 15:51:40 +08:00
|
|
|
zval **result;
|
1999-04-22 10:48:28 +08:00
|
|
|
pgsql_result_handle *pg_result;
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &result)==FAILURE) {
|
1999-04-22 10:48:28 +08:00
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
2000-05-25 15:51:40 +08:00
|
|
|
|
|
|
|
ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result);
|
|
|
|
if (Z_LVAL_PP(result) == 0) {
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2000-05-25 15:51:40 +08:00
|
|
|
zend_list_delete(Z_LVAL_PP(result));
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto int pg_last_oid(resource result)
|
1999-04-22 10:48:28 +08:00
|
|
|
Returns the last object identifier */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_last_oid)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-05-25 15:51:40 +08:00
|
|
|
zval **result;
|
1999-04-22 10:48:28 +08:00
|
|
|
PGresult *pgsql_result;
|
|
|
|
pgsql_result_handle *pg_result;
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &result)==FAILURE) {
|
1999-04-22 10:48:28 +08:00
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result);
|
1999-04-22 10:48:28 +08:00
|
|
|
pgsql_result = pg_result->result;
|
2002-03-29 19:24:54 +08:00
|
|
|
#ifdef HAVE_PQOIDVALUE
|
2001-12-17 08:24:59 +08:00
|
|
|
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
|
2001-09-26 05:58:48 +08:00
|
|
|
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));
|
|
|
|
Z_STRVAL_P(return_value) = estrndup(Z_STRVAL_P(return_value), Z_STRLEN_P(return_value));
|
|
|
|
Z_TYPE_P(return_value) = IS_STRING;
|
1999-04-22 10:48:28 +08:00
|
|
|
} else {
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_STRVAL_P(return_value) = empty_string;
|
2000-05-23 14:03:34 +08:00
|
|
|
}
|
|
|
|
#endif
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2000-06-01 04:40:28 +08:00
|
|
|
/* {{{ proto bool pg_trace(string filename [, string mode [, resource connection]])
|
2000-06-24 01:09:54 +08:00
|
|
|
Enable tracing a PostgreSQL connection */
|
2000-06-01 04:40:28 +08:00
|
|
|
PHP_FUNCTION(pg_trace)
|
|
|
|
{
|
2000-07-29 14:07:26 +08:00
|
|
|
zval **z_filename, **z_mode, **z_pgsql_link = NULL;
|
2000-06-01 04:40:28 +08:00
|
|
|
int id = -1;
|
|
|
|
PGconn *pgsql;
|
|
|
|
char *mode = "w";
|
|
|
|
FILE *fp;
|
2002-03-16 09:28:57 +08:00
|
|
|
php_stream *stream;
|
2000-06-01 04:40:28 +08:00
|
|
|
id = PGG(default_link);
|
|
|
|
|
|
|
|
switch (ZEND_NUM_ARGS()) {
|
|
|
|
case 1:
|
|
|
|
if (zend_get_parameters_ex(1, &z_filename)==FAILURE) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2000-07-29 14:07:26 +08:00
|
|
|
CHECK_DEFAULT_LINK(id);
|
2000-06-01 04:40:28 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (zend_get_parameters_ex(2, &z_filename, &z_mode)==FAILURE) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2000-07-29 14:07:26 +08:00
|
|
|
CHECK_DEFAULT_LINK(id);
|
2000-06-01 04:40:28 +08:00
|
|
|
convert_to_string_ex(z_mode);
|
|
|
|
mode = Z_STRVAL_PP(z_mode);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if (zend_get_parameters_ex(3, &z_filename, &z_mode, &z_pgsql_link)==FAILURE) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
convert_to_string_ex(z_mode);
|
|
|
|
mode = Z_STRVAL_PP(z_mode);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ZEND_WRONG_PARAM_COUNT();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_FETCH_RESOURCE2(pgsql, PGconn *, z_pgsql_link, id, "PostgreSQL link", le_link, le_plink);
|
|
|
|
convert_to_string_ex(z_filename);
|
|
|
|
|
2002-03-18 06:50:59 +08:00
|
|
|
stream = php_stream_open_wrapper(Z_STRVAL_PP(z_filename), mode, ENFORCE_SAFE_MODE|REPORT_ERRORS, NULL);
|
2000-06-01 04:40:28 +08:00
|
|
|
|
2002-03-16 05:03:08 +08:00
|
|
|
if (!stream) {
|
2000-06-01 04:40:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2002-03-22 06:35:02 +08:00
|
|
|
if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void**)fp, REPORT_ERRORS)) {
|
2002-03-16 05:03:08 +08:00
|
|
|
php_stream_close(stream);
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2002-03-20 22:21:30 +08:00
|
|
|
php_stream_auto_cleanup(stream);
|
2000-06-01 04:40:28 +08:00
|
|
|
PQtrace(pgsql, fp);
|
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
2000-06-01 04:40:28 +08:00
|
|
|
|
2001-12-10 14:14:03 +08:00
|
|
|
/* {{{ proto bool pg_untrace([resource connection])
|
2000-06-01 04:40:28 +08:00
|
|
|
Disable tracing of a PostgreSQL connection */
|
|
|
|
PHP_FUNCTION(pg_untrace)
|
|
|
|
{
|
2000-07-29 14:07:26 +08:00
|
|
|
zval **pgsql_link = NULL;
|
2000-06-01 04:40:28 +08:00
|
|
|
int id = -1;
|
|
|
|
PGconn *pgsql;
|
|
|
|
|
|
|
|
switch (ZEND_NUM_ARGS()) {
|
|
|
|
case 0:
|
|
|
|
id = PGG(default_link);
|
2000-07-29 14:07:26 +08:00
|
|
|
CHECK_DEFAULT_LINK(id);
|
2000-06-01 04:40:28 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (zend_get_parameters_ex(1, &pgsql_link)==FAILURE) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ZEND_WRONG_PARAM_COUNT();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);
|
|
|
|
PQuntrace(pgsql);
|
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
2000-06-01 04:40:28 +08:00
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto int pg_lo_create(resource connection)
|
1999-04-22 10:48:28 +08:00
|
|
|
Create a large object */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_lo_create)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-07-29 14:07:26 +08:00
|
|
|
zval **pgsql_link = NULL;
|
1999-04-22 10:48:28 +08:00
|
|
|
PGconn *pgsql;
|
|
|
|
Oid pgsql_oid;
|
2000-05-25 15:51:40 +08:00
|
|
|
int id = -1;
|
1999-11-20 08:30:08 +08:00
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
switch(ZEND_NUM_ARGS()) {
|
1999-04-22 10:48:28 +08:00
|
|
|
case 0:
|
1999-11-20 08:30:08 +08:00
|
|
|
id = PGG(default_link);
|
2000-07-29 14:07:26 +08:00
|
|
|
CHECK_DEFAULT_LINK(id);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
2000-05-25 15:51:40 +08:00
|
|
|
if (zend_get_parameters_ex(1, &pgsql_link)==FAILURE) {
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);
|
1999-04-22 10:48:28 +08:00
|
|
|
|
|
|
|
/* XXX: Archive modes not supported until I get some more data. Don't think anybody's
|
|
|
|
using it anyway. I believe it's also somehow related to the 'time travel' feature of
|
|
|
|
PostgreSQL, that's on the list of features to be removed... Create modes not supported.
|
|
|
|
What's the use of an object that can be only written to, but not read from, and vice
|
|
|
|
versa? Beats me... And the access type (r/w) must be specified again when opening
|
|
|
|
the object, probably (?) overrides this. (Jouni)
|
2002-03-11 15:23:07 +08:00
|
|
|
*/
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
if ((pgsql_oid = lo_creat(pgsql, INV_READ|INV_WRITE))==0) {
|
2002-03-26 16:43:57 +08:00
|
|
|
php_error(E_WARNING, "%s() unable to create PostgreSQL large object",
|
|
|
|
get_active_function_name(TSRMLS_C));
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_LVAL_P(return_value) = pgsql_oid;
|
|
|
|
Z_TYPE_P(return_value) = IS_LONG;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto bool pg_lo_unlink([resource connection,] int large_object_oid)
|
1999-04-22 10:48:28 +08:00
|
|
|
Delete a large object */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_lo_unlink)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-07-29 14:07:26 +08:00
|
|
|
zval **pgsql_link = NULL, **oid;
|
1999-04-22 10:48:28 +08:00
|
|
|
PGconn *pgsql;
|
|
|
|
Oid pgsql_oid;
|
2000-05-25 15:51:40 +08:00
|
|
|
int id = -1;
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
switch(ZEND_NUM_ARGS()) {
|
1999-04-22 10:48:28 +08:00
|
|
|
case 1:
|
2000-05-25 15:51:40 +08:00
|
|
|
if (zend_get_parameters_ex(1, &oid)==FAILURE) {
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2000-05-25 15:51:40 +08:00
|
|
|
convert_to_long_ex(oid);
|
|
|
|
pgsql_oid = Z_LVAL_PP(oid);
|
1999-11-20 08:30:08 +08:00
|
|
|
id = PGG(default_link);
|
2000-07-29 14:07:26 +08:00
|
|
|
CHECK_DEFAULT_LINK(id);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
2000-05-25 15:51:40 +08:00
|
|
|
if (zend_get_parameters_ex(2, &pgsql_link, &oid)==FAILURE) {
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2000-05-25 15:51:40 +08:00
|
|
|
convert_to_long_ex(oid);
|
|
|
|
pgsql_oid = Z_LVAL_PP(oid);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
if (lo_unlink(pgsql, pgsql_oid) == -1) {
|
2002-03-26 16:43:57 +08:00
|
|
|
php_error(E_WARNING, "%s() unable to delete PostgreSQL large object %d",
|
|
|
|
get_active_function_name(TSRMLS_C), (int) pgsql_oid);
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto resource pg_lo_open([resource connection,] int large_object_oid, string mode)
|
1999-04-22 10:48:28 +08:00
|
|
|
Open a large object and return fd */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_lo_open)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-07-29 14:07:26 +08:00
|
|
|
zval **pgsql_link = NULL, **oid, **mode;
|
1999-04-22 10:48:28 +08:00
|
|
|
PGconn *pgsql;
|
|
|
|
Oid pgsql_oid;
|
2000-05-25 15:51:40 +08:00
|
|
|
int id = -1, pgsql_mode=0, pgsql_lofd;
|
1999-04-22 10:48:28 +08:00
|
|
|
int create=0;
|
|
|
|
char *mode_string=NULL;
|
|
|
|
pgLofp *pgsql_lofp;
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
switch(ZEND_NUM_ARGS()) {
|
1999-04-22 10:48:28 +08:00
|
|
|
case 2:
|
2000-05-25 15:51:40 +08:00
|
|
|
if (zend_get_parameters_ex(2, &oid, &mode)==FAILURE) {
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2000-05-25 15:51:40 +08:00
|
|
|
convert_to_long_ex(oid);
|
|
|
|
pgsql_oid = Z_LVAL_PP(oid);
|
|
|
|
convert_to_string_ex(mode);
|
|
|
|
mode_string = Z_STRVAL_PP(mode);
|
1999-11-20 08:30:08 +08:00
|
|
|
id = PGG(default_link);
|
2000-07-29 14:07:26 +08:00
|
|
|
CHECK_DEFAULT_LINK(id);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
case 3:
|
2000-05-25 15:51:40 +08:00
|
|
|
if (zend_get_parameters_ex(3, &pgsql_link, &oid, &mode)==FAILURE) {
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2000-05-25 15:51:40 +08:00
|
|
|
convert_to_long_ex(oid);
|
|
|
|
pgsql_oid = Z_LVAL_PP(oid);
|
|
|
|
convert_to_string_ex(mode);
|
|
|
|
mode_string = Z_STRVAL_PP(mode);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);
|
1999-04-22 10:48:28 +08:00
|
|
|
|
|
|
|
/* r/w/+ is little bit more PHP-like than INV_READ/INV_WRITE and a lot of
|
|
|
|
faster to type. Unfortunately, doesn't behave the same way as fopen()...
|
|
|
|
(Jouni)
|
2002-03-11 15:23:07 +08:00
|
|
|
*/
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
if (strchr(mode_string, 'r') == mode_string) {
|
1999-04-22 10:48:28 +08:00
|
|
|
pgsql_mode |= INV_READ;
|
2000-05-25 15:51:40 +08:00
|
|
|
if (strchr(mode_string, '+') == mode_string+1) {
|
1999-04-22 10:48:28 +08:00
|
|
|
pgsql_mode |= INV_WRITE;
|
|
|
|
}
|
|
|
|
}
|
2000-05-25 15:51:40 +08:00
|
|
|
if (strchr(mode_string, 'w') == mode_string) {
|
1999-04-22 10:48:28 +08:00
|
|
|
pgsql_mode |= INV_WRITE;
|
|
|
|
create = 1;
|
2000-05-25 15:51:40 +08:00
|
|
|
if (strchr(mode_string, '+') == mode_string+1) {
|
1999-04-22 10:48:28 +08:00
|
|
|
pgsql_mode |= INV_READ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pgsql_lofp = (pgLofp *) emalloc(sizeof(pgLofp));
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
if ((pgsql_lofd = lo_open(pgsql, pgsql_oid, pgsql_mode)) == -1) {
|
1999-04-22 10:48:28 +08:00
|
|
|
if (create) {
|
2000-05-25 15:51:40 +08:00
|
|
|
if ((pgsql_oid = lo_creat(pgsql, INV_READ|INV_WRITE)) == 0) {
|
1999-04-22 10:48:28 +08:00
|
|
|
efree(pgsql_lofp);
|
2002-03-26 16:43:57 +08:00
|
|
|
php_error(E_WARNING, "%s() unable to create PostgreSQL large object",
|
|
|
|
get_active_function_name(TSRMLS_C));
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
} else {
|
2000-05-25 15:51:40 +08:00
|
|
|
if ((pgsql_lofd = lo_open(pgsql, pgsql_oid, pgsql_mode)) == -1) {
|
|
|
|
if (lo_unlink(pgsql, pgsql_oid) == -1) {
|
1999-04-22 10:48:28 +08:00
|
|
|
efree(pgsql_lofp);
|
2002-03-26 16:43:57 +08:00
|
|
|
php_error(E_WARNING, "%s() Something's really messed up!!! Your database is badly corrupted in a way NOT related to PHP",
|
|
|
|
get_active_function_name(TSRMLS_C));
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
efree(pgsql_lofp);
|
2002-03-26 16:43:57 +08:00
|
|
|
php_error(E_WARNING, "%s() unable to open PostgreSQL large object",
|
|
|
|
get_active_function_name(TSRMLS_C));
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
} else {
|
|
|
|
pgsql_lofp->conn = pgsql;
|
|
|
|
pgsql_lofp->lofd = pgsql_lofd;
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_LVAL_P(return_value) = zend_list_insert(pgsql_lofp, le_lofp);
|
|
|
|
Z_TYPE_P(return_value) = IS_LONG;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
efree(pgsql_lofp);
|
2002-03-26 16:43:57 +08:00
|
|
|
php_error(E_WARNING,"%s() unable to open PostgreSQL large object",
|
|
|
|
get_active_function_name(TSRMLS_C));
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pgsql_lofp->conn = pgsql;
|
|
|
|
pgsql_lofp->lofd = pgsql_lofd;
|
2000-05-25 15:51:40 +08:00
|
|
|
ZEND_REGISTER_RESOURCE(return_value, pgsql_lofp, le_lofp);
|
|
|
|
/*
|
2002-03-11 15:23:07 +08:00
|
|
|
Z_LVAL_P(return_value) = zend_list_insert(pgsql_lofp, le_lofp);
|
|
|
|
Z_TYPE_P(return_value) = IS_LONG;
|
2000-05-25 15:51:40 +08:00
|
|
|
*/
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto bool pg_lo_close(resource large_object)
|
1999-04-22 10:48:28 +08:00
|
|
|
Close a large object */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_lo_close)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-05-25 15:51:40 +08:00
|
|
|
zval **pgsql_lofp;
|
1999-04-22 10:48:28 +08:00
|
|
|
pgLofp *pgsql;
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
switch(ZEND_NUM_ARGS()) {
|
1999-04-22 10:48:28 +08:00
|
|
|
case 1:
|
2000-05-25 15:51:40 +08:00
|
|
|
if (zend_get_parameters_ex(1, &pgsql_lofp)==FAILURE) {
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
ZEND_FETCH_RESOURCE(pgsql, pgLofp *, pgsql_lofp, -1, "PostgreSQL large object", le_lofp);
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
if (lo_close((PGconn *)pgsql->conn, pgsql->lofd) < 0) {
|
2002-03-26 16:43:57 +08:00
|
|
|
php_error(E_WARNING, "%s() unable to close PostgreSQL large object descriptor %d",
|
|
|
|
get_active_function_name(TSRMLS_C), pgsql->lofd);
|
1999-04-22 10:48:28 +08:00
|
|
|
RETVAL_FALSE;
|
|
|
|
} else {
|
|
|
|
RETVAL_TRUE;
|
|
|
|
}
|
2000-05-25 15:51:40 +08:00
|
|
|
|
|
|
|
zend_list_delete(Z_LVAL_PP(pgsql_lofp));
|
1999-04-22 10:48:28 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto string pg_lo_read(resource large_object [, int len])
|
1999-04-22 10:48:28 +08:00
|
|
|
Read a large object */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_lo_read)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-05-25 15:51:40 +08:00
|
|
|
zval **pgsql_id, **len;
|
2001-05-27 09:10:36 +08:00
|
|
|
int buf_len = 1024, nbytes;
|
1999-04-22 10:48:28 +08:00
|
|
|
char *buf;
|
|
|
|
pgLofp *pgsql;
|
|
|
|
|
2001-05-27 09:10:36 +08:00
|
|
|
if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 2 ||
|
|
|
|
zend_get_parameters_ex(ZEND_NUM_ARGS(), &pgsql_id, &len) == FAILURE) {
|
|
|
|
WRONG_PARAM_COUNT;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
ZEND_FETCH_RESOURCE(pgsql, pgLofp *, pgsql_id, -1, "PostgreSQL large object", le_lofp);
|
|
|
|
|
2001-05-27 09:10:36 +08:00
|
|
|
if (ZEND_NUM_ARGS() > 1) {
|
|
|
|
convert_to_long_ex(len);
|
|
|
|
buf_len = Z_LVAL_PP(len);
|
|
|
|
}
|
|
|
|
|
1999-04-22 10:48:28 +08:00
|
|
|
buf = (char *) emalloc(sizeof(char)*(buf_len+1));
|
|
|
|
if ((nbytes = lo_read((PGconn *)pgsql->conn, pgsql->lofd, buf, buf_len))<0) {
|
|
|
|
efree(buf);
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2001-05-27 09:10:36 +08:00
|
|
|
|
|
|
|
buf[nbytes] = 0;
|
|
|
|
RETURN_STRINGL(buf, nbytes, 0);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto int pg_lo_write(resource large_object, string buf [, int len])
|
1999-04-22 10:48:28 +08:00
|
|
|
Write a large object */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_lo_write)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2001-05-27 09:10:36 +08:00
|
|
|
zval **pgsql_id, **str, **z_len;
|
2000-05-25 15:51:40 +08:00
|
|
|
int nbytes;
|
2001-05-27 09:10:36 +08:00
|
|
|
int len;
|
1999-04-22 10:48:28 +08:00
|
|
|
pgLofp *pgsql;
|
2001-05-27 09:10:36 +08:00
|
|
|
int argc = ZEND_NUM_ARGS();
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-05-27 09:10:36 +08:00
|
|
|
if (argc < 2 || argc > 3 ||
|
|
|
|
zend_get_parameters_ex(argc, &pgsql_id, &str, &z_len) == FAILURE) {
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
|
|
|
|
|
|
|
convert_to_string_ex(str);
|
|
|
|
|
|
|
|
if (argc > 2) {
|
|
|
|
convert_to_long_ex(z_len);
|
|
|
|
len = Z_LVAL_PP(z_len);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
len = Z_STRLEN_PP(str);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
ZEND_FETCH_RESOURCE(pgsql, pgLofp *, pgsql_id, -1, "PostgreSQL large object", le_lofp);
|
|
|
|
|
2001-05-27 09:10:36 +08:00
|
|
|
if ((nbytes = lo_write((PGconn *)pgsql->conn, pgsql->lofd, Z_STRVAL_PP(str), len)) == -1) {
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2001-05-27 09:10:36 +08:00
|
|
|
|
|
|
|
RETURN_LONG(nbytes);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto int pg_lo_read_all(resource large_object)
|
1999-04-22 10:48:28 +08:00
|
|
|
Read a large object and send straight to browser */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_lo_read_all)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-05-25 15:51:40 +08:00
|
|
|
zval **pgsql_id;
|
|
|
|
int i, tbytes;
|
1999-04-22 10:48:28 +08:00
|
|
|
volatile int nbytes;
|
|
|
|
char buf[8192];
|
|
|
|
pgLofp *pgsql;
|
|
|
|
int output=1;
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
switch(ZEND_NUM_ARGS()) {
|
1999-04-22 10:48:28 +08:00
|
|
|
case 1:
|
2000-06-27 21:26:04 +08:00
|
|
|
if (zend_get_parameters_ex(1, &pgsql_id)==FAILURE) {
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-05-25 15:51:40 +08:00
|
|
|
ZEND_FETCH_RESOURCE(pgsql, pgLofp *, pgsql_id, -1, "PostgreSQL large object", le_lofp);
|
1999-04-22 10:48:28 +08:00
|
|
|
|
|
|
|
tbytes = 0;
|
|
|
|
while ((nbytes = lo_read((PGconn *)pgsql->conn, pgsql->lofd, buf, 8192))>0) {
|
|
|
|
for(i=0; i<nbytes; i++) {
|
1999-07-17 20:56:38 +08:00
|
|
|
if (output) { (void) PUTC(buf[i]); }
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
tbytes += i;
|
|
|
|
}
|
2001-12-17 08:24:59 +08:00
|
|
|
RETURN_LONG(tbytes);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2000-06-05 04:24:07 +08:00
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto int pg_lo_import([resource connection, ] string filename)
|
2000-06-05 04:24:07 +08:00
|
|
|
Import large object direct from filesystem */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_lo_import)
|
2000-06-05 04:24:07 +08:00
|
|
|
{
|
2001-12-17 08:24:59 +08:00
|
|
|
zval *pgsql_link = NULL;
|
|
|
|
char *file_in;
|
|
|
|
int id = -1, name_len;
|
|
|
|
int argc = ZEND_NUM_ARGS();
|
2000-06-05 04:24:07 +08:00
|
|
|
PGconn *pgsql;
|
|
|
|
Oid oid;
|
2001-12-17 08:24:59 +08:00
|
|
|
|
|
|
|
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,
|
2002-03-11 15:23:07 +08:00
|
|
|
"s", &file_in, &name_len) == SUCCESS) {
|
2001-12-17 08:24:59 +08:00
|
|
|
id = PGG(default_link);
|
|
|
|
CHECK_DEFAULT_LINK(id);
|
|
|
|
}
|
|
|
|
else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
|
2002-03-11 15:23:07 +08:00
|
|
|
"rs", &pgsql_link, &file_in, &name_len) == SUCCESS) {
|
2001-12-17 08:24:59 +08:00
|
|
|
php_error(E_NOTICE, "Old API for %s() is used.", get_active_function_name(TSRMLS_C));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
WRONG_PARAM_COUNT;
|
2000-06-05 04:24:07 +08:00
|
|
|
}
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
if (PG(safe_mode) &&(!php_checkuid(file_in, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
|
2000-06-05 04:24:07 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
|
2000-06-05 04:24:07 +08:00
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
oid = lo_import(pgsql, file_in);
|
2000-06-05 04:24:07 +08:00
|
|
|
|
|
|
|
if (oid > 0) {
|
|
|
|
RETURN_LONG((int)oid);
|
|
|
|
} else {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto bool pg_lo_export([resource connection, ] int objoid, string filename)
|
2000-06-19 00:31:52 +08:00
|
|
|
Export large object direct to filesystem */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_lo_export)
|
2000-06-05 04:24:07 +08:00
|
|
|
{
|
2001-12-17 08:24:59 +08:00
|
|
|
zval *pgsql_link = NULL;
|
|
|
|
char *file_out;
|
|
|
|
int id = -1, name_len, oid_id;
|
|
|
|
int argc = ZEND_NUM_ARGS();
|
2000-06-05 04:24:07 +08:00
|
|
|
Oid oid;
|
|
|
|
PGconn *pgsql;
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
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,
|
2002-03-11 15:23:07 +08:00
|
|
|
"ls", &oid_id, &file_out, &name_len) == SUCCESS) {
|
2001-12-17 08:24:59 +08:00
|
|
|
id = PGG(default_link);
|
|
|
|
CHECK_DEFAULT_LINK(id);
|
|
|
|
}
|
|
|
|
else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
|
2002-03-11 15:23:07 +08:00
|
|
|
"lsr", &oid_id, &file_out, &name_len, &pgsql_link) == SUCCESS) {
|
2001-12-17 08:24:59 +08:00
|
|
|
php_error(E_NOTICE, "Old API for %s() is used.", get_active_function_name(TSRMLS_C));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
WRONG_PARAM_COUNT;
|
2000-06-05 04:24:07 +08:00
|
|
|
}
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
|
2000-06-05 04:24:07 +08:00
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
oid = (Oid) oid_id;
|
2000-06-05 04:24:07 +08:00
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
if (lo_export(pgsql, oid, file_out)) {
|
2000-06-05 04:24:07 +08:00
|
|
|
RETURN_TRUE;
|
|
|
|
} else {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 20:14:46 +08:00
|
|
|
/* {{{ proto bool pg_lo_seek(resource large_object, int offset [, int whence])
|
2001-12-17 08:24:59 +08:00
|
|
|
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();
|
2000-07-02 16:59:44 +08:00
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2002-03-29 19:24:54 +08:00
|
|
|
#ifdef HAVE_PQCLIENTENCODING
|
2001-12-10 14:14:03 +08:00
|
|
|
/* {{{ proto int pg_set_client_encoding([resource connection,] string encoding)
|
2000-07-02 16:59:44 +08:00
|
|
|
Set client encoding */
|
2000-09-13 12:13:36 +08:00
|
|
|
PHP_FUNCTION(pg_set_client_encoding)
|
2000-07-02 16:59:44 +08:00
|
|
|
{
|
2000-07-29 14:07:26 +08:00
|
|
|
zval **encoding, **pgsql_link = NULL;
|
2000-07-02 16:59:44 +08:00
|
|
|
int id = -1;
|
|
|
|
PGconn *pgsql;
|
|
|
|
|
|
|
|
switch(ZEND_NUM_ARGS()) {
|
|
|
|
case 1:
|
|
|
|
if (zend_get_parameters_ex(1, &encoding)==FAILURE) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
id = PGG(default_link);
|
2000-07-29 14:07:26 +08:00
|
|
|
CHECK_DEFAULT_LINK(id);
|
2000-07-02 16:59:44 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (zend_get_parameters_ex(2, &pgsql_link, &encoding)==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(encoding);
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_LVAL_P(return_value) = PQsetClientEncoding(pgsql, Z_STRVAL_PP(encoding));
|
|
|
|
Z_TYPE_P(return_value) = IS_LONG;
|
2000-07-02 16:59:44 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-10 14:14:03 +08:00
|
|
|
/* {{{ proto string pg_client_encoding([resource connection])
|
2000-07-02 16:59:44 +08:00
|
|
|
Get the current client encoding */
|
2000-09-13 12:13:36 +08:00
|
|
|
PHP_FUNCTION(pg_client_encoding)
|
2000-07-02 16:59:44 +08:00
|
|
|
{
|
2000-07-29 14:07:26 +08:00
|
|
|
zval **pgsql_link = NULL;
|
2000-07-02 16:59:44 +08:00
|
|
|
int id = -1;
|
|
|
|
PGconn *pgsql;
|
|
|
|
|
|
|
|
switch(ZEND_NUM_ARGS()) {
|
|
|
|
case 0:
|
|
|
|
id = PGG(default_link);
|
2000-07-29 14:07:26 +08:00
|
|
|
CHECK_DEFAULT_LINK(id);
|
2000-07-02 16:59:44 +08:00
|
|
|
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);
|
|
|
|
|
2000-07-29 12:24:11 +08:00
|
|
|
/* Just do the same as found in PostgreSQL sources... */
|
|
|
|
|
|
|
|
#ifndef HAVE_PGSQL_WITH_MULTIBYTE_SUPPORT
|
|
|
|
#define pg_encoding_to_char(x) "SQL_ASCII"
|
|
|
|
#endif
|
|
|
|
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_STRVAL_P(return_value)
|
2002-03-11 15:23:07 +08:00
|
|
|
= (char *) pg_encoding_to_char(PQclientEncoding(pgsql));
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
|
|
|
|
Z_STRVAL_P(return_value) = (char *) estrdup(Z_STRVAL_P(return_value));
|
|
|
|
Z_TYPE_P(return_value) = IS_STRING;
|
2000-07-02 16:59:44 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
#endif
|
2001-12-10 12:02:08 +08:00
|
|
|
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
#define COPYBUFSIZ 8192
|
|
|
|
|
|
|
|
/* {{{ proto bool pg_end_copy([resource connection])
|
|
|
|
Sync with backend. Completes the Copy command */
|
|
|
|
PHP_FUNCTION(pg_end_copy)
|
2001-12-10 12:02:08 +08:00
|
|
|
{
|
|
|
|
zval **pgsql_link = NULL;
|
|
|
|
int id = -1;
|
|
|
|
PGconn *pgsql;
|
2001-12-17 08:24:59 +08:00
|
|
|
int result = 0;
|
2001-12-10 12:02:08 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
result = PQendcopy(pgsql);
|
|
|
|
|
|
|
|
if (result!=0) {
|
2002-03-26 16:43:57 +08:00
|
|
|
php_error(E_WARNING, "%s() PostgreSQL query failed: %s",
|
|
|
|
get_active_function_name(TSRMLS_C), PQerrorMessage(pgsql));
|
2001-12-17 08:24:59 +08:00
|
|
|
RETURN_FALSE;
|
2002-03-11 15:23:07 +08:00
|
|
|
}
|
|
|
|
RETURN_TRUE;
|
2001-12-17 08:24:59 +08:00
|
|
|
}
|
2001-12-10 12:02:08 +08:00
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
|
|
|
|
/* {{{ proto bool pg_put_line([resource connection,] string query)
|
|
|
|
Send null-terminated string to backend server*/
|
|
|
|
PHP_FUNCTION(pg_put_line)
|
2001-12-10 12:02:08 +08:00
|
|
|
{
|
2001-12-17 08:24:59 +08:00
|
|
|
zval **query, **pgsql_link = NULL;
|
2001-12-10 12:02:08 +08:00
|
|
|
int id = -1;
|
|
|
|
PGconn *pgsql;
|
2001-12-17 08:24:59 +08:00
|
|
|
int result = 0;
|
2001-12-10 12:02:08 +08:00
|
|
|
|
|
|
|
switch(ZEND_NUM_ARGS()) {
|
2001-12-17 08:24:59 +08:00
|
|
|
case 1:
|
|
|
|
if (zend_get_parameters_ex(1, &query)==FAILURE) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2001-12-10 12:02:08 +08:00
|
|
|
id = PGG(default_link);
|
|
|
|
CHECK_DEFAULT_LINK(id);
|
|
|
|
break;
|
2001-12-17 08:24:59 +08:00
|
|
|
case 2:
|
|
|
|
if (zend_get_parameters_ex(2, &pgsql_link, &query)==FAILURE) {
|
2001-12-10 12:02:08 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
break;
|
|
|
|
}
|
2001-12-17 08:24:59 +08:00
|
|
|
|
2001-12-10 12:02:08 +08:00
|
|
|
ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);
|
2001-12-17 08:24:59 +08:00
|
|
|
|
|
|
|
convert_to_string_ex(query);
|
|
|
|
result = PQputline(pgsql, Z_STRVAL_PP(query));
|
|
|
|
|
|
|
|
if (result==EOF) {
|
2002-03-26 16:43:57 +08:00
|
|
|
php_error(E_WARNING, "%s() PostgreSQL query failed: %s",
|
|
|
|
get_active_function_name(TSRMLS_C), PQerrorMessage(pgsql));
|
2001-12-17 08:24:59 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2002-01-21 08:36:22 +08:00
|
|
|
/* {{{ proto array pg_copy_to(int connection, string table_name [, string delimiter [, string null_as]])
|
|
|
|
Copy table to array */
|
2001-12-17 08:24:59 +08:00
|
|
|
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'",
|
2002-03-11 15:23:07 +08:00
|
|
|
table_name, pg_delim, pg_null_as);
|
2001-12-17 08:24:59 +08:00
|
|
|
|
|
|
|
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))) {
|
2002-03-26 16:43:57 +08:00
|
|
|
php_error(E_WARNING, "%s() query failed: %s",
|
2001-12-17 08:24:59 +08:00
|
|
|
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)) {
|
2002-03-26 16:43:57 +08:00
|
|
|
php_error(E_WARNING, "%s() query failed: %s",
|
2001-12-17 08:24:59 +08:00
|
|
|
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);
|
2002-03-26 16:43:57 +08:00
|
|
|
php_error(E_WARNING, "%s() query failed: %s",
|
2001-12-17 08:24:59 +08:00
|
|
|
get_active_function_name(TSRMLS_C), PQerrorMessage(pgsql));
|
|
|
|
RETURN_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2002-01-21 08:36:22 +08:00
|
|
|
/* {{{ proto bool pg_copy_from(int connection, string table_name , array rows [, string delimiter [, string null_as]])
|
|
|
|
Copy table from array */
|
2001-12-17 08:24:59 +08:00
|
|
|
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'",
|
2002-03-11 15:23:07 +08:00
|
|
|
table_name, pg_delim, pg_null_as);
|
2001-12-17 08:24:59 +08:00
|
|
|
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",
|
2002-03-11 15:23:07 +08:00
|
|
|
get_active_function_name(TSRMLS_C), PQerrorMessage(pgsql));
|
2001-12-17 08:24:59 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2002-03-29 19:24:54 +08:00
|
|
|
#ifdef HAVE_PQESCAPE
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto string pg_escape_string(string data)
|
|
|
|
Escape string for text/char type */
|
|
|
|
PHP_FUNCTION(pg_escape_string)
|
|
|
|
{
|
|
|
|
char *from = NULL, *to = NULL;
|
|
|
|
int len;
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
|
|
|
|
&from, &len) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-04-04 18:25:12 +08:00
|
|
|
to = (char *)emalloc(len*2+1);
|
2001-12-17 08:24:59 +08:00
|
|
|
len = (int)PQescapeString(to, from, strlen(from));
|
|
|
|
if (len < 0) {
|
2002-04-04 18:25:12 +08:00
|
|
|
efree(to);
|
2001-12-17 08:24:59 +08:00
|
|
|
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;
|
|
|
|
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) {
|
2002-04-04 18:25:12 +08:00
|
|
|
/* Don't need to free "to" here*/
|
2001-12-17 08:24:59 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2002-04-04 18:25:12 +08:00
|
|
|
RETURN_STRINGL(to, len, 1);
|
|
|
|
free(to);
|
2001-12-17 08:24:59 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
#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;
|
2002-03-29 19:24:54 +08:00
|
|
|
char *err = NULL;
|
2001-12-17 08:24:59 +08:00
|
|
|
|
|
|
|
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) {
|
2002-03-11 15:23:07 +08:00
|
|
|
RETURN_FALSE;
|
2001-12-17 08:24:59 +08:00
|
|
|
}
|
2002-03-29 19:24:54 +08:00
|
|
|
err = (char *)PQresultErrorMessage(pgsql_result);
|
|
|
|
RETURN_STRING(err,1);
|
2001-12-17 08:24:59 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ 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);
|
|
|
|
|
2001-12-10 12:02:08 +08:00
|
|
|
PQreset(pgsql);
|
|
|
|
if (PQstatus(pgsql) == CONNECTION_BAD) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
#define PHP_PG_ASYNC_IS_BUSY 1
|
|
|
|
#define PHP_PG_ASYNC_REQUEST_CANCEL 2
|
|
|
|
|
|
|
|
/* {{{ php_pgsql_do_async
|
|
|
|
*/
|
2002-02-06 15:16:10 +08:00
|
|
|
static void php_pgsql_do_async(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
|
2001-12-10 12:02:08 +08:00
|
|
|
{
|
2001-12-17 08:24:59 +08:00
|
|
|
zval *pgsql_link;
|
2001-12-10 12:02:08 +08:00
|
|
|
int id = -1;
|
|
|
|
PGconn *pgsql;
|
|
|
|
PGresult *pgsql_result;
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r",
|
|
|
|
&pgsql_link) == FAILURE) {
|
|
|
|
return;
|
2001-12-10 12:02:08 +08:00
|
|
|
}
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
|
2001-12-10 12:02:08 +08:00
|
|
|
|
2002-03-29 19:24:54 +08:00
|
|
|
if (PQ_SETNONBLOCKING(pgsql, 1)) {
|
2001-12-19 13:41:26 +08:00
|
|
|
php_error(E_NOTICE,"%s() cannot set connection to nonblocking mode",
|
|
|
|
get_active_function_name(TSRMLS_C));
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2001-12-10 12:02:08 +08:00
|
|
|
switch(entry_type) {
|
|
|
|
case PHP_PG_ASYNC_IS_BUSY:
|
|
|
|
PQconsumeInput(pgsql);
|
|
|
|
Z_LVAL_P(return_value) = PQisBusy(pgsql);
|
|
|
|
Z_TYPE_P(return_value) = IS_LONG;
|
|
|
|
break;
|
|
|
|
case PHP_PG_ASYNC_REQUEST_CANCEL:
|
|
|
|
Z_LVAL_P(return_value) = PQrequestCancel(pgsql);
|
|
|
|
Z_TYPE_P(return_value) = IS_LONG;
|
|
|
|
while ((pgsql_result = PQgetResult(pgsql))) {
|
|
|
|
PQclear(pgsql_result);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2002-03-26 16:43:57 +08:00
|
|
|
php_error(E_ERROR,"%s() PostgreSQL module error. Report this error",
|
|
|
|
get_active_function_name(TSRMLS_C));
|
2001-12-10 12:02:08 +08:00
|
|
|
break;
|
|
|
|
}
|
2002-03-29 19:24:54 +08:00
|
|
|
if (PQ_SETNONBLOCKING(pgsql, 0)) {
|
2001-12-19 13:41:26 +08:00
|
|
|
php_error(E_NOTICE,"%s() cannot set connection to blocking mode",
|
|
|
|
get_active_function_name(TSRMLS_C));
|
|
|
|
}
|
2001-12-10 12:02:08 +08:00
|
|
|
convert_to_boolean_ex(&return_value);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 19:53:38 +08:00
|
|
|
/* {{{ proto bool pg_cancel_query(resource connection)
|
2001-12-10 12:02:08 +08:00
|
|
|
Cancel request */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_cancel_query)
|
2001-12-10 12:02:08 +08:00
|
|
|
{
|
|
|
|
php_pgsql_do_async(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_ASYNC_REQUEST_CANCEL);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 19:53:38 +08:00
|
|
|
/* {{{ proto bool pg_connection_busy(resource connection)
|
2001-12-10 12:02:08 +08:00
|
|
|
Get connection is busy or not */
|
2001-12-17 08:24:59 +08:00
|
|
|
PHP_FUNCTION(pg_connection_busy)
|
2001-12-10 12:02:08 +08:00
|
|
|
{
|
|
|
|
php_pgsql_do_async(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_ASYNC_IS_BUSY);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
/* {{{ proto bool pg_send_query(resource connection, string qeury)
|
2001-12-10 12:02:08 +08:00
|
|
|
Send asynchronous query */
|
|
|
|
PHP_FUNCTION(pg_send_query)
|
|
|
|
{
|
2001-12-17 08:24:59 +08:00
|
|
|
zval *pgsql_link;
|
|
|
|
char *query;
|
|
|
|
int len;
|
2001-12-10 12:02:08 +08:00
|
|
|
int id = -1;
|
|
|
|
PGconn *pgsql;
|
|
|
|
PGresult *res;
|
|
|
|
int leftover = 0;
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs",
|
|
|
|
&pgsql_link, &query, &len) == FAILURE) {
|
|
|
|
return;
|
2001-12-10 12:02:08 +08:00
|
|
|
}
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
|
|
|
|
|
2002-03-29 19:24:54 +08:00
|
|
|
if (PQ_SETNONBLOCKING(pgsql, 1)) {
|
2001-12-17 08:24:59 +08:00
|
|
|
php_error(E_NOTICE,"%s() cannot set connection to nonblocking mode",
|
|
|
|
get_active_function_name(TSRMLS_C));
|
2001-12-10 12:02:08 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
while ((res = PQgetResult(pgsql))) {
|
|
|
|
PQclear(res);
|
|
|
|
leftover = 1;
|
|
|
|
}
|
|
|
|
if (leftover) {
|
2001-12-19 13:41:26 +08:00
|
|
|
php_error(E_NOTICE,"%s() - There are results on this connection. Call pg_get_result() until it returns FALSE",
|
2001-12-17 08:24:59 +08:00
|
|
|
get_active_function_name(TSRMLS_C));
|
2001-12-10 12:02:08 +08:00
|
|
|
}
|
2001-12-17 08:24:59 +08:00
|
|
|
if (!PQsendQuery(pgsql, query)) {
|
2001-12-10 12:02:08 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2002-03-29 19:24:54 +08:00
|
|
|
if (PQ_SETNONBLOCKING(pgsql, 0)) {
|
2001-12-17 08:24:59 +08:00
|
|
|
php_error(E_NOTICE,"%s() cannot set connection to blocking mode",
|
|
|
|
get_active_function_name(TSRMLS_C));
|
2001-12-10 12:02:08 +08:00
|
|
|
}
|
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
|
2001-12-17 19:48:07 +08:00
|
|
|
/* {{{ proto resource pg_get_result([resource connection])
|
2001-12-10 12:02:08 +08:00
|
|
|
Get asynchronous query result */
|
|
|
|
PHP_FUNCTION(pg_get_result)
|
|
|
|
{
|
2001-12-17 08:24:59 +08:00
|
|
|
zval *pgsql_link;
|
2001-12-10 12:02:08 +08:00
|
|
|
int id = -1;
|
|
|
|
PGconn *pgsql;
|
|
|
|
PGresult *pgsql_result;
|
|
|
|
pgsql_result_handle *pg_result;
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r",
|
|
|
|
&pgsql_link) == FAILURE) {
|
|
|
|
return;
|
2001-12-10 12:02:08 +08:00
|
|
|
}
|
|
|
|
|
2001-12-17 08:24:59 +08:00
|
|
|
ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
|
|
|
|
|
2001-12-10 12:02:08 +08:00
|
|
|
pgsql_result = PQgetResult(pgsql);
|
|
|
|
if (!pgsql_result) {
|
|
|
|
/* no result */
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
pg_result = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle));
|
|
|
|
pg_result->conn = pgsql;
|
|
|
|
pg_result->result = pgsql_result;
|
|
|
|
pg_result->row = -1;
|
|
|
|
ZEND_REGISTER_RESOURCE(return_value, pg_result, le_result);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-17 19:53:38 +08:00
|
|
|
/* {{{ proto int pg_result_status(resource result)
|
2001-12-17 08:24:59 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
1999-04-22 10:48:28 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
2001-09-09 21:29:31 +08:00
|
|
|
* vim600: sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: sw=4 ts=4
|
1999-04-22 10:48:28 +08:00
|
|
|
*/
|