php-src/ext/oci8/oci8.c

4932 lines
121 KiB
C
Raw Normal View History

1999-04-22 06:49:16 +08:00
/*
+----------------------------------------------------------------------+
1999-07-16 21:13:16 +08:00
| PHP version 4.0 |
1999-04-22 06:49:16 +08:00
+----------------------------------------------------------------------+
2001-02-26 14:11:02 +08:00
| Copyright (c) 1997-2001 The PHP Group |
1999-04-22 06:49:16 +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 |
| 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 06:49:16 +08:00
+----------------------------------------------------------------------+
| Authors: Stig S<EFBFBD>ther Bakken <ssb@fast.no> |
2000-10-29 17:14:55 +08:00
| Thies C. Arntzen <thies@thieso.net> |
| |
| Collection support by Andy Sautins <asautins@veripost.net> |
| Temporary LOB support by David Benson <dbenson@mancala.com> |
1999-04-22 06:49:16 +08:00
+----------------------------------------------------------------------+
*/
1999-04-22 06:49:16 +08:00
/* $Id$ */
/* TODO list:
*
* - php.ini flags
* especialliy important for things like oci_ping
1999-12-01 15:34:06 +08:00
* - Change return-value for OCIFetch*() (1-row read, 0-Normal end, false-error)
1999-04-22 06:49:16 +08:00
* - Error mode (print or shut up?)
* - OCIPasswordChange()
* - binding of arrays
* - Character sets for NCLOBS
* - split the module into an upper (php-callable) and lower (c-callable) layer!
* - remove all XXXs
* - clean up and documentation
* - make OCIInternalDebug accept a mask of flags....
* - add some flags to OCIFetchStatement (maxrows etc...)
* - have one ocifree() call.
* - make it possible to have persistent statements?
* - implement connection pooling in ZTS mode.
* - implement automatic commit *OR* rollback on script end
* - failover
* - change all the lob stuff to work without classes (optional)!
* - make sure that the callbacks terminate the strings with \0
* - cleanup the ociexecute semantics for refcursors
2000-02-27 22:31:04 +08:00
* - make $lob->savefile use O_BINARY
* - line 2728: ub4 length = -1; needs fixing
* - delay OCIInitialize() as far as we can.
* - add PHP Array <-> OCICollection conversion
* - add Collection iterator object for INDEX BY tables
1999-04-22 06:49:16 +08:00
*/
/* {{{ includes & stuff */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
1999-04-22 06:49:16 +08:00
#include "php.h"
2000-04-07 05:07:44 +08:00
#include "ext/standard/info.h"
/* #define HAVE_OCI8_TEMP_LOB 1 */
#define WITH_COLLECTIONS 1
1999-08-10 22:33:59 +08:00
#if HAVE_OCI8
1999-07-08 22:12:07 +08:00
#include "php_oci8.h"
1999-04-22 06:49:16 +08:00
1999-08-17 21:54:25 +08:00
/* True globals, no need for thread safety */
static int le_conn;
static int le_stmt;
static int le_desc;
#ifdef WITH_COLLECTIONS
static int le_coll;
#endif
static int le_server;
static int le_session;
static zend_class_entry *oci_lob_class_entry_ptr;
#ifdef WITH_COLLECTIONS
static zend_class_entry *oci_coll_class_entry_ptr;
#endif
#ifndef SQLT_BFILEE
#define SQLT_BFILEE 114
#endif
#ifndef SQLT_CFILEE
#define SQLT_CFILEE 115
#endif
1999-04-22 06:49:16 +08:00
#define SAFE_STRING(s) ((s)?(s):"")
2000-04-28 18:37:17 +08:00
#ifndef PHP_WIN32
1999-08-17 21:54:25 +08:00
#include "build-defs.h"
1999-04-22 06:49:16 +08:00
#endif
1999-08-17 21:54:25 +08:00
#include <fcntl.h>
2000-08-07 20:01:52 +08:00
#ifndef O_BINARY
#define O_BINARY 0
#endif
1999-04-22 06:49:16 +08:00
/* }}} */
/* {{{ thread safety stuff */
1999-08-17 21:54:25 +08:00
#ifdef ZTS
int oci_globals_id;
#else
PHP_OCI_API php_oci_globals oci_globals;
#endif
1999-04-22 06:49:16 +08:00
/* }}} */
/* {{{ dynamically loadable module stuff */
2000-05-23 17:33:51 +08:00
#ifdef COMPILE_DL_OCI8
ZEND_GET_MODULE(oci8)
1999-04-22 06:49:16 +08:00
#endif /* COMPILE_DL */
/* }}} */
/* {{{ startup/shutdown/info/internal function prototypes */
1999-08-17 21:54:25 +08:00
PHP_MINIT_FUNCTION(oci);
PHP_RINIT_FUNCTION(oci);
PHP_MSHUTDOWN_FUNCTION(oci);
PHP_RSHUTDOWN_FUNCTION(oci);
PHP_MINFO_FUNCTION(oci);
1999-04-22 06:49:16 +08:00
static ub4 oci_handle_error(oci_connection *connection, ub4 errcode);
1999-08-17 21:54:25 +08:00
static ub4 oci_error(OCIError *err_p, char *what, sword status);
static int oci_ping(oci_server *server);
static void oci_debug(const char *format, ...);
1999-04-22 06:49:16 +08:00
static void _oci_conn_list_dtor(oci_connection *connection);
static void _oci_stmt_list_dtor(zend_rsrc_list_entry *rsrc);
static void _oci_descriptor_list_dtor(zend_rsrc_list_entry *rsrc);
#ifdef WITH_COLLECTIONS
static void _oci_coll_list_dtor(zend_rsrc_list_entry *rsrc);
#endif
static void _oci_server_list_dtor(zend_rsrc_list_entry *rsrc);
static void _oci_session_list_dtor(zend_rsrc_list_entry *rsrc);
2000-10-22 05:17:31 +08:00
static void php_oci_free_conn_list(zend_rsrc_list_entry *rsrc);
1999-09-13 19:13:53 +08:00
static void _oci_column_hash_dtor(void *data);
static void _oci_define_hash_dtor(void *data);
static void _oci_bind_hash_dtor(void *data);
1999-04-22 06:49:16 +08:00
static oci_connection *oci_get_conn(zval **);
static oci_statement *oci_get_stmt(zval **);
static oci_descriptor *oci_get_desc(int);
#ifdef WITH_COLLECTIONS
/* Questionable name. Very close to oci_get_col */
static oci_collection *oci_get_coll(int);
#endif
static oci_out_column *oci_get_col(oci_statement *, int, zval **);
1999-04-22 06:49:16 +08:00
static int _oci_make_zval(zval *, oci_statement *, oci_out_column *, char *, int mode);
static oci_statement *oci_parse(oci_connection *, char *, int);
static int oci_execute(oci_statement *, char *, ub4 mode);
1999-08-17 21:54:25 +08:00
static int oci_fetch(oci_statement *, ub4, char *);
static int oci_loadlob(oci_connection *, oci_descriptor *, char **, ub4 *length);
static int oci_setprefetch(oci_statement *statement, int size);
1999-04-22 06:49:16 +08:00
1999-08-17 21:54:25 +08:00
static void oci_do_connect(INTERNAL_FUNCTION_PARAMETERS,int persistent,int exclusive);
1999-04-22 06:49:16 +08:00
static oci_server *_oci_open_server(char *dbname,int persistent);
1999-08-17 21:54:25 +08:00
static void _oci_close_server(oci_server *server);
static oci_session *_oci_open_session(oci_server* server,char *username,char *password,int persistent,int exclusive);
static void _oci_close_session(oci_session *session);
1999-04-22 06:49:16 +08:00
1999-08-17 21:54:25 +08:00
static sb4 oci_bind_in_callback(dvoid *, OCIBind *, ub4, ub4, dvoid **, ub4 *, ub1 *, dvoid **);
static sb4 oci_bind_out_callback(dvoid *, OCIBind *, ub4, ub4, dvoid **, ub4 **, ub1 *, dvoid **, ub2 **);
1999-04-22 06:49:16 +08:00
1999-05-12 22:28:01 +08:00
#if 0
1999-08-17 21:54:25 +08:00
static sb4 oci_failover_callback(dvoid *svchp,dvoid* envhp,dvoid *fo_ctx,ub4 fo_type, ub4 fo_event);
1999-05-12 22:28:01 +08:00
#endif
1999-04-22 06:49:16 +08:00
/* }}} */
/* {{{ extension function prototypes */
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocibindbyname);
PHP_FUNCTION(ocidefinebyname);
PHP_FUNCTION(ocicolumnisnull);
PHP_FUNCTION(ocicolumnname);
PHP_FUNCTION(ocicolumnsize);
PHP_FUNCTION(ocicolumnscale);
PHP_FUNCTION(ocicolumnprecision);
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocicolumntype);
PHP_FUNCTION(ocicolumntyperaw);
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ociexecute);
PHP_FUNCTION(ocifetch);
PHP_FUNCTION(ocicancel);
PHP_FUNCTION(ocifetchinto);
PHP_FUNCTION(ocifetchstatement);
PHP_FUNCTION(ocifreestatement);
PHP_FUNCTION(ociinternaldebug);
PHP_FUNCTION(ocilogoff);
PHP_FUNCTION(ocilogon);
PHP_FUNCTION(ocinlogon);
PHP_FUNCTION(ociplogon);
PHP_FUNCTION(ocierror);
PHP_FUNCTION(ocifreedesc);
PHP_FUNCTION(ocisavelob);
PHP_FUNCTION(ocisavelobfile);
PHP_FUNCTION(ociloadlob);
PHP_FUNCTION(ociwritelobtofile);
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocicommit);
PHP_FUNCTION(ocirollback);
PHP_FUNCTION(ocinewdescriptor);
PHP_FUNCTION(ocinumcols);
PHP_FUNCTION(ociparse);
PHP_FUNCTION(ocinewcursor);
PHP_FUNCTION(ociresult);
PHP_FUNCTION(ociserverversion);
PHP_FUNCTION(ocistatementtype);
PHP_FUNCTION(ocirowcount);
PHP_FUNCTION(ocisetprefetch);
#ifdef HAVE_OCI8_TEMP_LOB
PHP_FUNCTION(ociwritetemporarylob);
PHP_FUNCTION(ocicloselob);
#endif
#ifdef WITH_COLLECTIONS
PHP_FUNCTION(ocinewcollection);
PHP_FUNCTION(ocifreecollection);
PHP_FUNCTION(ocicollappend);
PHP_FUNCTION(ocicollgetelem);
PHP_FUNCTION(ocicollassignelem);
PHP_FUNCTION(ocicollassign);
PHP_FUNCTION(ocicollsize);
PHP_FUNCTION(ocicollmax);
PHP_FUNCTION(ocicolltrim);
#endif
1999-04-22 06:49:16 +08:00
#define OCI_GET_STMT(statement,value) \
statement = oci_get_stmt(value); \
if (statement == NULL) { \
RETURN_FALSE; \
}
#define OCI_GET_CONN(connection,value) \
connection = oci_get_conn(value); \
if (connection == NULL) { \
RETURN_FALSE; \
}
#define OCI_GET_DESC(descriptor,index) \
descriptor = oci_get_desc(index); \
if (descriptor == NULL) { \
RETURN_FALSE; \
}
#ifdef WITH_COLLECTIONS
#define OCI_GET_COLL(collection,index) \
collection = oci_get_coll(index); \
if (collection == NULL) { \
RETURN_FALSE; \
}
#endif
1999-04-22 06:49:16 +08:00
/* }}} */
/* {{{ extension definition structures */
#define OCI_ASSOC 1<<0
#define OCI_NUM 1<<1
#define OCI_BOTH (OCI_ASSOC|OCI_NUM)
#define OCI_RETURN_NULLS 1<<2
#define OCI_RETURN_LOBS 1<<3
static unsigned char a3_arg_force_ref[] = { 3, BYREF_NONE, BYREF_NONE, BYREF_FORCE };
static unsigned char a2_arg_force_ref[] = { 2, BYREF_NONE, BYREF_FORCE };
1999-08-10 22:33:59 +08:00
static zend_function_entry php_oci_functions[] = {
PHP_FE(ocidefinebyname, a3_arg_force_ref)
PHP_FE(ocibindbyname, a3_arg_force_ref)
PHP_FE(ocicolumnisnull, NULL)
PHP_FE(ocicolumnname, NULL)
PHP_FE(ocicolumnsize, NULL)
PHP_FE(ocicolumnscale, NULL)
PHP_FE(ocicolumnprecision, NULL)
1999-08-10 22:33:59 +08:00
PHP_FE(ocicolumntype, NULL)
PHP_FE(ocicolumntyperaw, NULL)
1999-08-10 22:33:59 +08:00
PHP_FE(ociexecute, NULL)
PHP_FE(ocicancel, NULL)
PHP_FE(ocifetch, NULL)
PHP_FE(ocifetchinto, a2_arg_force_ref)
PHP_FE(ocifetchstatement,a2_arg_force_ref)
PHP_FE(ocifreestatement, NULL)
PHP_FE(ociinternaldebug, NULL)
PHP_FE(ocinumcols, NULL)
PHP_FE(ociparse, NULL)
PHP_FE(ocinewcursor, NULL)
PHP_FE(ociresult, NULL)
PHP_FE(ociserverversion, NULL)
PHP_FE(ocistatementtype, NULL)
PHP_FE(ocirowcount, NULL)
PHP_FE(ocilogoff, NULL)
PHP_FE(ocilogon, NULL)
PHP_FE(ocinlogon, NULL)
PHP_FE(ociplogon, NULL)
PHP_FE(ocierror, NULL)
PHP_FE(ocifreedesc, NULL)
PHP_FE(ocisavelob, NULL)
PHP_FE(ocisavelobfile, NULL)
PHP_FE(ociloadlob, NULL)
PHP_FE(ociwritelobtofile,NULL)
1999-08-10 22:33:59 +08:00
PHP_FE(ocicommit, NULL)
PHP_FE(ocirollback, NULL)
PHP_FE(ocinewdescriptor, NULL)
PHP_FE(ocisetprefetch, NULL)
#ifdef WITH_COLLECTIONS
PHP_FE(ocifreecollection,NULL)
PHP_FE(ocicollappend, NULL)
PHP_FE(ocicollgetelem, NULL)
PHP_FE(ocicollassignelem,NULL)
PHP_FE(ocicollassign, NULL)
PHP_FE(ocicollsize, NULL)
PHP_FE(ocicollmax, NULL)
PHP_FE(ocicolltrim, NULL)
PHP_FE(ocinewcollection, NULL)
#endif
1999-11-13 01:05:57 +08:00
PHP_FALIAS(ocifreecursor,ocifreestatement,NULL)
1999-08-10 22:33:59 +08:00
{NULL,NULL,NULL}
1999-04-22 06:49:16 +08:00
};
static zend_function_entry php_oci_lob_class_functions[] = {
PHP_FALIAS(load, ociloadlob, NULL)
PHP_FALIAS(writetofile, ociwritelobtofile,NULL)
#ifdef HAVE_OCI8_TEMP_LOB
PHP_FALIAS(writetemporary, ociwritetemporarylob,NULL)
PHP_FALIAS(close, ocicloselob, NULL)
#endif
PHP_FALIAS(save, ocisavelob, NULL)
PHP_FALIAS(savefile, ocisavelobfile, NULL)
PHP_FALIAS(free, ocifreedesc, NULL)
{NULL,NULL,NULL}
};
#ifdef WITH_COLLECTIONS
static zend_function_entry php_oci_coll_class_functions[] = {
PHP_FALIAS(append, ocicollappend, NULL)
PHP_FALIAS(getelem, ocicollgetelem, NULL)
PHP_FALIAS(assignelem, ocicollassignelem, NULL)
PHP_FALIAS(assign, ocicollassign, NULL)
PHP_FALIAS(size, ocicollsize, NULL)
PHP_FALIAS(max, ocicollmax, NULL)
PHP_FALIAS(trim, ocicolltrim, NULL)
PHP_FALIAS(free, ocifreecollection, NULL)
{NULL,NULL,NULL}
};
#endif
zend_module_entry oci8_module_entry = {
"oci8", /* extension name */
1999-08-17 21:54:25 +08:00
php_oci_functions, /* extension function list */
PHP_MINIT(oci), /* extension-wide startup function */
PHP_MSHUTDOWN(oci), /* extension-wide shutdown function */
PHP_RINIT(oci), /* per-request startup function */
PHP_RSHUTDOWN(oci), /* per-request shutdown function */
PHP_MINFO(oci), /* information function */
1999-04-22 06:49:16 +08:00
STANDARD_MODULE_PROPERTIES
};
/* }}} */
/* {{{ debug malloc/realloc/free */
#define OCI_USE_EMALLOC 0 /* set this to 1 if you want to use the php memory manager! */
1999-08-17 21:54:25 +08:00
#if OCI_USE_EMALLOC
CONST dvoid *ocimalloc(dvoid *ctx, size_t size)
{
dvoid *ret;
ret = (dvoid *)malloc(size);
1999-08-17 21:54:25 +08:00
oci_debug("ocimalloc(%d) = %08x", size,ret);
return ret;
}
CONST dvoid *ocirealloc(dvoid *ctx, dvoid *ptr, size_t size)
{
dvoid *ret;
1999-08-17 21:54:25 +08:00
oci_debug("ocirealloc(%08x, %d)", ptr, size);
ret = (dvoid *)realloc(ptr, size);
return ptr;
}
CONST void ocifree(dvoid *ctx, dvoid *ptr)
{
1999-08-17 21:54:25 +08:00
oci_debug("ocifree(%08x)", ptr);
free(ptr);
}
#endif
1999-04-22 06:49:16 +08:00
/* }}} */
/* {{{ startup, shutdown and info functions */
static void php_oci_init_globals(OCILS_D)
1999-08-17 21:54:25 +08:00
{
2000-07-15 22:54:04 +08:00
OCI(shutdown) = 0;
1999-04-22 06:49:16 +08:00
1999-08-17 21:54:25 +08:00
OCI(user) = malloc(sizeof(HashTable));
zend_hash_init(OCI(user), 13, NULL, NULL, 1);
1999-04-22 06:49:16 +08:00
1999-08-17 21:54:25 +08:00
OCI(server) = malloc(sizeof(HashTable));
zend_hash_init(OCI(server), 13, NULL, NULL, 1);
1999-08-19 00:48:59 +08:00
OCIEnvInit(&OCI(pEnv), OCI_DEFAULT, 0, NULL);
OCIHandleAlloc(OCI(pEnv),
(dvoid **)&OCI(pError),
OCI_HTYPE_ERROR,
0,
NULL);
1999-08-17 21:54:25 +08:00
}
1999-04-22 06:49:16 +08:00
1999-08-17 21:54:25 +08:00
PHP_MINIT_FUNCTION(oci)
{
zend_class_entry oci_lob_class_entry;
#ifdef WITH_COLLECTIONS
zend_class_entry oci_coll_class_entry;
#endif
#ifdef WITH_COLLECTIONS
#define PHP_OCI_INIT_MODE OCI_DEFAULT | OCI_OBJECT
1999-08-17 21:54:25 +08:00
#else
#define PHP_OCI_INIT_MODE OCI_DEFAULT
#endif
1999-04-22 06:49:16 +08:00
#if OCI_USE_EMALLOC
OCIInitialize(PHP_OCI_INIT_MODE, NULL, ocimalloc, ocirealloc, ocifree);
#else
OCIInitialize(PHP_OCI_INIT_MODE, NULL, NULL, NULL, NULL);
#endif
1999-08-19 00:48:59 +08:00
#ifdef ZTS
oci_globals_id = ts_allocate_id(sizeof(php_oci_globals), (ts_allocate_ctor) php_oci_init_globals, NULL);
#else
php_oci_init_globals(OCILS_C);
1999-08-17 21:54:25 +08:00
#endif
le_stmt = zend_register_list_destructors_ex(_oci_stmt_list_dtor, NULL, "oci8 statement", module_number);
le_conn = zend_register_list_destructors_ex(php_oci_free_conn_list, NULL, "oci8 connection", module_number);
le_desc = zend_register_list_destructors_ex(_oci_descriptor_list_dtor, NULL, "oci8 descriptor", module_number);
#ifdef WITH_COLLECTIONS
le_coll = zend_register_list_destructors_ex(_oci_coll_list_dtor, NULL, "oci8 collection", module_number);
#endif
le_server = zend_register_list_destructors_ex(_oci_server_list_dtor, NULL, "oci8 server", module_number);
le_session = zend_register_list_destructors_ex(_oci_session_list_dtor, NULL, "oci8 session", module_number);
1999-08-17 21:54:25 +08:00
INIT_CLASS_ENTRY(oci_lob_class_entry, "OCI-Lob", php_oci_lob_class_functions);
#ifdef WITH_COLLECTIONS
INIT_CLASS_ENTRY(oci_coll_class_entry, "OCI-Collection", php_oci_coll_class_functions);
#endif
oci_lob_class_entry_ptr = zend_register_internal_class(&oci_lob_class_entry);
#ifdef WITH_COLLECTIONS
oci_coll_class_entry_ptr = zend_register_internal_class(&oci_coll_class_entry);
#endif
1999-04-22 06:49:16 +08:00
2000-10-29 17:14:55 +08:00
/* thies@thieso.net 990203 i do not think that we will need all of them - just in here for completeness for now! */
1999-04-22 06:49:16 +08:00
REGISTER_LONG_CONSTANT("OCI_DEFAULT",OCI_DEFAULT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("OCI_DESCRIBE_ONLY",OCI_DESCRIBE_ONLY, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("OCI_COMMIT_ON_SUCCESS",OCI_COMMIT_ON_SUCCESS, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("OCI_EXACT_FETCH",OCI_EXACT_FETCH, CONST_CS | CONST_PERSISTENT);
/* for OCIBindByName (real "oci" names + short "php" names*/
REGISTER_LONG_CONSTANT("SQLT_BFILEE",SQLT_BFILEE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLT_CFILEE",SQLT_CFILEE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLT_CLOB",SQLT_CLOB, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLT_BLOB",SQLT_BLOB, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLT_RDD",SQLT_RDD, CONST_CS | CONST_PERSISTENT);
#ifdef WITH_COLLECTIONS
REGISTER_LONG_CONSTANT("OCI_B_SQLT_NTY",SQLT_NTY, CONST_CS | CONST_PERSISTENT);
REGISTER_STRING_CONSTANT("OCI_SYSDATE","SYSDATE",CONST_PERSISTENT);
#endif
1999-04-22 06:49:16 +08:00
REGISTER_LONG_CONSTANT("OCI_B_BFILE",SQLT_BFILEE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("OCI_B_CFILEE",SQLT_CFILEE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("OCI_B_CLOB",SQLT_CLOB, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("OCI_B_BLOB",SQLT_BLOB, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("OCI_B_ROWID",SQLT_RDD, CONST_CS | CONST_PERSISTENT);
1999-05-12 22:28:01 +08:00
REGISTER_LONG_CONSTANT("OCI_B_CURSOR",SQLT_RSET, CONST_CS | CONST_PERSISTENT);
1999-06-09 18:40:08 +08:00
REGISTER_LONG_CONSTANT("OCI_B_BIN",SQLT_BIN, CONST_CS | CONST_PERSISTENT);
1999-04-22 06:49:16 +08:00
/* for OCIFetchInto & OCIResult */
REGISTER_LONG_CONSTANT("OCI_ASSOC",OCI_ASSOC, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("OCI_NUM",OCI_NUM, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("OCI_BOTH",OCI_BOTH, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("OCI_RETURN_NULLS",OCI_RETURN_NULLS, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("OCI_RETURN_LOBS",OCI_RETURN_LOBS, CONST_CS | CONST_PERSISTENT);
/* for OCINewDescriptor (real "oci" names + short "php" names*/
REGISTER_LONG_CONSTANT("OCI_DTYPE_FILE",OCI_DTYPE_FILE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("OCI_DTYPE_LOB",OCI_DTYPE_LOB, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("OCI_DTYPE_ROWID",OCI_DTYPE_ROWID, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("OCI_D_FILE",OCI_DTYPE_FILE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("OCI_D_LOB",OCI_DTYPE_LOB, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("OCI_D_ROWID",OCI_DTYPE_ROWID, CONST_CS | CONST_PERSISTENT);
return SUCCESS;
}
/* ----------------------------------------------------------------- */
1999-08-17 21:54:25 +08:00
PHP_RINIT_FUNCTION(oci)
1999-04-22 06:49:16 +08:00
{
1999-08-17 21:54:25 +08:00
OCILS_FETCH();
1999-04-22 06:49:16 +08:00
/* XXX NYI
1999-08-17 21:54:25 +08:00
OCI(num_links) =
OCI(num_persistent);
*/
1999-04-22 06:49:16 +08:00
1999-08-17 21:54:25 +08:00
OCI(debug_mode) = 0; /* start "fresh" */
1999-04-22 06:49:16 +08:00
oci_debug("php_rinit_oci");
1999-04-22 06:49:16 +08:00
return SUCCESS;
}
static int _session_pcleanup(oci_session *session)
{
_oci_close_session(session);
1999-05-12 22:28:01 +08:00
return 1;
}
1999-08-17 21:54:25 +08:00
static int _server_pcleanup(oci_server *server)
{
1999-08-17 21:54:25 +08:00
_oci_close_server(server);
1999-05-12 22:28:01 +08:00
return 1;
}
1999-04-22 06:49:16 +08:00
1999-08-17 21:54:25 +08:00
PHP_MSHUTDOWN_FUNCTION(oci)
1999-04-22 06:49:16 +08:00
{
1999-08-17 21:54:25 +08:00
OCILS_FETCH();
OCI(shutdown) = 1;
oci_debug("START php_mshutdown_oci");
zend_hash_apply(OCI(user),(int (*)(void *))_session_pcleanup);
1999-08-17 21:54:25 +08:00
zend_hash_apply(OCI(server),(int (*)(void *))_server_pcleanup);
1999-08-17 21:54:25 +08:00
zend_hash_destroy(OCI(user));
zend_hash_destroy(OCI(server));
1999-08-17 21:54:25 +08:00
free(OCI(user));
free(OCI(server));
1999-08-17 21:54:25 +08:00
OCIHandleFree((dvoid *)OCI(pEnv), OCI_HTYPE_ENV);
oci_debug("END php_mshutdown_oci");
1999-06-09 18:40:08 +08:00
return SUCCESS;
1999-06-09 18:40:08 +08:00
}
1999-08-17 21:54:25 +08:00
PHP_RSHUTDOWN_FUNCTION(oci)
1999-04-22 06:49:16 +08:00
{
oci_debug("START php_rshutdown_oci");
#if 0
/* XXX free all statements, rollback all outstanding transactions */
zend_hash_apply(OCI(user),(int (*)(void *))_session_cleanup);
1999-08-17 21:54:25 +08:00
zend_hash_apply(OCI(server),(int (*)(void *))_server_cleanup);
#endif
oci_debug("END php_rshutdown_oci");
1999-04-22 06:49:16 +08:00
return SUCCESS;
}
1999-08-17 21:54:25 +08:00
PHP_MINFO_FUNCTION(oci)
1999-04-22 06:49:16 +08:00
{
2000-04-07 05:07:44 +08:00
php_info_print_table_start();
php_info_print_table_row(2, "OCI8 Support", "enabled");
php_info_print_table_row(2, "Revision", "$Revision$");
2000-04-28 18:37:17 +08:00
#ifndef PHP_WIN32
2000-05-08 17:06:48 +08:00
php_info_print_table_row(2, "Oracle Version", PHP_OCI8_VERSION );
php_info_print_table_row(2, "Compile-time ORACLE_HOME", PHP_OCI8_DIR );
php_info_print_table_row(2, "Libraries Used", PHP_OCI8_SHARED_LIBADD );
1999-04-22 06:49:16 +08:00
#endif
2000-04-07 05:07:44 +08:00
php_info_print_table_end();
1999-04-22 06:49:16 +08:00
}
/* }}} */
/* {{{ _oci_define_hash_dtor() */
1999-04-22 06:49:16 +08:00
static void
_oci_define_hash_dtor(void *data)
1999-04-22 06:49:16 +08:00
{
1999-09-13 19:13:53 +08:00
oci_define *define = (oci_define *) data;
oci_debug("_oci_define_hash_dtor: %s",define->name);
zval_ptr_dtor(&define->zval);
1999-04-22 06:49:16 +08:00
if (define->name) {
efree(define->name);
define->name = 0;
}
}
/* }}} */
/* {{{ _oci_bind_hash_dtor() */
1999-04-22 06:49:16 +08:00
static void
_oci_bind_hash_dtor(void *data)
{
oci_bind *bind = (oci_bind *) data;
oci_debug("_oci_bind_hash_dtor:");
zval_ptr_dtor(&(bind->zval));
}
/* }}} */
/* {{{ _oci_bind_pre_exec() */
static int
_oci_bind_pre_exec(void *data)
{
oci_bind *bind = (oci_bind *) data;
/* reset all bind stuff to a normal state..-. */
bind->indicator = 0;
return 0;
}
/* }}} */
/* {{{ _oci_bind_post_exec() */
static int
_oci_bind_post_exec(void *data)
{
oci_bind *bind = (oci_bind *) data;
if (bind->indicator == -1) { /* NULL */
zval *val = bind->zval;
zval_dtor(val);
ZVAL_NULL(val);
} else if (bind->zval->type == IS_STRING) {
2001-07-09 14:48:15 +08:00
bind->zval->value.str.val = erealloc(bind->zval->value.str.val, bind->zval->value.str.len+1);
bind->zval->value.str.val[ bind->zval->value.str.len ] = '\0';
}
return 0;
}
/* }}} */
/* {{{ _oci_column_hash_dtor() */
static void
_oci_column_hash_dtor(void *data)
1999-09-13 19:13:53 +08:00
{
oci_out_column *column = (oci_out_column *) data;
1999-04-22 06:49:16 +08:00
oci_debug("START _oci_column_hash_dtor: %s",column->name);
1999-04-22 06:49:16 +08:00
if (column->stmtid) {
zend_list_delete(column->stmtid);
}
if (column->is_descr) {
zend_list_delete(column->descid);
} else {
if (column->data) {
efree(column->data);
1999-04-22 06:49:16 +08:00
}
}
oci_debug("END _oci_column_hash_dtor: %s",column->name);
1999-09-29 22:20:27 +08:00
1999-04-22 06:49:16 +08:00
if (column->name) {
efree(column->name);
}
}
/* }}} */
/* {{{ _oci_stmt_list_dtor() */
1999-04-22 06:49:16 +08:00
static void
_oci_stmt_list_dtor(zend_rsrc_list_entry *rsrc)
1999-04-22 06:49:16 +08:00
{
oci_statement *statement = (oci_statement *)rsrc->ptr;
oci_debug("START _oci_stmt_list_dtor: id=%d last_query=\"%s\"",statement->id,SAFE_STRING(statement->last_query));
1999-04-22 06:49:16 +08:00
if (statement->pStmt) {
OCIHandleFree(statement->pStmt, OCI_HTYPE_STMT);
statement->pStmt = 0;
}
if (statement->pError) {
OCIHandleFree(statement->pError, OCI_HTYPE_ERROR);
statement->pError = 0;
}
if (statement->last_query) {
efree(statement->last_query);
}
if (statement->columns) {
zend_hash_destroy(statement->columns);
1999-04-22 06:49:16 +08:00
efree(statement->columns);
}
if (statement->binds) {
zend_hash_destroy(statement->binds);
1999-04-22 06:49:16 +08:00
efree(statement->binds);
}
if (statement->defines) {
zend_hash_destroy(statement->defines);
1999-04-22 06:49:16 +08:00
efree(statement->defines);
}
zend_list_delete(statement->conn->id);
oci_debug("END _oci_stmt_list_dtor: id=%d",statement->id);
1999-04-22 06:49:16 +08:00
efree(statement);
}
/* }}} */
/* {{{ _oci_conn_list_dtor() */
1999-04-22 06:49:16 +08:00
static void
_oci_conn_list_dtor(oci_connection *connection)
1999-04-22 06:49:16 +08:00
{
/*
as the connection is "only" a in memory service context we do not disconnect from oracle.
*/
oci_debug("START _oci_conn_list_dtor: id=%d",connection->id);
1999-04-22 06:49:16 +08:00
if (connection->pServiceContext) {
connection->error =
OCITransRollback(connection->pServiceContext,
connection->pError,
(ub4)0);
if (connection->error) {
oci_error(connection->pError, "failed to rollback outstanding transactions!", connection->error);
}
1999-04-22 06:49:16 +08:00
OCIHandleFree((dvoid *) connection->pServiceContext, (ub4) OCI_HTYPE_SVCCTX);
}
if (connection->session && connection->session->exclusive) {
/* exclusive connection created via OCINLogon() close their
associated session when destructed */
zend_list_delete(connection->session->num);
}
1999-04-22 06:49:16 +08:00
if (connection->pError) {
OCIHandleFree((dvoid *) connection->pError, (ub4) OCI_HTYPE_ERROR);
}
oci_debug("END _oci_conn_list_dtor: id=%d",connection->id);
1999-09-29 22:20:27 +08:00
1999-04-22 06:49:16 +08:00
efree(connection);
}
/* }}} */
/* {{{ php_oci_free_conn_list
*/
static void php_oci_free_conn_list(zend_rsrc_list_entry *rsrc)
{
oci_connection *conn = (oci_connection *)rsrc->ptr;
_oci_conn_list_dtor(conn);
}
/* }}} */
#ifdef WITH_COLLECTIONS
/* {{{ _oci_coll_list_dtor()
*/
static void
_oci_coll_list_dtor(zend_rsrc_list_entry *rsrc)
{
oci_collection *coll = (oci_collection *)rsrc->ptr;
oci_debug("START _oci_coll_list_dtor: %d",coll->id);
zend_list_delete(coll->conn->id);
/* Note sure if we need to free the object. Have an
oracle TAR out on this one.
OCIDescriptorFree(descr->ocidescr, descr->type); */
oci_debug("END _oci_coll_list_dtor: %d",coll->id);
efree(coll);
}
/* }}} */
#endif
/* {{{ _oci_descriptor_list_dtor()
*/
static void
_oci_descriptor_list_dtor(zend_rsrc_list_entry *rsrc)
{
oci_descriptor *descr = (oci_descriptor *)rsrc->ptr;
oci_debug("START _oci_descriptor_list_dtor: %d",descr->id);
zend_list_delete(descr->conn->id);
OCIDescriptorFree(descr->ocidescr, descr->type);
oci_debug("END _oci_descriptor_list_dtor: %d",descr->id);
efree(descr);
}
/* }}} */
/* {{{ _oci_server_list_dtor()
*/
static void
_oci_server_list_dtor(zend_rsrc_list_entry *rsrc)
{
oci_server *server = (oci_server *)rsrc->ptr;
if (server->persistent)
return;
_oci_close_server(server);
}
/* }}} */
/* {{{ _oci_session_list_dtor()
*/
static void
_oci_session_list_dtor(zend_rsrc_list_entry *rsrc)
{
oci_session *session = (oci_session *)rsrc->ptr;
if (session->persistent)
return;
_oci_close_session(session);
}
1999-04-22 06:49:16 +08:00
/* }}} */
/* {{{ oci_handle_error
*/
static ub4
oci_handle_error(oci_connection *connection, ub4 errcode)
{
2001-02-17 23:54:40 +08:00
switch (errcode) {
case 22: /* ORA-00022 Invalid session id */
case 1012: /* ORA-01012: */
case 3113: /* ORA-03113: end-of-file on communication channel */
2001-05-04 22:53:45 +08:00
connection->is_open = 0;
connection->session->is_open = 0;
connection->session->server->is_open = 0;
2001-02-17 23:54:40 +08:00
return 1; /* fatal error */
}
return 0; /* no fatal error */
}
/* }}} */
1999-08-17 21:54:25 +08:00
/* {{{ oci_error() */
1999-04-22 06:49:16 +08:00
static ub4
1999-08-17 21:54:25 +08:00
oci_error(OCIError *err_p, char *what, sword status)
1999-04-22 06:49:16 +08:00
{
text errbuf[512];
1999-06-09 18:40:08 +08:00
sb4 errcode = 0;
1999-04-22 06:49:16 +08:00
switch (status) {
case OCI_SUCCESS:
break;
case OCI_SUCCESS_WITH_INFO:
php_error(E_WARNING, "%s: OCI_SUCCESS_WITH_INFO", what);
break;
case OCI_NEED_DATA:
php_error(E_WARNING, "%s: OCI_NEED_DATA", what);
break;
case OCI_NO_DATA:
php_error(E_WARNING, "%s: OCI_NO_DATA", what);
break;
case OCI_ERROR:
OCIErrorGet(err_p, (ub4)1, NULL, &errcode, errbuf,
(ub4)sizeof(errbuf), (ub4)OCI_HTYPE_ERROR);
php_error(E_WARNING, "%s: %s", what, errbuf);
break;
case OCI_INVALID_HANDLE:
php_error(E_WARNING, "%s: OCI_INVALID_HANDLE", what);
break;
case OCI_STILL_EXECUTING:
php_error(E_WARNING, "%s: OCI_STILL_EXECUTING", what);
break;
case OCI_CONTINUE:
php_error(E_WARNING, "%s: OCI_CONTINUE", what);
break;
default:
break;
1999-04-22 06:49:16 +08:00
}
return errcode;
}
/* }}} */
/* {{{ oci_ping() */
1999-04-22 06:49:16 +08:00
static int oci_ping(oci_server *server)
1999-04-22 06:49:16 +08:00
{
char version[256];
OCILS_FETCH();
OCI(error) =
OCIServerVersion(server->pServer,
OCI(pError),
(text*)version,
sizeof(version),
OCI_HTYPE_SERVER);
if (OCI(error) == OCI_SUCCESS) {
return 1;
}
oci_error(OCI(pError), "oci_ping", OCI(error));
return 0;
1999-04-22 06:49:16 +08:00
}
/* }}} */
/************************* INTERNAL FUNCTIONS *************************/
1999-08-17 21:54:25 +08:00
/* {{{ oci_debug() */
1999-04-22 06:49:16 +08:00
2000-02-19 21:11:02 +08:00
static void oci_debug(const char *format, ...)
1999-04-22 06:49:16 +08:00
{
1999-08-17 21:54:25 +08:00
OCILS_FETCH();
1999-04-22 06:49:16 +08:00
1999-08-17 21:54:25 +08:00
if (OCI(debug_mode)) {
1999-04-22 06:49:16 +08:00
char buffer[1024];
va_list args;
va_start(args, format);
vsnprintf(buffer, sizeof(buffer)-1, format, args);
va_end(args);
buffer[sizeof(buffer)-1] = '\0';
php_printf("OCIDebug: %s<br>\n", buffer);
1999-04-22 06:49:16 +08:00
}
}
/* }}} */
1999-08-17 21:54:25 +08:00
/* {{{ oci_get_conn() */
1999-04-22 06:49:16 +08:00
static oci_connection *oci_get_conn(zval **conn)
1999-04-22 06:49:16 +08:00
{
1999-08-17 21:54:25 +08:00
oci_connection *connection;
1999-04-22 06:49:16 +08:00
connection = (oci_connection *) zend_fetch_resource(conn, -1, "OCI8-Connection", NULL, 1, le_conn);
2001-05-04 22:53:45 +08:00
if (connection && connection->is_open) {
return connection;
} else {
return (oci_connection *) NULL;
}
1999-04-22 06:49:16 +08:00
}
/* }}} */
1999-08-17 21:54:25 +08:00
/* {{{ oci_get_stmt() */
1999-04-22 06:49:16 +08:00
static oci_statement *oci_get_stmt(zval **stmt)
1999-04-22 06:49:16 +08:00
{
1999-08-17 21:54:25 +08:00
oci_statement *statement;
1999-04-22 06:49:16 +08:00
statement = (oci_statement *) zend_fetch_resource(stmt, -1, "OCI8-Statement", NULL, 1, le_stmt);
2001-05-04 22:53:45 +08:00
if (statement && statement->conn->is_open) {
return statement;
} else {
return (oci_statement *) NULL;
}
1999-04-22 06:49:16 +08:00
}
/* }}} */
/* {{{ oci_get_desc() */
static oci_descriptor *oci_get_desc(int ind)
{
oci_descriptor *descriptor;
int actual_resource_type;
descriptor = (oci_descriptor *) zend_list_find(ind, &actual_resource_type);
if (descriptor && (actual_resource_type == le_desc)) {
return descriptor;
} else {
return (oci_descriptor *) NULL;
}
}
1999-04-22 06:49:16 +08:00
/* }}} */
1999-08-17 21:54:25 +08:00
/* {{{ oci_get_col() */
1999-04-22 06:49:16 +08:00
1999-08-17 21:54:25 +08:00
static oci_out_column *
oci_get_col(oci_statement *statement, int col, zval **value)
1999-04-22 06:49:16 +08:00
{
1999-08-17 21:54:25 +08:00
oci_out_column *outcol = NULL;
1999-04-22 06:49:16 +08:00
int i;
1999-06-09 18:40:08 +08:00
if (statement->columns == 0) { /* we release the columns at the end of a fetch */
return NULL;
}
if (value) {
if ((*value)->type == IS_STRING) {
1999-04-22 06:49:16 +08:00
for (i = 0; i < statement->ncolumns; i++) {
outcol = oci_get_col(statement, i + 1, 0);
1999-04-22 06:49:16 +08:00
if (outcol == NULL) {
continue;
} else if (((int) outcol->name_len == (*value)->value.str.len)
&& (! strncmp(outcol->name,(*value)->value.str.val,(*value)->value.str.len))) {
1999-04-22 06:49:16 +08:00
return outcol;
}
}
} else {
convert_to_long_ex(value);
return oci_get_col(statement,(*value)->value.lval,0);
1999-04-22 06:49:16 +08:00
}
} else if (col != -1) {
if (zend_hash_index_find(statement->columns, col, (void **)&outcol) == FAILURE) {
php_error(E_WARNING, "Invalid column %d", col);
1999-04-22 06:49:16 +08:00
return NULL;
}
return outcol;
}
return NULL;
}
/* }}} */
/* {{{ oci_new_desc() */
static oci_descriptor*
oci_new_desc(int type,oci_connection *connection)
{
oci_descriptor *descr;
OCILS_FETCH();
descr = emalloc(sizeof(oci_descriptor));
descr->type = type;
switch (descr->type) {
case OCI_DTYPE_FILE:
case OCI_DTYPE_LOB:
case OCI_DTYPE_ROWID:
break;
default:
php_error(E_WARNING, "Unknown descriptor type %d.",descr->type);
return 0;
}
OCI(error) =
OCIDescriptorAlloc(OCI(pEnv),
(dvoid*)&(descr->ocidescr),
descr->type,
(size_t) 0,
(dvoid **) 0);
if (OCI(error)) {
ub4 error;
error = oci_error(OCI(pError),"OCIDescriptorAlloc %d",OCI(error));
oci_handle_error(connection, error);
return 0;
}
descr->id = zend_list_insert(descr,le_desc);
descr->conn = connection;
zend_list_addref(connection->id);
oci_debug("oci_new_desc %d",descr->id);
return descr;
}
#ifdef WITH_COLLECTIONS
/* {{{ _oci_get_ocicoll() */
static int
_oci_get_ocicoll(zval *id,oci_collection **collection)
{
zval **coll;
if (zend_hash_find(id->value.obj.properties, "collection", sizeof("collection"), (void **)&coll) == FAILURE) {
php_error(E_WARNING, "cannot find collection");
return 0;
}
if ((*collection = oci_get_coll((*coll)->value.lval)) == NULL) {
php_error(E_WARNING, "collection not found");
return 0;
}
return (*coll)->value.lval;
}
/* }}} */
#endif
/* }}} */
/* {{{ _oci_get_ocidesc() */
static int
_oci_get_ocidesc(zval *id,oci_descriptor **descriptor)
{
zval **desc;
if (zend_hash_find(id->value.obj.properties, "descriptor", sizeof("descriptor"), (void **)&desc) == FAILURE) {
php_error(E_WARNING, "cannot find descriptor");
return 0;
}
if ((*descriptor = oci_get_desc((*desc)->value.lval)) == NULL) {
php_error(E_WARNING, "descriptor not found");
return 0;
}
return (*desc)->value.lval;
}
/* }}} */
/* {{{ _oci_make_zval() */
1999-04-22 06:49:16 +08:00
static int
_oci_make_zval(zval *value,oci_statement *statement,oci_out_column *column, char *func, int mode)
1999-04-22 06:49:16 +08:00
{
1999-08-17 21:54:25 +08:00
oci_descriptor *descr;
1999-04-22 06:49:16 +08:00
ub4 loblen;
int size;
1999-04-22 06:49:16 +08:00
char *buffer;
1999-06-09 18:40:08 +08:00
if (column->indicator || column->retcode)
1999-07-08 22:12:07 +08:00
if ((column->indicator != -1) && (column->retcode != 1405))
oci_debug("_oci_make_zval: %16s,retlen = %4d,retlen4 = %d,storage_size4 = %4d,indicator %4d, retcode = %4d",
column->name,column->retlen,column->retlen4,column->storage_size4,column->indicator,column->retcode);
1999-04-22 06:49:16 +08:00
if (column->indicator == -1) { /* column is NULL */
ZVAL_NULL(value);
1999-04-22 06:49:16 +08:00
return 0;
}
1999-05-12 22:28:01 +08:00
if (column->is_cursor) { /* REFCURSOR -> simply return the statement id */
ZVAL_RESOURCE(value,column->stmtid);
zend_list_addref(column->stmtid);
1999-05-12 22:28:01 +08:00
} else if (column->is_descr) {
1999-07-08 22:12:07 +08:00
if ((column->data_type != SQLT_RDD) && (mode & OCI_RETURN_LOBS)) {
1999-04-22 06:49:16 +08:00
/* OCI_RETURN_LOBS means that we want the content of the LOB back instead of the locator */
descr = oci_get_desc(column->descid);
if (! descr) {
php_error(E_WARNING, "unable to find my descriptor %d",column->data);
1999-04-22 06:49:16 +08:00
return -1;
}
if (oci_loadlob(statement->conn,descr,&buffer,&loblen)) {
ZVAL_BOOL(value,0);
} else {
ZVAL_STRINGL(value,buffer,loblen,0);
}
} else {
/* return the locator */
object_init_ex(value, oci_lob_class_entry_ptr);
add_property_resource(value, "descriptor", column->descid);
zend_list_addref(column->descid);
1999-04-22 06:49:16 +08:00
}
} else {
switch (column->retcode) {
case 0:
/* intact value */
if (column->piecewise) {
size = column->retlen4;
} else {
size = column->retlen;
}
break;
default:
/* XXX we SHOULD maybe have a different behaviour for unknown results! */
ZVAL_BOOL(value,0);
return 0;
1999-04-22 06:49:16 +08:00
}
ZVAL_STRINGL(value,column->data,size,1);
1999-04-22 06:49:16 +08:00
}
return 0;
}
/* }}} */
1999-08-17 21:54:25 +08:00
/* {{{ oci_setprefetch() */
1999-04-22 06:49:16 +08:00
1999-07-08 22:12:07 +08:00
static int
1999-08-17 21:54:25 +08:00
oci_setprefetch(oci_statement *statement,int size)
1999-07-08 22:12:07 +08:00
{
ub4 prefetch;
prefetch = size * 1024;
statement->error =
1999-08-17 21:54:25 +08:00
oci_error(statement->pError,
1999-07-08 22:12:07 +08:00
"OCIAttrSet OCI_ATTR_PREFETCH_MEMORY",
OCIAttrSet(statement->pStmt,
OCI_HTYPE_STMT,
&prefetch,
0,
OCI_ATTR_PREFETCH_MEMORY,
statement->pError));
if (statement->error) {
oci_handle_error(statement->conn, statement->error);
}
1999-07-08 22:12:07 +08:00
prefetch = size;
statement->error =
1999-08-17 21:54:25 +08:00
oci_error(statement->pError,
1999-07-08 22:12:07 +08:00
"OCIAttrSet OCI_ATTR_PREFETCH_MEMORY",
OCIAttrSet(statement->pStmt,
OCI_HTYPE_STMT,
&prefetch,
0,
OCI_ATTR_PREFETCH_ROWS,
statement->pError));
if (statement->error) {
oci_handle_error(statement->conn, statement->error);
}
1999-07-08 22:12:07 +08:00
return 1;
}
1999-08-10 22:33:59 +08:00
/* }}} */
1999-08-17 21:54:25 +08:00
/* {{{ oci_parse() */
1999-08-10 22:33:59 +08:00
static oci_statement *oci_parse(oci_connection *connection, char *query, int len)
1999-04-22 06:49:16 +08:00
{
1999-08-17 21:54:25 +08:00
oci_statement *statement;
OCILS_FETCH();
1999-04-22 06:49:16 +08:00
1999-08-17 21:54:25 +08:00
statement = ecalloc(1,sizeof(oci_statement));
1999-08-17 21:54:25 +08:00
OCIHandleAlloc(OCI(pEnv),
1999-04-22 06:49:16 +08:00
(dvoid **)&statement->pStmt,
OCI_HTYPE_STMT,
0,
NULL);
1999-08-17 21:54:25 +08:00
OCIHandleAlloc(OCI(pEnv),
1999-04-22 06:49:16 +08:00
(dvoid **)&statement->pError,
OCI_HTYPE_ERROR,
0,
NULL);
1999-05-12 22:28:01 +08:00
if (len > 0) {
connection->error =
1999-08-17 21:54:25 +08:00
oci_error(connection->pError,
1999-05-12 22:28:01 +08:00
"OCIParse",
OCIStmtPrepare(statement->pStmt,
connection->pError,
1999-06-09 18:40:08 +08:00
(text*)query,
1999-05-12 22:28:01 +08:00
len,
OCI_NTV_SYNTAX,
OCI_DEFAULT));
if (connection->error) {
OCIHandleFree(statement->pStmt, OCI_HTYPE_STMT);
OCIHandleFree(statement->pError, OCI_HTYPE_ERROR);
efree(statement);
oci_handle_error(connection, connection->error);
1999-05-12 22:28:01 +08:00
return 0;
}
1999-04-22 06:49:16 +08:00
}
1999-05-12 22:28:01 +08:00
if (query) {
statement->last_query = estrdup(query);
}
1999-04-22 06:49:16 +08:00
statement->conn = connection;
statement->id = zend_list_insert(statement,le_stmt);
1999-04-22 06:49:16 +08:00
1999-08-17 21:54:25 +08:00
oci_debug("oci_parse \"%s\" id=%d conn=%d",
1999-05-12 22:28:01 +08:00
SAFE_STRING(query),
1999-04-22 06:49:16 +08:00
statement->id,
statement->conn->id);
zend_list_addref(statement->conn->id);
return statement;
1999-04-22 06:49:16 +08:00
}
/* }}} */
1999-08-17 21:54:25 +08:00
/* {{{ oci_execute() */
1999-04-22 06:49:16 +08:00
static int
oci_execute(oci_statement *statement, char *func,ub4 mode)
1999-04-22 06:49:16 +08:00
{
1999-08-17 21:54:25 +08:00
oci_out_column *outcol;
oci_out_column column;
1999-04-22 06:49:16 +08:00
OCIParam *param = 0;
text *colname;
ub4 counter;
ub2 define_type;
ub2 stmttype;
ub4 iters;
ub4 colcount;
1999-07-08 22:12:07 +08:00
ub2 dynamic;
int dtype;
1999-07-08 22:12:07 +08:00
dvoid *buf;
oci_descriptor *descr;
1999-04-22 06:49:16 +08:00
1999-05-12 22:28:01 +08:00
statement->error =
1999-08-17 21:54:25 +08:00
oci_error(statement->pError,
"OCIAttrGet OCI_HTYPE_STMT/OCI_ATTR_STMT_TYPE",
OCIAttrGet((dvoid *)statement->pStmt,
OCI_HTYPE_STMT,
(ub2 *)&stmttype,
(ub4 *)0,
OCI_ATTR_STMT_TYPE,
statement->pError));
1999-05-12 22:28:01 +08:00
if (statement->error) {
oci_handle_error(statement->conn, statement->error);
1999-04-22 06:49:16 +08:00
return 0;
}
if (stmttype == OCI_STMT_SELECT) {
iters = 0;
} else {
iters = 1;
}
if (statement->last_query) {
/* if we execute refcursors we don't have a query and
we don't want to execute!!! */
1999-04-22 06:49:16 +08:00
if (statement->binds) {
zend_hash_apply(statement->binds, (int (*)(void *)) _oci_bind_pre_exec);
}
1999-05-12 22:28:01 +08:00
statement->error =
1999-08-17 21:54:25 +08:00
oci_error(statement->pError,
"OCIStmtExecute",
OCIStmtExecute(statement->conn->pServiceContext,
statement->pStmt,
statement->pError,
iters,
0,
NULL,
NULL,
mode));
if (statement->binds) {
zend_hash_apply(statement->binds, (int (*)(void *)) _oci_bind_post_exec);
}
2001-02-17 23:54:40 +08:00
oci_handle_error(statement->conn, statement->error);
2001-02-17 23:54:40 +08:00
if (statement->error) {
return 0;
}
1999-04-22 06:49:16 +08:00
}
if (stmttype == OCI_STMT_SELECT && (statement->executed == 0)) {
/* we only need to do the define step is this very statement is executed the first time! */
statement->executed++;
ALLOC_HASHTABLE(statement->columns);
zend_hash_init(statement->columns, 13, NULL, _oci_column_hash_dtor, 0);
1999-04-22 06:49:16 +08:00
counter = 1;
1999-05-12 22:28:01 +08:00
statement->error =
1999-08-17 21:54:25 +08:00
oci_error(statement->pError,
"OCIAttrGet OCI_HTYPE_STMT/OCI_ATTR_PARAM_COUNT",
OCIAttrGet((dvoid *)statement->pStmt,
OCI_HTYPE_STMT,
(dvoid *)&colcount,
(ub4 *)0,
OCI_ATTR_PARAM_COUNT,
statement->pError));
1999-05-12 22:28:01 +08:00
if (statement->error) {
oci_handle_error(statement->conn, statement->error);
1999-04-22 06:49:16 +08:00
return 0; /* XXX we loose memory!!! */
}
statement->ncolumns = colcount;
1999-04-22 06:49:16 +08:00
for (counter = 1; counter <= colcount; counter++) {
1999-08-17 21:54:25 +08:00
memset(&column,0,sizeof(oci_out_column));
if (zend_hash_index_update(statement->columns, counter, &column,
sizeof(oci_out_column), (void**) &outcol) == FAILURE) {
1999-04-22 06:49:16 +08:00
efree(statement->columns);
/* out of memory */
return 0;
}
1999-04-22 06:49:16 +08:00
outcol->statement = statement;
1999-05-12 22:28:01 +08:00
statement->error =
1999-08-17 21:54:25 +08:00
oci_error(statement->pError,
1999-05-12 22:28:01 +08:00
"OCIParamGet OCI_HTYPE_STMT",
OCIParamGet(
(dvoid *)statement->pStmt,
OCI_HTYPE_STMT,
statement->pError,
(dvoid**)&param,
1999-05-12 22:28:01 +08:00
counter));
if (statement->error) {
oci_handle_error(statement->conn, statement->error);
1999-04-22 06:49:16 +08:00
return 0; /* XXX we loose memory!!! */
}
1999-05-12 22:28:01 +08:00
statement->error =
1999-08-17 21:54:25 +08:00
oci_error(statement->pError,
1999-05-12 22:28:01 +08:00
"OCIAttrGet OCI_DTYPE_PARAM/OCI_ATTR_DATA_TYPE",
OCIAttrGet((dvoid *)param,
OCI_DTYPE_PARAM,
1999-07-08 22:12:07 +08:00
(dvoid *)&outcol->data_type,
1999-05-12 22:28:01 +08:00
(ub4 *)0,
OCI_ATTR_DATA_TYPE,
statement->pError));
if (statement->error) {
oci_handle_error(statement->conn, statement->error);
1999-04-22 06:49:16 +08:00
return 0; /* XXX we loose memory!!! */
}
1999-06-09 18:40:08 +08:00
1999-05-12 22:28:01 +08:00
statement->error =
1999-08-17 21:54:25 +08:00
oci_error(statement->pError,
1999-05-12 22:28:01 +08:00
"OCIAttrGet OCI_DTYPE_PARAM/OCI_ATTR_DATA_SIZE",
OCIAttrGet((dvoid *)param,
OCI_DTYPE_PARAM,
1999-07-08 22:12:07 +08:00
(dvoid *)&outcol->data_size,
1999-05-12 22:28:01 +08:00
(dvoid *)0,
OCI_ATTR_DATA_SIZE,
statement->pError));
if (statement->error) {
oci_handle_error(statement->conn, statement->error);
1999-04-22 06:49:16 +08:00
return 0; /* XXX we loose memory!!! */
}
1999-07-08 22:12:07 +08:00
outcol->storage_size4 = outcol->data_size;
outcol->retlen = outcol->data_size;
1999-04-22 06:49:16 +08:00
statement->error =
oci_error(statement->pError,
"OCIAttrGet OCI_DTYPE_PARAM/OCI_ATTR_SCALE",
OCIAttrGet((dvoid *)param,
OCI_DTYPE_PARAM,
(dvoid *)&outcol->scale,
(dvoid *)0,
OCI_ATTR_SCALE,
statement->pError));
if (statement->error) {
oci_handle_error(statement->conn, statement->error);
return 0; /* XXX we lose memory!!! */
}
statement->error =
oci_error(statement->pError,
"OCIAttrGet OCI_DTYPE_PARAM/OCI_ATTR_PRECISION",
OCIAttrGet((dvoid *)param,
OCI_DTYPE_PARAM,
(dvoid *)&outcol->precision,
(dvoid *)0,
OCI_ATTR_PRECISION,
statement->pError));
if (statement->error) {
oci_handle_error(statement->conn, statement->error);
return 0; /* XXX we lose memory!!! */
}
1999-05-12 22:28:01 +08:00
statement->error =
1999-08-17 21:54:25 +08:00
oci_error(statement->pError,
1999-05-12 22:28:01 +08:00
"OCIAttrGet OCI_DTYPE_PARAM/OCI_ATTR_NAME",
OCIAttrGet((dvoid *)param,
OCI_DTYPE_PARAM,
(dvoid **)&colname, /* XXX this string is NOT zero terminated!!!! */
(ub4 *)&outcol->name_len,
(ub4)OCI_ATTR_NAME,
statement->pError));
if (statement->error) {
oci_handle_error(statement->conn, statement->error);
1999-04-22 06:49:16 +08:00
return 0; /* XXX we loose memory!!! */
}
1999-06-09 18:40:08 +08:00
outcol->name = estrndup((char*) colname,outcol->name_len);
1999-04-22 06:49:16 +08:00
/* find a user-setted define */
if (statement->defines) {
zend_hash_find(statement->defines,outcol->name,outcol->name_len,(void **) &outcol->define);
1999-04-22 06:49:16 +08:00
}
1999-07-08 22:12:07 +08:00
buf = 0;
switch (outcol->data_type) {
case SQLT_RSET:
outcol->pstmt = oci_parse(statement->conn,0,0);
outcol->stmtid = outcol->pstmt->id;
1999-05-12 22:28:01 +08:00
define_type = SQLT_RSET;
1999-07-08 22:12:07 +08:00
outcol->is_cursor = 1;
outcol->storage_size4 = -1;
outcol->retlen = -1;
dynamic = OCI_DEFAULT;
buf = &(outcol->pstmt->pStmt);
1999-04-22 06:49:16 +08:00
break;
case SQLT_RDD: /* ROWID */
case SQLT_BLOB: /* binary LOB */
case SQLT_CLOB: /* character LOB */
case SQLT_BFILE: /* binary file LOB */
1999-07-08 22:12:07 +08:00
define_type = outcol->data_type;
1999-04-22 06:49:16 +08:00
outcol->is_descr = 1;
outcol->storage_size4 = -1;
1999-07-08 22:12:07 +08:00
dynamic = OCI_DEFAULT;
if (outcol->data_type == SQLT_BFILE) {
dtype = OCI_DTYPE_FILE;
1999-07-08 22:12:07 +08:00
} else if (outcol->data_type == SQLT_RDD ) {
dtype = OCI_DTYPE_ROWID;
1999-07-08 22:12:07 +08:00
} else {
dtype = OCI_DTYPE_LOB;
1999-07-08 22:12:07 +08:00
}
descr = oci_new_desc(dtype,statement->conn);
outcol->descid = descr->id;
buf = &(descr->ocidescr);
1999-04-22 06:49:16 +08:00
break;
1999-06-09 18:40:08 +08:00
case SQLT_LNG:
1999-07-08 22:12:07 +08:00
case SQLT_LBI:
if (outcol->data_type == SQLT_LBI) {
define_type = SQLT_BIN;
} else {
define_type = SQLT_CHR;
1999-07-08 22:12:07 +08:00
}
1999-08-17 21:54:25 +08:00
outcol->storage_size4 = OCI_MAX_DATA_SIZE;
1999-06-09 18:40:08 +08:00
outcol->piecewise = 1;
1999-07-08 22:12:07 +08:00
dynamic = OCI_DYNAMIC_FETCH;
1999-04-22 06:49:16 +08:00
break;
1999-07-08 22:12:07 +08:00
case SQLT_BIN:
1999-04-22 06:49:16 +08:00
default:
define_type = SQLT_CHR;
1999-07-08 22:12:07 +08:00
if ((outcol->data_type == SQLT_DAT) || (outcol->data_type == SQLT_NUM)) {
outcol->storage_size4 = 512; /* XXX this should fit "most" NLS date-formats and Numbers */
1999-04-22 06:49:16 +08:00
} else {
outcol->storage_size4++; /* add one for string terminator */
}
1999-07-08 22:12:07 +08:00
if (outcol->data_type == SQLT_BIN) {
outcol->storage_size4 *= 3;
}
dynamic = OCI_DEFAULT;
buf = outcol->data = (text *) emalloc(outcol->storage_size4);
1999-04-22 06:49:16 +08:00
break;
}
1999-07-08 22:12:07 +08:00
if (dynamic == OCI_DYNAMIC_FETCH) {
statement->error =
1999-08-17 21:54:25 +08:00
oci_error(statement->pError,
1999-07-08 22:12:07 +08:00
"OCIDefineByPos",
OCIDefineByPos(statement->pStmt, /* IN/OUT handle to the requested SQL query */
(OCIDefine **)&outcol->pDefine,/* IN/OUT pointer to a pointer to a define handle */
statement->pError, /* IN/OUT An error handle */
counter, /* IN position in the select list */
(dvoid *)NULL, /* IN/OUT pointer to a buffer */
1999-07-08 22:12:07 +08:00
outcol->storage_size4, /* IN The size of each valuep buffer in bytes */
define_type, /* IN The data type */
(dvoid *)&outcol->indicator, /* IN pointer to an indicator variable or arr */
(ub2 *)NULL, /* IN/OUT Pointer to array of length of data fetched */
(ub2 *)NULL, /* OUT Pointer to array of column-level return codes */
OCI_DYNAMIC_FETCH)); /* IN mode (OCI_DEFAULT, OCI_DYNAMIC_FETCH) */
1999-07-08 22:12:07 +08:00
} else {
statement->error =
1999-08-17 21:54:25 +08:00
oci_error(statement->pError,
1999-07-08 22:12:07 +08:00
"OCIDefineByPos",
OCIDefineByPos(statement->pStmt, /* IN/OUT handle to the requested SQL query */
(OCIDefine **)&outcol->pDefine,/* IN/OUT pointer to a pointer to a define handle */
statement->pError, /* IN/OUT An error handle */
counter, /* IN position in the select list */
(dvoid *)buf, /* IN/OUT pointer to a buffer */
outcol->storage_size4, /* IN The size of each valuep buffer in bytes */
define_type, /* IN The data type */
(dvoid *)&outcol->indicator, /* IN pointer to an indicator variable or arr */
(ub2 *)&outcol->retlen, /* IN/OUT Pointer to array of length of data fetched */
(ub2 *)&outcol->retcode, /* OUT Pointer to array of column-level return codes */
OCI_DEFAULT)); /* IN mode (OCI_DEFAULT, OCI_DYNAMIC_FETCH) */
1999-07-08 22:12:07 +08:00
}
1999-05-12 22:28:01 +08:00
if (statement->error) {
oci_handle_error(statement->conn, statement->error);
1999-04-22 06:49:16 +08:00
return 0; /* XXX we loose memory!!! */
}
}
}
1999-07-08 22:12:07 +08:00
1999-04-22 06:49:16 +08:00
return 1;
}
/* }}} */
1999-08-17 21:54:25 +08:00
/* {{{ oci_fetch() */
1999-04-22 06:49:16 +08:00
static int
_oci_column_pre_fetch(void *data)
{
oci_out_column *col = (oci_out_column *) data;
if (col->piecewise) {
col->retlen4 = 0;
}
return 0;
}
1999-04-22 06:49:16 +08:00
static int
1999-08-17 21:54:25 +08:00
oci_fetch(oci_statement *statement, ub4 nrows, char *func)
1999-04-22 06:49:16 +08:00
{
int i;
1999-08-17 21:54:25 +08:00
oci_out_column *column;
1999-04-22 06:49:16 +08:00
if (statement->columns) {
zend_hash_apply(statement->columns, (int (*)(void *)) _oci_column_pre_fetch);
1999-06-09 18:40:08 +08:00
}
1999-05-12 22:28:01 +08:00
statement->error =
OCIStmtFetch(statement->pStmt,
statement->pError, nrows,
OCI_FETCH_NEXT,
OCI_DEFAULT);
if ((statement->error == OCI_NO_DATA) || (nrows == 0)) {
/* XXX this is needed for REFCURSORS! */
if (statement->columns) {
zend_hash_destroy(statement->columns);
1999-05-12 22:28:01 +08:00
efree(statement->columns);
statement->columns = 0;
statement->ncolumns = 0;
1999-05-12 22:28:01 +08:00
}
statement->executed = 0;
statement->error = 0; /* OCI_NO_DATA is NO error for us!!! */
1999-04-22 06:49:16 +08:00
return 0;
}
while (statement->error == OCI_NEED_DATA) {
for (i = 0; i < statement->ncolumns; i++) {
column = oci_get_col(statement, i + 1, 0);
if (column->piecewise) {
if (! column->data) {
column->data = (text *) emalloc(OCI_PIECE_SIZE);
} else {
column->data = erealloc(column->data,column->retlen4 + OCI_PIECE_SIZE);
}
column->cb_retlen = OCI_PIECE_SIZE;
OCIStmtSetPieceInfo((void *) column->pDefine,
OCI_HTYPE_DEFINE,
statement->pError,
((char*)column->data) + column->retlen4,
&(column->cb_retlen),
OCI_NEXT_PIECE,
&column->indicator,
&column->retcode);
}
}
statement->error =
OCIStmtFetch(statement->pStmt,
statement->pError, nrows,
OCI_FETCH_NEXT,
OCI_DEFAULT);
for (i = 0; i < statement->ncolumns; i++) {
column = oci_get_col(statement, i + 1, 0);
if (column->piecewise) {
column->retlen4 += column->cb_retlen;
}
}
}
1999-05-12 22:28:01 +08:00
if (statement->error == OCI_SUCCESS_WITH_INFO || statement->error == OCI_SUCCESS) {
1999-04-22 06:49:16 +08:00
/* do the stuff needed for OCIDefineByName */
for (i = 0; i < statement->ncolumns; i++) {
column = oci_get_col(statement, i + 1, 0);
if (column == NULL) {
1999-04-22 06:49:16 +08:00
continue;
}
1999-06-09 18:40:08 +08:00
if (! column->define) {
continue;
}
1999-07-12 18:36:19 +08:00
zval_dtor(column->define->zval);
_oci_make_zval(column->define->zval,statement,column,"OCIFetch",0);
1999-04-22 06:49:16 +08:00
}
return 1;
}
1999-08-17 21:54:25 +08:00
oci_error(statement->pError, func, statement->error);
oci_handle_error(statement->conn, statement->error);
1999-04-22 06:49:16 +08:00
return 0;
}
/* }}} */
/* {{{ oci_loadlob() */
1999-04-22 06:49:16 +08:00
#define LOBREADSIZE 1048576l /* 1MB */
static int
oci_loadlob(oci_connection *connection, oci_descriptor *mydescr, char **buffer,ub4 *loblen)
{
ub4 siz = 0;
ub4 readlen = 0;
char *buf;
1999-04-22 06:49:16 +08:00
*loblen = 0;
1999-04-22 06:49:16 +08:00
if (mydescr->type == OCI_DTYPE_FILE) {
1999-05-12 22:28:01 +08:00
connection->error =
OCILobFileOpen(connection->pServiceContext,
connection->pError,
mydescr->ocidescr,
OCI_FILE_READONLY);
if (connection->error) {
1999-08-17 21:54:25 +08:00
oci_error(connection->pError, "OCILobFileOpen",connection->error);
oci_handle_error(connection, connection->error);
return -1;
1999-04-22 06:49:16 +08:00
}
}
connection->error =
OCILobGetLength(connection->pServiceContext,
connection->pError,
mydescr->ocidescr,
&readlen);
if (connection->error) {
oci_error(connection->pError, "OCILobGetLength",connection->error);
oci_handle_error(connection, connection->error);
return -1;
}
buf = emalloc(readlen + 1);
while (readlen > 0) { /* thies loop should not be entered on readlen == 0 */
connection->error =
OCILobRead(connection->pServiceContext,
connection->pError,
mydescr->ocidescr,
&readlen, /* IN/OUT bytes toread/read */
siz + 1, /* offset (starts with 1) */
2000-08-16 23:23:05 +08:00
(dvoid *) ((char *) buf + siz),
readlen, /* size of buffer */
(dvoid *)0,
(OCICallbackLobRead) 0, /* callback... */
(ub2) 0, /* The character set ID of the buffer data. */
(ub1) SQLCS_IMPLICIT); /* The character set form of the buffer data. */
siz += readlen;
readlen = LOBREADSIZE;
if (connection->error == OCI_NEED_DATA) {
buf = erealloc(buf,siz + LOBREADSIZE + 1);
continue;
} else {
break;
}
}
1999-05-12 22:28:01 +08:00
if (connection->error) {
1999-08-17 21:54:25 +08:00
oci_error(connection->pError, "OCILobRead", connection->error);
oci_handle_error(connection, connection->error);
efree(buf);
return -1;
1999-04-22 06:49:16 +08:00
}
if (mydescr->type == OCI_DTYPE_FILE) {
1999-05-12 22:28:01 +08:00
connection->error =
OCILobFileClose(connection->pServiceContext,
connection->pError,
mydescr->ocidescr);
if (connection->error) {
1999-08-17 21:54:25 +08:00
oci_error(connection->pError, "OCILobFileClose", connection->error);
oci_handle_error(connection, connection->error);
efree(buf);
return -1;
1999-04-22 06:49:16 +08:00
}
}
buf = erealloc(buf,siz+1);
buf[ siz ] = 0;
1999-04-22 06:49:16 +08:00
*buffer = buf;
*loblen = siz;
oci_debug("OCIloadlob: size=%d",siz);
return 0;
}
/* }}} */
1999-08-17 21:54:25 +08:00
/* {{{ oci_failover_callback() */
1999-05-12 22:28:01 +08:00
#if 0 /* not needed yet ! */
static sb4
1999-08-17 21:54:25 +08:00
oci_failover_callback(dvoid *svchp,
dvoid* envhp,
dvoid *fo_ctx,
ub4 fo_type,
ub4 fo_event)
{
/*
2000-10-29 17:14:55 +08:00
this stuff is from an oci sample - it will get cleaned up as soon as i understand it!!! (thies@thieso.net 990420)
right now i cant get oracle to even call it;-(((((((((((
*/
switch (fo_event)
{
case OCI_FO_BEGIN:
{
printf(" Failing Over ... Please stand by \n");
printf(" Failover type was found to be %s \n",
((fo_type==OCI_FO_NONE) ? "NONE"
:(fo_type==OCI_FO_SESSION) ? "SESSION"
:(fo_type==OCI_FO_SELECT) ? "SELECT"
: "UNKNOWN!"));
printf(" Failover Context is :%s\n",
(fo_ctx?(char *)fo_ctx:"NULL POINTER!"));
break;
}
case OCI_FO_ABORT:
{
printf(" Failover aborted. Failover will not take place.\n");
break;
}
case OCI_FO_END:
{
printf(" Failover ended ...resuming services\n");
break;
}
case OCI_FO_REAUTH:
{
printf(" Failed over user. Resuming services\n");
/* Application can check the OCI_ATTR_SESSION attribute of
the service handle to find out the user being
re-authenticated.
After this, the application can replay any ALTER SESSION
commands associated with this session. These must have
been saved by the application in the fo_ctx
*/
break;
}
case OCI_FO_ERROR:
{
printf(" Failover error gotten. Sleeping...\n");
php_sleep(3);
/* cannot find this blody define !!! return OCI_FO_RETRY; */
break;
}
default:
{
printf("Bad Failover Event: %ld.\n", fo_event);
break;
}
}
return 0;
}
1999-05-12 22:28:01 +08:00
#endif
1999-04-22 06:49:16 +08:00
/* }}} */
1999-08-17 21:54:25 +08:00
/* {{{ oci_bind_in_callback() */
1999-04-22 06:49:16 +08:00
static sb4
oci_bind_in_callback(dvoid *ictxp, /* context pointer */
OCIBind *bindp, /* bind handle */
ub4 iter, /* 0-based execute iteration value */
ub4 index, /* index of current array for PL/SQL or row index for SQL */
dvoid **bufpp, /* pointer to data */
ub4 *alenp, /* size after value/piece has been read */
ub1 *piecep, /* which piece */
dvoid **indpp) /* indicator value */
1999-04-22 06:49:16 +08:00
{
1999-08-17 21:54:25 +08:00
oci_bind *phpbind;
zval *val;
1999-04-22 06:49:16 +08:00
if (!(phpbind=(oci_bind *)ictxp) || !(val = phpbind->zval)) {
php_error(E_WARNING, "!phpbind || !phpbind->val");
1999-04-22 06:49:16 +08:00
return OCI_ERROR;
}
if (ZVAL_IS_NULL(val)) {
/* we're going to insert a NULL column */
phpbind->indicator = -1;
*bufpp = 0;
*alenp = -1;
*indpp = (dvoid *)&phpbind->indicator;
} else if ((phpbind->descr == 0) && (phpbind->pStmt == 0)) {
/* "normal string bind */
1999-04-22 06:49:16 +08:00
convert_to_string(val);
*bufpp = val->value.str.val;
*alenp = val->value.str.len;
1999-04-22 06:49:16 +08:00
*indpp = (dvoid *)&phpbind->indicator;
1999-05-12 22:28:01 +08:00
} else if (phpbind->pStmt != 0) {
/* RSET */
1999-05-12 22:28:01 +08:00
*bufpp = phpbind->pStmt;
*alenp = -1; /* seems to be allright */
*indpp = (dvoid *)&phpbind->indicator;
} else {
/* descriptor bind */
1999-04-22 06:49:16 +08:00
*bufpp = phpbind->descr;
*alenp = -1; /* seems to be allright */
*indpp = (dvoid *)&phpbind->indicator;
}
*piecep = OCI_ONE_PIECE; /* pass all data in one go */
return OCI_CONTINUE;
}
/* }}} */
1999-08-17 21:54:25 +08:00
/* {{{ oci_bind_out_callback() */
1999-04-22 06:49:16 +08:00
static sb4
oci_bind_out_callback(dvoid *octxp, /* context pointer */
OCIBind *bindp, /* bind handle */
ub4 iter, /* 0-based execute iteration value */
ub4 index, /* index of current array for PL/SQL or row index for SQL */
dvoid **bufpp, /* pointer to data */
ub4 **alenpp, /* size after value/piece has been read */
ub1 *piecep, /* which piece */
dvoid **indpp, /* indicator value */
ub2 **rcodepp) /* return code */
1999-04-22 06:49:16 +08:00
{
1999-08-17 21:54:25 +08:00
oci_bind *phpbind;
zval *val;
sb4 retval = OCI_ERROR;
1999-04-22 06:49:16 +08:00
if (!(phpbind=(oci_bind *)octxp) || !(val = phpbind->zval)) {
php_error(E_WARNING, "!phpbind || !phpbind->val");
return retval;
1999-04-22 06:49:16 +08:00
}
if ((val->type == IS_OBJECT) || (val->type == IS_RESOURCE)) {
retval = OCI_CONTINUE;
} else {
convert_to_string(val);
zval_dtor(val);
val->value.str.len = OCI_PIECE_SIZE; /* 64K-1 is max XXX */
val->value.str.val = emalloc(phpbind->zval->value.str.len);
/* XXX we assume that zend-zval len has 4 bytes */
*alenpp = (ub4*) &phpbind->zval->value.str.len;
*bufpp = phpbind->zval->value.str.val;
1999-04-22 06:49:16 +08:00
*piecep = OCI_ONE_PIECE;
*rcodepp = &phpbind->retcode;
*indpp = &phpbind->indicator;
retval = OCI_CONTINUE;
1999-04-22 06:49:16 +08:00
}
return retval;
1999-04-22 06:49:16 +08:00
}
/* }}} */
/* {{{ _oci_open_session()
1999-04-22 06:49:16 +08:00
*/
static oci_session *_oci_open_session(oci_server* server,char *username,char *password,int persistent,int exclusive)
1999-04-22 06:49:16 +08:00
{
oci_session *session = 0, *psession = 0;
1999-04-22 06:49:16 +08:00
OCISvcCtx *svchp = 0;
1999-05-12 22:28:01 +08:00
char *hashed_details;
1999-08-17 21:54:25 +08:00
OCILS_FETCH();
1999-04-22 06:49:16 +08:00
/*
check if we already have this user authenticated
we will reuse authenticated users within a request no matter if the user requested a persistent
connections or not!
but only as pesistent requested connections will be kept between requests!
*/
hashed_details = (char *) malloc(strlen(SAFE_STRING(username))+
strlen(SAFE_STRING(password))+
strlen(SAFE_STRING(server->dbname))+1);
sprintf(hashed_details,"%s%s%s",
1999-04-22 06:49:16 +08:00
SAFE_STRING(username),
SAFE_STRING(password),
SAFE_STRING(server->dbname));
1999-05-12 22:28:01 +08:00
if (! exclusive) {
zend_hash_find(OCI(user), hashed_details, strlen(hashed_details)+1, (void **) &session);
1999-04-22 06:49:16 +08:00
1999-05-12 22:28:01 +08:00
if (session) {
2001-05-04 22:53:45 +08:00
if (session->is_open) {
1999-05-12 22:28:01 +08:00
if (persistent) {
session->persistent = 1;
}
free(hashed_details);
1999-05-12 22:28:01 +08:00
return session;
} else {
_oci_close_session(session);
1999-05-12 22:28:01 +08:00
/* breakthru to open */
}
}
1999-04-22 06:49:16 +08:00
}
1999-08-17 21:54:25 +08:00
session = calloc(1,sizeof(oci_session));
1999-04-22 06:49:16 +08:00
if (! session) {
goto CLEANUP;
}
session->persistent = persistent;
session->hashed_details = hashed_details;
1999-04-22 06:49:16 +08:00
session->server = server;
session->exclusive = exclusive;
1999-04-22 06:49:16 +08:00
/* allocate temporary Service Context */
1999-08-17 21:54:25 +08:00
OCI(error) =
OCIHandleAlloc(OCI(pEnv),
1999-05-12 22:28:01 +08:00
(dvoid **)&svchp,
OCI_HTYPE_SVCCTX,
0,
NULL);
1999-08-17 21:54:25 +08:00
if (OCI(error) != OCI_SUCCESS) {
oci_error(OCI(pError), "_oci_open_session: OCIHandleAlloc OCI_HTYPE_SVCCTX", OCI(error));
1999-04-22 06:49:16 +08:00
goto CLEANUP;
}
/* allocate private session-handle */
1999-08-17 21:54:25 +08:00
OCI(error) =
OCIHandleAlloc(OCI(pEnv),
1999-05-12 22:28:01 +08:00
(dvoid **)&session->pSession,
OCI_HTYPE_SESSION,
0,
NULL);
1999-08-17 21:54:25 +08:00
if (OCI(error) != OCI_SUCCESS) {
oci_error(OCI(pError), "_oci_open_session: OCIHandleAlloc OCI_HTYPE_SESSION", OCI(error));
1999-04-22 06:49:16 +08:00
goto CLEANUP;
}
/* Set the server handle in service handle */
1999-08-17 21:54:25 +08:00
OCI(error) =
1999-05-12 22:28:01 +08:00
OCIAttrSet(svchp,
OCI_HTYPE_SVCCTX,
server->pServer,
0,
OCI_ATTR_SERVER,
1999-08-17 21:54:25 +08:00
OCI(pError));
if (OCI(error) != OCI_SUCCESS) {
oci_error(OCI(pError), "_oci_open_session: OCIAttrSet OCI_ATTR_SERVER", OCI(error));
1999-04-22 06:49:16 +08:00
goto CLEANUP;
}
/* set the username in user handle */
1999-08-17 21:54:25 +08:00
OCI(error) =
1999-05-12 22:28:01 +08:00
OCIAttrSet((dvoid *) session->pSession,
(ub4) OCI_HTYPE_SESSION,
(dvoid *) username,
(ub4) strlen(username),
(ub4) OCI_ATTR_USERNAME,
1999-08-17 21:54:25 +08:00
OCI(pError));
if (OCI(error) != OCI_SUCCESS) {
oci_error(OCI(pError), "OCIAttrSet OCI_ATTR_USERNAME", OCI(error));
1999-04-22 06:49:16 +08:00
goto CLEANUP;
}
/* set the password in user handle */
1999-08-17 21:54:25 +08:00
OCI(error) =
1999-05-12 22:28:01 +08:00
OCIAttrSet((dvoid *) session->pSession,
(ub4) OCI_HTYPE_SESSION,
(dvoid *) password,
(ub4) strlen(password),
(ub4) OCI_ATTR_PASSWORD,
1999-08-17 21:54:25 +08:00
OCI(pError));
if (OCI(error) != OCI_SUCCESS) {
oci_error(OCI(pError), "OCIAttrSet OCI_ATTR_PASSWORD", OCI(error));
1999-04-22 06:49:16 +08:00
goto CLEANUP;
}
1999-08-17 21:54:25 +08:00
OCI(error) =
1999-05-12 22:28:01 +08:00
OCISessionBegin(svchp,
1999-08-17 21:54:25 +08:00
OCI(pError),
1999-05-12 22:28:01 +08:00
session->pSession,
(ub4) OCI_CRED_RDBMS,
(ub4) OCI_DEFAULT);
1999-08-17 21:54:25 +08:00
if (OCI(error) != OCI_SUCCESS) {
oci_error(OCI(pError), "OCISessionBegin", OCI(error));
1999-04-22 06:49:16 +08:00
goto CLEANUP;
}
/* Free Temporary Service Context */
OCIHandleFree((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX);
1999-05-12 22:28:01 +08:00
if (exclusive) {
psession = session;
1999-05-12 22:28:01 +08:00
} else {
zend_hash_update(OCI(user),
session->hashed_details,
strlen(session->hashed_details)+1,
(void *)session,
sizeof(oci_session),
(void**)&psession);
1999-05-12 22:28:01 +08:00
}
1999-04-22 06:49:16 +08:00
psession->num = zend_list_insert(psession,le_session);
2001-05-04 22:53:45 +08:00
psession->is_open = 1;
1999-04-22 06:49:16 +08:00
oci_debug("_oci_open_session new sess=%d user=%s",psession->num,username);
1999-04-22 06:49:16 +08:00
if (! exclusive) free(session);
1999-04-22 06:49:16 +08:00
return psession;
CLEANUP:
oci_debug("_oci_open_session: FAILURE -> CLEANUP called");
1999-05-12 22:28:01 +08:00
_oci_close_session(session);
1999-04-22 06:49:16 +08:00
return 0;
}
/* }}} */
/* {{{ _oci_close_session()
1999-04-22 06:49:16 +08:00
*/
static void
_oci_close_session(oci_session *session)
1999-04-22 06:49:16 +08:00
{
OCISvcCtx *svchp;
char *hashed_details;
1999-08-17 21:54:25 +08:00
OCILS_FETCH();
1999-04-22 06:49:16 +08:00
if (! session) {
return;
}
oci_debug("START _oci_close_session: logging-off sess=%d",session->num);
2001-05-04 22:53:45 +08:00
if (session->is_open) {
/* Temporary Service Context */
1999-08-17 21:54:25 +08:00
OCI(error) =
OCIHandleAlloc(OCI(pEnv),
1999-05-12 22:28:01 +08:00
(dvoid **) &svchp,
(ub4) OCI_HTYPE_SVCCTX,
(size_t) 0,
(dvoid **) 0);
1999-08-17 21:54:25 +08:00
if (OCI(error) != OCI_SUCCESS) {
oci_error(OCI(pError), "_oci_close_session OCIHandleAlloc OCI_HTYPE_SVCCTX", OCI(error));
}
/* Set the server handle in service handle */
1999-08-17 21:54:25 +08:00
OCI(error) =
1999-05-12 22:28:01 +08:00
OCIAttrSet(svchp,
OCI_HTYPE_SVCCTX,
session->server->pServer,
0,
OCI_ATTR_SERVER,
1999-08-17 21:54:25 +08:00
OCI(pError));
if (OCI(error) != OCI_SUCCESS) {
oci_error(OCI(pError), "_oci_close_session: OCIAttrSet OCI_ATTR_SERVER", OCI(error));
}
/* Set the Authentication handle in the service handle */
1999-08-17 21:54:25 +08:00
OCI(error) =
1999-05-12 22:28:01 +08:00
OCIAttrSet(svchp,
OCI_HTYPE_SVCCTX,
session->pSession,
0,
OCI_ATTR_SESSION,
1999-08-17 21:54:25 +08:00
OCI(pError));
if (OCI(error) != OCI_SUCCESS) {
oci_error(OCI(pError), "_oci_close_session: OCIAttrSet OCI_ATTR_SESSION", OCI(error));
}
1999-08-17 21:54:25 +08:00
OCI(error) =
1999-05-12 22:28:01 +08:00
OCISessionEnd(svchp,
1999-08-17 21:54:25 +08:00
OCI(pError),
1999-05-12 22:28:01 +08:00
session->pSession,
(ub4) 0);
1999-08-17 21:54:25 +08:00
if (OCI(error) != OCI_SUCCESS) {
oci_error(OCI(pError), "_oci_close_session: OCISessionEnd", OCI(error));
}
OCIHandleFree((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX);
} else {
oci_debug("_oci_close_session: logging-off DEAD session");
}
1999-05-12 22:28:01 +08:00
if (session->pSession) {
OCIHandleFree((dvoid *) session->pSession, (ub4) OCI_HTYPE_SESSION);
}
hashed_details = session->hashed_details;
if (! OCI(shutdown)) {
zend_hash_del(OCI(user), hashed_details, strlen(hashed_details)+1);
}
free(hashed_details);
1999-04-22 06:49:16 +08:00
}
/* }}} */
/* {{{ _oci_open_server()
1999-04-22 06:49:16 +08:00
*/
static oci_server *_oci_open_server(char *dbname,int persistent)
1999-04-22 06:49:16 +08:00
{
oci_server *server, *pserver = 0;
1999-08-17 21:54:25 +08:00
OCILS_FETCH();
1999-04-22 06:49:16 +08:00
/*
check if we already have this server open
we will reuse servers within a request no matter if the user requested persistent
1999-04-22 06:49:16 +08:00
connections or not!
but only as pesistent requested connections will be kept between requests!
*/
zend_hash_find(OCI(server), dbname, strlen(dbname)+1, (void **) &pserver);
1999-04-22 06:49:16 +08:00
if (pserver) {
/* XXX ini-flag */
/*
if (! oci_ping(pserver)) {
2001-05-04 22:53:45 +08:00
pserver->is_open = 0;
}
*/
2001-05-04 22:53:45 +08:00
if (pserver->is_open) {
/* if our new users uses this connection persistent, we're keeping it! */
if (persistent) {
pserver->persistent = persistent;
}
1999-05-12 22:28:01 +08:00
return pserver;
} else { /* server "died" in the meantime - try to reconnect! */
_oci_close_server(pserver);
/* breakthru to open */
}
1999-04-22 06:49:16 +08:00
}
1999-08-17 21:54:25 +08:00
server = calloc(1,sizeof(oci_server));
1999-04-22 06:49:16 +08:00
server->persistent = persistent;
1999-05-12 22:28:01 +08:00
server->dbname = strdup(SAFE_STRING(dbname));
1999-04-22 06:49:16 +08:00
1999-08-17 21:54:25 +08:00
OCIHandleAlloc(OCI(pEnv),
1999-04-22 06:49:16 +08:00
(dvoid **)&server->pServer,
OCI_HTYPE_SERVER,
0,
NULL);
1999-08-17 21:54:25 +08:00
OCI(error) =
1999-05-12 22:28:01 +08:00
OCIServerAttach(server->pServer,
1999-08-17 21:54:25 +08:00
OCI(pError),
(text*)server->dbname,
strlen(server->dbname),
1999-05-12 22:28:01 +08:00
(ub4) OCI_DEFAULT);
1999-04-22 06:49:16 +08:00
1999-08-17 21:54:25 +08:00
if (OCI(error)) {
oci_error(OCI(pError), "_oci_open_server", OCI(error));
1999-04-22 06:49:16 +08:00
goto CLEANUP;
}
zend_hash_update(OCI(server),
server->dbname,
strlen(server->dbname)+1,
(void *)server,
sizeof(oci_server),
(void**)&pserver);
pserver->num = zend_list_insert(pserver,le_server);
2001-05-04 22:53:45 +08:00
pserver->is_open = 1;
oci_debug("_oci_open_server new conn=%d dname=%s",server->num,server->dbname);
free(server);
return pserver;
1999-05-12 22:28:01 +08:00
CLEANUP:
oci_debug("_oci_open_server: FAILURE -> CLEANUP called");
_oci_close_server(server);
return 0;
}
1999-04-22 06:49:16 +08:00
1999-05-12 22:28:01 +08:00
#if 0
server->failover.fo_ctx = (dvoid *) server;
1999-08-17 21:54:25 +08:00
server->failover.callback_function = oci_failover_callback;
1999-04-22 06:49:16 +08:00
error = OCIAttrSet((dvoid *)server->pServer,
(ub4) OCI_HTYPE_SERVER,
(dvoid *) &server->failover,
(ub4) 0,
(ub4) OCI_ATTR_FOCBK,
1999-08-17 21:54:25 +08:00
OCI(pError));
if (error) {
oci_error(OCI(pError), "_oci_open_server OCIAttrSet OCI_ATTR_FOCBK", error);
goto CLEANUP;
1999-04-22 06:49:16 +08:00
}
1999-05-12 22:28:01 +08:00
#endif
1999-04-22 06:49:16 +08:00
/* }}} */
/* {{{ _oci_close_server()
*/
1999-04-22 06:49:16 +08:00
static int _oci_session_cleanup(void *data)
{
list_entry *le = (list_entry *) data;
if (le->type == le_session) {
oci_server *server = ((oci_session*) le->ptr)->server;
2001-05-04 22:53:45 +08:00
if (server->is_open == 2)
return 1;
1999-05-12 22:28:01 +08:00
}
1999-04-22 06:49:16 +08:00
return 0;
}
static void
1999-08-17 21:54:25 +08:00
_oci_close_server(oci_server *server)
1999-04-22 06:49:16 +08:00
{
char *dbname;
int oldopen;
1999-08-17 21:54:25 +08:00
OCILS_FETCH();
ELS_FETCH();
2001-05-04 22:53:45 +08:00
oldopen = server->is_open;
server->is_open = 2;
if (! OCI(shutdown)) {
zend_hash_apply(&EG(regular_list),_oci_session_cleanup);
1999-04-22 06:49:16 +08:00
}
2001-05-04 22:53:45 +08:00
server->is_open = oldopen;
1999-04-22 06:49:16 +08:00
oci_debug("START _oci_close_server: detaching conn=%d dbname=%s",server->num,server->dbname);
/* XXX close server here */
2001-05-04 22:53:45 +08:00
if (server->is_open) {
1999-08-17 21:54:25 +08:00
if (server->pServer && OCI(pError)) {
OCI(error) =
1999-05-12 22:28:01 +08:00
OCIServerDetach(server->pServer,
1999-08-17 21:54:25 +08:00
OCI(pError),
1999-05-12 22:28:01 +08:00
OCI_DEFAULT);
1999-08-17 21:54:25 +08:00
if (OCI(error)) {
oci_error(OCI(pError), "oci_close_server OCIServerDetach", OCI(error));
}
}
} else {
1999-08-19 12:13:56 +08:00
oci_debug("_oci_close_server: closing DEAD server");
}
1999-05-12 22:28:01 +08:00
if (server->pServer) {
OCIHandleFree((dvoid *) server->pServer, (ub4) OCI_HTYPE_SERVER);
1999-05-12 22:28:01 +08:00
}
dbname = server->dbname;
if (! OCI(shutdown)) {
zend_hash_del(OCI(server),dbname,strlen(dbname)+1);
}
free(dbname);
1999-04-22 06:49:16 +08:00
}
/* }}} */
1999-08-17 21:54:25 +08:00
/* {{{ oci_do_connect()
1999-04-22 06:49:16 +08:00
Connect to an Oracle database and log on. returns a new session.
*/
1999-08-17 21:54:25 +08:00
static void oci_do_connect(INTERNAL_FUNCTION_PARAMETERS,int persistent,int exclusive)
1999-04-22 06:49:16 +08:00
{
1999-06-09 18:40:08 +08:00
char *username, *password, *dbname;
zval **userParam, **passParam, **dbParam;
1999-08-17 21:54:25 +08:00
oci_server *server = 0;
oci_session *session = 0;
oci_connection *connection = 0;
OCILS_FETCH();
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(3, &userParam, &passParam, &dbParam) == SUCCESS) {
convert_to_string_ex(userParam);
convert_to_string_ex(passParam);
convert_to_string_ex(dbParam);
username = (*userParam)->value.str.val;
password = (*passParam)->value.str.val;
dbname = (*dbParam)->value.str.val;
1999-12-19 06:40:35 +08:00
} else if (zend_get_parameters_ex(2, &userParam, &passParam) == SUCCESS) {
convert_to_string_ex(userParam);
convert_to_string_ex(passParam);
username = (*userParam)->value.str.val;
password = (*passParam)->value.str.val;
1999-06-09 18:40:08 +08:00
dbname = "";
1999-04-22 06:49:16 +08:00
} else {
WRONG_PARAM_COUNT;
}
1999-08-17 21:54:25 +08:00
connection = (oci_connection *) ecalloc(1,sizeof(oci_connection));
1999-04-22 06:49:16 +08:00
if (! connection) {
goto CLEANUP;
}
server = _oci_open_server(dbname,persistent);
1999-04-22 06:49:16 +08:00
if (! server) {
goto CLEANUP;
}
persistent = server->persistent; /* if our server-context is not persistent we can't */
session = _oci_open_session(server,username,password,persistent,exclusive);
1999-04-22 06:49:16 +08:00
if (! session) {
goto CLEANUP;
}
/* set our session */
connection->session = session;
/* allocate our private error-handle */
1999-08-17 21:54:25 +08:00
OCI(error) =
OCIHandleAlloc(OCI(pEnv),
1999-05-12 22:28:01 +08:00
(dvoid **)&connection->pError,
OCI_HTYPE_ERROR,
0,
NULL);
1999-08-17 21:54:25 +08:00
if (OCI(error) != OCI_SUCCESS) {
oci_error(OCI(pError), "oci_do_connect: OCIHandleAlloc OCI_HTYPE_ERROR",OCI(error));
1999-04-22 06:49:16 +08:00
goto CLEANUP;
}
/* allocate our service-context */
1999-08-17 21:54:25 +08:00
OCI(error) =
OCIHandleAlloc(OCI(pEnv),
1999-05-12 22:28:01 +08:00
(dvoid **)&connection->pServiceContext,
OCI_HTYPE_SVCCTX,
0,
NULL);
1999-08-17 21:54:25 +08:00
if (OCI(error) != OCI_SUCCESS) {
oci_error(OCI(pError), "oci_do_connect: OCIHandleAlloc OCI_HTYPE_SVCCTX",OCI(error));
1999-04-22 06:49:16 +08:00
goto CLEANUP;
}
/* Set the server handle in service handle */
1999-05-12 22:28:01 +08:00
connection->error =
OCIAttrSet(connection->pServiceContext,
OCI_HTYPE_SVCCTX,
server->pServer,
0,
OCI_ATTR_SERVER,
connection->pError);
if (connection->error != OCI_SUCCESS) {
1999-08-17 21:54:25 +08:00
oci_error(connection->pError, "oci_do_connect: OCIAttrSet OCI_ATTR_SERVER", connection->error);
1999-04-22 06:49:16 +08:00
goto CLEANUP;
}
1999-05-12 22:28:01 +08:00
/* Set the Authentication handle in the service handle */
connection->error =
OCIAttrSet(connection->pServiceContext,
OCI_HTYPE_SVCCTX,
session->pSession,
0,
OCI_ATTR_SESSION,
connection->pError);
if (connection->error != OCI_SUCCESS) {
1999-08-17 21:54:25 +08:00
oci_error(connection->pError, "oci_do_connect: OCIAttrSet OCI_ATTR_SESSION", connection->error);
1999-04-22 06:49:16 +08:00
goto CLEANUP;
}
/*
OCIAttrSet((dvoid *)session->server->pServer,
OCI_HTYPE_SERVER,
(dvoid *) "demo",
0,
OCI_ATTR_EXTERNAL_NAME,
connection->pError);
OCIAttrSet((dvoid *)session->server->pServer,
OCI_HTYPE_SERVER,
(dvoid *) "txn demo2",
0,
OCI_ATTR_INTERNAL_NAME,
connection->pError);
*/
connection->id = zend_list_insert(connection, le_conn);
2001-05-04 22:53:45 +08:00
connection->is_open = 1;
1999-08-17 21:54:25 +08:00
oci_debug("oci_do_connect: id=%d",connection->id);
1999-04-22 06:49:16 +08:00
RETURN_RESOURCE(connection->id);
1999-07-08 22:12:07 +08:00
1999-04-22 06:49:16 +08:00
CLEANUP:
1999-08-17 21:54:25 +08:00
oci_debug("oci_do_connect: FAILURE -> CLEANUP called");
if (connection->id) {
zend_list_delete(connection->id);
} else {
_oci_conn_list_dtor(connection);
}
1999-04-22 06:49:16 +08:00
RETURN_FALSE;
}
/* }}} */
/************************* EXTENSION FUNCTIONS *************************/
/* {{{ proto int ocidefinebyname(int stmt, string name, mixed &var [, int type])
Define a PHP variable to an Oracle column by name */
/* if you want to define a LOB/CLOB etc make sure you allocate it via OCINewDescriptor BEFORE defining!!!
1999-04-22 06:49:16 +08:00
*/
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocidefinebyname)
1999-04-22 06:49:16 +08:00
{
zval **stmt, **name, **var, **type;
1999-08-17 21:54:25 +08:00
oci_statement *statement;
oci_define *define, *tmp_define;
ub2 ocitype = SQLT_CHR; /* zero terminated string */
int ac = ZEND_NUM_ARGS();
1999-04-22 06:49:16 +08:00
1999-12-19 06:40:35 +08:00
if (ac < 3 || ac > 4 || zend_get_parameters_ex(ac, &stmt, &name, &var, &type) == FAILURE) {
WRONG_PARAM_COUNT;
}
1999-04-22 06:49:16 +08:00
switch (ac) {
case 4:
convert_to_long_ex(type);
ocitype = (ub2) (*type)->value.lval;
/* possible breakthru */
1999-04-22 06:49:16 +08:00
}
OCI_GET_STMT(statement,stmt);
1999-04-22 06:49:16 +08:00
convert_to_string_ex(name);
1999-04-22 06:49:16 +08:00
if (statement->defines == NULL) {
ALLOC_HASHTABLE(statement->defines);
zend_hash_init(statement->defines, 13, NULL, _oci_define_hash_dtor, 0);
1999-04-22 06:49:16 +08:00
}
define = ecalloc(1,sizeof(oci_define));
if (zend_hash_add(statement->defines,
(*name)->value.str.val,
(*name)->value.str.len,
define,
sizeof(oci_define),
(void **)&tmp_define) == SUCCESS) {
1999-04-22 06:49:16 +08:00
efree(define);
define = tmp_define;
} else {
1999-12-04 20:06:32 +08:00
efree(define);
1999-04-22 06:49:16 +08:00
RETURN_FALSE;
}
define->name = (text*) estrndup((*name)->value.str.val,(*name)->value.str.len);
define->name_len = (*name)->value.str.len;
1999-04-22 06:49:16 +08:00
define->type = ocitype;
define->zval = *var;
zval_add_ref(var);
1999-04-22 06:49:16 +08:00
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int ocibindbyname(int stmt, string name, mixed &var, int maxlength [, int type])
Bind a PHP variable to an Oracle placeholder by name */
/* if you want to bind a LOB/CLOB etc make sure you allocate it via OCINewDescriptor BEFORE binding!!!
1999-04-22 06:49:16 +08:00
*/
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocibindbyname)
1999-04-22 06:49:16 +08:00
{
zval **stmt, **name, **var, **maxlen, **type;
1999-08-17 21:54:25 +08:00
oci_statement *statement;
oci_statement *bindstmt;
oci_bind bind, *bindp;
1999-08-17 21:54:25 +08:00
oci_descriptor *descr;
#ifdef WITH_COLLECTIONS
oci_collection *coll;
int mode = OCI_DATA_AT_EXEC;
dvoid *mycoll = 0;
#endif
ub2 ocitype = SQLT_CHR; /* unterminated string */
1999-05-12 22:28:01 +08:00
OCIStmt *mystmt = 0;
1999-04-22 06:49:16 +08:00
dvoid *mydescr = 0;
sb4 value_sz = -1;
int ac = ZEND_NUM_ARGS(), inx;
1999-04-22 06:49:16 +08:00
if (ac < 3 || ac > 5 || zend_get_parameters_ex(ac, &stmt, &name, &var, &maxlen, &type) == FAILURE) {
WRONG_PARAM_COUNT;
}
1999-04-22 06:49:16 +08:00
switch (ac) {
case 5:
convert_to_long_ex(type);
2000-04-24 20:35:30 +08:00
ocitype = (ub2) (*type)->value.lval;
/* possible breakthru */
case 4:
convert_to_long_ex(maxlen);
value_sz = (*maxlen)->value.lval;
/* possible breakthru */
1999-04-22 06:49:16 +08:00
}
OCI_GET_STMT(statement,stmt);
switch (ocitype) {
#ifdef WITH_COLLECTIONS
case SQLT_NTY:
if((*var)->type != IS_OBJECT) {
php_error(E_WARNING,"Variable must be allocated using OCINewCollection()");
RETURN_FALSE;
}
if ((inx = _oci_get_ocicoll(*var,&coll)) == 0) {
php_error(E_WARNING,"Variable must be allocated using OCINewCollection()");
RETURN_FALSE;
}
if (! (mycoll = (dvoid *) coll->coll)) {
php_error(E_WARNING,"Collection empty");
RETURN_FALSE;
}
value_sz = sizeof(void*);
mode = OCI_DEFAULT;
break;
#endif
case SQLT_BFILEE:
case SQLT_CFILEE:
case SQLT_CLOB:
case SQLT_BLOB:
case SQLT_RDD:
if ((*var)->type != IS_OBJECT) {
php_error(E_WARNING,"Variable must be allocated using OCINewDescriptor()");
RETURN_FALSE;
}
if ((inx = _oci_get_ocidesc(*var,&descr)) == 0) {
php_error(E_WARNING,"Variable must be allocated using OCINewDescriptor()");
RETURN_FALSE;
}
if (! (mydescr = (dvoid *) descr->ocidescr)) {
php_error(E_WARNING,"Descriptor empty");
RETURN_FALSE;
}
value_sz = sizeof(void*);
break;
case SQLT_RSET:
OCI_GET_STMT(bindstmt,var);
if (! (mystmt = bindstmt->pStmt)) {
RETURN_FALSE;
}
value_sz = sizeof(void*);
break;
}
if ((ocitype == SQLT_CHR) && (value_sz == -1)) {
convert_to_string_ex(var);
value_sz = (*var)->value.str.len;
}
if (value_sz == 0) {
value_sz = 1;
1999-04-22 06:49:16 +08:00
}
convert_to_string_ex(name);
1999-04-22 06:49:16 +08:00
if (! statement->binds) {
ALLOC_HASHTABLE(statement->binds);
zend_hash_init(statement->binds, 13, NULL, _oci_bind_hash_dtor, 0);
1999-04-22 06:49:16 +08:00
}
memset((void*)&bind,0,sizeof(oci_bind));
zend_hash_next_index_insert(statement->binds,&bind,sizeof(oci_bind),(void **)&bindp);
bindp->descr = mydescr;
bindp->pStmt = mystmt;
bindp->zval = *var;
zval_add_ref(var);
1999-04-22 06:49:16 +08:00
1999-05-12 22:28:01 +08:00
statement->error =
OCIBindByName(statement->pStmt, /* statement handle */
(OCIBind **)&bindp->pBind, /* bind hdl (will alloc) */
statement->pError, /* error handle */
(text*) (*name)->value.str.val, /* placeholder name */
(*name)->value.str.len, /* placeholder length */
(dvoid *)0, /* in/out data */
value_sz, /* OCI_MAX_DATA_SIZE, */ /* max size of input/output data */
(ub2)ocitype, /* in/out data type */
(dvoid *)&bindp->indicator, /* indicator (ignored) */
(ub2 *)0, /* size array (ignored) */
(ub2 *)&bindp->retcode, /* return code (ignored) */
(ub4)0, /* maxarr_len (PL/SQL only?) */
(ub4 *)0, /* actual array size (PL/SQL only?) */
#ifdef WITH_COLLECTIONS
mode /* mode */);
#else
OCI_DATA_AT_EXEC /* mode */);
#endif
1999-05-12 22:28:01 +08:00
if (statement->error != OCI_SUCCESS) {
1999-08-17 21:54:25 +08:00
oci_error(statement->pError, "OCIBindByName", statement->error);
oci_handle_error(statement->conn, statement->error);
1999-04-22 06:49:16 +08:00
RETURN_FALSE;
}
#ifdef WITH_COLLECTIONS
if(mode == OCI_DATA_AT_EXEC) {
#endif
1999-05-12 22:28:01 +08:00
statement->error =
OCIBindDynamic(bindp->pBind,
1999-05-12 22:28:01 +08:00
statement->pError,
(dvoid *)bindp,
1999-08-17 21:54:25 +08:00
oci_bind_in_callback,
(dvoid *)bindp,
1999-08-17 21:54:25 +08:00
oci_bind_out_callback);
1999-05-12 22:28:01 +08:00
if (statement->error != OCI_SUCCESS) {
1999-08-17 21:54:25 +08:00
oci_error(statement->pError, "OCIBindDynamic", statement->error);
oci_handle_error(statement->conn, statement->error);
1999-04-22 06:49:16 +08:00
RETURN_FALSE;
}
#ifdef WITH_COLLECTIONS
}
#endif
#ifdef WITH_COLLECTIONS
if(ocitype == SQLT_NTY) {
/* Bind object */
statement->error = OCIBindObject(bindp->pBind,
statement->pError,
coll->tdo,
(dvoid **) &(coll->coll),
(ub4 *) 0, (dvoid **) 0, (ub4 *) 0);
if (statement->error) {
oci_error(statement->pError, "OCIBindObject", statement->error);
RETURN_FALSE;
}
}
#endif
1999-04-22 06:49:16 +08:00
RETURN_TRUE;
}
/* }}} */
1999-04-22 06:49:16 +08:00
/* {{{ proto string ocifreedesc(object lob)
Deletes large object description */
1999-04-22 06:49:16 +08:00
PHP_FUNCTION(ocifreedesc)
1999-04-22 06:49:16 +08:00
{
zval *id;
int inx;
oci_descriptor *descriptor;
1999-04-22 06:49:16 +08:00
1999-07-12 19:49:32 +08:00
if ((id = getThis()) != 0) {
inx = _oci_get_ocidesc(id,&descriptor);
if (inx) {
oci_debug("OCIfreedesc: descr=%d",inx);
zend_list_delete(inx);
RETURN_TRUE;
1999-04-22 06:49:16 +08:00
}
}
php_error(E_NOTICE, "OCIFreeDesc() should not be called like this. Use $somelob->free() to free a LOB");
RETURN_FALSE;
1999-04-22 06:49:16 +08:00
}
/* }}} */
/* {{{ proto string ocisavelob(object lob)
Saves a large object */
1999-04-22 06:49:16 +08:00
PHP_FUNCTION(ocisavelob)
1999-04-22 06:49:16 +08:00
{
zval *id, **arg,**oarg;
1999-04-22 06:49:16 +08:00
OCILobLocator *mylob;
1999-08-17 21:54:25 +08:00
oci_connection *connection;
oci_descriptor *descr;
int offparam,inx;
1999-04-22 06:49:16 +08:00
ub4 loblen;
ub4 curloblen;
ub4 offset;
1999-04-22 06:49:16 +08:00
1999-07-12 19:49:32 +08:00
if ((id = getThis()) != 0) {
if ((inx = _oci_get_ocidesc(id,&descr)) == 0) {
1999-04-22 06:49:16 +08:00
RETURN_FALSE;
}
1999-04-22 06:49:16 +08:00
mylob = (OCILobLocator *) descr->ocidescr;
if (! mylob) {
RETURN_FALSE;
}
connection = descr->conn;
offset = 0;
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(2, &arg, &oarg) == SUCCESS) {
convert_to_long_ex(oarg);
offparam = (*oarg)->value.lval;
connection->error =
OCILobGetLength(connection->pServiceContext,
connection->pError,
mylob,
&curloblen);
oci_debug("OCIsavedesc: curloblen=%d",curloblen);
if (offparam == -1) {
offset = curloblen;
2001-05-31 01:49:33 +08:00
} else if ((ub4) offparam >= curloblen) {
php_error(E_WARNING, "Offset smaller than current LOB-Size - appending");
offset = curloblen;
} else {
offset = offparam;
}
1999-12-19 06:40:35 +08:00
} else if (zend_get_parameters_ex(1, &arg) == FAILURE) {
1999-04-22 06:49:16 +08:00
WRONG_PARAM_COUNT;
}
offset++;
convert_to_string_ex(arg);
loblen = (*arg)->value.str.len;
1999-04-22 06:49:16 +08:00
if (loblen < 1) {
php_error(E_WARNING, "Cannot save a lob wich size is less than 1 byte");
1999-04-22 06:49:16 +08:00
RETURN_FALSE;
}
1999-05-12 22:28:01 +08:00
connection->error =
OCILobWrite(connection->pServiceContext,
connection->pError,
mylob,
&loblen,
(ub4) offset,
(dvoid *) (*arg)->value.str.val,
(ub4) loblen,
OCI_ONE_PIECE,
(dvoid *)0,
(OCICallbackLobWrite) 0,
(ub2) 0,
(ub1) SQLCS_IMPLICIT );
oci_debug("OCIsavedesc: size=%d offset=%d",loblen,offset);
1999-04-22 06:49:16 +08:00
1999-05-12 22:28:01 +08:00
if (connection->error) {
1999-08-17 21:54:25 +08:00
oci_error(connection->pError, "OCILobWrite", connection->error);
oci_handle_error(connection, connection->error);
1999-04-22 06:49:16 +08:00
RETURN_FALSE;
}
RETURN_TRUE;
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto string ocisavelobfile(object lob)
Saves a large object file */
PHP_FUNCTION(ocisavelobfile)
{
zval *id, **arg;
OCILobLocator *mylob;
oci_connection *connection;
oci_descriptor *descr;
char *filename;
int fp,inx;
char buf[8192];
ub4 offset = 1;
ub4 loblen;
if ((id = getThis()) != 0) {
if ((inx = _oci_get_ocidesc(id,&descr)) == 0) {
RETURN_FALSE;
}
mylob = (OCILobLocator *) descr->ocidescr;
if (! mylob) {
RETURN_FALSE;
}
connection = descr->conn;
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(arg);
1999-12-18 03:16:50 +08:00
if (php_check_open_basedir((*arg)->value.str.val)) {
RETURN_FALSE;
}
filename = (*arg)->value.str.val;
if ((fp = VCWD_OPEN((filename, O_RDONLY|O_BINARY))) == -1) {
php_error(E_WARNING, "Can't open file %s", filename);
RETURN_FALSE;
}
while ((loblen = read(fp, &buf, sizeof(buf))) > 0) {
connection->error =
OCILobWrite(connection->pServiceContext,
connection->pError,
mylob,
&loblen,
(ub4) offset,
(dvoid *) &buf,
(ub4) loblen,
OCI_ONE_PIECE,
(dvoid *)0,
(OCICallbackLobWrite) 0,
(ub2) 0,
(ub1) SQLCS_IMPLICIT);
oci_debug("OCIsavelob: size=%d",loblen);
if (connection->error) {
oci_error(connection->pError, "OCILobWrite", connection->error);
oci_handle_error(connection, connection->error);
close(fp);
RETURN_FALSE;
}
offset += loblen;
}
close(fp);
RETURN_TRUE;
}
RETURN_FALSE;
}
1999-04-22 06:49:16 +08:00
/* }}} */
/* {{{ proto string ociloadlob(object lob)
Loads a large object */
1999-04-22 06:49:16 +08:00
PHP_FUNCTION(ociloadlob)
1999-04-22 06:49:16 +08:00
{
zval *id;
1999-08-17 21:54:25 +08:00
oci_descriptor *descr;
1999-04-22 06:49:16 +08:00
char *buffer;
int inx;
1999-04-22 06:49:16 +08:00
ub4 loblen;
1999-07-12 19:49:32 +08:00
if ((id = getThis()) != 0) {
if ((inx = _oci_get_ocidesc(id,&descr)) == 0) {
1999-04-22 06:49:16 +08:00
RETURN_FALSE;
}
if (!oci_loadlob(descr->conn,descr,&buffer,&loblen)) {
1999-04-22 06:49:16 +08:00
RETURN_STRINGL(buffer,loblen,0);
}
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto void ociwritelobtofile(object lob [, string filename] [, int start] [, int length])
Writes a large object into a file */
PHP_FUNCTION(ociwritelobtofile)
{
zval *id, **zfilename, **zstart, **zlength;
char *filename = NULL;
int start = -1;
ub4 length = -1;
oci_connection *connection;
oci_descriptor *descr;
char *buffer=0;
ub4 loblen;
int ac = ZEND_NUM_ARGS();
int fp = -1,inx;
OCILobLocator *mylob;
int coffs;
if ((id = getThis()) != 0) {
if ((inx = _oci_get_ocidesc(id,&descr)) == 0) {
RETURN_FALSE;
}
mylob = (OCILobLocator *) descr->ocidescr;
if (! mylob) {
RETURN_FALSE;
}
connection = descr->conn;
1999-12-19 06:40:35 +08:00
if (ac < 0 || ac > 3 || zend_get_parameters_ex(ac, &zfilename, &zstart, &zlength) == FAILURE) {
WRONG_PARAM_COUNT;
}
switch (ac) {
case 3:
convert_to_long_ex(zlength);
length = (*zlength)->value.lval;
case 2:
convert_to_long_ex(zstart);
start = (*zstart)->value.lval;
case 1:
convert_to_string_ex(zfilename);
filename = (*zfilename)->value.str.val;
}
if (filename && *filename) {
1999-12-18 03:16:50 +08:00
if (php_check_open_basedir(filename)) {
goto bail;
}
if ((fp = VCWD_OPEN((filename,O_CREAT | O_RDWR | O_BINARY | O_TRUNC, 0600))) == -1) {
php_error(E_WARNING, "Can't create file %s", filename);
goto bail;
}
}
connection->error =
OCILobGetLength(connection->pServiceContext,
connection->pError,
descr->ocidescr,
&loblen);
if (connection->error) {
oci_error(connection->pError, "OCILobGetLength", connection->error);
oci_handle_error(connection, connection->error);
goto bail;
}
if (descr->type == OCI_DTYPE_FILE) {
connection->error =
OCILobFileOpen(connection->pServiceContext,
connection->pError,
descr->ocidescr,
OCI_FILE_READONLY);
if (connection->error) {
oci_error(connection->pError, "OCILobFileOpen",connection->error);
oci_handle_error(connection, connection->error);
goto bail;
}
}
if (start == -1) {
start = 0;
}
if (length == -1) {
length = loblen - start;
}
if ((start + length) > loblen) {
length = loblen - start;
}
#define OCI_LOB_READ_BUFFER 128*1024
buffer = emalloc(OCI_LOB_READ_BUFFER);
coffs = start;
oci_debug("ociwritelobtofile(start = %d, length = %d, loblen = %d",start,length,loblen);
while (length > 0) {
ub4 toread;
if (length > OCI_LOB_READ_BUFFER) {
toread = OCI_LOB_READ_BUFFER;
} else {
toread = length;
}
oci_debug("OCILobRead(coffs = %d, toread = %d",coffs,toread);
connection->error =
OCILobRead(connection->pServiceContext,
connection->pError,
descr->ocidescr,
&toread, /* IN/OUT bytes toread/read */
coffs+1, /* offset (starts with 1) */
(dvoid *) buffer,
toread, /* size of buffer */
(dvoid *)0,
(OCICallbackLobRead) 0, /* callback... */
(ub2) 0, /* The character set ID of the buffer data. */
(ub1) SQLCS_IMPLICIT); /* The character set form of the buffer data. */
oci_debug("OCILobRead(read - %d",toread);
if (connection->error) {
oci_error(connection->pError, "OCILobRead", connection->error);
oci_handle_error(connection, connection->error);
goto bail;
}
if (fp != -1) {
2001-05-31 01:49:33 +08:00
if ((ub4) write(fp,buffer,toread) != toread) {
php_error(E_WARNING, "cannot write file!");
goto bail;
}
} else {
PHPWRITE(buffer,toread);
}
length -= toread;
coffs += toread;
}
efree(buffer);
buffer = 0;
if (fp != -1) {
close(fp);
fp = 0;
}
if (descr->type == OCI_DTYPE_FILE) {
connection->error =
OCILobFileClose(connection->pServiceContext,
connection->pError,
descr->ocidescr);
if (connection->error) {
oci_error(connection->pError, "OCILobFileClose", connection->error);
oci_handle_error(connection, connection->error);
goto bail;
}
}
RETURN_TRUE;
}
bail:
if (fp != -1) {
close(fp);
}
if (buffer) {
efree(buffer);
}
RETURN_FALSE;
}
/* }}} */
#ifdef HAVE_OCI8_TEMP_LOB
/* {{{ proto int ociwritetemporarylob(int stmt, int loc, string var)
Return the row count of an OCI statement */
PHP_FUNCTION(ociwritetemporarylob)
{
zval *id, **var;
OCILobLocator *mylob;
oci_connection *connection;
oci_descriptor *descr;
ub4 offset = 1;
ub4 loblen;
oci_debug ("oci_write_temporary_lob");
if ((id = getThis()) == 0) {
RETURN_FALSE;
}
if (_oci_get_ocidesc(id,&descr) == 0) {
RETURN_FALSE;
}
mylob = (OCILobLocator *) descr->ocidescr;
if (! mylob) {
RETURN_FALSE;
}
connection = descr->conn;
if (zend_get_parameters_ex(1, &var) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(var);
connection->error =
OCILobCreateTemporary(connection->pServiceContext,
connection->pError,
mylob,
OCI_DEFAULT,
OCI_DEFAULT,
OCI_TEMP_CLOB,
TRUE,
OCI_DURATION_SESSION);
if (connection->error) {
oci_error(connection->pError, "OCILobCreateTemporary", connection->error);
oci_handle_error(connection, connection->error);
RETURN_FALSE;
}
connection->error =
OCILobOpen(connection->pServiceContext,
connection->pError,
mylob,
OCI_LOB_READWRITE);
if (connection->error) {
oci_error(connection->pError, "OCILobOpen", connection->error);
oci_handle_error(connection, connection->error);
RETURN_FALSE;
}
convert_to_string_ex(var);
loblen = (*var)->value.str.len;
if (loblen < 1) {
php_error(E_WARNING, "Cannot save a lob wich size is less than 1 byte");
RETURN_FALSE;
}
connection->error =
OCILobWrite(connection->pServiceContext,
connection->pError,
mylob,
(ub4 *) &loblen,
(ub4) offset,
(dvoid *) (*var)->value.str.val,
(ub4) loblen,
OCI_ONE_PIECE,
(dvoid *)0,
(sb4 (*)(dvoid *, dvoid *, ub4 *, ub1 *)) 0,
(ub2) 0,
(ub1) SQLCS_IMPLICIT );
if (connection->error) {
oci_error(connection->pError, "OCILobWrite", connection->error);
oci_handle_error(connection, connection->error);
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto string ocicloselob(object lob)
Closes lob descriptor */
PHP_FUNCTION(ocicloselob)
{
zval *id;
int inx;
OCILobLocator *mylob;
oci_connection *connection;
oci_descriptor *descriptor;
if ((id = getThis()) != 0) {
inx = _oci_get_ocidesc(id,&descriptor);
if (inx) {
mylob = (OCILobLocator *) descriptor->ocidescr;
if (! mylob) {
RETURN_FALSE;
}
connection = descriptor->conn;
connection->error = OCILobClose (connection->pServiceContext,
connection->pError,
mylob);
if (connection->error) {
oci_error(connection->pError, "OCILobClose", connection->error);
oci_handle_error(connection, connection->error);
RETURN_FALSE;
}
oci_debug("oci_close_lob: descr=%d",inx);
RETURN_TRUE;
}
}
php_error(E_NOTICE, "OCICloselob() should not be called like this. Use $somelob->close() to close a LOB");
RETURN_FALSE;
}
/* }}} */
#endif
/* {{{ proto string ocinewdescriptor(int connection [, int type])
Initialize a new empty descriptor LOB/FILE (LOB is default) */
1999-04-22 06:49:16 +08:00
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocinewdescriptor)
1999-04-22 06:49:16 +08:00
{
zval **conn, **type;
1999-08-17 21:54:25 +08:00
oci_connection *connection;
oci_descriptor *descr;
int dtype;
1999-04-22 06:49:16 +08:00
dtype = OCI_DTYPE_LOB;
1999-04-22 06:49:16 +08:00
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(2, &conn, &type) == SUCCESS) {
convert_to_long_ex(type);
dtype = (*type)->value.lval;
1999-12-19 06:40:35 +08:00
} else if (zend_get_parameters_ex(1, &conn) == FAILURE) {
1999-04-22 06:49:16 +08:00
WRONG_PARAM_COUNT;
}
OCI_GET_CONN(connection,conn);
1999-04-22 06:49:16 +08:00
descr = oci_new_desc(dtype,connection);
1999-04-22 06:49:16 +08:00
object_init_ex(return_value, oci_lob_class_entry_ptr);
add_property_resource(return_value, "descriptor", descr->id);
1999-04-22 06:49:16 +08:00
}
/* }}} */
/* {{{ proto string ocirollback(int conn)
Rollback the current context */
1999-04-22 06:49:16 +08:00
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocirollback)
1999-04-22 06:49:16 +08:00
{
zval **conn;
1999-08-17 21:54:25 +08:00
oci_connection *connection;
1999-04-22 06:49:16 +08:00
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(1, &conn) == FAILURE) {
1999-04-22 06:49:16 +08:00
WRONG_PARAM_COUNT;
}
OCI_GET_CONN(connection,conn);
1999-04-22 06:49:16 +08:00
1999-05-12 22:28:01 +08:00
connection->error =
OCITransRollback(connection->pServiceContext,
connection->pError,
(ub4)0);
1999-05-12 22:28:01 +08:00
if (connection->error) {
1999-08-17 21:54:25 +08:00
oci_error(connection->pError, "OCIRollback", connection->error);
oci_handle_error(connection, connection->error);
1999-04-22 06:49:16 +08:00
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto string ocicommit(int conn)
Commit the current context */
1999-04-22 06:49:16 +08:00
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocicommit)
1999-04-22 06:49:16 +08:00
{
zval **conn;
1999-08-17 21:54:25 +08:00
oci_connection *connection;
1999-04-22 06:49:16 +08:00
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(1, &conn) == FAILURE) {
1999-04-22 06:49:16 +08:00
WRONG_PARAM_COUNT;
}
OCI_GET_CONN(connection,conn);
1999-04-22 06:49:16 +08:00
oci_debug("<OCITransCommit");
1999-05-12 22:28:01 +08:00
connection->error =
OCITransCommit(connection->pServiceContext,
connection->pError,
(ub4)0);
1999-04-22 06:49:16 +08:00
oci_debug(">OCITransCommit");
1999-05-12 22:28:01 +08:00
if (connection->error) {
1999-08-17 21:54:25 +08:00
oci_error(connection->pError, "OCICommit", connection->error);
oci_handle_error(connection, connection->error);
1999-04-22 06:49:16 +08:00
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto string ocicolumnname(int stmt, int col)
Tell the name of a column */
1999-04-22 06:49:16 +08:00
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocicolumnname)
1999-04-22 06:49:16 +08:00
{
zval **stmt, **col;
1999-08-17 21:54:25 +08:00
oci_statement *statement;
oci_out_column *outcol;
1999-04-22 06:49:16 +08:00
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(2, &stmt, &col) == FAILURE) {
1999-04-22 06:49:16 +08:00
WRONG_PARAM_COUNT;
}
OCI_GET_STMT(statement,stmt);
outcol = oci_get_col(statement, -1, col);
1999-04-22 06:49:16 +08:00
if (outcol == NULL) {
RETURN_FALSE;
}
RETURN_STRINGL(outcol->name, outcol->name_len, 1);
}
/* }}} */
/* {{{ proto int ocicolumnsize(int stmt, int col)
Tell the maximum data size of a column */
1999-04-22 06:49:16 +08:00
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocicolumnsize)
1999-04-22 06:49:16 +08:00
{
zval **stmt, **col;
1999-08-17 21:54:25 +08:00
oci_statement *statement;
oci_out_column *outcol;
1999-04-22 06:49:16 +08:00
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(2, &stmt, &col) == FAILURE) {
1999-04-22 06:49:16 +08:00
WRONG_PARAM_COUNT;
}
OCI_GET_STMT(statement,stmt);
outcol = oci_get_col(statement, -1, col);
1999-04-22 06:49:16 +08:00
if (outcol == NULL) {
RETURN_FALSE;
}
oci_debug("ocicolumnsize: %16s, retlen = %4d, retlen4 = %d, data_size = %4d, storage_size4 = %4d, indicator %4d, retcode = %4d",
outcol->name,outcol->retlen,outcol->retlen4,outcol->data_size,outcol->storage_size4,outcol->indicator,outcol->retcode);
/* Handle data type of LONG */
if(outcol->data_type == SQLT_LNG){
RETURN_LONG(outcol->storage_size4);
}else{
RETURN_LONG(outcol->data_size);
}
}
/* }}} */
/* {{{ proto int ocicolumnscale(int stmt, int col)
Tell the scale of a column */
PHP_FUNCTION(ocicolumnscale)
{
zval **stmt, **col;
oci_statement *statement;
oci_out_column *outcol;
if (zend_get_parameters_ex(2, &stmt, &col) == FAILURE) {
WRONG_PARAM_COUNT;
}
OCI_GET_STMT(statement,stmt);
outcol = oci_get_col(statement, -1, col);
if (outcol == NULL) {
RETURN_FALSE;
}
RETURN_LONG(outcol->scale);
}
/* }}} */
/* {{{ proto int ocicolumnprecision(int stmt, int col)
Tell the precision of a column */
PHP_FUNCTION(ocicolumnprecision)
{
zval **stmt, **col;
oci_statement *statement;
oci_out_column *outcol;
if (zend_get_parameters_ex(2, &stmt, &col) == FAILURE) {
WRONG_PARAM_COUNT;
}
OCI_GET_STMT(statement,stmt);
outcol = oci_get_col(statement, -1, col);
if (outcol == NULL) {
RETURN_FALSE;
}
RETURN_LONG(outcol->precision);
1999-04-22 06:49:16 +08:00
}
/* }}} */
2000-03-15 05:47:47 +08:00
/* {{{ proto mixed ocicolumntype(int stmt, int col)
Tell the data type of a column */
1999-04-22 06:49:16 +08:00
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocicolumntype)
1999-04-22 06:49:16 +08:00
{
zval **stmt, **col;
1999-08-17 21:54:25 +08:00
oci_statement *statement;
oci_out_column *outcol;
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(2, &stmt, &col) == FAILURE) {
1999-04-22 06:49:16 +08:00
WRONG_PARAM_COUNT;
}
OCI_GET_STMT(statement,stmt);
outcol = oci_get_col(statement, -1, col);
1999-04-22 06:49:16 +08:00
if (outcol == NULL) {
RETURN_FALSE;
}
1999-07-08 22:12:07 +08:00
switch (outcol->data_type) {
1999-04-22 06:49:16 +08:00
case SQLT_DAT:
RETVAL_STRING("DATE",1);
break;
case SQLT_NUM:
RETVAL_STRING("NUMBER",1);
break;
case SQLT_LNG:
RETVAL_STRING("LONG",1);
break;
case SQLT_BIN:
RETVAL_STRING("RAW",1);
break;
case SQLT_LBI:
RETVAL_STRING("LONG RAW",1);
break;
case SQLT_CHR:
RETVAL_STRING("VARCHAR",1);
break;
1999-05-12 22:28:01 +08:00
case SQLT_RSET:
RETVAL_STRING("REFCURSOR",1);
break;
1999-04-22 06:49:16 +08:00
case SQLT_AFC:
RETVAL_STRING("CHAR",1);
break;
case SQLT_BLOB:
RETVAL_STRING("BLOB",1);
break;
case SQLT_CLOB:
RETVAL_STRING("CLOB",1);
break;
case SQLT_BFILE:
RETVAL_STRING("BFILE",1);
break;
case SQLT_RDD:
RETVAL_STRING("ROWID",1);
break;
default:
1999-07-08 22:12:07 +08:00
RETVAL_LONG(outcol->data_type);
1999-04-22 06:49:16 +08:00
}
}
/* }}} */
2000-03-15 05:47:47 +08:00
/* {{{ proto mixed ocicolumntyperaw(int stmt, int col)
Tell the raw oracle data type of a column */
PHP_FUNCTION(ocicolumntyperaw)
{
zval **stmt, **col;
oci_statement *statement;
oci_out_column *outcol;
if (zend_get_parameters_ex(2, &stmt, &col) == FAILURE) {
WRONG_PARAM_COUNT;
}
OCI_GET_STMT(statement,stmt);
outcol = oci_get_col(statement, -1, col);
if (outcol == NULL) {
RETURN_FALSE;
}
RETVAL_LONG(outcol->data_type);
}
1999-04-22 06:49:16 +08:00
/* }}} */
/* {{{ proto int ocicolumnisnull(int stmt, int col)
Tell whether a column is NULL */
1999-04-22 06:49:16 +08:00
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocicolumnisnull)
1999-04-22 06:49:16 +08:00
{
zval **stmt, **col;
1999-08-17 21:54:25 +08:00
oci_statement *statement;
oci_out_column *outcol;
1999-04-22 06:49:16 +08:00
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(2, &stmt, &col) == FAILURE) {
1999-04-22 06:49:16 +08:00
WRONG_PARAM_COUNT;
}
OCI_GET_STMT(statement,stmt);
outcol = oci_get_col(statement, -1, col);
1999-04-22 06:49:16 +08:00
if (outcol == NULL) {
RETURN_FALSE;
}
if (outcol->indicator == -1) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
2000-05-24 06:30:21 +08:00
/* {{{ proto void ociinternaldebug(int onoff)
Toggle internal debugging output for the OCI extension */
1999-04-22 06:49:16 +08:00
/* Disables or enables the internal debug output.
* By default it is disabled.
*/
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ociinternaldebug)
1999-04-22 06:49:16 +08:00
{
zval **arg;
1999-08-17 21:54:25 +08:00
OCILS_FETCH();
1999-04-22 06:49:16 +08:00
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(1, &arg) == FAILURE) {
1999-04-22 06:49:16 +08:00
WRONG_PARAM_COUNT;
}
convert_to_long_ex(arg);
OCI(debug_mode) = (*arg)->value.lval;
1999-04-22 06:49:16 +08:00
}
/* }}} */
/* {{{ proto int ociexecute(int stmt [, int mode])
Execute a parsed statement */
1999-04-22 06:49:16 +08:00
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ociexecute)
1999-04-22 06:49:16 +08:00
{
zval **stmt,**mode;
1999-08-17 21:54:25 +08:00
oci_statement *statement;
1999-04-22 06:49:16 +08:00
ub4 execmode;
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(2, &stmt, &mode) == SUCCESS) {
convert_to_long_ex(mode);
execmode = (*mode)->value.lval;
1999-12-19 06:40:35 +08:00
} else if (zend_get_parameters_ex(1, &stmt) == SUCCESS) {
1999-04-22 06:49:16 +08:00
execmode = OCI_COMMIT_ON_SUCCESS;
} else {
WRONG_PARAM_COUNT;
}
OCI_GET_STMT(statement,stmt);
1999-04-22 06:49:16 +08:00
if (oci_execute(statement, "OCIExecute",execmode)) {
1999-05-12 22:28:01 +08:00
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto int ocicancel(int stmt)
Prepare a new row of data for reading */
1999-05-12 22:28:01 +08:00
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocicancel)
1999-05-12 22:28:01 +08:00
{
zval **stmt;
1999-08-17 21:54:25 +08:00
oci_statement *statement;
1999-05-12 22:28:01 +08:00
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(1, &stmt) == FAILURE) {
1999-05-12 22:28:01 +08:00
WRONG_PARAM_COUNT;
}
OCI_GET_STMT(statement,stmt);
1999-08-17 21:54:25 +08:00
if (oci_fetch(statement, 0, "OCICancel")) {
1999-04-22 06:49:16 +08:00
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto int ocifetch(int stmt)
Prepare a new row of data for reading */
1999-04-22 06:49:16 +08:00
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocifetch)
1999-04-22 06:49:16 +08:00
{
zval **stmt;
1999-08-17 21:54:25 +08:00
oci_statement *statement;
1999-04-22 06:49:16 +08:00
ub4 nrows = 1; /* only one row at a time is supported for now */
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(1, &stmt) == FAILURE) {
1999-04-22 06:49:16 +08:00
WRONG_PARAM_COUNT;
}
OCI_GET_STMT(statement,stmt);
1999-08-17 21:54:25 +08:00
if (oci_fetch(statement, nrows, "OCIFetch")) {
1999-04-22 06:49:16 +08:00
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto int ocifetchinto(int stmt, array &output [, int mode])
Fetch a row of result data into an array */
1999-04-22 06:49:16 +08:00
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocifetchinto)
1999-04-22 06:49:16 +08:00
{
zval **stmt, **array, *element, **fmode;
1999-08-17 21:54:25 +08:00
oci_statement *statement;
oci_out_column *column;
1999-04-22 06:49:16 +08:00
ub4 nrows = 1;
int i, used;
1999-04-22 06:49:16 +08:00
int mode = OCI_NUM;
int ac = ZEND_NUM_ARGS();
1999-12-19 06:40:35 +08:00
if (ac < 2 || ac > 3 || zend_get_parameters_ex(ac, &stmt, &array, &fmode) == FAILURE) {
WRONG_PARAM_COUNT;
}
switch (ac) {
case 3:
convert_to_long_ex(fmode);
mode = (*fmode)->value.lval;
/* possible breakthru */
1999-04-22 06:49:16 +08:00
}
OCI_GET_STMT(statement,stmt);
1999-04-22 06:49:16 +08:00
1999-08-17 21:54:25 +08:00
if (!oci_fetch(statement, nrows, "OCIFetchInto")) {
1999-04-22 06:49:16 +08:00
RETURN_FALSE;
}
zval_dtor(*array);
if (array_init(*array) == FAILURE) {
php_error(E_WARNING, "OCIFetchInto: unable to convert arg 2 to array");
RETURN_FALSE;
1999-04-22 06:49:16 +08:00
}
for (i = 0; i < statement->ncolumns; i++) {
column = oci_get_col(statement, i + 1, 0);
if (column == NULL) {
1999-04-22 06:49:16 +08:00
continue;
}
1999-04-22 06:49:16 +08:00
if ((column->indicator == -1) && ((mode & OCI_RETURN_NULLS) == 0)) {
continue;
}
used = 0;
MAKE_STD_ZVAL(element);
_oci_make_zval(element,statement,column,"OCIFetchInto",mode);
if ((mode & OCI_NUM) || (! (mode & OCI_ASSOC))) {
zend_hash_index_update((*array)->value.ht,i,(void *)&element,sizeof(zval*),NULL);
used=1;
1999-04-22 06:49:16 +08:00
}
if (mode & OCI_ASSOC) {
if (used) {
element->refcount++;
}
zend_hash_update((*array)->value.ht,column->name,column->name_len+1,(void *)&element,sizeof(zval*),NULL);
1999-04-22 06:49:16 +08:00
}
}
RETURN_LONG(statement->ncolumns);
}
/* }}} */
/* {{{ proto int ocifetchstatement(int stmt, array &output)
Fetch all rows of result data into an array */
1999-04-22 06:49:16 +08:00
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocifetchstatement)
1999-04-22 06:49:16 +08:00
{
zval **stmt, **array, *element, **fmode, *tmp;
1999-08-17 21:54:25 +08:00
oci_statement *statement;
oci_out_column **columns;
zval ***outarrs;
1999-04-22 06:49:16 +08:00
ub4 nrows = 1;
int i;
int mode = OCI_NUM;
int rows = 0;
char *namebuf;
int ac = ZEND_NUM_ARGS();
1999-04-22 06:49:16 +08:00
1999-12-19 06:40:35 +08:00
if (ac < 2 || ac > 3 || zend_get_parameters_ex(ac, &stmt, &array, &fmode) == FAILURE) {
1999-04-22 06:49:16 +08:00
WRONG_PARAM_COUNT;
}
switch (ac) {
case 3:
convert_to_long_ex(fmode);
mode = (*fmode)->value.lval;
/* possible breakthru */
1999-04-22 06:49:16 +08:00
}
OCI_GET_STMT(statement,stmt);
zval_dtor(*array);
array_init(*array);
1999-04-22 06:49:16 +08:00
1999-08-17 21:54:25 +08:00
columns = emalloc(statement->ncolumns * sizeof(oci_out_column *));
outarrs = emalloc(statement->ncolumns * sizeof(zval*));
1999-04-22 06:49:16 +08:00
for (i = 0; i < statement->ncolumns; i++) {
columns[ i ] = oci_get_col(statement, i + 1, 0);
1999-07-12 19:49:32 +08:00
MAKE_STD_ZVAL(tmp);
1999-06-09 18:40:08 +08:00
array_init(tmp);
1999-04-22 06:49:16 +08:00
namebuf = estrndup(columns[ i ]->name,columns[ i ]->name_len);
1999-04-22 06:49:16 +08:00
zend_hash_update((*array)->value.ht, namebuf, columns[ i ]->name_len+1, (void *) &tmp, sizeof(zval*), (void **) &(outarrs[ i ]));
efree(namebuf);
1999-04-22 06:49:16 +08:00
}
1999-08-17 21:54:25 +08:00
while (oci_fetch(statement, nrows, "OCIFetchStatement")) {
1999-04-22 06:49:16 +08:00
for (i = 0; i < statement->ncolumns; i++) {
1999-07-12 19:49:32 +08:00
MAKE_STD_ZVAL(element);
1999-09-13 19:13:53 +08:00
_oci_make_zval(element,statement,columns[ i ], "OCIFetchStatement",OCI_RETURN_LOBS);
1999-06-09 18:40:08 +08:00
zend_hash_index_update((*(outarrs[ i ]))->value.ht, rows, (void *)&element, sizeof(zval*), NULL);
1999-04-22 06:49:16 +08:00
}
rows++;
}
efree(columns);
efree(outarrs);
RETURN_LONG(rows);
}
/* }}} */
/* {{{ proto int ocifreestatement(int stmt)
Free all resources associated with a statement */
1999-04-22 06:49:16 +08:00
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocifreestatement)
1999-04-22 06:49:16 +08:00
{
zval **stmt;
1999-08-17 21:54:25 +08:00
oci_statement *statement;
1999-04-22 06:49:16 +08:00
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(1, &stmt) == FAILURE) {
1999-04-22 06:49:16 +08:00
WRONG_PARAM_COUNT;
}
OCI_GET_STMT(statement,stmt);
zend_list_delete(statement->id);
1999-04-22 06:49:16 +08:00
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int ocilogoff(int conn)
Disconnect from database */
1999-04-22 06:49:16 +08:00
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocilogoff)
1999-04-22 06:49:16 +08:00
{
#if 0
this function does nothing any more. server-connections get automagiclly closed on
request-end. connection handles will "dissappear" as soon as they are no longer
referenced. as this module makes heavy use of zends reference-counting mechanism
this is the desired behavior. it has always been a bad idea to close a connection that
has outstanding transactions. this way we have a nice-clean approach.
2000-10-29 17:14:55 +08:00
(thies@thieso.net 20000110)
1999-08-17 21:54:25 +08:00
oci_connection *connection;
zval **conn;
1999-04-22 06:49:16 +08:00
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(1, &conn) == FAILURE) {
1999-04-22 06:49:16 +08:00
WRONG_PARAM_COUNT;
}
OCI_GET_CONN(connection,conn);
1999-04-22 06:49:16 +08:00
2001-05-04 22:53:45 +08:00
connection->is_open = 0;
1999-06-09 18:40:08 +08:00
zend_hash_apply(list,(int (*)(void *))_stmt_cleanup);
1999-06-09 18:40:08 +08:00
if (zend_list_delete(connection->id) == SUCCESS) {
1999-04-22 06:49:16 +08:00
RETURN_TRUE;
} else {
RETURN_FALSE;
}
#endif
1999-04-22 06:49:16 +08:00
}
1999-05-12 22:28:01 +08:00
/* }}} */
/* {{{ proto int ocinlogon(string user, string pass [, string db])
Connect to an Oracle database and log on. returns a new session */
1999-05-12 22:28:01 +08:00
/* Connects to an Oracle 8 database and logs on. If the
* optional third parameter is not specified, PHP uses the environment
* variable ORACLE_SID to determine which database to connect to.
*/
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocinlogon)
1999-05-12 22:28:01 +08:00
{
1999-08-17 21:54:25 +08:00
oci_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,0,1);
1999-05-12 22:28:01 +08:00
}
1999-04-22 06:49:16 +08:00
/* }}} */
/* {{{ proto int ocilogon(string user, string pass [, string db])
Connect to an Oracle database and log on. Returns a new session.
1999-04-22 06:49:16 +08:00
*/
/* Connects to an Oracle 8 database and logs on. If the
* optional third parameter is not specified, PHP uses the environment
* variable ORACLE_SID to determine which database to connect to.
*/
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocilogon)
1999-04-22 06:49:16 +08:00
{
1999-08-17 21:54:25 +08:00
oci_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,0,0);
1999-04-22 06:49:16 +08:00
}
/* }}} */
/* {{{ proto int ociplogon(string user, string pass [, string db])
Connect to an Oracle database using a persistent connection and log on. Returns a new session. */
1999-04-22 06:49:16 +08:00
/* Connects to an Oracle 8 database and logs on. If the
* optional third parameter is not specified, PHP uses the environment
* variable ORACLE_SID to determine which database to connect to.
*/
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ociplogon)
1999-04-22 06:49:16 +08:00
{
1999-08-17 21:54:25 +08:00
oci_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,1,0);
1999-04-22 06:49:16 +08:00
}
/* }}} */
/* {{{ proto array ocierror([int stmt|conn|global])
Return the last error of stmt|conn|global. If no error happened returns false. */
1999-04-22 06:49:16 +08:00
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocierror)
1999-04-22 06:49:16 +08:00
{
zval **arg;
1999-08-17 21:54:25 +08:00
oci_statement *statement;
oci_connection *connection;
1999-04-22 06:49:16 +08:00
text errbuf[512];
1999-06-09 18:40:08 +08:00
sb4 errcode = 0;
1999-05-12 22:28:01 +08:00
sword error = 0;
1999-04-22 06:49:16 +08:00
dvoid *errh = NULL;
1999-08-17 21:54:25 +08:00
OCILS_FETCH();
1999-04-22 06:49:16 +08:00
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(1, &arg) == SUCCESS) {
statement = (oci_statement *) zend_fetch_resource(arg, -1, NULL, NULL, 1, le_stmt);
if (statement) {
1999-05-12 22:28:01 +08:00
errh = statement->pError;
error = statement->error;
} else {
connection = (oci_connection *) zend_fetch_resource(arg, -1, NULL, NULL, 1, le_conn);
if (connection) {
1999-05-12 22:28:01 +08:00
errh = connection->pError;
error = connection->error;
}
}
} else {
1999-08-17 21:54:25 +08:00
errh = OCI(pError);
error = OCI(error);
1999-04-22 06:49:16 +08:00
}
1999-05-12 22:28:01 +08:00
if (! error) { /* no error set in the handle */
RETURN_FALSE;
1999-04-22 06:49:16 +08:00
}
if (! errh) {
php_error(E_WARNING, "OCIError: unable to find Error handle");
1999-04-22 06:49:16 +08:00
RETURN_FALSE;
}
OCIErrorGet(errh,1,NULL,&errcode,errbuf,(ub4)sizeof(errbuf),(ub4)OCI_HTYPE_ERROR);
if (errcode) {
array_init(return_value);
add_assoc_long(return_value, "code", errcode);
1999-06-09 18:40:08 +08:00
add_assoc_string(return_value, "message", (char*) errbuf, 1);
1999-04-22 06:49:16 +08:00
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto int ocinumcols(int stmt)
Return the number of result columns in a statement */
1999-04-22 06:49:16 +08:00
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocinumcols)
1999-04-22 06:49:16 +08:00
{
zval **stmt;
1999-08-17 21:54:25 +08:00
oci_statement *statement;
1999-04-22 06:49:16 +08:00
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(1, &stmt) == FAILURE) {
1999-04-22 06:49:16 +08:00
WRONG_PARAM_COUNT;
}
OCI_GET_STMT(statement,stmt);
1999-04-22 06:49:16 +08:00
RETURN_LONG(statement->ncolumns);
}
/* }}} */
/* {{{ proto int ociparse(int conn, string query)
Parse a query and return a statement */
1999-04-22 06:49:16 +08:00
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ociparse)
1999-04-22 06:49:16 +08:00
{
zval **conn, **query;
1999-08-17 21:54:25 +08:00
oci_connection *connection;
oci_statement *statement;
1999-04-22 06:49:16 +08:00
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(2, &conn, &query) == FAILURE) {
1999-04-22 06:49:16 +08:00
WRONG_PARAM_COUNT;
}
OCI_GET_CONN(connection,conn);
1999-04-22 06:49:16 +08:00
convert_to_string_ex(query);
statement = oci_parse(connection,(*query)->value.str.val,(*query)->value.str.len);
1999-04-22 06:49:16 +08:00
if (statement) {
RETURN_RESOURCE(statement->id);
} else {
RETURN_FALSE;
}
1999-07-08 22:12:07 +08:00
}
/* }}} */
/* {{{ proto int ocisetprefetch(int conn, string query)
Set a prefetch query??? */
1999-07-08 22:12:07 +08:00
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocisetprefetch)
1999-07-08 22:12:07 +08:00
{
zval **stmt, **size;
1999-08-17 21:54:25 +08:00
oci_statement *statement;
1999-07-08 22:12:07 +08:00
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(2, &stmt, &size) == FAILURE) {
1999-07-08 22:12:07 +08:00
WRONG_PARAM_COUNT;
}
convert_to_long_ex(size);
1999-07-08 22:12:07 +08:00
OCI_GET_STMT(statement,stmt);
1999-07-08 22:12:07 +08:00
oci_setprefetch(statement,(*size)->value.lval);
1999-07-08 22:12:07 +08:00
RETURN_TRUE;
1999-04-22 06:49:16 +08:00
}
1999-05-12 22:28:01 +08:00
/* }}} */
/* {{{ proto int ocinewcursor(int conn)
Return a new cursor (Statement-Handle) - use this to bind ref-cursors! */
1999-05-12 22:28:01 +08:00
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocinewcursor)
1999-05-12 22:28:01 +08:00
{
zval **conn;
1999-08-17 21:54:25 +08:00
oci_connection *connection;
oci_statement *statement;
1999-05-12 22:28:01 +08:00
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(1, &conn) == FAILURE) {
1999-05-12 22:28:01 +08:00
WRONG_PARAM_COUNT;
}
OCI_GET_CONN(connection,conn);
1999-05-12 22:28:01 +08:00
statement = oci_parse(connection,0,0);
RETURN_RESOURCE(statement->id);
1999-05-12 22:28:01 +08:00
}
1999-04-22 06:49:16 +08:00
/* }}} */
/* {{{ proto string ociresult(int stmt, mixed column)
Return a single column of result data */
1999-04-22 06:49:16 +08:00
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ociresult)
1999-04-22 06:49:16 +08:00
{
zval **stmt, **col;
1999-08-17 21:54:25 +08:00
oci_statement *statement;
oci_out_column *outcol = NULL;
1999-04-22 06:49:16 +08:00
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(2, &stmt, &col) == FAILURE) {
1999-04-22 06:49:16 +08:00
WRONG_PARAM_COUNT;
}
OCI_GET_STMT(statement,stmt);
1999-04-22 06:49:16 +08:00
outcol = oci_get_col(statement, -1, col);
1999-04-22 06:49:16 +08:00
if (outcol == NULL) {
RETURN_FALSE;
}
_oci_make_zval(return_value,statement,outcol, "OCIResult",0);
1999-04-22 06:49:16 +08:00
}
/* }}} */
/* {{{ proto string ociserverversion(int conn)
Return a string containing server version information */
1999-04-22 06:49:16 +08:00
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ociserverversion)
1999-04-22 06:49:16 +08:00
{
1999-08-17 21:54:25 +08:00
oci_connection *connection;
zval **conn;
1999-04-22 06:49:16 +08:00
char version[256];
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(1, &conn) == FAILURE) {
1999-04-22 06:49:16 +08:00
WRONG_PARAM_COUNT;
}
OCI_GET_CONN(connection,conn);
1999-05-12 22:28:01 +08:00
connection->error =
OCIServerVersion(connection->pServiceContext,
connection->pError,
1999-06-09 18:40:08 +08:00
(text*)version,
1999-05-12 22:28:01 +08:00
sizeof(version),
OCI_HTYPE_SVCCTX);
1999-05-12 22:28:01 +08:00
if (connection->error != OCI_SUCCESS) {
1999-08-17 21:54:25 +08:00
oci_error(connection->pError, "OCIServerVersion", connection->error);
oci_handle_error(connection, connection->error);
1999-04-22 06:49:16 +08:00
RETURN_FALSE;
}
1999-04-22 06:49:16 +08:00
RETURN_STRING(version,1);
}
/* }}} */
/* {{{ proto int ocistatementtype(int stmt)
Return the query type of an OCI statement */
1999-06-09 18:40:08 +08:00
1999-04-22 06:49:16 +08:00
/* XXX it would be better with a general interface to OCIAttrGet() */
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocistatementtype)
1999-04-22 06:49:16 +08:00
{
zval **stmt;
1999-08-17 21:54:25 +08:00
oci_statement *statement;
1999-04-22 06:49:16 +08:00
ub2 stmttype;
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(1, &stmt) == FAILURE) {
1999-04-22 06:49:16 +08:00
WRONG_PARAM_COUNT;
}
OCI_GET_STMT(statement,stmt);
1999-04-22 06:49:16 +08:00
1999-05-12 22:28:01 +08:00
statement->error =
OCIAttrGet((dvoid *)statement->pStmt,
OCI_HTYPE_STMT,
(ub2 *)&stmttype,
(ub4 *)0,
OCI_ATTR_STMT_TYPE,
statement->pError);
if (statement->error != OCI_SUCCESS) {
1999-08-17 21:54:25 +08:00
oci_error(statement->pError, "OCIStatementType", statement->error);
oci_handle_error(statement->conn, statement->error);
1999-04-22 06:49:16 +08:00
RETURN_FALSE;
}
switch (stmttype) {
case OCI_STMT_SELECT:
RETVAL_STRING("SELECT",1);
break;
case OCI_STMT_UPDATE:
RETVAL_STRING("UPDATE",1);
break;
case OCI_STMT_DELETE:
RETVAL_STRING("DELETE",1);
break;
case OCI_STMT_INSERT:
RETVAL_STRING("INSERT",1);
break;
case OCI_STMT_CREATE:
RETVAL_STRING("CREATE",1);
break;
case OCI_STMT_DROP:
RETVAL_STRING("DROP",1);
break;
case OCI_STMT_ALTER:
RETVAL_STRING("ALTER",1);
break;
case OCI_STMT_BEGIN:
RETVAL_STRING("BEGIN",1);
break;
case OCI_STMT_DECLARE:
RETVAL_STRING("DECLARE",1);
break;
default:
RETVAL_STRING("UNKNOWN",1);
}
}
/* }}} */
1999-04-22 06:49:16 +08:00
/* {{{ proto int ocirowcount(int stmt)
Return the row count of an OCI statement */
1999-08-10 22:33:59 +08:00
PHP_FUNCTION(ocirowcount)
1999-04-22 06:49:16 +08:00
{
zval **stmt;
1999-08-17 21:54:25 +08:00
oci_statement *statement;
1999-04-22 06:49:16 +08:00
ub4 rowcount;
1999-12-19 06:40:35 +08:00
if (zend_get_parameters_ex(1, &stmt) == FAILURE) {
1999-04-22 06:49:16 +08:00
WRONG_PARAM_COUNT;
}
OCI_GET_STMT(statement,stmt);
1999-04-22 06:49:16 +08:00
1999-05-12 22:28:01 +08:00
statement->error =
OCIAttrGet((dvoid *)statement->pStmt,
OCI_HTYPE_STMT,
(ub2 *)&rowcount,
(ub4 *)0,
OCI_ATTR_ROW_COUNT,
statement->pError);
1999-04-22 06:49:16 +08:00
1999-05-12 22:28:01 +08:00
if (statement->error != OCI_SUCCESS) {
1999-08-17 21:54:25 +08:00
oci_error(statement->pError, "OCIRowCount", statement->error);
oci_handle_error(statement->conn, statement->error);
1999-04-22 06:49:16 +08:00
RETURN_FALSE;
}
RETURN_LONG(rowcount);
}
/* }}} */
#ifdef WITH_COLLECTIONS
/* {{{ oci_get_coll() */
static oci_collection *oci_get_coll(int ind)
{
oci_collection *collection;
int actual_resource_type;
collection = (oci_collection *) zend_list_find(ind, &actual_resource_type);
if (collection && (actual_resource_type == le_coll)) {
return collection;
} else {
return (oci_collection *) NULL;
}
}
/* }}} */
/* {{{ proto string ocifreecollection(object lob)
Deletes collection object*/
PHP_FUNCTION(ocifreecollection)
{
zval *id;
int inx;
oci_collection *coll;
oci_connection *connection;
2001-05-31 01:43:32 +08:00
OCILS_FETCH();
if ((id = getThis()) != 0) {
inx = _oci_get_ocicoll(id,&coll);
if (inx) {
/*
* Do we need to free the object?
*
*/
connection = coll->conn;
oci_debug("OCIfreecollection: coll=%d",inx);
connection->error = OCIObjectFree(OCI(pEnv),
connection->pError,
(dvoid *)coll->coll,
(ub2)(OCI_OBJECTFREE_FORCE));
if (connection->error) {
oci_error(connection->pError, "OCIObjectFree", connection->error);
RETURN_FALSE;
}
zend_list_delete(inx);
RETURN_TRUE;
}
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto string ocicollappend(object collection,value)
Append an object to the collection */
PHP_FUNCTION(ocicollappend)
{
zval *id, **arg;
oci_connection *connection;
oci_collection *coll;
OCINumber num;
OCIString *ocistr = (OCIString *)0;
OCIInd new_ind = OCI_IND_NOTNULL;
OCIDate dt;
int inx;
double ndx;
OCILS_FETCH();
if ((id = getThis()) != 0) {
if ((inx = _oci_get_ocicoll(id,&coll)) == 0) {
RETURN_FALSE;
}
connection = coll->conn;
if (zend_get_parameters_ex(1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
switch(coll->element_typecode) {
case OCI_TYPECODE_DATE:
convert_to_string_ex(arg);
connection->error = OCIDateFromText(connection->pError,
(*arg)->value.str.val,
(*arg)->value.str.len,
0,0,0,0,&dt);
if (connection->error) {
oci_error(connection->pError, "OCIDateFromText", connection->error);
RETURN_FALSE;
}
connection->error = OCICollAppend(OCI(pEnv),
connection->pError,
(dvoid *) &dt,
(dvoid *) &new_ind,
(OCIColl *) coll->coll);
if (connection->error) {
oci_error(connection->pError, "OCICollAppend", connection->error);
RETURN_FALSE;
}
RETURN_TRUE;
break;
case OCI_TYPECODE_VARCHAR2 :
convert_to_string_ex(arg);
connection->error = OCIStringAssignText(OCI(pEnv),
connection->pError,
(*arg)->value.str.val,
(*arg)->value.str.len,
&ocistr);
if (connection->error) {
oci_error(connection->pError, "OCIStringAssignText", connection->error);
RETURN_FALSE;
}
connection->error = OCICollAppend(OCI(pEnv),
connection->pError,
(dvoid *) ocistr,
(dvoid *) &new_ind,
(OCIColl *) coll->coll);
if (connection->error) {
oci_error(connection->pError, "OCICollAppend", connection->error);
RETURN_FALSE;
}
RETURN_TRUE;
break;
case OCI_TYPECODE_UNSIGNED16 : /* UNSIGNED SHORT */
case OCI_TYPECODE_UNSIGNED32 : /* UNSIGNED LONG */
case OCI_TYPECODE_REAL : /* REAL */
case OCI_TYPECODE_DOUBLE : /* DOUBLE */
case OCI_TYPECODE_INTEGER : /* INT */
case OCI_TYPECODE_SIGNED16 : /* SHORT */
case OCI_TYPECODE_SIGNED32 : /* LONG */
case OCI_TYPECODE_DECIMAL : /* DECIMAL */
case OCI_TYPECODE_FLOAT : /* FLOAT */
case OCI_TYPECODE_NUMBER : /* NUMBER */
case OCI_TYPECODE_SMALLINT : /* SMALLINT */
convert_to_double_ex(arg);
ndx = (double)(*arg)->value.dval;
connection->error = OCINumberFromReal(connection->pError,&ndx,
sizeof(double),&num);
if (connection->error) {
oci_error(connection->pError, "OCINumberFromReal", connection->error);
RETURN_FALSE;
}
connection->error = OCICollAppend(OCI(pEnv),
connection->pError,
(dvoid *) &num,
(dvoid *) &new_ind,
(OCIColl *) coll->coll);
RETURN_TRUE;
break;
}
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto string ocicollgetelem(object collection,ndx)
Retrieve the value at collection index ndx */
PHP_FUNCTION(ocicollgetelem)
{
zval *id,**arg;
oci_connection *connection;
oci_collection *coll;
ub4 ndx;
int inx;
dvoid *elem;
dvoid *elemind;
boolean exists;
OCIString *ocistr = (OCIString *)0;
text *str;
char buff[1024];
int len;
double dnum;
OCILS_FETCH();
if ((id = getThis()) != 0) {
if ((inx = _oci_get_ocicoll(id,&coll)) == 0) {
RETURN_FALSE;
}
if (zend_get_parameters_ex(1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long_ex(arg);
ndx = (*arg)->value.lval;
connection = coll->conn;
connection->error = OCICollGetElem(OCI(pEnv),
connection->pError,
coll->coll,
ndx,
&exists,
&elem,
&elemind);
if (connection->error) {
oci_error(connection->pError, "OCICollGetElem", connection->error);
RETURN_FALSE;
}
switch(coll->element_typecode) {
case OCI_TYPECODE_DATE:
len = 1024;
OCIDateToText(connection->pError,
elem,
0, /* fmt */
0, /* fmt_length */
0, /* lang_name */
0, /* lang_length */
&len,buff);
RETURN_STRINGL(buff,len,1);
case OCI_TYPECODE_VARCHAR2 :
ocistr = *(OCIString **)elem;
str = OCIStringPtr(OCI(pEnv),ocistr);
RETURN_STRINGL(str,strlen(str),1);
break;
case OCI_TYPECODE_UNSIGNED16 : /* UNSIGNED SHORT */
case OCI_TYPECODE_UNSIGNED32 : /* UNSIGNED LONG */
case OCI_TYPECODE_REAL : /* REAL */
case OCI_TYPECODE_DOUBLE : /* DOUBLE */
case OCI_TYPECODE_INTEGER : /* INT */
case OCI_TYPECODE_SIGNED16 : /* SHORT */
case OCI_TYPECODE_SIGNED32 : /* LONG */
case OCI_TYPECODE_DECIMAL : /* DECIMAL */
case OCI_TYPECODE_FLOAT : /* FLOAT */
case OCI_TYPECODE_NUMBER : /* NUMBER */
case OCI_TYPECODE_SMALLINT : /* SMALLINT */
connection->error = OCINumberToReal(connection->pError,
(CONST OCINumber *) elem,
(uword) sizeof(dnum), (dvoid *) &dnum);
if (connection->error) {
oci_error(connection->pError, "OCINumberToReal", connection->error);
RETURN_FALSE;
}
RETURN_DOUBLE(dnum);
break;
}
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto string ocicollassign(object collection,object)
Assign a collection from another existing collection */
PHP_FUNCTION(ocicollassign)
{
zval *id,**from;
oci_connection *connection;
oci_collection *coll,*from_coll;
int inx;
OCILS_FETCH();
if ((id = getThis()) != 0) {
if ((inx = _oci_get_ocicoll(id,&coll)) == 0) {
RETURN_FALSE;
}
if (zend_get_parameters_ex(1, &from) == FAILURE) {
WRONG_PARAM_COUNT;
}
if ((inx = _oci_get_ocicoll(*from,&from_coll)) == 0) {
RETURN_FALSE;
}
connection = coll->conn;
connection->error = OCICollAssign(OCI(pEnv),connection->pError,
from_coll->coll,coll->coll);
if (connection->error) {
oci_error(connection->pError, "OCICollAssignElem", connection->error);
RETURN_FALSE;
}
RETURN_TRUE;
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto string ocicollassignelem(object collection,ndx,val)
Assign element val to collection at index ndx */
PHP_FUNCTION(ocicollassignelem)
{
zval *id,**index,**val;
oci_connection *connection;
oci_collection *coll;
OCINumber num;
OCIInd new_ind = OCI_IND_NOTNULL;
ub4 ndx;
int inx;
OCIString *ocistr = (OCIString *)0;
OCIDate dt;
double dnum;
OCILS_FETCH();
if ((id = getThis()) != 0) {
if ((inx = _oci_get_ocicoll(id,&coll)) == 0) {
RETURN_FALSE;
}
if (zend_get_parameters_ex(2, &index,&val) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long_ex(index);
ndx = (*index)->value.lval;
connection = coll->conn;
if (connection->error) {
oci_error(connection->pError, "OCICollAssignElem", connection->error);
RETURN_FALSE;
}
switch(coll->element_typecode) {
case OCI_TYPECODE_DATE:
convert_to_string_ex(val);
connection->error = OCIDateFromText(connection->pError,
(*val)->value.str.val,
(*val)->value.str.len,
0,0,0,0,&dt);
if (connection->error) {
oci_error(connection->pError, "OCIDateFromText", connection->error);
RETURN_FALSE;
}
connection->error = OCICollAssignElem(OCI(pEnv),
connection->pError,
ndx,
(dword *)&dt,
&new_ind,
coll->coll);
if (connection->error) {
oci_error(connection->pError, "OCICollAssignElem", connection->error);
RETURN_FALSE;
}
break;
case OCI_TYPECODE_VARCHAR2 :
convert_to_string_ex(val);
connection->error = OCIStringAssignText(OCI(pEnv),
connection->pError,
(*val)->value.str.val,
(*val)->value.str.len,
&ocistr);
if (connection->error) {
oci_error(connection->pError, "OCIStringAssignText", connection->error);
RETURN_FALSE;
}
connection->error = OCICollAssignElem(OCI(pEnv),
connection->pError,
ndx,
(dword *)ocistr,
&new_ind,
coll->coll);
if (connection->error) {
oci_error(connection->pError, "OCICollAssignElem", connection->error);
RETURN_FALSE;
}
RETURN_TRUE;
break;
case OCI_TYPECODE_UNSIGNED16 : /* UNSIGNED SHORT */
case OCI_TYPECODE_UNSIGNED32 : /* UNSIGNED LONG */
case OCI_TYPECODE_REAL : /* REAL */
case OCI_TYPECODE_DOUBLE : /* DOUBLE */
case OCI_TYPECODE_INTEGER : /* INT */
case OCI_TYPECODE_SIGNED16 : /* SHORT */
case OCI_TYPECODE_SIGNED32 : /* LONG */
case OCI_TYPECODE_DECIMAL : /* DECIMAL */
case OCI_TYPECODE_FLOAT : /* FLOAT */
case OCI_TYPECODE_NUMBER : /* NUMBER */
case OCI_TYPECODE_SMALLINT : /* SMALLINT */
convert_to_double_ex(val);
dnum = (double)(*val)->value.dval;
connection->error = OCINumberFromReal(connection->pError,&dnum,
sizeof(double),&num);
if (connection->error) {
oci_error(connection->pError, "OCINumberFromReal", connection->error);
RETURN_FALSE;
}
connection->error = OCICollAssignElem(OCI(pEnv),
connection->pError,
ndx,
(dword *)&num,
&new_ind,
coll->coll);
if (connection->error) {
oci_error(connection->pError, "OCICollAssignElem", connection->error);
RETURN_FALSE;
}
RETURN_TRUE;
break;
}
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto string ocicollsize(object collection)
Return the size of a collection */
PHP_FUNCTION(ocicollsize)
{
zval *id;
oci_connection *connection;
oci_collection *coll;
sb4 sz;
int inx;
OCILS_FETCH();
if ((id = getThis()) != 0) {
if ((inx = _oci_get_ocicoll(id,&coll)) == 0) {
RETURN_FALSE;
}
connection = coll->conn;
connection->error = OCICollSize(OCI(pEnv),coll->conn->pError,coll->coll,&sz);
if (connection->error) {
oci_error(connection->pError, "OCICollSize", connection->error);
RETURN_FALSE;
}
RETURN_LONG(sz);
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto string ocicollmax(object collection)
Return the max value of a collection. For a
varray this is the maximum length of the array */
PHP_FUNCTION(ocicollmax)
{
zval *id;
oci_collection *coll;
sb4 sz;
int inx;
OCILS_FETCH();
if ((id = getThis()) != 0) {
if ((inx = _oci_get_ocicoll(id,&coll)) == 0) {
RETURN_FALSE;
}
sz = OCICollMax(OCI(pEnv),coll->coll);
RETURN_LONG(sz);
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto string ocicolltrim(object collection,num)
Trim num elements from the end of a collection */
PHP_FUNCTION(ocicolltrim)
{
zval *id,**arg;
oci_collection *coll;
int inx;
OCILS_FETCH();
if ((id = getThis()) != 0) {
if ((inx = _oci_get_ocicoll(id,&coll)) == 0) {
RETURN_FALSE;
}
if (zend_get_parameters_ex(1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long_ex(arg);
coll->conn->error = OCICollTrim(OCI(pEnv),coll->conn->pError,(*arg)->value.lval,coll->coll);
if (coll->conn->error) {
oci_error(coll->conn->pError, "OCICollTrim", coll->conn->error);
RETURN_FALSE;
}
RETURN_TRUE;
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto string ocinewcollection(int connection, string tdo,[string schema])
Initialize a new collection */
PHP_FUNCTION(ocinewcollection)
{
dvoid *dschp1;
dvoid *parmp1;
dvoid *parmp2;
zval **conn, **tdo, **schema;
oci_connection *connection;
oci_collection *coll;
int ac = ZEND_NUM_ARGS();
OCILS_FETCH();
if (ac < 2 || ac > 3 || zend_get_parameters_ex(ac, &conn, &tdo, &schema) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(tdo);
if(ac == 3) {
convert_to_string_ex(schema);
}
coll = emalloc(sizeof(oci_collection));
OCI_GET_CONN(connection,conn);
coll->conn = connection;
coll->id = zend_list_insert(coll,le_coll);
zend_list_addref(connection->id);
connection->error = OCITypeByName(OCI(pEnv),
connection->pError,
connection->pServiceContext,
ac==3?(text *)(*schema)->value.str.val:(text *)0,
ac==3?(ub4)(*schema)->value.str.len: (ub4)0,
(text *) (*tdo)->value.str.val,
(ub4) (*tdo)->value.str.len,
(CONST text *) 0, (ub4) 0,
OCI_DURATION_SESSION,
OCI_TYPEGET_ALL,
&(coll->tdo));
if (connection->error) {
oci_error(connection->pError, "OCITypeByName", connection->error);
RETURN_FALSE;
}
connection->error = OCIHandleAlloc(OCI(pEnv), (dvoid **) &dschp1,
(ub4) OCI_HTYPE_DESCRIBE,
(size_t) 0, (dvoid **) 0);
if (connection->error) {
oci_error(connection->pError, "OCI_HTYPE_DESCRIBE", connection->error);
RETURN_FALSE;
}
connection->error = OCIDescribeAny(connection->pServiceContext, connection->pError, (dvoid *) coll->tdo,
(ub4) 0, OCI_OTYPE_PTR, (ub1)1,
(ub1) OCI_PTYPE_TYPE, dschp1);
if (connection->error) {
oci_error(connection->pError, "OCI_OTYPE_PTR", connection->error);
RETURN_FALSE;
}
connection->error = OCIAttrGet((dvoid *) dschp1,
(ub4) OCI_HTYPE_DESCRIBE,
(dvoid *)&parmp1, (ub4 *)0, (ub4)OCI_ATTR_PARAM,connection->pError);
if (connection->error) {
oci_error(connection->pError, "OCI_ATTR_PARAM", connection->error);
RETURN_FALSE;
}
/* get the collection type code of the attribute */
connection->error = OCIAttrGet((dvoid*) parmp1, (ub4) OCI_DTYPE_PARAM,
(dvoid*) &(coll->coll_typecode), (ub4 *) 0,
(ub4) OCI_ATTR_COLLECTION_TYPECODE,
connection->pError);
if (connection->error) {
oci_error(connection->pError, "OCI_ATTR_COLLECTION_TYPECODE", connection->error);
RETURN_FALSE;
}
switch(coll->coll_typecode) {
case OCI_TYPECODE_VARRAY:
case OCI_TYPECODE_TABLE:
connection->error = OCIAttrGet((dvoid*) parmp1,
(ub4) OCI_DTYPE_PARAM,
(dvoid*) &parmp2, (ub4 *) 0,
(ub4) OCI_ATTR_COLLECTION_ELEMENT,
connection->pError);
if (connection->error) {
oci_error(connection->pError, "OCI_ATTR_COLLECTION_ELEMENT", connection->error);
RETURN_FALSE;
}
connection->error = OCIAttrGet((dvoid*) parmp2,
(ub4) OCI_DTYPE_PARAM,
(dvoid*) &(coll->elem_ref), (ub4 *) 0,
(ub4) OCI_ATTR_REF_TDO,
connection->pError);
if (connection->error) {
oci_error(connection->pError, "OCI_ATTR_REF_TDO", connection->error);
RETURN_FALSE;
}
connection->error = OCITypeByRef(OCI(pEnv), connection->pError, coll->elem_ref,
OCI_DURATION_SESSION,
OCI_TYPEGET_HEADER, &(coll->element_type));
if (connection->error) {
oci_error(connection->pError, "OCI_TYPEGET_HEADER", connection->error);
RETURN_FALSE;
}
connection->error = OCIAttrGet((dvoid*) parmp2,
(ub4) OCI_DTYPE_PARAM,
(dvoid*) &(coll->element_typecode), (ub4 *) 0,
(ub4) OCI_ATTR_TYPECODE,
connection->pError);
if (connection->error) {
oci_error(connection->pError, "OCI_ATTR_TYPECODE", connection->error);
RETURN_FALSE;
}
break;
default:
php_error(E_WARNING, "OCINewCollection - Unknown Type %d", coll->coll_typecode);
}
/* Create object to hold return table */
connection->error = OCIObjectNew(OCI(pEnv),
connection->pError,
connection->pServiceContext,
OCI_TYPECODE_TABLE,
coll->tdo,
(dvoid *)0,
OCI_DURATION_DEFAULT,
TRUE,
(dvoid **) &(coll->coll));
if (connection->error) {
oci_error(connection->pError, "OCIObjectNew", connection->error);
RETURN_FALSE;
}
object_init_ex(return_value, oci_coll_class_entry_ptr);
add_property_resource(return_value, "collection",coll->id);
}
/* }}} */
#endif
1999-04-22 06:49:16 +08:00
#endif /* HAVE_OCI8 */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 tw=78 fdm=marker
* vim<600: sw=4 ts=4 tw=78
1999-04-22 06:49:16 +08:00
*/