~ubuntu-branches/ubuntu/hardy/php5/hardy-updates

« back to all changes in this revision

Viewing changes to ext/oci8/oci8.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-10-09 03:14:32 UTC
  • Revision ID: james.westby@ubuntu.com-20051009031432-kspik3lobxstafv9
Tags: upstream-5.0.5
ImportĀ upstreamĀ versionĀ 5.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   +----------------------------------------------------------------------+
 
3
   | PHP Version 5                                                        |
 
4
   +----------------------------------------------------------------------+
 
5
   | Copyright (c) 1997-2004 The PHP Group                                |
 
6
   +----------------------------------------------------------------------+
 
7
   | This source file is subject to version 3.0 of the PHP license,       |
 
8
   | that is bundled with this package in the file LICENSE, and is        |
 
9
   | available through the world-wide-web at the following url:           |
 
10
   | http://www.php.net/license/3_0.txt.                                  |
 
11
   | If you did not receive a copy of the PHP license and are unable to   |
 
12
   | obtain it through the world-wide-web, please send a note to          |
 
13
   | license@php.net so we can mail you a copy immediately.               |
 
14
   +----------------------------------------------------------------------+
 
15
   | Authors: Stig Sļæ½ther Bakken <ssb@php.net>                            |
 
16
   |          Thies C. Arntzen <thies@thieso.net>                         |
 
17
   |          Maxim Maletsky <maxim@maxim.cx>                             |
 
18
   |                                                                      |
 
19
   | Collection support by Andy Sautins <asautins@veripost.net>           |
 
20
   | Temporary LOB support by David Benson <dbenson@mancala.com>          |
 
21
   | ZTS per process OCIPLogon by Harald Radi <harald.radi@nme.at>        |
 
22
   +----------------------------------------------------------------------+
 
23
 */
 
24
 
 
25
/* $Id: oci8.c,v 1.257.2.8 2005/06/13 09:31:03 tony2001 Exp $ */
 
26
 
 
27
/* TODO list:
 
28
 *
 
29
 * - php.ini flags 
 
30
 *   especialliy important for things like oci_ping
 
31
 *              allowpconns
 
32
 *              timeout
 
33
 *              maxlifetime
 
34
 *              maxpconns
 
35
 * - Change return-value for OCIFetch*() (1-row read, 0-Normal end, false-error) 
 
36
 * - Error mode (print or shut up?)
 
37
 * - binding of arrays
 
38
 * - Character sets for NCLOBS
 
39
 * - split the module into an upper (php-callable) and lower (c-callable) layer!
 
40
 * - remove all XXXs
 
41
 * - clean up and documentation
 
42
 * - make OCIInternalDebug accept a mask of flags....
 
43
 * - have one ocifree() call.
 
44
 * - make it possible to have persistent statements?
 
45
 * - failover
 
46
 * - change all the lob stuff to work without classes (optional)! 
 
47
 * - make sure that the callbacks terminate the strings with \0
 
48
 * - cleanup the ociexecute semantics for refcursors
 
49
 * - make $lob->savefile use O_BINARY
 
50
 * - line 2728: ub4 length = -1; needs fixing
 
51
 * - delay OCIInitialize() as far as we can.
 
52
 * - add PHP Array <-> OCICollection conversion
 
53
 * - add Collection iterator object for INDEX BY tables
 
54
 * - make auto-rollback only happen if we have an outstanding transaction
 
55
 * - implement ocidisconnect
 
56
 * - add OCI9-specific functions and separate them from OCI8 with ifdefs
 
57
 */
 
58
 
 
59
/* {{{ includes & stuff */
 
60
 
 
61
#ifdef HAVE_CONFIG_H
 
62
#include "config.h"
 
63
#endif
 
64
 
 
65
#include "php.h"
 
66
#include "ext/standard/info.h"
 
67
#include "php_ini.h"
 
68
 
 
69
#if HAVE_OCI8
 
70
 
 
71
#include "php_oci8.h"
 
72
 
 
73
/* True globals, only used by thread safe functions */
 
74
static TsHashTable *persistent_servers;
 
75
static TsHashTable *persistent_sessions;
 
76
 
 
77
static long num_persistent = 0;
 
78
static long num_links = 0;
 
79
 
 
80
/* True globals, no need for thread safety */
 
81
static int le_conn;
 
82
static int le_stmt;
 
83
static int le_desc;
 
84
#ifdef PHP_OCI8_HAVE_COLLECTIONS 
 
85
static int le_coll;
 
86
#endif
 
87
static int le_server;
 
88
static int le_session;
 
89
static zend_class_entry *oci_lob_class_entry_ptr;
 
90
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
91
static zend_class_entry *oci_coll_class_entry_ptr;
 
92
#endif
 
93
 
 
94
#ifndef SQLT_BFILEE
 
95
#define SQLT_BFILEE 114
 
96
#endif
 
97
#ifndef SQLT_CFILEE
 
98
#define SQLT_CFILEE 115
 
99
#endif
 
100
 
 
101
#define SAFE_STRING(s) ((s)?(s):"")
 
102
 
 
103
#ifdef ZTS
 
104
MUTEX_T mx_lock;
 
105
 
 
106
#define mutex_alloc(mutex) mutex = tsrm_mutex_alloc()
 
107
#define mutex_free(mutex) tsrm_mutex_free(mutex)
 
108
#define mutex_lock(mutex) tsrm_mutex_lock(mutex)
 
109
#define mutex_unlock(mutex) tsrm_mutex_unlock(mutex)
 
110
#define thread_id()     tsrm_thread_id()
 
111
#else
 
112
#define mutex_alloc(mutex)
 
113
#define mutex_free(mutex)
 
114
#define mutex_lock(mutex)
 
115
#define mutex_unlock(mutex)
 
116
#define thread_id() 1
 
117
#endif
 
118
 
 
119
/* dirty marcos to make sure we _never_ call oracle-functions recursivly 
 
120
 *
 
121
 * i'm well aware that we should _never_ call exit directly - this core is for
 
122
 * pure testing and commented out - as you can see;-)
 
123
 * thies@thieso.net 20010723
 
124
 */
 
125
 
 
126
#define CALL_OCI(call) \
 
127
{ \
 
128
        if (OCI(in_call)) { \
 
129
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "OCI8 Recursive call!\n"); \
 
130
                exit(-1); \
 
131
        } else { \
 
132
                OCI(in_call)=1; \
 
133
                call; \
 
134
                OCI(in_call)=0; \
 
135
        } \
 
136
}
 
137
 
 
138
#define CALL_OCI_RETURN(retcode,call) \
 
139
{ \
 
140
        if (OCI(in_call)) { \
 
141
                retcode=-1; \
 
142
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "OCI8 Recursive call!\n"); \
 
143
                exit(-1); \
 
144
        } else { \
 
145
                OCI(in_call)=1; \
 
146
                retcode=call; \
 
147
                OCI(in_call)=0; \
 
148
        } \
 
149
}
 
150
 
 
151
#include <fcntl.h>
 
152
 
 
153
#ifndef O_BINARY
 
154
#define O_BINARY 0
 
155
#endif
 
156
 
 
157
/* }}} */
 
158
/* {{{ thread safety stuff */
 
159
 
 
160
#ifdef ZTS
 
161
int oci_globals_id;
 
162
#else
 
163
PHP_OCI_API php_oci_globals oci_globals;
 
164
#endif
 
165
 
 
166
/* }}} */
 
167
/* {{{ dynamically loadable module stuff */
 
168
 
 
169
#ifdef COMPILE_DL_OCI8
 
170
ZEND_GET_MODULE(oci8)
 
171
# ifdef PHP_WIN32
 
172
# include "zend_arg_defs.c"
 
173
# endif
 
174
#endif /* COMPILE_DL */
 
175
 
 
176
/* }}} */
 
177
/* {{{ startup/shutdown/info/internal function prototypes */
 
178
 
 
179
PHP_MINIT_FUNCTION(oci);
 
180
PHP_RINIT_FUNCTION(oci);
 
181
PHP_MSHUTDOWN_FUNCTION(oci);
 
182
PHP_RSHUTDOWN_FUNCTION(oci);
 
183
PHP_MINFO_FUNCTION(oci);
 
184
 
 
185
static ub4 oci_handle_error(oci_connection *connection, ub4 errcode);
 
186
static ub4 oci_error(OCIError *err_p, char *what, sword status);
 
187
static int oci_ping(oci_server *server);
 
188
static void oci_debug(const char *format, ...);
 
189
 
 
190
static void _oci_conn_list_dtor(oci_connection *connection TSRMLS_DC);
 
191
static void _oci_stmt_list_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC);
 
192
static void _oci_descriptor_list_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC);
 
193
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
194
static void _oci_coll_list_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC);
 
195
#endif
 
196
static void _oci_server_list_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC);
 
197
static void _oci_session_list_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC);
 
198
static void php_oci_free_conn_list(zend_rsrc_list_entry *rsrc TSRMLS_DC);
 
199
 
 
200
static void _oci_column_hash_dtor(void *data);
 
201
static void _oci_define_hash_dtor(void *data);
 
202
static void _oci_bind_hash_dtor(void *data);
 
203
static void _oci_desc_flush_hash_dtor(void *data);
 
204
 
 
205
static oci_connection *oci_get_conn(zval ** TSRMLS_DC);
 
206
static oci_statement *oci_get_stmt(zval ** TSRMLS_DC);
 
207
static oci_descriptor *oci_get_desc(int TSRMLS_DC);
 
208
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
209
/* Questionable name.  Very close to oci_get_col */
 
210
static oci_collection *oci_get_coll(int TSRMLS_DC);
 
211
#endif
 
212
static oci_out_column *oci_get_col(oci_statement *, int, zval **);
 
213
 
 
214
static int _oci_make_zval(zval *, oci_statement *, oci_out_column *, char *, int mode TSRMLS_DC);
 
215
static oci_statement *oci_parse(oci_connection *, char *, int);
 
216
static int oci_execute(oci_statement *, char *, ub4 mode);
 
217
static int oci_fetch(oci_statement *, ub4, char * TSRMLS_DC);
 
218
static int oci_lobgetlen(oci_connection *, oci_descriptor *, ub4 *length);
 
219
static int oci_loadlob(oci_connection *, oci_descriptor *, char **, ub4 *length);
 
220
static int oci_readlob(oci_connection *, oci_descriptor *, char **, ub4 *len);
 
221
static int oci_setprefetch(oci_statement *statement, int size);
 
222
 
 
223
static void oci_do_connect(INTERNAL_FUNCTION_PARAMETERS,int persistent,int exclusive);
 
224
 
 
225
static oci_server *_oci_open_server(char *dbname,int persistent);
 
226
static void _oci_close_server(oci_server *server);
 
227
 
 
228
static oci_session *_oci_open_session(oci_server* server,char *username,char *password,int persistent,int exclusive, char *charset);
 
229
static void _oci_close_session(oci_session *session);
 
230
 
 
231
static sb4 oci_bind_in_callback(dvoid *, OCIBind *, ub4, ub4, dvoid **, ub4 *, ub1 *, dvoid **);
 
232
static sb4 oci_bind_out_callback(dvoid *, OCIBind *, ub4, ub4, dvoid **, ub4 **, ub1 *, dvoid **, ub2 **);
 
233
 
 
234
#if 0
 
235
static sb4 oci_failover_callback(dvoid *svchp,dvoid* envhp,dvoid *fo_ctx,ub4 fo_type, ub4 fo_event);
 
236
#endif
 
237
 
 
238
static int oci_lob_flush(oci_descriptor *, int flush_flag TSRMLS_DC);
 
239
 
 
240
/* }}} */
 
241
 
 
242
/* {{{ extension macros 
 
243
*/
 
244
#define OCI_GET_STMT(statement,value) \
 
245
        statement = oci_get_stmt(value TSRMLS_CC); \
 
246
        if (statement == NULL) { \
 
247
                RETURN_FALSE; \
 
248
        }
 
249
 
 
250
#define OCI_GET_CONN(connection,value) \
 
251
        connection = oci_get_conn(value TSRMLS_CC); \
 
252
        if (connection == NULL) { \
 
253
                RETURN_FALSE; \
 
254
        }
 
255
 
 
256
#define OCI_GET_DESC(descriptor,index) \
 
257
        descriptor = oci_get_desc(index TSRMLS_CC); \
 
258
        if (descriptor == NULL) { \
 
259
                RETURN_FALSE; \
 
260
        }
 
261
 
 
262
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
263
#define OCI_GET_COLL(collection,index) \
 
264
        collection = oci_get_coll(index TSRMLS_CC); \
 
265
        if (collection == NULL) { \
 
266
                RETURN_FALSE; \
 
267
        }
 
268
#endif
 
269
 
 
270
#define IS_LOB_INTERNAL(lob) \
 
271
        if (lob->type != OCI_DTYPE_LOB) { \
 
272
                switch (lob->type) { \
 
273
                        case OCI_DTYPE_FILE: \
 
274
                                php_error_docref(NULL TSRMLS_CC, E_NOTICE, "internal LOB was expected, FILE locator is given"); \
 
275
                                break; \
 
276
                        case OCI_DTYPE_ROWID: \
 
277
                                php_error_docref(NULL TSRMLS_CC, E_NOTICE, "internal LOB was expected, ROWID locator is given"); \
 
278
                                break; \
 
279
                        default: \
 
280
                                php_error_docref(NULL TSRMLS_CC, E_NOTICE, "internal LOB was expected, locator of unknown type is given"); \
 
281
                                break; \
 
282
                } \
 
283
                RETURN_FALSE; \
 
284
        }
 
285
/* }}} */
 
286
 
 
287
/* {{{ extension function prototypes
 
288
*/
 
289
PHP_FUNCTION(oci_bind_by_name);
 
290
PHP_FUNCTION(oci_define_by_name);
 
291
PHP_FUNCTION(oci_field_is_null);
 
292
PHP_FUNCTION(oci_field_name);
 
293
PHP_FUNCTION(oci_field_size);
 
294
PHP_FUNCTION(oci_field_scale);
 
295
PHP_FUNCTION(oci_field_precision);
 
296
PHP_FUNCTION(oci_field_type);
 
297
PHP_FUNCTION(oci_field_type_raw);
 
298
PHP_FUNCTION(oci_execute);
 
299
PHP_FUNCTION(oci_fetch);
 
300
PHP_FUNCTION(oci_cancel);
 
301
PHP_FUNCTION(ocifetchinto);
 
302
PHP_FUNCTION(oci_fetch_object);
 
303
PHP_FUNCTION(oci_fetch_row);
 
304
PHP_FUNCTION(oci_fetch_assoc);
 
305
PHP_FUNCTION(oci_fetch_array);
 
306
PHP_FUNCTION(ocifetchstatement);
 
307
PHP_FUNCTION(oci_fetch_all);
 
308
PHP_FUNCTION(oci_free_statement);
 
309
PHP_FUNCTION(oci_internal_debug);
 
310
PHP_FUNCTION(oci_close);
 
311
PHP_FUNCTION(oci_connect);
 
312
PHP_FUNCTION(oci_new_connect);
 
313
PHP_FUNCTION(oci_pconnect);
 
314
PHP_FUNCTION(oci_error);
 
315
PHP_FUNCTION(oci_free_descriptor);
 
316
PHP_FUNCTION(oci_commit);
 
317
PHP_FUNCTION(oci_rollback);
 
318
PHP_FUNCTION(oci_new_descriptor);
 
319
PHP_FUNCTION(oci_num_fields);
 
320
PHP_FUNCTION(oci_parse);
 
321
PHP_FUNCTION(oci_new_cursor);
 
322
PHP_FUNCTION(oci_result);
 
323
PHP_FUNCTION(oci_server_version);
 
324
PHP_FUNCTION(oci_statement_type);
 
325
PHP_FUNCTION(oci_num_rows);
 
326
PHP_FUNCTION(oci_set_prefetch);
 
327
PHP_FUNCTION(oci_password_change);
 
328
PHP_FUNCTION(oci_lob_save);
 
329
PHP_FUNCTION(oci_lob_import);
 
330
PHP_FUNCTION(oci_lob_export);
 
331
PHP_FUNCTION(oci_lob_load);
 
332
PHP_FUNCTION(oci_lob_tell);
 
333
PHP_FUNCTION(oci_lob_write);
 
334
PHP_FUNCTION(oci_lob_append);
 
335
PHP_FUNCTION(oci_lob_copy);
 
336
PHP_FUNCTION(oci_lob_truncate);
 
337
PHP_FUNCTION(oci_lob_erase);
 
338
PHP_FUNCTION(oci_lob_flush);
 
339
PHP_FUNCTION(ocisetbufferinglob);
 
340
PHP_FUNCTION(ocigetbufferinglob);
 
341
PHP_FUNCTION(oci_lob_is_equal);
 
342
PHP_FUNCTION(oci_lob_rewind);
 
343
PHP_FUNCTION(oci_lob_read);
 
344
PHP_FUNCTION(oci_lob_eof);
 
345
PHP_FUNCTION(oci_lob_seek);
 
346
PHP_FUNCTION(oci_lob_size);
 
347
#ifdef HAVE_OCI8_TEMP_LOB
 
348
PHP_FUNCTION(oci_lob_write_temporary);
 
349
PHP_FUNCTION(oci_lob_close);
 
350
#endif
 
351
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
352
PHP_FUNCTION(oci_new_collection);
 
353
PHP_FUNCTION(oci_free_collection);
 
354
PHP_FUNCTION(oci_collection_append);
 
355
PHP_FUNCTION(oci_collection_element_get);
 
356
PHP_FUNCTION(oci_collection_element_assign);
 
357
PHP_FUNCTION(oci_collection_assign);
 
358
PHP_FUNCTION(oci_collection_size);
 
359
PHP_FUNCTION(oci_collection_max);
 
360
PHP_FUNCTION(oci_collection_trim);
 
361
#endif
 
362
/* }}} */
 
363
 
 
364
/* {{{ extension definition structures
 
365
*/
 
366
#define OCI_ASSOC               1<<0
 
367
#define OCI_NUM                 1<<1
 
368
#define OCI_BOTH                (OCI_ASSOC|OCI_NUM)
 
369
 
 
370
#define OCI_RETURN_NULLS        1<<2
 
371
#define OCI_RETURN_LOBS         1<<3
 
372
 
 
373
#define OCI_FETCHSTATEMENT_BY_COLUMN    1<<4
 
374
#define OCI_FETCHSTATEMENT_BY_ROW       1<<5
 
375
#define OCI_FETCHSTATEMENT_BY           (OCI_FETCHSTATEMENT_BY_COLUMN | OCI_FETCHSTATEMENT_BY_ROW)
 
376
 
 
377
static zend_function_entry php_oci_functions[] = {
 
378
        PHP_FE(oci_define_by_name,          third_arg_force_ref)
 
379
        PHP_FE(oci_bind_by_name,            third_arg_force_ref)
 
380
        PHP_FE(oci_field_is_null,           NULL)
 
381
        PHP_FE(oci_field_name,              NULL)
 
382
        PHP_FE(oci_field_size,              NULL)
 
383
        PHP_FE(oci_field_scale,             NULL)
 
384
        PHP_FE(oci_field_precision,         NULL)
 
385
        PHP_FE(oci_field_type,              NULL)
 
386
        PHP_FE(oci_field_type_raw,          NULL)
 
387
        PHP_FE(oci_execute,                 NULL)
 
388
        PHP_FE(oci_cancel,                  NULL)
 
389
        PHP_FE(oci_fetch,                   NULL)
 
390
        PHP_FE(oci_fetch_object,            NULL)
 
391
        PHP_FE(oci_fetch_row,               NULL)
 
392
        PHP_FE(oci_fetch_assoc,             NULL)
 
393
        PHP_FE(oci_fetch_array,             NULL)
 
394
        PHP_FE(ocifetchinto,                second_arg_force_ref)
 
395
        PHP_FE(oci_fetch_all,               second_arg_force_ref)
 
396
        PHP_FE(oci_free_statement,          NULL)
 
397
        PHP_FE(oci_internal_debug,          NULL)
 
398
        PHP_FE(oci_num_fields,              NULL)
 
399
        PHP_FE(oci_parse,                   NULL)
 
400
        PHP_FE(oci_new_cursor,              NULL)
 
401
        PHP_FE(oci_result,                  NULL)
 
402
        PHP_FE(oci_server_version,          NULL)
 
403
        PHP_FE(oci_statement_type,          NULL)
 
404
        PHP_FE(oci_num_rows,                NULL)
 
405
        PHP_FE(oci_close,                   NULL)
 
406
        PHP_FE(oci_connect,                 NULL)
 
407
        PHP_FE(oci_new_connect,             NULL)
 
408
        PHP_FE(oci_pconnect,                NULL)
 
409
        PHP_FE(oci_error,                   NULL)
 
410
        PHP_FE(oci_free_descriptor,         NULL)
 
411
        PHP_FE(oci_lob_save,                NULL)
 
412
        PHP_FE(oci_lob_import,              NULL)
 
413
        PHP_FE(oci_lob_size,                NULL)       
 
414
        PHP_FE(oci_lob_load,                NULL)
 
415
        PHP_FE(oci_lob_read,                NULL)
 
416
        PHP_FE(oci_lob_eof,                 NULL)       
 
417
        PHP_FE(oci_lob_tell,                NULL)
 
418
        PHP_FE(oci_lob_truncate,            NULL)
 
419
        PHP_FE(oci_lob_erase,               NULL)
 
420
        PHP_FE(oci_lob_flush,               NULL)
 
421
        PHP_FE(ocisetbufferinglob,          NULL)
 
422
        PHP_FE(ocigetbufferinglob,          NULL)
 
423
        PHP_FE(oci_lob_is_equal,                    NULL)
 
424
        PHP_FE(oci_lob_rewind,              NULL)
 
425
        PHP_FE(oci_lob_write,               NULL)
 
426
        PHP_FE(oci_lob_append,              NULL)
 
427
        PHP_FE(oci_lob_copy,                NULL)
 
428
        PHP_FE(oci_lob_export,              NULL)
 
429
        PHP_FE(oci_commit,                  NULL)
 
430
        PHP_FE(oci_rollback,                NULL)
 
431
        PHP_FE(oci_new_descriptor,          NULL)
 
432
        PHP_FE(oci_set_prefetch,            NULL)
 
433
        PHP_FE(oci_password_change,         NULL)
 
434
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
435
        PHP_FE(oci_free_collection,         NULL)
 
436
        PHP_FE(oci_collection_append,       NULL)
 
437
        PHP_FE(oci_collection_element_get,      NULL)
 
438
        PHP_FE(oci_collection_element_assign,   NULL)
 
439
        PHP_FE(oci_collection_assign,       NULL)
 
440
        PHP_FE(oci_collection_size,         NULL)
 
441
        PHP_FE(oci_collection_max,          NULL)
 
442
        PHP_FE(oci_collection_trim,         NULL)
 
443
        PHP_FE(oci_new_collection,          NULL)
 
444
#endif
 
445
        
 
446
        PHP_FALIAS(oci_free_cursor,     oci_free_statement,       NULL)
 
447
        PHP_FALIAS(ocifreecursor,       oci_free_statement,       NULL)
 
448
        PHP_FALIAS(ocibindbyname,       oci_bind_by_name,       third_arg_force_ref)
 
449
        PHP_FALIAS(ocidefinebyname,     oci_define_by_name,     third_arg_force_ref)
 
450
        PHP_FALIAS(ocicolumnisnull,     oci_field_is_null,      NULL)
 
451
        PHP_FALIAS(ocicolumnname,       oci_field_name,         NULL)
 
452
        PHP_FALIAS(ocicolumnsize,       oci_field_size,         NULL)
 
453
        PHP_FALIAS(ocicolumnscale,      oci_field_scale,        NULL)
 
454
        PHP_FALIAS(ocicolumnprecision,  oci_field_precision,    NULL)
 
455
        PHP_FALIAS(ocicolumntype,       oci_field_type,         NULL)
 
456
        PHP_FALIAS(ocicolumntyperaw,    oci_field_type_raw,     NULL)
 
457
        PHP_FALIAS(ociexecute,          oci_execute,            NULL)
 
458
        PHP_FALIAS(ocicancel,           oci_cancel,             NULL)
 
459
        PHP_FALIAS(ocifetch,            oci_fetch,              NULL)
 
460
        PHP_FALIAS(ocifetchstatement,   oci_fetch_all,          second_arg_force_ref)
 
461
        PHP_FALIAS(ocifreestatement,    oci_free_statement,     NULL)
 
462
        PHP_FALIAS(ociinternaldebug,    oci_internal_debug,     NULL)
 
463
        PHP_FALIAS(ocinumcols,          oci_num_fields,         NULL)
 
464
        PHP_FALIAS(ociparse,            oci_parse,              NULL)
 
465
        PHP_FALIAS(ocinewcursor,        oci_new_cursor,         NULL)
 
466
        PHP_FALIAS(ociresult,           oci_result,             NULL)
 
467
        PHP_FALIAS(ociserverversion,    oci_server_version,     NULL)
 
468
        PHP_FALIAS(ocistatementtype,    oci_statement_type,     NULL)
 
469
        PHP_FALIAS(ocirowcount,         oci_num_rows,           NULL)
 
470
        PHP_FALIAS(ocilogoff,           oci_close,              NULL)
 
471
        PHP_FALIAS(ocilogon,            oci_connect,            NULL)
 
472
        PHP_FALIAS(ocinlogon,           oci_new_connect,        NULL)
 
473
        PHP_FALIAS(ociplogon,           oci_pconnect,           NULL)
 
474
        PHP_FALIAS(ocierror,            oci_error,              NULL)
 
475
        PHP_FALIAS(ocifreedesc,         oci_free_descriptor,    NULL)
 
476
        PHP_FALIAS(ocisavelob,          oci_lob_save,           NULL)
 
477
        PHP_FALIAS(ocisavelobfile,      oci_lob_import,         NULL)
 
478
        PHP_FALIAS(ociwritelobtofile,   oci_lob_export,         NULL)
 
479
        PHP_FALIAS(ociloadlob,          oci_lob_load,           NULL)
 
480
        PHP_FALIAS(ocicommit,           oci_commit,             NULL)
 
481
        PHP_FALIAS(ocirollback,         oci_rollback,           NULL)
 
482
        PHP_FALIAS(ocinewdescriptor,    oci_new_descriptor,     NULL)
 
483
        PHP_FALIAS(ocisetprefetch,      oci_set_prefetch,       NULL)
 
484
        PHP_FALIAS(ocipasswordchange,   oci_password_change,    NULL)
 
485
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
486
        PHP_FALIAS(ocifreecollection,   oci_free_collection,    NULL)
 
487
        PHP_FALIAS(ocinewcollection,    oci_new_collection,     NULL)
 
488
        PHP_FALIAS(ocicollappend,       oci_collection_append,  NULL)
 
489
        PHP_FALIAS(ocicollgetelem,      oci_collection_element_get,     NULL)
 
490
        PHP_FALIAS(ocicollassignelem,   oci_collection_element_assign,  NULL)
 
491
        PHP_FALIAS(ocicollsize,         oci_collection_size,    NULL)
 
492
        PHP_FALIAS(ocicollmax,          oci_collection_max,     NULL)
 
493
        PHP_FALIAS(ocicolltrim,         oci_collection_trim,    NULL)
 
494
#endif
 
495
        {NULL,NULL,NULL}
 
496
};
 
497
 
 
498
static zend_function_entry php_oci_lob_class_functions[] = {
 
499
        PHP_FALIAS(load,        oci_lob_load,           NULL)
 
500
        PHP_FALIAS(tell,        oci_lob_tell,           NULL)
 
501
        PHP_FALIAS(truncate,    oci_lob_truncate,       NULL)
 
502
        PHP_FALIAS(erase,       oci_lob_erase,          NULL)
 
503
        PHP_FALIAS(flush,       oci_lob_flush,          NULL)
 
504
        PHP_FALIAS(setbuffering,ocisetbufferinglob,     NULL)
 
505
        PHP_FALIAS(getbuffering,ocigetbufferinglob,     NULL)
 
506
        PHP_FALIAS(rewind,      oci_lob_rewind,                 NULL)
 
507
        PHP_FALIAS(read,        oci_lob_read,           NULL)
 
508
        PHP_FALIAS(eof,         oci_lob_eof,            NULL)
 
509
        PHP_FALIAS(seek,        oci_lob_seek,           NULL)   
 
510
        PHP_FALIAS(write,       oci_lob_write,          NULL)
 
511
        PHP_FALIAS(append,      oci_lob_append,         NULL)
 
512
        PHP_FALIAS(size,        oci_lob_size,           NULL)
 
513
        PHP_FALIAS(writetofile, oci_lob_export,         NULL)
 
514
#ifdef HAVE_OCI8_TEMP_LOB
 
515
        PHP_FALIAS(writetemporary,  oci_lob_write_temporary,    NULL)
 
516
        PHP_FALIAS(close,           oci_lob_close,              NULL)
 
517
#endif
 
518
        PHP_FALIAS(save,        oci_lob_save,           NULL)
 
519
        PHP_FALIAS(savefile,    oci_lob_import,         NULL)
 
520
        PHP_FALIAS(free,        oci_free_descriptor,    NULL)
 
521
        {NULL,NULL,NULL}
 
522
};
 
523
 
 
524
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
525
static zend_function_entry php_oci_coll_class_functions[] = {
 
526
        PHP_FALIAS(append,        oci_collection_append,            NULL)
 
527
        PHP_FALIAS(getelem,       oci_collection_element_get,       NULL)
 
528
        PHP_FALIAS(assignelem,    oci_collection_element_assign,    NULL)
 
529
        PHP_FALIAS(assign,        oci_collection_assign,            NULL)
 
530
        PHP_FALIAS(size,          oci_collection_size,              NULL)
 
531
        PHP_FALIAS(max,           oci_collection_max,               NULL)
 
532
        PHP_FALIAS(trim,          oci_collection_trim,              NULL)
 
533
        PHP_FALIAS(free,          oci_free_collection,              NULL)
 
534
        {NULL,NULL,NULL}
 
535
};
 
536
#endif
 
537
 
 
538
zend_module_entry oci8_module_entry = {
 
539
        STANDARD_MODULE_HEADER,
 
540
        "oci8",               /* extension name */
 
541
        php_oci_functions,    /* extension function list */
 
542
        PHP_MINIT(oci),       /* extension-wide startup function */
 
543
        PHP_MSHUTDOWN(oci),   /* extension-wide shutdown function */
 
544
        PHP_RINIT(oci),       /* per-request startup function */
 
545
        PHP_RSHUTDOWN(oci),   /* per-request shutdown function */
 
546
        PHP_MINFO(oci),       /* information function */
 
547
        NO_VERSION_YET,
 
548
        STANDARD_MODULE_PROPERTIES
 
549
};
 
550
/* }}} */
 
551
 
 
552
/* {{{ startup, shutdown and info functions
 
553
*/
 
554
static void php_oci_init_globals(php_oci_globals *oci_globals_p TSRMLS_DC)
 
555
 
556
        OCI(shutdown)   = 0;
 
557
        OCI(in_call)    = 0;
 
558
 
 
559
        CALL_OCI(
 
560
                OCIEnvInit(
 
561
                        &OCI(pEnv), 
 
562
                        OCI_DEFAULT, 
 
563
                        0, 
 
564
                        NULL
 
565
                )
 
566
        );
 
567
        
 
568
        CALL_OCI(
 
569
                OCIHandleAlloc(
 
570
                        OCI(pEnv), 
 
571
                        (dvoid **)&OCI(pError), 
 
572
                        OCI_HTYPE_ERROR, 
 
573
                        0, 
 
574
                        NULL
 
575
                )
 
576
        );
 
577
}
 
578
 
 
579
static int _sessions_pcleanup(zend_llist *session_list TSRMLS_DC)
 
580
{
 
581
        zend_llist_destroy(session_list);
 
582
 
 
583
        return 1;
 
584
}
 
585
 
 
586
static int _session_pcleanup(oci_session *session TSRMLS_DC)
 
587
{
 
588
        _oci_close_session(session);
 
589
 
 
590
        return 1;
 
591
}
 
592
 
 
593
static int _server_pcleanup(oci_server *server TSRMLS_DC)
 
594
{
 
595
        _oci_close_server(server);
 
596
 
 
597
        return 1;
 
598
}
 
599
 
 
600
PHP_MINIT_FUNCTION(oci)
 
601
{
 
602
        zend_class_entry oci_lob_class_entry;
 
603
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
604
        zend_class_entry oci_coll_class_entry;
 
605
#endif
 
606
 
 
607
#ifdef HAVE_OCI8_SHARED_MODE
 
608
 
 
609
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
610
#define PHP_OCI_INIT_MODE_TMP OCI_SHARED | OCI_OBJECT
 
611
#else
 
612
#define PHP_OCI_INIT_MODE_TMP OCI_SHARED
 
613
#endif
 
614
 
 
615
#else
 
616
 
 
617
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
618
#define PHP_OCI_INIT_MODE_TMP OCI_DEFAULT | OCI_OBJECT
 
619
#else
 
620
#define PHP_OCI_INIT_MODE_TMP OCI_DEFAULT
 
621
#endif
 
622
 
 
623
#endif
 
624
 
 
625
#ifdef ZTS
 
626
#define PHP_OCI_INIT_MODE PHP_OCI_INIT_MODE_TMP | OCI_THREADED
 
627
#else
 
628
#define PHP_OCI_INIT_MODE PHP_OCI_INIT_MODE_TMP
 
629
#endif
 
630
 
 
631
        mutex_alloc(mx_lock);
 
632
 
 
633
        persistent_servers = malloc(sizeof(TsHashTable));
 
634
        persistent_sessions = malloc(sizeof(TsHashTable));
 
635
        zend_ts_hash_init(persistent_servers, 13, NULL, (dtor_func_t) _server_pcleanup, 1); 
 
636
        zend_ts_hash_init(persistent_sessions, 13, NULL, (dtor_func_t) _sessions_pcleanup, 1); 
 
637
 
 
638
        OCIInitialize(PHP_OCI_INIT_MODE, NULL, NULL, NULL, NULL);
 
639
 
 
640
#ifdef ZTS
 
641
        ts_allocate_id(&oci_globals_id, sizeof(php_oci_globals), (ts_allocate_ctor) php_oci_init_globals, NULL);
 
642
#else
 
643
        php_oci_init_globals(&oci_globals TSRMLS_CC);
 
644
#endif
 
645
 
 
646
        le_stmt = zend_register_list_destructors_ex(_oci_stmt_list_dtor, NULL, "oci8 statement", module_number);
 
647
        le_conn = zend_register_list_destructors_ex(php_oci_free_conn_list, NULL, "oci8 connection", module_number);
 
648
        le_desc = zend_register_list_destructors_ex(_oci_descriptor_list_dtor, NULL, "oci8 descriptor", module_number);
 
649
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
650
        le_coll = zend_register_list_destructors_ex(_oci_coll_list_dtor, NULL, "oci8 collection", module_number);
 
651
#endif
 
652
        le_server = zend_register_list_destructors_ex(_oci_server_list_dtor, NULL, "oci8 server", module_number);
 
653
        le_session = zend_register_list_destructors_ex(_oci_session_list_dtor, NULL, "oci8 session", module_number);
 
654
 
 
655
        INIT_CLASS_ENTRY(oci_lob_class_entry, "OCI-Lob", php_oci_lob_class_functions);
 
656
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
657
        INIT_CLASS_ENTRY(oci_coll_class_entry, "OCI-Collection", php_oci_coll_class_functions);
 
658
#endif
 
659
 
 
660
        oci_lob_class_entry_ptr = zend_register_internal_class(&oci_lob_class_entry TSRMLS_CC);
 
661
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
662
        oci_coll_class_entry_ptr = zend_register_internal_class(&oci_coll_class_entry TSRMLS_CC);
 
663
#endif
 
664
 
 
665
/* thies@thieso.net 990203 i do not think that we will need all of them - just in here for completeness for now! */
 
666
        REGISTER_LONG_CONSTANT("OCI_DEFAULT",OCI_DEFAULT, CONST_CS | CONST_PERSISTENT);
 
667
        REGISTER_LONG_CONSTANT("OCI_DESCRIBE_ONLY",OCI_DESCRIBE_ONLY, CONST_CS | CONST_PERSISTENT);
 
668
        REGISTER_LONG_CONSTANT("OCI_COMMIT_ON_SUCCESS",OCI_COMMIT_ON_SUCCESS, CONST_CS | CONST_PERSISTENT);
 
669
        REGISTER_LONG_CONSTANT("OCI_EXACT_FETCH",OCI_EXACT_FETCH, CONST_CS | CONST_PERSISTENT);
 
670
 
 
671
/* for $LOB->seek() */
 
672
        REGISTER_LONG_CONSTANT("OCI_SEEK_SET",OCI_SEEK_SET, CONST_CS | CONST_PERSISTENT);
 
673
        REGISTER_LONG_CONSTANT("OCI_SEEK_CUR",OCI_SEEK_CUR, CONST_CS | CONST_PERSISTENT);
 
674
        REGISTER_LONG_CONSTANT("OCI_SEEK_END",OCI_SEEK_END, CONST_CS | CONST_PERSISTENT);
 
675
 
 
676
/*      for $LOB->flush() */
 
677
        REGISTER_LONG_CONSTANT("OCI_LOB_BUFFER_FREE",OCI_LOB_BUFFER_FREE, CONST_CS | CONST_PERSISTENT);
 
678
        
 
679
/* for OCIBindByName (real "oci" names + short "php" names*/
 
680
        REGISTER_LONG_CONSTANT("SQLT_BFILEE",SQLT_BFILEE, CONST_CS | CONST_PERSISTENT);
 
681
        REGISTER_LONG_CONSTANT("SQLT_CFILEE",SQLT_CFILEE, CONST_CS | CONST_PERSISTENT);
 
682
        REGISTER_LONG_CONSTANT("SQLT_CLOB",SQLT_CLOB, CONST_CS | CONST_PERSISTENT);
 
683
        REGISTER_LONG_CONSTANT("SQLT_BLOB",SQLT_BLOB, CONST_CS | CONST_PERSISTENT);
 
684
        REGISTER_LONG_CONSTANT("SQLT_RDD",SQLT_RDD, CONST_CS | CONST_PERSISTENT);
 
685
 
 
686
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
687
        REGISTER_LONG_CONSTANT("OCI_B_NTY",SQLT_NTY, CONST_CS | CONST_PERSISTENT);
 
688
        REGISTER_LONG_CONSTANT("SQLT_NTY",SQLT_NTY, CONST_CS | CONST_PERSISTENT);       
 
689
        REGISTER_STRING_CONSTANT("OCI_SYSDATE","SYSDATE",CONST_PERSISTENT);
 
690
#endif
 
691
 
 
692
        REGISTER_LONG_CONSTANT("OCI_B_BFILE",SQLT_BFILEE, CONST_CS | CONST_PERSISTENT);
 
693
        REGISTER_LONG_CONSTANT("OCI_B_CFILEE",SQLT_CFILEE, CONST_CS | CONST_PERSISTENT);
 
694
        REGISTER_LONG_CONSTANT("OCI_B_CLOB",SQLT_CLOB, CONST_CS | CONST_PERSISTENT);
 
695
        REGISTER_LONG_CONSTANT("OCI_B_BLOB",SQLT_BLOB, CONST_CS | CONST_PERSISTENT);
 
696
        REGISTER_LONG_CONSTANT("OCI_B_ROWID",SQLT_RDD, CONST_CS | CONST_PERSISTENT);
 
697
        REGISTER_LONG_CONSTANT("OCI_B_CURSOR",SQLT_RSET, CONST_CS | CONST_PERSISTENT);
 
698
        REGISTER_LONG_CONSTANT("OCI_B_BIN",SQLT_BIN, CONST_CS | CONST_PERSISTENT);
 
699
 
 
700
/* for OCIFetchStatement */
 
701
        REGISTER_LONG_CONSTANT("OCI_FETCHSTATEMENT_BY_COLUMN", OCI_FETCHSTATEMENT_BY_COLUMN, CONST_CS | CONST_PERSISTENT);
 
702
        REGISTER_LONG_CONSTANT("OCI_FETCHSTATEMENT_BY_ROW", OCI_FETCHSTATEMENT_BY_ROW, CONST_CS | CONST_PERSISTENT);
 
703
 
 
704
/* for OCIFetchInto &  OCIResult */
 
705
        REGISTER_LONG_CONSTANT("OCI_ASSOC",OCI_ASSOC, CONST_CS | CONST_PERSISTENT);
 
706
        REGISTER_LONG_CONSTANT("OCI_NUM",OCI_NUM, CONST_CS | CONST_PERSISTENT);
 
707
        REGISTER_LONG_CONSTANT("OCI_BOTH",OCI_BOTH, CONST_CS | CONST_PERSISTENT);
 
708
        REGISTER_LONG_CONSTANT("OCI_RETURN_NULLS",OCI_RETURN_NULLS, CONST_CS | CONST_PERSISTENT);
 
709
        REGISTER_LONG_CONSTANT("OCI_RETURN_LOBS",OCI_RETURN_LOBS, CONST_CS | CONST_PERSISTENT);
 
710
 
 
711
/* for OCINewDescriptor (real "oci" names + short "php" names*/
 
712
        REGISTER_LONG_CONSTANT("OCI_DTYPE_FILE",OCI_DTYPE_FILE, CONST_CS | CONST_PERSISTENT);
 
713
        REGISTER_LONG_CONSTANT("OCI_DTYPE_LOB",OCI_DTYPE_LOB, CONST_CS | CONST_PERSISTENT);
 
714
        REGISTER_LONG_CONSTANT("OCI_DTYPE_ROWID",OCI_DTYPE_ROWID, CONST_CS | CONST_PERSISTENT);
 
715
 
 
716
        REGISTER_LONG_CONSTANT("OCI_D_FILE",OCI_DTYPE_FILE, CONST_CS | CONST_PERSISTENT);
 
717
        REGISTER_LONG_CONSTANT("OCI_D_LOB",OCI_DTYPE_LOB, CONST_CS | CONST_PERSISTENT);
 
718
        REGISTER_LONG_CONSTANT("OCI_D_ROWID",OCI_DTYPE_ROWID, CONST_CS | CONST_PERSISTENT);
 
719
 
 
720
/* for OCIWriteTemporaryLob */
 
721
#ifdef HAVE_OCI8_TEMP_LOB
 
722
        REGISTER_LONG_CONSTANT("OCI_TEMP_CLOB",OCI_TEMP_CLOB, CONST_CS | CONST_PERSISTENT);
 
723
        REGISTER_LONG_CONSTANT("OCI_TEMP_BLOB",OCI_TEMP_BLOB, CONST_CS | CONST_PERSISTENT);
 
724
#endif
 
725
 
 
726
        return SUCCESS;
 
727
}
 
