xref: /PHP-8.0/ext/pdo_oci/oci_statement.c (revision 9377c305)
1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | http://www.php.net/license/3_01.txt                                  |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | Author: Wez Furlong <wez@php.net>                                    |
14   +----------------------------------------------------------------------+
15 */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include "php.h"
22 #include "php_ini.h"
23 #include "ext/standard/info.h"
24 #include "pdo/php_pdo.h"
25 #include "pdo/php_pdo_driver.h"
26 #include "php_pdo_oci.h"
27 #include "php_pdo_oci_int.h"
28 #include "Zend/zend_extensions.h"
29 
30 #define PDO_OCI_LOBMAXSIZE (4294967295UL) /* OCI_LOBMAXSIZE */
31 
32 #define STMT_CALL(name, params)											\
33 	do {																\
34 		S->last_err = name params;										\
35 		S->last_err = _oci_error(S->err, stmt->dbh, stmt, #name, S->last_err, FALSE, __FILE__, __LINE__); \
36 		if (S->last_err) {												\
37 			return 0;													\
38 		}																\
39 	} while(0)
40 
41 #define STMT_CALL_MSG(name, msg, params)								\
42 	do { 																\
43 		S->last_err = name params;										\
44 		S->last_err = _oci_error(S->err, stmt->dbh, stmt, #name ": " #msg, S->last_err, FALSE, __FILE__, __LINE__); \
45 		if (S->last_err) {												\
46 			return 0;													\
47 		}																\
48 	} while(0)
49 
50 static php_stream *oci_create_lob_stream(zval *dbh, pdo_stmt_t *stmt, OCILobLocator *lob);
51 
52 #define OCI_TEMPLOB_CLOSE(envhp, svchp, errhp, lob)				\
53 	do															\
54 	{															\
55 		boolean isTempLOB;										\
56 		OCILobIsTemporary(envhp, errhp, lob, &isTempLOB);		\
57 		if (isTempLOB)											\
58 			OCILobFreeTemporary(svchp, errhp, lob);				\
59 	} while(0)
60 
oci_stmt_dtor(pdo_stmt_t * stmt)61 static int oci_stmt_dtor(pdo_stmt_t *stmt) /* {{{ */
62 {
63 	pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data;
64 	HashTable *BC = stmt->bound_columns;
65 	HashTable *BP = stmt->bound_params;
66 
67 	int i;
68 
69 	if (S->stmt) {
70 		/* cancel server side resources for the statement if we didn't
71 		 * fetch it all */
72 		OCIStmtFetch(S->stmt, S->err, 0, OCI_FETCH_NEXT, OCI_DEFAULT);
73 
74 		/* free the handle */
75 		OCIHandleFree(S->stmt, OCI_HTYPE_STMT);
76 		S->stmt = NULL;
77 	}
78 	if (S->err) {
79 		OCIHandleFree(S->err, OCI_HTYPE_ERROR);
80 		S->err = NULL;
81 	}
82 
83 	/* need to ensure these go away now */
84 	if (BC) {
85 		zend_hash_destroy(BC);
86 		FREE_HASHTABLE(stmt->bound_columns);
87 		stmt->bound_columns = NULL;
88 	}
89 
90 	if (BP) {
91 		zend_hash_destroy(BP);
92 		FREE_HASHTABLE(stmt->bound_params);
93 		stmt->bound_params = NULL;
94 	}
95 
96 	if (S->einfo.errmsg) {
97 		pefree(S->einfo.errmsg, stmt->dbh->is_persistent);
98 		S->einfo.errmsg = NULL;
99 	}
100 
101 	if (S->cols) {
102 		for (i = 0; i < stmt->column_count; i++) {
103 			if (S->cols[i].data) {
104 				switch (S->cols[i].dtype) {
105 					case SQLT_BLOB:
106 					case SQLT_CLOB:
107 						OCI_TEMPLOB_CLOSE(S->H->env, S->H->svc, S->H->err,
108 							(OCILobLocator *) S->cols[i].data);
109 						OCIDescriptorFree(S->cols[i].data, OCI_DTYPE_LOB);
110 						break;
111 					default:
112 						efree(S->cols[i].data);
113 				}
114 			}
115 		}
116 		efree(S->cols);
117 		S->cols = NULL;
118 	}
119 	efree(S);
120 
121 	stmt->driver_data = NULL;
122 
123 	return 1;
124 } /* }}} */
125 
oci_stmt_execute(pdo_stmt_t * stmt)126 static int oci_stmt_execute(pdo_stmt_t *stmt) /* {{{ */
127 {
128 	pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data;
129 	ub4 rowcount;
130 	b4 mode;
131 
132 	if (!S->stmt_type) {
133 		STMT_CALL_MSG(OCIAttrGet, "OCI_ATTR_STMT_TYPE",
134 				(S->stmt, OCI_HTYPE_STMT, &S->stmt_type, 0, OCI_ATTR_STMT_TYPE, S->err));
135 	}
136 
137 	if (stmt->executed) {
138 		/* ensure that we cancel the cursor from a previous fetch */
139 		OCIStmtFetch(S->stmt, S->err, 0, OCI_FETCH_NEXT, OCI_DEFAULT);
140 	}
141 
142 #ifdef OCI_STMT_SCROLLABLE_READONLY /* needed for oci8 ? */
143 	if (S->exec_type == OCI_STMT_SCROLLABLE_READONLY) {
144 		mode = OCI_STMT_SCROLLABLE_READONLY;
145 	} else
146 #endif
147 	if (stmt->dbh->auto_commit && !stmt->dbh->in_txn) {
148 		mode = OCI_COMMIT_ON_SUCCESS;
149 	} else {
150 		mode = OCI_DEFAULT;
151 	}
152 
153 	STMT_CALL(OCIStmtExecute, (S->H->svc, S->stmt, S->err,
154 				(S->stmt_type == OCI_STMT_SELECT && !S->have_blobs) ? 0 : 1, 0, NULL, NULL,
155 				mode));
156 
157 	if (!stmt->executed) {
158 		ub4 colcount;
159 		/* do first-time-only definition of bind/mapping stuff */
160 
161 		/* how many columns do we have ? */
162 		STMT_CALL_MSG(OCIAttrGet, "ATTR_PARAM_COUNT",
163 				(S->stmt, OCI_HTYPE_STMT, &colcount, 0, OCI_ATTR_PARAM_COUNT, S->err));
164 
165 		stmt->column_count = (int)colcount;
166 
167 		if (S->cols) {
168 			int i;
169 			for (i = 0; i < stmt->column_count; i++) {
170 				if (S->cols[i].data) {
171 					switch (S->cols[i].dtype) {
172 						case SQLT_BLOB:
173 						case SQLT_CLOB:
174 							/* do nothing */
175 							break;
176 						default:
177 							efree(S->cols[i].data);
178 					}
179 				}
180 			}
181 			efree(S->cols);
182 		}
183 
184 		S->cols = ecalloc(colcount, sizeof(pdo_oci_column));
185 	}
186 
187 	STMT_CALL_MSG(OCIAttrGet, "ATTR_ROW_COUNT",
188 			(S->stmt, OCI_HTYPE_STMT, &rowcount, 0, OCI_ATTR_ROW_COUNT, S->err));
189 	stmt->row_count = (long)rowcount;
190 
191 	return 1;
192 } /* }}} */
193 
oci_bind_input_cb(dvoid * ctx,OCIBind * bindp,ub4 iter,ub4 index,dvoid ** bufpp,ub4 * alenp,ub1 * piecep,dvoid ** indpp)194 static sb4 oci_bind_input_cb(dvoid *ctx, OCIBind *bindp, ub4 iter, ub4 index, dvoid **bufpp, ub4 *alenp, ub1 *piecep, dvoid **indpp) /* {{{ */
195 {
196 	struct pdo_bound_param_data *param = (struct pdo_bound_param_data*)ctx;
197 	pdo_oci_bound_param *P = (pdo_oci_bound_param*)param->driver_data;
198 	zval *parameter;
199 
200 	ZEND_ASSERT(param);
201 
202 	*indpp = &P->indicator;
203 
204     if (Z_ISREF(param->parameter))
205 		parameter = Z_REFVAL(param->parameter);
206 	else
207 		parameter = &param->parameter;
208 
209 	if (P->thing) {
210 		*bufpp = P->thing;
211 		*alenp = sizeof(void*);
212 	} else if (ZVAL_IS_NULL(parameter)) {
213 		/* insert a NULL value into the column */
214 		P->indicator = -1; /* NULL */
215 		*bufpp = 0;
216 		*alenp = -1;
217 	} else if (!P->thing) {
218 		/* regular string bind */
219 		if (!try_convert_to_string(parameter)) {
220 			return OCI_ERROR;
221 		}
222 		*bufpp = Z_STRVAL_P(parameter);
223 		*alenp = (ub4) Z_STRLEN_P(parameter);
224 	}
225 
226 	*piecep = OCI_ONE_PIECE;
227 	return OCI_CONTINUE;
228 } /* }}} */
229 
oci_bind_output_cb(dvoid * ctx,OCIBind * bindp,ub4 iter,ub4 index,dvoid ** bufpp,ub4 ** alenpp,ub1 * piecep,dvoid ** indpp,ub2 ** rcodepp)230 static sb4 oci_bind_output_cb(dvoid *ctx, OCIBind *bindp, ub4 iter, ub4 index, dvoid **bufpp, ub4 **alenpp, ub1 *piecep, dvoid **indpp, ub2 **rcodepp) /* {{{ */
231 {
232 	struct pdo_bound_param_data *param = (struct pdo_bound_param_data*)ctx;
233 	pdo_oci_bound_param *P = (pdo_oci_bound_param*)param->driver_data;
234 	zval *parameter;
235 
236 	ZEND_ASSERT(param);
237 
238 	if (Z_ISREF(param->parameter))
239 		parameter = Z_REFVAL(param->parameter);
240 	else
241 		parameter = &param->parameter;
242 
243 	if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
244 		P->actual_len = sizeof(OCILobLocator*);
245 		*bufpp = P->thing;
246 		*alenpp = &P->actual_len;
247 		*piecep = OCI_ONE_PIECE;
248 		*rcodepp = &P->retcode;
249 		*indpp = &P->indicator;
250 		return OCI_CONTINUE;
251 	}
252 
253 	if (Z_TYPE_P(parameter) == IS_OBJECT || Z_TYPE_P(parameter) == IS_RESOURCE) {
254 		return OCI_CONTINUE;
255 	}
256 
257 	zval_ptr_dtor(parameter);
258 
259 	Z_STR_P(parameter) = zend_string_alloc(param->max_value_len, 1);
260 	P->used_for_output = 1;
261 
262 	P->actual_len = (ub4) Z_STRLEN_P(parameter);
263 	*alenpp = &P->actual_len;
264 	*bufpp = (Z_STR_P(parameter))->val;
265 	*piecep = OCI_ONE_PIECE;
266 	*rcodepp = &P->retcode;
267 	*indpp = &P->indicator;
268 
269 	return OCI_CONTINUE;
270 } /* }}} */
271 
oci_stmt_param_hook(pdo_stmt_t * stmt,struct pdo_bound_param_data * param,enum pdo_param_event event_type)272 static int oci_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param, enum pdo_param_event event_type) /* {{{ */
273 {
274 	pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data;
275 
276 	/* we're only interested in parameters for prepared SQL right now */
277 	if (param->is_param) {
278 		pdo_oci_bound_param *P;
279 		sb4 value_sz = -1;
280 		zval *parameter;
281 
282 		if (Z_ISREF(param->parameter))
283 			parameter = Z_REFVAL(param->parameter);
284 		else
285 			parameter = &param->parameter;
286 
287 		P = (pdo_oci_bound_param*)param->driver_data;
288 
289 		switch (event_type) {
290 			case PDO_PARAM_EVT_FETCH_PRE:
291 			case PDO_PARAM_EVT_FETCH_POST:
292 			case PDO_PARAM_EVT_NORMALIZE:
293 				/* Do nothing */
294 				break;
295 
296 			case PDO_PARAM_EVT_FREE:
297 				P = param->driver_data;
298 				if (P && P->thing) {
299 					OCI_TEMPLOB_CLOSE(S->H->env, S->H->svc, S->H->err, P->thing);
300 					OCIDescriptorFree(P->thing, OCI_DTYPE_LOB);
301 					P->thing = NULL;
302 					efree(P);
303 				}
304 				else if (P) {
305 					efree(P);
306 				}
307 				break;
308 
309 			case PDO_PARAM_EVT_ALLOC:
310 				P = (pdo_oci_bound_param*)ecalloc(1, sizeof(pdo_oci_bound_param));
311 				param->driver_data = P;
312 
313 				/* figure out what we're doing */
314 				switch (PDO_PARAM_TYPE(param->param_type)) {
315 					case PDO_PARAM_STMT:
316 						return 0;
317 
318 					case PDO_PARAM_LOB:
319 						/* P->thing is now an OCILobLocator * */
320 						P->oci_type = SQLT_BLOB;
321 						value_sz = (sb4) sizeof(OCILobLocator*);
322 						break;
323 
324 					case PDO_PARAM_STR:
325 					default:
326 						P->oci_type = SQLT_CHR;
327 						value_sz = (sb4) param->max_value_len;
328 						if (param->max_value_len == 0) {
329 							value_sz = (sb4) 1332; /* maximum size before value is interpreted as a LONG value */
330 						}
331 
332 				}
333 
334 				if (param->name) {
335 					STMT_CALL(OCIBindByName, (S->stmt,
336 							&P->bind, S->err, (text*)param->name->val,
337 							(sb4) param->name->len, 0, value_sz, P->oci_type,
338 							&P->indicator, 0, &P->retcode, 0, 0,
339 							OCI_DATA_AT_EXEC));
340 				} else {
341 					STMT_CALL(OCIBindByPos, (S->stmt,
342 							&P->bind, S->err, ((ub4)param->paramno)+1,
343 							0, value_sz, P->oci_type,
344 							&P->indicator, 0, &P->retcode, 0, 0,
345 							OCI_DATA_AT_EXEC));
346 				}
347 
348 				STMT_CALL(OCIBindDynamic, (P->bind,
349 							S->err,
350 							param, oci_bind_input_cb,
351 							param, oci_bind_output_cb));
352 
353 				return 1;
354 
355 			case PDO_PARAM_EVT_EXEC_PRE:
356 				P->indicator = 0;
357 				P->used_for_output = 0;
358 				if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
359 					ub4 empty = 0;
360 					STMT_CALL(OCIDescriptorAlloc, (S->H->env, &P->thing, OCI_DTYPE_LOB, 0, NULL));
361 					STMT_CALL(OCIAttrSet, (P->thing, OCI_DTYPE_LOB, &empty, 0, OCI_ATTR_LOBEMPTY, S->err));
362 					S->have_blobs = 1;
363 				}
364 				return 1;
365 
366 			case PDO_PARAM_EVT_EXEC_POST:
367 				/* fixup stuff set in motion in oci_bind_output_cb */
368 				if (P->used_for_output) {
369 					if (P->indicator == -1) {
370 						/* set up a NULL value */
371 						if (Z_TYPE_P(parameter) == IS_STRING) {
372 							/* OCI likes to stick non-terminated strings in things */
373 							*Z_STRVAL_P(parameter) = '\0';
374 						}
375 						zval_ptr_dtor_str(parameter);
376 						ZVAL_UNDEF(parameter);
377 					} else if (Z_TYPE_P(parameter) == IS_STRING) {
378 						Z_STR_P(parameter) = zend_string_init(Z_STRVAL_P(parameter), P->actual_len, 1);
379 					}
380 				} else if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB && P->thing) {
381 					php_stream *stm;
382 
383 					if (Z_TYPE_P(parameter) == IS_NULL) {
384 						/* if the param is NULL, then we assume that they
385 						 * wanted to bind a lob locator into it from the query
386 						 * */
387 
388 						stm = oci_create_lob_stream(&stmt->database_object_handle, stmt, (OCILobLocator*)P->thing);
389 						if (stm) {
390 							OCILobOpen(S->H->svc, S->err, (OCILobLocator*)P->thing, OCI_LOB_READWRITE);
391 							php_stream_to_zval(stm, parameter);
392 						}
393 					} else {
394 						/* we're a LOB being used for insert; transfer the data now */
395 						size_t n;
396 						ub4 amt, offset = 1;
397 						char *consume;
398 
399 						php_stream_from_zval_no_verify(stm, parameter);
400 						if (stm) {
401 							OCILobOpen(S->H->svc, S->err, (OCILobLocator*)P->thing, OCI_LOB_READWRITE);
402 							do {
403 								char buf[8192];
404 								n = php_stream_read(stm, buf, sizeof(buf));
405 								if ((int)n <= 0) {
406 									break;
407 								}
408 								consume = buf;
409 								do {
410 									amt = (ub4) n;
411 									OCILobWrite(S->H->svc, S->err, (OCILobLocator*)P->thing,
412 											&amt, offset, consume, (ub4) n,
413 											OCI_ONE_PIECE,
414 											NULL, NULL, 0, SQLCS_IMPLICIT);
415 									offset += amt;
416 									n -= amt;
417 									consume += amt;
418 								} while (n);
419 							} while (1);
420 							OCILobClose(S->H->svc, S->err, (OCILobLocator*)P->thing);
421 							OCILobFlushBuffer(S->H->svc, S->err, (OCILobLocator*)P->thing, 0);
422 						} else if (Z_TYPE_P(parameter) == IS_STRING) {
423 							/* stick the string into the LOB */
424 							consume = Z_STRVAL_P(parameter);
425 							n = Z_STRLEN_P(parameter);
426 							if (n) {
427 								OCILobOpen(S->H->svc, S->err, (OCILobLocator*)P->thing, OCI_LOB_READWRITE);
428 								while (n) {
429 									amt = (ub4) n;
430 									OCILobWrite(S->H->svc, S->err, (OCILobLocator*)P->thing,
431 											&amt, offset, consume, (ub4) n,
432 											OCI_ONE_PIECE,
433 											NULL, NULL, 0, SQLCS_IMPLICIT);
434 									consume += amt;
435 									n -= amt;
436 								}
437 								OCILobClose(S->H->svc, S->err, (OCILobLocator*)P->thing);
438 							}
439 						}
440 						OCI_TEMPLOB_CLOSE(S->H->env, S->H->svc, S->H->err, P->thing);
441 						OCIDescriptorFree(P->thing, OCI_DTYPE_LOB);
442 						P->thing = NULL;
443 					}
444 				}
445 
446 				return 1;
447 		}
448 	}
449 
450 	return 1;
451 } /* }}} */
452 
oci_stmt_fetch(pdo_stmt_t * stmt,enum pdo_fetch_orientation ori,zend_long offset)453 static int oci_stmt_fetch(pdo_stmt_t *stmt, enum pdo_fetch_orientation ori,	zend_long offset) /* {{{ */
454 {
455 #ifdef HAVE_OCISTMTFETCH2
456 	ub4 ociori = OCI_FETCH_NEXT;
457 #endif
458 	pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data;
459 
460 #ifdef HAVE_OCISTMTFETCH2
461 	switch (ori) {
462 		case PDO_FETCH_ORI_NEXT:	ociori = OCI_FETCH_NEXT; break;
463 		case PDO_FETCH_ORI_PRIOR:	ociori = OCI_FETCH_PRIOR; break;
464 		case PDO_FETCH_ORI_FIRST:	ociori = OCI_FETCH_FIRST; break;
465 		case PDO_FETCH_ORI_LAST:	ociori = OCI_FETCH_LAST; break;
466 		case PDO_FETCH_ORI_ABS:		ociori = OCI_FETCH_ABSOLUTE; break;
467 		case PDO_FETCH_ORI_REL:		ociori = OCI_FETCH_RELATIVE; break;
468 	}
469 	S->last_err = OCIStmtFetch2(S->stmt, S->err, 1, ociori, (sb4) offset, OCI_DEFAULT);
470 #else
471 	S->last_err = OCIStmtFetch(S->stmt, S->err, 1, OCI_FETCH_NEXT, OCI_DEFAULT);
472 #endif
473 
474 	if (S->last_err == OCI_NO_DATA) {
475 		/* no (more) data */
476 		return 0;
477 	}
478 
479 	if (S->last_err == OCI_NEED_DATA) {
480 		oci_stmt_error("OCI_NEED_DATA");
481 		return 0;
482 	}
483 
484 	if (S->last_err == OCI_SUCCESS_WITH_INFO || S->last_err == OCI_SUCCESS) {
485 		return 1;
486 	}
487 
488 	oci_stmt_error("OCIStmtFetch");
489 
490 	return 0;
491 } /* }}} */
492 
oci_define_callback(dvoid * octxp,OCIDefine * define,ub4 iter,dvoid ** bufpp,ub4 ** alenpp,ub1 * piecep,dvoid ** indpp,ub2 ** rcodepp)493 static sb4 oci_define_callback(dvoid *octxp, OCIDefine *define, ub4 iter, dvoid **bufpp,
494 		ub4 **alenpp, ub1 *piecep, dvoid **indpp, ub2 **rcodepp)
495 {
496 	pdo_oci_column *col = (pdo_oci_column*)octxp;
497 
498 	switch (col->dtype) {
499 		case SQLT_BLOB:
500 		case SQLT_CLOB:
501 			*piecep = OCI_ONE_PIECE;
502 			*bufpp = col->data;
503 			*alenpp = &col->datalen;
504 			*indpp = (dvoid *)&col->indicator;
505 			break;
506 		EMPTY_SWITCH_DEFAULT_CASE();
507 	}
508 
509 	return OCI_CONTINUE;
510 }
511 
oci_stmt_describe(pdo_stmt_t * stmt,int colno)512 static int oci_stmt_describe(pdo_stmt_t *stmt, int colno) /* {{{ */
513 {
514 	pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data;
515 	OCIParam *param = NULL;
516 	text *colname;
517 	ub2 dtype, data_size, precis;
518 	ub4 namelen;
519 	struct pdo_column_data *col = &stmt->columns[colno];
520 	zend_bool dyn = FALSE;
521 
522 	/* describe the column */
523 	STMT_CALL(OCIParamGet, (S->stmt, OCI_HTYPE_STMT, S->err, (dvoid*)&param, colno+1));
524 
525 	/* what type ? */
526 	STMT_CALL_MSG(OCIAttrGet, "OCI_ATTR_DATA_TYPE",
527 			(param, OCI_DTYPE_PARAM, &dtype, 0, OCI_ATTR_DATA_TYPE, S->err));
528 
529 	/* how big ? */
530 	STMT_CALL_MSG(OCIAttrGet, "OCI_ATTR_DATA_SIZE",
531 			(param, OCI_DTYPE_PARAM, &data_size, 0, OCI_ATTR_DATA_SIZE, S->err));
532 
533 	/* precision ? */
534 	STMT_CALL_MSG(OCIAttrGet, "OCI_ATTR_PRECISION",
535 			(param, OCI_DTYPE_PARAM, &precis, 0, OCI_ATTR_PRECISION, S->err));
536 
537 	/* name ? */
538 	STMT_CALL_MSG(OCIAttrGet, "OCI_ATTR_NAME",
539 			(param, OCI_DTYPE_PARAM, &colname, &namelen, OCI_ATTR_NAME, S->err));
540 
541 	col->precision = precis;
542 	col->maxlen = data_size;
543 	col->name = zend_string_init((char *)colname, namelen, 0);
544 
545 	S->cols[colno].dtype = dtype;
546 
547 	/* how much room do we need to store the field */
548 	switch (dtype) {
549 		case SQLT_LBI:
550 		case SQLT_LNG:
551 			if (dtype == SQLT_LBI) {
552 				dtype = SQLT_BIN;
553 			} else {
554 				dtype = SQLT_CHR;
555 			}
556 			S->cols[colno].datalen = 512; /* XXX should be INT_MAX and fetched by pieces */
557 			S->cols[colno].data = emalloc(S->cols[colno].datalen + 1);
558 			col->param_type = PDO_PARAM_STR;
559 			break;
560 
561 		case SQLT_BLOB:
562 		case SQLT_CLOB:
563 			col->param_type = PDO_PARAM_LOB;
564 			STMT_CALL(OCIDescriptorAlloc, (S->H->env, (dvoid**)&S->cols[colno].data, OCI_DTYPE_LOB, 0, NULL));
565 			S->cols[colno].datalen = sizeof(OCILobLocator*);
566 			dyn = TRUE;
567 			break;
568 
569 		case SQLT_BIN:
570 		default:
571 			if (dtype == SQLT_DAT || dtype == SQLT_NUM || dtype == SQLT_RDD
572 #ifdef SQLT_TIMESTAMP
573 					|| dtype == SQLT_TIMESTAMP
574 #endif
575 #ifdef SQLT_TIMESTAMP_TZ
576 					|| dtype == SQLT_TIMESTAMP_TZ
577 #endif
578 					) {
579 				/* should be big enough for most date formats and numbers */
580 				S->cols[colno].datalen = 512;
581 #if defined(SQLT_IBFLOAT) && defined(SQLT_IBDOUBLE)
582 			} else if (dtype == SQLT_IBFLOAT || dtype == SQLT_IBDOUBLE) {
583 				S->cols[colno].datalen = 1024;
584 #endif
585 			} else if (dtype == SQLT_BIN) {
586 				S->cols[colno].datalen = (ub4) col->maxlen * 2; /* raw characters to hex digits */
587 			} else {
588 				S->cols[colno].datalen = (ub4) (col->maxlen * S->H->max_char_width);
589 			}
590 
591 			S->cols[colno].data = emalloc(S->cols[colno].datalen + 1);
592 			dtype = SQLT_CHR;
593 
594 			/* returning data as a string */
595 			col->param_type = PDO_PARAM_STR;
596 	}
597 
598 	STMT_CALL(OCIDefineByPos, (S->stmt, &S->cols[colno].def, S->err, colno+1,
599 				S->cols[colno].data, S->cols[colno].datalen, dtype, &S->cols[colno].indicator,
600 				&S->cols[colno].fetched_len, &S->cols[colno].retcode, dyn ? OCI_DYNAMIC_FETCH : OCI_DEFAULT));
601 
602 	if (dyn) {
603 		STMT_CALL(OCIDefineDynamic, (S->cols[colno].def, S->err, &S->cols[colno],
604 				oci_define_callback));
605 	}
606 
607 	return 1;
608 } /* }}} */
609 
610 struct _oci_lob_env {
611 	OCISvcCtx *svc;
612 	OCIError  *err;
613 };
614 typedef struct _oci_lob_env oci_lob_env;
615 
616 struct oci_lob_self {
617 	zval dbh;
618 	pdo_stmt_t *stmt;
619 	pdo_oci_stmt *S;
620 	OCILobLocator *lob;
621 	oci_lob_env   *E;
622 	ub4 offset;
623 };
624 
oci_blob_write(php_stream * stream,const char * buf,size_t count)625 static ssize_t oci_blob_write(php_stream *stream, const char *buf, size_t count)
626 {
627 	struct oci_lob_self *self = (struct oci_lob_self*)stream->abstract;
628 	ub4 amt;
629 	sword r;
630 
631 	amt = (ub4) count;
632 	r = OCILobWrite(self->E->svc, self->E->err, self->lob,
633 		&amt, self->offset, (char*)buf, (ub4) count,
634 		OCI_ONE_PIECE,
635 		NULL, NULL, 0, SQLCS_IMPLICIT);
636 
637 	if (r != OCI_SUCCESS) {
638 		return (ssize_t)-1;
639 	}
640 
641 	self->offset += amt;
642 	return amt;
643 }
644 
oci_blob_read(php_stream * stream,char * buf,size_t count)645 static ssize_t oci_blob_read(php_stream *stream, char *buf, size_t count)
646 {
647 	struct oci_lob_self *self = (struct oci_lob_self*)stream->abstract;
648 	ub4 amt;
649 	sword r;
650 
651 	amt = (ub4) count;
652 	r = OCILobRead(self->E->svc, self->E->err, self->lob,
653 		&amt, self->offset, buf, (ub4) count,
654 		NULL, NULL, 0, SQLCS_IMPLICIT);
655 
656 	if (r != OCI_SUCCESS && r != OCI_NEED_DATA) {
657 		return (size_t)-1;
658 	}
659 
660 	self->offset += amt;
661 	if (amt < count) {
662 		stream->eof = 1;
663 	}
664 	return amt;
665 }
666 
oci_blob_close(php_stream * stream,int close_handle)667 static int oci_blob_close(php_stream *stream, int close_handle)
668 {
669 	struct oci_lob_self *self = (struct oci_lob_self *)stream->abstract;
670 	pdo_stmt_t *stmt = self->stmt;
671 
672 	if (close_handle) {
673 		zend_object *obj = &stmt->std;
674 
675 		OCILobClose(self->E->svc, self->E->err, self->lob);
676 		zval_ptr_dtor(&self->dbh);
677 		GC_DELREF(obj);
678 		efree(self->E);
679 		efree(self);
680 	}
681 
682 	/* php_pdo_free_statement(stmt); */
683 	return 0;
684 }
685 
oci_blob_flush(php_stream * stream)686 static int oci_blob_flush(php_stream *stream)
687 {
688 	struct oci_lob_self *self = (struct oci_lob_self*)stream->abstract;
689 	OCILobFlushBuffer(self->E->svc, self->E->err, self->lob, 0);
690 	return 0;
691 }
692 
oci_blob_seek(php_stream * stream,zend_off_t offset,int whence,zend_off_t * newoffset)693 static int oci_blob_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffset)
694 {
695 	struct oci_lob_self *self = (struct oci_lob_self*)stream->abstract;
696 
697 	if (offset >= PDO_OCI_LOBMAXSIZE) {
698 		return -1;
699 	} else {
700 		self->offset = (ub4) offset + 1;  /* Oracle LOBS are 1-based, but PHP is 0-based */
701 		return 0;
702 	}
703 }
704 
705 static const php_stream_ops oci_blob_stream_ops = {
706 	oci_blob_write,
707 	oci_blob_read,
708 	oci_blob_close,
709 	oci_blob_flush,
710 	"pdo_oci blob stream",
711 	oci_blob_seek,
712 	NULL,
713 	NULL,
714 	NULL
715 };
716 
oci_create_lob_stream(zval * dbh,pdo_stmt_t * stmt,OCILobLocator * lob)717 static php_stream *oci_create_lob_stream(zval *dbh, pdo_stmt_t *stmt, OCILobLocator *lob)
718 {
719 	php_stream *stm;
720 	struct oci_lob_self *self = ecalloc(1, sizeof(*self));
721 
722 	ZVAL_COPY_VALUE(&self->dbh, dbh);
723 	self->lob = lob;
724 	self->offset = 1; /* 1-based */
725 	self->stmt = stmt;
726 	self->S = (pdo_oci_stmt*)stmt->driver_data;
727 	self->E = ecalloc(1, sizeof(oci_lob_env));
728 	self->E->svc = self->S->H->svc;
729 	self->E->err = self->S->err;
730 
731 	stm = php_stream_alloc(&oci_blob_stream_ops, self, 0, "r+b");
732 
733 	if (stm) {
734 		zend_object *obj;
735 		obj = &stmt->std;
736 		Z_ADDREF(self->dbh);
737 		GC_ADDREF(obj);
738 		return stm;
739 	}
740 
741 	efree(self);
742 	return NULL;
743 }
744 
oci_stmt_get_col(pdo_stmt_t * stmt,int colno,char ** ptr,size_t * len,int * caller_frees)745 static int oci_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, size_t *len, int *caller_frees) /* {{{ */
746 {
747 	pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data;
748 	pdo_oci_column *C = &S->cols[colno];
749 
750 	/* check the indicator to ensure that the data is intact */
751 	if (C->indicator == -1) {
752 		/* A NULL value */
753 		*ptr = NULL;
754 		*len = 0;
755 		return 1;
756 	} else if (C->indicator == 0) {
757 		/* it was stored perfectly */
758 
759 		if (C->dtype == SQLT_BLOB || C->dtype == SQLT_CLOB) {
760 			if (C->data) {
761 				*ptr = (char*)oci_create_lob_stream(&stmt->database_object_handle, stmt, (OCILobLocator*)C->data);
762 				OCILobOpen(S->H->svc, S->err, (OCILobLocator*)C->data, OCI_LOB_READONLY);
763 			}
764 			*len = (size_t) 0;
765 			return *ptr ? 1 : 0;
766 		}
767 
768 		*ptr = C->data;
769 		*len = (size_t) C->fetched_len;
770 		return 1;
771 	} else {
772 		/* it was truncated */
773 		php_error_docref(NULL, E_WARNING, "Column %d data was too large for buffer and was truncated to fit it", colno);
774 
775 		*ptr = C->data;
776 		*len = (size_t) C->fetched_len;
777 		return 1;
778 	}
779 } /* }}} */
780 
781 
oci_stmt_col_meta(pdo_stmt_t * stmt,zend_long colno,zval * return_value)782 static int oci_stmt_col_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value) /* {{{ */
783 {
784 	pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data;
785 	OCIParam *param = NULL;
786 	ub2 dtype, precis;
787 	sb1 scale;
788 	zval flags;
789 	ub1 isnull, charset_form;
790 	if (!S->stmt) {
791 		return FAILURE;
792 	}
793 	if (colno >= stmt->column_count) {
794 		/* error invalid column */
795 		return FAILURE;
796 	}
797 
798 	array_init(return_value);
799 	array_init(&flags);
800 
801 	/* describe the column */
802 	STMT_CALL(OCIParamGet, (S->stmt, OCI_HTYPE_STMT, S->err, (dvoid*)&param, colno+1));
803 
804 	/* column data type */
805 	STMT_CALL_MSG(OCIAttrGet, "OCI_ATTR_DATA_TYPE",
806 			(param, OCI_DTYPE_PARAM, &dtype, 0, OCI_ATTR_DATA_TYPE, S->err));
807 
808 	/* column precision */
809 	STMT_CALL_MSG(OCIAttrGet, "OCI_ATTR_PRECISION",
810 			(param, OCI_DTYPE_PARAM, &precis, 0, OCI_ATTR_PRECISION, S->err));
811 
812 	/* column scale */
813 	STMT_CALL_MSG(OCIAttrGet, "OCI_ATTR_SCALE",
814 			(param, OCI_DTYPE_PARAM, &scale, 0, OCI_ATTR_SCALE, S->err));
815 
816 	/* string column charset form */
817 	if (dtype == SQLT_CHR || dtype == SQLT_VCS || dtype == SQLT_AFC || dtype == SQLT_CLOB) {
818 		STMT_CALL_MSG(OCIAttrGet, "OCI_ATTR_CHARSET_FORM",
819 			(param, OCI_DTYPE_PARAM, &charset_form, 0, OCI_ATTR_CHARSET_FORM, S->err));
820 	}
821 
822 
823 	if (dtype) {
824 	/* if there is a declared type */
825 		switch (dtype) {
826 #ifdef SQLT_TIMESTAMP
827 		case SQLT_TIMESTAMP:
828 			add_assoc_string(return_value, "oci:decl_type", "TIMESTAMP");
829 			add_assoc_string(return_value, "native_type", "TIMESTAMP");
830 			break;
831 #endif
832 #ifdef SQLT_TIMESTAMP_TZ
833 		case SQLT_TIMESTAMP_TZ:
834 			add_assoc_string(return_value, "oci:decl_type", "TIMESTAMP WITH TIMEZONE");
835 			add_assoc_string(return_value, "native_type", "TIMESTAMP WITH TIMEZONE");
836 			break;
837 #endif
838 #ifdef SQLT_TIMESTAMP_LTZ
839 		case SQLT_TIMESTAMP_LTZ:
840 			add_assoc_string(return_value, "oci:decl_type", "TIMESTAMP WITH LOCAL TIMEZONE");
841 			add_assoc_string(return_value, "native_type", "TIMESTAMP WITH LOCAL TIMEZONE");
842 			break;
843 #endif
844 #ifdef SQLT_INTERVAL_YM
845 		case SQLT_INTERVAL_YM:
846 			add_assoc_string(return_value, "oci:decl_type", "INTERVAL YEAR TO MONTH");
847 			add_assoc_string(return_value, "native_type", "INTERVAL YEAR TO MONTH");
848 			break;
849 #endif
850 #ifdef SQLT_INTERVAL_DS
851 		case SQLT_INTERVAL_DS:
852 			add_assoc_string(return_value, "oci:decl_type", "INTERVAL DAY TO SECOND");
853 			add_assoc_string(return_value, "native_type", "INTERVAL DAY TO SECOND");
854 			break;
855 #endif
856 		case SQLT_DAT:
857 			add_assoc_string(return_value, "oci:decl_type", "DATE");
858 			add_assoc_string(return_value, "native_type", "DATE");
859 			break;
860 		case SQLT_FLT :
861 		case SQLT_NUM:
862 			/* if the precision is nonzero and scale is -127 then it is a FLOAT */
863 			if (scale == -127 && precis != 0) {
864 				add_assoc_string(return_value, "oci:decl_type", "FLOAT");
865 				add_assoc_string(return_value, "native_type", "FLOAT");
866 			} else {
867 				add_assoc_string(return_value, "oci:decl_type", "NUMBER");
868 				add_assoc_string(return_value, "native_type", "NUMBER");
869 			}
870 			break;
871 		case SQLT_LNG:
872 			add_assoc_string(return_value, "oci:decl_type", "LONG");
873 			add_assoc_string(return_value, "native_type", "LONG");
874 			break;
875 		case SQLT_BIN:
876 			add_assoc_string(return_value, "oci:decl_type", "RAW");
877 			add_assoc_string(return_value, "native_type", "RAW");
878 			break;
879 		case SQLT_LBI:
880 			add_assoc_string(return_value, "oci:decl_type", "LONG RAW");
881 			add_assoc_string(return_value, "native_type", "LONG RAW");
882 			break;
883 		case SQLT_CHR:
884 		case SQLT_VCS:
885 			if (charset_form == SQLCS_NCHAR) {
886 				add_assoc_string(return_value, "oci:decl_type", "NVARCHAR2");
887 				add_assoc_string(return_value, "native_type", "NVARCHAR2");
888 			} else {
889 				add_assoc_string(return_value, "oci:decl_type", "VARCHAR2");
890 				add_assoc_string(return_value, "native_type", "VARCHAR2");
891 			}
892 			break;
893 		case SQLT_AFC:
894 			if (charset_form == SQLCS_NCHAR) {
895 				add_assoc_string(return_value, "oci:decl_type", "NCHAR");
896 				add_assoc_string(return_value, "native_type", "NCHAR");
897 			} else {
898 				add_assoc_string(return_value, "oci:decl_type", "CHAR");
899 				add_assoc_string(return_value, "native_type", "CHAR");
900 			}
901 			break;
902 		case SQLT_BLOB:
903 			add_assoc_string(return_value, "oci:decl_type", "BLOB");
904 			add_next_index_string(&flags, "blob");
905 			add_assoc_string(return_value, "native_type", "BLOB");
906 			break;
907 		case SQLT_CLOB:
908 			if (charset_form == SQLCS_NCHAR) {
909 				add_assoc_string(return_value, "oci:decl_type", "NCLOB");
910 				add_assoc_string(return_value, "native_type", "NCLOB");
911 			} else {
912 				add_assoc_string(return_value, "oci:decl_type", "CLOB");
913 				add_assoc_string(return_value, "native_type", "CLOB");
914 			}
915 			add_next_index_string(&flags, "blob");
916 			break;
917 		case SQLT_BFILE:
918 			add_assoc_string(return_value, "oci:decl_type", "BFILE");
919 			add_next_index_string(&flags, "blob");
920 			add_assoc_string(return_value, "native_type", "BFILE");
921 			break;
922 		case SQLT_RDD:
923 			add_assoc_string(return_value, "oci:decl_type", "ROWID");
924 			add_assoc_string(return_value, "native_type", "ROWID");
925 			break;
926 		case SQLT_BFLOAT:
927 		case SQLT_IBFLOAT:
928 			add_assoc_string(return_value, "oci:decl_type", "BINARY_FLOAT");
929 			add_assoc_string(return_value, "native_type", "BINARY_FLOAT");
930 			break;
931 		case SQLT_BDOUBLE:
932 		case SQLT_IBDOUBLE:
933 			add_assoc_string(return_value, "oci:decl_type", "BINARY_DOUBLE");
934 			add_assoc_string(return_value, "native_type", "BINARY_DOUBLE");
935 			break;
936 		default:
937 			add_assoc_long(return_value, "oci:decl_type", dtype);
938 			add_assoc_string(return_value, "native_type", "UNKNOWN");
939 		}
940 	} else {
941 		/* if the column is NULL */
942 		add_assoc_long(return_value, "oci:decl_type", 0);
943 		add_assoc_string(return_value, "native_type", "NULL");
944 	}
945 
946 	/* column can be null */
947 	STMT_CALL_MSG(OCIAttrGet, "OCI_ATTR_IS_NULL",
948 			(param, OCI_DTYPE_PARAM, &isnull, 0, OCI_ATTR_IS_NULL, S->err));
949 
950 	if (isnull) {
951 		add_next_index_string(&flags, "nullable");
952 	} else {
953 		add_next_index_string(&flags, "not_null");
954 	}
955 
956 	/* PDO type */
957 	switch (dtype) {
958 		case SQLT_BFILE:
959 		case SQLT_BLOB:
960 		case SQLT_CLOB:
961 			add_assoc_long(return_value, "pdo_type", PDO_PARAM_LOB);
962 			break;
963 		default:
964 			add_assoc_long(return_value, "pdo_type", PDO_PARAM_STR);
965 	}
966 
967 	add_assoc_long(return_value, "scale", scale);
968 	add_assoc_zval(return_value, "flags", &flags);
969 
970 	OCIDescriptorFree(param, OCI_DTYPE_PARAM);
971 	return SUCCESS;
972 } /* }}} */
973 
974 const struct pdo_stmt_methods oci_stmt_methods = {
975 	oci_stmt_dtor,
976 	oci_stmt_execute,
977 	oci_stmt_fetch,
978 	oci_stmt_describe,
979 	oci_stmt_get_col,
980 	oci_stmt_param_hook,
981 	NULL, /* set_attr */
982 	NULL, /* get_attr */
983 	oci_stmt_col_meta,
984 	NULL,
985 	NULL
986 };
987