1/*
2  +----------------------------------------------------------------------+
3  | PHP Version 7                                                        |
4  +----------------------------------------------------------------------+
5  | Copyright (c) 1997-2015 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) {
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(param->parameter);
211        *alenp = Z_STRLEN(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) {
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(param->parameter) == IS_OBJECT || Z_TYPE(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(param->parameter) = param->max_value_len;
246    Z_STR(param->parameter) = ecalloc(1, Z_STRLEN(param->parameter)+1);
247    P->used_for_output = 1;
248
249    P->actual_len = Z_STRLEN(param->parameter);
250    *alenpp = &P->actual_len;
251    *bufpp = Z_STRVAL(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->val,
312                            param->name->len, 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(param->parameter) == IS_STRING) {
347                            /* OCI likes to stick non-terminated strings in things */
348                            *Z_STRVAL(param->parameter) = '\0';
349                        }
350                        zval_dtor(&param->parameter);
351                        ZVAL_UNDEF(&param->parameter);
352                    } else if (Z_TYPE(param->parameter) == IS_STRING) {
353                        Z_STRLEN(param->parameter) = P->actual_len;
354                        Z_STR(param->parameter) = erealloc(Z_STRVAL(param->parameter), P->actual_len+1);
355                        Z_STRVAL(param->parameter)[P->actual_len] = '\0';
356                    }
357                } else if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB && P->thing) {
358                    php_stream *stm;
359
360                    if (Z_TYPE(param->parameter) == IS_NULL) {
361                        /* if the param is NULL, then we assume that they
362                         * wanted to bind a lob locator into it from the query
363                         * */
364
365                        stm = oci_create_lob_stream(stmt, (OCILobLocator*)P->thing);
366                        if (stm) {
367                            OCILobOpen(S->H->svc, S->err, (OCILobLocator*)P->thing, OCI_LOB_READWRITE);
368                            php_stream_to_zval(stm, &param->parameter);
369                            P->thing = NULL;
370                        }
371                    } else {
372                        /* we're a LOB being used for insert; transfer the data now */
373                        size_t n;
374                        ub4 amt, offset = 1;
375                        char *consume;
376
377                        php_stream_from_zval_no_verify(stm, &param->parameter);
378                        if (stm) {
379                            OCILobOpen(S->H->svc, S->err, (OCILobLocator*)P->thing, OCI_LOB_READWRITE);
380                            do {
381                                char buf[8192];
382                                n = php_stream_read(stm, buf, sizeof(buf));
383                                if ((int)n <= 0) {
384                                    break;
385                                }
386                                consume = buf;
387                                do {
388                                    amt = n;
389                                    OCILobWrite(S->H->svc, S->err, (OCILobLocator*)P->thing,
390                                            &amt, offset, consume, n,
391                                            OCI_ONE_PIECE,
392                                            NULL, NULL, 0, SQLCS_IMPLICIT);
393                                    offset += amt;
394                                    n -= amt;
395                                    consume += amt;
396                                } while (n);
397                            } while (1);
398                            OCILobClose(S->H->svc, S->err, (OCILobLocator*)P->thing);
399                            OCILobFlushBuffer(S->H->svc, S->err, (OCILobLocator*)P->thing, 0);
400                        } else if (Z_TYPE(param->parameter) == IS_STRING) {
401                            /* stick the string into the LOB */
402                            consume = Z_STRVAL(param->parameter);
403                            n = Z_STRLEN(param->parameter);
404                            if (n) {
405                                OCILobOpen(S->H->svc, S->err, (OCILobLocator*)P->thing, OCI_LOB_READWRITE);
406                                while (n) {
407                                    amt = n;
408                                    OCILobWrite(S->H->svc, S->err, (OCILobLocator*)P->thing,
409                                            &amt, offset, consume, n,
410                                            OCI_ONE_PIECE,
411                                            NULL, NULL, 0, SQLCS_IMPLICIT);
412                                    consume += amt;
413                                    n -= amt;
414                                }
415                                OCILobClose(S->H->svc, S->err, (OCILobLocator*)P->thing);
416                            }
417                        }
418                        OCIDescriptorFree(P->thing, OCI_DTYPE_LOB);
419                        P->thing = NULL;
420                    }
421                }
422
423                return 1;
424        }
425    }
426
427    return 1;
428} /* }}} */
429
430static int oci_stmt_fetch(pdo_stmt_t *stmt, enum pdo_fetch_orientation ori, zend_long offset) /* {{{ */
431{
432#if HAVE_OCISTMTFETCH2
433    ub4 ociori;
434#endif
435    pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data;
436
437#if HAVE_OCISTMTFETCH2
438    switch (ori) {
439        case PDO_FETCH_ORI_NEXT:    ociori = OCI_FETCH_NEXT; break;
440        case PDO_FETCH_ORI_PRIOR:   ociori = OCI_FETCH_PRIOR; break;
441        case PDO_FETCH_ORI_FIRST:   ociori = OCI_FETCH_FIRST; break;
442        case PDO_FETCH_ORI_LAST:    ociori = OCI_FETCH_LAST; break;
443        case PDO_FETCH_ORI_ABS:     ociori = OCI_FETCH_ABSOLUTE; break;
444        case PDO_FETCH_ORI_REL:     ociori = OCI_FETCH_RELATIVE; break;
445    }
446    S->last_err = OCIStmtFetch2(S->stmt, S->err, 1, ociori, offset, OCI_DEFAULT);
447#else
448    S->last_err = OCIStmtFetch(S->stmt, S->err, 1, OCI_FETCH_NEXT, OCI_DEFAULT);
449#endif
450
451    if (S->last_err == OCI_NO_DATA) {
452        /* no (more) data */
453        return 0;
454    }
455
456    if (S->last_err == OCI_NEED_DATA) {
457        oci_stmt_error("OCI_NEED_DATA");
458        return 0;
459    }
460
461    if (S->last_err == OCI_SUCCESS_WITH_INFO || S->last_err == OCI_SUCCESS) {
462        return 1;
463    }
464
465    oci_stmt_error("OCIStmtFetch");
466
467    return 0;
468} /* }}} */
469
470static sb4 oci_define_callback(dvoid *octxp, OCIDefine *define, ub4 iter, dvoid **bufpp,
471        ub4 **alenpp, ub1 *piecep, dvoid **indpp, ub2 **rcodepp)
472{
473    pdo_oci_column *col = (pdo_oci_column*)octxp;
474
475    switch (col->dtype) {
476        case SQLT_BLOB:
477        case SQLT_CLOB:
478            *piecep = OCI_ONE_PIECE;
479            *bufpp = col->data;
480            *alenpp = &col->datalen;
481            *indpp = (dvoid *)&col->indicator;
482            break;
483
484        default:
485            php_error_docref(NULL, E_WARNING,
486                "unhandled datatype in oci_define_callback; this should not happen");
487            return OCI_ERROR;
488    }
489
490    return OCI_CONTINUE;
491}
492
493static int oci_stmt_describe(pdo_stmt_t *stmt, int colno) /* {{{ */
494{
495    pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data;
496    OCIParam *param = NULL;
497    text *colname;
498    ub2 dtype, data_size, scale, precis;
499    ub4 namelen;
500    struct pdo_column_data *col = &stmt->columns[colno];
501    zend_bool dyn = FALSE;
502
503    /* describe the column */
504    STMT_CALL(OCIParamGet, (S->stmt, OCI_HTYPE_STMT, S->err, (dvoid*)&param, colno+1));
505
506    /* what type ? */
507    STMT_CALL_MSG(OCIAttrGet, "OCI_ATTR_DATA_TYPE",
508            (param, OCI_DTYPE_PARAM, &dtype, 0, OCI_ATTR_DATA_TYPE, S->err));
509
510    /* how big ? */
511    STMT_CALL_MSG(OCIAttrGet, "OCI_ATTR_DATA_SIZE",
512            (param, OCI_DTYPE_PARAM, &data_size, 0, OCI_ATTR_DATA_SIZE, S->err));
513
514    /* scale ? */
515    STMT_CALL_MSG(OCIAttrGet, "OCI_ATTR_SCALE",
516            (param, OCI_DTYPE_PARAM, &scale, 0, OCI_ATTR_SCALE, S->err));
517
518    /* precision ? */
519    STMT_CALL_MSG(OCIAttrGet, "OCI_ATTR_PRECISION",
520            (param, OCI_DTYPE_PARAM, &precis, 0, OCI_ATTR_PRECISION, S->err));
521
522    /* name ? */
523    STMT_CALL_MSG(OCIAttrGet, "OCI_ATTR_NAME",
524            (param, OCI_DTYPE_PARAM, &colname, &namelen, OCI_ATTR_NAME, S->err));
525
526    col->precision = scale;
527    col->maxlen = data_size;
528    col->name = zend_string_init((char *)colname, namelen, 0);
529
530    S->cols[colno].dtype = dtype;
531
532    /* how much room do we need to store the field */
533    switch (dtype) {
534        case SQLT_LBI:
535        case SQLT_LNG:
536            if (dtype == SQLT_LBI) {
537                dtype = SQLT_BIN;
538            } else {
539                dtype = SQLT_CHR;
540            }
541            S->cols[colno].datalen = 512; /* XXX should be INT_MAX and fetched by pieces */
542            S->cols[colno].data = emalloc(S->cols[colno].datalen + 1);
543            col->param_type = PDO_PARAM_STR;
544            break;
545
546        case SQLT_BLOB:
547        case SQLT_CLOB:
548            col->param_type = PDO_PARAM_LOB;
549            STMT_CALL(OCIDescriptorAlloc, (S->H->env, (dvoid**)&S->cols[colno].data, OCI_DTYPE_LOB, 0, NULL));
550            S->cols[colno].datalen = sizeof(OCILobLocator*);
551            dyn = TRUE;
552            break;
553
554        case SQLT_BIN:
555        default:
556            if (dtype == SQLT_DAT || dtype == SQLT_NUM || dtype == SQLT_RDD
557#ifdef SQLT_TIMESTAMP
558                    || dtype == SQLT_TIMESTAMP
559#endif
560#ifdef SQLT_TIMESTAMP_TZ
561                    || dtype == SQLT_TIMESTAMP_TZ
562#endif
563                    ) {
564                /* should be big enough for most date formats and numbers */
565                S->cols[colno].datalen = 512;
566#if defined(SQLT_IBFLOAT) && defined(SQLT_IBDOUBLE)
567            } else if (dtype == SQLT_IBFLOAT || dtype == SQLT_IBDOUBLE) {
568                S->cols[colno].datalen = 1024;
569#endif
570            } else {
571                S->cols[colno].datalen = col->maxlen;
572            }
573            if (dtype == SQLT_BIN) {
574                S->cols[colno].datalen *= 3;
575            }
576            S->cols[colno].data = emalloc(S->cols[colno].datalen + 1);
577            dtype = SQLT_CHR;
578
579            /* returning data as a string */
580            col->param_type = PDO_PARAM_STR;
581    }
582
583    STMT_CALL(OCIDefineByPos, (S->stmt, &S->cols[colno].def, S->err, colno+1,
584                S->cols[colno].data, S->cols[colno].datalen, dtype, &S->cols[colno].indicator,
585                &S->cols[colno].fetched_len, &S->cols[colno].retcode, dyn ? OCI_DYNAMIC_FETCH : OCI_DEFAULT));
586
587    if (dyn) {
588        STMT_CALL(OCIDefineDynamic, (S->cols[colno].def, S->err, &S->cols[colno],
589                oci_define_callback));
590    }
591
592    return 1;
593} /* }}} */
594
595struct oci_lob_self {
596    pdo_stmt_t *stmt;
597    pdo_oci_stmt *S;
598    OCILobLocator *lob;
599    ub4 offset;
600};
601
602static size_t oci_blob_write(php_stream *stream, const char *buf, size_t count)
603{
604    struct oci_lob_self *self = (struct oci_lob_self*)stream->abstract;
605    ub4 amt;
606    sword r;
607
608    amt = count;
609    r = OCILobWrite(self->S->H->svc, self->S->err, self->lob,
610        &amt, self->offset, (char*)buf, count,
611        OCI_ONE_PIECE,
612        NULL, NULL, 0, SQLCS_IMPLICIT);
613
614    if (r != OCI_SUCCESS) {
615        return (size_t)-1;
616    }
617
618    self->offset += amt;
619    return amt;
620}
621
622static size_t oci_blob_read(php_stream *stream, char *buf, size_t count)
623{
624    struct oci_lob_self *self = (struct oci_lob_self*)stream->abstract;
625    ub4 amt;
626    sword r;
627
628    amt = count;
629    r = OCILobRead(self->S->H->svc, self->S->err, self->lob,
630        &amt, self->offset, buf, count,
631        NULL, NULL, 0, SQLCS_IMPLICIT);
632
633    if (r != OCI_SUCCESS && r != OCI_NEED_DATA) {
634        return (size_t)-1;
635    }
636
637    self->offset += amt;
638    if (amt < count) {
639        stream->eof = 1;
640    }
641    return amt;
642}
643
644static int oci_blob_close(php_stream *stream, int close_handle)
645{
646    struct oci_lob_self *self = (struct oci_lob_self*)stream->abstract;
647    pdo_stmt_t *stmt = self->stmt;
648
649    if (close_handle) {
650        OCILobClose(self->S->H->svc, self->S->err, self->lob);
651        efree(self);
652    }
653
654    php_pdo_free_statement(stmt);
655    return 0;
656}
657
658static int oci_blob_flush(php_stream *stream)
659{
660    struct oci_lob_self *self = (struct oci_lob_self*)stream->abstract;
661    OCILobFlushBuffer(self->S->H->svc, self->S->err, self->lob, 0);
662    return 0;
663}
664
665static int oci_blob_seek(php_stream *stream, off_t offset, int whence, off_t *newoffset)
666{
667    struct oci_lob_self *self = (struct oci_lob_self*)stream->abstract;
668
669    if (offset >= PDO_OCI_LOBMAXSIZE) {
670        return -1;
671    } else {
672        self->offset = offset + 1;  /* Oracle LOBS are 1-based, but PHP is 0-based */
673        return 0;
674    }
675}
676
677static php_stream_ops oci_blob_stream_ops = {
678    oci_blob_write,
679    oci_blob_read,
680    oci_blob_close,
681    oci_blob_flush,
682    "pdo_oci blob stream",
683    oci_blob_seek,
684    NULL,
685    NULL,
686    NULL
687};
688
689static php_stream *oci_create_lob_stream(pdo_stmt_t *stmt, OCILobLocator *lob)
690{
691    php_stream *stm;
692    struct oci_lob_self *self = ecalloc(1, sizeof(*self));
693    self->lob = lob;
694    self->offset = 1; /* 1-based */
695    self->stmt = stmt;
696    self->S = (pdo_oci_stmt*)stmt->driver_data;
697
698    stm = php_stream_alloc(&oci_blob_stream_ops, self, 0, "r+b");
699
700    if (stm) {
701        return stm;
702    }
703
704    efree(self);
705    return NULL;
706}
707
708static int oci_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees) /* {{{ */
709{
710    pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data;
711    pdo_oci_column *C = &S->cols[colno];
712
713    /* check the indicator to ensure that the data is intact */
714    if (C->indicator == -1) {
715        /* A NULL value */
716        *ptr = NULL;
717        *len = 0;
718        return 1;
719    } else if (C->indicator == 0) {
720        /* it was stored perfectly */
721
722        if (C->dtype == SQLT_BLOB || C->dtype == SQLT_CLOB) {
723            if (C->data) {
724                *ptr = (char*)oci_create_lob_stream(stmt, (OCILobLocator*)C->data);
725                OCILobOpen(S->H->svc, S->err, (OCILobLocator*)C->data, OCI_LOB_READONLY);
726            }
727            *len = 0;
728            return *ptr ? 1 : 0;
729        }
730
731        *ptr = C->data;
732        *len = C->fetched_len;
733        return 1;
734    } else {
735        /* it was truncated */
736        php_error_docref(NULL, E_WARNING, "column %d data was too large for buffer and was truncated to fit it", colno);
737
738        *ptr = C->data;
739        *len = C->fetched_len;
740        return 1;
741    }
742} /* }}} */
743
744struct pdo_stmt_methods oci_stmt_methods = {
745    oci_stmt_dtor,
746    oci_stmt_execute,
747    oci_stmt_fetch,
748    oci_stmt_describe,
749    oci_stmt_get_col,
750    oci_stmt_param_hook
751};
752
753/*
754 * Local variables:
755 * tab-width: 4
756 * c-basic-offset: 4
757 * End:
758 * vim600: noet sw=4 ts=4 fdm=marker
759 * vim<600: noet sw=4 ts=4
760 */
761