728
 
 
729
/* ----------------------------------------------------------------- */
 
730
 
 
731
PHP_RINIT_FUNCTION(oci)
 
732
{
 
733
        OCI(debug_mode) = 0; /* start "fresh" */
 
734
/*      OCI(in_call) = 0; i don't think we want this! */
 
735
 
 
736
        oci_debug("php_rinit_oci");
 
737
 
 
738
        return SUCCESS;
 
739
}
 
740
 
 
741
PHP_MSHUTDOWN_FUNCTION(oci)
 
742
{
 
743
        OCI(shutdown) = 1;
 
744
 
 
745
        oci_debug("START php_mshutdown_oci");
 
746
 
 
747
        zend_ts_hash_destroy(persistent_sessions);
 
748
        zend_ts_hash_destroy(persistent_servers);
 
749
 
 
750
        free(persistent_sessions);
 
751
        free(persistent_servers);
 
752
 
 
753
        mutex_free(mx_lock);
 
754
 
 
755
        CALL_OCI(
 
756
                OCIHandleFree(
 
757
                                (dvoid *) OCI(pEnv), 
 
758
                                OCI_HTYPE_ENV
 
759
                )
 
760
        );
 
761
 
 
762
        oci_debug("END   php_mshutdown_oci");
 
763
 
 
764
        return SUCCESS;
 
765
}
 
766
 
 
767
PHP_RSHUTDOWN_FUNCTION(oci)
 
768
{
 
769
        oci_debug("START php_rshutdown_oci");
 
770
 
 
771
#if 0
 
772
        /* XXX free all statements, rollback all outstanding transactions */
 
773
 
 
774
        zend_ts_hash_apply(persistent_sessions, (apply_func_t) _session_cleanup TSRMLS_CC);
 
775
        zend_ts_hash_apply(persistent_servers, (apply_func_t) _server_cleanup TSRMLS_CC);
 
776
#endif
 
777
 
 
778
        oci_debug("END   php_rshutdown_oci");
 
779
 
 
780
        return SUCCESS;
 
781
}
 
782
 
 
783
PHP_MINFO_FUNCTION(oci)
 
784
{
 
785
        char buf[32];
 
786
 
 
787
        php_info_print_table_start();
 
788
        php_info_print_table_row(2, "OCI8 Support", "enabled");
 
789
        php_info_print_table_row(2, "Revision", "$Revision: 1.257.2.8 $");
 
790
 
 
791
        sprintf(buf, "%ld", num_persistent);
 
792
        php_info_print_table_row(2, "Active Persistent Links", buf);
 
793
        sprintf(buf, "%ld", num_links);
 
794
        php_info_print_table_row(2, "Active Links", buf);
 
795
 
 
796
#ifndef PHP_WIN32
 
797
        php_info_print_table_row(2, "Oracle Version", PHP_OCI8_VERSION );
 
798
        php_info_print_table_row(2, "Compile-time ORACLE_HOME", PHP_OCI8_DIR );
 
799
        php_info_print_table_row(2, "Libraries Used", PHP_OCI8_SHARED_LIBADD );
 
800
#endif
 
801
 
 
802
#ifdef HAVE_OCI8_TEMP_LOB
 
803
        php_info_print_table_row(2, "Temporary Lob support", "enabled" );
 
804
#else
 
805
        php_info_print_table_row(2, "Temporary Lob support", "disabled" );
 
806
#endif
 
807
 
 
808
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
809
        php_info_print_table_row(2, "Collections support", "enabled" );
 
810
#else
 
811
        php_info_print_table_row(2, "Collections support", "disabled" );
 
812
#endif
 
813
 
 
814
        php_info_print_table_end();
 
815
}
 
816
/* }}} */
 
817
 
 
818
/* {{{ _oci_define_hash_dtor()
 
819
*/
 
820
static void _oci_define_hash_dtor(void *data)
 
821
{
 
822
        oci_define *define = (oci_define *) data;
 
823
 
 
824
        oci_debug("_oci_define_hash_dtor: %s",define->name);
 
825
 
 
826
        zval_ptr_dtor(&define->zval);
 
827
 
 
828
        if (define->name) {
 
829
                efree(define->name);
 
830
                define->name = 0;
 
831
        }
 
832
}
 
833
/* }}} */
 
834
 
 
835
/* {{{ _oci_desc_flush_hash_dtor()
 
836
 */
 
837
static void _oci_desc_flush_hash_dtor(void *data)
 
838
{
 
839
        oci_descriptor *descr = *(oci_descriptor **)data;
 
840
        TSRMLS_FETCH();
 
841
        
 
842
        if (descr->buffering == 2 && (descr->type == OCI_DTYPE_LOB || descr->type == OCI_DTYPE_FILE)) {
 
843
                oci_lob_flush(descr,OCI_LOB_BUFFER_FREE TSRMLS_CC);
 
844
                descr->buffering = 1;
 
845
        }
 
846
}
 
847
/* }}} */
 
848
 
 
849
/* {{{ _oci_bind_hash_dtor()
 
850
*/
 
851
static void _oci_bind_hash_dtor(void *data)
 
852
{
 
853
        oci_bind *bind = (oci_bind *) data;
 
854
 
 
855
        oci_debug("_oci_bind_hash_dtor:");
 
856
 
 
857
        zval_ptr_dtor(&(bind->zval));
 
858
}
 
859
/* }}} */
 
860
 
 
861
/* {{{ _oci_bind_pre_exec()
 
862
*/
 
863
static int _oci_bind_pre_exec(void *data TSRMLS_DC)
 
864
{
 
865
        oci_bind *bind = (oci_bind *) data;
 
866
 
 
867
        /* reset all bind stuff to a normal state..-. */
 
868
 
 
869
        bind->indicator = 0; 
 
870
 
 
871
        return 0;
 
872
}
 
873
/* }}} */
 
874
 
 
875
/* {{{ _oci_bind_post_exec()
 
876
*/
 
877
static int _oci_bind_post_exec(void *data TSRMLS_DC)
 
878
{
 
879
        oci_bind *bind = (oci_bind *) data;
 
880
 
 
881
        if (bind->indicator == -1) { /* NULL */
 
882
                zval *val = bind->zval;
 
883
                if (Z_TYPE_P(val) == IS_STRING && (Z_STRVAL_P(val) != empty_string)) {
 
884
                        *Z_STRVAL_P(val) = '\0'; /* XXX avoid warning in debug mode */
 
885
                }
 
886
                zval_dtor(val);
 
887
                ZVAL_NULL(val);
 
888
        } else if (Z_TYPE_P(bind->zval) == IS_STRING && (Z_STRVAL_P(bind->zval) != empty_string)) {
 
889
                Z_STRVAL_P(bind->zval) = erealloc(Z_STRVAL_P(bind->zval), Z_STRLEN_P(bind->zval)+1);
 
890
                Z_STRVAL_P(bind->zval)[ Z_STRLEN_P(bind->zval) ] = '\0';
 
891
        }
 
892
 
 
893
 
 
894
        return 0;
 
895
}
 
896
/* }}} */
 
897
 
 
898
/* {{{ _oci_column_hash_dtor()
 
899
*/
 
900
static void _oci_column_hash_dtor(void *data)
 
901
{       
 
902
        oci_out_column *column = (oci_out_column *) data;
 
903
        TSRMLS_FETCH();
 
904
 
 
905
        oci_debug("START _oci_column_hash_dtor: %s",column->name);
 
906
 
 
907
        if (column->stmtid) {
 
908
                zend_list_delete(column->stmtid);
 
909
        }
 
910
 
 
911
        if (column->is_descr) {
 
912
                zend_list_delete(column->descid);
 
913
        } else {
 
914
                if (column->data) {
 
915
                        efree(column->data);
 
916
                }
 
917
        }
 
918
 
 
919
        oci_debug("END   _oci_column_hash_dtor: %s",column->name);
 
920
 
 
921
        if (column->name) {
 
922
                efree(column->name);
 
923
        }
 
924
}
 
925
/* }}} */
 
926
 
 
927
/* {{{ _oci_stmt_list_dtor()
 
928
*/
 
929
static void _oci_stmt_list_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
 
930
{
 
931
        oci_statement *statement = (oci_statement *)rsrc->ptr;
 
932
        oci_debug("START _oci_stmt_list_dtor: id=%d last_query=\"%s\"",statement->id,SAFE_STRING(statement->last_query));
 
933
 
 
934
        if (statement->pStmt) {
 
935
                CALL_OCI(
 
936
                        OCIHandleFree(
 
937
                                statement->pStmt, 
 
938
                                OCI_HTYPE_STMT
 
939
                        )
 
940
                );
 
941
                
 
942
                statement->pStmt = 0;
 
943
        }
 
944
 
 
945
        if (statement->pError) {
 
946
                CALL_OCI(
 
947
                        OCIHandleFree(
 
948
                                statement->pError, 
 
949
                                OCI_HTYPE_ERROR
 
950
                        )
 
951
                );
 
952
                
 
953
                statement->pError = 0;
 
954
        }
 
955
 
 
956
        if (statement->last_query) {
 
957
                efree(statement->last_query);
 
958
        }
 
959
 
 
960
        if (statement->columns) {
 
961
                zend_hash_destroy(statement->columns);
 
962
                efree(statement->columns);
 
963
        }
 
964
 
 
965
        if (statement->binds) {
 
966
                zend_hash_destroy(statement->binds);
 
967
                efree(statement->binds);
 
968
        }
 
969
 
 
970
        if (statement->defines) {
 
971
                zend_hash_destroy(statement->defines);
 
972
                efree(statement->defines);
 
973
        }
 
974
 
 
975
        oci_debug("END   _oci_stmt_list_dtor: id=%d",statement->id);
 
976
 
 
977
        efree(statement);
 
978
}
 
979
/* }}} */
 
980
 
 
981
/* {{{ _oci_conn_list_dtor()
 
982
*/
 
983
static void _oci_conn_list_dtor(oci_connection *connection TSRMLS_DC)
 
984
{
 
985
        /* 
 
986
           as the connection is "only" a in memory service context we do not disconnect from oracle.
 
987
        */
 
988
 
 
989
        oci_debug("START _oci_conn_list_dtor: id=%d",connection->id);
 
990
 
 
991
        if (connection->pServiceContext) {
 
992
 
 
993
                if (connection->needs_commit) {
 
994
                        oci_debug("OCITransRollback");
 
995
                        CALL_OCI_RETURN(connection->error,
 
996
                                OCITransRollback(
 
997
                                        connection->pServiceContext,
 
998
                                        connection->pError,
 
999
                                        (ub4)0
 
1000
                                )
 
1001
                        );
 
1002
         
 
1003
                        if (connection->error) {
 
1004
                                oci_error(connection->pError, "failed to rollback outstanding transactions!", connection->error);
 
1005
                        }
 
1006
                        connection->needs_commit = 0;
 
1007
                } else {
 
1008
                        oci_debug("nothing to do..");
 
1009
                }
 
1010
 
 
1011
                CALL_OCI(
 
1012
                        OCIHandleFree(
 
1013
                                (dvoid *) connection->pServiceContext, 
 
1014
                                (ub4) OCI_HTYPE_SVCCTX
 
1015
                        )
 
1016
                );
 
1017
        }
 
1018
 
 
1019
        if (connection->pError) {
 
1020
                CALL_OCI(
 
1021
                        OCIHandleFree(
 
1022
                                (dvoid *) connection->pError, 
 
1023
                                (ub4) OCI_HTYPE_ERROR
 
1024
                        )
 
1025
                );
 
1026
        }
 
1027
 
 
1028
        if (connection->session && connection->session->exclusive) {
 
1029
                /* close associated session when destructed */
 
1030
                zend_list_delete(connection->session->num);
 
1031
        }
 
1032
 
 
1033
        if (connection->descriptors) {
 
1034
                zend_hash_destroy(connection->descriptors);
 
1035
                efree(connection->descriptors);
 
1036
        }
 
1037
 
 
1038
        oci_debug("END   _oci_conn_list_dtor: id=%d",connection->id);
 
1039
 
 
1040
        efree(connection);
 
1041
}
 
1042
/* }}} */
 
1043
 
 
1044
/* {{{ php_oci_free_conn_list
 
1045
 */
 
1046
static void php_oci_free_conn_list(zend_rsrc_list_entry *rsrc TSRMLS_DC)
 
1047
{
 
1048
        oci_connection *conn = (oci_connection *)rsrc->ptr;
 
1049
        _oci_conn_list_dtor(conn TSRMLS_CC);
 
1050
}
 
1051
/* }}} */
 
1052
 
 
1053
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
1054
 
 
1055
/* {{{ _oci_coll_list_dtor()
 
1056
 */
 
1057
static void _oci_coll_list_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
 
1058
{
 
1059
        oci_collection *coll = (oci_collection *)rsrc->ptr;
 
1060
        oci_debug("START _oci_coll_list_dtor: %d",coll->id);
 
1061
 
 
1062
        /* Note sure if we need to free the object.  Have an
 
1063
           oracle TAR out on this one.
 
1064
           OCIDescriptorFree(descr->ocidescr, descr->type); */
 
1065
 
 
1066
        oci_debug("END   _oci_coll_list_dtor: %d",coll->id);
 
1067
 
 
1068
        efree(coll);
 
1069
}
 
1070
/* }}} */
 
1071
#endif
 
1072
 
 
1073
/* {{{ _oci_descriptor_list_dtor()
 
1074
 */
 
1075
static void _oci_descriptor_list_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
 
1076
{
 
1077
        oci_descriptor *descr = (oci_descriptor *)rsrc->ptr;
 
1078
        oci_debug("START _oci_descriptor_list_dtor: %d",descr->id);
 
1079
 
 
1080
        /* flushing Lobs & Files with buffering enabled */
 
1081
        if ((descr->type == OCI_DTYPE_FILE || descr->type == OCI_DTYPE_LOB) && descr->buffering == 2) {
 
1082
                oci_debug("descriptor #%d needs to be flushed. flushing..",descr->id);
 
1083
                oci_lob_flush(descr,OCI_LOB_BUFFER_FREE TSRMLS_CC);
 
1084
        }
 
1085
 
 
1086
        CALL_OCI(
 
1087
                OCIDescriptorFree(
 
1088
                        descr->ocidescr, 
 
1089
                        descr->type
 
1090
                )
 
1091
        );
 
1092
 
 
1093
        oci_debug("END   _oci_descriptor_list_dtor: %d",descr->id);
 
1094
 
 
1095
        efree(descr);
 
1096
}
 
1097
/* }}} */
 
1098
 
 
1099
/* {{{ _oci_server_list_dtor()
 
1100
 */
 
1101
static void _oci_server_list_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
 
1102
{
 
1103
#if 0
 
1104
        oci_server *server = (oci_server *)rsrc->ptr;
 
1105
        if (server->persistent)
 
1106
                return;
 
1107
 
 
1108
        _oci_close_server(server);
 
1109
#endif
 
1110
}
 
1111
/* }}} */
 
1112
 
 
1113
/* {{{ _oci_session_list_dtor()
 
1114
 */
 
1115
static void _oci_session_list_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
 
1116
{
 
1117
        oci_session *session = (oci_session *)rsrc->ptr;
 
1118
        if (session->persistent) {
 
1119
                /* clear thread assignment */
 
1120
                session->thread = 0;
 
1121
                return;
 
1122
        }
 
1123
 
 
1124
        _oci_close_session(session);
 
1125
}
 
1126
/* }}} */
 
1127
 
 
1128
/* {{{ oci_handle_error
 
1129
 */
 
1130
static ub4 oci_handle_error(oci_connection *connection, ub4 errcode)
 
1131
{
 
1132
        switch (errcode) {
 
1133
                case 1013: /* user requested cancel of current operation */
 
1134
                        zend_bailout();
 
1135
                        break;
 
1136
 
 
1137
                case 22:   /* ORA-00022 Invalid session id */
 
1138
                case 1012: /* ORA-01012: */
 
1139
                case 3113: /* ORA-03113: end-of-file on communication channel */
 
1140
                case 604:
 
1141
                case 1041:
 
1142
                        connection->is_open = 0;
 
1143
                        connection->session->is_open = 0;
 
1144
                        connection->session->server->is_open = 0;
 
1145
                        return 1; /* fatal error */
 
1146
        }
 
1147
 
 
1148
        return 0; /* no fatal error */
 
1149
}
 
1150
/* }}} */
 
1151
 
 
1152
/* {{{ oci_error()
 
1153
*/
 
1154
static ub4 oci_error(OCIError *err_p, char *what, sword status)
 
1155
{
 
1156
        text errbuf[512];
 
1157
        sb4 errcode = 0;
 
1158
        TSRMLS_FETCH();
 
1159
 
 
1160
        switch (status) {
 
1161
                case OCI_SUCCESS:
 
1162
                        break;
 
1163
                case OCI_SUCCESS_WITH_INFO:
 
1164
                        CALL_OCI(
 
1165
                                OCIErrorGet(
 
1166
                                        err_p, 
 
1167
                                        (ub4)1, 
 
1168
                                        NULL, 
 
1169
                                        &errcode, 
 
1170
                                        errbuf,
 
1171
                                        (ub4)sizeof(errbuf), 
 
1172
                                        (ub4)OCI_HTYPE_ERROR
 
1173
                                )
 
1174
                        );
 
1175
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s: OCI_SUCCESS_WITH_INFO: %s", what, errbuf);
 
1176
                        break;
 
1177
                case OCI_NEED_DATA:
 
1178
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s: OCI_NEED_DATA", what);
 
1179
                        break;
 
1180
                case OCI_NO_DATA:
 
1181
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s: OCI_NO_DATA", what);
 
1182
                        break;
 
1183
                case OCI_ERROR: {
 
1184
                        CALL_OCI(
 
1185
                                OCIErrorGet(
 
1186
                                        err_p, 
 
1187
                                        (ub4)1, 
 
1188
                                        NULL, 
 
1189
                                        &errcode, 
 
1190
                                        errbuf,
 
1191
                                        (ub4)sizeof(errbuf), 
 
1192
                                        (ub4)OCI_HTYPE_ERROR
 
1193
                                )
 
1194
                        );
 
1195
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s: %s", what, errbuf);
 
1196
                        break;
 
1197
                }
 
1198
                case OCI_INVALID_HANDLE:
 
1199
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s: OCI_INVALID_HANDLE", what);
 
1200
                        break;
 
1201
                case OCI_STILL_EXECUTING:
 
1202
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s: OCI_STILL_EXECUTING", what);
 
1203
                        break;
 
1204
                case OCI_CONTINUE:
 
1205
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s: OCI_CONTINUE", what);
 
1206
                        break;
 
1207
                default:
 
1208
                        break;
 
1209
        }
 
1210
        return errcode;
 
1211
}
 
1212
/* }}} */
 
1213
 
 
1214
/* {{{ oci_ping()
 
1215
*/
 
1216
static int oci_ping(oci_server *server)
 
1217
{
 
1218
        char version[256];
 
1219
        TSRMLS_FETCH();
 
1220
        
 
1221
        CALL_OCI_RETURN(OCI(error),
 
1222
                OCIServerVersion(
 
1223
                        server->pServer,
 
1224
                        OCI(pError), 
 
1225
                        (text*)version, 
 
1226
                        sizeof(version),
 
1227
                        OCI_HTYPE_SERVER
 
1228
                )
 
1229
        );
 
1230
 
 
1231
        if (OCI(error) == OCI_SUCCESS) {
 
1232
                return 1;
 
1233
        }
 
1234
 
 
1235
        oci_error(OCI(pError), "oci_ping", OCI(error));
 
1236
 
 
1237
        return 0;
 
1238
}
 
1239
/* }}} */
 
1240
 
 
1241
/************************* INTERNAL FUNCTIONS *************************/
 
1242
 
 
1243
/* {{{ oci_debug()
 
1244
*/
 
1245
static void oci_debug(const char *format, ...)
 
1246
{
 
1247
        TSRMLS_FETCH();
 
1248
 
 
1249
        if (OCI(debug_mode)) {
 
1250
                char buffer[1024];
 
1251
                va_list args;
 
1252
 
 
1253
                va_start(args, format);
 
1254
                vsnprintf(buffer, sizeof(buffer)-1, format, args);
 
1255
                va_end(args);
 
1256
                buffer[sizeof(buffer)-1] = '\0';
 
1257
                php_printf("OCIDebug: %s<br />\n", buffer);
 
1258
        }
 
1259
}
 
1260
/* }}} */
 
1261
 
 
1262
/* {{{ oci_get_conn()
 
1263
*/
 
1264
static oci_connection *oci_get_conn(zval **conn TSRMLS_DC)
 
1265
{
 
1266
        oci_connection *connection;
 
1267
 
 
1268
        connection = (oci_connection *) zend_fetch_resource(conn TSRMLS_CC, -1, "OCI8-Connection", NULL, 1, le_conn);
 
1269
 
 
1270
        if (connection && connection->is_open) {
 
1271
                return connection;
 
1272
        } else {
 
1273
                return (oci_connection *) NULL;
 
1274
        }
 
1275
}
 
1276
/* }}} */
 
1277
 
 
1278
/* {{{ oci_get_stmt()
 
1279
*/
 
1280
static oci_statement *oci_get_stmt(zval **stmt TSRMLS_DC)
 
1281
{
 
1282
        oci_statement *statement;
 
1283
 
 
1284
        statement = (oci_statement *) zend_fetch_resource(stmt TSRMLS_CC, -1, "OCI8-Statement", NULL, 1, le_stmt);
 
1285
 
 
1286
        if (statement && statement->conn->is_open) {
 
1287
                return statement;
 
1288
        } else {
 
1289
                return (oci_statement *) NULL;
 
1290
        }
 
1291
}
 
1292
/* }}} */
 
1293
 
 
1294
/* {{{ oci_get_desc()
 
1295
*/
 
1296
static oci_descriptor *oci_get_desc(int ind TSRMLS_DC)
 
1297
{
 
1298
        oci_descriptor *descriptor;
 
1299
        int actual_resource_type;
 
1300
 
 
1301
        descriptor = (oci_descriptor *) zend_list_find(ind, &actual_resource_type);
 
1302
 
 
1303
        if (descriptor && (actual_resource_type == le_desc)) {
 
1304
                return descriptor;
 
1305
        } else {
 
1306
                return (oci_descriptor *) NULL;
 
1307
        }
 
1308
}
 
1309
/* }}} */
 
1310
 
 
1311
/* {{{ oci_get_col()
 
1312
*/
 
1313
static oci_out_column *oci_get_col(oci_statement *statement, int col, zval **value)
 
1314
{
 
1315
        oci_out_column *outcol = NULL;
 
1316
        int i;
 
1317
        TSRMLS_FETCH();
 
1318
 
 
1319
        if (statement->columns == 0) { /* we release the columns at the end of a fetch */
 
1320
                return NULL;
 
1321
        }
 
1322
 
 
1323
        if (value) {
 
1324
                if (Z_TYPE_PP(value) == IS_STRING) {
 
1325
                        for (i = 0; i < statement->ncolumns; i++) {
 
1326
                                outcol = oci_get_col(statement, i + 1, 0);
 
1327
                                if (outcol == NULL) {
 
1328
                                        continue;
 
1329
                                } else if (((int) outcol->name_len == Z_STRLEN_PP(value)) && 
 
1330
                                                        (!strncmp(outcol->name, Z_STRVAL_PP(value), Z_STRLEN_PP(value)))
 
1331
                                ) {
 
1332
                                        return outcol;
 
1333
                                }
 
1334
                        }
 
1335
                } else {
 
1336
                        convert_to_long_ex(value);
 
1337
                        return oci_get_col(statement,Z_LVAL_PP(value),0);
 
1338
                }
 
1339
        } else if (col != -1) {
 
1340
                if (zend_hash_index_find(statement->columns, col, (void **)&outcol) == FAILURE) {
 
1341
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid column %d", col);
 
1342
                        return NULL;
 
1343
                }
 
1344
                return outcol;
 
1345
        }
 
1346
 
 
1347
        return NULL;
 
1348
}
 
1349
/* }}} */
 
1350
 
 
1351
/* {{{ oci_new_desc()
 
1352
*/
 
1353
static oci_descriptor *oci_new_desc(int type, oci_connection *connection)
 
1354
{
 
1355
        oci_descriptor *descr;
 
1356
        TSRMLS_FETCH();
 
1357
 
 
1358
        descr = emalloc(sizeof(oci_descriptor));
 
1359
        
 
1360
        descr->type = type;
 
1361
 
 
1362
        switch (descr->type) {
 
1363
                case OCI_DTYPE_FILE:
 
1364
                case OCI_DTYPE_LOB:
 
1365
                case OCI_DTYPE_ROWID:
 
1366
                        break;
 
1367
 
 
1368
                default:
 
1369
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown descriptor type %d.", descr->type);
 
1370
                        efree(descr);
 
1371
                        return 0;
 
1372
        }
 
1373
        
 
1374
        CALL_OCI_RETURN(OCI(error),
 
1375
                OCIDescriptorAlloc(
 
1376
                        connection->session->pEnv,
 
1377
                        (dvoid*)&(descr->ocidescr), 
 
1378
                        descr->type, 
 
1379
                        (size_t) 0, 
 
1380
                        (dvoid **) 0
 
1381
                )
 
1382
        );
 
1383
        
 
1384
        if (OCI(error)) {
 
1385
                ub4 error;
 
1386
                error = oci_error(OCI(pError),"OCIDescriptorAlloc %d",OCI(error));
 
1387
                oci_handle_error(connection, error);
 
1388
                efree(descr);
 
1389
                return 0;
 
1390
        }
 
1391
 
 
1392
        descr->id = zend_list_insert(descr,le_desc);
 
1393
        descr->conn = connection;
 
1394
        descr->lob_current_position = 0;
 
1395
        descr->lob_size = -1;                           /* we should set it to -1 to know, that it's just not initialized */
 
1396
        descr->buffering = 0;                           /* buffering is off by default */
 
1397
        zend_list_addref(connection->id);
 
1398
   
 
1399
        if (descr->type == OCI_DTYPE_LOB || descr->type == OCI_DTYPE_FILE) {
 
1400
                /* add Lobs & Files to hash. we'll flush them ate the end */
 
1401
                if (!connection->descriptors) {
 
1402
                        ALLOC_HASHTABLE(connection->descriptors);
 
1403
                        zend_hash_init(connection->descriptors, 13, NULL, _oci_desc_flush_hash_dtor, 0);
 
1404
                }
 
1405
 
 
1406
                zend_hash_next_index_insert(connection->descriptors,&descr,sizeof(oci_descriptor *),NULL);
 
1407
        }
 
1408
        oci_debug("oci_new_desc %d",descr->id);
 
1409
 
 
1410
        return descr;
 
1411
}
 
1412
/* }}} */
 
1413
 
 
1414
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
1415
 
 
1416
/* {{{ _oci_get_ocicoll()
 
1417
*/
 
1418
static int _oci_get_ocicoll(zval *id,oci_collection **collection TSRMLS_DC)
 
1419
{
 
1420
        zval **coll;
 
1421
        
 
1422
        if (zend_hash_find(Z_OBJPROP_P(id), "collection", sizeof("collection"), (void **)&coll) == FAILURE) {
 
1423
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot find collection");
 
1424
                return 0;
 
1425
        }
 
1426
        if ((*collection = oci_get_coll(Z_LVAL_PP(coll) TSRMLS_CC)) == NULL) {
 
1427
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "collection not found");
 
1428
                return 0;
 
1429
        }
 
1430
 
 
1431
        return Z_LVAL_PP(coll);
 
1432
}
 
1433
/* }}} */
 
1434
 
 
1435
#endif
 
1436
 
 
1437
/* {{{ _oci_get_ocidesc()
 
1438
*/
 
1439
static int _oci_get_ocidesc(zval *id,oci_descriptor **descriptor TSRMLS_DC)
 
1440
{
 
1441
        zval **desc;
 
1442
        
 
1443
        if (zend_hash_find(Z_OBJPROP_P(id), "descriptor", sizeof("descriptor"), (void **)&desc) == FAILURE) {
 
1444
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot find descriptor");
 
1445
                return 0;
 
1446
        }
 
1447
 
 
1448
        if ((*descriptor = oci_get_desc(Z_LVAL_PP(desc) TSRMLS_CC)) == NULL) {
 
1449
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "descriptor not found");
 
1450
                return 0;
 
1451
        }
 
1452
        
 
1453
        return Z_LVAL_PP(desc);
 
1454
}
 
1455
/* }}} */
 
1456
 
 
1457
/* {{{ _oci_make_zval()
 
1458
*/
 
1459
static int _oci_make_zval(zval *value,oci_statement *statement,oci_out_column *column, char *func, int mode TSRMLS_DC)
 
1460
{
 
1461
        oci_descriptor *descr;
 
1462
        ub4 loblen;
 
1463
        int size;
 
1464
        char *buffer;
 
1465
        
 
1466
        if (column->indicator || column->retcode)
 
1467
                if ((column->indicator != -1) && (column->retcode != 1405))
 
1468
                        oci_debug("_oci_make_zval: %16s,retlen = %4d,retlen4 = %d,storage_size4 = %4d,indicator %4d, retcode = %4d",
 
1469
                                        column->name,column->retlen,column->retlen4,column->storage_size4,column->indicator,column->retcode);
 
1470
        
 
1471
        if ((!statement->has_data) || (column->indicator == -1)) { /* column is NULL or statment has no current data */
 
1472
                ZVAL_NULL(value); 
 
1473
                return 0;
 
1474
        }
 
1475
        
 
1476
        if (column->is_cursor) { /* REFCURSOR -> simply return the statement id */
 
1477
                ZVAL_RESOURCE(value,column->stmtid);
 
1478
                zend_list_addref(column->stmtid);
 
1479
        } else if (column->is_descr) {
 
1480
                if ((column->data_type != SQLT_RDD) && (mode & OCI_RETURN_LOBS)) {
 
1481
                        /* OCI_RETURN_LOBS means that we want the content of the LOB back instead of the locator */
 
1482
 
 
1483
                        descr = oci_get_desc(column->descid TSRMLS_CC);
 
1484
                        if (!descr) {
 
1485
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to find my descriptor %p",column->data);
 
1486
                                return -1;
 
1487
                        }
 
1488
                        
 
1489
                        if (oci_loadlob(statement->conn,descr,&buffer,&loblen)) {
 
1490
                                ZVAL_FALSE(value);
 
1491
                        } else {
 
1492
                                ZVAL_STRINGL(value,buffer,loblen,0);
 
1493
                        } 
 
1494
                } else { 
 
1495
                        /* return the locator */
 
1496
                        object_init_ex(value, oci_lob_class_entry_ptr);
 
1497
                        add_property_resource(value, "descriptor", column->descid);
 
1498
                        zend_list_addref(column->descid);
 
1499
                }
 
1500
        } else {
 
1501
                switch (column->retcode) {
 
1502
                case 0: 
 
1503
                        /* intact value */
 
1504
                        if (column->piecewise) {
 
1505
                                size = column->retlen4;
 
1506
                        } else {
 
1507
                                size = column->retlen;
 
1508
                        }
 
1509
                        break;
 
1510
                        
 
1511
                default:                                
 
1512
                        /* XXX we SHOULD maybe have a different behaviour for unknown results! */
 
1513
                        ZVAL_FALSE(value); 
 
1514
                        return 0;
 
1515
                }
 
1516
                
 
1517
                ZVAL_STRINGL(value,column->data,size,1);
 
1518
        }
 
1519
 
 
1520
        return 0;
 
1521
}
 
1522
/* }}} */
 
1523
 
 
1524
/* {{{ oci_setprefetch()
 
1525
*/
 
1526
static int oci_setprefetch(oci_statement *statement,int size)
 
1527
 
1528
        ub4   prefetch;
 
1529
        sword error;
 
1530
        TSRMLS_FETCH();
 
1531
 
 
1532
        prefetch = size * 1024;
 
1533
 
 
1534
        CALL_OCI_RETURN(error,
 
1535
                OCIAttrSet(
 
1536
                        statement->pStmt,
 
1537
                        OCI_HTYPE_STMT,
 
1538
                        &prefetch,
 
1539
                        0, 
 
1540
                        OCI_ATTR_PREFETCH_MEMORY,
 
1541
                        statement->pError
 
1542
                )
 
1543
        );
 
1544
 
 
1545
        statement->error = oci_error(statement->pError, "OCIAttrSet OCI_ATTR_PREFETCH_MEMORY", error);
 
1546
 
 
1547
        if (statement->error) {
 
1548
                oci_handle_error(statement->conn, statement->error);
 
1549
        }
 
1550
 
 
1551
        prefetch = size;
 
1552
        CALL_OCI_RETURN(error,
 
1553
                OCIAttrSet(
 
1554
                        statement->pStmt,
 
1555
                        OCI_HTYPE_STMT,
 
1556
                        &prefetch,
 
1557
                        0, 
 
1558
                        OCI_ATTR_PREFETCH_ROWS,
 
1559
                        statement->pError
 
1560
                )
 
1561
        );
 
1562
 
 
1563
        statement->error = oci_error(statement->pError, "OCIAttrSet OCI_ATTR_PREFETCH_MEMORY", error);
 
1564
        if (statement->error) {
 
1565
                oci_handle_error(statement->conn, statement->error);
 
1566
        }
 
1567
 
 
1568
        return 1;
 
1569
}
 
1570
/* }}} */
 
1571
 
 
1572
/* {{{ oci_parse()
 
1573
*/
 
1574
static oci_statement *oci_parse(oci_connection *connection, char *query, int len)
 
1575
{
 
1576
        oci_statement *statement;
 
1577
        sword error;
 
1578
        TSRMLS_FETCH();
 
1579
 
 
1580
        statement = ecalloc(1,sizeof(oci_statement));
 
1581
 
 
1582
        CALL_OCI(
 
1583
                OCIHandleAlloc(
 
1584
                        connection->session->pEnv,
 
1585
                        (dvoid **)&statement->pStmt,
 
1586
                        OCI_HTYPE_STMT, 
 
1587
                        0, 
 
1588
                        NULL
 
1589
                )
 
1590
        );
 
1591
 
 
1592
        CALL_OCI(
 
1593
                OCIHandleAlloc(
 
1594
                        connection->session->pEnv,
 
1595
                        (dvoid **)&statement->pError,
 
1596
                        OCI_HTYPE_ERROR,
 
1597
                        0,
 
1598
                        NULL
 
1599
                )
 
1600
        );
 
1601
 
 
1602
        if (len > 0) {
 
1603
                CALL_OCI_RETURN(error, 
 
1604
                        OCIStmtPrepare(
 
1605
                                statement->pStmt,
 
1606
                                connection->pError,
 
1607
                                (text*)query,
 
1608
                                len,
 
1609
                                OCI_NTV_SYNTAX,
 
1610
                                OCI_DEFAULT
 
1611
                        )
 
1612
                );
 
1613
 
 
1614
                connection->error = oci_error(connection->pError, "OCIParse", error);
 
1615
                if (connection->error) {
 
1616
                        CALL_OCI(
 
1617
                                OCIHandleFree(
 
1618
                                        statement->pStmt, 
 
1619
                                        OCI_HTYPE_STMT
 
1620
                                )
 
1621
                        );
 
1622
                        
 
1623
                        CALL_OCI(
 
1624
                                OCIHandleFree(
 
1625
                                        statement->pError, 
 
1626
                                        OCI_HTYPE_ERROR
 
1627
                                )
 
1628
                        );
 
1629
 
 
1630
                        efree(statement);
 
1631
                        oci_handle_error(connection, connection->error);
 
1632
                        return 0;
 
1633
                }
 
1634
        }
 
1635
 
 
1636
        if (query) {
 
1637
                statement->last_query = estrdup(query);
 
1638
        }
 
1639
 
 
1640
        statement->conn = connection;
 
1641
        statement->has_data = 0;
 
1642
 
 
1643
        statement->id = zend_list_insert(statement,le_stmt);
 
1644
 
 
1645
        oci_debug("oci_parse \"%s\" id=%d conn=%d",
 
1646
                                SAFE_STRING(query),
 
1647
                                statement->id,
 
1648
                                statement->conn->id);
 
1649
 
 
1650
        zend_list_addref(statement->conn->id);
 
1651
 
 
1652
        return statement;
 
1653
}
 
