1/*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2014 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 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_01.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   |                                                                      |
18   | Collection support by Andy Sautins <asautins@veripost.net>           |
19   | Temporary LOB support by David Benson <dbenson@mancala.com>          |
20   | ZTS per process OCIPLogon by Harald Radi <harald.radi@nme.at>        |
21   |                                                                      |
22   | Redesigned by: Antony Dovgal <antony@zend.com>                       |
23   |                Andi Gutmans <andi@zend.com>                          |
24   |                Wez Furlong <wez@omniti.com>                          |
25   +----------------------------------------------------------------------+
26*/
27
28/* $Id$ */
29
30
31#ifdef HAVE_CONFIG_H
32#include "config.h"
33#endif
34
35#include "php.h"
36#include "ext/standard/info.h"
37#include "php_ini.h"
38
39#if HAVE_OCI8
40
41#include "php_oci8.h"
42#include "php_oci8_int.h"
43
44/* {{{ php_oci_statement_create()
45 Create statemend handle and allocate necessary resources */
46php_oci_statement *php_oci_statement_create(php_oci_connection *connection, char *query, int query_len)
47{
48    php_oci_statement *statement;
49    sword errstatus;
50
51    connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
52
53    statement = ecalloc(1,sizeof(php_oci_statement));
54
55    if (!query_len) {
56        /* do not allocate stmt handle for refcursors, we'll get it from OCIStmtPrepare2() */
57        PHP_OCI_CALL(OCIHandleAlloc, (connection->env, (dvoid **)&(statement->stmt), OCI_HTYPE_STMT, 0, NULL));
58    }
59
60    PHP_OCI_CALL(OCIHandleAlloc, (connection->env, (dvoid **)&(statement->err), OCI_HTYPE_ERROR, 0, NULL));
61
62    if (query_len > 0) {
63        PHP_OCI_CALL_RETURN(errstatus, OCIStmtPrepare2,
64                (
65                 connection->svc,
66                 &(statement->stmt),
67                 connection->err,
68                 (text *)query,
69                 query_len,
70                 NULL,
71                 0,
72                 OCI_NTV_SYNTAX,
73                 OCI_DEFAULT
74                )
75        );
76#ifdef HAVE_OCI8_DTRACE
77        if (DTRACE_OCI8_SQLTEXT_ENABLED()) {
78            DTRACE_OCI8_SQLTEXT(connection, connection->client_id, statement, query);
79        }
80#endif /* HAVE_OCI8_DTRACE */
81
82        if (errstatus != OCI_SUCCESS) {
83            connection->errcode = php_oci_error(connection->err, errstatus);
84
85            PHP_OCI_CALL(OCIStmtRelease, (statement->stmt, statement->err, NULL, 0, OCI_STRLS_CACHE_DELETE));
86            PHP_OCI_CALL(OCIHandleFree,(statement->err, OCI_HTYPE_ERROR));
87            PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
88            efree(statement);
89            return NULL;
90        }
91    }
92
93    if (query && query_len) {
94        statement->last_query = estrndup(query, query_len);
95        statement->last_query_len = query_len;
96    }
97    else {
98        statement->last_query = NULL;
99        statement->last_query_len = 0;
100    }
101
102    statement->connection = connection;
103    statement->has_data = 0;
104    statement->has_descr = 0;
105    statement->parent_stmtid = 0;
106    statement->impres_child_stmt = NULL;
107    statement->impres_count = 0;
108    statement->impres_flag = PHP_OCI_IMPRES_UNKNOWN;  /* may or may not have Implicit Result Set children */
109    ++GC_REFCOUNT(statement->connection->id);
110
111    if (OCI_G(default_prefetch) >= 0) {
112        php_oci_statement_set_prefetch(statement, (ub4)OCI_G(default_prefetch));
113    } else {
114        php_oci_statement_set_prefetch(statement, (ub4)100); /* semi-arbitrary, "sensible default" */
115    }
116
117    PHP_OCI_REGISTER_RESOURCE(statement, le_statement);
118
119    OCI_G(num_statements)++;
120
121    return statement;
122}
123/* }}} */
124
125/* {{{ php_oci_get_implicit_resultset()
126   Fetch implicit result set statement resource */
127php_oci_statement *php_oci_get_implicit_resultset(php_oci_statement *statement)
128{
129#if (OCI_MAJOR_VERSION < 12)
130    php_error_docref(NULL, E_WARNING, "Implicit results are available in Oracle Database 12c onwards");
131    return NULL;
132#else
133    void *result;
134    ub4   rtype;
135    php_oci_statement *statement2;  /* implicit result set statement handle */
136    sword errstatus;
137
138    PHP_OCI_CALL_RETURN(errstatus, OCIStmtGetNextResult, (statement->stmt, statement->err, &result, &rtype, OCI_DEFAULT));
139    if (errstatus == OCI_NO_DATA) {
140        return NULL;
141    }
142
143    if (rtype != OCI_RESULT_TYPE_SELECT) {
144        /* Only OCI_RESULT_TYPE_SELECT is supported by Oracle DB 12cR1 */
145        php_error_docref(NULL, E_WARNING, "Unexpected implicit result type returned from Oracle Database");
146        return NULL;
147    } else {
148        statement2 = ecalloc(1,sizeof(php_oci_statement));
149
150        PHP_OCI_CALL(OCIHandleAlloc, (statement->connection->env, (dvoid **)&(statement2->err), OCI_HTYPE_ERROR, 0, NULL));
151        statement2->stmt = (OCIStmt *)result;
152        statement2->parent_stmtid = statement->id;
153        statement2->impres_child_stmt = NULL;
154        statement2->impres_count = 0;
155        statement2->impres_flag = PHP_OCI_IMPRES_IS_CHILD;
156        statement2->connection = statement->connection;
157        statement2->errcode = 0;
158        statement2->last_query = NULL;
159        statement2->last_query_len = 0;
160        statement2->columns = NULL;
161        statement2->binds = NULL;
162        statement2->defines = NULL;
163        statement2->ncolumns = 0;
164        statement2->executed = 0;
165        statement2->has_data = 0;
166        statement2->has_descr = 0;
167        statement2->stmttype = 0;
168
169        Z_ADDREF_P(statement->id);
170        Z_ADDREF_P(statement2->connection->id);
171
172        php_oci_statement_set_prefetch(statement2, statement->prefetch_count);
173
174        PHP_OCI_REGISTER_RESOURCE(statement2, le_statement);
175
176        OCI_G(num_statements)++;
177
178        return statement2;
179    }
180#endif /* OCI_MAJOR_VERSION < 12 */
181}
182/* }}} */
183
184/* {{{ php_oci_statement_set_prefetch()
185 Set prefetch buffer size for the statement */
186int php_oci_statement_set_prefetch(php_oci_statement *statement, ub4 prefetch )
187{
188    sword errstatus;
189
190    if (prefetch > 20000) {
191        prefetch = 20000;       /* keep it somewhat sane */
192    }
193
194    PHP_OCI_CALL_RETURN(errstatus, OCIAttrSet, (statement->stmt, OCI_HTYPE_STMT, &prefetch, 0, OCI_ATTR_PREFETCH_ROWS, statement->err));
195
196    if (errstatus != OCI_SUCCESS) {
197        statement->errcode = php_oci_error(statement->err, errstatus);
198        PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
199        statement->prefetch_count = 0;
200        return 1;
201    }
202    statement->prefetch_count = prefetch;
203    statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
204    return 0;
205}
206/* }}} */
207
208/* {{{ php_oci_cleanup_pre_fetch()
209   Helper function to cleanup ref-cursors and descriptors from the previous row */
210int php_oci_cleanup_pre_fetch(void *data)
211{
212    php_oci_out_column *outcol = data;
213
214    if (!outcol->is_descr && !outcol->is_cursor)
215        return ZEND_HASH_APPLY_KEEP;
216
217    switch(outcol->data_type) {
218        case SQLT_CLOB:
219        case SQLT_BLOB:
220        case SQLT_RDD:
221        case SQLT_BFILE:
222            if (outcol->descid) {
223                zend_list_delete(outcol->descid);
224                outcol->descid = 0;
225            }
226            break;
227        case SQLT_RSET:
228            if (outcol->stmtid) {
229                zend_list_delete(outcol->stmtid);
230                outcol->stmtid = 0;
231                outcol->nested_statement = NULL;
232            }
233            break;
234        default:
235            break;
236    }
237    return ZEND_HASH_APPLY_KEEP;
238
239}
240/* }}} */
241
242/* {{{ php_oci_statement_fetch()
243 Fetch a row from the statement */
244int php_oci_statement_fetch(php_oci_statement *statement, ub4 nrows)
245{
246    int i;
247    void *handlepp;
248    ub4 typep, iterp, idxp;
249    ub1 in_outp, piecep;
250    zend_bool piecewisecols = 0;
251    php_oci_out_column *column;
252    sword errstatus;
253
254    statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
255
256    if (statement->has_descr && statement->columns) {
257        zend_hash_apply(statement->columns, (apply_func_t) php_oci_cleanup_pre_fetch);
258    }
259
260    PHP_OCI_CALL_RETURN(errstatus, OCIStmtFetch, (statement->stmt, statement->err, nrows, OCI_FETCH_NEXT, OCI_DEFAULT));
261
262    if (errstatus == OCI_NO_DATA || nrows == 0) {
263        if (statement->last_query == NULL) {
264            /* reset define-list for refcursors */
265            if (statement->columns) {
266                zend_hash_destroy(statement->columns);
267                efree(statement->columns);
268                statement->columns = NULL;
269                statement->ncolumns = 0;
270            }
271            statement->executed = 0;
272        }
273
274        statement->has_data = 0;
275
276        if (nrows == 0) {
277            /* this is exactly what we requested */
278            return 0;
279        }
280        return 1;
281    }
282
283    /* reset length for all piecewise columns */
284    for (i = 0; i < statement->ncolumns; i++) {
285        column = php_oci_statement_get_column(statement, i + 1, NULL, 0);
286        if (column && column->piecewise) {
287            column->retlen4 = 0;
288            piecewisecols = 1;
289        }
290    }
291
292    while (errstatus == OCI_NEED_DATA) {
293        if (piecewisecols) {
294            PHP_OCI_CALL_RETURN(errstatus,
295                OCIStmtGetPieceInfo,
296                   (
297                    statement->stmt,
298                    statement->err,
299                    &handlepp,
300                    &typep,
301                    &in_outp,
302                    &iterp,
303                    &idxp,
304                    &piecep
305                   )
306            );
307
308            /* scan through our columns for a piecewise column with a matching handle */
309            for (i = 0; i < statement->ncolumns; i++) {
310                column = php_oci_statement_get_column(statement, i + 1, NULL, 0);
311                if (column && column->piecewise && handlepp == column->oci_define)   {
312                    if (!column->data) {
313                        column->data = (text *) ecalloc(1, PHP_OCI_PIECE_SIZE + 1);
314                    } else {
315                        column->data = erealloc(column->data, column->retlen4 + PHP_OCI_PIECE_SIZE + 1);
316                    }
317                    column->cb_retlen = PHP_OCI_PIECE_SIZE;
318
319                    /* and instruct fetch to fetch waiting piece into our buffer */
320                    PHP_OCI_CALL(OCIStmtSetPieceInfo,
321                           (
322                            (void *) column->oci_define,
323                            OCI_HTYPE_DEFINE,
324                            statement->err,
325                            ((char*)column->data) + column->retlen4,
326                            &(column->cb_retlen),
327                            piecep,
328                            &column->indicator,
329                            &column->retcode
330                           )
331                    );
332                }
333            }
334        }
335
336        PHP_OCI_CALL_RETURN(errstatus, OCIStmtFetch, (statement->stmt, statement->err, nrows, OCI_FETCH_NEXT, OCI_DEFAULT));
337
338        if (piecewisecols) {
339            for (i = 0; i < statement->ncolumns; i++) {
340                column = php_oci_statement_get_column(statement, i + 1, NULL, 0);
341                if (column && column->piecewise && handlepp == column->oci_define)  {
342                    column->retlen4 += column->cb_retlen;
343                }
344            }
345        }
346    }
347
348    if (errstatus == OCI_SUCCESS_WITH_INFO || errstatus == OCI_SUCCESS) {
349        statement->has_data = 1;
350
351        /* do the stuff needed for OCIDefineByName */
352        for (i = 0; i < statement->ncolumns; i++) {
353            column = php_oci_statement_get_column(statement, i + 1, NULL, 0);
354            if (column == NULL) {
355                continue;
356            }
357
358            if (!column->define) {
359                continue;
360            }
361
362            zval_dtor(&column->define->zval);
363            php_oci_column_to_zval(column, &column->define->zval, 0);
364        }
365
366        return 0;
367    }
368
369    statement->errcode = php_oci_error(statement->err, errstatus);
370    PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
371
372    statement->has_data = 0;
373
374    return 1;
375}
376/* }}} */
377
378/* {{{ php_oci_statement_get_column()
379 Get column from the result set */
380php_oci_out_column *php_oci_statement_get_column(php_oci_statement *statement, zend_long column_index, char *column_name, int column_name_len)
381{
382    php_oci_out_column *column = NULL;
383    int i;
384
385    if (statement->columns == NULL) { /* we release the columns at the end of a fetch */
386        return NULL;
387    }
388
389    if (column_name) {
390        for (i = 0; i < statement->ncolumns; i++) {
391            column = php_oci_statement_get_column(statement, i + 1, NULL, 0);
392            if (column == NULL) {
393                continue;
394            } else if (((int) column->name_len == column_name_len) && (!strncmp(column->name, column_name, column_name_len))) {
395                return column;
396            }
397        }
398    } else if (column_index != -1) {
399        if ((column = zend_hash_index_find_ptr(statement->columns, column_index)) == NULL) {
400            return NULL;
401        }
402        return column;
403    }
404
405    return NULL;
406}
407/* }}} */
408
409/* {{{ php_oci_define_callback() */
410sb4 php_oci_define_callback(dvoid *ctx, OCIDefine *define, ub4 iter, dvoid **bufpp, ub4 **alenpp, ub1 *piecep, dvoid **indpp, ub2 **rcpp)
411{
412    php_oci_out_column *outcol = (php_oci_out_column *)ctx;
413
414    if (!outcol) {
415
416        php_error_docref(NULL, E_WARNING, "Invalid context pointer value");
417        return OCI_ERROR;
418    }
419
420    switch(outcol->data_type) {
421        case SQLT_RSET: {
422                php_oci_statement *nested_stmt;
423
424                nested_stmt = php_oci_statement_create(outcol->statement->connection, NULL, 0);
425                if (!nested_stmt) {
426                    return OCI_ERROR;
427                }
428                nested_stmt->parent_stmtid = outcol->statement->id;
429                ++GC_REFCOUNT(outcol->statement->id);
430                outcol->nested_statement = nested_stmt;
431                outcol->stmtid = nested_stmt->id;
432
433                *bufpp = nested_stmt->stmt;
434                *alenpp = &(outcol->retlen4);
435                *piecep = OCI_ONE_PIECE;
436                *indpp = &(outcol->indicator);
437                *rcpp = &(outcol->retcode);
438                return OCI_CONTINUE;
439            }
440            break;
441        case SQLT_RDD:
442        case SQLT_BLOB:
443        case SQLT_CLOB:
444        case SQLT_BFILE: {
445                php_oci_descriptor *descr;
446                int dtype;
447
448                if (outcol->data_type == SQLT_BFILE) {
449                    dtype = OCI_DTYPE_FILE;
450                } else if (outcol->data_type == SQLT_RDD ) {
451                    dtype = OCI_DTYPE_ROWID;
452                } else {
453                    dtype = OCI_DTYPE_LOB;
454                }
455
456                descr = php_oci_lob_create(outcol->statement->connection, dtype);
457                if (!descr) {
458                    return OCI_ERROR;
459                }
460                outcol->descid = descr->id;
461                descr->charset_form = outcol->charset_form;
462
463                *bufpp = descr->descriptor;
464                *alenpp = &(outcol->retlen4);
465                *piecep = OCI_ONE_PIECE;
466                *indpp = &(outcol->indicator);
467                *rcpp = &(outcol->retcode);
468
469                return OCI_CONTINUE;
470            }
471            break;
472    }
473    return OCI_ERROR;
474}
475/* }}} */
476
477/* {{{ php_oci_statement_execute()
478 Execute statement */
479int php_oci_statement_execute(php_oci_statement *statement, ub4 mode)
480{
481    php_oci_out_column *outcol;
482    php_oci_out_column column;
483    OCIParam *param = NULL;
484    text *colname;
485    ub4 counter;
486    ub2 define_type;
487    ub4 iters;
488    ub4 colcount;
489    ub2 dynamic;
490    dvoid *buf;
491    sword errstatus;
492
493    switch (mode) {
494        case OCI_COMMIT_ON_SUCCESS:
495        case OCI_DESCRIBE_ONLY:
496        case OCI_DEFAULT:
497            /* only these are allowed */
498#ifdef HAVE_OCI8_DTRACE
499            if (DTRACE_OCI8_EXECUTE_MODE_ENABLED()) {
500                DTRACE_OCI8_EXECUTE_MODE(statement->connection, statement->connection->client_id, statement, mode);
501            }
502#endif /* HAVE_OCI8_DTRACE */
503            break;
504        default:
505            php_error_docref(NULL, E_WARNING, "Invalid execute mode given: %d", mode);
506            return 1;
507            break;
508    }
509
510    if (!statement->stmttype) {
511        /* get statement type */
512        PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)statement->stmt, OCI_HTYPE_STMT, (ub2 *)&statement->stmttype, (ub4 *)0, OCI_ATTR_STMT_TYPE, statement->err));
513
514        if (errstatus != OCI_SUCCESS) {
515            statement->errcode = php_oci_error(statement->err, errstatus);
516            PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
517            return 1;
518        } else {
519            statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
520        }
521    }
522
523    if (statement->stmttype == OCI_STMT_SELECT) {
524        iters = 0;
525    } else {
526        iters = 1;
527    }
528
529    if (statement->last_query) { /* Don't execute REFCURSORS or Implicit Result Set handles */
530
531        if (statement->binds) {
532            int result = 0;
533            zend_hash_apply_with_argument(statement->binds, (apply_func_arg_t) php_oci_bind_pre_exec, (void *)&result);
534            if (result) {
535                return 1;
536            }
537        }
538
539        /* execute statement */
540        PHP_OCI_CALL_RETURN(errstatus, OCIStmtExecute, (statement->connection->svc, statement->stmt, statement->err, iters, 0, NULL, NULL, mode));
541
542        if (errstatus != OCI_SUCCESS) {
543            statement->errcode = php_oci_error(statement->err, errstatus);
544            PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
545            return 1;
546        }
547
548        if (statement->binds) {
549            zend_hash_apply(statement->binds, (apply_func_t) php_oci_bind_post_exec);
550        }
551
552        if (mode & OCI_COMMIT_ON_SUCCESS) {
553            /* No need to rollback on disconnect */
554            statement->connection->rb_on_disconnect = 0;
555        } else if (statement->stmttype != OCI_STMT_SELECT) {
556            /* Assume some uncommitted DML occurred */
557            statement->connection->rb_on_disconnect = 1;
558        }
559        /* else for SELECT with OCI_NO_AUTO_COMMIT, leave
560         * "rb_on_disconnect" at its previous value.  SELECT can't
561         * initiate uncommitted DML. (An AUTONOMOUS_TRANSACTION in
562         * invoked PL/SQL must explicitly rollback/commit else the
563         * SELECT fails).
564         */
565
566        statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
567    }
568
569    if (statement->stmttype == OCI_STMT_SELECT && statement->executed == 0) {
570        /* we only need to do the define step is this very statement is executed the first time! */
571        statement->executed = 1;
572
573        ALLOC_HASHTABLE(statement->columns);
574        zend_hash_init(statement->columns, 13, NULL, php_oci_column_hash_dtor, 0);
575
576        counter = 1;
577
578        /* get number of columns */
579        PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)statement->stmt, OCI_HTYPE_STMT, (dvoid *)&colcount, (ub4 *)0, OCI_ATTR_PARAM_COUNT, statement->err));
580
581        if (errstatus != OCI_SUCCESS) {
582            statement->errcode = php_oci_error(statement->err, errstatus);
583            PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
584            return 1;
585        }
586
587        statement->ncolumns = colcount;
588
589        for (counter = 1; counter <= colcount; counter++) {
590            memset(&column,0,sizeof(php_oci_out_column));
591
592            if ((outcol = zend_hash_index_update_ptr(statement->columns, counter, &column)) == NULL) {
593                efree(statement->columns);
594                /* out of memory */
595                return 1;
596            }
597
598            /* get column */
599            PHP_OCI_CALL_RETURN(errstatus, OCIParamGet, ((dvoid *)statement->stmt, OCI_HTYPE_STMT, statement->err, (dvoid**)&param, counter));
600
601            if (errstatus != OCI_SUCCESS) {
602                statement->errcode = php_oci_error(statement->err, errstatus);
603                PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
604                return 1;
605            }
606
607            /* get column datatype */
608            PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)param, OCI_DTYPE_PARAM, (dvoid *)&outcol->data_type, (ub4 *)0, OCI_ATTR_DATA_TYPE, statement->err));
609
610            if (errstatus != OCI_SUCCESS) {
611                PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
612                statement->errcode = php_oci_error(statement->err, errstatus);
613                PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
614                return 1;
615            }
616
617            /* get character set form  */
618            PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)param, OCI_DTYPE_PARAM, (dvoid *)&outcol->charset_form, (ub4 *)0, OCI_ATTR_CHARSET_FORM, statement->err));
619
620            if (errstatus != OCI_SUCCESS) {
621                PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
622                statement->errcode = php_oci_error(statement->err, errstatus);
623                PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
624                return 1;
625            }
626
627            /* get character set id  */
628            PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)param, OCI_DTYPE_PARAM, (dvoid *)&outcol->charset_id, (ub4 *)0, OCI_ATTR_CHARSET_ID, statement->err));
629
630            if (errstatus != OCI_SUCCESS) {
631                PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
632                statement->errcode = php_oci_error(statement->err, errstatus);
633                PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
634                return 1;
635            }
636
637            /* get size of the column */
638            PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)param, OCI_DTYPE_PARAM, (dvoid *)&outcol->data_size, (dvoid *)0, OCI_ATTR_DATA_SIZE, statement->err));
639
640            if (errstatus != OCI_SUCCESS) {
641                PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
642                statement->errcode = php_oci_error(statement->err, errstatus);
643                PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
644                return 1;
645            }
646
647            outcol->storage_size4 = outcol->data_size;
648            outcol->retlen = outcol->data_size;
649
650            /* get scale of the column */
651            PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)param, OCI_DTYPE_PARAM, (dvoid *)&outcol->scale, (dvoid *)0, OCI_ATTR_SCALE, statement->err));
652
653            if (errstatus != OCI_SUCCESS) {
654                PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
655                statement->errcode = php_oci_error(statement->err, errstatus);
656                PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
657                return 1;
658            }
659
660            /* get precision of the column */
661            PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)param, OCI_DTYPE_PARAM, (dvoid *)&outcol->precision, (dvoid *)0, OCI_ATTR_PRECISION, statement->err));
662
663            if (errstatus != OCI_SUCCESS) {
664                PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
665                statement->errcode = php_oci_error(statement->err, errstatus);
666                PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
667                return 1;
668            }
669
670            /* get name of the column */
671            PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)param, OCI_DTYPE_PARAM, (dvoid **)&colname, (ub4 *)&outcol->name_len, (ub4)OCI_ATTR_NAME, statement->err));
672
673            if (errstatus != OCI_SUCCESS) {
674                PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
675                statement->errcode = php_oci_error(statement->err, errstatus);
676                PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
677                return 1;
678            }
679            PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
680
681            outcol->name = estrndup((char*) colname, outcol->name_len);
682
683            /* find a user-set define */
684            if (statement->defines) {
685                if ((outcol->define = zend_hash_str_find_ptr(statement->defines, outcol->name, outcol->name_len)) != NULL) {
686                    if (outcol->define->type) {
687                        outcol->data_type = outcol->define->type;
688                    }
689                }
690            }
691
692            buf = 0;
693            switch (outcol->data_type) {
694                case SQLT_RSET:
695                    outcol->statement = statement; /* parent handle */
696
697                    define_type = SQLT_RSET;
698                    outcol->is_cursor = 1;
699                    outcol->statement->has_descr = 1;
700                    outcol->storage_size4 = -1;
701                    outcol->retlen = -1;
702                    dynamic = OCI_DYNAMIC_FETCH;
703                    break;
704
705                case SQLT_RDD:   /* ROWID */
706                case SQLT_BLOB:  /* binary LOB */
707                case SQLT_CLOB:  /* character LOB */
708                case SQLT_BFILE: /* binary file LOB */
709                    outcol->statement = statement; /* parent handle */
710
711                    define_type = outcol->data_type;
712                    outcol->is_descr = 1;
713                    outcol->statement->has_descr = 1;
714                    outcol->storage_size4 = -1;
715                    outcol->chunk_size = 0;
716                    dynamic = OCI_DYNAMIC_FETCH;
717                    break;
718
719                case SQLT_LNG:
720                case SQLT_LBI:
721                    if (outcol->data_type == SQLT_LBI) {
722                        define_type = SQLT_BIN;
723                    } else {
724                        define_type = SQLT_CHR;
725                    }
726                    outcol->storage_size4 = PHP_OCI_MAX_DATA_SIZE;
727                    outcol->piecewise = 1;
728                    dynamic = OCI_DYNAMIC_FETCH;
729                    break;
730
731                case SQLT_BIN:
732                default:
733                    define_type = SQLT_CHR;
734                    if (outcol->data_type == SQLT_BIN) {
735                        define_type = SQLT_BIN;
736                    }
737                    if ((outcol->data_type == SQLT_DAT) || (outcol->data_type == SQLT_NUM)
738#ifdef SQLT_TIMESTAMP
739                        || (outcol->data_type == SQLT_TIMESTAMP)
740#endif
741#ifdef SQLT_TIMESTAMP_TZ
742                        || (outcol->data_type == SQLT_TIMESTAMP_TZ)
743#endif
744#ifdef SQLT_TIMESTAMP_LTZ
745                        || (outcol->data_type == SQLT_TIMESTAMP_LTZ)
746#endif
747#ifdef SQLT_INTERVAL_YM
748                        || (outcol->data_type == SQLT_INTERVAL_YM)
749#endif
750#ifdef SQLT_INTERVAL_DS
751                        || (outcol->data_type == SQLT_INTERVAL_DS)
752#endif
753                        ) {
754                        outcol->storage_size4 = 512; /* XXX this should fit "most" NLS date-formats and Numbers */
755#if defined(SQLT_IBFLOAT) && defined(SQLT_IBDOUBLE)
756                    } else if (outcol->data_type == SQLT_IBFLOAT || outcol->data_type == SQLT_IBDOUBLE) {
757                        outcol->storage_size4 = 1024;
758#endif
759                    } else {
760                        outcol->storage_size4++; /* add one for string terminator */
761                    }
762
763                    outcol->storage_size4 *= 3;
764
765                    dynamic = OCI_DEFAULT;
766                    buf = outcol->data = (text *) safe_emalloc(1, outcol->storage_size4, 0);
767                    memset(buf, 0, outcol->storage_size4);
768                    break;
769            }
770
771            if (dynamic == OCI_DYNAMIC_FETCH) {
772                PHP_OCI_CALL_RETURN(errstatus,
773                    OCIDefineByPos,
774                    (
775                        statement->stmt,                            /* IN/OUT handle to the requested SQL query */
776                        (OCIDefine **)&outcol->oci_define,          /* IN/OUT pointer to a pointer to a define handle */
777                        statement->err,                             /* IN/OUT An error handle  */
778                        counter,                                    /* IN     position in the select list */
779                        (dvoid *)NULL,                              /* IN/OUT pointer to a buffer */
780                        outcol->storage_size4,                      /* IN     The size of each valuep buffer in bytes */
781                        define_type,                                /* IN     The data type */
782                        (dvoid *)&outcol->indicator,                /* IN     pointer to an indicator variable or arr */
783                        (ub2 *)NULL,                                /* IN/OUT Pointer to array of length of data fetched */
784                        (ub2 *)NULL,                                /* OUT    Pointer to array of column-level return codes */
785                        OCI_DYNAMIC_FETCH                           /* IN     mode (OCI_DEFAULT, OCI_DYNAMIC_FETCH) */
786                    )
787                );
788
789            } else {
790                PHP_OCI_CALL_RETURN(errstatus,
791                    OCIDefineByPos,
792                    (
793                        statement->stmt,                            /* IN/OUT handle to the requested SQL query */
794                        (OCIDefine **)&outcol->oci_define,          /* IN/OUT pointer to a pointer to a define handle */
795                        statement->err,                             /* IN/OUT An error handle  */
796                        counter,                                    /* IN     position in the select list */
797                        (dvoid *)buf,                               /* IN/OUT pointer to a buffer */
798                        outcol->storage_size4,                      /* IN     The size of each valuep buffer in bytes */
799                        define_type,                                /* IN     The data type */
800                        (dvoid *)&outcol->indicator,                /* IN     pointer to an indicator variable or arr */
801                        (ub2 *)&outcol->retlen,                     /* IN/OUT Pointer to array of length of data fetched */
802                        (ub2 *)&outcol->retcode,                    /* OUT    Pointer to array of column-level return codes */
803                        OCI_DEFAULT                                 /* IN     mode (OCI_DEFAULT, OCI_DYNAMIC_FETCH) */
804                    )
805                );
806
807            }
808
809            if (errstatus != OCI_SUCCESS) {
810                statement->errcode = php_oci_error(statement->err, errstatus);
811                PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
812                return 1;
813            }
814
815            /* additional OCIDefineDynamic() call */
816            switch (outcol->data_type) {
817                case SQLT_RSET:
818                case SQLT_RDD:
819                case SQLT_BLOB:
820                case SQLT_CLOB:
821                case SQLT_BFILE:
822                    PHP_OCI_CALL_RETURN(errstatus,
823                        OCIDefineDynamic,
824                        (
825                            outcol->oci_define,
826                            statement->err,
827                            (dvoid *)outcol,
828                            php_oci_define_callback
829                        )
830                    );
831
832                    if (errstatus != OCI_SUCCESS) {
833                        statement->errcode = php_oci_error(statement->err, errstatus);
834                        PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
835                        return 1;
836                    }
837                    break;
838            }
839        }
840        statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
841    }
842
843    return 0;
844}
845/* }}} */
846
847/* {{{ php_oci_statement_cancel()
848 Cancel statement */
849int php_oci_statement_cancel(php_oci_statement *statement)
850{
851    return php_oci_statement_fetch(statement, 0);
852}
853/* }}} */
854
855/* {{{ php_oci_statement_free()
856 Destroy statement handle and free associated resources */
857void php_oci_statement_free(php_oci_statement *statement)
858{
859    if (statement->stmt) {
860        if (statement->last_query_len) { /* FIXME: magical */
861            PHP_OCI_CALL(OCIStmtRelease, (statement->stmt, statement->err, NULL, 0, statement->errcode ? OCI_STRLS_CACHE_DELETE : OCI_DEFAULT));
862        } else if (statement->impres_flag != PHP_OCI_IMPRES_IS_CHILD) {  /* Oracle doc says don't free Implicit Result Set handles */
863            PHP_OCI_CALL(OCIHandleFree, (statement->stmt, OCI_HTYPE_STMT));
864        }
865        statement->stmt = NULL;
866    }
867
868    if (statement->err) {
869        PHP_OCI_CALL(OCIHandleFree, (statement->err, OCI_HTYPE_ERROR));
870        statement->err = NULL;
871    }
872
873    if (statement->last_query) {
874        efree(statement->last_query);
875    }
876
877    if (statement->columns) {
878        zend_hash_destroy(statement->columns);
879        efree(statement->columns);
880    }
881
882    if (statement->binds) {
883        zend_hash_destroy(statement->binds);
884        efree(statement->binds);
885    }
886
887    if (statement->defines) {
888        zend_hash_destroy(statement->defines);
889        efree(statement->defines);
890    }
891
892    if (statement->parent_stmtid) {
893        zend_list_delete(statement->parent_stmtid);
894    }
895
896    zend_list_delete(statement->connection->id);
897    efree(statement);
898
899    OCI_G(num_statements)--;
900}
901/* }}} */
902
903/* {{{ php_oci_bind_pre_exec()
904 Helper function */
905int php_oci_bind_pre_exec(void *data, void *result)
906{
907    php_oci_bind *bind = (php_oci_bind *) data;
908
909    *(int *)result = 0;
910
911    if (Z_TYPE(bind->zval) == IS_ARRAY) {
912        /* These checks are currently valid for oci_bind_by_name, not
913         * oci_bind_array_by_name.  Also bind->type and
914         * bind->indicator are not used for oci_bind_array_by_name.
915         */
916        return 0;
917    }
918    switch (bind->type) {
919        case SQLT_NTY:
920        case SQLT_BFILEE:
921        case SQLT_CFILEE:
922        case SQLT_CLOB:
923        case SQLT_BLOB:
924        case SQLT_RDD:
925            if (Z_TYPE(bind->zval) != IS_OBJECT) {
926                php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
927                *(int *)result = 1;
928            }
929            break;
930
931        case SQLT_CHR:
932        case SQLT_AFC:
933        case SQLT_INT:
934        case SQLT_NUM:
935#if defined(OCI_MAJOR_VERSION) && OCI_MAJOR_VERSION >= 12
936        case SQLT_BOL:
937#endif
938        case SQLT_LBI:
939        case SQLT_BIN:
940        case SQLT_LNG:
941            if (Z_TYPE(bind->zval) == IS_RESOURCE || Z_TYPE(bind->zval) == IS_OBJECT) {
942                php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
943                *(int *)result = 1;
944            }
945            break;
946
947        case SQLT_RSET:
948            if (Z_TYPE(bind->zval) != IS_RESOURCE) {
949                php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
950                *(int *)result = 1;
951            }
952            break;
953    }
954
955    /* reset all bind stuff to a normal state... */
956    bind->indicator = 0;
957
958    return 0;
959}
960/* }}} */
961
962/* {{{ php_oci_bind_post_exec()
963 Helper function */
964int php_oci_bind_post_exec(void *data)
965{
966    php_oci_bind *bind = (php_oci_bind *) data;
967    php_oci_connection *connection = bind->parent_statement->connection;
968    sword errstatus;
969
970    if (bind->indicator == -1) { /* NULL */
971        zval *val = &bind->zval;
972        if (Z_TYPE_P(val) == IS_STRING) {
973            *Z_STRVAL_P(val) = '\0'; /* XXX avoid warning in debug mode */
974        }
975        zval_dtor(val);
976        ZVAL_NULL(val);
977    } else if (Z_TYPE(bind->zval) == IS_STRING
978               && Z_STRLEN(bind->zval) > 0
979               && Z_STRVAL(bind->zval)[ Z_STRLEN(bind->zval) ] != '\0') {
980        /* The post- PHP 5.3 feature for "interned" strings disallows
981         * their reallocation but (i) any IN binds either interned or
982         * not should already be null terminated and (ii) for OUT
983         * binds, php_oci_bind_out_callback() should have allocated a
984         * new string that we can modify here.
985         */
986        Z_STR(bind->zval) = zend_string_realloc(Z_STR(bind->zval), Z_STRLEN(bind->zval)+1, 0);
987        Z_STRVAL(bind->zval)[ Z_STRLEN(bind->zval) ] = '\0';
988    } else if (Z_TYPE(bind->zval) == IS_ARRAY) {
989        int i;
990        zval *entry = NULL;
991        HashTable *hash = HASH_OF(&bind->zval);
992
993        zend_hash_internal_pointer_reset(hash);
994
995        switch (bind->array.type) {
996            case SQLT_NUM:
997            case SQLT_INT:
998            case SQLT_LNG:
999                for (i = 0; i < bind->array.current_length; i++) {
1000                    if ((i < bind->array.old_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
1001                        zval_dtor(entry);
1002                        ZVAL_LONG(entry, ((ub4 *)(bind->array.elements))[i]);
1003                        zend_hash_move_forward(hash);
1004                    } else {
1005                        add_next_index_long(&bind->zval, ((ub4 *)(bind->array.elements))[i]);
1006                    }
1007                }
1008                break;
1009            case SQLT_FLT:
1010                for (i = 0; i < bind->array.current_length; i++) {
1011                    if ((i < bind->array.old_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
1012                        zval_dtor(entry);
1013                        ZVAL_DOUBLE(entry, ((double *)(bind->array.elements))[i]);
1014                        zend_hash_move_forward(hash);
1015                    } else {
1016                        add_next_index_double(&bind->zval, ((double *)(bind->array.elements))[i]);
1017                    }
1018                }
1019                break;
1020            case SQLT_ODT:
1021                for (i = 0; i < bind->array.current_length; i++) {
1022                    oratext buff[1024];
1023                    ub4 buff_len = 1024;
1024
1025                    memset((void*)buff,0,sizeof(buff));
1026
1027                    if ((i < bind->array.old_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
1028                        PHP_OCI_CALL_RETURN(errstatus, OCIDateToText, (connection->err, &(((OCIDate *)(bind->array.elements))[i]), 0, 0, 0, 0, &buff_len, buff));
1029                        zval_dtor(entry);
1030
1031                        if (errstatus != OCI_SUCCESS) {
1032                            connection->errcode = php_oci_error(connection->err, errstatus);
1033                            PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
1034                            ZVAL_NULL(entry);
1035                        } else {
1036                            connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
1037                            ZVAL_STRINGL(entry, (char *)buff, buff_len);
1038                        }
1039                        zend_hash_move_forward(hash);
1040                    } else {
1041                        PHP_OCI_CALL_RETURN(errstatus, OCIDateToText, (connection->err, &(((OCIDate *)(bind->array.elements))[i]), 0, 0, 0, 0, &buff_len, buff));
1042                        if (errstatus != OCI_SUCCESS) {
1043                            connection->errcode = php_oci_error(connection->err, errstatus);
1044                            PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
1045                            add_next_index_null(&bind->zval);
1046                        } else {
1047                            connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
1048                            add_next_index_stringl(&bind->zval, (char *)buff, buff_len);
1049                        }
1050                    }
1051                }
1052                break;
1053
1054            case SQLT_AFC:
1055            case SQLT_CHR:
1056            case SQLT_VCS:
1057            case SQLT_AVC:
1058            case SQLT_STR:
1059            case SQLT_LVC:
1060                for (i = 0; i < bind->array.current_length; i++) {
1061                    /* int curr_element_length = strlen(((text *)bind->array.elements)+i*bind->array.max_length); */
1062                    int curr_element_length = bind->array.element_lengths[i];
1063                    if ((i < bind->array.old_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
1064                        zval_dtor(entry);
1065                        ZVAL_STRINGL(entry, (char *)(((text *)bind->array.elements)+i*bind->array.max_length), curr_element_length);
1066                        zend_hash_move_forward(hash);
1067                    } else {
1068                        add_next_index_stringl(&bind->zval, (char *)(((text *)bind->array.elements)+i*bind->array.max_length), curr_element_length);
1069                    }
1070                }
1071                break;
1072        }
1073    }
1074
1075    return 0;
1076}
1077/* }}} */
1078
1079/* {{{ php_oci_bind_by_name()
1080 Bind zval to the given placeholder */
1081int php_oci_bind_by_name(php_oci_statement *statement, char *name, int name_len, zval *var, zend_long maxlength, ub2 type)
1082{
1083    php_oci_collection *bind_collection = NULL;
1084    php_oci_descriptor *bind_descriptor = NULL;
1085    php_oci_statement  *bind_statement  = NULL;
1086    dvoid *oci_desc                 = NULL;
1087    /* dvoid *php_oci_collection           = NULL; */
1088    OCIStmt *oci_stmt               = NULL;
1089    dvoid *bind_data                = NULL;
1090    php_oci_bind bind, *old_bind, *bindp;
1091    int mode = OCI_DATA_AT_EXEC;
1092    sb4 value_sz = -1;
1093    sword errstatus;
1094
1095    switch (type) {
1096        case SQLT_NTY:
1097        {
1098            zval *tmp;
1099
1100            if (Z_TYPE_P(var) != IS_OBJECT || (tmp = zend_hash_str_find(Z_OBJPROP_P(var), "collection", sizeof("collection"))) == NULL) {
1101                php_error_docref(NULL, E_WARNING, "Unable to find collection property");
1102                return 1;
1103            }
1104
1105            PHP_OCI_ZVAL_TO_COLLECTION_EX(tmp, bind_collection);
1106            value_sz = sizeof(void*);
1107            mode = OCI_DEFAULT;
1108
1109            if (!bind_collection->collection) {
1110                return 1;
1111            }
1112        }
1113            break;
1114        case SQLT_BFILEE:
1115        case SQLT_CFILEE:
1116        case SQLT_CLOB:
1117        case SQLT_BLOB:
1118        case SQLT_RDD:
1119        {
1120            zval *tmp;
1121
1122            if (Z_TYPE_P(var) != IS_OBJECT || (tmp = zend_hash_str_find(Z_OBJPROP_P(var), "descriptor", sizeof("descriptor"))) == NULL) {
1123                php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
1124                return 1;
1125            }
1126
1127            PHP_OCI_ZVAL_TO_DESCRIPTOR_EX(tmp, bind_descriptor);
1128
1129            value_sz = sizeof(void*);
1130
1131            oci_desc = bind_descriptor->descriptor;
1132
1133            if (!oci_desc) {
1134                return 1;
1135            }
1136        }
1137            break;
1138
1139        case SQLT_INT:
1140        case SQLT_NUM:
1141            if (Z_TYPE_P(var) == IS_RESOURCE || Z_TYPE_P(var) == IS_OBJECT) {
1142                php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
1143                return 1;
1144            }
1145            convert_to_long(var);
1146            bind_data = (ub4 *)&Z_LVAL_P(var);
1147            value_sz = sizeof(ub4);
1148            mode = OCI_DEFAULT;
1149            break;
1150
1151        case SQLT_LBI:
1152        case SQLT_BIN:
1153        case SQLT_LNG:
1154        case SQLT_AFC:
1155        case SQLT_CHR: /* SQLT_CHR is the default value when type was not specified */
1156            if (Z_TYPE_P(var) == IS_RESOURCE || Z_TYPE_P(var) == IS_OBJECT) {
1157                php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
1158                return 1;
1159            }
1160            if (Z_TYPE_P(var) != IS_NULL) {
1161                convert_to_string(var);
1162            }
1163            if (maxlength == -1) {
1164                value_sz = (Z_TYPE_P(var) == IS_STRING) ? Z_STRLEN_P(var) : 0;
1165            } else {
1166                value_sz = maxlength;
1167            }
1168            break;
1169
1170        case SQLT_RSET:
1171            if (Z_TYPE_P(var) != IS_RESOURCE) {
1172                php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
1173                return 1;
1174            }
1175            PHP_OCI_ZVAL_TO_STATEMENT_EX(var, bind_statement);
1176            value_sz = sizeof(void*);
1177
1178            oci_stmt = bind_statement->stmt;
1179
1180            if (!oci_stmt) {
1181                return 1;
1182            }
1183            break;
1184
1185#if defined(OCI_MAJOR_VERSION) && OCI_MAJOR_VERSION >= 12
1186        case SQLT_BOL:
1187            if (Z_TYPE_P(var) == IS_RESOURCE || Z_TYPE_P(var) == IS_OBJECT) {
1188                php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
1189                return 1;
1190            }
1191            convert_to_boolean(var);
1192            bind_data = (int *)&Z_LVAL_P(var);
1193            value_sz = sizeof(int);
1194
1195            mode = OCI_DEFAULT;
1196            break;
1197#endif
1198
1199        default:
1200            php_error_docref(NULL, E_WARNING, "Unknown or unsupported datatype given: %d", (int)type);
1201            return 1;
1202            break;
1203    }
1204
1205    if (value_sz == 0) {
1206        value_sz = 1;
1207    }
1208
1209    if (!statement->binds) {
1210        ALLOC_HASHTABLE(statement->binds);
1211        zend_hash_init(statement->binds, 13, NULL, php_oci_bind_hash_dtor, 0);
1212    }
1213
1214    memset((void*)&bind,0,sizeof(php_oci_bind));
1215    if ((old_bind = zend_hash_str_find_ptr(statement->binds, name, name_len)) != NULL) {
1216        bindp = old_bind;
1217        zval_ptr_dtor(&bindp->zval);
1218    } else {
1219        bindp = zend_hash_update_ptr(statement->binds, zend_string_init(name, name_len + 1, 0), &bind);
1220    }
1221
1222    bindp->descriptor = oci_desc;
1223    bindp->statement = oci_stmt;
1224    bindp->parent_statement = statement;
1225    ZVAL_COPY(&bindp->zval, var);
1226    bindp->type = type;
1227    Z_ADDREF_P(var);
1228
1229    PHP_OCI_CALL_RETURN(errstatus,
1230        OCIBindByName,
1231        (
1232            statement->stmt,                 /* statement handle */
1233            (OCIBind **)&bindp->bind,        /* bind hdl (will alloc) */
1234            statement->err,                  /* error handle */
1235            (text*) name,                    /* placeholder name */
1236            name_len,                        /* placeholder length */
1237            (dvoid *)bind_data,              /* in/out data */
1238            value_sz, /* PHP_OCI_MAX_DATA_SIZE, */ /* max size of input/output data */
1239            type,                            /* in/out data type */
1240            (dvoid *)&bindp->indicator,      /* indicator (ignored) */
1241            (ub2 *)0,                        /* size array (ignored) */
1242            (ub2 *)&bindp->retcode,          /* return code (ignored) */
1243            (ub4)0,                          /* maxarr_len (PL/SQL only?) */
1244            (ub4 *)0,                        /* actual array size (PL/SQL only?) */
1245            mode                             /* mode */
1246        )
1247    );
1248
1249    if (errstatus != OCI_SUCCESS) {
1250        statement->errcode = php_oci_error(statement->err, errstatus);
1251        PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
1252        return 1;
1253    }
1254
1255    if (mode == OCI_DATA_AT_EXEC) {
1256        PHP_OCI_CALL_RETURN(errstatus, OCIBindDynamic,
1257                (
1258                 bindp->bind,
1259                 statement->err,
1260                 (dvoid *)bindp,
1261                 php_oci_bind_in_callback,
1262                 (dvoid *)bindp,
1263                 php_oci_bind_out_callback
1264                )
1265        );
1266
1267        if (errstatus != OCI_SUCCESS) {
1268            statement->errcode = php_oci_error(statement->err, errstatus);
1269            PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
1270            return 1;
1271        }
1272    }
1273
1274    if (type == SQLT_NTY) {
1275        /* Bind object */
1276        PHP_OCI_CALL_RETURN(errstatus, OCIBindObject,
1277                (
1278                 bindp->bind,
1279                 statement->err,
1280                 bind_collection->tdo,
1281                 (dvoid **) &(bind_collection->collection),
1282                 (ub4 *) 0,
1283                 (dvoid **) 0,
1284                 (ub4 *) 0
1285                )
1286        );
1287
1288        if (errstatus) {
1289            statement->errcode = php_oci_error(statement->err, errstatus);
1290            PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
1291            return 1;
1292        }
1293    }
1294
1295    statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
1296    return 0;
1297}
1298/* }}} */
1299
1300/* {{{ php_oci_bind_in_callback()
1301 Callback used when binding LOBs and VARCHARs */
1302sb4 php_oci_bind_in_callback(
1303                    dvoid *ictxp,     /* context pointer */
1304                    OCIBind *bindp,   /* bind handle */
1305                    ub4 iter,         /* 0-based execute iteration value */
1306                    ub4 index,        /* index of current array for PL/SQL or row index for SQL */
1307                    dvoid **bufpp,    /* pointer to data */
1308                    ub4 *alenp,       /* size after value/piece has been read */
1309                    ub1 *piecep,      /* which piece */
1310                    dvoid **indpp)    /* indicator value */
1311{
1312    php_oci_bind *phpbind;
1313    zval *val;
1314
1315    if (!(phpbind=(php_oci_bind *)ictxp) || !(val = &phpbind->zval)) {
1316        php_error_docref(NULL, E_WARNING, "Invalid phpbind pointer value");
1317        return OCI_ERROR;
1318    }
1319
1320    if (ZVAL_IS_NULL(val)) {
1321        /* we're going to insert a NULL column */
1322        phpbind->indicator = -1;
1323        *bufpp = 0;
1324        *alenp = -1;
1325        *indpp = (dvoid *)&phpbind->indicator;
1326    } else  if ((phpbind->descriptor == 0) && (phpbind->statement == 0)) {
1327        /* "normal string bind */
1328        convert_to_string(val);
1329
1330        *bufpp = Z_STRVAL_P(val);
1331        *alenp = Z_STRLEN_P(val);
1332        *indpp = (dvoid *)&phpbind->indicator;
1333    } else if (phpbind->statement != 0) {
1334        /* RSET */
1335        *bufpp = phpbind->statement;
1336        *alenp = -1;        /* seems to be allright */
1337        *indpp = (dvoid *)&phpbind->indicator;
1338    } else {
1339        /* descriptor bind */
1340        *bufpp = phpbind->descriptor;
1341        *alenp = -1;        /* seems to be allright */
1342        *indpp = (dvoid *)&phpbind->indicator;
1343    }
1344
1345    *piecep = OCI_ONE_PIECE; /* pass all data in one go */
1346
1347    return OCI_CONTINUE;
1348}
1349/* }}} */
1350
1351/* {{{ php_oci_bind_out_callback()
1352 Callback used when binding LOBs and VARCHARs */
1353sb4 php_oci_bind_out_callback(
1354                    dvoid *octxp,      /* context pointer */
1355                    OCIBind *bindp,    /* bind handle */
1356                    ub4 iter,          /* 0-based execute iteration value */
1357                    ub4 index,         /* index of current array for PL/SQL or row index for SQL */
1358                    dvoid **bufpp,     /* pointer to data */
1359                    ub4 **alenpp,      /* size after value/piece has been read */
1360                    ub1 *piecep,       /* which piece */
1361                    dvoid **indpp,     /* indicator value */
1362                    ub2 **rcodepp)     /* return code */
1363{
1364    php_oci_bind *phpbind;
1365    zval *val;
1366    sb4 retval = OCI_ERROR;
1367
1368    if (!(phpbind=(php_oci_bind *)octxp) || !(val = &phpbind->zval)) {
1369        php_error_docref(NULL, E_WARNING, "Invalid phpbind pointer value");
1370        return retval;
1371    }
1372
1373    if (Z_TYPE_P(val) == IS_RESOURCE) {
1374        /* Processing for ref-cursor out binds */
1375        if (phpbind->statement != NULL) {
1376            *bufpp = phpbind->statement;
1377            *alenpp = &phpbind->dummy_len;
1378            *piecep = OCI_ONE_PIECE;
1379            *rcodepp = &phpbind->retcode;
1380            *indpp = &phpbind->indicator;
1381        }
1382        retval = OCI_CONTINUE;
1383    } else if (Z_TYPE_P(val) == IS_OBJECT) {
1384        zval *tmp;
1385        php_oci_descriptor *desc;
1386
1387        if (!phpbind->descriptor) {
1388            return OCI_ERROR;
1389        }
1390
1391        /* Do not use the cached lob size if the descriptor is an
1392         * out-bind as the contents would have been changed for in/out
1393         * binds (Bug #46994).
1394         */
1395        if ((tmp = zend_hash_str_find(Z_OBJPROP_P(val), "descriptor", sizeof("descriptor"))) == NULL) {
1396            php_error_docref(NULL, E_WARNING, "Unable to find object outbind descriptor property");
1397            return OCI_ERROR;
1398        }
1399        PHP_OCI_ZVAL_TO_DESCRIPTOR_EX(tmp, desc);
1400        desc->lob_size = -1;    /* force OCI8 to update cached size */
1401
1402        *alenpp = &phpbind->dummy_len;
1403        *bufpp = phpbind->descriptor;
1404        *piecep = OCI_ONE_PIECE;
1405        *rcodepp = &phpbind->retcode;
1406        *indpp = &phpbind->indicator;
1407        retval = OCI_CONTINUE;
1408    } else {
1409        convert_to_string(val);
1410        zval_dtor(val);
1411
1412        //Z_STRLEN_P(val) = PHP_OCI_PIECE_SIZE; /* 64K-1 is max XXX */
1413        //Z_STRVAL_P(val) = ecalloc(1, Z_STRLEN_P(phpbind->zval) + 1);
1414        // XXX is this right?
1415        ZVAL_STRINGL(val, NULL, Z_STRLEN(phpbind->zval) + 1);
1416
1417        /* XXX we assume that zend-zval len has 4 bytes */
1418        *alenpp = (ub4*) &Z_STRLEN(phpbind->zval);
1419        *bufpp = Z_STRVAL(phpbind->zval);
1420        *piecep = OCI_ONE_PIECE;
1421        *rcodepp = &phpbind->retcode;
1422        *indpp = &phpbind->indicator;
1423        retval = OCI_CONTINUE;
1424    }
1425
1426    return retval;
1427}
1428/* }}} */
1429
1430/* {{{ php_oci_statement_get_column_helper()
1431 Helper function to get column by name and index */
1432php_oci_out_column *php_oci_statement_get_column_helper(INTERNAL_FUNCTION_PARAMETERS, int need_data)
1433{
1434    zval *z_statement, *column_index;
1435    php_oci_statement *statement;
1436    php_oci_out_column *column;
1437
1438    if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &z_statement, &column_index) == FAILURE) {
1439        return NULL;
1440    }
1441
1442    statement = (php_oci_statement *) zend_fetch_resource(z_statement, -1, "oci8 statement", NULL, 1, le_statement);
1443
1444    if (!statement) {
1445        return NULL;
1446    }
1447
1448    if (need_data && !statement->has_data) {
1449        return NULL;
1450    }
1451
1452    if (Z_TYPE_P(column_index) == IS_STRING) {
1453        column = php_oci_statement_get_column(statement, -1, Z_STRVAL_P(column_index), Z_STRLEN_P(column_index));
1454        if (!column) {
1455            php_error_docref(NULL, E_WARNING, "Invalid column name \"%s\"", Z_STRVAL_P(column_index));
1456            return NULL;
1457        }
1458    } else {
1459        zval tmp;
1460        /* NB: for PHP4 compat only, it should be using 'Z' instead */
1461        tmp = *column_index;
1462        zval_copy_ctor(&tmp);
1463        convert_to_long(&tmp);
1464        column = php_oci_statement_get_column(statement, Z_LVAL(tmp), NULL, 0);
1465        if (!column) {
1466            php_error_docref(NULL, E_WARNING, "Invalid column index \"%pd\"", Z_LVAL(tmp));
1467            zval_dtor(&tmp);
1468            return NULL;
1469        }
1470        zval_dtor(&tmp);
1471    }
1472    return column;
1473}
1474/* }}} */
1475
1476/* {{{ php_oci_statement_get_type()
1477 Return type of the statement */
1478int php_oci_statement_get_type(php_oci_statement *statement, ub2 *type)
1479{
1480    ub2 statement_type;
1481    sword errstatus;
1482
1483    *type = 0;
1484
1485    PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)statement->stmt, OCI_HTYPE_STMT, (ub2 *)&statement_type, (ub4 *)0, OCI_ATTR_STMT_TYPE, statement->err));
1486
1487    if (errstatus != OCI_SUCCESS) {
1488        statement->errcode = php_oci_error(statement->err, errstatus);
1489        PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
1490        return 1;
1491    }
1492    statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
1493    *type = statement_type;
1494
1495    return 0;
1496}
1497/* }}} */
1498
1499/* {{{ php_oci_statement_get_numrows()
1500 Get the number of rows fetched to the clientside (NOT the number of rows in the result set) */
1501int php_oci_statement_get_numrows(php_oci_statement *statement, ub4 *numrows)
1502{
1503    ub4 statement_numrows;
1504    sword errstatus;
1505
1506    *numrows = 0;
1507
1508    PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)statement->stmt, OCI_HTYPE_STMT, (ub4 *)&statement_numrows, (ub4 *)0, OCI_ATTR_ROW_COUNT, statement->err));
1509
1510    if (errstatus != OCI_SUCCESS) {
1511        statement->errcode = php_oci_error(statement->err, errstatus);
1512        PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
1513        return 1;
1514    }
1515    statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
1516    *numrows = statement_numrows;
1517
1518    return 0;
1519}
1520/* }}} */
1521
1522/* {{{ php_oci_bind_array_by_name()
1523 Bind arrays to PL/SQL types */
1524int php_oci_bind_array_by_name(php_oci_statement *statement, char *name, int name_len, zval *var, zend_long max_table_length, zend_long maxlength, zend_long type)
1525{
1526    php_oci_bind *bind, *bindp;
1527    sword errstatus;
1528
1529    convert_to_array(var);
1530
1531    if (maxlength < -1) {
1532        php_error_docref(NULL, E_WARNING, "Invalid max length value (%pd)", maxlength);
1533        return 1;
1534    }
1535
1536    switch(type) {
1537        case SQLT_NUM:
1538        case SQLT_INT:
1539        case SQLT_LNG:
1540            bind = php_oci_bind_array_helper_number(var, max_table_length);
1541            break;
1542
1543        case SQLT_FLT:
1544            bind = php_oci_bind_array_helper_double(var, max_table_length);
1545            break;
1546
1547        case SQLT_AFC:
1548        case SQLT_CHR:
1549        case SQLT_VCS:
1550        case SQLT_AVC:
1551        case SQLT_STR:
1552        case SQLT_LVC:
1553            if (maxlength == -1 && zend_hash_num_elements(Z_ARRVAL_P(var)) == 0) {
1554                php_error_docref(NULL, E_WARNING, "You must provide max length value for empty arrays");
1555                return 1;
1556            }
1557            bind = php_oci_bind_array_helper_string(var, max_table_length, maxlength);
1558            break;
1559        case SQLT_ODT:
1560            bind = php_oci_bind_array_helper_date(var, max_table_length, statement->connection);
1561            break;
1562        default:
1563            php_error_docref(NULL, E_WARNING, "Unknown or unsupported datatype given: %pd", type);
1564            return 1;
1565            break;
1566    }
1567
1568    if (bind == NULL) {
1569        /* failed to generate bind struct */
1570        return 1;
1571    }
1572
1573    if (!statement->binds) {
1574        ALLOC_HASHTABLE(statement->binds);
1575        zend_hash_init(statement->binds, 13, NULL, php_oci_bind_hash_dtor, 0);
1576    }
1577
1578    bindp = zend_hash_update_ptr(statement->binds, zend_string_init(name, name_len + 1, 0), bind);
1579
1580    bindp->descriptor = NULL;
1581    bindp->statement = NULL;
1582    bindp->parent_statement = statement;
1583    bindp->bind = NULL;
1584    ZVAL_COPY(&bindp->zval, var);
1585    bindp->array.type = type;
1586    bindp->indicator = 0;       /* not used for array binds */
1587    bindp->type = 0;            /* not used for array binds */
1588
1589    Z_ADDREF_P(var);
1590
1591    PHP_OCI_CALL_RETURN(errstatus,
1592                            OCIBindByName,
1593                            (
1594                                statement->stmt,
1595                                (OCIBind **)&bindp->bind,
1596                                statement->err,
1597                                (text *)name,
1598                                name_len,
1599                                (dvoid *) bindp->array.elements,
1600                                (sb4) bind->array.max_length,
1601                                (ub2)type,
1602                                (dvoid *)bindp->array.indicators,
1603                                (ub2 *)bind->array.element_lengths,
1604                                (ub2 *)0, /* bindp->array.retcodes, */
1605                                (ub4) max_table_length,
1606                                (ub4 *) &(bindp->array.current_length),
1607                                (ub4) OCI_DEFAULT
1608                            )
1609                        );
1610
1611
1612    if (errstatus != OCI_SUCCESS) {
1613        efree(bind);
1614        statement->errcode = php_oci_error(statement->err, errstatus);
1615        PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
1616        return 1;
1617    }
1618    statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
1619    efree(bind);
1620    return 0;
1621}
1622/* }}} */
1623
1624/* {{{ php_oci_bind_array_helper_string()
1625 Bind arrays to PL/SQL types */
1626php_oci_bind *php_oci_bind_array_helper_string(zval *var, zend_long max_table_length, zend_long maxlength)
1627{
1628    php_oci_bind *bind;
1629    ub4 i;
1630    HashTable *hash;
1631    zval *entry;
1632
1633    hash = HASH_OF(var);
1634
1635    if (maxlength == -1) {
1636        zend_hash_internal_pointer_reset(hash);
1637        while ((entry = zend_hash_get_current_data(hash)) != NULL) {
1638            convert_to_string_ex(entry);
1639            if (Z_STRLEN_P(entry) > maxlength) {
1640                maxlength = Z_STRLEN_P(entry) + 1;
1641            }
1642            zend_hash_move_forward(hash);
1643        }
1644    }
1645
1646    bind = emalloc(sizeof(php_oci_bind));
1647    bind->array.elements        = (text *)safe_emalloc(max_table_length * (maxlength + 1), sizeof(text), 0);
1648    memset(bind->array.elements, 0, max_table_length * (maxlength + 1) * sizeof(text));
1649    bind->array.current_length  = zend_hash_num_elements(Z_ARRVAL_P(var));
1650    bind->array.old_length      = bind->array.current_length;
1651    bind->array.max_length      = maxlength;
1652    bind->array.element_lengths = safe_emalloc(max_table_length, sizeof(ub2), 0);
1653    memset(bind->array.element_lengths, 0, max_table_length*sizeof(ub2));
1654    bind->array.indicators      = safe_emalloc(max_table_length, sizeof(sb2), 0);
1655    memset(bind->array.indicators, 0, max_table_length*sizeof(sb2));
1656
1657    zend_hash_internal_pointer_reset(hash);
1658
1659    for (i = 0; i < bind->array.current_length; i++) {
1660        if ((entry = zend_hash_get_current_data(hash)) != NULL) {
1661            convert_to_string_ex(entry);
1662            bind->array.element_lengths[i] = Z_STRLEN_P(entry);
1663            if (Z_STRLEN_P(entry) == 0) {
1664                bind->array.indicators[i] = -1;
1665            }
1666            zend_hash_move_forward(hash);
1667        } else {
1668            break;
1669        }
1670    }
1671
1672    zend_hash_internal_pointer_reset(hash);
1673    for (i = 0; i < max_table_length; i++) {
1674        if ((i < bind->array.current_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
1675            int element_length;
1676
1677            convert_to_string_ex(entry);
1678            element_length = (maxlength > Z_STRLEN_P(entry)) ? Z_STRLEN_P(entry) : maxlength;
1679
1680            memcpy((text *)bind->array.elements + i*maxlength, Z_STRVAL_P(entry), element_length);
1681            ((text *)bind->array.elements)[i*maxlength + element_length] = '\0';
1682
1683            zend_hash_move_forward(hash);
1684        } else {
1685            ((text *)bind->array.elements)[i*maxlength] = '\0';
1686        }
1687    }
1688    zend_hash_internal_pointer_reset(hash);
1689
1690    return bind;
1691}
1692/* }}} */
1693
1694/* {{{ php_oci_bind_array_helper_number()
1695 Bind arrays to PL/SQL types */
1696php_oci_bind *php_oci_bind_array_helper_number(zval *var, zend_long max_table_length)
1697{
1698    php_oci_bind *bind;
1699    ub4 i;
1700    HashTable *hash;
1701    zval *entry;
1702
1703    hash = HASH_OF(var);
1704
1705    bind = emalloc(sizeof(php_oci_bind));
1706    bind->array.elements        = (ub4 *)safe_emalloc(max_table_length, sizeof(ub4), 0);
1707    bind->array.current_length  = zend_hash_num_elements(Z_ARRVAL_P(var));
1708    bind->array.old_length      = bind->array.current_length;
1709    bind->array.max_length      = sizeof(ub4);
1710    bind->array.element_lengths = safe_emalloc(max_table_length, sizeof(ub2), 0);
1711    memset(bind->array.element_lengths, 0, max_table_length * sizeof(ub2));
1712    bind->array.indicators      = NULL;
1713
1714    zend_hash_internal_pointer_reset(hash);
1715    for (i = 0; i < max_table_length; i++) {
1716        if (i < bind->array.current_length) {
1717            bind->array.element_lengths[i] = sizeof(ub4);
1718        }
1719        if ((i < bind->array.current_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
1720            convert_to_long_ex(entry);
1721            ((ub4 *)bind->array.elements)[i] = (ub4) Z_LVAL_P(entry);
1722            zend_hash_move_forward(hash);
1723        } else {
1724            ((ub4 *)bind->array.elements)[i] = 0;
1725        }
1726    }
1727    zend_hash_internal_pointer_reset(hash);
1728
1729    return bind;
1730}
1731/* }}} */
1732
1733/* {{{ php_oci_bind_array_helper_double()
1734 Bind arrays to PL/SQL types */
1735php_oci_bind *php_oci_bind_array_helper_double(zval *var, zend_long max_table_length)
1736{
1737    php_oci_bind *bind;
1738    ub4 i;
1739    HashTable *hash;
1740    zval *entry;
1741
1742    hash = HASH_OF(var);
1743
1744    bind = emalloc(sizeof(php_oci_bind));
1745    bind->array.elements        = (double *)safe_emalloc(max_table_length, sizeof(double), 0);
1746    bind->array.current_length  = zend_hash_num_elements(Z_ARRVAL_P(var));
1747    bind->array.old_length      = bind->array.current_length;
1748    bind->array.max_length      = sizeof(double);
1749    bind->array.element_lengths = safe_emalloc(max_table_length, sizeof(ub2), 0);
1750    memset(bind->array.element_lengths, 0, max_table_length * sizeof(ub2));
1751    bind->array.indicators      = NULL;
1752
1753    zend_hash_internal_pointer_reset(hash);
1754    for (i = 0; i < max_table_length; i++) {
1755        if (i < bind->array.current_length) {
1756            bind->array.element_lengths[i] = sizeof(double);
1757        }
1758        if ((i < bind->array.current_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
1759            convert_to_double_ex(entry);
1760            ((double *)bind->array.elements)[i] = (double) Z_DVAL_P(entry);
1761            zend_hash_move_forward(hash);
1762        } else {
1763            ((double *)bind->array.elements)[i] = 0;
1764        }
1765    }
1766    zend_hash_internal_pointer_reset(hash);
1767
1768    return bind;
1769}
1770/* }}} */
1771
1772/* {{{ php_oci_bind_array_helper_date()
1773 Bind arrays to PL/SQL types */
1774php_oci_bind *php_oci_bind_array_helper_date(zval *var, zend_long max_table_length, php_oci_connection *connection)
1775{
1776    php_oci_bind *bind;
1777    ub4 i;
1778    HashTable *hash;
1779    zval *entry;
1780    sword errstatus;
1781
1782    hash = HASH_OF(var);
1783
1784    bind = emalloc(sizeof(php_oci_bind));
1785    bind->array.elements        = (OCIDate *)safe_emalloc(max_table_length, sizeof(OCIDate), 0);
1786    bind->array.current_length  = zend_hash_num_elements(Z_ARRVAL_P(var));
1787    bind->array.old_length      = bind->array.current_length;
1788    bind->array.max_length      = sizeof(OCIDate);
1789    bind->array.element_lengths = safe_emalloc(max_table_length, sizeof(ub2), 0);
1790    memset(bind->array.element_lengths, 0, max_table_length * sizeof(ub2));
1791    bind->array.indicators      = NULL;
1792
1793    zend_hash_internal_pointer_reset(hash);
1794    for (i = 0; i < max_table_length; i++) {
1795        OCIDate oci_date;
1796        if (i < bind->array.current_length) {
1797            bind->array.element_lengths[i] = sizeof(OCIDate);
1798        }
1799        if ((i < bind->array.current_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
1800
1801            convert_to_string_ex(entry);
1802            PHP_OCI_CALL_RETURN(errstatus, OCIDateFromText, (connection->err, (CONST text *)Z_STRVAL_P(entry), Z_STRLEN_P(entry), NULL, 0, NULL, 0, &oci_date));
1803
1804            if (errstatus != OCI_SUCCESS) {
1805                /* failed to convert string to date */
1806                efree(bind->array.element_lengths);
1807                efree(bind->array.elements);
1808                efree(bind);
1809                connection->errcode = php_oci_error(connection->err, errstatus);
1810                PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
1811                return NULL;
1812            }
1813
1814            ((OCIDate *)bind->array.elements)[i] = oci_date;
1815            zend_hash_move_forward(hash);
1816        } else {
1817            PHP_OCI_CALL_RETURN(errstatus, OCIDateFromText, (connection->err, (CONST text *)"01-JAN-00", sizeof("01-JAN-00")-1, NULL, 0, NULL, 0, &oci_date));
1818
1819            if (errstatus != OCI_SUCCESS) {
1820                /* failed to convert string to date */
1821                efree(bind->array.element_lengths);
1822                efree(bind->array.elements);
1823                efree(bind);
1824                connection->errcode = php_oci_error(connection->err, errstatus);
1825                PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
1826                return NULL;
1827            }
1828
1829            ((OCIDate *)bind->array.elements)[i] = oci_date;
1830        }
1831        connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
1832    }
1833    zend_hash_internal_pointer_reset(hash);
1834
1835    return bind;
1836}
1837/* }}} */
1838
1839#endif /* HAVE_OCI8 */
1840
1841/*
1842 * Local variables:
1843 * tab-width: 4
1844 * c-basic-offset: 4
1845 * End:
1846 * vim600: noet sw=4 ts=4 fdm=marker
1847 * vim<600: noet sw=4 ts=4
1848 */
1849