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  | Author: Wez Furlong <wez@php.net>                                    |
16  +----------------------------------------------------------------------+
17*/
18
19/* $Id$ */
20
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include "php.h"
26#include "php_ini.h"
27#include "ext/standard/info.h"
28#include "pdo/php_pdo.h"
29#include "pdo/php_pdo_driver.h"
30#include "php_pdo_oci.h"
31#include "php_pdo_oci_int.h"
32#include "Zend/zend_extensions.h"
33
34#define PDO_OCI_LOBMAXSIZE (4294967295UL) /* OCI_LOBMAXSIZE */
35
36#define STMT_CALL(name, params)                                         \
37    do {                                                                \
38        S->last_err = name params;                                      \
39        S->last_err = _oci_error(S->err, stmt->dbh, stmt, #name, S->last_err, FALSE, __FILE__, __LINE__); \
40        if (S->last_err) {                                              \
41            return 0;                                                   \
42        }                                                               \
43    } while(0)
44
45#define STMT_CALL_MSG(name, msg, params)                                \
46    do {                                                                \
47        S->last_err = name params;                                      \
48        S->last_err = _oci_error(S->err, stmt->dbh, stmt, #name ": " #msg, S->last_err, FALSE, __FILE__, __LINE__); \
49        if (S->last_err) {                                              \
50            return 0;                                                   \
51        }                                                               \
52    } while(0)
53
54static php_stream *oci_create_lob_stream(pdo_stmt_t *stmt, OCILobLocator *lob);
55
56static int oci_stmt_dtor(pdo_stmt_t *stmt) /* {{{ */
57{
58    pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data;
59    HashTable *BC = stmt->bound_columns;
60    HashTable *BP = stmt->bound_params;
61
62    int i;
63
64    if (S->stmt) {
65        /* cancel server side resources for the statement if we didn't
66         * fetch it all */
67        OCIStmtFetch(S->stmt, S->err, 0, OCI_FETCH_NEXT, OCI_DEFAULT);
68
69        /* free the handle */
70        OCIHandleFree(S->stmt, OCI_HTYPE_STMT);
71        S->stmt = NULL;
72    }
73    if (S->err) {
74        OCIHandleFree(S->err, OCI_HTYPE_ERROR);
75        S->err = NULL;
76    }
77
78    /* need to ensure these go away now */
79    if (BC) {
80        zend_hash_destroy(BC);
81        FREE_HASHTABLE(stmt->bound_columns);
82        stmt->bound_columns = NULL;
83    }
84
85    if (BP) {
86        zend_hash_destroy(BP);
87        FREE_HASHTABLE(stmt->bound_params);
88        stmt->bound_params = NULL;
89    }
90
91    if (S->einfo.errmsg) {
92        pefree(S->einfo.errmsg, stmt->dbh->is_persistent);
93        S->einfo.errmsg = NULL;
94    }
95
96    if (S->cols) {
97        for (i = 0; i < stmt->column_count; i++) {
98            if (S->cols[i].data) {
99                switch (S->cols[i].dtype) {
100                    case SQLT_BLOB:
101                    case SQLT_CLOB:
102                        OCIDescriptorFree(S->cols[i].data, OCI_DTYPE_LOB);
103                        break;
104                    default:
105                        efree(S->cols[i].data);
106                }
107            }
108        }
109        efree(S->cols);
110        S->cols = NULL;
111    }
112    efree(S);
113
114    stmt->driver_data = NULL;
115
116    return 1;
117} /* }}} */
118
119static int oci_stmt_execute(pdo_stmt_t *stmt) /* {{{ */
120{
121    pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data;
122    ub4 rowcount;
123    b4 mode;
124
125    if (!S->stmt_type) {
126        STMT_CALL_MSG(OCIAttrGet, "OCI_ATTR_STMT_TYPE",
127                (S->stmt, OCI_HTYPE_STMT, &S->stmt_type, 0, OCI_ATTR_STMT_TYPE, S->err));
128    }
129
130    if (stmt->executed) {
131        /* ensure that we cancel the cursor from a previous fetch */
132        OCIStmtFetch(S->stmt, S->err, 0, OCI_FETCH_NEXT, OCI_DEFAULT);
133    }
134
135#ifdef OCI_STMT_SCROLLABLE_READONLY /* needed for oci8 ? */
136    if (S->exec_type == OCI_STMT_SCROLLABLE_READONLY) {
137        mode = OCI_STMT_SCROLLABLE_READONLY;
138    } else
139#endif
140    if (stmt->dbh->auto_commit && !stmt->dbh->in_txn) {
141        mode = OCI_COMMIT_ON_SUCCESS;
142    } else {
143        mode = OCI_DEFAULT;
144    }
145
146    STMT_CALL(OCIStmtExecute, (S->H->svc, S->stmt, S->err,
147                (S->stmt_type == OCI_STMT_SELECT && !S->have_blobs) ? 0 : 1, 0, NULL, NULL,
148                mode));
149
150    if (!stmt->executed) {
151        ub4 colcount;
152        /* do first-time-only definition of bind/mapping stuff */
153
154        /* how many columns do we have ? */
155        STMT_CALL_MSG(OCIAttrGet, "ATTR_PARAM_COUNT",
156                (S->stmt, OCI_HTYPE_STMT, &colcount, 0, OCI_ATTR_PARAM_COUNT, S->err));
157
158        stmt->column_count = (int)colcount;
159
160        if (S->cols) {
161            int i;
162            for (i = 0; i < stmt->column_count; i++) {
163                if (S->cols[i].data) {
164                    switch (S->cols[i].dtype) {
165                        case SQLT_BLOB:
166                        case SQLT_CLOB:
167                            /* do nothing */
168                            break;
169                        default:
170                            efree(S->cols[i].data);
171                    }
172                }
173            }
174            efree(S->cols);
175        }
176
177        S->cols = ecalloc(colcount, sizeof(pdo_oci_column));
178    }
179
180    STMT_CALL_MSG(OCIAttrGet, "ATTR_ROW_COUNT",
181            (S->stmt, OCI_HTYPE_STMT, &rowcount, 0, OCI_ATTR_ROW_COUNT, S->err));
182    stmt->row_count = (long)rowcount;
183
184    return 1;
185} /* }}} */
186
187static sb4 oci_bind_input_cb(dvoid *ctx, OCIBind *bindp, ub4 iter, ub4 index, dvoid **bufpp, ub4 *alenp, ub1 *piecep, dvoid **indpp) /* {{{ */
188{
189    struct pdo_bound_param_data *param = (struct pdo_bound_param_data*)ctx;
190    pdo_oci_bound_param *P = (pdo_oci_bound_param*)param->driver_data;
191
192    if (!param || !param->parameter) {
193        php_error_docref(NULL, E_WARNING, "param is NULL in oci_bind_input_cb; this should not happen");
194        return OCI_ERROR;
195    }
196
197    *indpp = &P->indicator;
198
199    if (P->thing) {
200        *bufpp = P->thing;
201        *alenp = sizeof(void*);
202    } else if (ZVAL_IS_NULL(param->parameter)) {
203        /* insert a NULL value into the column */
204        P->indicator = -1; /* NULL */
205        *bufpp = 0;
206        *alenp = -1;
207    } else if (!P->thing) {
208        /* regular string bind */
209        convert_to_string(param->parameter);
210        *bufpp = Z_STRVAL_P(param->parameter);
211        *alenp = Z_STRLEN_P(param->parameter);
212    }
213
214    *piecep = OCI_ONE_PIECE;
215    return OCI_CONTINUE;
216} /* }}} */
217
218static sb4 oci_bind_output_cb(dvoid *ctx, OCIBind *bindp, ub4 iter, ub4 index, dvoid **bufpp, ub4 **alenpp, ub1 *piecep, dvoid **indpp, ub2 **rcodepp) /* {{{ */
219{
220    struct pdo_bound_param_data *param = (struct pdo_bound_param_data*)ctx;
221    pdo_oci_bound_param *P = (pdo_oci_bound_param*)param->driver_data;
222
223    if (!param || !param->parameter) {
224        php_error_docref(NULL, E_WARNING, "param is NULL in oci_bind_output_cb; this should not happen");
225        return OCI_ERROR;
226    }
227
228    if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
229        P->actual_len = sizeof(OCILobLocator*);
230        *bufpp = P->thing;
231        *alenpp = &P->actual_len;
232        *piecep = OCI_ONE_PIECE;
233        *rcodepp = &P->retcode;
234        *indpp = &P->indicator;
235        return OCI_CONTINUE;
236    }
237
238    if (Z_TYPE_P(param->parameter) == IS_OBJECT || Z_TYPE_P(param->parameter) == IS_RESOURCE) {
239        return OCI_CONTINUE;
240    }
241
242    convert_to_string(param->parameter);
243    zval_dtor(param->parameter);
244
245    Z_STRLEN_P(param->parameter) = param->max_value_len;
246    Z_STRVAL_P(param->parameter) = ecalloc(1, Z_STRLEN_P(param->parameter)+1);
247    P->used_for_output = 1;
248
249    P->actual_len = Z_STRLEN_P(param->parameter);
250    *alenpp = &P->actual_len;
251    *bufpp = Z_STRVAL_P(param->parameter);
252    *piecep = OCI_ONE_PIECE;
253    *rcodepp = &P->retcode;
254    *indpp = &P->indicator;
255
256    return OCI_CONTINUE;
257} /* }}} */
258
259static int oci_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param, enum pdo_param_event event_type) /* {{{ */
260{
261    pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data;
262
263    /* we're only interested in parameters for prepared SQL right now */
264    if (param->is_param) {
265        pdo_oci_bound_param *P;
266        sb4 value_sz = -1;
267
268        P = (pdo_oci_bound_param*)param->driver_data;
269
270        switch (event_type) {
271            case PDO_PARAM_EVT_FETCH_PRE:
272            case PDO_PARAM_EVT_FETCH_POST:
273            case PDO_PARAM_EVT_NORMALIZE:
274                /* Do nothing */
275                break;
276
277            case PDO_PARAM_EVT_FREE:
278                P = param->driver_data;
279                if (P) {
280                    efree(P);
281                }
282                break;
283
284            case PDO_PARAM_EVT_ALLOC:
285                P = (pdo_oci_bound_param*)ecalloc(1, sizeof(pdo_oci_bound_param));
286                param->driver_data = P;
287
288                /* figure out what we're doing */
289                switch (PDO_PARAM_TYPE(param->param_type)) {
290                    case PDO_PARAM_STMT:
291                        return 0;
292
293                    case PDO_PARAM_LOB:
294                        /* P->thing is now an OCILobLocator * */
295                        P->oci_type = SQLT_BLOB;
296                        value_sz = sizeof(OCILobLocator*);
297                        break;
298
299                    case PDO_PARAM_STR:
300                    default:
301                        P->oci_type = SQLT_CHR;
302                        value_sz = param->max_value_len;
303                        if (param->max_value_len == 0) {
304                            value_sz = 1332; /* maximum size before value is interpreted as a LONG value */
305                        }
306
307                }
308
309                if (param->name) {
310                    STMT_CALL(OCIBindByName, (S->stmt,
311                            &P->bind, S->err, (text*)param->name,
312                            param->namelen, 0, value_sz, P->oci_type,
313                            &P->indicator, 0, &P->retcode, 0, 0,
314                            OCI_DATA_AT_EXEC));
315                } else {
316                    STMT_CALL(OCIBindByPos, (S->stmt,
317                            &P->bind, S->err, param->paramno+1,
318                            0, value_sz, P->oci_type,
319                            &P->indicator, 0, &P->retcode, 0, 0,
320                            OCI_DATA_AT_EXEC));
321                }
322
323                STMT_CALL(OCIBindDynamic, (P->bind,
324                            S->err,
325                            param, oci_bind_input_cb,
326                            param, oci_bind_output_cb));
327
328                return 1;
329
330            case PDO_PARAM_EVT_EXEC_PRE:
331                P->indicator = 0;
332                P->used_for_output = 0;
333                if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
334                    ub4 empty = 0;
335                    STMT_CALL(OCIDescriptorAlloc, (S->H->env, &P->thing, OCI_DTYPE_LOB, 0, NULL));
336                    STMT_CALL(OCIAttrSet, (P->thing, OCI_DTYPE_LOB, &empty, 0, OCI_ATTR_LOBEMPTY, S->err));
337                    S->have_blobs = 1;
338                }
339                return 1;
340
341            case PDO_PARAM_EVT_EXEC_POST:
342                /* fixup stuff set in motion in oci_bind_output_cb */
343                if (P->used_for_output) {
344                    if (P->indicator == -1) {
345                        /* set up a NULL value */
346                        if (Z_TYPE_P(param->parameter) == IS_STRING
347#if ZEND_EXTENSION_API_NO < 220040718
348                                && Z_STRVAL_P(param->parameter) != empty_string
349#endif
350                           ) {
351                            /* OCI likes to stick non-terminated strings in things */
352                            *Z_STRVAL_P(param->parameter) = '\0';
353                        }
354                        zval_dtor(param->parameter);
355                        ZVAL_NULL(param->parameter);
356                    } else if (Z_TYPE_P(param->parameter) == IS_STRING
357#if ZEND_EXTENSION_API_NO < 220040718
358                            && Z_STRVAL_P(param->parameter) != empty_string
359#endif
360                            ) {
361                        Z_STRLEN_P(param->parameter) = P->actual_len;
362                        Z_STRVAL_P(param->parameter) = erealloc(Z_STRVAL_P(param->parameter), P->actual_len+1);
363                        Z_STRVAL_P(param->parameter)[P->actual_len] = '\0';
364                    }
365                } else if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB && P->thing) {
366                    php_stream *stm;
367
368                    if (Z_TYPE_P(param->parameter) == IS_NULL) {
369                        /* if the param is NULL, then we assume that they
370                         * wanted to bind a lob locator into it from the query
371                         * */
372
373                        stm = oci_create_lob_stream(stmt, (OCILobLocator*)P->thing);
374                        if (stm) {
375                            OCILobOpen(S->H->svc, S->err, (OCILobLocator*)P->thing, OCI_LOB_READWRITE);
376                            php_stream_to_zval(stm, param->parameter);
377                            P->thing = NULL;
378                        }
379                    } else {
380                        /* we're a LOB being used for insert; transfer the data now */
381                        size_t n;
382                        ub4 amt, offset = 1;
383                        char *consume;
384
385                        php_stream_from_zval_no_verify(stm, &param->parameter);
386                        if (stm) {
387                            OCILobOpen(S->H->svc, S->err, (OCILobLocator*)P->thing, OCI_LOB_READWRITE);
388                            do {
389                                char buf[8192];
390                                n = php_stream_read(stm, buf, sizeof(buf));
391                                if ((int)n <= 0) {
392                                    break;
393                                }
394                                consume = buf;
395                                do {
396                                    amt = n;
397                                    OCILobWrite(S->H->svc, S->err, (OCILobLocator*)P->thing,
398                                            &amt, offset, consume, n,
399                                            OCI_ONE_PIECE,
400                                            NULL, NULL, 0, SQLCS_IMPLICIT);
401                                    offset += amt;
402                                    n -= amt;
403                                    consume += amt;
404                                } while (n);
405                            } while (1);
406                            OCILobClose(S->H->svc, S->err, (OCILobLocator*)P->thing);
407                            OCILobFlushBuffer(S->H->svc, S->err, (OCILobLocator*)P->thing, 0);
408                        } else if (Z_TYPE_P(param->parameter) == IS_STRING) {
409                            /* stick the string into the LOB */
410                            consume = Z_STRVAL_P(param->parameter);
411                            n = Z_STRLEN_P(param->parameter);
412                            if (n) {
413                                OCILobOpen(S->H->svc, S->err, (OCILobLocator*)P->thing, OCI_LOB_READWRITE);
414                                while (n) {
415                                    amt = n;
416                                    OCILobWrite(S->H->svc, S->err, (OCILobLocator*)P->thing,
417                                            &amt, offset, consume, n,
418                                            OCI_ONE_PIECE,
419                                            NULL, NULL, 0, SQLCS_IMPLICIT);
420                                    consume += amt;
421                                    n -= amt;
422                                }
423                                OCILobClose(S->H->svc, S->err, (OCILobLocator*)P->thing);
424                            }
425                        }
426                        OCIDescriptorFree(P->thing, OCI_DTYPE_LOB);
427                        P->thing = NULL;
428                    }
429                }
430
431                return 1;
432        }
433    }
434
435    return 1;
436} /* }}} */
437
438static int oci_stmt_fetch(pdo_stmt_t *stmt, enum pdo_fetch_orientation ori, long offset) /* {{{ */
439{
440#if HAVE_OCISTMTFETCH2
441    ub4 ociori;
442#endif
443    pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data;
444
445#if HAVE_OCISTMTFETCH2
446    switch (ori) {
447        case PDO_FETCH_ORI_NEXT:    ociori = OCI_FETCH_NEXT; break;
448        case PDO_FETCH_ORI_PRIOR:   ociori = OCI_FETCH_PRIOR; break;
449        case PDO_FETCH_ORI_FIRST:   ociori = OCI_FETCH_FIRST; break;
450        case PDO_FETCH_ORI_LAST:    ociori = OCI_FETCH_LAST; break;
451        case PDO_FETCH_ORI_ABS:     ociori = OCI_FETCH_ABSOLUTE; break;
452        case PDO_FETCH_ORI_REL:     ociori = OCI_FETCH_RELATIVE; break;
453    }
454    S->last_err = OCIStmtFetch2(S->stmt, S->err, 1, ociori, offset, OCI_DEFAULT);
455#else
456    S->last_err = OCIStmtFetch(S->stmt, S->err, 1, OCI_FETCH_NEXT, OCI_DEFAULT);
457#endif
458
459    if (S->last_err == OCI_NO_DATA) {
460        /* no (more) data */
461        return 0;
462    }
463
464    if (S->last_err == OCI_NEED_DATA) {
465        oci_stmt_error("OCI_NEED_DATA");
466        return 0;
467    }
468
469    if (S->last_err == OCI_SUCCESS_WITH_INFO || S->last_err == OCI_SUCCESS) {
470        return 1;
471    }
472
473    oci_stmt_error("OCIStmtFetch");
474
475    return 0;
476} /* }}} */
477
478static sb4 oci_define_callback(dvoid *octxp, OCIDefine *define, ub4 iter, dvoid **bufpp,
479        ub4 **alenpp, ub1 *piecep, dvoid **indpp, ub2 **rcodepp)
480{
481    pdo_oci_column *col = (pdo_oci_column*)octxp;
482
483    switch (col->dtype) {
484        case SQLT_BLOB:
485        case SQLT_CLOB:
486            *piecep = OCI_ONE_PIECE;
487            *bufpp = col->data;
488            *alenpp = &col->datalen;
489            *indpp = (dvoid *)&col->indicator;
490            break;
491
492        default:
493            php_error_docref(NULL, E_WARNING,
494                "unhandled datatype in oci_define_callback; this should not happen");
495            return OCI_ERROR;
496    }
497
498    return OCI_CONTINUE;
499}
500
501static int oci_stmt_describe(pdo_stmt_t *stmt, int colno) /* {{{ */
502{
503    pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data;
504    OCIParam *param = NULL;
505    text *colname;
506    ub2 dtype, data_size, scale, precis;
507    ub4 namelen;
508    struct pdo_column_data *col = &stmt->columns[colno];
509    zend_bool dyn = FALSE;
510
511    /* describe the column */
512    STMT_CALL(OCIParamGet, (S->stmt, OCI_HTYPE_STMT, S->err, (dvoid*)&param, colno+1));
513
514    /* what type ? */
515    STMT_CALL_MSG(OCIAttrGet, "OCI_ATTR_DATA_TYPE",
516            (param, OCI_DTYPE_PARAM, &dtype, 0, OCI_ATTR_DATA_TYPE, S->err));
517
518    /* how big ? */
519    STMT_CALL_MSG(OCIAttrGet, "OCI_ATTR_DATA_SIZE",
520            (param, OCI_DTYPE_PARAM, &data_size, 0, OCI_ATTR_DATA_SIZE, S->err));
521
522    /* scale ? */
523    STMT_CALL_MSG(OCIAttrGet, "OCI_ATTR_SCALE",
524            (param, OCI_DTYPE_PARAM, &scale, 0, OCI_ATTR_SCALE, S->err));
525
526    /* precision ? */
527    STMT_CALL_MSG(OCIAttrGet, "OCI_ATTR_PRECISION",
528            (param, OCI_DTYPE_PARAM, &precis, 0, OCI_ATTR_PRECISION, S->err));
529
530    /* name ? */
531    STMT_CALL_MSG(OCIAttrGet, "OCI_ATTR_NAME",
532            (param, OCI_DTYPE_PARAM, &colname, &namelen, OCI_ATTR_NAME, S->err));
533
534    col->precision = scale;
535    col->maxlen = data_size;
536    col->namelen = namelen;
537    col->name = estrndup((char *)colname, namelen);
538
539    S->cols[colno].dtype = dtype;
540
541    /* how much room do we need to store the field */
542    switch (dtype) {
543        case SQLT_LBI:
544        case SQLT_LNG:
545            if (dtype == SQLT_LBI) {
546                dtype = SQLT_BIN;
547            } else {
548                dtype = SQLT_CHR;
549            }
550            S->cols[colno].datalen = 512; /* XXX should be INT_MAX and fetched by pieces */
551            S->cols[colno].data = emalloc(S->cols[colno].datalen + 1);
552            col->param_type = PDO_PARAM_STR;
553            break;
554
555        case SQLT_BLOB:
556        case SQLT_CLOB:
557            col->param_type = PDO_PARAM_LOB;
558            STMT_CALL(OCIDescriptorAlloc, (S->H->env, (dvoid**)&S->cols[colno].data, OCI_DTYPE_LOB, 0, NULL));
559            S->cols[colno].datalen = sizeof(OCILobLocator*);
560            dyn = TRUE;
561            break;
562
563        case SQLT_BIN:
564        default:
565            if (dtype == SQLT_DAT || dtype == SQLT_NUM || dtype == SQLT_RDD
566#ifdef SQLT_TIMESTAMP
567                    || dtype == SQLT_TIMESTAMP
568#endif
569#ifdef SQLT_TIMESTAMP_TZ
570                    || dtype == SQLT_TIMESTAMP_TZ
571#endif
572                    ) {
573                /* should be big enough for most date formats and numbers */
574                S->cols[colno].datalen = 512;
575#if defined(SQLT_IBFLOAT) && defined(SQLT_IBDOUBLE)
576            } else if (dtype == SQLT_IBFLOAT || dtype == SQLT_IBDOUBLE) {
577                S->cols[colno].datalen = 1024;
578#endif
579            } else {
580                S->cols[colno].datalen = col->maxlen;
581            }
582            if (dtype == SQLT_BIN) {
583                S->cols[colno].datalen *= 3;
584            }
585            S->cols[colno].data = emalloc(S->cols[colno].datalen + 1);
586            dtype = SQLT_CHR;
587
588            /* returning data as a string */
589            col->param_type = PDO_PARAM_STR;
590    }
591
592    STMT_CALL(OCIDefineByPos, (S->stmt, &S->cols[colno].def, S->err, colno+1,
593                S->cols[colno].data, S->cols[colno].datalen, dtype, &S->cols[colno].indicator,
594                &S->cols[colno].fetched_len, &S->cols[colno].retcode, dyn ? OCI_DYNAMIC_FETCH : OCI_DEFAULT));
595
596    if (dyn) {
597        STMT_CALL(OCIDefineDynamic, (S->cols[colno].def, S->err, &S->cols[colno],
598                oci_define_callback));
599    }
600
601    return 1;
602} /* }}} */
603
604struct oci_lob_self {
605    pdo_stmt_t *stmt;
606    pdo_oci_stmt *S;
607    OCILobLocator *lob;
608    ub4 offset;
609};
610
611static size_t oci_blob_write(php_stream *stream, const char *buf, size_t count)
612{
613    struct oci_lob_self *self = (struct oci_lob_self*)stream->abstract;
614    ub4 amt;
615    sword r;
616
617    amt = count;
618    r = OCILobWrite(self->S->H->svc, self->S->err, self->lob,
619        &amt, self->offset, (char*)buf, count,
620        OCI_ONE_PIECE,
621        NULL, NULL, 0, SQLCS_IMPLICIT);
622
623    if (r != OCI_SUCCESS) {
624        return (size_t)-1;
625    }
626
627    self->offset += amt;
628    return amt;
629}
630
631static size_t oci_blob_read(php_stream *stream, char *buf, size_t count)
632{
633    struct oci_lob_self *self = (struct oci_lob_self*)stream->abstract;
634    ub4 amt;
635    sword r;
636
637    amt = count;
638    r = OCILobRead(self->S->H->svc, self->S->err, self->lob,
639        &amt, self->offset, buf, count,
640        NULL, NULL, 0, SQLCS_IMPLICIT);
641
642    if (r != OCI_SUCCESS && r != OCI_NEED_DATA) {
643        return (size_t)-1;
644    }
645
646    self->offset += amt;
647    if (amt < count) {
648        stream->eof = 1;
649    }
650    return amt;
651}
652
653static int oci_blob_close(php_stream *stream, int close_handle)
654{
655    struct oci_lob_self *self = (struct oci_lob_self*)stream->abstract;
656    pdo_stmt_t *stmt = self->stmt;
657
658    if (close_handle) {
659        OCILobClose(self->S->H->svc, self->S->err, self->lob);
660        efree(self);
661    }
662
663    php_pdo_stmt_delref(stmt);
664    return 0;
665}
666
667static int oci_blob_flush(php_stream *stream)
668{
669    struct oci_lob_self *self = (struct oci_lob_self*)stream->abstract;
670    OCILobFlushBuffer(self->S->H->svc, self->S->err, self->lob, 0);
671    return 0;
672}
673
674static int oci_blob_seek(php_stream *stream, off_t offset, int whence, off_t *newoffset)
675{
676    struct oci_lob_self *self = (struct oci_lob_self*)stream->abstract;
677
678    if (offset >= PDO_OCI_LOBMAXSIZE) {
679        return -1;
680    } else {
681        self->offset = offset + 1;  /* Oracle LOBS are 1-based, but PHP is 0-based */
682        return 0;
683    }
684}
685
686static php_stream_ops oci_blob_stream_ops = {
687    oci_blob_write,
688    oci_blob_read,
689    oci_blob_close,
690    oci_blob_flush,
691    "pdo_oci blob stream",
692    oci_blob_seek,
693    NULL,
694    NULL,
695    NULL
696};
697
698static php_stream *oci_create_lob_stream(pdo_stmt_t *stmt, OCILobLocator *lob)
699{
700    php_stream *stm;
701    struct oci_lob_self *self = ecalloc(1, sizeof(*self));
702    self->lob = lob;
703    self->offset = 1; /* 1-based */
704    self->stmt = stmt;
705    self->S = (pdo_oci_stmt*)stmt->driver_data;
706
707    stm = php_stream_alloc(&oci_blob_stream_ops, self, 0, "r+b");
708
709    if (stm) {
710        php_pdo_stmt_addref(stmt);
711        return stm;
712    }
713
714    efree(self);
715    return NULL;
716}
717
718static int oci_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees) /* {{{ */
719{
720    pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data;
721    pdo_oci_column *C = &S->cols[colno];
722
723    /* check the indicator to ensure that the data is intact */
724    if (C->indicator == -1) {
725        /* A NULL value */
726        *ptr = NULL;
727        *len = 0;
728        return 1;
729    } else if (C->indicator == 0) {
730        /* it was stored perfectly */
731
732        if (C->dtype == SQLT_BLOB || C->dtype == SQLT_CLOB) {
733            if (C->data) {
734                *ptr = (char*)oci_create_lob_stream(stmt, (OCILobLocator*)C->data);
735                OCILobOpen(S->H->svc, S->err, (OCILobLocator*)C->data, OCI_LOB_READONLY);
736            }
737            *len = 0;
738            return *ptr ? 1 : 0;
739        }
740
741        *ptr = C->data;
742        *len = C->fetched_len;
743        return 1;
744    } else {
745        /* it was truncated */
746        php_error_docref(NULL, E_WARNING, "column %d data was too large for buffer and was truncated to fit it", colno);
747
748        *ptr = C->data;
749        *len = C->fetched_len;
750        return 1;
751    }
752} /* }}} */
753
754struct pdo_stmt_methods oci_stmt_methods = {
755    oci_stmt_dtor,
756    oci_stmt_execute,
757    oci_stmt_fetch,
758    oci_stmt_describe,
759    oci_stmt_get_col,
760    oci_stmt_param_hook
761};
762
763/*
764 * Local variables:
765 * tab-width: 4
766 * c-basic-offset: 4
767 * End:
768 * vim600: noet sw=4 ts=4 fdm=marker
769 * vim<600: noet sw=4 ts=4
770 */
771