1654
/* }}} */
 
1655
 
 
1656
/* {{{ oci_execute()
 
1657
*/
 
1658
static int oci_execute(oci_statement *statement, char *func,ub4 mode)
 
1659
{
 
1660
        oci_out_column *outcol;
 
1661
        oci_out_column column;
 
1662
        OCIParam *param = 0;
 
1663
        text *colname;
 
1664
        ub4 counter;
 
1665
        ub2 define_type;
 
1666
        ub4 iters;
 
1667
        ub4 colcount;
 
1668
        ub2 dynamic;
 
1669
        int dtype;
 
1670
        dvoid *buf;
 
1671
        oci_descriptor *descr;
 
1672
        sword error;
 
1673
        TSRMLS_FETCH();
 
1674
 
 
1675
        if (!statement->stmttype) {
 
1676
                CALL_OCI_RETURN(error,
 
1677
                        OCIAttrGet(
 
1678
                                (dvoid *)statement->pStmt,
 
1679
                                OCI_HTYPE_STMT,
 
1680
                                (ub2 *)&statement->stmttype,
 
1681
                                (ub4 *)0,
 
1682
                                OCI_ATTR_STMT_TYPE,
 
1683
                                statement->pError
 
1684
                        )
 
1685
                );
 
1686
 
 
1687
                statement->error = oci_error(statement->pError, "OCIAttrGet OCI_HTYPE_STMT/OCI_ATTR_STMT_TYPE", error);
 
1688
 
 
1689
                if (statement->error) {
 
1690
                        oci_handle_error(statement->conn, statement->error);
 
1691
                        return 0;
 
1692
                }
 
1693
        }
 
1694
 
 
1695
        if (statement->stmttype == OCI_STMT_SELECT) {
 
1696
                iters = 0;
 
1697
        } else {
 
1698
                iters = 1;
 
1699
        }
 
1700
        
 
1701
        if (statement->last_query) { 
 
1702
                /* if we execute refcursors we don't have a query and 
 
1703
                   we don't want to execute!!! */
 
1704
 
 
1705
                if (statement->binds) {
 
1706
                        zend_hash_apply(statement->binds, (apply_func_t) _oci_bind_pre_exec TSRMLS_CC);
 
1707
                }
 
1708
 
 
1709
                CALL_OCI_RETURN(error,
 
1710
                        OCIStmtExecute(
 
1711
                                statement->conn->pServiceContext,
 
1712
                                statement->pStmt,
 
1713
                                statement->pError,
 
1714
                                iters,
 
1715
                                0,
 
1716
                                NULL,
 
1717
                                NULL,
 
1718
                                mode
 
1719
                        )
 
1720
                );
 
1721
 
 
1722
                statement->error = oci_error(statement->pError, "OCIStmtExecute", error);
 
1723
 
 
1724
                if (statement->binds) {
 
1725
                        zend_hash_apply(statement->binds, (apply_func_t) _oci_bind_post_exec TSRMLS_CC);
 
1726
                }
 
1727
 
 
1728
                oci_handle_error(statement->conn, statement->error);
 
1729
 
 
1730
                if (statement->error) {
 
1731
                        return 0;
 
1732
                }
 
1733
 
 
1734
                if (mode & OCI_COMMIT_ON_SUCCESS) {
 
1735
                        statement->conn->needs_commit = 0;
 
1736
                } else {
 
1737
                        statement->conn->needs_commit = 1;
 
1738
                }
 
1739
        }
 
1740
 
 
1741
        if ((statement->stmttype == OCI_STMT_SELECT) && (statement->executed == 0)) {
 
1742
                /* we only need to do the define step is this very statement is executed the first time! */
 
1743
                statement->executed = 1;
 
1744
                
 
1745
                ALLOC_HASHTABLE(statement->columns);
 
1746
                zend_hash_init(statement->columns, 13, NULL, _oci_column_hash_dtor, 0);
 
1747
                
 
1748
                counter = 1;
 
1749
 
 
1750
                CALL_OCI_RETURN(error,
 
1751
                        OCIAttrGet(
 
1752
                                (dvoid *)statement->pStmt,
 
1753
                                OCI_HTYPE_STMT,
 
1754
                                (dvoid *)&colcount,
 
1755
                                (ub4 *)0,
 
1756
                                OCI_ATTR_PARAM_COUNT,
 
1757
                                statement->pError
 
1758
                        )
 
1759
                );
 
1760
 
 
1761
                statement->error = oci_error(statement->pError, "OCIAttrGet OCI_HTYPE_STMT/OCI_ATTR_PARAM_COUNT", error);
 
1762
                if (statement->error) {
 
1763
                        oci_handle_error(statement->conn, statement->error);
 
1764
                        return 0; /* XXX we loose memory!!! */
 
1765
                }
 
1766
 
 
1767
                statement->ncolumns = colcount;
 
1768
                
 
1769
                for (counter = 1; counter <= colcount; counter++) {
 
1770
                        memset(&column,0,sizeof(oci_out_column));
 
1771
                        
 
1772
                        if (zend_hash_index_update(statement->columns, counter, &column,
 
1773
                                                                                sizeof(oci_out_column), (void**) &outcol) == FAILURE) {
 
1774
                                efree(statement->columns);
 
1775
                                /* out of memory */
 
1776
                                return 0;
 
1777
                        } 
 
1778
                        
 
1779
                        outcol->statement = statement;
 
1780
 
 
1781
                        CALL_OCI_RETURN(error,
 
1782
                                OCIParamGet(
 
1783
                                        (dvoid *)statement->pStmt,
 
1784
                                        OCI_HTYPE_STMT,
 
1785
                                        statement->pError,
 
1786
                                        (dvoid**)&param,
 
1787
                                        counter
 
1788
                                )
 
1789
                        );
 
1790
 
 
1791
                        statement->error = oci_error(statement->pError, "OCIParamGet OCI_HTYPE_STMT", error);
 
1792
                        if (statement->error) {
 
1793
                                oci_handle_error(statement->conn, statement->error);
 
1794
                                return 0; /* XXX we loose memory!!! */
 
1795
                        }
 
1796
 
 
1797
                        CALL_OCI_RETURN(error,
 
1798
                                OCIAttrGet(
 
1799
                                        (dvoid *)param,
 
1800
                                        OCI_DTYPE_PARAM,
 
1801
                                        (dvoid *)&outcol->data_type,
 
1802
                                        (ub4 *)0,
 
1803
                                        OCI_ATTR_DATA_TYPE,
 
1804
                                        statement->pError
 
1805
                                )
 
1806
                        );
 
1807
 
 
1808
                        statement->error = oci_error(statement->pError, "OCIAttrGet OCI_DTYPE_PARAM/OCI_ATTR_DATA_TYPE", error);
 
1809
                        if (statement->error) {
 
1810
                                oci_handle_error(statement->conn, statement->error);
 
1811
                                return 0; /* XXX we loose memory!!! */
 
1812
                        }
 
1813
 
 
1814
                        CALL_OCI_RETURN(error,
 
1815
                                OCIAttrGet(
 
1816
                                        (dvoid *)param,
 
1817
                                        OCI_DTYPE_PARAM,
 
1818
                                        (dvoid *)&outcol->data_size,
 
1819
                                        (dvoid *)0,
 
1820
                                        OCI_ATTR_DATA_SIZE,
 
1821
                                        statement->pError
 
1822
                                )
 
1823
                        );
 
1824
 
 
1825
                        statement->error = oci_error(statement->pError, "OCIAttrGet OCI_DTYPE_PARAM/OCI_ATTR_DATA_SIZE", error);
 
1826
                        if (statement->error) {
 
1827
                                oci_handle_error(statement->conn, statement->error);
 
1828
                                return 0; /* XXX we loose memory!!! */
 
1829
                        }
 
1830
 
 
1831
                        outcol->storage_size4 = outcol->data_size;
 
1832
                        outcol->retlen = outcol->data_size;
 
1833
        
 
1834
                        CALL_OCI_RETURN(error,
 
1835
                                OCIAttrGet(
 
1836
                                        (dvoid *)param, 
 
1837
                                        OCI_DTYPE_PARAM, 
 
1838
                                        (dvoid *)&outcol->scale, 
 
1839
                                        (dvoid *)0, 
 
1840
                                        OCI_ATTR_SCALE, 
 
1841
                                        statement->pError
 
1842
                                )
 
1843
                        );
 
1844
                                                
 
1845
                        statement->error = oci_error(statement->pError, "OCIAttrGet OCI_DTYPE_PARAM/OCI_ATTR_SCALE", error);
 
1846
                        if (statement->error) {
 
1847
                                oci_handle_error(statement->conn, statement->error);
 
1848
                                return 0; /* XXX we lose memory!!! */
 
1849
                        }
 
1850
 
 
1851
                        CALL_OCI_RETURN(error,
 
1852
                                OCIAttrGet(
 
1853
                                        (dvoid *)param, 
 
1854
                                        OCI_DTYPE_PARAM, 
 
1855
                                        (dvoid *)&outcol->precision, 
 
1856
                                        (dvoid *)0, 
 
1857
                                        OCI_ATTR_PRECISION, 
 
1858
                                        statement->pError
 
1859
                                )
 
1860
                        );
 
1861
 
 
1862
                        statement->error = oci_error(statement->pError, "OCIAttrGet OCI_DTYPE_PARAM/OCI_ATTR_PRECISION", error);
 
1863
                        if (statement->error) {
 
1864
                                oci_handle_error(statement->conn, statement->error);
 
1865
                                return 0; /* XXX we lose memory!!! */
 
1866
                        }
 
1867
                                        
 
1868
                        CALL_OCI_RETURN(error,
 
1869
                                OCIAttrGet(
 
1870
                                        (dvoid *)param, 
 
1871
                                        OCI_DTYPE_PARAM, 
 
1872
                                        (dvoid **)&colname,                                     /* XXX this string is NOT zero terminated!!!! */ 
 
1873
                                        (ub4 *)&outcol->name_len, 
 
1874
                                        (ub4)OCI_ATTR_NAME, 
 
1875
                                        statement->pError
 
1876
                                )
 
1877
                        );
 
1878
 
 
1879
                        statement->error = oci_error(statement->pError, "OCIAttrGet OCI_DTYPE_PARAM/OCI_ATTR_NAME", error);
 
1880
                        if (statement->error) {
 
1881
                                oci_handle_error(statement->conn, statement->error);
 
1882
                                return 0; /* XXX we loose memory!!! */
 
1883
                        }
 
1884
 
 
1885
                        outcol->name =  estrndup((char*) colname,outcol->name_len);
 
1886
 
 
1887
                        /* find a user-setted define */
 
1888
                        if (statement->defines) {
 
1889
                                zend_hash_find(statement->defines,outcol->name,outcol->name_len,(void **) &outcol->define);
 
1890
                        }
 
1891
 
 
1892
                        buf = 0;
 
1893
                        switch (outcol->data_type) {
 
1894
                                case SQLT_RSET:
 
1895
                                        outcol->pstmt = oci_parse(statement->conn,0,0);
 
1896
                                        outcol->stmtid = outcol->pstmt->id;
 
1897
 
 
1898
                                        define_type = SQLT_RSET;
 
1899
                                        outcol->is_cursor = 1;
 
1900
                                        outcol->storage_size4 = -1;
 
1901
                                        outcol->retlen = -1;
 
1902
                                        dynamic = OCI_DEFAULT;
 
1903
                                        buf = &(outcol->pstmt->pStmt);
 
1904
                                        break;
 
1905
 
 
1906
                                case SQLT_RDD:   /* ROWID */
 
1907
                                case SQLT_BLOB:  /* binary LOB */
 
1908
                                case SQLT_CLOB:  /* character LOB */
 
1909
                                case SQLT_BFILE: /* binary file LOB */
 
1910
                                        define_type = outcol->data_type;
 
1911
                                        outcol->is_descr = 1;
 
1912
                                        outcol->storage_size4 = -1;
 
1913
                                        dynamic = OCI_DEFAULT;
 
1914
 
 
1915
                                        if (outcol->data_type == SQLT_BFILE) {
 
1916
                                                dtype = OCI_DTYPE_FILE;
 
1917
                                        } else if (outcol->data_type == SQLT_RDD ) {
 
1918
                                                dtype = OCI_DTYPE_ROWID;
 
1919
                                        } else {
 
1920
                                                dtype = OCI_DTYPE_LOB;
 
1921
                                        }
 
1922
                                        
 
1923
                                        descr = oci_new_desc(dtype,statement->conn);
 
1924
                                        if (!descr) {
 
1925
                                                /* need better error checking XXX */
 
1926
                                        }
 
1927
                                        outcol->descid = descr->id;
 
1928
                                        buf =  &(descr->ocidescr);
 
1929
                                        break;
 
1930
 
 
1931
                                case SQLT_LNG:
 
1932
                                case SQLT_LBI:
 
1933
                                        if (outcol->data_type == SQLT_LBI) {
 
1934
                                                define_type = SQLT_BIN;
 
1935
                                        } else {
 
1936
                                                define_type = SQLT_CHR;
 
1937
                                        }
 
1938
                                        outcol->storage_size4 = OCI_MAX_DATA_SIZE;
 
1939
                                        outcol->piecewise = 1;
 
1940
                                        dynamic = OCI_DYNAMIC_FETCH;
 
1941
                                        break;
 
1942
 
 
1943
                                case SQLT_BIN:
 
1944
                                default:
 
1945
                                        define_type = SQLT_CHR;
 
1946
                                        if ((outcol->data_type == SQLT_DAT) || (outcol->data_type == SQLT_NUM)
 
1947
#ifdef SQLT_TIMESTAMP
 
1948
                                                || (outcol->data_type == SQLT_TIMESTAMP)
 
1949
#endif
 
1950
#ifdef SQLT_TIMESTAMP_TZ
 
1951
                                                || (outcol->data_type == SQLT_TIMESTAMP_TZ)
 
1952
#endif
 
1953
                                                ) {
 
1954
                                                outcol->storage_size4 = 512; /* XXX this should fit "most" NLS date-formats and Numbers */
 
1955
                                        } else {
 
1956
                                                outcol->storage_size4++; /* add one for string terminator */
 
1957
                                        }
 
1958
                                        if (outcol->data_type == SQLT_BIN) {
 
1959
                                                outcol->storage_size4 *= 3;
 
1960
                                        }
 
1961
                                        dynamic = OCI_DEFAULT;
 
1962
                                        buf = outcol->data = (text *) emalloc(outcol->storage_size4);
 
1963
                                        break;
 
1964
                        }
 
1965
 
 
1966
                        if (dynamic == OCI_DYNAMIC_FETCH) {
 
1967
                                CALL_OCI_RETURN(error,
 
1968
                                        OCIDefineByPos(
 
1969
                                                statement->pStmt,                           /* IN/OUT handle to the requested SQL query */
 
1970
                                                (OCIDefine **)&outcol->pDefine,             /* IN/OUT pointer to a pointer to a define handle */
 
1971
                                                statement->pError,                          /* IN/OUT An error handle  */
 
1972
                                                counter,                                    /* IN     position in the select list */
 
1973
                                                (dvoid *)NULL,                              /* IN/OUT pointer to a buffer */
 
1974
                                                outcol->storage_size4,                      /* IN     The size of each valuep buffer in bytes */
 
1975
                                                define_type,                                /* IN     The data type */
 
1976
                                                (dvoid *)&outcol->indicator,                /* IN     pointer to an indicator variable or arr */
 
1977
                                                (ub2 *)NULL,                                /* IN/OUT Pointer to array of length of data fetched */
 
1978
                                                (ub2 *)NULL,                                /* OUT    Pointer to array of column-level return codes */
 
1979
                                                OCI_DYNAMIC_FETCH                               /* IN     mode (OCI_DEFAULT, OCI_DYNAMIC_FETCH) */
 
1980
                                        )
 
1981
                                );
 
1982
 
 
1983
                                statement->error = oci_error(statement->pError, "OCIDefineByPos", error);
 
1984
                        } else {
 
1985
                                CALL_OCI_RETURN(error,
 
1986
                                        OCIDefineByPos(
 
1987
                                                statement->pStmt,                           /* IN/OUT handle to the requested SQL query */ 
 
1988
                                                (OCIDefine **)&outcol->pDefine,             /* IN/OUT pointer to a pointer to a define handle */
 
1989
                                                statement->pError,                          /* IN/OUT An error handle  */
 
1990
                                                counter,                                    /* IN     position in the select list */
 
1991
                                                (dvoid *)buf,                               /* IN/OUT pointer to a buffer */
 
1992
                                                outcol->storage_size4,                      /* IN     The size of each valuep buffer in bytes */
 
1993
                                                define_type,                                /* IN     The data type */
 
1994
                                                (dvoid *)&outcol->indicator,                /* IN     pointer to an indicator variable or arr */
 
1995
                                                (ub2 *)&outcol->retlen,                     /* IN/OUT Pointer to array of length of data fetched */
 
1996
                                                (ub2 *)&outcol->retcode,                    /* OUT    Pointer to array of column-level return codes */
 
1997
                                                OCI_DEFAULT                                     /* IN     mode (OCI_DEFAULT, OCI_DYNAMIC_FETCH) */
 
1998
                                        )
 
1999
                                );
 
2000
 
 
2001
                                statement->error = oci_error(statement->pError, "OCIDefineByPos", error);
 
2002
                        }
 
2003
                        if (statement->error) {
 
2004
                                oci_handle_error(statement->conn, statement->error);
 
2005
                                return 0; /* XXX we loose memory!!! */
 
2006
                        }
 
2007
                }
 
2008
        }
 
2009
 
 
2010
        return 1;
 
2011
}
 
2012
/* }}} */
 
2013
 
 
2014
/* {{{ oci_fetch()
 
2015
*/
 
2016
static int _oci_column_pre_fetch(void *data TSRMLS_DC)
 
2017
{
 
2018
        oci_out_column *col = (oci_out_column *) data;
 
2019
        
 
2020
        if (col->piecewise) {
 
2021
                col->retlen4 = 0; 
 
2022
        }
 
2023
        
 
2024
        return 0;
 
2025
}
 
2026
 
 
2027
static int oci_fetch(oci_statement *statement, ub4 nrows, char *func TSRMLS_DC)
 
2028
{
 
2029
        int i;
 
2030
        oci_out_column *column;
 
2031
 
 
2032
        if (statement->columns) {
 
2033
                zend_hash_apply(statement->columns, (apply_func_t) _oci_column_pre_fetch TSRMLS_CC);
 
2034
        }
 
2035
 
 
2036
 
 
2037
        CALL_OCI_RETURN(statement->error,
 
2038
                OCIStmtFetch(
 
2039
                        statement->pStmt, 
 
2040
                        statement->pError, 
 
2041
                        nrows, 
 
2042
                        OCI_FETCH_NEXT, 
 
2043
                        OCI_DEFAULT
 
2044
                )
 
2045
        );
 
2046
 
 
2047
        if ((statement->error == OCI_NO_DATA) || (nrows == 0)) {
 
2048
                if (statement->last_query == 0) {
 
2049
                        /* reset define-list for refcursors */
 
2050
                        if (statement->columns) {
 
2051
                                zend_hash_destroy(statement->columns);
 
2052
                                efree(statement->columns);
 
2053
                                statement->columns = 0;
 
2054
                                statement->ncolumns = 0;
 
2055
                        }
 
2056
                        statement->executed = 0;
 
2057
                }
 
2058
 
 
2059
                statement->error = 0; /* OCI_NO_DATA is NO error for us!!! */
 
2060
                statement->has_data = 0;
 
2061
 
 
2062
                return 0;
 
2063
        }
 
2064
 
 
2065
        while (statement->error == OCI_NEED_DATA) {
 
2066
                for (i = 0; i < statement->ncolumns; i++) {
 
2067
                        column = oci_get_col(statement, i + 1, 0);
 
2068
                        if (column->piecewise)  {
 
2069
                                if (!column->data) {
 
2070
                                        column->data = (text *) emalloc(OCI_PIECE_SIZE);
 
2071
                                } else {
 
2072
                                        column->data = erealloc(column->data,column->retlen4 + OCI_PIECE_SIZE);
 
2073
                                }
 
2074
 
 
2075
                                column->cb_retlen = OCI_PIECE_SIZE;
 
2076
 
 
2077
                                CALL_OCI(
 
2078
                                        OCIStmtSetPieceInfo(
 
2079
                                                (void *) column->pDefine, 
 
2080
                                                OCI_HTYPE_DEFINE, 
 
2081
                                                statement->pError, 
 
2082
                                                ((char*)column->data) + column->retlen4, 
 
2083
                                                &(column->cb_retlen), 
 
2084
                                                OCI_NEXT_PIECE, 
 
2085
                                                &column->indicator, 
 
2086
                                                &column->retcode
 
2087
                                        )
 
2088
                                );
 
2089
                        }
 
2090
                }
 
2091
 
 
2092
                CALL_OCI_RETURN(statement->error,
 
2093
                        OCIStmtFetch(
 
2094
                                statement->pStmt, 
 
2095
                                statement->pError, 
 
2096
                                nrows, 
 
2097
                                OCI_FETCH_NEXT, 
 
2098
                                OCI_DEFAULT
 
2099
                        )
 
2100
                );
 
2101
 
 
2102
                for (i = 0; i < statement->ncolumns; i++) {
 
2103
                        column = oci_get_col(statement, i + 1, 0);
 
2104
                        if (column->piecewise)  {
 
2105
                                column->retlen4 += column->cb_retlen;
 
2106
                        }
 
2107
                }
 
2108
        }
 
2109
 
 
2110
        if (statement->error == OCI_SUCCESS_WITH_INFO || statement->error == OCI_SUCCESS) {
 
2111
                statement->has_data = 1;
 
2112
 
 
2113
                /* do the stuff needed for OCIDefineByName */
 
2114
                for (i = 0; i < statement->ncolumns; i++) {
 
2115
                        column = oci_get_col(statement, i + 1, 0);
 
2116
                        if (column == NULL) {
 
2117
                                continue;
 
2118
                        }
 
2119
                        
 
2120
                        if (!column->define) {
 
2121
                                continue;
 
2122
                        }
 
2123
                        
 
2124
                        zval_dtor(column->define->zval);
 
2125
                        _oci_make_zval(column->define->zval,statement,column,"OCIFetch",0 TSRMLS_CC);
 
2126
                }
 
2127
 
 
2128
                return 1;
 
2129
        }
 
2130
 
 
2131
        oci_error(statement->pError, func, statement->error);
 
2132
        oci_handle_error(statement->conn, statement->error);
 
2133
 
 
2134
        statement->has_data = 0;
 
2135
 
 
2136
        return 0;
 
2137
}
 
2138
/* }}} */
 
2139
 
 
2140
/* {{{ oci_lobgetlen()
 
2141
*/
 
2142
static int oci_lobgetlen(oci_connection *connection, oci_descriptor *mydescr, ub4 *loblen)
 
2143
{
 
2144
        TSRMLS_FETCH();
 
2145
 
 
2146
        *loblen = 0;
 
2147
        
 
2148
        /* do we need to ask oracle about LOB's length, if we do already know it? I think no. */
 
2149
        if (mydescr->lob_size >= 0) {
 
2150
                *loblen = mydescr->lob_size;
 
2151
        } else {
 
2152
                if (mydescr->type == OCI_DTYPE_FILE) {
 
2153
                        CALL_OCI_RETURN(connection->error,
 
2154
                                OCILobFileOpen(
 
2155
                                        connection->pServiceContext, 
 
2156
                                        connection->pError, 
 
2157
                                        mydescr->ocidescr,
 
2158
                                        OCI_FILE_READONLY
 
2159
                                )
 
2160
                        );
 
2161
                        if (connection->error) {
 
2162
                                oci_error(connection->pError, "OCILobFileOpen",connection->error);
 
2163
                                oci_handle_error(connection, connection->error);
 
2164
                                return -1;
 
2165
                        }
 
2166
                }
 
2167
                
 
2168
                CALL_OCI_RETURN(connection->error, 
 
2169
                        OCILobGetLength(
 
2170
                                        connection->pServiceContext, 
 
2171
                                        connection->pError, 
 
2172
                                        mydescr->ocidescr, 
 
2173
                                        loblen
 
2174
                        )
 
2175
                );
 
2176
 
 
2177
                if (connection->error) {
 
2178
                        oci_error(connection->pError, "OCILobGetLength",connection->error);
 
2179
                        oci_handle_error(connection, connection->error);
 
2180
                        return -1;
 
2181
                }
 
2182
                mydescr->lob_size = *loblen;
 
2183
 
 
2184
                if (mydescr->type == OCI_DTYPE_FILE) {
 
2185
                        CALL_OCI_RETURN(connection->error,
 
2186
                                OCILobFileClose(
 
2187
                                        connection->pServiceContext, 
 
2188
                                        connection->pError, 
 
2189
                                        mydescr->ocidescr
 
2190
                                )
 
2191
                        );
 
2192
 
 
2193
                        if (connection->error) {
 
2194
                                oci_error(connection->pError, "OCILobFileClose", connection->error);
 
2195
                                oci_handle_error(connection, connection->error);
 
2196
                                return -1;
 
2197
                        }
 
2198
                }
 
2199
        }
 
2200
        
 
2201
        oci_debug("oci_lobgetlen: len=%d",*loblen);
 
2202
 
 
2203
        return 0;
 
2204
}
 
2205
/* }}} */
 
2206
 
 
2207
/* {{{ oci_loadlob()
 
2208
*/
 
2209
#define LOBREADSIZE 1048576l /* 1MB */
 
2210
static int oci_loadlob(oci_connection *connection, oci_descriptor *mydescr, char **buffer, ub4 *loblen)
 
2211
{
 
2212
        ub4 siz = 0;
 
2213
        ub4 readlen = 0;
 
2214
        char *buf;
 
2215
        TSRMLS_FETCH();
 
2216
 
 
2217
        *loblen = 0;
 
2218
        
 
2219
        if (mydescr->type == OCI_DTYPE_FILE) {
 
2220
                CALL_OCI_RETURN(connection->error,
 
2221
                        OCILobFileOpen(
 
2222
                                connection->pServiceContext, 
 
2223
                                connection->pError, 
 
2224
                                mydescr->ocidescr,
 
2225
                                OCI_FILE_READONLY
 
2226
                        )
 
2227
                );
 
2228
 
 
2229
                if (connection->error) {
 
2230
                        oci_error(connection->pError, "OCILobFileOpen",connection->error);
 
2231
                        oci_handle_error(connection, connection->error);
 
2232
                        return -1;
 
2233
                }
 
2234
        }
 
2235
        
 
2236
        CALL_OCI_RETURN(connection->error,
 
2237
                OCILobGetLength(
 
2238
                        connection->pServiceContext, 
 
2239
                        connection->pError, 
 
2240
                        mydescr->ocidescr, 
 
2241
                        &readlen
 
2242
                )
 
2243
        );
 
2244
 
 
2245
        if (connection->error) {
 
2246
                oci_error(connection->pError, "OCILobGetLength",connection->error);
 
2247
                oci_handle_error(connection, connection->error);
 
2248
                return -1;
 
2249
        }
 
2250
 
 
2251
        buf = emalloc(readlen + 1);
 
2252
 
 
2253
        while (readlen > 0) { /* thies loop should not be entered on readlen == 0 */
 
2254
                CALL_OCI_RETURN(connection->error,
 
2255
                        OCILobRead(
 
2256
                                connection->pServiceContext, 
 
2257
                                connection->pError, 
 
2258
                                mydescr->ocidescr, 
 
2259
                                &readlen,                                                               /* IN/OUT bytes toread/read */ 
 
2260
                                siz + 1,                                                                /* offset (starts with 1) */ 
 
2261
                                (dvoid *) ((char *) buf + siz), 
 
2262
                                readlen,                                                                /* size of buffer */ 
 
2263
                                (dvoid *)0, 
 
2264
                                (OCICallbackLobRead) 0,                                 /* callback... */ 
 
2265
                                (ub2) connection->session->charsetId,   /* The character set ID of the buffer data. */ 
 
2266
                                (ub1) SQLCS_IMPLICIT                                    /* The character set form of the buffer data. */
 
2267
                        )
 
2268
                );
 
2269
 
 
2270
                siz += readlen;
 
2271
                readlen = LOBREADSIZE;
 
2272
 
 
2273
                if (connection->error == OCI_NEED_DATA) {
 
2274
                        buf = erealloc(buf,siz + LOBREADSIZE + 1);      
 
2275
                        continue;
 
2276
                } else {
 
2277
                        break;
 
2278
                }
 
2279
        }
 
2280
 
 
2281
        if (connection->error) {
 
2282
                oci_error(connection->pError, "OCILobRead", connection->error);
 
2283
                oci_handle_error(connection, connection->error);
 
2284
                efree(buf);
 
2285
                return -1;
 
2286
        }
 
2287
 
 
2288
        if (mydescr->type == OCI_DTYPE_FILE) {
 
2289
                CALL_OCI_RETURN(connection->error,
 
2290
                        OCILobFileClose(
 
2291
                                connection->pServiceContext, 
 
2292
                                connection->pError, 
 
2293
                                mydescr->ocidescr
 
2294
                        )
 
2295
                );
 
2296
 
 
2297
                if (connection->error) {
 
2298
                        oci_error(connection->pError, "OCILobFileClose", connection->error);
 
2299
                        oci_handle_error(connection, connection->error);
 
2300
                        efree(buf);
 
2301
                        return -1;
 
2302
                }
 
2303
        }
 
2304
 
 
2305
        buf = erealloc(buf,siz+1);
 
2306
        buf[ siz ] = 0;
 
2307
 
 
2308
        *buffer = buf;
 
2309
        *loblen = siz;
 
2310
 
 
2311
        oci_debug("oci_loadlob: size=%d",siz);
 
2312
 
 
2313
        return 0;
 
2314
}
 
2315
/* }}} */
 
2316
 
 
2317
/* {{{ oci_readlob()
 
2318
*/
 
2319
static int oci_readlob(oci_connection *connection, oci_descriptor *mydescr, char **buffer, ub4 *len)
 
2320
{
 
2321
        ub4 siz = 0;
 
2322
        ub4 readlen = 0;
 
2323
        ub4 loblen = 0;
 
2324
        ub4 bytes = 0;
 
2325
        char *buf;
 
2326
        TSRMLS_FETCH();
 
2327
 
 
2328
        /* we're not going to read LOB, if length is not known */
 
2329
        if (!len || (int)*len <= 0) {
 
2330
                return -1;
 
2331
        }
 
2332
 
 
2333
        if (mydescr->type == OCI_DTYPE_FILE) {
 
2334
                CALL_OCI_RETURN(connection->error,
 
2335
                        OCILobFileOpen(
 
2336
                                connection->pServiceContext, 
 
2337
                                connection->pError, 
 
2338
                                mydescr->ocidescr,
 
2339
                                OCI_FILE_READONLY
 
2340
                        )
 
2341
                );
 
2342
 
 
2343
                if (connection->error) {
 
2344
                        oci_error(connection->pError, "OCILobFileOpen",connection->error);
 
2345
                        oci_handle_error(connection, connection->error);
 
2346
                        return -1;
 
2347
                }
 
2348
        }
 
2349
        
 
2350
        if (oci_lobgetlen(connection, mydescr, &loblen) != 0) {
 
2351
                *len = 0;
 
2352
                return -1;
 
2353
        }
 
2354
        
 
2355
        /* check if we're in LOB's borders */
 
2356
        if ((mydescr->lob_current_position + *len) > loblen) {
 
2357
                *len = loblen - mydescr->lob_current_position;
 
2358
        }
 
2359
 
 
2360
        if ((int)*len > 0) {
 
2361
                buf = emalloc(*len + 1);
 
2362
 
 
2363
                /* set offset to current LOB's position */
 
2364
                siz = mydescr->lob_current_position;
 
2365
 
 
2366
                /* check if len is smaller, if not - using LOBREADSIZE' sized buffer */
 
2367
                if (*len > LOBREADSIZE) {
 
2368
                        readlen = LOBREADSIZE;
 
2369
                } else {
 
2370
                        readlen = *len;
 
2371
                }
 
2372
        } else {
 
2373
                *len = 0;
 
2374
                return -1;
 
2375
        }
 
2376
 
 
2377
        while (readlen > 0 && bytes < *len && siz < loblen) {  /* paranoia */
 
2378
                CALL_OCI_RETURN(connection->error,
 
2379
                        OCILobRead(
 
2380
                                connection->pServiceContext, 
 
2381
                                connection->pError, 
 
2382
                                mydescr->ocidescr, 
 
2383
                                &readlen,                                                               /* IN/OUT bytes toread/read */ 
 
2384
                                siz + 1,                                                                /* offset (starts with 1) */ 
 
2385
                                (dvoid *) ((char *) buf + bytes),       
 
2386
                                readlen,                                                                /* size of buffer */ 
 
2387
                                (dvoid *)0, 
 
2388
                                (OCICallbackLobRead) 0,                                 /* callback... */ 
 
2389
                                (ub2) connection->session->charsetId,   /* The character set ID of the buffer data. */ 
 
2390
                                (ub1) SQLCS_IMPLICIT                                    /* The character set form of the buffer data. */
 
2391
                        )
 
2392
                );
 
2393
 
 
2394
                siz += readlen;
 
2395
                bytes += readlen;
 
2396
 
 
2397
                if ((*len - bytes) > LOBREADSIZE) {
 
2398
                        readlen = LOBREADSIZE;
 
2399
                } else {
 
2400
                        readlen = *len - bytes;
 
2401
                }
 
2402
 
 
2403
                if (connection->error == OCI_NEED_DATA) {
 
2404
                        buf = erealloc(buf,bytes + LOBREADSIZE + 1);    
 
2405
                        continue;
 
2406
                } else {
 
2407
                        break;
 
2408
                }
 
2409
        }
 
2410
 
 
2411
        /* moving current position */
 
2412
        mydescr->lob_current_position = siz;
 
2413
 
 
2414
        if (connection->error) {
 
2415
                oci_error(connection->pError, "OCILobRead", connection->error);
 
2416
                oci_handle_error(connection, connection->error);
 
2417
                efree(buf);
 
2418
                return -1;
 
2419
        }
 
2420
 
 
2421
        if (mydescr->type == OCI_DTYPE_FILE) {
 
2422
                CALL_OCI_RETURN(connection->error,
 
2423
                        OCILobFileClose(
 
2424
                                connection->pServiceContext, 
 
2425
                                connection->pError, 
 
2426
                                mydescr->ocidescr
 
2427
                        )
 
2428
                );
 
2429
 
 
2430
                if (connection->error) {
 
2431
                        oci_error(connection->pError, "OCILobFileClose", connection->error);
 
2432
                        oci_handle_error(connection, connection->error);
 
2433
                        efree(buf);
 
2434
                        return -1;
 
2435
                }
 
2436
        }
 
2437
 
 
2438
        buf = erealloc(buf,bytes+1);
 
2439
        buf[ bytes ] = 0;
 
2440
 
 
2441
        *buffer = buf;
 
2442
        *len = bytes;
 
2443
 
 
2444
        oci_debug("oci_readlob: size=%d",bytes);
 
2445
 
 
2446
        return 0;
 
2447
}
 
2448
/* }}} */
 
2449
 
 
2450
/* {{{ oci_failover_callback()
 
2451
*/
 
2452
#if 0 /* not needed yet ! */
 
2453
static sb4 oci_failover_callback(dvoid *svchp, dvoid *envhp, dvoid *fo_ctx, ub4 fo_type, ub4 fo_event)
 
2454
{
 
2455
        /* 
 
2456
           this stuff is from an oci sample - it will get cleaned up as soon as i understand it!!! (thies@thieso.net 990420) 
 
2457
           right now i cant get oracle to even call it;-(((((((((((
 
2458
        */
 
2459
 
 
2460
        switch (fo_event) {
 
2461
                case OCI_FO_BEGIN: {
 
2462
                        printf(" Failing Over ... Please stand by \n");
 
2463
                        printf(" Failover type was found to be %s \n",
 
2464
                                        ((fo_type==OCI_FO_NONE) ? "NONE"
 
2465
                                        :(fo_type==OCI_FO_SESSION) ? "SESSION"
 
2466
                                        :(fo_type==OCI_FO_SELECT) ? "SELECT"
 
2467
                                        : "UNKNOWN!"));
 
2468
                        printf(" Failover Context is :%s\n",
 
2469
                                        (fo_ctx?(char *)fo_ctx:"NULL POINTER!"));
 
2470
                        break;
 
2471
                }
 
2472
 
 
2473
                case OCI_FO_ABORT: {
 
2474
                        printf(" Failover aborted. Failover will not take place.\n");
 
2475
                        break;
 
2476
                }
 
2477
 
 
2478
                case OCI_FO_END: {
 
2479
                        printf(" Failover ended ...resuming services\n");
 
2480
                        break;
 
2481
                }
 
2482
 
 
2483
                case OCI_FO_REAUTH: {
 
2484
                        printf(" Failed over user. Resuming services\n");
 
2485
                                
 
2486
                        /* Application can check the OCI_ATTR_SESSION attribute of
 
2487
                           the service handle to find out the user being
 
2488
                           re-authenticated.
 
2489
                                   
 
2490
                           After this, the application can replay any ALTER SESSION
 
2491
                           commands associated with this session.  These must have
 
2492
                           been saved by the application in the fo_ctx
 
2493
                        */
 
2494
                        break;
 
2495
                }
 
2496
 
 
2497
                case OCI_FO_ERROR: {
 
2498
                        printf(" Failover error gotten. Sleeping...\n");
 
2499
                        php_sleep(3);
 
2500
                        /* cannot find this blody define !!! return OCI_FO_RETRY; */
 
2501
                        break;
 
2502
                }
 
2503
                        
 
2504
                default: {
 
2505
                        printf("Bad Failover Event: %ld.\n",  fo_event);
 
2506
                        break;
 
2507
                }
 
2508
        }
 
2509
 
 
2510
        return 0;  
 
2511
}
 
2512
#endif
 
2513
/* }}} */
 
2514
 
 
2515
/* {{{ oci_bind_in_callback()
 
2516
*/
 
2517
static sb4 oci_bind_in_callback(
 
2518
                                        dvoid *ictxp,     /* context pointer */
 
2519
                                        OCIBind *bindp,   /* bind handle */
 
2520
                                        ub4 iter,         /* 0-based execute iteration value */
 
2521
                                        ub4 index,        /* index of current array for PL/SQL or row index for SQL */
 
2522
                                        dvoid **bufpp,    /* pointer to data */
 
2523
                                        ub4 *alenp,       /* size after value/piece has been read */
 
2524
                                        ub1 *piecep,      /* which piece */
 
2525
                                        dvoid **indpp)    /* indicator value */
 
2526
{
 
2527
        oci_bind *phpbind;
 
2528
        zval *val;
 
2529
        TSRMLS_FETCH();
 
2530
 
 
2531
        if (!(phpbind=(oci_bind *)ictxp) || !(val = phpbind->zval)) {
 
2532
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "!phpbind || !phpbind->val");
 
2533
                return OCI_ERROR;
 
2534
        }
 
2535
 
 
2536
        if (ZVAL_IS_NULL(val)) {
 
2537
                /* we're going to insert a NULL column */
 
2538
                phpbind->indicator = -1;
 
2539
                *bufpp = 0;
 
2540
                *alenp = -1;
 
2541
                *indpp = (dvoid *)&phpbind->indicator;
 
2542
        } else  if ((phpbind->descr == 0) && (phpbind->pStmt == 0)) {
 
2543
                /* "normal string bind */
 
2544
                convert_to_string(val); 
 
2545
 
 
2546
                *bufpp = Z_STRVAL_P(val);
 
2547
                *alenp = Z_STRLEN_P(val);
 
2548
                *indpp = (dvoid *)&phpbind->indicator;
 
2549
        } else if (phpbind->pStmt != 0) {
 
2550
                /* RSET */
 
2551
                *bufpp = phpbind->pStmt;
 
2552
                *alenp = -1;            /* seems to be allright */
 
2553
                *indpp = (dvoid *)&phpbind->indicator;
 
2554
        } else { 
 
2555
                /* descriptor bind */
 
2556
                *bufpp = phpbind->descr;
 
2557
                *alenp = -1;            /* seems to be allright */
 
2558
                *indpp = (dvoid *)&phpbind->indicator;
 
2559
        }
 
2560
 
 
2561
        *piecep = OCI_ONE_PIECE; /* pass all data in one go */
 
2562
 
 
2563
        return OCI_CONTINUE;
 
2564
}
 
2565
/* }}} */
 
2566
 
 
2567
/* {{{ oci_bind_out_callback() */
 
2568
static sb4 oci_bind_out_callback(
 
2569
                                        dvoid *octxp,      /* context pointer */
 
2570
                                        OCIBind *bindp,    /* bind handle */
 
2571
                                        ub4 iter,          /* 0-based execute iteration value */
 
2572
                                        ub4 index,         /* index of current array for PL/SQL or row index for SQL */
 
2573
                                        dvoid **bufpp,     /* pointer to data */
 
2574
                                        ub4 **alenpp,      /* size after value/piece has been read */
 
2575
                                        ub1 *piecep,       /* which piece */
 
2576
                                        dvoid **indpp,     /* indicator value */
 
2577
                                        ub2 **rcodepp)     /* return code */
 
2578
{
 
2579
        oci_bind *phpbind;
 
2580
        zval *val;
 
2581
        sb4 retval = OCI_ERROR;
 
2582
        TSRMLS_FETCH();
 
2583
 
 
2584
        if (!(phpbind=(oci_bind *)octxp) || !(val = phpbind->zval)) {
 
2585
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "!phpbind || !phpbind->val");
 
2586
                return retval;
 
2587
        }
 
2588
 
 
2589
        if ((Z_TYPE_P(val) == IS_OBJECT) || (Z_TYPE_P(val) == IS_RESOURCE)) {
 
2590
                retval = OCI_CONTINUE;
 
2591
        } else {
 
2592
                convert_to_string(val);
 
2593
                zval_dtor(val);
 
2594
                
 
2595
                Z_STRLEN_P(val) = OCI_PIECE_SIZE; /* 64K-1 is max XXX */
 
2596
                Z_STRVAL_P(val) = emalloc(Z_STRLEN_P(phpbind->zval));
 
2597
                
 
2598
                /* XXX we assume that zend-zval len has 4 bytes */
 
2599
                *alenpp = (ub4*) &Z_STRLEN_P(phpbind->zval); 
 
2600
                *bufpp = Z_STRVAL_P(phpbind->zval);
 
2601
                *piecep = OCI_ONE_PIECE;
 
2602
                *rcodepp = &phpbind->retcode;
 
2603
                *indpp = &phpbind->indicator;
 
2604
                retval = OCI_CONTINUE;
 
2605
        }
 
2606
 
 
2607
        return retval;
 
2608
}
 
2609
/* }}} */
 
2610
 
 
2611
/* {{{ _oci_open_session()
 
2612
*/
 
2613
#include "ext/standard/php_smart_str.h"
 
2614
static oci_session *_oci_open_session(oci_server* server,char *username,char *password,int persistent,int exclusive,char *charset)
 
2615
{
 
2616
        zend_llist *session_list;
 
2617
        oci_session *session = NULL;
 
2618
        OCISvcCtx *svchp = NULL;
 
2619
        smart_str hashed_details = {0};
 
2620
#ifdef HAVE_OCI_9_2
 
2621
        ub2 charsetid = 0;
 
2622
#endif
 
2623
        TSRMLS_FETCH();
 
2624
 
 
2625
        /* 
 
2626
           check if we already have this user authenticated
 
2627
 
 
2628
           we will reuse authenticated users within a request no matter if the user requested a persistent 
 
2629
           connections or not!
 
2630
           
 
2631
           but only as persistent requested connections will be kept between requests!
 
2632
        */
 
2633
 
 
2634
#if defined(HAVE_OCI_9_2)
 
2635
        if (*charset) {
 
2636
                smart_str_appends_ex(&hashed_details, charset, 1);
 
2637
        } else {
 
2638
                size_t rsize;
 
2639
 
 
2640
                /* Safe, charsetid is initialized to 0 */
 
2641
                CALL_OCI(
 
2642
                        OCINlsEnvironmentVariableGet(
 
2643
                                &charsetid, 
 
2644
                                2, 
 
2645
                                OCI_NLS_CHARSET_ID, 
 
2646
                                0,
 
2647
                                &rsize
 
2648
                        )
 
2649
                );
 
2650
 
 
2651
                smart_str_append_unsigned_ex(&hashed_details, charsetid, 1);
 
2652
 
 
2653
                charsetid = 0;
 
2654
        }
 
2655
#else
 
2656
        {
 
2657
                char *nls_lang = getenv("NLS_LANG");
 
2658
 
 
2659
                /* extract charset from NLS_LANG=LANUAGE_TERRITORY.CHARSET */
 
2660
                if (nls_lang) {
 
2661
                        char *p = strchr(nls_lang, '.');
 
2662
 
 
2663
                        if (p) {
 
2664
                                smart_str_appends_ex(&hashed_details, p + 1, 1);
 
2665
                        }
 
2666
                }
 
2667
        }
 
2668
#endif
 
2669
 
 
2670
        smart_str_appends_ex(&hashed_details, SAFE_STRING(username), 1);
 
2671
        smart_str_appends_ex(&hashed_details, SAFE_STRING(password), 1);
 
2672
        smart_str_appends_ex(&hashed_details, SAFE_STRING(server->dbname), 1);
 
2673
        smart_str_0(&hashed_details);
 
2674
 
 
2675
        if (!exclusive) {
 
2676
                mutex_lock(mx_lock);
 
2677
                if (zend_ts_hash_find(persistent_sessions, hashed_details.c, hashed_details.len+1, (void **) &session_list) != SUCCESS) {
 
2678
                        zend_llist tmp;
 
2679
                        /* first session, set up a session list */
 
2680
                        zend_llist_init(&tmp, sizeof(oci_session), (llist_dtor_func_t) _session_pcleanup, 1);
 
2681
                        zend_ts_hash_update(persistent_sessions, hashed_details.c, hashed_details.len+1, &tmp, sizeof(zend_llist), (void **) &session_list);
 
2682
                } else {
 
2683
 
 
2684
                        /* session list found, search for an idle session or an already opened session by the current thread */
 
2685
                        session = zend_llist_get_first(session_list);
 
2686
                        while ((session != NULL) && session->thread && (session->thread != thread_id())) {
 
2687
                                session = zend_llist_get_next(session_list);
 
2688
                        }
 
2689
 
 
2690
                        if (session != NULL) {
 
2691
                                /* mark session as busy */
 
2692
                                session->thread = thread_id();
 
2693
                        }
 
2694
 
 
2695
                }
 
2696
 
 
2697
                if (session) {
 
2698
                        if (session->is_open) {
 
2699
                                if (persistent) {
 
2700
                                        session->persistent = 1;
 
2701
                                }
 
2702
                                smart_str_free_ex(&hashed_details, 1);
 
2703
                                mutex_unlock(mx_lock);
 
2704
                                return session;
 
2705
                        } else {
 
2706
                                _oci_close_session(session);
 
2707
                                /* breakthru to open */
 
2708
                        }
 
2709
                }
 
2710
                mutex_unlock(mx_lock);
 
2711
        }
 
2712
 
 
2713
        session = ecalloc(1,sizeof(oci_session));
 
2714
 
 
2715
        if (!session) {
 
2716
                goto CLEANUP;
 
2717
        }
 
2718
 
 
2719
        session->persistent = persistent;
 
2720
        session->server = server;
 
2721
        session->exclusive = exclusive;
 
2722
        session->sessions_list = session_list;
 
2723
        session->thread = thread_id();
 
2724
 
 
2725
#ifdef HAVE_OCI_9_2
 
2726
 
 
2727
        /* following chunk is Oracle 9i+ ONLY */
 
2728
        if (*charset) {
 
2729
                /*
 
2730
                   get ub2 charset id based on charset
 
2731
                   this is pretty secure, since if we don't have a valid character set name,
 
2732
                   0 comes back and we can still use the 0 in all further statements -> OCI uses NLS_LANG
 
2733
                   setting in that case
 
2734
                */
 
2735
                CALL_OCI_RETURN(charsetid,
 
2736
                        OCINlsCharSetNameToId(
 
2737
                                OCI(pEnv),
 
2738
                                charset
 
2739
                        )
 
2740
                );
 
2741
                
 
2742
                oci_debug("oci_do_connect: using charset id=%d",charsetid);
 
2743
        }
 
2744
 
 
2745
        session->charsetId = charsetid;
 
2746
        
 
2747
        /* create an environment using the character set id, Oracle 9i+ ONLY */
 
2748
        CALL_OCI(
 
2749
                OCIEnvNlsCreate(
 
2750
                        &session->pEnv,
 
2751
                        PHP_OCI_INIT_MODE, 
 
2752
                        0, 
 
2753
                        NULL,
 
2754
                        NULL,
 
2755
                        NULL,
 
2756
                        0,
 
2757
                        NULL,
 
2758
                        charsetid,
 
2759
                        charsetid
 
2760
                )
 
2761
        );
 
2762
 
 
2763
#else
 
2764
 
 
2765
        /* fallback solution (simply use global env and charset, same behaviour as always been) */
 
2766
        session->pEnv = OCI(pEnv);
 
2767
        session->charsetId = 0;
 
2768
 
 
2769
#endif  /* HAVE_OCI_9_2 */
 
2770
 
 
2771
        /* allocate temporary Service Context */
 
2772
        CALL_OCI_RETURN(OCI(error),
 
2773
                OCIHandleAlloc(
 
2774
                        session->pEnv, 
 
2775
                        (dvoid **)&svchp, 
 
2776
                        OCI_HTYPE_SVCCTX, 
 
2777
                        0, 
 
2778
                        NULL
 
2779
                )
 
2780
        );
 
2781
 
 
2782
        if (OCI(error) != OCI_SUCCESS) {
 
2783
                oci_error(OCI(pError), "_oci_open_session: OCIHandleAlloc OCI_HTYPE_SVCCTX", OCI(error));
 
2784
                goto CLEANUP;
 
2785
        }
 
2786
 
 
2787
        /* allocate private session-handle */
 
2788
        CALL_OCI_RETURN(OCI(error),
 
2789
                OCIHandleAlloc(
 
2790
                        session->pEnv, 
 
2791
                        (dvoid **)&session->pSession, 
 
2792
                        OCI_HTYPE_SESSION, 
 
2793
                        0, 
 
2794
                        NULL
 
2795
                )
 
2796
        );
 
2797
 
 
2798
        if (OCI(error) != OCI_SUCCESS) {
 
2799
                oci_error(OCI(pError), "_oci_open_session: OCIHandleAlloc OCI_HTYPE_SESSION", OCI(error));
 
2800
                goto CLEANUP;
 
2801
        }
 
2802
 
 
2803
        /* Set the server handle in service handle */ 
 
2804
        CALL_OCI_RETURN(OCI(error),
 
2805
                OCIAttrSet(
 
2806
                        svchp, 
 
2807
                        OCI_HTYPE_SVCCTX, 
 
2808
                        server->pServer, 
 
2809
                        0,
 
2810
                        OCI_ATTR_SERVER,
 
2811
                        OCI(pError)
 
2812
                )
 
2813
        );
 
2814
 
 
2815
        if (OCI(error) != OCI_SUCCESS) {
 
2816
                oci_error(OCI(pError), "_oci_open_session: OCIAttrSet OCI_ATTR_SERVER", OCI(error));
 
2817
                goto CLEANUP;
 
2818
        }
 
2819
 
 
2820
        /* set the username in user handle */
 
2821
        CALL_OCI_RETURN(OCI(error),
 
2822
                OCIAttrSet(
 
2823
                        (dvoid *) session->pSession, 
 
2824
                        (ub4) OCI_HTYPE_SESSION, 
 
2825
                        (dvoid *) username, 
 
2826
                        (ub4) strlen(username), 
 
2827
                        (ub4) OCI_ATTR_USERNAME, 
 
2828
                        OCI(pError)
 
2829
                )
 
2830
        );
 
2831
 
 
2832
        if (OCI(error) != OCI_SUCCESS) {
 
2833
                oci_error(OCI(pError), "OCIAttrSet OCI_ATTR_USERNAME", OCI(error));
 
2834
                goto CLEANUP;
 
2835
        }
 
2836
 
 
2837
        /* set the password in user handle */
 
2838
        CALL_OCI_RETURN(OCI(error),
 
2839
                OCIAttrSet(
 
2840
                        (dvoid *) session->pSession, 
 
2841
                        (ub4) OCI_HTYPE_SESSION, 
 
2842
                        (dvoid *) password, 
 
2843
                        (ub4) strlen(password), 
 
2844
                        (ub4) OCI_ATTR_PASSWORD, 
 
2845
                        OCI(pError)
 
2846
                )
 
2847
        );
 
2848
 
 
2849
        if (OCI(error) != OCI_SUCCESS) {
 
2850
                oci_error(OCI(pError), "OCIAttrSet OCI_ATTR_PASSWORD", OCI(error));
 
2851
                goto CLEANUP;
 
2852
        }
 
2853
 
 
2854
        CALL_OCI_RETURN(OCI(error),
 
2855
                OCISessionBegin(
 
2856
                        svchp, 
 
2857
                        OCI(pError), 
 
2858
                        session->pSession, 
 
2859
                        (ub4) OCI_CRED_RDBMS, 
 
2860
                        (ub4) OCI_DEFAULT
 
2861
                )
 
2862
        );
 
2863
 
 
2864
        if (OCI(error) != OCI_SUCCESS) {
 
2865
                oci_error(OCI(pError), "OCISessionBegin", OCI(error));
 
2866
                /* OCISessionBegin returns OCI_SUCCESS_WITH_INFO when
 
2867
                 * user's password has expired, but is still usable.
 
2868
                 * */
 
2869
                if (OCI(error) != OCI_SUCCESS_WITH_INFO) {
 
2870
                        goto CLEANUP;
 
2871
                }
 
2872
        }
 
2873
 
 
2874
        /* Free Temporary Service Context */
 
2875
        CALL_OCI(
 
2876
                OCIHandleFree(
 
2877
                        (dvoid *) svchp, 
 
2878
                        (ub4) OCI_HTYPE_SVCCTX
 
2879
                )
 
2880
        );
 
2881
 
 
2882
        session->num = zend_list_insert(session, le_session);
 
2883
        session->is_open = 1;
 
2884
 
 
2885
        mutex_lock(mx_lock);
 
2886
                num_links++;
 
2887
                if (!exclusive) {
 
2888
                        zend_llist_add_element(session_list, session);
 
2889
                        efree(session);
 
2890
                        session = (oci_session*) session_list->tail->data;
 
2891
                        num_persistent++;
 
2892
                }
 
2893
        mutex_unlock(mx_lock);
 
2894
 
 
2895
        oci_debug("_oci_open_session new sess=%d user=%s",session->num,username);
 
2896
 
 
2897
        return session;
 
2898
 
 
2899
CLEANUP:
 
2900
        oci_debug("_oci_open_session: FAILURE -> CLEANUP called");
 
2901
 
 
2902
        _oci_close_session(session);
 
2903
 
 
2904
        return 0;
 
2905
}
 
2906
/* }}} */
 
2907
 
 
2908
/* {{{ _oci_close_session()
 
2909
*/
 
2910
static int _session_compare(void *a, void *b)
 
2911
{
 
2912
        oci_session *sess1 = (oci_session*) a;
 
2913
        oci_session *sess2 = (oci_session*) b;
 
2914
        
 
2915
        return sess1->num == sess2->num;
 
2916
}
 
2917
 
 
2918
static void _oci_close_session(oci_session *session)
 
2919
{
 
2920
        OCISvcCtx *svchp;
 
2921
        TSRMLS_FETCH();
 
2922
 
 
2923
        if (!session) {
 
2924
                return;
 
2925
        }
 
2926
 
 
2927
        oci_debug("START _oci_close_session: logging-off sess=%d",session->num);
 
2928
 
 
2929
        if (session->is_open) {
 
2930
                /* Temporary Service Context */
 
2931
                CALL_OCI_RETURN(OCI(error),
 
2932
                        OCIHandleAlloc(
 
2933
                                session->pEnv, 
 
2934
                                (dvoid **) &svchp, 
 
2935
                                (ub4) OCI_HTYPE_SVCCTX, 
 
2936
                                (size_t) 0, 
 
2937
                                (dvoid **) 0
 
2938
                        )
 
2939
                );
 
2940
 
 
2941
                if (OCI(error) != OCI_SUCCESS) {
 
2942
                        oci_error(OCI(pError), "_oci_close_session OCIHandleAlloc OCI_HTYPE_SVCCTX", OCI(error));
 
2943
                }
 
2944
                
 
2945
                /* Set the server handle in service handle */ 
 
2946
                CALL_OCI_RETURN(OCI(error),
 
2947
                        OCIAttrSet(
 
2948
                                svchp, 
 
2949
                                OCI_HTYPE_SVCCTX, 
 
2950
                                session->server->pServer, 
 
2951
                                0, 
 
2952
                                OCI_ATTR_SERVER, 
 
2953
                                OCI(pError)
 
2954
                        )
 
2955
                );
 
2956
 
 
2957
                if (OCI(error) != OCI_SUCCESS) {
 
2958
                        oci_error(OCI(pError), "_oci_close_session: OCIAttrSet OCI_ATTR_SERVER", OCI(error));
 
2959
                }
 
2960
                
 
2961
                /* Set the Authentication handle in the service handle */
 
2962
                CALL_OCI_RETURN(OCI(error),
 
2963
                        OCIAttrSet(
 
2964
                                svchp,  
 
2965
                                OCI_HTYPE_SVCCTX, 
 
2966
                                session->pSession, 
 
2967
                                0, 
 
2968
                                OCI_ATTR_SESSION, 
 
2969
                                OCI(pError)
 
2970
                        )
 
2971
                );
 
2972
 
 
2973
                if (OCI(error) != OCI_SUCCESS) {
 
2974
                        oci_error(OCI(pError), "_oci_close_session: OCIAttrSet OCI_ATTR_SESSION", OCI(error));
 
2975
                }
 
2976
        
 
2977
                CALL_OCI_RETURN(OCI(error),
 
2978
                        OCISessionEnd(
 
2979
                                svchp, 
 
2980
                                OCI(pError), 
 
2981
                                session->pSession, 
 
2982
                                (ub4) 0
 
2983
                        )
 
2984
                );
 
2985
 
 
2986
                if (OCI(error) != OCI_SUCCESS) {
 
2987
                        oci_error(OCI(pError), "_oci_close_session: OCISessionEnd", OCI(error));
 
2988
                }
 
2989
 
 
2990
                CALL_OCI(
 
2991
                        OCIHandleFree(
 
2992
                                (dvoid *) svchp, 
 
2993
                                (ub4) OCI_HTYPE_SVCCTX
 
2994
                        )
 
2995
                );
 
2996
 
 
2997
        } else {
 
2998
                oci_debug("_oci_close_session: logging-off DEAD session");
 
2999
        }
 
3000
 
 
3001
        if (session->pSession) {
 
3002
                CALL_OCI(
 
3003
                        OCIHandleFree(
 
3004
                                (dvoid *) session->pSession, 
 
3005
                                (ub4) OCI_HTYPE_SESSION
 
3006
                        )
 
3007
                );
 
3008
        }
 
3009
 
 
3010
        mutex_lock(mx_lock);
 
3011
                num_links--;
 
3012
                if (!OCI(shutdown) && session->persistent) {
 
3013
                        zend_llist_del_element(session->sessions_list, session, _session_compare);
 
3014
                        num_persistent--;
 
3015
                }
 
3016
        mutex_unlock(mx_lock);
 
3017
 
 
3018
#ifdef HAVE_OCI_9_2
 
3019
        /* free environment handle (and fix bug #29652 with growing .msb FD number under weirdie Solarises) */
 
3020
        CALL_OCI(
 
3021
                OCIHandleFree(
 
3022
                                (dvoid *) session->pEnv, 
 
3023
                                OCI_HTYPE_ENV
 
3024
                )
 
3025
        );
 
3026
#endif
 
3027
 
 
3028
        if (session->exclusive) {
 
3029
                efree(session);
 
3030
        }
 
3031
}
 
3032
/* }}} */
 
3033
 
 
3034
/* {{{ _oci_open_server()
 
3035
*/
 
3036
static oci_server *_oci_open_server(char *dbname,int persistent)
 
3037
 
3038
        oci_server *server, *pserver = NULL;
 
3039
        TSRMLS_FETCH();
 
3040
 
 
3041
        /* 
 
3042
           check if we already have this server open 
 
3043
           
 
3044
           we will reuse servers within a request no matter if the user requested persistent 
 
3045
           connections or not!
 
3046
           
 
3047
           but only as pesistent requested connections will be kept between requests!
 
3048
        */
 
3049
 
 
3050
        /* TODO either keep servers global or don't reuse them at all */
 
3051
        zend_ts_hash_find(persistent_servers, dbname, strlen(dbname)+1, (void **) &pserver);
 
3052
 
 
3053
        if (pserver) {
 
3054
                /* XXX ini-flag */
 
3055
                /*
 
3056
                if (!oci_ping(pserver)) {
 
3057
                        pserver->is_open = 0;
 
3058
                }
 
3059
                */
 
3060
                if (pserver->is_open) {
 
3061
                        /* if our new users uses this connection persistent, we're keeping it! */
 
3062
                        if (persistent) {
 
3063
                                pserver->persistent = persistent;
 
3064
                        }
 
3065
 
 
3066
                        return pserver;
 
3067
                } else { /* server "died" in the meantime - try to reconnect! */
 
3068
                        _oci_close_server(pserver);
 
3069
                        /* breakthru to open */
 
3070
                }
 
3071
        }
 
3072
        
 
3073
        server = ecalloc(1,sizeof(oci_server));
 
3074
 
 
3075
        server->persistent = persistent;
 
3076
        server->dbname = strdup(SAFE_STRING(dbname));
 
3077
 
 
3078
        CALL_OCI(
 
3079
                OCIHandleAlloc(
 
3080
                        OCI(pEnv), 
 
3081
                        (dvoid **)&server->pServer, 
 
3082
                        OCI_HTYPE_SERVER, 
 
3083
                        0, 
 
3084
                        NULL
 
3085
                )
 
3086
        );
 
3087
 
 
3088
        CALL_OCI_RETURN(OCI(error),
 
3089
                OCIServerAttach(
 
3090
                        server->pServer, 
 
3091
                        OCI(pError), 
 
3092
                        (text*)server->dbname, 
 
3093
                        strlen(server->dbname), 
 
3094
                        (ub4) OCI_DEFAULT
 
3095
                )
 
3096
        );
 
3097
 
 
3098
        if (OCI(error)) {
 
3099
                oci_error(OCI(pError), "_oci_open_server", OCI(error));
 
3100
                goto CLEANUP;
 
3101
        }
 
3102
        
 
3103
        zend_ts_hash_update(persistent_servers,
 
3104
                                         server->dbname,
 
3105
                                         strlen(server->dbname)+1, 
 
3106
                                         (void *)server,
 
3107
                                         sizeof(oci_server),
 
3108
                                         (void**)&pserver);
 
3109
 
 
3110
        pserver->num  = zend_list_insert(pserver,le_server);
 
3111
        pserver->is_open = 1;
 
3112
 
 
3113
        oci_debug("_oci_open_server new conn=%d dname=%s",server->num,server->dbname);
 
3114
 
 
3115
        efree(server);
 
3116
 
 
3117
        return pserver;
 
3118
 
 
3119
CLEANUP:
 
3120
        oci_debug("_oci_open_server: FAILURE -> CLEANUP called");
 
3121
 
 
3122
        _oci_close_server(server);
 
3123
                
 
3124
        return 0;
 
3125
}
 
3126
 
 
3127
#if 0 
 
3128
        server->failover.fo_ctx = (dvoid *) server;
 
3129
        server->failover.callback_function = oci_failover_callback;
 
3130
 
 
3131
        error = OCIAttrSet((dvoid *)server->pServer,
 
3132
                                                (ub4) OCI_HTYPE_SERVER,
 
3133
                                                (dvoid *) &server->failover, 
 
3134
                                                (ub4) 0,
 
3135
                                                (ub4) OCI_ATTR_FOCBK,
 
3136
                                                OCI(pError));
 
3137
 
 
3138
        if (error) {
 
3139
                oci_error(OCI(pError), "_oci_open_server OCIAttrSet OCI_ATTR_FOCBK", error);
 
3140
                goto CLEANUP;
 
3141
        }
 
3142
#endif
 
3143
/* }}} */
 
3144
 
 
3145
/* {{{ _oci_close_server()
 
3146
*/
 
3147
static int _oci_session_cleanup(void *data TSRMLS_DC)
 
3148
{
 
3149
        zend_rsrc_list_entry *le = (zend_rsrc_list_entry *) data;
 
3150
 
 
3151
        if (le->type == le_session) {
 
3152
                oci_server *server = ((oci_session*) le->ptr)->server;
 
3153
                if (server && server->is_open == 2) 
 
3154
                        return 1;
 
3155
        }
 
3156
        return 0;
 
3157
}
 
3158
 
 
3159
static void _oci_close_server(oci_server *server)
 
3160
{
 
3161
        char *dbname;
 
3162
        int oldopen;
 
3163
        TSRMLS_FETCH();
 
3164
 
 
3165
        oldopen = server->is_open;
 
3166
        server->is_open = 2;
 
3167
        if (!OCI(shutdown)) {
 
3168
                zend_hash_apply(&EG(regular_list), (apply_func_t) _oci_session_cleanup TSRMLS_CC);
 
3169
        }
 
3170
        server->is_open = oldopen;
 
3171
 
 
3172
        oci_debug("START _oci_close_server: detaching conn=%d dbname=%s",server->num,server->dbname);
 
3173
 
 
3174
        /* XXX close server here */
 
3175
 
 
3176
        if (server->is_open) {
 
3177
                if (server->pServer && OCI(pError)) {
 
3178
                        CALL_OCI_RETURN(OCI(error), 
 
3179
                                OCIServerDetach(
 
3180
                                        server->pServer, 
 
3181
                                        OCI(pError), 
 
3182
                                        OCI_DEFAULT
 
3183
                                )
 
3184
                        );
 
3185
 
 
3186
                        if (OCI(error)) {
 
3187
                                oci_error(OCI(pError), "oci_close_server OCIServerDetach", OCI(error));
 
3188
                        }
 
3189
                }
 
3190
        } else {
 
3191
                oci_debug("_oci_close_server: closing DEAD server");
 
3192
        }
 
3193
 
 
3194
        if (server->pServer) {
 
3195
                CALL_OCI(
 
3196
                        OCIHandleFree(
 
3197
                                (dvoid *) server->pServer, 
 
3198
                                (ub4) OCI_HTYPE_SERVER
 
3199
                        )
 
3200
                );
 
3201
        }
 
3202
 
 
3203
        dbname = server->dbname;
 
3204
 
 
3205
        if (!OCI(shutdown)) {
 
3206
                zend_ts_hash_del(persistent_servers, dbname, strlen(dbname)+1);
 
3207
        }
 
3208
 
 
3209
        free(dbname);
 
3210
}
 
3211
/* }}} */
 
3212
 
 
3213
/* {{{ oci_do_connect()
 
3214
   Connect to an Oracle database and log on. returns a new session. */
 
3215
static void oci_do_connect(INTERNAL_FUNCTION_PARAMETERS,int persistent,int exclusive)
 
3216
{
 
3217
        char *username, *password, *dbname, *charset;
 
3218
        zval **userParam, **passParam, **dbParam, **charParam;
 
3219
        oci_server *server = 0;
 
3220
        oci_session *session = 0;
 
3221
        oci_connection *connection = 0;
 
3222
        
 
3223
        /* if a forth parameter is handed over, it is the charset identifier (but is only used in Oracle 9i+) */
 
3224
        if (zend_get_parameters_ex(4, &userParam, &passParam, &dbParam, &charParam) == SUCCESS) {
 
3225
                convert_to_string_ex(userParam);
 
3226
                convert_to_string_ex(passParam);
 
3227
                convert_to_string_ex(dbParam);
 
3228
                convert_to_string_ex(charParam);
 
3229
 
 
3230
                username = Z_STRVAL_PP(userParam);
 
3231
                password = Z_STRVAL_PP(passParam);
 
3232
                dbname = Z_STRVAL_PP(dbParam);
 
3233
                charset = Z_STRVAL_PP(charParam);
 
3234
                oci_debug("oci_do_connect: using charset=%s",charset);
 
3235
        } else if (zend_get_parameters_ex(3, &userParam, &passParam, &dbParam) == SUCCESS) {
 
3236
                convert_to_string_ex(userParam);
 
3237
                convert_to_string_ex(passParam);
 
3238
                convert_to_string_ex(dbParam);
 
3239
 
 
3240
                username = Z_STRVAL_PP(userParam);
 
3241
                password = Z_STRVAL_PP(passParam);
 
3242
                dbname = Z_STRVAL_PP(dbParam);
 
3243
                charset = "";
 
3244
        } else if (zend_get_parameters_ex(2, &userParam, &passParam) == SUCCESS) {
 
3245
                convert_to_string_ex(userParam);
 
3246
                convert_to_string_ex(passParam);
 
3247
 
 
3248
                username = Z_STRVAL_PP(userParam);
 
3249
                password = Z_STRVAL_PP(passParam);
 
3250
                dbname = "";
 
3251
                charset = "";
 
3252
        } else {
 
3253
                WRONG_PARAM_COUNT;
 
3254
        }
 
3255
 
 
3256
        connection = (oci_connection *) ecalloc(1,sizeof(oci_connection));
 
3257
 
 
3258
        if (!connection) {
 
3259
                goto CLEANUP;
 
3260
        }
 
3261
 
 
3262
        server = _oci_open_server(dbname,persistent);
 
3263
 
 
3264
        if (!server) {
 
3265
                goto CLEANUP;
 
3266
        }
 
3267
 
 
3268
        if (exclusive) {
 
3269
                /* exlusive session can never be persistent!*/
 
3270
                persistent = 0;
 
3271
        } else {
 
3272
                /* if our server-context is not persistent we can't */
 
3273
                persistent = (server->persistent) ? persistent : 0;
 
3274
        }
 
3275
 
 
3276
        session = _oci_open_session(server,username,password,persistent,exclusive,charset);
 
3277
 
 
3278
        if (!session) {
 
3279
                goto CLEANUP;
 
3280
        }
 
3281
 
 
3282
        /* set our session */
 
3283
        connection->session = session;
 
3284
 
 
3285
        /* allocate our private error-handle */
 
3286
        CALL_OCI_RETURN(OCI(error),
 
3287
                OCIHandleAlloc(
 
3288
                        connection->session->pEnv, 
 
3289
                        (dvoid **)&connection->pError, 
 
3290
                        OCI_HTYPE_ERROR, 
 
3291
                        0, 
 
3292
                        NULL
 
3293
                )
 
3294
        );
 
3295
 
 
3296
        if (OCI(error) != OCI_SUCCESS) {
 
3297
                oci_error(OCI(pError), "oci_do_connect: OCIHandleAlloc OCI_HTYPE_ERROR",OCI(error));
 
3298
                goto CLEANUP;
 
3299
        }
 
3300
 
 
3301
        /* allocate our service-context */
 
3302
        CALL_OCI_RETURN(OCI(error),
 
3303
                OCIHandleAlloc(
 
3304
                        connection->session->pEnv, 
 
3305
                        (dvoid **)&connection->pServiceContext, 
 
3306
                        OCI_HTYPE_SVCCTX, 
 
3307
                        0, 
 
3308
                        NULL
 
3309
                )
 
3310
        );
 
3311
 
 
3312
        if (OCI(error) != OCI_SUCCESS) {
 
3313
                oci_error(OCI(pError), "oci_do_connect: OCIHandleAlloc OCI_HTYPE_SVCCTX",OCI(error));
 
3314
                goto CLEANUP;
 
3315
        }
 
3316
 
 
3317
        /* Set the server handle in service handle */ 
 
3318
        CALL_OCI_RETURN(connection->error,
 
3319
                OCIAttrSet(
 
3320
                        connection->pServiceContext, 
 
3321
                        OCI_HTYPE_SVCCTX, 
 
3322
                        server->pServer, 
 
3323
                        0, 
 
3324
                        OCI_ATTR_SERVER, 
 
3325
                        connection->pError
 
3326
                )
 
3327
        );
 
3328
 
 
3329
        if (connection->error != OCI_SUCCESS) {
 
3330
                oci_error(connection->pError, "oci_do_connect: OCIAttrSet OCI_ATTR_SERVER", connection->error);
 
3331
                goto CLEANUP;
 
3332
        }
 
3333
 
 
3334
        /* Set the Authentication handle in the service handle */
 
3335
        CALL_OCI_RETURN(connection->error,
 
3336
                OCIAttrSet(
 
3337
                        connection->pServiceContext, 
 
3338
                        OCI_HTYPE_SVCCTX, 
 
3339
                        session->pSession, 
 
3340
                        0, 
 
3341
                        OCI_ATTR_SESSION, 
 
3342
                        connection->pError
 
3343
                )
 
3344
        );
 
3345
 
 
3346
        if (connection->error != OCI_SUCCESS) {
 
3347
                oci_error(connection->pError, "oci_do_connect: OCIAttrSet OCI_ATTR_SESSION", connection->error);
 
3348
                goto CLEANUP;
 
3349
        }
 
3350
 
 
3351
        /*
 
3352
        OCIAttrSet((dvoid *)session->server->pServer, 
 
3353
                                OCI_HTYPE_SERVER,
 
3354
                                (dvoid *) "demo",
 
3355
                                0,
 
3356
                                OCI_ATTR_EXTERNAL_NAME,
 
3357
                                connection->pError);
 
3358
 
 
3359
        OCIAttrSet((dvoid *)session->server->pServer,
 
3360
                                OCI_HTYPE_SERVER,
 
3361
                                (dvoid *) "txn demo2",
 
3362
                                0,
 
3363
                                OCI_ATTR_INTERNAL_NAME,
 
3364
                                connection->pError);
 
3365
        */
 
3366
 
 
3367
        connection->id = zend_list_insert(connection, le_conn);
 
3368
 
 
3369
        connection->is_open = 1;
 
3370
 
 
3371
        oci_debug("oci_do_connect: id=%d",connection->id);
 
3372
 
 
3373
        RETURN_RESOURCE(connection->id);
 
3374
        
 
3375
CLEANUP:
 
3376
        oci_debug("oci_do_connect: FAILURE -> CLEANUP called");
 
3377
 
 
3378
        if (connection->id) {
 
3379
                zend_list_delete(connection->id);
 
3380
        } else {
 
3381
                _oci_conn_list_dtor(connection TSRMLS_CC);
 
3382
        }
 
3383
 
 
3384
        RETURN_FALSE;
 
3385
}
 
3386
/* }}} */
 
3387
 
 
3388
/* {{{ oci_lob_flush()
 
3389
*/
 
3390
static int oci_lob_flush(oci_descriptor* descr, int flush_flag TSRMLS_DC)
 
3391
{
 
3392
        OCILobLocator *mylob;
 
3393
        oci_connection *connection;
 
3394
        
 
3395
        mylob = (OCILobLocator *) descr->ocidescr;
 
3396
 
 
3397
        if (!mylob) {
 
3398
                return 0;
 
3399
        }
 
3400
 
 
3401
        /* do not really flush buffer, but reporting success
 
3402
         * to suppress OCI error when flushing not used buffer
 
3403
         * */
 
3404
        if (descr->buffering != 2) {
 
3405
                return 1;
 
3406
        }
 
3407
 
 
3408
        connection = descr->conn;
 
3409
 
 
3410
        CALL_OCI_RETURN(connection->error,
 
3411
                OCILobFlushBuffer(
 
3412
                        connection->pServiceContext,
 
3413
                        connection->pError,
 
3414
                        mylob,
 
3415
                        flush_flag
 
3416
                )
 
3417
        );
 
3418
 
 
3419
        oci_debug("oci_lob_flush: flush_flag=%d",flush_flag);
 
3420
 
 
3421
        if (connection->error) {
 
3422
                oci_error(connection->pError, "OCILobFlushBuffer", connection->error);
 
3423
                oci_handle_error(connection, connection->error);
 
3424
                return 0;
 
3425
        }
 
3426
 
 
3427
        /* marking buffer as enabled and not used */
 
3428
        descr->buffering = 1;
 
3429
        return 1;
 
3430
}
 
3431
/* }}} */
 
3432
 
 
3433
/* {{{ php_oci_fetch_row()
 
3434
*/
 
3435
static void php_oci_fetch_row (INTERNAL_FUNCTION_PARAMETERS, int mode, int expected_args)
 
3436
{
 
3437
        zval **stmt, **arg2, **arg3;
 
3438
        oci_statement *statement;
 
3439
        oci_out_column *column;
 
3440
        ub4 nrows = 1;
 
3441
        int i;
 
3442
 
 
3443
        if (ZEND_NUM_ARGS() > expected_args) {
 
3444
                WRONG_PARAM_COUNT;
 
3445
        }
 
3446
 
 
3447
        if (expected_args > 2) {
 
3448
                /* only for ocifetchinto BC */
 
3449
 
 
3450
                switch (ZEND_NUM_ARGS()) {
 
3451
                        case 2:
 
3452
                                if (zend_get_parameters_ex(2, &stmt, &arg2) == FAILURE) {
 
3453
                                        RETURN_FALSE;
 
3454
                                }
 
3455
                                if (!mode) {
 
3456
                                        mode = OCI_NUM;
 
3457
                                }
 
3458
                                break;
 
3459
 
 
3460
                        case 3:
 
3461
                                if (zend_get_parameters_ex(3, &stmt, &arg2, &arg3) == FAILURE) {
 
3462
                                        RETURN_FALSE;
 
3463
                                }
 
3464
                                convert_to_long_ex(arg3);
 
3465
                                mode = Z_LVAL_PP(arg3);
 
3466
                                break;
 
3467
 
 
3468
                        default:
 
3469
                                WRONG_PARAM_COUNT;
 
3470
                                break;
 
3471
                }
 
3472
 
 
3473
        } else {
 
3474
 
 
3475
                switch (ZEND_NUM_ARGS()) {
 
3476
                        case 1:
 
3477
                                if (zend_get_parameters_ex(1, &stmt) == FAILURE) {
 
3478
                                        RETURN_FALSE;
 
3479
                                }
 
3480
                                if (!mode) {
 
3481
                                        mode = OCI_BOTH;
 
3482
                                }
 
3483
                                break;
 
3484
 
 
3485
                        case 2:
 
3486
                                if (zend_get_parameters_ex(2, &stmt, &arg2)==FAILURE) {
 
3487
                                        RETURN_FALSE;
 
3488
                                }
 
3489
                                convert_to_long_ex(arg2);
 
3490
                                mode = Z_LVAL_PP(arg2);
 
3491
                                break;
 
3492
                        
 
3493
                        default:
 
3494
                                WRONG_PARAM_COUNT;
 
3495
                                break;
 
3496
                }
 
3497
        }
 
3498
 
 
3499
        OCI_GET_STMT(statement,stmt);
 
3500
 
 
3501
        if (!oci_fetch(statement, nrows, "OCIFetchInto" TSRMLS_CC)) {
 
3502
                RETURN_FALSE;
 
3503
        }
 
3504
 
 
3505
        array_init(return_value);
 
3506
 
 
3507
        for (i = 0; i < statement->ncolumns; i++) {
 
3508
                column = oci_get_col(statement, i + 1, 0);
 
3509
                if (column == NULL) {
 
3510
                        continue;
 
3511
                }
 
3512
                if ((column->indicator == -1) && ((mode & OCI_RETURN_NULLS) == 0)) {
 
3513
                        continue;
 
3514
                }
 
3515
 
 
3516
                if (!(column->indicator == -1)) {
 
3517
                        zval *element;
 
3518
                        
 
3519
                        MAKE_STD_ZVAL(element);
 
3520
                        _oci_make_zval(element,statement,column,"OCIFetchInto",mode TSRMLS_CC);
 
3521
 
 
3522
                        if (mode & OCI_NUM || !(mode & OCI_ASSOC)) {
 
3523
                                add_index_zval(return_value, i, element);
 
3524
                        }
 
3525
                        if (mode & OCI_ASSOC) {
 
3526
                                if (mode & OCI_NUM) {
 
3527
                                        ZVAL_ADDREF(element);
 
3528
                                }
 
3529
                                add_assoc_zval(return_value, column->name, element);
 
3530
                        }
 
3531
                
 
3532
                } else {
 
3533
                        if (mode & OCI_NUM || !(mode & OCI_ASSOC)) {
 
3534
                                add_index_null(return_value, i);
 
3535
                        }
 
3536
                        if (mode & OCI_ASSOC) {
 
3537
                                add_assoc_null(return_value, column->name);
 
3538
                        }
 
3539
                }
 
3540
        }
 
3541
 
 
3542
        if (expected_args > 2) {
 
3543
                /* only for ocifetchinto BC
 
3544
                 * in all other cases we return array, not long
 
3545
                 */
 
3546
                REPLACE_ZVAL_VALUE(arg2, return_value, 1); /* copy return_value to given reference */
 
3547
                zval_dtor(return_value);
 
3548
                RETURN_LONG(statement->ncolumns);
 
3549
        }
 
3550
}
 
3551
/* }}} */
 
3552
 
 
3553
/************************* EXTENSION FUNCTIONS *************************/
 
3554
 
 
3555
/* {{{ proto bool oci_define_by_name(resource stmt, string name, mixed &var [, int type])
 
3556
   Define a PHP variable to an Oracle column by name */
 
3557
/* if you want to define a LOB/CLOB etc make sure you allocate it via OCINewDescriptor BEFORE defining!!! */
 
3558
PHP_FUNCTION(oci_define_by_name)
 
3559
{
 
3560
        zval **stmt, **name, **var, **type;
 
3561
        oci_statement *statement;
 
3562
        oci_define *define, *tmp_define;
 
3563
        ub2     ocitype = SQLT_CHR; /* zero terminated string */
 
3564
        int ac = ZEND_NUM_ARGS();
 
3565
 
 
3566
        if (ac < 3 || ac > 4 || zend_get_parameters_ex(ac, &stmt, &name, &var, &type) == FAILURE) {
 
3567
                WRONG_PARAM_COUNT;
 
3568
        }
 
3569
 
 
3570
        switch (ac) {
 
3571
                case 4:
 
3572
                        convert_to_long_ex(type);
 
3573
                        ocitype = (ub2) Z_LVAL_PP(type);
 
3574
                        /* possible breakthru */
 
3575
        }
 
3576
 
 
3577
        OCI_GET_STMT(statement,stmt);
 
3578
 
 
3579
        convert_to_string_ex(name);
 
3580
 
 
3581
        if (statement->defines == NULL) {
 
3582
                ALLOC_HASHTABLE(statement->defines);
 
3583
                zend_hash_init(statement->defines, 13, NULL, _oci_define_hash_dtor, 0);
 
3584
        }
 
3585
 
 
3586
        define = ecalloc(1,sizeof(oci_define));
 
3587
 
 
3588
        if (zend_hash_add(statement->defines,
 
3589
                                          Z_STRVAL_PP(name),
 
3590
                                          Z_STRLEN_PP(name),
 
3591
                                          define,
 
3592
                                          sizeof(oci_define),
 
3593
                                          (void **)&tmp_define) == SUCCESS) {
 
3594
                efree(define);
 
3595
                define = tmp_define;
 
3596
        } else {
 
3597
                efree(define);
 
3598
                RETURN_FALSE;
 
3599
        }
 
3600
 
 
3601
        define->name = (text*) estrndup(Z_STRVAL_PP(name),Z_STRLEN_PP(name));
 
3602
        define->name_len = Z_STRLEN_PP(name);
 
3603
        define->type = ocitype;
 
3604
        define->zval = *var;
 
3605
        zval_add_ref(var);
 
3606
 
 
3607
        RETURN_TRUE;
 
3608
}
 
3609
/* }}} */
 
3610
 
 
3611
/* {{{ proto bool oci_bind_by_name(resource stmt, string name, mixed &var, [, int maxlength [, int type]])
 
3612
   Bind a PHP variable to an Oracle placeholder by name */
 
3613
/* if you want to bind a LOB/CLOB etc make sure you allocate it via OCINewDescriptor BEFORE binding!!! */
 
3614
PHP_FUNCTION(oci_bind_by_name)
 
3615
{
 
3616
        zval **stmt, **name, **var, **maxlen, **type;
 
3617
        oci_statement *statement;
 
3618
        oci_statement *bindstmt;
 
3619
        oci_bind bind, *bindp;
 
3620
        oci_descriptor *descr;
 
3621
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
3622
        oci_collection *coll;
 
3623
        dvoid *mycoll = 0;
 
3624
#endif
 
3625
        int mode = OCI_DATA_AT_EXEC;
 
3626
        ub2     ocitype = SQLT_CHR; /* unterminated string */
 
3627
        OCIStmt *mystmt = 0;
 
3628
        dvoid *mydescr = 0;
 
3629
        sb4 value_sz = -1;
 
3630
        int ac = ZEND_NUM_ARGS(), inx;
 
3631
 
 
3632
        if (ac < 3 || ac > 5 || zend_get_parameters_ex(ac, &stmt, &name, &var, &maxlen, &type) == FAILURE) {
 
3633
                WRONG_PARAM_COUNT;
 
3634
        }
 
3635
 
 
3636
        switch (ac) {
 
3637
                case 5:
 
3638
                        convert_to_long_ex(type);
 
3639
                        ocitype = (ub2) Z_LVAL_PP(type);
 
3640
                        /* possible breakthru */
 
3641
                case 4:
 
3642
                        convert_to_long_ex(maxlen);
 
3643
                        value_sz = Z_LVAL_PP(maxlen);
 
3644
                        /* possible breakthru */
 
3645
        }
 
3646
 
 
3647
        OCI_GET_STMT(statement,stmt);
 
3648
 
 
3649
        switch (ocitype) {
 
3650
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
3651
                case SQLT_NTY:
 
3652
                        if (Z_TYPE_PP(var) != IS_OBJECT) {
 
3653
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Variable must be allocated using OCINewCollection()");
 
3654
                                RETURN_FALSE;
 
3655
                        }
 
3656
                        if ((inx = _oci_get_ocicoll(*var,&coll TSRMLS_CC)) == 0) {
 
3657
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Variable must be allocated using OCINewCollection()");
 
3658
                                RETURN_FALSE;
 
3659
                        }
 
3660
                        if (!(mycoll = (dvoid *) coll->coll)) {
 
3661
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Collection empty");
 
3662
                                RETURN_FALSE;
 
3663
                        }
 
3664
                        value_sz = sizeof(void*);
 
3665
                        mode = OCI_DEFAULT;
 
3666
break;
 
3667
#endif
 
3668
                case SQLT_BFILEE:
 
3669
                case SQLT_CFILEE:
 
3670
                case SQLT_CLOB:
 
3671
                case SQLT_BLOB:
 
3672
                case SQLT_RDD:
 
3673
                        if (Z_TYPE_PP(var) != IS_OBJECT) {
 
3674
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Variable must be allocated using OCINewDescriptor()");
 
3675
                                RETURN_FALSE;
 
3676
                        }
 
3677
 
 
3678
                        if ((inx = _oci_get_ocidesc(*var,&descr TSRMLS_CC)) == 0) {
 
3679
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Variable must be allocated using OCINewDescriptor()");
 
3680
                                RETURN_FALSE;
 
3681
                        }
 
3682
                
 
3683
                        if (!(mydescr = (dvoid *) descr->ocidescr)) {
 
3684
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Descriptor empty");
 
3685
                                RETURN_FALSE;
 
3686
                        }
 
3687
                        value_sz = sizeof(void*);
 
3688
                        break;
 
3689
 
 
3690
                case SQLT_RSET:
 
3691
                        OCI_GET_STMT(bindstmt,var);
 
3692
 
 
3693
                        if (!(mystmt = bindstmt->pStmt)) {
 
3694
                                RETURN_FALSE;
 
3695
                        }
 
3696
                        value_sz = sizeof(void*);
 
3697
                        break;
 
3698
                case SQLT_CHR:
 
3699
                        break;
 
3700
                default:
 
3701
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown or unsupported datatype given: %u", ocitype);
 
3702
                        RETURN_FALSE;
 
3703
                        break;
 
3704
        }
 
3705
        
 
3706
        if ((ocitype == SQLT_CHR) && (value_sz == -1)) {
 
3707
                convert_to_string_ex(var);
 
3708
                value_sz = Z_STRLEN_PP(var);
 
3709
        }
 
3710
 
 
3711
        if (value_sz == 0) { 
 
3712
                value_sz = 1;
 
3713
        }
 
3714
 
 
3715
        convert_to_string_ex(name);
 
3716
 
 
3717
        if (!statement->binds) {
 
3718
                ALLOC_HASHTABLE(statement->binds);
 
3719
                zend_hash_init(statement->binds, 13, NULL, _oci_bind_hash_dtor, 0);
 
3720
        }
 
3721
 
 
3722
        memset((void*)&bind,0,sizeof(oci_bind));
 
3723
        zend_hash_update(statement->binds, Z_STRVAL_PP(name), Z_STRLEN_PP(name) + 1, &bind, sizeof(oci_bind), (void **)&bindp);
 
3724
        
 
3725
        bindp->descr = mydescr;
 
3726
        bindp->pStmt = mystmt;
 
3727
        bindp->zval = *var;
 
3728
        zval_add_ref(var); 
 
3729
        
 
3730
        CALL_OCI_RETURN(statement->error,
 
3731
                OCIBindByName(
 
3732
                        statement->pStmt,                /* statement handle */
 
3733
                        (OCIBind **)&bindp->pBind,       /* bind hdl (will alloc) */
 
3734
                        statement->pError,               /* error handle */
 
3735
                        (text*) Z_STRVAL_PP(name),       /* placeholder name */                                   
 
3736
                        Z_STRLEN_PP(name),               /* placeholder length */
 
3737
                        (dvoid *)0,                      /* in/out data */
 
3738
                        value_sz, /* OCI_MAX_DATA_SIZE, */ /* max size of input/output data */
 
3739
                        (ub2)ocitype,                    /* in/out data type */
 
3740
                        (dvoid *)&bindp->indicator,      /* indicator (ignored) */
 
3741
                        (ub2 *)0,                        /* size array (ignored) */
 
3742
                        (ub2 *)&bindp->retcode,          /* return code (ignored) */
 
3743
                        (ub4)0,                          /* maxarr_len (PL/SQL only?) */
 
3744
                        (ub4 *)0,                        /* actual array size (PL/SQL only?) */
 
3745
                        mode                             /* mode */
 
3746
                )
 
3747
        );
 
3748
 
 
3749
        if (statement->error != OCI_SUCCESS) {
 
3750
                oci_error(statement->pError, "OCIBindByName", statement->error);
 
3751
                oci_handle_error(statement->conn, statement->error);
 
3752
                RETURN_FALSE;
 
3753
        }
 
3754
 
 
3755
        if (mode == OCI_DATA_AT_EXEC) {
 
3756
                CALL_OCI_RETURN(statement->error,
 
3757
                        OCIBindDynamic(
 
3758
                                bindp->pBind, 
 
3759
                                statement->pError, 
 
3760
                                (dvoid *)bindp, 
 
3761
                                oci_bind_in_callback, 
 
3762
                                (dvoid *)bindp, 
 
3763
                                oci_bind_out_callback
 
3764
                        )
 
3765
                );
 
3766
 
 
3767
                if (statement->error != OCI_SUCCESS) {
 
3768
                        oci_error(statement->pError, "OCIBindDynamic", statement->error);
 
3769
                        oci_handle_error(statement->conn, statement->error);
 
3770
                        RETURN_FALSE;
 
3771
                }
 
3772
        }
 
3773
 
 
3774
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
3775
        if (ocitype == SQLT_NTY) {
 
3776
                /* Bind object */
 
3777
                CALL_OCI_RETURN(statement->error,
 
3778
                        OCIBindObject(
 
3779
                                bindp->pBind, 
 
3780
                                statement->pError, 
 
3781
                                coll->tdo, 
 
3782
                                (dvoid **) &(coll->coll), 
 
3783
                                (ub4 *) 0, 
 
3784
                                (dvoid **) 0, 
 
3785
                                (ub4 *) 0
 
3786
                        )
 
3787
                );
 
3788
 
 
3789
                if (statement->error) {
 
3790
                        oci_error(statement->pError, "OCIBindObject", statement->error);
 
3791
                        RETURN_FALSE;
 
3792
                }
 
3793
        }
 
3794
#endif
 
3795
        
 
3796
        RETURN_TRUE;
 
3797
}
 
3798
/* }}} */
 
3799
 
 
3800
/* {{{ proto bool oci_free_descriptor()
 
3801
   Deletes large object description */
 
3802
PHP_FUNCTION(oci_free_descriptor)
 
3803
{
 
3804
        zval *id;
 
3805
        int inx;
 
3806
        oci_descriptor *descriptor;
 
3807
 
 
3808
        if ((id = getThis()) != 0) {
 
3809
                inx = _oci_get_ocidesc(id,&descriptor TSRMLS_CC);
 
3810
                if (inx) {
 
3811
                        oci_debug("oci_free_descriptor: descr=%d",inx);
 
3812
                        zend_list_delete(inx);
 
3813
                        RETURN_TRUE;
 
3814
                }
 
3815
        }
 
3816
 
 
3817
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "oci_free_descriptor() should not be called like this. Use $somelob->free() to free a LOB");
 
3818
 
 
3819
        RETURN_FALSE;
 
3820
}
 
3821
/* }}} */
 
3822
 
 
3823
/* {{{ proto bool oci_lob_save( string data [, int offset ])
 
3824
   Saves a large object */
 
3825
PHP_FUNCTION(oci_lob_save)
 
3826
{
 
3827
        zval *id, **arg,**oarg;
 
3828
        OCILobLocator *mylob;
 
3829
        oci_connection *connection;
 
3830
        oci_descriptor *descr;
 
3831
        int offparam,inx;
 
3832
        ub4 loblen;
 
3833
        ub4 curloblen;
 
3834
        ub4 offset;
 
3835
 
 
3836
        if ((id = getThis()) != 0) {
 
3837
                if ((inx = _oci_get_ocidesc(id,&descr TSRMLS_CC)) == 0) {
 
3838
                        RETURN_FALSE;
 
3839
                }
 
3840
                
 
3841
                mylob = (OCILobLocator *) descr->ocidescr;
 
3842
 
 
3843
                if (!mylob) {
 
3844
                        RETURN_FALSE;
 
3845
                }
 
3846
 
 
3847
                connection = descr->conn;
 
3848
 
 
3849
                offset = 0;     
 
3850
                if (zend_get_parameters_ex(2, &arg, &oarg) == SUCCESS) {
 
3851
                        convert_to_long_ex(oarg);
 
3852
                        offparam = Z_LVAL_PP(oarg);
 
3853
 
 
3854
                        CALL_OCI_RETURN(connection->error,
 
3855
                                OCILobGetLength(
 
3856
                                        connection->pServiceContext, 
 
3857
                                        connection->pError, 
 
3858
                                        mylob, 
 
3859
                                        &curloblen
 
3860
                                )
 
3861
                        );
 
3862
 
 
3863
                        oci_debug("oci_lob_save: curloblen=%d",curloblen);
 
3864
 
 
3865
                        if (offparam == -1) {
 
3866
                                offset = curloblen;
 
3867
                        } else if ((ub4)offparam >= curloblen) {
 
3868
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset is bigger than current LOB-Size - appending");
 
3869
                                offset = curloblen;
 
3870
                        } else {
 
3871
                                offset = (ub4)offparam;
 
3872
                        }
 
3873
                } else if (zend_get_parameters_ex(1, &arg) == FAILURE) {
 
3874
                        WRONG_PARAM_COUNT;
 
3875
                }
 
3876
 
 
3877
                convert_to_string_ex(arg);
 
3878
                loblen = Z_STRLEN_PP(arg);
 
3879
        
 
3880
                if (loblen < 1) {
 
3881
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot save a lob which size is less than 1 byte");
 
3882
                        RETURN_FALSE;
 
3883
                }
 
3884
 
 
3885
                if (offset <= 0) {
 
3886
                        offset = 1;
 
3887
                }
 
3888
                
 
3889
                CALL_OCI_RETURN(connection->error,
 
3890
                        OCILobWrite(
 
3891
                                connection->pServiceContext, 
 
3892
                                connection->pError,
 
3893
                                mylob,
 
3894
                                &loblen,
 
3895
                                (ub4) offset,
 
3896
                                (dvoid *) Z_STRVAL_PP(arg),
 
3897
                                (ub4) loblen,
 
3898
                                OCI_ONE_PIECE,
 
3899
                                (dvoid *)0,
 
3900
                                (OCICallbackLobWrite) 0,
 
3901
                                (ub2) 0,
 
3902
                                (ub1) SQLCS_IMPLICIT
 
3903
                        )
 
3904
                );
 
3905
 
 
3906
                oci_debug("oci_lob_save: size=%d offset=%d",loblen,offset);
 
3907
 
 
3908
                if (connection->error) {
 
3909
                        oci_error(connection->pError, "OCILobWrite", connection->error);
 
3910
                        oci_handle_error(connection, connection->error);
 
3911
                        RETURN_FALSE;
 
3912
                }
 
3913
 
 
3914
                RETURN_TRUE;
 
3915
        }
 
3916
 
 
3917
  RETURN_FALSE;
 
3918
}
 
3919
/* }}} */
 
3920
 
 
3921
/* {{{ proto bool oci_lob_import( string filename )
 
3922
   Saves a large object to file */
 
3923
PHP_FUNCTION(oci_lob_import)
 
3924
{
 
3925
        zval *id, **arg;
 
3926
        OCILobLocator *mylob;
 
3927
        oci_connection *connection;
 
3928
        oci_descriptor *descr;
 
3929
        char *filename;
 
3930
        int fp,inx;
 
3931
        char buf[8192];
 
3932
        ub4 offset = 1;
 
3933
        ub4 loblen;
 
3934
 
 
3935
        if ((id = getThis()) != 0) {
 
3936
                if ((inx = _oci_get_ocidesc(id,&descr TSRMLS_CC)) == 0) {
 
3937
                        RETURN_FALSE;
 
3938
                }
 
3939
 
 
3940
                mylob = (OCILobLocator *) descr->ocidescr;
 
3941
 
 
3942
                if (!mylob) {
 
3943
                        RETURN_FALSE;
 
3944
                }
 
3945
 
 
3946
                connection = descr->conn;
 
3947
 
 
3948
                if (zend_get_parameters_ex(1, &arg) == FAILURE) {
 
3949
                        WRONG_PARAM_COUNT;
 
3950
                }
 
3951
 
 
3952
                convert_to_string_ex(arg);
 
3953
 
 
3954
                if (php_check_open_basedir(Z_STRVAL_PP(arg) TSRMLS_CC)) {
 
3955
                        RETURN_FALSE;
 
3956
                }
 
3957
 
 
3958
                filename = Z_STRVAL_PP(arg);
 
3959
 
 
3960
                if ((fp = VCWD_OPEN(filename, O_RDONLY|O_BINARY)) == -1) {
 
3961
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't open file %s", filename);
 
3962
                        RETURN_FALSE;
 
3963
                }
 
3964
 
 
3965
                while ((loblen = read(fp, &buf, sizeof(buf))) > 0) {    
 
3966
                        CALL_OCI_RETURN(connection->error,
 
3967
                                OCILobWrite(
 
3968
                                        connection->pServiceContext, 
 
3969
                                        connection->pError, 
 
3970
                                        mylob, 
 
3971
                                        &loblen, 
 
3972
                                        (ub4) offset, 
 
3973
                                        (dvoid *) &buf, 
 
3974
                                        (ub4) loblen, 
 
3975
                                        OCI_ONE_PIECE, 
 
3976
                                        (dvoid *)0, 
 
3977
                                        (OCICallbackLobWrite) 0, 
 
3978
                                        (ub2) 0, 
 
3979
                                        (ub1) SQLCS_IMPLICIT
 
3980
                                )
 
3981
                        );
 
3982
 
 
3983
                        oci_debug("oci_lob_import: size=%d",loblen);
 
3984
 
 
3985
                        if (connection->error) {
 
3986
                                oci_error(connection->pError, "OCILobWrite", connection->error);
 
3987
                                oci_handle_error(connection, connection->error);
 
3988
                                close(fp);
 
3989
                                RETURN_FALSE;
 
3990
                        }
 
3991
 
 
3992
                        offset += loblen;
 
3993
                }
 
3994
                close(fp);
 
3995
 
 
3996
                RETURN_TRUE;
 
3997
        }
 
3998
 
 
3999
  RETURN_FALSE;
 
4000
}
 
4001
/* }}} */
 
4002
 
 
4003
/* {{{ proto string oci_lob_load()
 
4004
   Loads a large object */
 
4005
PHP_FUNCTION(oci_lob_load)
 
4006
{
 
4007
        zval *id;
 
4008
        oci_descriptor *descr;
 
4009
        char *buffer;
 
4010
        int inx;
 
4011
        ub4 loblen;
 
4012
 
 
4013
        if ((id = getThis()) != 0) {
 
4014
                if ((inx = _oci_get_ocidesc(id,&descr TSRMLS_CC)) == 0) {
 
4015
                        RETURN_FALSE;
 
4016
                }
 
4017
                
 
4018
                if (!oci_loadlob(descr->conn,descr,&buffer,&loblen)) {
 
4019
                        RETURN_STRINGL(buffer,loblen,0);
 
4020
                } else {
 
4021
                        RETURN_FALSE;
 
4022
                }
 
4023
        }
 
4024
 
 
4025
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "oci_lob_load() should not be called like this. Use $somelob->load() to load a LOB");
 
4026
 
 
4027
        RETURN_FALSE;
 
4028
}
 
4029
/* }}} */
 
4030
 
 
4031
/* {{{ proto string oci_lob_read( int length )
 
4032
   Reads particular part of a large object */
 
4033
PHP_FUNCTION(oci_lob_read)
 
4034
{
 
4035
        zval *id;
 
4036
        zval **len;
 
4037
        oci_descriptor *descr;
 
4038
        char *buffer;
 
4039
        int inx;
 
4040
        ub4 loblen;
 
4041
 
 
4042
        if ((id = getThis()) != 0) {
 
4043
                if ((inx = _oci_get_ocidesc(id,&descr TSRMLS_CC)) == 0) {
 
4044
                        RETURN_FALSE;
 
4045
                }
 
4046
                
 
4047
                if (zend_get_parameters_ex(1, &len) == FAILURE) {
 
4048
                        WRONG_PARAM_COUNT;
 
4049
                }
 
4050
 
 
4051
                loblen = Z_LVAL_PP(len);
 
4052
                if (oci_readlob(descr->conn,descr,&buffer,&loblen) == 0) {
 
4053
                        RETURN_STRINGL(buffer,loblen,0);
 
4054
                } else {
 
4055
                        RETURN_FALSE;
 
4056
                }
 
4057
        }
 
4058
 
 
4059
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "oci_lob_read() should not be called like this. Use $somelob->read($len) to read a LOB");
 
4060
        RETURN_FALSE;
 
4061
}
 
4062
/* }}} */
 
4063
 
 
4064
/* {{{ proto bool oci_lob_eof()
 
4065
   Checks if EOF is reached */
 
4066
PHP_FUNCTION(oci_lob_eof)
 
4067
{
 
4068
        zval *id;
 
4069
        oci_descriptor *descr;
 
4070
        int inx;
 
4071
        int len;
 
4072
 
 
4073
        if ((id = getThis()) != 0) {
 
4074
                if ((inx = _oci_get_ocidesc(id,&descr TSRMLS_CC)) == 0) {
 
4075
                        RETURN_FALSE;
 
4076
                }
 
4077
                
 
4078
                if (oci_lobgetlen(descr->conn,descr,&len) == 0 && descr->lob_size >= 0) {
 
4079
                        if (descr->lob_size == descr->lob_current_position) {
 
4080
                                RETURN_TRUE;
 
4081
                        } else {
 
4082
                                RETURN_FALSE;
 
4083
                        }
 
4084
                }
 
4085
                RETURN_FALSE;
 
4086
        }
 
4087
 
 
4088
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "oci_lob_eof() should not be called like this. Use $somelob->eof() to check if end of LOB is reached");
 
4089
        RETURN_FALSE;
 
4090
}
 
4091
/* }}} */
 
4092
 
 
4093
/* {{{ proto int oci_lob_tell()
 
4094
   Tells LOB pointer position */
 
4095
PHP_FUNCTION(oci_lob_tell)
 
4096
{
 
4097
        zval *id;
 
4098
        oci_descriptor *descr;
 
4099
        int inx;
 
4100
 
 
4101
        if ((id = getThis()) != 0) {
 
4102
                if ((inx = _oci_get_ocidesc(id,&descr TSRMLS_CC)) == 0) {
 
4103
                        RETURN_FALSE;
 
4104
                }
 
4105
                RETURN_LONG(descr->lob_current_position);       
 
4106
        }
 
4107
 
 
4108
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "oci_lob_tell() should not be called like this. Use $somelob->tell() to get current position of LOB pointer");
 
4109
        RETURN_FALSE;
 
4110
}
 
4111
/* }}} */
 
4112
 
 
4113
/* {{{ proto bool oci_lob_rewind()
 
4114
   Rewind pointer of a LOB */
 
4115
PHP_FUNCTION(oci_lob_rewind)
 
4116
{
 
4117
        zval *id;
 
4118
        oci_descriptor *descr;
 
4119
        int inx;
 
4120
 
 
4121
        if ((id = getThis()) != 0) {
 
4122
                if ((inx = _oci_get_ocidesc(id,&descr TSRMLS_CC)) == 0) {
 
4123
                        RETURN_FALSE;
 
4124
                }
 
4125
                descr->lob_current_position = 0;
 
4126
                RETURN_TRUE;
 
4127
        }
 
4128
 
 
4129
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "oci_lob_rewind() should not be called like this. Use $somelob->rewind() to set current position of LOB pointer to beginning");
 
4130
        RETURN_FALSE;
 
4131
}
 
4132
/* }}} */
 
4133
 
 
4134
/* {{{ proto bool oci_lob_seek( int offset [, int whence ])
 
4135
   Moves the pointer of a LOB */
 
4136
PHP_FUNCTION(oci_lob_seek)
 
4137
{
 
4138
        zval *id;
 
4139
        zval **arg1, **arg2;
 
4140
        int argcount = ZEND_NUM_ARGS(), whence = OCI_SEEK_SET;
 
4141
        oci_descriptor *descr;
 
4142
        int inx, len;
 
4143
 
 
4144
        if ((id = getThis()) != 0) {
 
4145
                if ((inx = _oci_get_ocidesc(id,&descr TSRMLS_CC)) == 0) {
 
4146
                        RETURN_FALSE;
 
4147
                }
 
4148
 
 
4149
                if (argcount < 1 || argcount > 2 || zend_get_parameters_ex(argcount, &arg1, &arg2) == FAILURE) {
 
4150
                        WRONG_PARAM_COUNT;
 
4151
                }
 
4152
                
 
4153
                convert_to_long_ex(arg1);
 
4154
                
 
4155
                if (oci_lobgetlen(descr->conn,descr,&len) == 0 && descr->lob_size >= 0) {
 
4156
                        if (argcount > 1) {
 
4157
                                convert_to_long_ex(arg2);
 
4158
                                whence = Z_LVAL_PP(arg2);
 
4159
                                switch (whence) {
 
4160
                                        case OCI_SEEK_CUR:
 
4161
                                                descr->lob_current_position += Z_LVAL_PP(arg1);
 
4162
                                                break;
 
4163
                                        
 
4164
                                        case OCI_SEEK_END:
 
4165
                                                if (descr->lob_size + Z_LVAL_PP(arg1) >= 0) {
 
4166
                                                        descr->lob_current_position = descr->lob_size + Z_LVAL_PP(arg1);
 
4167
                                                } else {
 
4168
                                                        descr->lob_current_position = 0;
 
4169
                                                }
 
4170
                                                break;
 
4171
                                        
 
4172
                                        case OCI_SEEK_SET:
 
4173
                                        default:
 
4174
                                                descr->lob_current_position = Z_LVAL_PP(arg1);
 
4175
                                                break;
 
4176
                                }
 
4177
                        } else {
 
4178
                                /* OCI_SEEK_SET by default */
 
4179
                                descr->lob_current_position = Z_LVAL_PP(arg1);
 
4180
                        }
 
4181
                        RETURN_TRUE;
 
4182
                } else {
 
4183
                        RETURN_FALSE;
 
4184
                }
 
4185
        }
 
4186
 
 
4187
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "oci_lob_seek() should not be called like this. Use $somelob->seek($offset) to move pointer");
 
4188
        RETURN_FALSE;
 
4189
}
 
4190
/* }}} */
 
4191
 
 
4192
/* {{{ proto int oci_lob_size()
 
4193
   Returns size of a large object */
 
4194
PHP_FUNCTION(oci_lob_size)
 
4195
{
 
4196
        zval *id;
 
4197
        oci_descriptor *descr;
 
4198
        int inx;
 
4199
        ub4 loblen;
 
4200
 
 
4201
        if ((id = getThis()) != 0) {
 
4202
                if ((inx = _oci_get_ocidesc(id,&descr TSRMLS_CC)) == 0) {
 
4203
                        RETURN_FALSE;
 
4204
                }
 
4205
 
 
4206
                if (!oci_lobgetlen(descr->conn,descr,&loblen)) {
 
4207
                        RETURN_LONG(loblen);
 
4208
                } else {
 
4209
                        RETURN_FALSE;
 
4210
                }
 
4211
        }
 
4212
 
 
4213
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "oci_lob_size() should not be called like this. Use $somelob->size() to get size of a LOB");
 
4214
        RETURN_FALSE;
 
4215
}
 
4216
/* }}} */
 
4217
 
 
4218
/* {{{ proto int oci_lob_write( string string [, int length ])
 
4219
   Writes data to current position of a LOB */
 
4220
PHP_FUNCTION(oci_lob_write)
 
4221
{
 
4222
        zval *id, **data,**length;
 
4223
        OCILobLocator *mylob;
 
4224
        oci_connection *connection;
 
4225
        oci_descriptor *descr;
 
4226
        int write_length,inx;
 
4227
        ub4 loblen;
 
4228
        ub4 curloblen;
 
4229
 
 
4230
        if ((id = getThis()) != 0) {
 
4231
                if ((inx = _oci_get_ocidesc(id,&descr TSRMLS_CC)) == 0) {
 
4232
                        RETURN_FALSE;
 
4233
                }
 
4234
                
 
4235
                IS_LOB_INTERNAL(descr);
 
4236
                        
 
4237
                mylob = (OCILobLocator *) descr->ocidescr;
 
4238
 
 
4239
                if (!mylob) {
 
4240
                        RETURN_FALSE;
 
4241
                }
 
4242
 
 
4243
                connection = descr->conn;
 
4244
 
 
4245
                if (oci_lobgetlen(descr->conn,descr,&curloblen) != 0) {
 
4246
                        RETURN_FALSE;
 
4247
                }
 
4248
                
 
4249
                if (zend_get_parameters_ex(2, &data, &length) == SUCCESS) {
 
4250
                        convert_to_string_ex(data);
 
4251
                        convert_to_long_ex(length);
 
4252
                        write_length = Z_LVAL_PP(length);
 
4253
                } else if (zend_get_parameters_ex(1, &data) == SUCCESS) {
 
4254
                        convert_to_string_ex(data);     
 
4255
                        write_length = Z_STRLEN_PP(data);
 
4256
                } else {
 
4257
                        WRONG_PARAM_COUNT;
 
4258
                }
 
4259
 
 
4260
                if (write_length < 1) {
 
4261
                        RETURN_LONG(0);
 
4262
                }
 
4263
                
 
4264
                loblen = write_length;
 
4265
                
 
4266
                CALL_OCI_RETURN(connection->error,
 
4267
                        OCILobWrite(
 
4268
                                connection->pServiceContext, 
 
4269
                                connection->pError,
 
4270
                                mylob,
 
4271
                                &loblen,
 
4272
                                (ub4) descr->lob_current_position+1,
 
4273
                                (dvoid *) Z_STRVAL_PP(data),
 
4274
                                (ub4) loblen,
 
4275
                                OCI_ONE_PIECE,
 
4276
                                (dvoid *)0,
 
4277
                                (OCICallbackLobWrite) 0,
 
4278
                                (ub2) 0,
 
4279
                                (ub1) SQLCS_IMPLICIT
 
4280
                        )
 
4281
                );
 
4282
 
 
4283
                oci_debug("oci_lob_write: size=%d offset=%d",loblen,descr->lob_current_position);
 
4284
 
 
4285
                if (connection->error) {
 
4286
                        oci_error(connection->pError, "OCILobWrite", connection->error);
 
4287
                        oci_handle_error(connection, connection->error);
 
4288
                        RETURN_FALSE;
 
4289
                }
 
4290
 
 
4291
                descr->lob_current_position += loblen;
 
4292
        
 
4293
                if (descr->lob_current_position > descr->lob_size) {
 
4294
                        descr->lob_size = descr->lob_current_position;
 
4295
                }
 
4296
                
 
4297
                /* marking buffer as used */
 
4298
                if (descr->buffering == 1) {
 
4299
                        descr->buffering = 2;
 
4300
                }
 
4301
                RETURN_LONG(loblen);
 
4302
        }
 
4303
        
 
4304
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "oci_lob_write() should not be called like this. Use $somelob->write($data,$len) to write to a LOB");
 
4305
        RETURN_FALSE;
 
4306
}
 
4307
/* }}} */
 
4308
 
 
4309
/* {{{ proto bool oci_lob_append( object lob )
 
4310
   Appends data from a LOB to another LOB */
 
4311
PHP_FUNCTION(oci_lob_append)
 
4312
{
 
4313
        zval *id, **arg;
 
4314
        OCILobLocator *mylob,*my_fromlob;
 
4315
        oci_connection *connection;
 
4316
        oci_descriptor *descr,*from_descr;
 
4317
        int inx;
 
4318
        ub4 curloblen,from_curloblen;
 
4319
 
 
4320
        if ((id = getThis()) != 0) {
 
4321
                if ((inx = _oci_get_ocidesc(id,&descr TSRMLS_CC)) == 0) {
 
4322
                        RETURN_FALSE;
 
4323
                }
 
4324
        
 
4325
                IS_LOB_INTERNAL(descr);
 
4326
                
 
4327
                mylob = (OCILobLocator *) descr->ocidescr;
 
4328
 
 
4329
                if (!mylob) {
 
4330
                        RETURN_FALSE;
 
4331
                }
 
4332
 
 
4333
                connection = descr->conn;
 
4334
 
 
4335
                if (oci_lobgetlen(descr->conn,descr,&curloblen) != 0) {
 
4336
                        RETURN_FALSE;
 
4337
                }
 
4338
                
 
4339
                if (zend_get_parameters_ex(1, &arg) == SUCCESS) {
 
4340
                        convert_to_object_ex(arg);
 
4341
                        if ((inx = _oci_get_ocidesc(*arg,&from_descr TSRMLS_CC)) == 0) {
 
4342
                                RETURN_FALSE;
 
4343
                        }
 
4344
                        
 
4345
                        my_fromlob = (OCILobLocator *) from_descr->ocidescr;
 
4346
 
 
4347
                        if (!my_fromlob) {
 
4348
                                RETURN_FALSE;
 
4349
                        }
 
4350
                        
 
4351
                        if (oci_lobgetlen(from_descr->conn,from_descr,&from_curloblen) != 0) {
 
4352
                                RETURN_FALSE;
 
4353
                        }
 
4354
                } else {
 
4355
                        WRONG_PARAM_COUNT;
 
4356
                }
 
4357
 
 
4358
                if (from_descr->lob_size == 0) {
 
4359
                        RETURN_LONG(0);
 
4360
                }
 
4361
 
 
4362
                CALL_OCI_RETURN(connection->error,
 
4363
                        OCILobAppend(
 
4364
                                connection->pServiceContext, 
 
4365
                                connection->pError,
 
4366
                                mylob,
 
4367
                                my_fromlob
 
4368
                        )
 
4369
                );
 
4370
 
 
4371
                if (connection->error) {
 
4372
                        oci_error(connection->pError, "OCILobAppend", connection->error);
 
4373
                        oci_handle_error(connection, connection->error);
 
4374
                        RETURN_FALSE;
 
4375
                }
 
4376
 
 
4377
                RETURN_TRUE;
 
4378
        }
 
4379
        
 
4380
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "oci_lob_append() should not be called like this. Use $somelob->append($LOB_from) to append data from a LOB to another LOB");
 
4381
        RETURN_FALSE;
 
4382
}
 
4383
/* }}} */
 
4384
 
 
4385
/* {{{ proto bool oci_lob_truncate( [ int length ])
 
4386
   Truncates a LOB */
 
4387
PHP_FUNCTION(oci_lob_truncate)
 
4388
{
 
4389
        zval *id, **length;
 
4390
        OCILobLocator *mylob;
 
4391
        oci_connection *connection;
 
4392
        oci_descriptor *descr;
 
4393
        int inx;
 
4394
        ub4 trim_length;
 
4395
        ub4 curloblen;
 
4396
 
 
4397
        if ((id = getThis()) != 0) {
 
4398
                if ((inx = _oci_get_ocidesc(id,&descr TSRMLS_CC)) == 0) {
 
4399
                        RETURN_FALSE;
 
4400
                }
 
4401
                
 
4402
                IS_LOB_INTERNAL(descr);
 
4403
 
 
4404
                mylob = (OCILobLocator *) descr->ocidescr;
 
4405
 
 
4406
                if (!mylob) {
 
4407
                        RETURN_FALSE;
 
4408
                }
 
4409
 
 
4410
                connection = descr->conn;
 
4411
 
 
4412
                if (oci_lobgetlen(descr->conn,descr,&curloblen) != 0) {
 
4413
                        RETURN_FALSE;
 
4414
                }
 
4415
                
 
4416
                if (zend_get_parameters_ex(1, &length) == SUCCESS) {
 
4417
                        convert_to_long_ex(length);     
 
4418
                        trim_length = Z_LVAL_PP(length);
 
4419
                } else {
 
4420
                        trim_length = 0;
 
4421
                }
 
4422
 
 
4423
                if (trim_length < 0) {
 
4424
                        /* negative length is not allowed */
 
4425
                        RETURN_FALSE;
 
4426
                }
 
4427
                
 
4428
                CALL_OCI_RETURN(connection->error,
 
4429
                        OCILobTrim(
 
4430
                                connection->pServiceContext, 
 
4431
                                connection->pError,
 
4432
                                mylob,
 
4433
                                trim_length
 
4434
                        )
 
4435
                );
 
4436
 
 
4437
                oci_debug("oci_lob_truncate: trim_length=%d",trim_length);
 
4438
 
 
4439
                if (connection->error) {
 
4440
                        oci_error(connection->pError, "OCILobTrim", connection->error);
 
4441
                        oci_handle_error(connection, connection->error);
 
4442
                        RETURN_FALSE;
 
4443
                }
 
4444
 
 
4445
                descr->lob_size = trim_length;
 
4446
                RETURN_TRUE;
 
4447
        }
 
4448
        
 
4449
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "oci_lob_truncate() should not be called like this. Use $somelob->truncate($length) to truncate a LOB to a specified length");
 
4450
        RETURN_FALSE;
 
4451
}
 
4452
/* }}} */
 
4453
 
 
4454
/* {{{ proto int oci_lob_erase( [ int offset [, int length ] ] )
 
4455
   Erases a specified portion of the internal LOB, starting at a specified offset */
 
4456
PHP_FUNCTION(oci_lob_erase)
 
4457
{
 
4458
        zval *id, **length, **offset;
 
4459
        OCILobLocator *mylob;
 
4460
        oci_connection *connection;
 
4461
        oci_descriptor *descr;
 
4462
        int inx;
 
4463
        ub4 erase_length, erase_offset;
 
4464
        ub4 curloblen;
 
4465
 
 
4466
        if ((id = getThis()) != 0) {
 
4467
                if ((inx = _oci_get_ocidesc(id,&descr TSRMLS_CC)) == 0) {
 
4468
                        RETURN_FALSE;
 
4469
                }
 
4470
                
 
4471
                IS_LOB_INTERNAL(descr);
 
4472
 
 
4473
                mylob = (OCILobLocator *) descr->ocidescr;
 
4474
 
 
4475
                if (!mylob) {
 
4476
                        RETURN_FALSE;
 
4477
                }
 
4478
 
 
4479
                connection = descr->conn;
 
4480
 
 
4481
                if (oci_lobgetlen(descr->conn,descr,&curloblen) != 0) {
 
4482
                        RETURN_FALSE;
 
4483
                }
 
4484
                
 
4485
                if (zend_get_parameters_ex(2, &offset, &length) == SUCCESS) {
 
4486
                        convert_to_long_ex(offset);
 
4487
                        convert_to_long_ex(length);     
 
4488
                        
 
4489
                        erase_offset = Z_LVAL_PP(offset);
 
4490
                        erase_length = Z_LVAL_PP(length);
 
4491
                } else if (zend_get_parameters_ex(1, &offset) == SUCCESS) {
 
4492
                        convert_to_long_ex(offset);
 
4493
 
 
4494
                        erase_offset = Z_LVAL_PP(offset);
 
4495
                        erase_length = descr->lob_size - erase_offset;
 
4496
                } else {
 
4497
                        erase_offset = 0;
 
4498
                        erase_length = descr->lob_size;
 
4499
                }
 
4500
 
 
4501
                if (erase_length < 1) {
 
4502
                        RETURN_LONG(0);
 
4503
                }
 
4504
 
 
4505
                if (erase_offset > descr->lob_size) {
 
4506
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "oci_lob_erase(): offset is greater than LOB's length");
 
4507
                }
 
4508
                
 
4509
                CALL_OCI_RETURN(connection->error,
 
4510
                        OCILobErase(
 
4511
                                connection->pServiceContext, 
 
4512
                                connection->pError,
 
4513
                                mylob,
 
4514
                                &erase_length,
 
4515
                                erase_offset+1
 
4516
                        )
 
4517
                );
 
4518
 
 
4519
                oci_debug("oci_lob_erase: erase_length=%d, erase_offset=%d",erase_length,erase_offset);
 
4520
 
 
4521
                if (connection->error) {
 
4522
                        oci_error(connection->pError, "OCILobErase", connection->error);
 
4523
                        oci_handle_error(connection, connection->error);
 
4524
                        RETURN_FALSE;
 
4525
                }
 
4526
 
 
4527
                RETURN_LONG(erase_length);
 
4528
        }
 
4529
        
 
4530
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "oci_lob_erase() should not be called like this. Use $somelob->erase($offset, $length) to erase specified part of LOB");
 
4531
        RETURN_FALSE;
 
4532
}
 
4533
/* }}} */
 
4534
 
 
4535
/* {{{ proto bool oci_lob_flush( [ int flag ] )
 
4536
   Flushes the LOB buffer */
 
4537
PHP_FUNCTION(oci_lob_flush)
 
4538
{
 
4539
        zval *id, **flag;
 
4540
        OCILobLocator *mylob;
 
4541
        oci_connection *connection;
 
4542
        oci_descriptor *descr;
 
4543
        int inx, flush_flag;
 
4544
 
 
4545
        if ((id = getThis()) != 0) {
 
4546
                if ((inx = _oci_get_ocidesc(id,&descr TSRMLS_CC)) == 0) {
 
4547
                        RETURN_FALSE;
 
4548
                }
 
4549
                
 
4550
                IS_LOB_INTERNAL(descr);
 
4551
                        
 
4552
                mylob = (OCILobLocator *) descr->ocidescr;
 
4553
 
 
4554
                if (!mylob) {
 
4555
                        RETURN_FALSE;
 
4556
                }
 
4557
 
 
4558
                connection = descr->conn;
 
4559
 
 
4560
                if (zend_get_parameters_ex(1, &flag) == SUCCESS) {
 
4561
                        convert_to_long_ex(flag);       
 
4562
                        flush_flag = Z_LVAL_PP(flag);
 
4563
                } else {
 
4564
                        flush_flag = 0;
 
4565
                }
 
4566
 
 
4567
                if (descr->buffering == 0) {
 
4568
                        /* buffering wasn't enabled, there is nothing to flush */
 
4569
                        RETURN_FALSE;
 
4570
                }
 
4571
 
 
4572
                if (oci_lob_flush(descr,flush_flag TSRMLS_CC) == 1) {
 
4573
                        RETURN_TRUE;
 
4574
                }
 
4575
                RETURN_FALSE;
 
4576
        }
 
4577
        
 
4578
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "oci_lob_flush() should not be called like this. Use $somelob->flush() to flush LOB buffer");
 
4579
        RETURN_FALSE;
 
4580
}
 
4581
/* }}} */
 
4582
 
 
4583
/* {{{ proto bool ocisetbufferinglob( boolean flag )
 
4584
   Enables/disables buffering for a LOB */
 
4585
PHP_FUNCTION(ocisetbufferinglob)
 
4586
{
 
4587
        zval *id, **flag;
 
4588
        OCILobLocator *mylob;
 
4589
        oci_connection *connection;
 
4590
        oci_descriptor *descr;
 
4591
        int inx, buffering_flag;
 
4592
        ub4 curloblen;
 
4593
 
 
4594
        if ((id = getThis()) != 0) {
 
4595
                if ((inx = _oci_get_ocidesc(id,&descr TSRMLS_CC)) == 0) {
 
4596
                        RETURN_FALSE;
 
4597
                }
 
4598
                
 
4599
                IS_LOB_INTERNAL(descr);
 
4600
                        
 
4601
                mylob = (OCILobLocator *) descr->ocidescr;
 
4602
 
 
4603
                if (!mylob) {
 
4604
                        RETURN_FALSE;
 
4605
                }
 
4606
 
 
4607
                connection = descr->conn;
 
4608
 
 
4609
                if (oci_lobgetlen(descr->conn,descr,&curloblen) != 0) {
 
4610
                        RETURN_FALSE;
 
4611
                }
 
4612
                
 
4613
                if (zend_get_parameters_ex(1, &flag) == SUCCESS) {
 
4614
                        convert_to_boolean_ex(flag);
 
4615
                        buffering_flag = Z_LVAL_PP(flag);
 
4616
                } else {
 
4617
                        WRONG_PARAM_COUNT;
 
4618
                }
 
4619
 
 
4620
                /* we'll return true if function was called twice with the same parameter */
 
4621
                if (buffering_flag == 0 && descr->buffering == 0) {
 
4622
                        RETURN_TRUE;
 
4623
                } else if (buffering_flag == 1 && descr->buffering > 0) {
 
4624
                        RETURN_TRUE;
 
4625
                }
 
4626
                
 
4627
                switch (buffering_flag) {
 
4628
                        case 0:
 
4629
                                CALL_OCI_RETURN(connection->error,
 
4630
                                        OCILobDisableBuffering(
 
4631
                                                connection->pServiceContext, 
 
4632
                                                connection->pError,
 
4633
                                                mylob
 
4634
                                        )
 
4635
                                );
 
4636
                                break;
 
4637
                        case 1:
 
4638
                                CALL_OCI_RETURN(connection->error, 
 
4639
                                        OCILobEnableBuffering(
 
4640
                                                connection->pServiceContext,
 
4641
                                                connection->pError,
 
4642
                                                mylob
 
4643
                                        )
 
4644
                                );
 
4645
                        break;
 
4646
                }
 
4647
                
 
4648
                oci_debug("oci_lob_set_buffering: buffering_flag=%d",buffering_flag);
 
4649
 
 
4650
                if (connection->error) {
 
4651
                        oci_error(connection->pError, "OCILobFlushBuffer", connection->error);
 
4652
                        oci_handle_error(connection, connection->error);
 
4653
                        RETURN_FALSE;
 
4654
                }
 
4655
 
 
4656
                descr->buffering = buffering_flag;
 
4657
                RETURN_TRUE;
 
4658
        }
 
4659
        
 
4660
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "OCISetBufferingLob() should not be called like this. Use $somelob->setBuffering($flag) to set buffering on/off for a LOB");
 
4661
        RETURN_FALSE;
 
4662
}
 
4663
/* }}} */
 
4664
 
 
4665
/* {{{ proto bool ocigetbufferinglob()
 
4666
   Returns current state of buffering for a LOB */
 
4667
PHP_FUNCTION(ocigetbufferinglob)
 
4668
{
 
4669
        zval *id;
 
4670
        OCILobLocator *mylob;
 
4671
        oci_descriptor *descr;
 
4672
        int inx;
 
4673
 
 
4674
        if ((id = getThis()) != 0) {
 
4675
                if ((inx = _oci_get_ocidesc(id,&descr TSRMLS_CC)) == 0) {
 
4676
                        RETURN_FALSE;
 
4677
                }
 
4678
                
 
4679
                IS_LOB_INTERNAL(descr);
 
4680
                        
 
4681
                mylob = (OCILobLocator *) descr->ocidescr;
 
4682
 
 
4683
                if (!mylob) {
 
4684
                        RETURN_FALSE;
 
4685
                }
 
4686
 
 
4687
                switch (descr->buffering) {
 
4688
                        case 1:
 
4689
                        case 2:
 
4690
                                RETURN_TRUE;
 
4691
                                break;
 
4692
 
 
4693
                        case 0:
 
4694
                        default:
 
4695
                                RETURN_FALSE;
 
4696
                                break;
 
4697
                }
 
4698
        }
 
4699
        
 
4700
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "OCIGetBufferingLob() should not be called like this. Use $somelob->getBuffering() to get current state of buffering for a LOB");
 
4701
        RETURN_FALSE;
 
4702
}
 
4703
/* }}} */
 
4704
 
 
4705
/* {{{ proto bool oci_lob_copy( object lob_to, object lob_from [, int length ] )
 
4706
   Copies data from a LOB to another LOB */
 
4707
PHP_FUNCTION(oci_lob_copy)
 
4708
{
 
4709
        zval **arg1, **arg2, **arg3;
 
4710
        OCILobLocator *mylob,*my_fromlob;
 
4711
        oci_connection *connection;
 
4712
        oci_descriptor *descr,*from_descr;
 
4713
        int inx, ac = ZEND_NUM_ARGS();
 
4714
        ub4 curloblen,from_curloblen, copylen;
 
4715
 
 
4716
                if (ac < 2 || ac > 3 || zend_get_parameters_ex(ac, &arg1, &arg2, &arg3) == FAILURE) {
 
4717
                        WRONG_PARAM_COUNT;
 
4718
                }
 
4719
 
 
4720
                convert_to_object_ex(arg1);
 
4721
                convert_to_object_ex(arg2);
 
4722
                
 
4723
                if ((inx = _oci_get_ocidesc(*arg1,&descr TSRMLS_CC)) == 0 || (inx = _oci_get_ocidesc(*arg2,&from_descr TSRMLS_CC)) == 0) {
 
4724
                        RETURN_FALSE;
 
4725
                }
 
4726
 
 
4727
                IS_LOB_INTERNAL(descr);
 
4728
                IS_LOB_INTERNAL(from_descr);
 
4729
 
 
4730
                mylob = (OCILobLocator *) descr->ocidescr;
 
4731
                my_fromlob = (OCILobLocator *) from_descr->ocidescr;
 
4732
                        
 
4733
                if (!mylob || !my_fromlob) {
 
4734
                        RETURN_FALSE;
 
4735
                }
 
4736
                        
 
4737
                if (oci_lobgetlen(descr->conn,descr,&curloblen) != 0 || oci_lobgetlen(from_descr->conn,from_descr,&from_curloblen) != 0) {
 
4738
                        RETURN_FALSE;
 
4739
                }
 
4740
 
 
4741
                if (ac == 3) {
 
4742
                        convert_to_long_ex(arg3);
 
4743
                        copylen = Z_LVAL_PP(arg3);
 
4744
                } else {
 
4745
                        copylen = from_descr->lob_size - from_descr->lob_current_position;
 
4746
                }
 
4747
 
 
4748
                if ((int)copylen <= 0) {
 
4749
                        RETURN_FALSE;
 
4750
                }
 
4751
 
 
4752
                connection = descr->conn;
 
4753
 
 
4754
                CALL_OCI_RETURN(connection->error,
 
4755
                        OCILobCopy(
 
4756
                                connection->pServiceContext, 
 
4757
                                connection->pError,
 
4758
                                mylob,
 
4759
                                my_fromlob,
 
4760
                                copylen,
 
4761
                                descr->lob_current_position+1,
 
4762
                                from_descr->lob_current_position+1
 
4763
                        )
 
4764
                );
 
4765
 
 
4766
                if (connection->error) {
 
4767
                        oci_error(connection->pError, "OCILobCopy", connection->error);
 
4768
                        oci_handle_error(connection, connection->error);
 
4769
                        RETURN_FALSE;
 
4770
                }
 
4771
 
 
4772
                RETURN_TRUE;
 
4773
}
 
4774
/* }}} */
 
4775
 
 
4776
/* {{{ proto bool oci_lob_is_equal( object lob1, object lob2 )
 
4777
   Tests to see if two LOB/FILE locators are equal */
 
4778
PHP_FUNCTION(oci_lob_is_equal)
 
4779
{
 
4780
        zval **arg1, **arg2;
 
4781
        OCILobLocator *first_lob,*second_lob;
 
4782
        oci_connection *connection;
 
4783
        oci_descriptor *first_descr,*second_descr;
 
4784
        int inx;
 
4785
        boolean is_equal;
 
4786
 
 
4787
                if (zend_get_parameters_ex(2, &arg1, &arg2) == FAILURE) {
 
4788
                        WRONG_PARAM_COUNT;
 
4789
                }
 
4790
 
 
4791
                convert_to_object_ex(arg1);
 
4792
                convert_to_object_ex(arg2);
 
4793
                
 
4794
                if ((inx = _oci_get_ocidesc(*arg1,&first_descr TSRMLS_CC)) == 0 || (inx = _oci_get_ocidesc(*arg2,&second_descr TSRMLS_CC)) == 0) {
 
4795
                        RETURN_FALSE;
 
4796
                }
 
4797
                
 
4798
                first_lob = (OCILobLocator *) first_descr->ocidescr;
 
4799
                second_lob = (OCILobLocator *) second_descr->ocidescr;
 
4800
                        
 
4801
                if (!first_lob || !second_lob) {
 
4802
                        RETURN_FALSE;
 
4803
                }
 
4804
                        
 
4805
                connection = first_descr->conn;
 
4806
 
 
4807
                CALL_OCI_RETURN(connection->error,
 
4808
                        OCILobIsEqual(
 
4809
                                connection->session->pEnv, 
 
4810
                                first_lob,
 
4811
                                second_lob,
 
4812
                                &is_equal
 
4813
                        )
 
4814
                );
 
4815
 
 
4816
                if (connection->error) {
 
4817
                        oci_error(connection->pError, "OCILobIsEqual", connection->error);
 
4818
                        oci_handle_error(connection, connection->error);
 
4819
                        RETURN_FALSE;
 
4820
                }
 
4821
 
 
4822
                if (is_equal == TRUE) {
 
4823
                        RETURN_TRUE;
 
4824
                } else {
 
4825
                        RETURN_FALSE;
 
4826
                }
 
4827
}
 
4828
/* }}} */
 
4829
 
 
4830
/* {{{ proto bool oci_lob_export([string filename [, int start [, int length]]])
 
4831
   Writes a large object into a file */
 
4832
PHP_FUNCTION(oci_lob_export)
 
4833
{
 
4834
        zval *id, **zfilename, **zstart, **zlength;
 
4835
        char *filename = NULL;
 
4836
        int start = -1;
 
4837
        ub4 length = -1;
 
4838
        oci_connection *connection;
 
4839
        oci_descriptor *descr;
 
4840
        char *buffer=0;
 
4841
        ub4 loblen;
 
4842
        int ac = ZEND_NUM_ARGS();
 
4843
        int fp = -1,inx;
 
4844
        OCILobLocator *mylob;
 
4845
        int coffs;
 
4846
 
 
4847
        if ((id = getThis()) != 0) {
 
4848
                if ((inx = _oci_get_ocidesc(id,&descr TSRMLS_CC)) == 0) {
 
4849
                        RETURN_FALSE;
 
4850
                }
 
4851
 
 
4852
                mylob = (OCILobLocator *) descr->ocidescr;
 
4853
 
 
4854
                if (!mylob) {
 
4855
                        RETURN_FALSE;
 
4856
                }
 
4857
 
 
4858
                connection = descr->conn;
 
4859
 
 
4860
                if (ac < 0 || ac > 3 || zend_get_parameters_ex(ac, &zfilename, &zstart, &zlength) == FAILURE) {
 
4861
                        WRONG_PARAM_COUNT;
 
4862
                }
 
4863
 
 
4864
                switch (ac) {
 
4865
                case 3:
 
4866
                        convert_to_long_ex(zlength);
 
4867
                        length = Z_LVAL_PP(zlength);
 
4868
                case 2:
 
4869
                        convert_to_long_ex(zstart);
 
4870
                        start = Z_LVAL_PP(zstart);
 
4871
                case 1:
 
4872
                        convert_to_string_ex(zfilename);
 
4873
                        filename = Z_STRVAL_PP(zfilename);
 
4874
                }
 
4875
 
 
4876
                if (filename && *filename) {
 
4877
                        if (php_check_open_basedir(filename TSRMLS_CC)) {
 
4878
                                goto bail;
 
4879
                        }
 
4880
 
 
4881
                        if ((fp = VCWD_OPEN_MODE(filename,O_CREAT | O_RDWR | O_BINARY | O_TRUNC, 0600)) == -1) {
 
4882
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't create file %s", filename);
 
4883
                                goto bail;
 
4884
                        } 
 
4885
                }
 
4886
        
 
4887
                CALL_OCI_RETURN(connection->error,
 
4888
                        OCILobGetLength(
 
4889
                                connection->pServiceContext, 
 
4890
                                connection->pError, 
 
4891
                                descr->ocidescr, 
 
4892
                                &loblen
 
4893
                        )
 
4894
                );
 
4895
 
 
4896
                if (connection->error) {
 
4897
                        oci_error(connection->pError, "OCILobGetLength", connection->error);
 
4898
                        oci_handle_error(connection, connection->error);
 
4899
                        goto bail;
 
4900
                }
 
4901
                
 
4902
                if (descr->type == OCI_DTYPE_FILE) {
 
4903
                        CALL_OCI_RETURN(connection->error,
 
4904
                                OCILobFileOpen(
 
4905
                                        connection->pServiceContext, 
 
4906
                                        connection->pError, 
 
4907
                                        descr->ocidescr, 
 
4908
                                        OCI_FILE_READONLY
 
4909
                                )
 
4910
                        );
 
4911
 
 
4912
                        if (connection->error) {
 
4913
                                oci_error(connection->pError, "OCILobFileOpen",connection->error);
 
4914
                                oci_handle_error(connection, connection->error);
 
4915
                                goto bail;
 
4916
                        }
 
4917
                }
 
4918
 
 
4919
                if (start == -1) {
 
4920
                        start = 0;
 
4921
                }
 
4922
 
 
4923
                if (length == -1) {
 
4924
                        length = loblen - start;
 
4925
                }
 
4926
                
 
4927
                if ((start + length) > loblen) {
 
4928
                        length = loblen - start;
 
4929
                }
 
4930
 
 
4931
#define OCI_LOB_READ_BUFFER 128*1024
 
4932
 
 
4933
                buffer = emalloc(OCI_LOB_READ_BUFFER);
 
4934
 
 
4935
                coffs = start;
 
4936
 
 
4937
                oci_debug("oci_lob_export(start = %d, length = %d, loblen = %d",start,length,loblen);
 
4938
 
 
4939
                while (length > 0) {
 
4940
                        ub4 toread;
 
4941
 
 
4942
                        if (length > OCI_LOB_READ_BUFFER) {
 
4943
                                toread = OCI_LOB_READ_BUFFER;
 
4944
                        } else {
 
4945
                                toread = length;
 
4946
                        }
 
4947
 
 
4948
                        oci_debug("oci_lob_read(coffs = %d, toread = %d",coffs,toread);
 
4949
 
 
4950
                        CALL_OCI_RETURN(connection->error,
 
4951
                                OCILobRead(
 
4952
                                        connection->pServiceContext, 
 
4953
                                        connection->pError,
 
4954
                                        descr->ocidescr,
 
4955
                                        &toread,                /* IN/OUT bytes toread/read */
 
4956
                                        coffs+1,                /* offset (starts with 1) */ 
 
4957
                                        (dvoid *) buffer,
 
4958
                                        toread,                 /* size of buffer */
 
4959
                                        (dvoid *)0,
 
4960
                                        (OCICallbackLobRead) 0, /* callback... */
 
4961
                                        (ub2) 0,                /* The character set ID of the buffer data. */
 
4962
                                        (ub1) SQLCS_IMPLICIT    /* The character set form of the buffer data. */
 
4963
                                )
 
4964
                        );
 
4965
 
 
4966
                        oci_debug("oci_lob_read(read - %d",toread);
 
4967
 
 
4968
                        if (connection->error) {
 
4969
                                oci_error(connection->pError, "OCILobRead", connection->error);
 
4970
                                oci_handle_error(connection, connection->error);
 
4971
                                goto bail;
 
4972
                        }
 
4973
 
 
4974
                        if (fp != -1) {
 
4975
                                if ((ub4) write(fp,buffer,toread) != toread) {
 
4976
                                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot write file!");
 
4977
                                        goto bail;
 
4978
                                }
 
4979
                        } else {
 
4980
                                PHPWRITE(buffer,toread);
 
4981
                        }
 
4982
 
 
4983
                        length -= toread;
 
4984
                        coffs += toread;
 
4985
                }
 
4986
 
 
4987
                efree(buffer);
 
4988
                buffer = 0;
 
4989
                
 
4990
                if (fp != -1) {
 
4991
                        close(fp);
 
4992
                        fp = 0;
 
4993
                }
 
4994
 
 
4995
                if (descr->type == OCI_DTYPE_FILE) {
 
4996
                        CALL_OCI_RETURN(connection->error,
 
4997
                                OCILobFileClose(
 
4998
                                        connection->pServiceContext, 
 
4999
                                        connection->pError, 
 
5000
                                        descr->ocidescr
 
5001
                                )
 
5002
                        );
 
5003
 
 
5004
                        if (connection->error) {
 
5005
                                oci_error(connection->pError, "OCILobFileClose", connection->error);
 
5006
                                oci_handle_error(connection, connection->error);
 
5007
                                goto bail;
 
5008
                        }
 
5009
                }
 
5010
                RETURN_TRUE;
 
5011
        }
 
5012
        
 
5013
bail:
 
5014
        if (fp != -1) {
 
5015
                close(fp);
 
5016
        }
 
5017
        
 
5018
        if (buffer) {
 
5019
                efree(buffer);
 
5020
        }
 
5021
 
 
5022
        RETURN_FALSE;
 
5023
}
 
5024
/* }}} */
 
5025
 
 
5026
#ifdef HAVE_OCI8_TEMP_LOB
 
5027
/* {{{ proto bool oci_lob_write_temporary(string var [, int lob_type])
 
5028
   Writes temporary blob */
 
5029
PHP_FUNCTION(oci_lob_write_temporary)
 
5030
{
 
5031
        zval *id, *var;
 
5032
        OCILobLocator *mylob;
 
5033
        oci_connection *connection;
 
5034
        oci_descriptor *descr;
 
5035
        ub4 offset = 1;
 
5036
        ub4 loblen;
 
5037
        long lob_type = OCI_TEMP_CLOB;
 
5038
 
 
5039
        oci_debug ("oci_write_temporary_lob");
 
5040
 
 
5041
        if ((id = getThis()) == 0) {
 
5042
                RETURN_FALSE;
 
5043
        }
 
5044
 
 
5045
        if (_oci_get_ocidesc(id,&descr TSRMLS_CC) == 0) {
 
5046
                RETURN_FALSE;
 
5047
        }
 
5048
 
 
5049
        mylob = (OCILobLocator *) descr->ocidescr;
 
5050
 
 
5051
        if (!mylob) {
 
5052
                RETURN_FALSE;
 
5053
        }
 
5054
 
 
5055
        connection = descr->conn;
 
5056
 
 
5057
        if (ZEND_NUM_ARGS() < 1) WRONG_PARAM_COUNT;
 
5058
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &var, &lob_type) == FAILURE) {
 
5059
                RETURN_FALSE;
 
5060
        }
 
5061
 
 
5062
        CALL_OCI_RETURN(connection->error,
 
5063
                OCILobCreateTemporary(
 
5064
                        connection->pServiceContext, 
 
5065
                        connection->pError, 
 
5066
                        mylob, 
 
5067
                        OCI_DEFAULT, 
 
5068
                        OCI_DEFAULT, 
 
5069
                        lob_type, 
 
5070
                        OCI_ATTR_NOCACHE, 
 
5071
                        OCI_DURATION_SESSION
 
5072
                )
 
5073
        );
 
5074
 
 
5075
        if (connection->error) {
 
5076
                oci_error(connection->pError, "OCILobCreateTemporary", connection->error);
 
5077
                oci_handle_error(connection, connection->error);
 
5078
                RETURN_FALSE;
 
5079
        }
 
5080
 
 
5081
        CALL_OCI_RETURN(connection->error,
 
5082
                OCILobOpen(
 
5083
                        connection->pServiceContext, 
 
5084
                        connection->pError, 
 
5085
                        mylob, 
 
5086
                        OCI_LOB_READWRITE
 
5087
                )
 
5088
        );
 
5089
 
 
5090
        if (connection->error) {
 
5091
                oci_error(connection->pError, "OCILobOpen", connection->error);
 
5092
                oci_handle_error(connection, connection->error);
 
5093
                RETURN_FALSE;
 
5094
        }
 
5095
 
 
5096
        convert_to_string_ex(&var);
 
5097
        loblen = Z_STRLEN_P(var);
 
5098
        
 
5099
        if (loblen < 1) {
 
5100
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot save a lob that is less than 1 byte");
 
5101
                RETURN_FALSE;
 
5102
        }
 
5103
 
 
5104
        CALL_OCI_RETURN(connection->error,
 
5105
                OCILobWrite(
 
5106
                        connection->pServiceContext, 
 
5107
                        connection->pError, 
 
5108
                        mylob, 
 
5109
                        (ub4 *) &loblen, 
 
5110
                        (ub4) offset, 
 
5111
                        (dvoid *) Z_STRVAL_P(var), 
 
5112
                        (ub4) loblen, 
 
5113
                        OCI_ONE_PIECE, 
 
5114
                        (dvoid *)0, 
 
5115
                        (sb4 (*)(dvoid *, dvoid *, ub4 *, ub1 *)) 0, 
 
5116
                        (ub2) 0, 
 
5117
                        (ub1) SQLCS_IMPLICIT
 
5118
                )
 
5119
        );
 
5120
 
 
5121
        if (connection->error) {
 
5122
                oci_error(connection->pError, "OCILobWrite", connection->error);
 
5123
                oci_handle_error(connection, connection->error);
 
5124
                RETURN_FALSE;
 
5125
        }
 
5126
 
 
5127
        RETURN_TRUE;
 
5128
}
 
5129
/* }}} */
 
5130
 
 
5131
/* {{{ proto bool oci_lob_close()
 
5132
   Closes lob descriptor */
 
5133
PHP_FUNCTION(oci_lob_close)
 
5134
{
 
5135
        zval *id;
 
5136
        int inx;
 
5137
        OCILobLocator *mylob;
 
5138
        oci_connection *connection;
 
5139
        oci_descriptor *descriptor;
 
5140
        int is_temporary;
 
5141
 
 
5142
        if ((id = getThis()) != 0) {
 
5143
                inx = _oci_get_ocidesc(id,&descriptor TSRMLS_CC);
 
5144
                if (inx) {
 
5145
 
 
5146
                        mylob = (OCILobLocator *) descriptor->ocidescr;
 
5147
 
 
5148
                        if (!mylob) {
 
5149
                                RETURN_FALSE;
 
5150
                        }
 
5151
 
 
5152
                        connection = descriptor->conn;
 
5153
 
 
5154
                        CALL_OCI_RETURN(connection->error,
 
5155
                                OCILobClose(
 
5156
                                        connection->pServiceContext, 
 
5157
                                        connection->pError, 
 
5158
                                        mylob
 
5159
                                )
 
5160
                        );
 
5161
 
 
5162
                        if (connection->error) {
 
5163
                                oci_error(connection->pError, "OCILobClose", connection->error);
 
5164
                                oci_handle_error(connection, connection->error);
 
5165
                                RETURN_FALSE;
 
5166
                        }
 
5167
 
 
5168
                        connection->error = 
 
5169
                                OCILobIsTemporary(connection->session->pEnv,
 
5170
                                                                        connection->pError,
 
5171
                                                                        mylob,
 
5172
                                                                        &is_temporary);
 
5173
                        if (is_temporary) {
 
5174
                                connection->error = 
 
5175
                                        OCILobFreeTemporary(connection->pServiceContext,
 
5176
                                                                                connection->pError,
 
5177
                                                                                mylob);
 
5178
 
 
5179
                                if (connection->error) {
 
5180
                                        oci_error(connection->pError, "OCILobFreeTemporary", 
 
5181
                                                          connection->error);
 
5182
                                        oci_handle_error(connection, connection->error);
 
5183
                                        RETURN_FALSE;
 
5184
                                }
 
5185
                                oci_debug("oci_lob_free_temporary: descr=%d",inx);
 
5186
                        }
 
5187
 
 
5188
                        oci_debug("oci_close_lob: descr=%d",inx);
 
5189
                        RETURN_TRUE;
 
5190
                }
 
5191
        }
 
5192
 
 
5193
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "oci_lob_close() should not be called like this. Use $somelob->close() to close a LOB");
 
5194
 
 
5195
        RETURN_FALSE;
 
5196
}
 
5197
/* }}} */
 
5198
#endif 
 
5199
 
 
5200
/* {{{ proto object oci_new_descriptor(resource connection [, int type])
 
5201
   Initialize a new empty descriptor LOB/FILE (LOB is default) */
 
5202
PHP_FUNCTION(oci_new_descriptor)
 
5203
{
 
5204
        zval **conn, **type;
 
5205
        oci_connection *connection;
 
5206
        oci_descriptor *descr;
 
5207
        int dtype;
 
5208
 
 
5209
        dtype = OCI_DTYPE_LOB;
 
5210
 
 
5211
        if (zend_get_parameters_ex(2, &conn, &type) == SUCCESS) {
 
5212
                convert_to_long_ex(type);
 
5213
                dtype = Z_LVAL_PP(type);
 
5214
        } else if (zend_get_parameters_ex(1, &conn) == FAILURE) {
 
5215
                WRONG_PARAM_COUNT;
 
5216
        }
 
5217
 
 
5218
        OCI_GET_CONN(connection,conn);
 
5219
 
 
5220
        descr = oci_new_desc(dtype,connection);
 
5221
 
 
5222
        if (!descr) {
 
5223
                RETURN_NULL();
 
5224
        }
 
5225
 
 
5226
        object_init_ex(return_value, oci_lob_class_entry_ptr);
 
5227
        add_property_resource(return_value, "descriptor", descr->id);
 
5228
}
 
5229
/* }}} */
 
5230
 
 
5231
/* {{{ proto bool oci_rollback(resource conn)
 
5232
   Rollback the current context */
 
5233
PHP_FUNCTION(oci_rollback)
 
5234
{
 
5235
        zval **conn;
 
5236
        oci_connection *connection;
 
5237
 
 
5238
        if (zend_get_parameters_ex(1, &conn) == FAILURE) {
 
5239
                WRONG_PARAM_COUNT;
 
5240
        }
 
5241
 
 
5242
        OCI_GET_CONN(connection,conn);
 
5243
 
 
5244
        if (connection->descriptors) {
 
5245
                zend_hash_apply(connection->descriptors,(apply_func_t) _oci_desc_flush_hash_dtor TSRMLS_CC);
 
5246
        }
 
5247
 
 
5248
        oci_debug("<OCITransRollback");
 
5249
 
 
5250
        CALL_OCI_RETURN(connection->error,
 
5251
                OCITransRollback(
 
5252
                        connection->pServiceContext, 
 
5253
                        connection->pError, 
 
5254
                        (ub4) 0
 
5255
                )
 
5256
        );
 
5257
 
 
5258
        connection->needs_commit = 0;
 
5259
 
 
5260
        oci_debug(">OCITransRollback");
 
5261
 
 
5262
        if (connection->error) {
 
5263
                oci_error(connection->pError, "OCIRollback", connection->error);
 
5264
                oci_handle_error(connection, connection->error);
 
5265
                RETURN_FALSE;
 
5266
        }
 
5267
        
 
5268
        RETURN_TRUE;
 
5269
}
 
5270
/* }}} */
 
5271
 
 
5272
/* {{{ proto bool oci_commit(resource conn)
 
5273
   Commit the current context */
 
5274
PHP_FUNCTION(oci_commit)
 
5275
{
 
5276
        zval **conn;
 
5277
        oci_connection *connection;
 
5278
 
 
5279
        if (zend_get_parameters_ex(1, &conn) == FAILURE) {
 
5280
                WRONG_PARAM_COUNT;
 
5281
        }
 
5282
 
 
5283
        OCI_GET_CONN(connection,conn);
 
5284
 
 
5285
        if (connection->descriptors) {
 
5286
                zend_hash_apply(connection->descriptors,(apply_func_t) _oci_desc_flush_hash_dtor TSRMLS_CC);
 
5287
        }
 
5288
 
 
5289
        oci_debug("<OCITransCommit");
 
5290
 
 
5291
        CALL_OCI_RETURN(connection->error,
 
5292
                OCITransCommit(
 
5293
                        connection->pServiceContext, 
 
5294
                        connection->pError, 
 
5295
                        (ub4) 0
 
5296
                )
 
5297
        );
 
5298
 
 
5299
        connection->needs_commit = 0;
 
5300
 
 
5301
        oci_debug(">OCITransCommit");
 
5302
 
 
5303
        if (connection->error) {
 
5304
                oci_error(connection->pError, "OCICommit", connection->error);
 
5305
                oci_handle_error(connection, connection->error);
 
5306
                RETURN_FALSE;
 
5307
        }
 
5308
        
 
5309
        RETURN_TRUE;
 
5310
}
 
5311
/* }}} */
 
5312
 
 
5313
/* {{{ proto string oci_field_name(resource stmt, int col)
 
5314
   Tell the name of a column */
 
5315
PHP_FUNCTION(oci_field_name)
 
5316
{
 
5317
        zval **stmt, **col;
 
5318
        oci_statement *statement;
 
5319
        oci_out_column *outcol;
 
5320
 
 
5321
        if (zend_get_parameters_ex(2, &stmt, &col) == FAILURE) {
 
5322
                WRONG_PARAM_COUNT;
 
5323
        }
 
5324
 
 
5325
        OCI_GET_STMT(statement,stmt);
 
5326
 
 
5327
        outcol = oci_get_col(statement, -1, col);
 
5328
        if (outcol == NULL) {
 
5329
                RETURN_FALSE;
 
5330
        }
 
5331
 
 
5332
        RETURN_STRINGL(outcol->name, outcol->name_len, 1);
 
5333
}
 
5334
/* }}} */
 
5335
 
 
5336
/* {{{ proto int oci_field_size(resource stmt, int col)
 
5337
   Tell the maximum data size of a column */
 
5338
PHP_FUNCTION(oci_field_size)
 
5339
{
 
5340
        zval **stmt, **col;
 
5341
        oci_statement *statement;
 
5342
        oci_out_column *outcol;
 
5343
 
 
5344
        if (zend_get_parameters_ex(2, &stmt, &col) == FAILURE) {
 
5345
                WRONG_PARAM_COUNT;
 
5346
        }
 
5347
 
 
5348
        OCI_GET_STMT(statement,stmt);
 
5349
 
 
5350
        outcol = oci_get_col(statement, -1, col);
 
5351
        if (outcol == NULL) {
 
5352
                RETURN_FALSE;
 
5353
        }
 
5354
 
 
5355
        oci_debug("oci_field_size: %16s, retlen = %4d, retlen4 = %d, data_size = %4d, storage_size4 = %4d, indicator %4d, retcode = %4d",
 
5356
                                  outcol->name,outcol->retlen,outcol->retlen4,outcol->data_size,outcol->storage_size4,outcol->indicator,outcol->retcode);
 
5357
                
 
5358
        /* Handle data type of LONG */
 
5359
        if (outcol->data_type == SQLT_LNG){
 
5360
                RETURN_LONG(outcol->storage_size4);
 
5361
        } else {
 
5362
                RETURN_LONG(outcol->data_size);
 
5363
        }
 
5364
}
 
5365
/* }}} */
 
5366
 
 
5367
/* {{{ proto int oci_field_scale(resource stmt, int col)
 
5368
   Tell the scale of a column */
 
5369
PHP_FUNCTION(oci_field_scale)
 
5370
{
 
5371
        zval **stmt, **col;
 
5372
        oci_statement *statement;
 
5373
        oci_out_column *outcol;
 
5374
 
 
5375
        if (zend_get_parameters_ex(2, &stmt, &col) == FAILURE) {
 
5376
                WRONG_PARAM_COUNT;
 
5377
        }
 
5378
 
 
5379
        OCI_GET_STMT(statement,stmt);
 
5380
 
 
5381
        outcol = oci_get_col(statement, -1, col);
 
5382
        if (outcol == NULL) {
 
5383
                RETURN_FALSE;
 
5384
        }
 
5385
        RETURN_LONG(outcol->scale);
 
5386
}
 
5387
/* }}} */
 
5388
 
 
5389
/* {{{ proto int oci_field_precision(resource stmt, int col)
 
5390
   Tell the precision of a column */
 
5391
PHP_FUNCTION(oci_field_precision)
 
5392
{
 
5393
        zval **stmt, **col;
 
5394
        oci_statement *statement;
 
5395
        oci_out_column *outcol;
 
5396
 
 
5397
        if (zend_get_parameters_ex(2, &stmt, &col) == FAILURE) {
 
5398
                WRONG_PARAM_COUNT;
 
5399
        }
 
5400
 
 
5401
        OCI_GET_STMT(statement,stmt);
 
5402
 
 
5403
        outcol = oci_get_col(statement, -1, col);
 
5404
        if (outcol == NULL) {
 
5405
                RETURN_FALSE;
 
5406
        }
 
5407
        RETURN_LONG(outcol->precision);
 
5408
}
 
5409
/* }}} */
 
5410
 
 
5411
/* {{{ proto mixed oci_field_type(resource stmt, int col)
 
5412
   Tell the data type of a column */
 
5413
PHP_FUNCTION(oci_field_type)
 
5414
{
 
5415
        zval **stmt, **col;
 
5416
        oci_statement *statement;
 
5417
        oci_out_column *outcol;
 
5418
        
 
5419
        if (zend_get_parameters_ex(2, &stmt, &col) == FAILURE) {
 
5420
                WRONG_PARAM_COUNT;
 
5421
        }
 
5422
 
 
5423
        OCI_GET_STMT(statement,stmt);
 
5424
 
 
5425
        outcol = oci_get_col(statement, -1, col);
 
5426
        if (outcol == NULL) {
 
5427
                RETURN_FALSE;
 
5428
        }
 
5429
        switch (outcol->data_type) {
 
5430
#ifdef SQLT_TIMESTAMP
 
5431
                case SQLT_TIMESTAMP:
 
5432
                        RETVAL_STRING("TIMESTAMP",1);
 
5433
                        break;
 
5434
#endif
 
5435
#ifdef SQLT_TIMESTAMP_TZ
 
5436
                case SQLT_TIMESTAMP_TZ:
 
5437
                        RETVAL_STRING("TIMESTAMP_TZ",1);
 
5438
                        break;
 
5439
#endif
 
5440
                case SQLT_DAT:
 
5441
                        RETVAL_STRING("DATE",1);
 
5442
                        break;
 
5443
                case SQLT_NUM:
 
5444
                        RETVAL_STRING("NUMBER",1);
 
5445
                        break;
 
5446
                case SQLT_LNG:
 
5447
                        RETVAL_STRING("LONG",1);
 
5448
                        break;
 
5449
                case SQLT_BIN:
 
5450
                        RETVAL_STRING("RAW",1);
 
5451
                        break;
 
5452
                case SQLT_LBI:
 
5453
                        RETVAL_STRING("LONG RAW",1);
 
5454
                        break;
 
5455
                case SQLT_CHR:
 
5456
                        RETVAL_STRING("VARCHAR",1);
 
5457
                        break;
 
5458
                case SQLT_RSET:
 
5459
                        RETVAL_STRING("REFCURSOR",1);
 
5460
                        break;
 
5461
                case SQLT_AFC:
 
5462
                        RETVAL_STRING("CHAR",1);
 
5463
                        break;
 
5464
                case SQLT_BLOB:
 
5465
                        RETVAL_STRING("BLOB",1);
 
5466
                        break;
 
5467
                case SQLT_CLOB:
 
5468
                        RETVAL_STRING("CLOB",1);
 
5469
                        break;
 
5470
                case SQLT_BFILE:
 
5471
                        RETVAL_STRING("BFILE",1);
 
5472
                        break;
 
5473
                case SQLT_RDD:
 
5474
                        RETVAL_STRING("ROWID",1);
 
5475
                        break;
 
5476
                default:
 
5477
                        RETVAL_LONG(outcol->data_type);
 
5478
        }
 
5479
}
 
5480
/* }}} */
 
5481
 
 
5482
/* {{{ proto int oci_field_type_raw(resource stmt, int col)
 
5483
   Tell the raw oracle data type of a column */
 
5484
PHP_FUNCTION(oci_field_type_raw)
 
5485
{
 
5486
        zval **stmt, **col;
 
5487
        oci_statement *statement;
 
5488
        oci_out_column *outcol;
 
5489
        
 
5490
        if (zend_get_parameters_ex(2, &stmt, &col) == FAILURE) {
 
5491
                WRONG_PARAM_COUNT;
 
5492
        }
 
5493
 
 
5494
        OCI_GET_STMT(statement,stmt);
 
5495
 
 
5496
        outcol = oci_get_col(statement, -1, col);
 
5497
        if (outcol == NULL) {
 
5498
                RETURN_FALSE;
 
5499
        }
 
5500
        RETVAL_LONG(outcol->data_type);
 
5501
}
 
5502
/* }}} */
 
5503
 
 
5504
/* {{{ proto bool oci_field_is_null(resource stmt, int col)
 
5505
   Tell whether a column is NULL */
 
5506
PHP_FUNCTION(oci_field_is_null)
 
5507
{
 
5508
        zval **stmt, **col;
 
5509
        oci_statement *statement;
 
5510
        oci_out_column *outcol;
 
5511
 
 
5512
        if (zend_get_parameters_ex(2, &stmt, &col) == FAILURE) {
 
5513
                WRONG_PARAM_COUNT;
 
5514
        }
 
5515
 
 
5516
        OCI_GET_STMT(statement,stmt);
 
5517
 
 
5518
        outcol = oci_get_col(statement, -1, col);
 
5519
        if (outcol == NULL) {
 
5520
                RETURN_FALSE;
 
5521
        }
 
5522
        if (outcol->indicator == -1) {
 
5523
                RETURN_TRUE;
 
5524
        } else {
 
5525
                RETURN_FALSE;
 
5526
        }
 
5527
}
 
5528
/* }}} */
 
5529
 
 
5530
/* {{{ proto void oci_internal_debug(int onoff)
 
5531
   Toggle internal debugging output for the OCI extension */
 
5532
PHP_FUNCTION(oci_internal_debug)
 
5533
{
 
5534
        zval **arg;
 
5535
 
 
5536
        if (zend_get_parameters_ex(1, &arg) == FAILURE) {
 
5537
                WRONG_PARAM_COUNT;
 
5538
        }
 
5539
        convert_to_long_ex(arg);
 
5540
        OCI(debug_mode) = Z_LVAL_PP(arg);
 
5541
}
 
5542
/* }}} */
 
5543
 
 
5544
/* {{{ proto bool oci_execute(resource stmt [, int mode])
 
5545
   Execute a parsed statement */
 
5546
PHP_FUNCTION(oci_execute)
 
5547
{
 
5548
        zval **stmt,**mode;
 
5549
        oci_statement *statement;
 
5550
        ub4 execmode;
 
5551
 
 
5552
        if (zend_get_parameters_ex(2, &stmt, &mode) == SUCCESS) {
 
5553
                convert_to_long_ex(mode);
 
5554
                execmode = Z_LVAL_PP(mode);
 
5555
        } else if (zend_get_parameters_ex(1, &stmt) == SUCCESS) {
 
5556
                execmode = OCI_COMMIT_ON_SUCCESS;
 
5557
        } else {
 
5558
                WRONG_PARAM_COUNT;
 
5559
        }
 
5560
 
 
5561
        OCI_GET_STMT(statement,stmt);
 
5562
 
 
5563
        if (oci_execute(statement, "OCIExecute",execmode)) {
 
5564
                RETURN_TRUE;
 
5565
        } else {
 
5566
                RETURN_FALSE;
 
5567
        }
 
5568
}
 
5569
/* }}} */
 
5570
 
 
5571
/* {{{ proto bool oci_cancel(resource stmt)
 
5572
   Cancel reading from a cursor */
 
5573
PHP_FUNCTION(oci_cancel)
 
5574
{
 
5575
        zval **stmt;
 
5576
        oci_statement *statement;
 
5577
 
 
5578
        if (zend_get_parameters_ex(1, &stmt) == FAILURE) {
 
5579
                WRONG_PARAM_COUNT;
 
5580
        }
 
5581
 
 
5582
        OCI_GET_STMT(statement,stmt);
 
5583
 
 
5584
        if (oci_fetch(statement, 0, "OCICancel" TSRMLS_CC)) {
 
5585
                RETURN_TRUE;
 
5586
        } else {
 
5587
                RETURN_FALSE;
 
5588
        }
 
5589
}
 
5590
/* }}} */
 
5591
 
 
5592
/* {{{ proto bool oci_fetch(resource stmt)
 
5593
   Prepare a new row of data for reading */
 
5594
PHP_FUNCTION(oci_fetch)
 
5595
{
 
5596
        zval **stmt;
 
5597
        oci_statement *statement;
 
5598
        ub4 nrows = 1; /* only one row at a time is supported for now */
 
5599
 
 
5600
        if (zend_get_parameters_ex(1, &stmt) == FAILURE) {
 
5601
                WRONG_PARAM_COUNT;
 
5602
        }
 
5603
 
 
5604
        OCI_GET_STMT(statement,stmt);
 
5605
 
 
5606
        if (oci_fetch(statement, nrows, "OCIFetch" TSRMLS_CC)) {
 
5607
                RETURN_TRUE;
 
5608
        } else {
 
5609
                RETURN_FALSE;
 
5610
        }
 
5611
}
 
5612
/* }}} */
 
5613
 
 
5614
/* {{{ proto int ocifetchinto(resource stmt, array &output [, int mode])
 
5615
   Fetch a row of result data into an array */
 
5616
PHP_FUNCTION(ocifetchinto)
 
5617
{
 
5618
        php_oci_fetch_row(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 3);
 
5619
}
 
5620
/* }}} */
 
5621
 
 
5622
/* {{{ proto int oci_fetch_all(resource stmt, array &output[, int skip[, int maxrows[, int flags]]])
 
5623
   Fetch all rows of result data into an array */
 
5624
PHP_FUNCTION(oci_fetch_all)
 
5625
{
 
5626
        zval **stmt, **array, *element, **zskip, **zmaxrows, **zflags, *tmp;
 
5627
        oci_statement *statement;
 
5628
        oci_out_column **columns;
 
5629
        zval ***outarrs;
 
5630
        ub4 nrows = 1;
 
5631
        int i;
 
5632
        int skip = 0, maxrows = -1;
 
5633
        int flags = 0;
 
5634
        int rows = 0;
 
5635
        int ac = ZEND_NUM_ARGS();
 
5636
 
 
5637
        if (ac < 2 || ac > 5 || zend_get_parameters_ex(ac, &stmt, &array, &zskip, &zmaxrows, &zflags) == FAILURE) {
 
5638
                WRONG_PARAM_COUNT;
 
5639
        }
 
5640
        
 
5641
        switch (ac) {
 
5642
                case 5:
 
5643
                        convert_to_long_ex(zflags);
 
5644
                        flags = Z_LVAL_PP(zflags);
 
5645
                case 4:
 
5646
                        convert_to_long_ex(zmaxrows);
 
5647
                        maxrows = Z_LVAL_PP(zmaxrows);
 
5648
                case 3:
 
5649
                        convert_to_long_ex(zskip);
 
5650
                        skip = Z_LVAL_PP(zskip);
 
5651
        }
 
5652
 
 
5653
        OCI_GET_STMT(statement,stmt);
 
5654
 
 
5655
        zval_dtor(*array);
 
5656
        array_init(*array);
 
5657
 
 
5658
        while (skip--) {
 
5659
                if (!oci_fetch(statement, nrows, "OCIFetchStatement" TSRMLS_CC)) {
 
5660
                        RETURN_LONG(0);
 
5661
                }
 
5662
        }
 
5663
 
 
5664
        if (flags & OCI_FETCHSTATEMENT_BY_ROW) {
 
5665
                columns = safe_emalloc(statement->ncolumns, sizeof(oci_out_column *), 0);
 
5666
 
 
5667
                for (i = 0; i < statement->ncolumns; i++) {
 
5668
                        columns[ i ] = oci_get_col(statement, i + 1, 0);
 
5669
                }
 
5670
 
 
5671
                while (oci_fetch(statement, nrows, "OCIFetchStatement" TSRMLS_CC)) {
 
5672
                        zval *row;
 
5673
 
 
5674
                        MAKE_STD_ZVAL(row);
 
5675
                        array_init(row);
 
5676
 
 
5677
                        for (i = 0; i < statement->ncolumns; i++) {
 
5678
                                MAKE_STD_ZVAL(element);
 
5679
 
 
5680
                                _oci_make_zval(element,statement,columns[ i ], "OCIFetchStatement",OCI_RETURN_LOBS TSRMLS_CC);
 
5681
 
 
5682
                                if (flags & OCI_NUM) {
 
5683
                                        zend_hash_next_index_insert(Z_ARRVAL_P(row), &element, sizeof(zval*), NULL);
 
5684
                                } else { /* default to ASSOC */
 
5685
                                        zend_hash_update(Z_ARRVAL_P(row), 
 
5686
                                                        columns[ i ]->name, columns[ i ]->name_len+1, 
 
5687
                                                        &element, sizeof(zval*), NULL);
 
5688
                                }
 
5689
                        }
 
5690
 
 
5691
                        zend_hash_next_index_insert(Z_ARRVAL_PP(array), &row, sizeof(zval*), NULL);
 
5692
 
 
5693
                        rows++;
 
5694
 
 
5695
                        if ((maxrows != -1) && (rows == maxrows)) {
 
5696
                                oci_fetch(statement, 0, "OCIFetchStatement" TSRMLS_CC);
 
5697
                                break;
 
5698
                        }
 
5699
                }
 
5700
                efree(columns);
 
5701
 
 
5702
        } else { /* default to BY_COLUMN */
 
5703
                columns = safe_emalloc(statement->ncolumns, sizeof(oci_out_column *), 0);
 
5704
                outarrs = safe_emalloc(statement->ncolumns, sizeof(zval*), 0);
 
5705
                
 
5706
                if (flags & OCI_NUM) {
 
5707
                        for (i = 0; i < statement->ncolumns; i++) {
 
5708
                                columns[ i ] = oci_get_col(statement, i + 1, 0);
 
5709
                                
 
5710
                                MAKE_STD_ZVAL(tmp);
 
5711
                                array_init(tmp);
 
5712
                                                
 
5713
                                zend_hash_next_index_insert(Z_ARRVAL_PP(array), 
 
5714
                                                &tmp, sizeof(zval*), (void **) &(outarrs[ i ]));
 
5715
                        }
 
5716
                } else { /* default to ASSOC */
 
5717
                        for (i = 0; i < statement->ncolumns; i++) {
 
5718
                                columns[ i ] = oci_get_col(statement, i + 1, 0);
 
5719
                                
 
5720
                                MAKE_STD_ZVAL(tmp);
 
5721
                                array_init(tmp);
 
5722
                                                
 
5723
                                zend_hash_update(Z_ARRVAL_PP(array), 
 
5724
                                                columns[ i ]->name, columns[ i ]->name_len+1, 
 
5725
                                                (void *) &tmp, sizeof(zval*), (void **) &(outarrs[ i ]));
 
5726
                        }
 
5727
                }
 
5728
 
 
5729
                while (oci_fetch(statement, nrows, "OCIFetchStatement" TSRMLS_CC)) {
 
5730
                        for (i = 0; i < statement->ncolumns; i++) {
 
5731
                                MAKE_STD_ZVAL(element);
 
5732
 
 
5733
                                _oci_make_zval(element,statement,columns[ i ], "OCIFetchStatement",OCI_RETURN_LOBS TSRMLS_CC);
 
5734
 
 
5735
                                zend_hash_index_update((*(outarrs[ i ]))->value.ht, rows, (void *)&element, sizeof(zval*), NULL);
 
5736
                        }
 
5737
 
 
5738
                        rows++;
 
5739
 
 
5740
                        if ((maxrows != -1) && (rows == maxrows)) {
 
5741
                                oci_fetch(statement, 0, "OCIFetchStatement" TSRMLS_CC);
 
5742
                                break;
 
5743
                        }
 
5744
                }
 
5745
                
 
5746
                efree(columns);
 
5747
                efree(outarrs);
 
5748
        } 
 
5749
 
 
5750
        RETURN_LONG(rows);
 
5751
}
 
5752
/* }}} */
 
5753
 
 
5754
/* {{{ proto object oci_fetch_object( resource stmt )
 
5755
   Fetch a result row as an object */
 
5756
PHP_FUNCTION(oci_fetch_object)
 
5757
{
 
5758
        php_oci_fetch_row(INTERNAL_FUNCTION_PARAM_PASSTHRU, OCI_ASSOC, 2);
 
5759
 
 
5760
        if (Z_TYPE_P(return_value) == IS_ARRAY) {
 
5761
                object_and_properties_init(return_value, ZEND_STANDARD_CLASS_DEF_PTR, Z_ARRVAL_P(return_value));
 
5762
        }
 
5763
}
 
5764
/* }}} */
 
5765
 
 
5766
/* {{{ proto array oci_fetch_row( resource stmt )
 
5767
   Fetch a result row as an enumerated array */
 
5768
PHP_FUNCTION(oci_fetch_row)
 
5769
{
 
5770
        php_oci_fetch_row(INTERNAL_FUNCTION_PARAM_PASSTHRU, OCI_NUM, 1);
 
5771
}
 
5772
/* }}} */
 
5773
 
 
5774
/* {{{ proto array oci_fetch_assoc( resource stmt )
 
5775
   Fetch a result row as an associative array */
 
5776
PHP_FUNCTION(oci_fetch_assoc)
 
5777
{
 
5778
        php_oci_fetch_row(INTERNAL_FUNCTION_PARAM_PASSTHRU, OCI_ASSOC, 1);
 
5779
}
 
5780
/* }}} */
 
5781
 
 
5782
/* {{{ proto array oci_fetch_array( resource stmt [, int mode ])
 
5783
   Fetch a result row as an array */
 
5784
PHP_FUNCTION(oci_fetch_array)
 
5785
{
 
5786
        php_oci_fetch_row(INTERNAL_FUNCTION_PARAM_PASSTHRU, OCI_BOTH, 2);
 
5787
}
 
5788
/* }}} */
 
5789
 
 
5790
/* {{{ proto bool oci_free_statement(resource stmt)
 
5791
   Free all resources associated with a statement */
 
5792
PHP_FUNCTION(oci_free_statement)
 
5793
{
 
5794
        zval **stmt;
 
5795
        oci_statement *statement;
 
5796
 
 
5797
        if (zend_get_parameters_ex(1, &stmt) == FAILURE) {
 
5798
                WRONG_PARAM_COUNT;
 
5799
        }
 
5800
 
 
5801
        OCI_GET_STMT(statement,stmt);
 
5802
 
 
5803
        zend_list_delete(statement->id);
 
5804
 
 
5805
        RETURN_TRUE;
 
5806
}
 
5807
/* }}} */
 
5808
 
 
5809
/* {{{ proto bool oci_close(resource conn)
 
5810
   Disconnect from database */
 
5811
PHP_FUNCTION(oci_close)
 
5812
{
 
5813
#if 0
 
5814
        this function does nothing any more. server-connections get automagiclly closed on 
 
5815
        request-end. connection handles will "dissappear" as soon as they are no longer 
 
5816
        referenced. as this module makes heavy use of zends reference-counting mechanism
 
5817
        this is the desired behavior. it has always been a bad idea to close a connection that
 
5818
        has outstanding transactions. this way we have a nice-clean approach.
 
5819
        (thies@thieso.net 20000110)
 
5820
 
 
5821
        oci_connection *connection;
 
5822
        zval **conn;
 
5823
 
 
5824
        if (zend_get_parameters_ex(1, &conn) == FAILURE) {
 
5825
                WRONG_PARAM_COUNT;
 
5826
        }
 
5827
 
 
5828
        OCI_GET_CONN(connection,conn);
 
5829
 
 
5830
        connection->is_open = 0;
 
5831
 
 
5832
        zend_hash_apply(list, (apply_func_t) _stmt_cleanup TSRMLS_CC);
 
5833
 
 
5834
        if (zend_list_delete(connection->id) == SUCCESS) {
 
5835
                RETURN_TRUE;
 
5836
        } else {
 
5837
                RETURN_FALSE;
 
5838
        }
 
5839
#endif
 
5840
}
 
5841
/* }}} */
 
5842
 
 
5843
/* {{{ proto resource oci_new_connect(string user, string pass [, string db])
 
5844
   Connect to an Oracle database and log on. Returns a new session. */
 
5845
PHP_FUNCTION(oci_new_connect)
 
5846
{
 
5847
        oci_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1);
 
5848
}
 
5849
/* }}} */
 
5850
 
 
5851
/* {{{ proto resource oci_connect(string user, string pass [, string db])
 
5852
   Connect to an Oracle database and log on. Returns a new session. */
 
5853
PHP_FUNCTION(oci_connect)
 
5854
{
 
5855
        oci_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1);
 
5856
}
 
5857
/* }}} */
 
5858
 
 
5859
/* {{{ proto resource oci_pconnect(string user, string pass [, string db])
 
5860
   Connect to an Oracle database using a persistent connection and log on. Returns a new session. */
 
5861
PHP_FUNCTION(oci_pconnect)
 
5862
{
 
5863
        oci_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,1,0);
 
5864
}
 
5865
/* }}} */
 
5866
 
 
5867
/* {{{ proto array oci_error([resource stmt|conn|global])
 
5868
   Return the last error of stmt|conn|global. If no error happened returns false. */
 
5869
PHP_FUNCTION(oci_error)
 
5870
{
 
5871
        zval **arg;
 
5872
        oci_statement *statement;
 
5873
        oci_connection *connection;
 
5874
        text errbuf[512];
 
5875
        sb4 errcode = 0;
 
5876
        sword error = 0;
 
5877
        dvoid *errh = NULL;
 
5878
#ifdef HAVE_OCI8_ATTR_STATEMENT
 
5879
        ub2 errorofs = 0;
 
5880
        text *sqltext = NULL;
 
5881
#endif
 
5882
 
 
5883
        if (zend_get_parameters_ex(1, &arg) == SUCCESS) {
 
5884
                statement = (oci_statement *) zend_fetch_resource(arg TSRMLS_CC, -1, NULL, NULL, 1, le_stmt);
 
5885
                if (statement) {
 
5886
                        errh = statement->pError;
 
5887
                        error = statement->error;
 
5888
 
 
5889
#ifdef HAVE_OCI8_ATTR_STATEMENT
 
5890
                        CALL_OCI_RETURN(statement->error,
 
5891
                                OCIAttrGet(
 
5892
                                        (dvoid *)statement->pStmt,
 
5893
                                        OCI_HTYPE_STMT,
 
5894
                                        (text *) &sqltext,
 
5895
                                        (ub4 *)0,
 
5896
                                        OCI_ATTR_STATEMENT,
 
5897
                                        statement->pError
 
5898
                                )
 
5899
                        );
 
5900
 
 
5901
                        CALL_OCI_RETURN(statement->error,
 
5902
                                OCIAttrGet(
 
5903
                                        (dvoid *)statement->pStmt,
 
5904
                                        OCI_HTYPE_STMT,
 
5905
                                        (ub2 *)&errorofs,
 
5906
                                        (ub4 *)0,
 
5907
                                        OCI_ATTR_PARSE_ERROR_OFFSET,
 
5908
                                        statement->pError
 
5909
                                )
 
5910
                        );
 
5911
#endif
 
5912
 
 
5913
                } else {
 
5914
                        connection = (oci_connection *) zend_fetch_resource(arg TSRMLS_CC, -1, NULL, NULL, 1, le_conn);
 
5915
                        if (connection) {
 
5916
                                errh = connection->pError;
 
5917
                                error = connection->error;
 
5918
                        }
 
5919
                }
 
5920
        } else {
 
5921
                errh = OCI(pError);
 
5922
                error = OCI(error);
 
5923
        }
 
5924
 
 
5925
        if (!error) { /* no error set in the handle */
 
5926
                RETURN_FALSE;
 
5927
        }
 
5928
 
 
5929
        if (!errh) {
 
5930
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "OCIError: unable to find Error handle");
 
5931
                RETURN_FALSE;
 
5932
        }
 
5933
 
 
5934
        CALL_OCI(
 
5935
                OCIErrorGet(
 
5936
                        errh,
 
5937
                        1,
 
5938
                        NULL,
 
5939
                        &errcode,
 
5940
                        errbuf,
 
5941
                        (ub4) sizeof(errbuf),
 
5942
                        (ub4) OCI_HTYPE_ERROR
 
5943
                )
 
5944
        );
 
5945
 
 
5946
        if (errcode) {
 
5947
                array_init(return_value);
 
5948
                add_assoc_long(return_value, "code", errcode);
 
5949
                add_assoc_string(return_value, "message", (char*) errbuf, 1);
 
5950
#ifdef HAVE_OCI8_ATTR_STATEMENT
 
5951
                add_assoc_long(return_value, "offset", errorofs);
 
5952
                add_assoc_string(return_value, "sqltext", sqltext ? (char *) sqltext : "", 1);
 
5953
#endif
 
5954
        } else {
 
5955
                RETURN_FALSE;
 
5956
        }
 
5957
}
 
5958
/* }}} */
 
5959
 
 
5960
/* {{{ proto int oci_num_fields(resource stmt)
 
5961
   Return the number of result columns in a statement */
 
5962
PHP_FUNCTION(oci_num_fields)
 
5963
{
 
5964
        zval **stmt;
 
5965
        oci_statement *statement;
 
5966
 
 
5967
        if (zend_get_parameters_ex(1, &stmt) == FAILURE) {
 
5968
                WRONG_PARAM_COUNT;
 
5969
        }
 
5970
 
 
5971
        OCI_GET_STMT(statement,stmt);
 
5972
 
 
5973
        RETURN_LONG(statement->ncolumns);
 
5974
}
 
5975
/* }}} */
 
5976
 
 
5977
/* {{{ proto resource oci_parse(resource conn, string query)
 
5978
   Parse a query and return a statement */
 
5979
PHP_FUNCTION(oci_parse)
 
5980
{
 
5981
        zval **conn, **query;
 
5982
        oci_connection *connection;
 
5983
        oci_statement *statement;
 
5984
 
 
5985
        if (zend_get_parameters_ex(2, &conn, &query) == FAILURE) {
 
5986
                WRONG_PARAM_COUNT;
 
5987
        }
 
5988
 
 
5989
        OCI_GET_CONN(connection,conn);
 
5990
 
 
5991
        convert_to_string_ex(query);
 
5992
 
 
5993
        statement = oci_parse(connection,Z_STRVAL_PP(query),Z_STRLEN_PP(query));
 
5994
 
 
5995
        if (statement) {
 
5996
                RETURN_RESOURCE(statement->id);
 
5997
        } else {
 
5998
                RETURN_FALSE;
 
5999
        }
 
6000
}
 
6001
/* }}} */
 
6002
 
 
6003
/* {{{ proto bool oci_set_prefetch(resource stmt, int prefetch_rows)
 
6004
  Sets the number of rows to be prefetched on execute to prefetch_rows for stmt */
 
6005
PHP_FUNCTION(oci_set_prefetch)
 
6006
{
 
6007
        zval **stmt, **size;
 
6008
        oci_statement *statement;
 
6009
 
 
6010
        if (zend_get_parameters_ex(2, &stmt, &size) == FAILURE) {
 
6011
                WRONG_PARAM_COUNT;
 
6012
        }
 
6013
 
 
6014
        convert_to_long_ex(size);
 
6015
 
 
6016
        OCI_GET_STMT(statement,stmt);
 
6017
 
 
6018
        oci_setprefetch(statement,Z_LVAL_PP(size)); 
 
6019
 
 
6020
        RETURN_TRUE;
 
6021
}
 
6022
/* }}} */
 
6023
 
 
6024
/* {{{ proto bool oci_password_change(resource conn, string username, string old_password, string new_password)
 
6025
  Changes the password of an account */
 
6026
PHP_FUNCTION(oci_password_change)
 
6027
{
 
6028
        zval **conn, **user_param, **pass_old_param, **pass_new_param;
 
6029
        text *user, *pass_old, *pass_new;
 
6030
        oci_connection *connection;
 
6031
 
 
6032
        /*  Disable in Safe Mode  */
 
6033
        if (PG(safe_mode)) {
 
6034
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "is disabled in Safe Mode");
 
6035
                RETURN_FALSE;
 
6036
        }
 
6037
 
 
6038
        if (zend_get_parameters_ex(4, &conn, &user_param, &pass_old_param, &pass_new_param) == FAILURE) {
 
6039
                WRONG_PARAM_COUNT;
 
6040
        }
 
6041
 
 
6042
        convert_to_string_ex(user_param);
 
6043
        convert_to_string_ex(pass_old_param);
 
6044
        convert_to_string_ex(pass_new_param);
 
6045
        
 
6046
        user      = Z_STRVAL_PP(user_param);
 
6047
        pass_old  = Z_STRVAL_PP(pass_old_param);
 
6048
        pass_new  = Z_STRVAL_PP(pass_new_param);
 
6049
 
 
6050
        OCI_GET_CONN(connection, conn);
 
6051
 
 
6052
        CALL_OCI_RETURN(connection->error,
 
6053
                OCIPasswordChange(
 
6054
                        connection->pServiceContext,
 
6055
                        connection->pError,
 
6056
                        user,
 
6057
                        strlen(user)+1,
 
6058
                        pass_old,
 
6059
                        strlen(pass_old)+1,
 
6060
                        pass_new,
 
6061
                        strlen(pass_new)+1,
 
6062
                        OCI_DEFAULT
 
6063
                )
 
6064
        );
 
6065
 
 
6066
        if (connection->error == OCI_SUCCESS) {
 
6067
                RETURN_TRUE;
 
6068
        } else {
 
6069
                oci_error(connection->pError, "OCIPasswordChange", connection->error);
 
6070
                oci_handle_error(connection, connection->error);
 
6071
                RETURN_FALSE;
 
6072
        }
 
6073
}
 
6074
/* }}} */
 
6075
 
 
6076
/* {{{ proto resource oci_new_cursor(resource conn)
 
6077
   Return a new cursor (Statement-Handle) - use this to bind ref-cursors! */
 
6078
PHP_FUNCTION(oci_new_cursor)
 
6079
{
 
6080
        zval **conn;
 
6081
        oci_connection *connection;
 
6082
        oci_statement *statement;
 
6083
 
 
6084
        if (zend_get_parameters_ex(1, &conn) == FAILURE) {
 
6085
                WRONG_PARAM_COUNT;
 
6086
        }
 
6087
 
 
6088
        OCI_GET_CONN(connection,conn);
 
6089
 
 
6090
        statement = oci_parse(connection,0,0);
 
6091
        
 
6092
        RETURN_RESOURCE(statement->id);
 
6093
}
 
6094
/* }}} */
 
6095
 
 
6096
/* {{{ proto string oci_result(resource stmt, mixed column)
 
6097
   Return a single column of result data */
 
6098
PHP_FUNCTION(oci_result)
 
6099
{
 
6100
        zval **stmt, **col;
 
6101
        oci_statement *statement;
 
6102
        oci_out_column *outcol = NULL;
 
6103
 
 
6104
        if (zend_get_parameters_ex(2, &stmt, &col) == FAILURE) {
 
6105
                WRONG_PARAM_COUNT;
 
6106
        }
 
6107
 
 
6108
        OCI_GET_STMT(statement,stmt);
 
6109
 
 
6110
        outcol = oci_get_col(statement, -1, col);
 
6111
 
 
6112
        if (outcol == NULL) {
 
6113
                RETURN_FALSE;
 
6114
        }
 
6115
 
 
6116
        _oci_make_zval(return_value,statement,outcol, "OCIResult",0 TSRMLS_CC);
 
6117
}
 
6118
/* }}} */
 
6119
 
 
6120
/* {{{ proto string oci_server_version(resource conn)
 
6121
   Return a string containing server version information */
 
6122
PHP_FUNCTION(oci_server_version)
 
6123
{
 
6124
        oci_connection *connection;
 
6125
        zval **conn;
 
6126
        char version[256];
 
6127
 
 
6128
        if (zend_get_parameters_ex(1, &conn) == FAILURE) {
 
6129
                WRONG_PARAM_COUNT;
 
6130
        }
 
6131
 
 
6132
        OCI_GET_CONN(connection,conn);
 
6133
 
 
6134
        CALL_OCI_RETURN(connection->error,
 
6135
                OCIServerVersion(
 
6136
                        connection->pServiceContext,
 
6137
                        connection->pError, 
 
6138
                        (text*)version, 
 
6139
                        sizeof(version), 
 
6140
                        OCI_HTYPE_SVCCTX
 
6141
                )
 
6142
        );
 
6143
        
 
6144
        if (connection->error != OCI_SUCCESS) {
 
6145
                oci_error(connection->pError, "OCIServerVersion", connection->error);
 
6146
                oci_handle_error(connection, connection->error);
 
6147
                RETURN_FALSE;
 
6148
        }
 
6149
 
 
6150
        RETURN_STRING(version,1);
 
6151
}
 
6152
/* }}} */
 
6153
 
 
6154
/* {{{ proto string oci_statement_type(resource stmt)
 
6155
   Return the query type of an OCI statement */
 
6156
/* XXX it would be better with a general interface to OCIAttrGet() */
 
6157
PHP_FUNCTION(oci_statement_type)
 
6158
{
 
6159
        zval **stmt;
 
6160
        oci_statement *statement;
 
6161
        ub2 stmttype;
 
6162
 
 
6163
        if (zend_get_parameters_ex(1, &stmt) == FAILURE) {
 
6164
                WRONG_PARAM_COUNT;
 
6165
        }
 
6166
 
 
6167
        OCI_GET_STMT(statement,stmt);
 
6168
 
 
6169
        CALL_OCI_RETURN(statement->error,
 
6170
                OCIAttrGet(
 
6171
                        (dvoid *)statement->pStmt, 
 
6172
                        OCI_HTYPE_STMT, 
 
6173
                        (ub2 *)&stmttype, 
 
6174
                        (ub4 *)0, 
 
6175
                        OCI_ATTR_STMT_TYPE, 
 
6176
                        statement->pError
 
6177
                )
 
6178
        );
 
6179
 
 
6180
        if (statement->error != OCI_SUCCESS) {
 
6181
                oci_error(statement->pError, "OCIStatementType", statement->error);
 
6182
                oci_handle_error(statement->conn, statement->error);
 
6183
                RETURN_FALSE;
 
6184
        }
 
6185
 
 
6186
        switch (stmttype) {
 
6187
                case OCI_STMT_SELECT:
 
6188
                        RETVAL_STRING("SELECT",1);
 
6189
                        break;
 
6190
                case OCI_STMT_UPDATE:
 
6191
                        RETVAL_STRING("UPDATE",1);
 
6192
                        break;
 
6193
                case OCI_STMT_DELETE:
 
6194
                        RETVAL_STRING("DELETE",1);
 
6195
                        break;
 
6196
                case OCI_STMT_INSERT:
 
6197
                        RETVAL_STRING("INSERT",1);
 
6198
                        break;
 
6199
                case OCI_STMT_CREATE:
 
6200
                        RETVAL_STRING("CREATE",1);
 
6201
                        break;
 
6202
                case OCI_STMT_DROP:
 
6203
                        RETVAL_STRING("DROP",1);
 
6204
                        break;
 
6205
                case OCI_STMT_ALTER:
 
6206
                        RETVAL_STRING("ALTER",1);
 
6207
                        break;
 
6208
                case OCI_STMT_BEGIN:
 
6209
                        RETVAL_STRING("BEGIN",1);
 
6210
                        break;
 
6211
                case OCI_STMT_DECLARE:
 
6212
                        RETVAL_STRING("DECLARE",1);
 
6213
                        break;
 
6214
                default:
 
6215
                        RETVAL_STRING("UNKNOWN",1);
 
6216
        }
 
6217
}
 
6218
/* }}} */
 
6219
 
 
6220
/* {{{ proto int oci_num_rows(resource stmt)
 
6221
   Return the row count of an OCI statement */
 
6222
PHP_FUNCTION(oci_num_rows)
 
6223
{
 
6224
        zval **stmt;
 
6225
        oci_statement *statement;
 
6226
        ub4 rowcount;
 
6227
 
 
6228
        if (zend_get_parameters_ex(1, &stmt) == FAILURE) {
 
6229
                WRONG_PARAM_COUNT;
 
6230
        }
 
6231
 
 
6232
        OCI_GET_STMT(statement,stmt);
 
6233
 
 
6234
        CALL_OCI_RETURN(statement->error,
 
6235
                OCIAttrGet(
 
6236
                        (dvoid *)statement->pStmt, 
 
6237
                        OCI_HTYPE_STMT, 
 
6238
                        (ub2 *)&rowcount, 
 
6239
                        (ub4 *)0, 
 
6240
                        OCI_ATTR_ROW_COUNT,
 
6241
                        statement->pError
 
6242
                )
 
6243
        );
 
6244
 
 
6245
        if (statement->error != OCI_SUCCESS) {
 
6246
                oci_error(statement->pError, "OCIRowCount", statement->error);
 
6247
                oci_handle_error(statement->conn, statement->error);
 
6248
                RETURN_FALSE;
 
6249
        }
 
6250
 
 
6251
        RETURN_LONG(rowcount);
 
6252
}
 
6253
/* }}} */
 
6254
 
 
6255
#ifdef PHP_OCI8_HAVE_COLLECTIONS
 
6256
/* {{{ oci_get_coll() */
 
6257
static oci_collection *oci_get_coll(int ind TSRMLS_DC)
 
6258
{
 
6259
        oci_collection *collection;
 
6260
        int actual_resource_type;
 
6261
 
 
6262
        collection = (oci_collection *) zend_list_find(ind, &actual_resource_type);
 
6263
 
 
6264
        if (collection && (actual_resource_type == le_coll)) {
 
6265
                return collection;
 
6266
        } else {
 
6267
                return (oci_collection *) NULL;
 
6268
        }
 
6269
}
 
6270
/* }}} */
 
6271
 
 
6272
/* {{{ proto bool oci_free_collection()
 
6273
   Deletes collection object*/
 
6274
PHP_FUNCTION(oci_free_collection)
 
6275
{
 
6276
        zval *id;
 
6277
        int inx;
 
6278
        oci_collection *coll;
 
6279
        oci_connection *connection;
 
6280
        
 
6281
        if ((id = getThis()) != 0) {
 
6282
                inx = _oci_get_ocicoll(id,&coll TSRMLS_CC);
 
6283
                if (inx) {
 
6284
                        /*
 
6285
                         * Do we need to free the object?
 
6286
                         * 
 
6287
                         */
 
6288
                        connection = coll->conn;
 
6289
                        oci_debug("oci_free_collection: coll=%d",inx);
 
6290
 
 
6291
                        CALL_OCI_RETURN(connection->error,
 
6292
                                OCIObjectFree(
 
6293
                                        connection->session->pEnv, 
 
6294
                                        connection->pError, 
 
6295
                                        (dvoid *)coll->coll, 
 
6296
                                        (ub2)(OCI_OBJECTFREE_FORCE)
 
6297
                                )
 
6298
                        );
 
6299
 
 
6300
                        if (connection->error) {
 
6301
                                oci_error(connection->pError, "OCIObjectFree", connection->error);
 
6302
                                RETURN_FALSE;
 
6303
                        }
 
6304
 
 
6305
                        zend_list_delete(inx);
 
6306
                        RETURN_TRUE;
 
6307
                }
 
6308
        }
 
6309
 
 
6310
        RETURN_FALSE;
 
6311
}
 
6312
/* }}} */
 
6313
 
 
6314
/* {{{ proto bool oci_collection_append(string value)
 
6315
   Append an object to the collection */
 
6316
PHP_FUNCTION(oci_collection_append)
 
6317
{
 
6318
        zval *id, **arg;
 
6319
        oci_connection *connection;
 
6320
        oci_collection *coll;
 
6321
        OCINumber num;
 
6322
        OCIString *ocistr = (OCIString *)0;
 
6323
        OCIInd new_ind = OCI_IND_NOTNULL;
 
6324
        OCIInd null_ind = OCI_IND_NULL;
 
6325
        OCIDate dt;
 
6326
        int inx;
 
6327
        double ndx;
 
6328
 
 
6329
        if ((id = getThis()) != 0) {
 
6330
                if ((inx = _oci_get_ocicoll(id,&coll TSRMLS_CC)) == 0) {
 
6331
                        RETURN_FALSE;
 
6332
                }
 
6333
 
 
6334
                connection = coll->conn;
 
6335
                if (zend_get_parameters_ex(1, &arg) == FAILURE) {
 
6336
                        WRONG_PARAM_COUNT;
 
6337
                }
 
6338
 
 
6339
                /* 
 
6340
                 * Handle NULLS.  For consistency with the rest of the OCI8 library, when
 
6341
                 * a value passed in is a 0 length string, consider it a null
 
6342
                 */
 
6343
                convert_to_string_ex(arg);
 
6344
                if (Z_STRLEN_PP(arg) == 0) {
 
6345
                        CALL_OCI_RETURN(connection->error,
 
6346
                                OCICollAppend(
 
6347
                                        connection->session->pEnv, 
 
6348
                                        connection->pError, 
 
6349
                                        (dvoid *)0, 
 
6350
                                        &null_ind, 
 
6351
                                        coll->coll
 
6352
                                )
 
6353
                        );
 
6354
                        if (connection->error) {
 
6355
                                oci_error(connection->pError, "OCICollAppend - NULL", connection->error);
 
6356
                                RETURN_FALSE;
 
6357
                        }
 
6358
 
 
6359
                        RETURN_TRUE;
 
6360
                }
 
6361
 
 
6362
                switch(coll->element_typecode) {
 
6363
                        case OCI_TYPECODE_DATE:
 
6364
                                convert_to_string_ex(arg);
 
6365
 
 
6366
                                CALL_OCI_RETURN(connection->error,
 
6367
                                        OCIDateFromText(
 
6368
                                                connection->pError, 
 
6369
                                                Z_STRVAL_PP(arg), 
 
6370
                                                Z_STRLEN_PP(arg), 
 
6371
                                                0,
 
6372
                                                0,
 
6373
                                                0,
 
6374
                                                0,
 
6375
                                                &dt
 
6376
                                        )
 
6377
                                );
 
6378
 
 
6379
                                if (connection->error) {
 
6380
                                        oci_error(connection->pError, "OCIDateFromText", connection->error);
 
6381
                                        RETURN_FALSE;
 
6382
                                }
 
6383
 
 
6384
                                CALL_OCI_RETURN(connection->error,
 
6385
                                        OCICollAppend(
 
6386
                                                connection->session->pEnv, 
 
6387
                                                connection->pError, 
 
6388
                                                (dvoid *) &dt, 
 
6389
                                                (dvoid *) &new_ind, 
 
6390
                                                (OCIColl *) coll->coll
 
6391
                                        )
 
6392
                                );
 
6393
 
 
6394
                                if (connection->error) {
 
6395
                                        oci_error(connection->pError, "OCICollAppend", connection->error);
 
6396
                                        RETURN_FALSE;
 
6397
                                }
 
6398
                                RETURN_TRUE;
 
6399
                                break;
 
6400
                        case OCI_TYPECODE_VARCHAR2 :
 
6401
                                convert_to_string_ex(arg);
 
6402
 
 
6403
                                CALL_OCI_RETURN(connection->error,
 
6404
                                        OCIStringAssignText(
 
6405
                                                connection->session->pEnv, 
 
6406
                                                connection->pError, 
 
6407
                                                Z_STRVAL_PP(arg), 
 
6408
                                                Z_STRLEN_PP(arg), 
 
6409
                                                &ocistr
 
6410
                                        )
 
6411
                                );
 
6412
 
 
6413
                                if (connection->error) {
 
6414
                                        oci_error(connection->pError, "OCIStringAssignText", connection->error);
 
6415
                                        RETURN_FALSE;
 
6416
                                }
 
6417
 
 
6418
                                CALL_OCI_RETURN(connection->error,
 
6419
                                        OCICollAppend(
 
6420
                                                connection->session->pEnv, 
 
6421
                                                connection->pError, 
 
6422
                                                (dvoid *) ocistr, 
 
6423
                                                (dvoid *) &new_ind, 
 
6424
                                                (OCIColl *) coll->coll
 
6425
                                        )
 
6426
                                );
 
6427
 
 
6428
                                if (connection->error) {
 
6429
                                        oci_error(connection->pError, "OCICollAppend", connection->error);
 
6430
                                        RETURN_FALSE;
 
6431
                                }
 
6432
                                RETURN_TRUE;
 
6433
                                break;
 
6434
                        case OCI_TYPECODE_UNSIGNED16 :                       /* UNSIGNED SHORT  */
 
6435
                        case OCI_TYPECODE_UNSIGNED32 :                        /* UNSIGNED LONG  */
 
6436
                        case OCI_TYPECODE_REAL :                                     /* REAL    */
 
6437
                        case OCI_TYPECODE_DOUBLE :                                   /* DOUBLE  */
 
6438
                        case OCI_TYPECODE_INTEGER :                                     /* INT  */
 
6439
                        case OCI_TYPECODE_SIGNED16 :                                  /* SHORT  */
 
6440
                        case OCI_TYPECODE_SIGNED32 :                                   /* LONG  */
 
6441
                        case OCI_TYPECODE_DECIMAL :                                 /* DECIMAL  */
 
6442
                        case OCI_TYPECODE_FLOAT :                                   /* FLOAT    */
 
6443
                        case OCI_TYPECODE_NUMBER :                                  /* NUMBER   */
 
6444
                        case OCI_TYPECODE_SMALLINT :                                /* SMALLINT */
 
6445
                                convert_to_double_ex(arg);
 
6446
                                ndx = (double)Z_DVAL_PP(arg);
 
6447
 
 
6448
                                CALL_OCI_RETURN(connection->error,
 
6449
                                        OCINumberFromReal(
 
6450
                                                connection->pError,
 
6451
                                                &ndx, 
 
6452
                                                sizeof(double),
 
6453
                                                &num
 
6454
                                        )
 
6455
                                );
 
6456
 
 
6457
                                if (connection->error) {
 
6458
                                        oci_error(connection->pError, "OCINumberFromReal", connection->error);
 
6459
                                        RETURN_FALSE;
 
6460
                                }
 
6461
 
 
6462
                                CALL_OCI_RETURN(connection->error,
 
6463
                                        OCICollAppend(
 
6464
                                                connection->session->pEnv, 
 
6465
                                                connection->pError, 
 
6466
                                                (dvoid *) &num, 
 
6467
                                                (dvoid *) &new_ind, 
 
6468
                                                (OCIColl *) coll->coll
 
6469
                                        )
 
6470
                                );
 
6471
 
 
6472
                                RETURN_TRUE;
 
6473
                                break;
 
6474
                        default:
 
6475
                                php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unknown or unsupported type of element");
 
6476
                                RETURN_FALSE;
 
6477
                                break;
 
6478
                }
 
6479
        }
 
6480
        
 
6481
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "oci_collection_append() should not be called like this. Use $collection->append($element) to append an element to the collection");
 
6482
        RETURN_FALSE;
 
6483
}
 
6484
/* }}} */
 
6485
 
 
6486
/* {{{ proto string oci_collection_element_get(int ndx)
 
6487
   Retrieve the value at collection index ndx */
 
6488
PHP_FUNCTION(oci_collection_element_get)
 
6489
{
 
6490
        zval *id,**arg;
 
6491
        oci_connection *connection;
 
6492
        oci_collection *coll;
 
6493
        ub4  ndx;
 
6494
        int inx;
 
6495
        dvoid *elem;
 
6496
        OCIInd *elemind;
 
6497
        boolean exists;
 
6498
        OCIString *ocistr = (OCIString *)0;
 
6499
        text *str;
 
6500
        char buff[1024];
 
6501
        int len;
 
6502
        double dnum;
 
6503
 
 
6504
        if ((id = getThis()) != 0) {
 
6505
                if ((inx = _oci_get_ocicoll(id,&coll TSRMLS_CC)) == 0) {
 
6506
                        RETURN_FALSE;
 
6507
                }
 
6508
                if (zend_get_parameters_ex(1, &arg) == FAILURE) {
 
6509
                        WRONG_PARAM_COUNT;
 
6510
                }
 
6511
 
 
6512
                convert_to_long_ex(arg);
 
6513
                ndx = Z_LVAL_PP(arg);
 
6514
 
 
6515
                connection = coll->conn;
 
6516
 
 
6517
                CALL_OCI_RETURN(connection->error,
 
6518
                        OCICollGetElem(
 
6519
                                connection->session->pEnv, 
 
6520
                                connection->pError, 
 
6521
                                coll->coll, 
 
6522
                                ndx, 
 
6523
                                &exists, 
 
6524
                                &elem, 
 
6525
                                (dvoid **)&elemind
 
6526
                        )
 
6527
                );
 
6528
 
 
6529
                if (connection->error) {
 
6530
                        oci_error(connection->pError, "OCICollGetElem", connection->error);
 
6531
                        RETURN_NULL();
 
6532
                }
 
6533
                
 
6534
                /* Return false if value does not exist at that location */
 
6535
                if (exists == 0) {
 
6536
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "OCICollGetElem - Invalid index %d", ndx);
 
6537
                        RETURN_FALSE;
 
6538
                }
 
6539
 
 
6540
                /* Return null if the value is null */
 
6541
                if (*elemind == OCI_IND_NULL) {
 
6542
                        RETURN_NULL();
 
6543
                } 
 
6544
 
 
6545
                switch (coll->element_typecode) {
 
6546
                        case OCI_TYPECODE_DATE:
 
6547
                                len = 1024;
 
6548
                                CALL_OCI(
 
6549
                                        OCIDateToText(
 
6550
                                                connection->pError, 
 
6551
                                                elem, 
 
6552
                                                0, /* fmt */ 
 
6553
                                                0, /* fmt_length */ 
 
6554
                                                0, /* lang_name */ 
 
6555
                                                0, /* lang_length */ 
 
6556
                                                &len,
 
6557
                                                buff
 
6558
                                        )
 
6559
                                );
 
6560
 
 
6561
                                RETURN_STRINGL(buff,len,1);
 
6562
                        case OCI_TYPECODE_VARCHAR2 :
 
6563
                                ocistr = *(OCIString **)elem;
 
6564
                                str = OCIStringPtr(connection->session->pEnv,ocistr); /* XXX not protected against recursion! */
 
6565
                                RETURN_STRINGL(str,strlen(str),1);
 
6566
                                break;
 
6567
                        case OCI_TYPECODE_UNSIGNED16 :                       /* UNSIGNED SHORT  */
 
6568
                        case OCI_TYPECODE_UNSIGNED32 :                       /* UNSIGNED LONG  */
 
6569
                        case OCI_TYPECODE_REAL :                             /* REAL    */
 
6570
                        case OCI_TYPECODE_DOUBLE :                           /* DOUBLE  */
 
6571
                        case OCI_TYPECODE_INTEGER :                          /* INT  */
 
6572
                        case OCI_TYPECODE_SIGNED16 :                         /* SHORT  */
 
6573
                        case OCI_TYPECODE_SIGNED32 :                         /* LONG  */
 
6574
                        case OCI_TYPECODE_DECIMAL :                          /* DECIMAL  */
 
6575
                        case OCI_TYPECODE_FLOAT :                            /* FLOAT    */
 
6576
                        case OCI_TYPECODE_NUMBER :                           /* NUMBER   */
 
6577
                        case OCI_TYPECODE_SMALLINT :                         /* SMALLINT */
 
6578
                                CALL_OCI_RETURN(connection->error,
 
6579
                                        OCINumberToReal(
 
6580
                                                connection->pError, 
 
6581
                                                (CONST OCINumber *) elem, 
 
6582
                                                (uword) sizeof(dnum), 
 
6583
                                                (dvoid *) &dnum
 
6584
                                        )
 
6585
                                );
 
6586
 
 
6587
                                if (connection->error) {
 
6588
                                        oci_error(connection->pError, "OCINumberToReal", connection->error);
 
6589
                                        RETURN_FALSE;
 
6590
                                }
 
6591
                                RETURN_DOUBLE(dnum);
 
6592
                                break;
 
6593
                        default:
 
6594
                                php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unknown or unsupported type of element");
 
6595
                                RETURN_FALSE;
 
6596
                                break;
 
6597
                }
 
6598
        }
 
6599
 
 
6600
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "oci_collection_element_get() should not be called like this. Use $collection->getelem($index) to get an element of the collection with the given index");
 
6601
        RETURN_FALSE;
 
6602
}
 
6603
/* }}} */
 
6604
 
 
6605
/* {{{ proto bool oci_collection_assign(object from)
 
6606
   Assign a collection from another existing collection */
 
6607
PHP_FUNCTION(oci_collection_assign)
 
6608
{
 
6609
        zval *id,**from;
 
6610
        oci_connection *connection;
 
6611
        oci_collection *coll,*from_coll;
 
6612
        int inx;
 
6613
 
 
6614
        if ((id = getThis()) != 0) {
 
6615
                if ((inx = _oci_get_ocicoll(id,&coll TSRMLS_CC)) == 0) {
 
6616
                        RETURN_FALSE;
 
6617
                }
 
6618
 
 
6619
                if (zend_get_parameters_ex(1, &from) == FAILURE) {
 
6620
                        WRONG_PARAM_COUNT;
 
6621
                }
 
6622
 
 
6623
                if ((inx = _oci_get_ocicoll(*from,&from_coll TSRMLS_CC)) == 0) {
 
6624
                        RETURN_FALSE;
 
6625
                }
 
6626
 
 
6627
                connection = coll->conn;
 
6628
        
 
6629
                CALL_OCI_RETURN(connection->error,
 
6630
                        OCICollAssign(
 
6631
                                connection->session->pEnv,
 
6632
                                connection->pError, 
 
6633
                                from_coll->coll,
 
6634
                                coll->coll
 
6635
                        )
 
6636
                );
 
6637
 
 
6638
                if (connection->error) {
 
6639
                        oci_error(connection->pError, "OCICollAssignElem", connection->error);
 
6640
                        RETURN_FALSE;
 
6641
                }
 
6642
                RETURN_TRUE;
 
6643
        }
 
6644
 
 
6645
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "oci_collection_assign() should not be called like this. Use $collection->assign($collection_value) to assign value to the collection");
 
6646
        RETURN_FALSE;
 
6647
}
 
6648
/* }}} */
 
6649
 
 
6650
/* {{{ proto bool oci_collection_element_assign(int index, string val)
 
6651
   Assign element val to collection at index ndx */
 
6652
PHP_FUNCTION(oci_collection_element_assign)
 
6653
{
 
6654
        zval *id,**index,**val;
 
6655
        oci_connection *connection;
 
6656
        oci_collection *coll;
 
6657
        OCINumber num;
 
6658
        OCIInd new_ind = OCI_IND_NOTNULL;
 
6659
        OCIInd null_ind = OCI_IND_NULL;
 
6660
        ub4  ndx;
 
6661
        int inx;
 
6662
        OCIString *ocistr = (OCIString *)0;
 
6663
        OCIDate dt;
 
6664
        double  dnum;
 
6665
 
 
6666
        if ((id = getThis()) != 0) {
 
6667
                if ((inx = _oci_get_ocicoll(id,&coll TSRMLS_CC)) == 0) {
 
6668
                        RETURN_FALSE;
 
6669
                }
 
6670
 
 
6671
                if (zend_get_parameters_ex(2, &index,&val) == FAILURE) {
 
6672
                        WRONG_PARAM_COUNT;
 
6673
                }
 
6674
 
 
6675
                convert_to_long_ex(index);
 
6676
                ndx = Z_LVAL_PP(index);
 
6677
 
 
6678
                connection = coll->conn;
 
6679
 
 
6680
                if (connection->error) {
 
6681
                        oci_error(connection->pError, "OCICollAssignElem", connection->error);
 
6682
                        RETURN_FALSE;
 
6683
                }
 
6684
 
 
6685
                /* 
 
6686
                 * Handle NULLs.  For consistency with the rest of the OCI8 library, when
 
6687
                 * a value passed in is a 0 length string, consider it a null
 
6688
                 */
 
6689
                convert_to_string_ex(val);
 
6690
 
 
6691
                if (Z_STRLEN_PP(val) == 0) {
 
6692
                        CALL_OCI_RETURN(connection->error,
 
6693
                                OCICollAssignElem(
 
6694
                                        connection->session->pEnv, 
 
6695
                                        connection->pError, 
 
6696
                                        ndx, 
 
6697
                                        (dvoid *)0, 
 
6698
                                        &null_ind, 
 
6699
                                        coll->coll
 
6700
                                )
 
6701
                        );
 
6702
                        if (connection->error) {
 
6703
                                oci_error(connection->pError, "OCICollAssignElem - NULL", connection->error);
 
6704
                                RETURN_FALSE;
 
6705
                        }
 
6706
 
 
6707
                        RETURN_TRUE;
 
6708
                }
 
6709
 
 
6710
                switch (coll->element_typecode) {
 
6711
                        case OCI_TYPECODE_DATE:
 
6712
                                convert_to_string_ex(val);
 
6713
                                CALL_OCI_RETURN(connection->error,
 
6714
                                        OCIDateFromText(
 
6715
                                                connection->pError, 
 
6716
                                                Z_STRVAL_PP(val), 
 
6717
                                                Z_STRLEN_PP(val), 
 
6718
                                                0,
 
6719
                                                0,
 
6720
                                                0,
 
6721
                                                0,
 
6722
                                                &dt
 
6723
                                        )
 
6724
                                );
 
6725
 
 
6726
                                if (connection->error) {
 
6727
                                        oci_error(connection->pError, "OCIDateFromText", connection->error);
 
6728
                                        RETURN_FALSE;
 
6729
                                }
 
6730
 
 
6731
                                CALL_OCI_RETURN(connection->error,
 
6732
                                        OCICollAssignElem(
 
6733
                                                connection->session->pEnv, 
 
6734
                                                connection->pError, 
 
6735
                                                ndx, 
 
6736
                                                (dvoid *)&dt, 
 
6737
                                                &new_ind, 
 
6738
                                                coll->coll
 
6739
                                        )
 
6740
                                );
 
6741
 
 
6742
                                if (connection->error) {
 
6743
                                        oci_error(connection->pError, "OCICollAssignElem", connection->error);
 
6744
                                        RETURN_FALSE;
 
6745
                                }
 
6746
                                break;
 
6747
                        case OCI_TYPECODE_VARCHAR2 :
 
6748
                                convert_to_string_ex(val);
 
6749
 
 
6750
                                CALL_OCI_RETURN(connection->error,
 
6751
                                        OCIStringAssignText(
 
6752
                                                connection->session->pEnv, 
 
6753
                                                connection->pError, 
 
6754
                                                Z_STRVAL_PP(val), 
 
6755
                                                Z_STRLEN_PP(val), 
 
6756
                                                &ocistr
 
6757
                                        )
 
6758
                                );
 
6759
 
 
6760
                                if (connection->error) {
 
6761
                                        oci_error(connection->pError, "OCIStringAssignText", connection->error);
 
6762
                                        RETURN_FALSE;
 
6763
                                }
 
6764
 
 
6765
                                CALL_OCI_RETURN(connection->error,
 
6766
                                        OCICollAssignElem(
 
6767
                                                connection->session->pEnv, 
 
6768
                                                connection->pError, 
 
6769
                                                ndx, 
 
6770
                                                (dvoid *)ocistr, 
 
6771
                                                &new_ind, 
 
6772
                                                coll->coll
 
6773
                                        )
 
6774
                                );
 
6775
 
 
6776
                                if (connection->error) {
 
6777
                                        oci_error(connection->pError, "OCICollAssignElem", connection->error);
 
6778
                                        RETURN_FALSE;
 
6779
                                }
 
6780
                                RETURN_TRUE;
 
6781
                                break;
 
6782
                        case OCI_TYPECODE_UNSIGNED16 :                       /* UNSIGNED SHORT  */
 
6783
                        case OCI_TYPECODE_UNSIGNED32 :                       /* UNSIGNED LONG  */
 
6784
                        case OCI_TYPECODE_REAL :                             /* REAL    */
 
6785
                        case OCI_TYPECODE_DOUBLE :                           /* DOUBLE  */
 
6786
                        case OCI_TYPECODE_INTEGER :                          /* INT  */
 
6787
                        case OCI_TYPECODE_SIGNED16 :                         /* SHORT  */
 
6788
                        case OCI_TYPECODE_SIGNED32 :                         /* LONG  */
 
6789
                        case OCI_TYPECODE_DECIMAL :                          /* DECIMAL  */
 
6790
                        case OCI_TYPECODE_FLOAT :                            /* FLOAT    */
 
6791
                        case OCI_TYPECODE_NUMBER :                           /* NUMBER   */
 
6792
                        case OCI_TYPECODE_SMALLINT :                         /* SMALLINT */
 
6793
                                convert_to_double_ex(val);
 
6794
                                dnum = (double)Z_DVAL_PP(val);
 
6795
 
 
6796
                                CALL_OCI_RETURN(connection->error,
 
6797
                                        OCINumberFromReal(
 
6798
                                                connection->pError,
 
6799
                                                &dnum, 
 
6800
                                                sizeof(double),
 
6801
                                                &num
 
6802
                                        )
 
6803
                                );
 
6804
 
 
6805
                                if (connection->error) {
 
6806
                                        oci_error(connection->pError, "OCINumberFromReal", connection->error);
 
6807
                                        RETURN_FALSE;
 
6808
                                }
 
6809
 
 
6810
                                CALL_OCI_RETURN(connection->error,
 
6811
                                        OCICollAssignElem(
 
6812
                                                connection->session->pEnv, 
 
6813
                                                connection->pError, 
 
6814
                                                ndx, 
 
6815
                                                (dvoid *)&num, 
 
6816
                                                &new_ind, 
 
6817
                                                coll->coll
 
6818
                                        )
 
6819
                                );
 
6820
 
 
6821
                                if (connection->error) {
 
6822
                                        oci_error(connection->pError, "OCICollAssignElem", connection->error);
 
6823
                                        RETURN_FALSE;
 
6824
                                }
 
6825
                                RETURN_TRUE;
 
6826
                                break;
 
6827
                }
 
6828
        }
 
6829
 
 
6830
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "oci_collection_element_assign() should not be called like this. Use $collection->assignelem($index, $value) to assign value to an element of the collection");
 
6831
        RETURN_FALSE;
 
6832
}
 
6833
/* }}} */
 
6834
 
 
6835
/* {{{ proto int oci_collection_size()
 
6836
   Return the size of a collection */
 
6837
PHP_FUNCTION(oci_collection_size)
 
6838
{
 
6839
        zval *id;
 
6840
        oci_connection *connection;
 
6841
        oci_collection *coll;
 
6842
        sb4 sz;
 
6843
        int inx;
 
6844
 
 
6845
        if ((id = getThis()) != 0) {
 
6846
                if ((inx = _oci_get_ocicoll(id,&coll TSRMLS_CC)) == 0) {
 
6847
                        RETURN_FALSE;
 
6848
                }
 
6849
                connection = coll->conn;
 
6850
 
 
6851
                CALL_OCI_RETURN(connection->error,
 
6852
                        OCICollSize(
 
6853
                                connection->session->pEnv,
 
6854
                                coll->conn->pError,
 
6855
                                coll->coll,
 
6856
                                &sz
 
6857
                        )
 
6858
                );
 
6859
 
 
6860
                if (connection->error) {
 
6861
                        oci_error(connection->pError, "OCICollSize", connection->error);
 
6862
                        RETURN_FALSE;
 
6863
                }
 
6864
 
 
6865
                RETURN_LONG(sz);
 
6866
        }
 
6867
 
 
6868
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "oci_collection_size() should not be called like this. Use $collection->size() to get size of the collection");
 
6869
        RETURN_FALSE;
 
6870
}
 
6871
/* }}} */
 
6872
 
 
6873
/* {{{ proto int oci_collection_max()
 
6874
   Return the max value of a collection. For a varray this is the maximum length of the array */
 
6875
PHP_FUNCTION(oci_collection_max)
 
6876
{
 
6877
        zval *id;
 
6878
        oci_collection *coll;
 
6879
        sb4 sz;
 
6880
        int inx;
 
6881
 
 
6882
        if ((id = getThis()) != 0) {
 
6883
                if ((inx = _oci_get_ocicoll(id,&coll TSRMLS_CC)) == 0) {
 
6884
                        RETURN_FALSE;
 
6885
                }
 
6886
                sz = OCICollMax(OCI(pEnv),coll->coll); /* XXX not protected against recursion */
 
6887
 
 
6888
                RETURN_LONG(sz);
 
6889
        }
 
6890
 
 
6891
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "oci_collection_max() should not be called like this. Use $collection->max() to get maximum number of elements in the collection");
 
6892
        RETURN_FALSE;
 
6893
}
 
6894
/* }}} */
 
6895
 
 
6896
/* {{{ proto bool oci_collection_trim(int num)
 
6897
   Trim num elements from the end of a collection */
 
6898
PHP_FUNCTION(oci_collection_trim)
 
6899
{
 
6900
        zval *id,**arg;
 
6901
        oci_collection *coll;
 
6902
        int inx;
 
6903
 
 
6904
        if ((id = getThis()) != 0) {
 
6905
                if ((inx = _oci_get_ocicoll(id,&coll TSRMLS_CC)) == 0) {
 
6906
                        RETURN_FALSE;
 
6907
                }
 
6908
                if (zend_get_parameters_ex(1, &arg) == FAILURE) {
 
6909
                        WRONG_PARAM_COUNT;
 
6910
                }
 
6911
                convert_to_long_ex(arg);
 
6912
 
 
6913
                CALL_OCI_RETURN(coll->conn->error,
 
6914
                        OCICollTrim(
 
6915
                                OCI(pEnv),
 
6916
                                coll->conn->pError,
 
6917
                                Z_LVAL_PP(arg),
 
6918
                                coll->coll
 
6919
                        )
 
6920
                );
 
6921
 
 
6922
                if (coll->conn->error) {
 
6923
                        oci_error(coll->conn->pError, "OCICollTrim", coll->conn->error);
 
6924
                        RETURN_FALSE;
 
6925
                }
 
6926
                RETURN_TRUE;
 
6927
        }
 
6928
 
 
6929
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "oci_collection_trim() should not be called like this. Use $collection->trim($length) to trim collection to the given length");
 
6930
        RETURN_FALSE;
 
6931
}
 
6932
/* }}} */
 
6933
 
 
6934
/* {{{ proto object oci_new_collection(resource connection, string tdo [, string schema])
 
6935
   Initialize a new collection */
 
6936
PHP_FUNCTION(oci_new_collection)
 
6937
{
 
6938
        dvoid *dschp1;
 
6939
        dvoid *parmp1;
 
6940
        dvoid *parmp2;
 
6941
        zval **conn, **tdo, **schema;
 
6942
        oci_connection *connection;
 
6943
        oci_collection *coll;
 
6944
        int ac = ZEND_NUM_ARGS();
 
6945
 
 
6946
        if (ac < 2 || ac > 3 || zend_get_parameters_ex(ac, &conn, &tdo, &schema) == FAILURE) {
 
6947
                WRONG_PARAM_COUNT;
 
6948
        }
 
6949
    
 
6950
        convert_to_string_ex(tdo);
 
6951
 
 
6952
        if (ac == 3) {
 
6953
                convert_to_string_ex(schema);
 
6954
        }
 
6955
 
 
6956
        coll = emalloc(sizeof(oci_collection));
 
6957
 
 
6958
        OCI_GET_CONN(connection,conn);
 
6959
 
 
6960
        coll->conn = connection;
 
6961
        coll->id = zend_list_insert(coll,le_coll);
 
6962
        zend_list_addref(connection->id);
 
6963
 
 
6964
        CALL_OCI_RETURN(connection->error,
 
6965
                OCITypeByName(
 
6966
                        connection->session->pEnv, 
 
6967
                        connection->pError, 
 
6968
                        connection->pServiceContext, 
 
6969
                        ac == 3 ? (text *) Z_STRVAL_PP(schema) : (text *) 0, 
 
6970
                        ac == 3 ? (ub4) Z_STRLEN_PP(schema) : (ub4) 0, 
 
6971
                        (text *) Z_STRVAL_PP(tdo), 
 
6972
                        (ub4)    Z_STRLEN_PP(tdo), 
 
6973
                        (CONST text *) 0, 
 
6974
                        (ub4) 0, 
 
6975
                        OCI_DURATION_SESSION, 
 
6976
                        OCI_TYPEGET_ALL, 
 
6977
                        &(coll->tdo)
 
6978
                )
 
6979
        );
 
6980
 
 
6981
        if (connection->error) {
 
6982
                oci_error(connection->pError, "OCITypeByName", connection->error);
 
6983
                RETURN_FALSE;
 
6984
        }
 
6985
 
 
6986
        CALL_OCI_RETURN(connection->error, 
 
6987
                OCIHandleAlloc(
 
6988
                        connection->session->pEnv, 
 
6989
                        (dvoid **) &dschp1, 
 
6990
                        (ub4) OCI_HTYPE_DESCRIBE, 
 
6991
                        (size_t) 0, 
 
6992
                        (dvoid **) 0
 
6993
                )
 
6994
        );
 
6995
 
 
6996
        if (connection->error) {
 
6997
                oci_error(connection->pError, "OCI_HTYPE_DESCRIBE", connection->error);
 
6998
                RETURN_FALSE;
 
6999
        }
 
7000
 
 
7001
        CALL_OCI_RETURN(connection->error,
 
7002
                OCIDescribeAny(
 
7003
                        connection->pServiceContext, 
 
7004
                        connection->pError, 
 
7005
                        (dvoid *) coll->tdo, 
 
7006
                        (ub4) 0, 
 
7007
                        OCI_OTYPE_PTR, 
 
7008
                        (ub1)1, 
 
7009
                        (ub1) OCI_PTYPE_TYPE, 
 
7010
                        dschp1
 
7011
                )
 
7012
        );
 
7013
 
 
7014
        if (connection->error) {
 
7015
                oci_error(connection->pError, "OCI_OTYPE_PTR", connection->error);
 
7016
                RETURN_FALSE;
 
7017
        }
 
7018
 
 
7019
        CALL_OCI_RETURN(connection->error,
 
7020
                OCIAttrGet(
 
7021
                        (dvoid *) dschp1, 
 
7022
                        (ub4) OCI_HTYPE_DESCRIBE, 
 
7023
                        (dvoid *)&parmp1, 
 
7024
                        (ub4 *)0, 
 
7025
                        (ub4)OCI_ATTR_PARAM,
 
7026
                        connection->pError
 
7027
                )
 
7028
        );
 
7029
 
 
7030
        if (connection->error) {
 
7031
                oci_error(connection->pError, "OCI_ATTR_PARAM", connection->error);
 
7032
                RETURN_FALSE;
 
7033
        }
 
7034
 
 
7035
        /* get the collection type code of the attribute */
 
7036
 
 
7037
        CALL_OCI_RETURN(connection->error,
 
7038
                OCIAttrGet(
 
7039
                        (dvoid*) parmp1, 
 
7040
                        (ub4) OCI_DTYPE_PARAM, 
 
7041
                        (dvoid*) &(coll->coll_typecode), 
 
7042
                        (ub4 *) 0, 
 
7043
                        (ub4) OCI_ATTR_COLLECTION_TYPECODE, 
 
7044
                        connection->pError
 
7045
                )
 
7046
        );
 
7047
 
 
7048
        if (connection->error) {
 
7049
                oci_error(connection->pError, "OCI_ATTR_COLLECTION_TYPECODE", connection->error);
 
7050
                RETURN_FALSE;
 
7051
        }
 
7052
 
 
7053
        switch(coll->coll_typecode) {
 
7054
                case OCI_TYPECODE_TABLE:
 
7055
                case OCI_TYPECODE_VARRAY:
 
7056
                        CALL_OCI_RETURN(connection->error,
 
7057
                                OCIAttrGet(
 
7058
                                        (dvoid*) parmp1, 
 
7059
                                        (ub4) OCI_DTYPE_PARAM, 
 
7060
                                        (dvoid*) &parmp2, 
 
7061
                                        (ub4 *) 0, 
 
7062
                                        (ub4) OCI_ATTR_COLLECTION_ELEMENT, 
 
7063
                                        connection->pError
 
7064
                                )
 
7065
                        );
 
7066
 
 
7067
                        if (connection->error) {
 
7068
                                oci_error(connection->pError, "OCI_ATTR_COLLECTION_ELEMENT", connection->error);
 
7069
                                RETURN_FALSE;
 
7070
                        }
 
7071
 
 
7072
                        CALL_OCI_RETURN(connection->error,
 
7073
                                OCIAttrGet(
 
7074
                                        (dvoid*) parmp2, 
 
7075
                                        (ub4) OCI_DTYPE_PARAM, 
 
7076
                                        (dvoid*) &(coll->elem_ref), 
 
7077
                                        (ub4 *) 0, 
 
7078
                                        (ub4) OCI_ATTR_REF_TDO, 
 
7079
                                        connection->pError
 
7080
                                )
 
7081
                        );
 
7082
 
 
7083
                        if (connection->error) {
 
7084
                                oci_error(connection->pError, "OCI_ATTR_REF_TDO", connection->error);
 
7085
                                RETURN_FALSE;
 
7086
                        }
 
7087
 
 
7088
                        CALL_OCI_RETURN(connection->error,
 
7089
                                OCITypeByRef(
 
7090
                                        connection->session->pEnv, 
 
7091
                                        connection->pError, 
 
7092
                                        coll->elem_ref, 
 
7093
                                        OCI_DURATION_SESSION, 
 
7094
                                        OCI_TYPEGET_HEADER, 
 
7095
                                        &(coll->element_type)
 
7096
                                )
 
7097
                        );
 
7098
 
 
7099
                        if (connection->error) {
 
7100
                                oci_error(connection->pError, "OCI_TYPEGET_HEADER", connection->error);
 
7101
                                RETURN_FALSE;
 
7102
                        }
 
7103
 
 
7104
                        CALL_OCI_RETURN(connection->error,
 
7105
                                OCIAttrGet(
 
7106
                                        (dvoid*) parmp2, 
 
7107
                                        (ub4) OCI_DTYPE_PARAM, 
 
7108
                                        (dvoid*) &(coll->element_typecode), 
 
7109
                                        (ub4 *) 0, 
 
7110
                                        (ub4) OCI_ATTR_TYPECODE, 
 
7111
                                        connection->pError
 
7112
                                )
 
7113
                        );
 
7114
 
 
7115
                        if (connection->error) {
 
7116
                                oci_error(connection->pError, "OCI_ATTR_TYPECODE", connection->error);
 
7117
                                RETURN_FALSE;
 
7118
                        }
 
7119
                        break;
 
7120
                default:
 
7121
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "OCINewCollection - Unknown Type %d", coll->coll_typecode);
 
7122
                        break;
 
7123
        }       
 
7124
 
 
7125
        /* Create object to hold return table */
 
7126
        CALL_OCI_RETURN(connection->error,
 
7127
                OCIObjectNew(
 
7128
                        connection->session->pEnv, 
 
7129
                        connection->pError, 
 
7130
                        connection->pServiceContext, 
 
7131
                        OCI_TYPECODE_TABLE, 
 
7132
                        coll->tdo, 
 
7133
                        (dvoid *)0, 
 
7134
                        OCI_DURATION_DEFAULT, 
 
7135
                        TRUE, 
 
7136
                        (dvoid **) &(coll->coll)
 
7137
                )
 
7138
        );
 
7139
 
 
7140
        if (connection->error) {
 
7141
                oci_error(connection->pError, "OCIObjectNew", connection->error);
 
7142
                RETURN_FALSE;
 
7143
        }
 
7144
 
 
7145
        object_init_ex(return_value, oci_coll_class_entry_ptr);
 
7146
        add_property_resource(return_value, "collection",coll->id);
 
7147
}
 
7148
/* }}} */
 
7149
#endif
 
7150
 
 
7151
#endif /* HAVE_OCI8 */
 
7152
 
 
7153
/*
 
7154
 * Local variables:
 
7155
 * tab-width: 4
 
7156
 * c-basic-offset: 4
 
7157
 * End:
 
7158
 * vim600: sw=4 ts=4 fdm=marker
 
7159
 * vim<600: sw=4 ts=4
 
7160
 */