1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 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: George Schlossnagle <george@omniti.com> |
16 : | Wez Furlong <wez@php.net> |
17 : | Johannes Schlueter <johannes@mysql.com> |
18 : +----------------------------------------------------------------------+
19 : */
20 :
21 : /* $Id: mysql_statement.c 280837 2009-05-20 08:29:23Z kalle $ */
22 :
23 : #ifdef HAVE_CONFIG_H
24 : #include "config.h"
25 : #endif
26 :
27 : #include "php.h"
28 : #include "php_ini.h"
29 : #include "ext/standard/info.h"
30 : #include "pdo/php_pdo.h"
31 : #include "pdo/php_pdo_driver.h"
32 : #include "php_pdo_mysql.h"
33 : #include "php_pdo_mysql_int.h"
34 :
35 : #ifdef PDO_USE_MYSQLND
36 : # define pdo_mysql_stmt_execute_prepared(stmt) pdo_mysql_stmt_execute_prepared_mysqlnd(stmt TSRMLS_CC)
37 : # define pdo_free_bound_result(res) zval_dtor(res.zv)
38 : # define pdo_mysql_stmt_close(stmt) mysqlnd_stmt_close(stmt, 0)
39 : #else
40 : # define pdo_mysql_stmt_execute_prepared(stmt) pdo_mysql_stmt_execute_prepared_libmysql(stmt TSRMLS_CC)
41 : # define pdo_free_bound_result(res) efree(res.buffer)
42 : # define pdo_mysql_stmt_close(stmt) mysql_stmt_close(stmt)
43 : #endif
44 :
45 :
46 :
47 : static int pdo_mysql_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
48 1557 : {
49 1557 : pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
50 :
51 1557 : PDO_DBG_ENTER("pdo_mysql_stmt_dtor");
52 1557 : PDO_DBG_INF_FMT("stmt=%p", S->stmt);
53 1557 : if (S->result) {
54 : /* free the resource */
55 480 : mysql_free_result(S->result);
56 480 : S->result = NULL;
57 : }
58 1557 : if (S->einfo.errmsg) {
59 24 : pefree(S->einfo.errmsg, stmt->dbh->is_persistent);
60 24 : S->einfo.errmsg = NULL;
61 : }
62 : #if HAVE_MYSQL_STMT_PREPARE || PDO_USE_MYSQLND
63 1557 : if (S->stmt) {
64 788 : pdo_mysql_stmt_close(S->stmt);
65 788 : S->stmt = NULL;
66 : }
67 : #endif /* HAVE_MYSQL_STMT_PREPARE || PDO_USE_MYSQLND */
68 :
69 : #ifndef PDO_USE_MYSQLND
70 : if (S->params) {
71 : efree(S->params);
72 : }
73 : if (S->in_null) {
74 : efree(S->in_null);
75 : }
76 : if (S->in_length) {
77 : efree(S->in_length);
78 : }
79 :
80 : #endif /* PDO_USE_MYSQLND */
81 :
82 : #ifdef HAVE_MYSQL_STMT_PREPARE
83 : if (S->bound_result)
84 : {
85 : int i;
86 : for (i = 0; i < stmt->column_count; i++) {
87 : pdo_free_bound_result(S->bound_result[i]);
88 : }
89 :
90 : efree(S->bound_result);
91 : efree(S->out_null);
92 : efree(S->out_length);
93 : }
94 : #endif /* HAVE_MYSQL_STMT_PREPARE */
95 :
96 :
97 : #if HAVE_MYSQL_NEXT_RESULT || PDO_USE_MYSQLND
98 1557 : if (S->H->server) {
99 3119 : while (mysql_more_results(S->H->server)) {
100 : MYSQL_RES *res;
101 7 : if (mysql_next_result(S->H->server) != 0) {
102 2 : break;
103 : }
104 :
105 5 : res = mysql_store_result(S->H->server);
106 5 : if (res) {
107 3 : mysql_free_result(res);
108 : }
109 : }
110 : }
111 : #endif /* HAVE_MYSQL_NEXT_RESULT || PDO_USE_MYSQLND */
112 : #if PDO_USE_MYSQLND
113 1557 : if (!S->stmt && S->current_data) {
114 169 : free(S->current_data);
115 : }
116 : #endif /* PDO_USE_MYSQLND */
117 :
118 1557 : efree(S);
119 1557 : PDO_DBG_RETURN(1);
120 : }
121 : /* }}} */
122 :
123 : static void pdo_mysql_stmt_set_row_count(pdo_stmt_t *stmt) /* {{{ */
124 638 : {
125 : long row_count;
126 638 : pdo_mysql_stmt *S = stmt->driver_data;
127 638 : row_count = (long) mysql_stmt_affected_rows(S->stmt);
128 638 : if (row_count != (long)-1) {
129 626 : stmt->row_count = row_count;
130 : }
131 638 : }
132 : /* }}} */
133 :
134 : #ifdef HAVE_MYSQL_STMT_PREPARE
135 : static int pdo_mysql_stmt_execute_prepared_libmysql(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
136 : {
137 : pdo_mysql_stmt *S = stmt->driver_data;
138 : pdo_mysql_db_handle *H = S->H;
139 :
140 : PDO_DBG_ENTER("pdo_mysql_stmt_execute_prepared_libmysql");
141 :
142 : /* (re)bind the parameters */
143 : if (mysql_stmt_bind_param(S->stmt, S->params) || mysql_stmt_execute(S->stmt)) {
144 : if (S->params) {
145 : efree(S->params);
146 : S->params = 0;
147 : }
148 : pdo_mysql_error_stmt(stmt);
149 : if (mysql_stmt_errno(S->stmt) == 2057) {
150 : /* CR_NEW_STMT_METADATA makes the statement unusable */
151 : S->stmt = NULL;
152 : }
153 : PDO_DBG_RETURN(0);
154 : }
155 :
156 : if (!S->result) {
157 : int i;
158 :
159 : /* figure out the result set format, if any */
160 : S->result = mysql_stmt_result_metadata(S->stmt);
161 : if (S->result) {
162 : int calc_max_length = H->buffered && S->max_length == 1;
163 : S->fields = mysql_fetch_fields(S->result);
164 : if (S->bound_result) {
165 : int i;
166 : for (i = 0; i < stmt->column_count; i++) {
167 : efree(S->bound_result[i].buffer);
168 : }
169 : efree(S->bound_result);
170 : efree(S->out_null);
171 : efree(S->out_length);
172 : }
173 :
174 : stmt->column_count = (int)mysql_num_fields(S->result);
175 : S->bound_result = ecalloc(stmt->column_count, sizeof(MYSQL_BIND));
176 : S->out_null = ecalloc(stmt->column_count, sizeof(my_bool));
177 : S->out_length = ecalloc(stmt->column_count, sizeof(unsigned long));
178 :
179 : /* summon memory to hold the row */
180 : for (i = 0; i < stmt->column_count; i++) {
181 : if (calc_max_length && S->fields[i].type == FIELD_TYPE_BLOB) {
182 : my_bool on = 1;
183 : mysql_stmt_attr_set(S->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &on);
184 : calc_max_length = 0;
185 : }
186 : switch (S->fields[i].type) {
187 : case FIELD_TYPE_INT24:
188 : S->bound_result[i].buffer_length = MAX_MEDIUMINT_WIDTH + 1;
189 : break;
190 : case FIELD_TYPE_LONG:
191 : S->bound_result[i].buffer_length = MAX_INT_WIDTH + 1;
192 : break;
193 : case FIELD_TYPE_LONGLONG:
194 : S->bound_result[i].buffer_length = MAX_BIGINT_WIDTH + 1;
195 : break;
196 : case FIELD_TYPE_TINY:
197 : S->bound_result[i].buffer_length = MAX_TINYINT_WIDTH + 1;
198 : break;
199 : case FIELD_TYPE_SHORT:
200 : S->bound_result[i].buffer_length = MAX_SMALLINT_WIDTH + 1;
201 : break;
202 : default:
203 : S->bound_result[i].buffer_length =
204 : S->fields[i].max_length? S->fields[i].max_length:
205 : S->fields[i].length;
206 : /* work-around for longtext and alike */
207 : if (S->bound_result[i].buffer_length > H->max_buffer_size) {
208 : S->bound_result[i].buffer_length = H->max_buffer_size;
209 : }
210 : }
211 :
212 : /* there are cases where the length reported by mysql is too short.
213 : * eg: when describing a table that contains an enum column. Since
214 : * we have no way of knowing the true length either, we'll bump up
215 : * our buffer size to a reasonable size, just in case */
216 : if (S->fields[i].max_length == 0 && S->bound_result[i].buffer_length < 128 && MYSQL_TYPE_VAR_STRING) {
217 : S->bound_result[i].buffer_length = 128;
218 : }
219 :
220 : S->out_length[i] = 0;
221 :
222 : S->bound_result[i].buffer = emalloc(S->bound_result[i].buffer_length);
223 : S->bound_result[i].is_null = &S->out_null[i];
224 : S->bound_result[i].length = &S->out_length[i];
225 : S->bound_result[i].buffer_type = MYSQL_TYPE_STRING;
226 : }
227 :
228 : if (mysql_stmt_bind_result(S->stmt, S->bound_result)) {
229 : pdo_mysql_error_stmt(stmt);
230 : PDO_DBG_RETURN(0);
231 : }
232 :
233 : /* if buffered, pre-fetch all the data */
234 : if (H->buffered) {
235 : mysql_stmt_store_result(S->stmt);
236 : }
237 : }
238 : }
239 :
240 : pdo_mysql_stmt_set_row_count(stmt);
241 : PDO_DBG_RETURN(1);
242 : }
243 : /* }}} */
244 : #endif
245 :
246 : #ifdef PDO_USE_MYSQLND
247 : static int pdo_mysql_stmt_execute_prepared_mysqlnd(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
248 641 : {
249 641 : pdo_mysql_stmt *S = stmt->driver_data;
250 641 : pdo_mysql_db_handle *H = S->H;
251 : int i;
252 :
253 641 : PDO_DBG_ENTER("pdo_mysql_stmt_execute_prepared_mysqlnd");
254 :
255 641 : if (mysql_stmt_execute(S->stmt)) {
256 3 : pdo_mysql_error_stmt(stmt);
257 3 : PDO_DBG_RETURN(0);
258 : }
259 :
260 638 : if (S->result) {
261 : /* TODO: add a test to check if we really have zvals here... */
262 14 : mysql_free_result(S->result);
263 14 : S->result = NULL;
264 : }
265 :
266 : /* for SHOW/DESCRIBE and others the column/field count is not available before execute */
267 638 : stmt->column_count = S->stmt->field_count;
268 1351 : for (i = 0; i < stmt->column_count; i++) {
269 713 : mysqlnd_stmt_bind_one_result(S->stmt, i);
270 : }
271 :
272 638 : S->result = mysqlnd_stmt_result_metadata(S->stmt);
273 638 : if (S->result) {
274 351 : S->fields = mysql_fetch_fields(S->result);
275 : /* if buffered, pre-fetch all the data */
276 351 : if (H->buffered) {
277 339 : if (mysql_stmt_store_result(S->stmt)) {
278 0 : PDO_DBG_RETURN(0);
279 : }
280 : }
281 : }
282 :
283 638 : pdo_mysql_stmt_set_row_count(stmt);
284 638 : PDO_DBG_RETURN(1);
285 : }
286 : /* }}} */
287 : #endif
288 :
289 : static int pdo_mysql_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
290 1316 : {
291 1316 : pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
292 1316 : pdo_mysql_db_handle *H = S->H;
293 : my_ulonglong row_count;
294 1316 : PDO_DBG_ENTER("pdo_mysql_stmt_execute");
295 1316 : PDO_DBG_INF_FMT("stmt=%p", S->stmt);
296 :
297 : #if HAVE_MYSQL_STMT_PREPARE || PDO_USE_MYSQLND
298 1316 : if (S->stmt) {
299 641 : PDO_DBG_RETURN(pdo_mysql_stmt_execute_prepared(stmt));
300 : }
301 : #endif
302 :
303 : /* ensure that we free any previous unfetched results */
304 675 : if (S->result) {
305 32 : mysql_free_result(S->result);
306 32 : S->result = NULL;
307 : }
308 :
309 675 : if (mysql_real_query(H->server, stmt->active_query_string, stmt->active_query_stringlen) != 0) {
310 19 : pdo_mysql_error_stmt(stmt);
311 19 : PDO_DBG_RETURN(0);
312 : }
313 :
314 656 : row_count = mysql_affected_rows(H->server);
315 656 : if (row_count == (my_ulonglong)-1) {
316 : /* we either have a query that returned a result set or an error occured
317 : lets see if we have access to a result set */
318 435 : if (!H->buffered) {
319 10 : S->result = mysql_use_result(H->server);
320 : } else {
321 425 : S->result = mysql_store_result(H->server);
322 : }
323 435 : if (NULL == S->result) {
324 0 : pdo_mysql_error_stmt(stmt);
325 0 : PDO_DBG_RETURN(0);
326 : }
327 :
328 435 : stmt->row_count = (long) mysql_num_rows(S->result);
329 435 : stmt->column_count = (int) mysql_num_fields(S->result);
330 435 : S->fields = mysql_fetch_fields(S->result);
331 :
332 : } else {
333 : /* this was a DML or DDL query (INSERT, UPDATE, DELETE, ... */
334 221 : stmt->row_count = (long) row_count;
335 : }
336 :
337 656 : PDO_DBG_RETURN(1);
338 : }
339 : /* {{{ */
340 :
341 : static int pdo_mysql_stmt_next_rowset(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
342 42 : {
343 : #if HAVE_MYSQL_NEXT_RESULT || PDO_USE_MYSQLND
344 42 : pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
345 42 : pdo_mysql_db_handle *H = S->H;
346 : long row_count;
347 : int ret;
348 42 : PDO_DBG_ENTER("pdo_mysql_stmt_next_rowset");
349 42 : PDO_DBG_INF_FMT("stmt=%p", S->stmt);
350 :
351 : #if PDO_USE_MYSQLND
352 42 : if (!H->emulate_prepare) {
353 18 : if (!mysqlnd_stmt_more_results(S->stmt)) {
354 4 : PDO_DBG_RETURN(0);
355 : }
356 14 : if (mysqlnd_stmt_next_result(S->stmt)) {
357 0 : PDO_DBG_RETURN(0);
358 : }
359 :
360 14 : if (!mysqlnd_stmt_more_results(S->stmt)) {
361 : /*
362 : MySQL gives us n + 1 result sets for
363 : CALL proc() and n result sets returned by the proc itself.
364 : Result set n + 1 is about the procedure call itself.
365 : As the PDO emulation does not return it, we skip it as well
366 : */
367 12 : PDO_DBG_RETURN(0);
368 : }
369 :
370 : /* TODO - this code is stolen from execute() - see above */
371 2 : if (S->result) {
372 2 : mysql_free_result(S->result);
373 2 : S->result = NULL;
374 : }
375 : {
376 : /* for SHOW/DESCRIBE and others the column/field count is not available before execute */
377 : int i;
378 :
379 2 : stmt->column_count = S->stmt->field_count;
380 6 : for (i = 0; i < stmt->column_count; i++) {
381 4 : mysqlnd_stmt_bind_one_result(S->stmt, i);
382 : }
383 : }
384 :
385 2 : S->result = mysqlnd_stmt_result_metadata(S->stmt);
386 2 : if (S->result) {
387 2 : S->fields = mysql_fetch_fields(S->result);
388 :
389 : /* if buffered, pre-fetch all the data */
390 2 : if (H->buffered) {
391 1 : if (mysql_stmt_store_result(S->stmt))
392 0 : PDO_DBG_RETURN(1);
393 : }
394 : }
395 2 : row_count = (long) mysql_stmt_affected_rows(S->stmt);
396 2 : if (row_count != (long)-1) {
397 1 : stmt->row_count = row_count;
398 : }
399 2 : PDO_DBG_RETURN(1);
400 : }
401 : #endif
402 :
403 : /* ensure that we free any previous unfetched results */
404 : #if HAVE_MYSQL_STMT_PREPARE
405 : if (S->stmt) {
406 : stmt->column_count = (int)mysql_num_fields(S->result);
407 : mysql_stmt_free_result(S->stmt);
408 : }
409 : #endif
410 24 : if (S->result) {
411 20 : mysql_free_result(S->result);
412 20 : S->result = NULL;
413 : }
414 :
415 24 : ret = mysql_next_result(H->server);
416 :
417 24 : if (ret > 0) {
418 7 : pdo_mysql_error_stmt(stmt);
419 7 : PDO_DBG_RETURN(0);
420 17 : } else if (ret < 0) {
421 : /* No more results */
422 0 : PDO_DBG_RETURN(0);
423 : } else {
424 17 : if (!H->buffered) {
425 2 : S->result = mysql_use_result(H->server);
426 2 : row_count = 0;
427 : } else {
428 15 : S->result = mysql_store_result(H->server);
429 15 : if ((my_ulonglong)-1 == (row_count = mysql_affected_rows(H->server))) {
430 0 : pdo_mysql_error_stmt(stmt);
431 0 : PDO_DBG_RETURN(0);
432 : }
433 : }
434 :
435 17 : if (NULL == S->result) {
436 14 : PDO_DBG_RETURN(0);
437 : }
438 :
439 3 : stmt->row_count = (long) row_count;
440 3 : stmt->column_count = (int) mysql_num_fields(S->result);
441 3 : S->fields = mysql_fetch_fields(S->result);
442 3 : PDO_DBG_RETURN(1);
443 : }
444 : #else
445 : strcpy(stmt->error_code, "HYC00");
446 : PDO_DBG_RETURN(0);
447 : #endif /* HAVE_MYSQL_STMT_PREPARE */
448 : }
449 : /* }}} */
450 :
451 :
452 : static const char * const pdo_param_event_names[] =
453 : {
454 : "PDO_PARAM_EVT_ALLOC",
455 : "PDO_PARAM_EVT_FREE",
456 : "PDO_PARAM_EVT_EXEC_PRE",
457 : "PDO_PARAM_EVT_EXEC_POST",
458 : "PDO_PARAM_EVT_FETCH_PRE",
459 : "PDO_PARAM_EVT_FETCH_POST",
460 : "PDO_PARAM_EVT_NORMALIZE",
461 : };
462 :
463 :
464 : static int pdo_mysql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
465 : enum pdo_param_event event_type TSRMLS_DC) /* {{{ */
466 35685 : {
467 : #ifndef PDO_USE_MYSQLND
468 : PDO_MYSQL_PARAM_BIND *b;
469 : #endif
470 : #if HAVE_MYSQL_STMT_PREPARE || PDO_USE_MYSQLND
471 35685 : pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
472 :
473 35685 : PDO_DBG_ENTER("pdo_mysql_stmt_param_hook");
474 35685 : PDO_DBG_INF_FMT("stmt=%p", S->stmt);
475 35685 : PDO_DBG_INF_FMT("event = %s", pdo_param_event_names[event_type]);
476 35685 : if (S->stmt && param->is_param) {
477 2039 : switch (event_type) {
478 : case PDO_PARAM_EVT_ALLOC:
479 : /* sanity check parameter number range */
480 461 : if (param->paramno < 0 || param->paramno >= S->num_params) {
481 100 : strcpy(stmt->error_code, "HY093");
482 100 : PDO_DBG_RETURN(0);
483 : }
484 361 : S->params_given++;
485 :
486 : #ifndef PDO_USE_MYSQLND
487 : b = &S->params[param->paramno];
488 : param->driver_data = b;
489 : b->is_null = &S->in_null[param->paramno];
490 : b->length = &S->in_length[param->paramno];
491 : /* recall how many parameters have been provided */
492 : #endif
493 361 : PDO_DBG_RETURN(1);
494 :
495 : case PDO_PARAM_EVT_EXEC_PRE:
496 272 : if (S->params_given < (unsigned int) S->num_params) {
497 : /* too few parameter bound */
498 1 : PDO_DBG_ERR("too few parameters bound");
499 1 : strcpy(stmt->error_code, "HY093");
500 1 : PDO_DBG_RETURN(0);
501 : }
502 :
503 : #if PDO_USE_MYSQLND
504 271 : if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL ||
505 : Z_TYPE_P(param->parameter) == IS_NULL) {
506 6 : mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, param->parameter, MYSQL_TYPE_NULL);
507 6 : PDO_DBG_RETURN(1);
508 : }
509 : #else
510 : b = (PDO_MYSQL_PARAM_BIND*)param->driver_data;
511 : *b->is_null = 0;
512 : if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL ||
513 : Z_TYPE_P(param->parameter) == IS_NULL) {
514 : *b->is_null = 1;
515 : b->buffer_type = MYSQL_TYPE_STRING;
516 : b->buffer = NULL;
517 : b->buffer_length = 0;
518 : *b->length = 0;
519 : PDO_DBG_RETURN(1);
520 : }
521 : #endif /* PDO_USE_MYSQLND */
522 :
523 265 : switch (PDO_PARAM_TYPE(param->param_type)) {
524 : case PDO_PARAM_STMT:
525 0 : PDO_DBG_RETURN(0);
526 : case PDO_PARAM_LOB:
527 1 : PDO_DBG_INF("PDO_PARAM_LOB");
528 1 : if (Z_TYPE_P(param->parameter) == IS_RESOURCE) {
529 : php_stream *stm;
530 1 : php_stream_from_zval_no_verify(stm, ¶m->parameter);
531 1 : if (stm) {
532 1 : SEPARATE_ZVAL_IF_NOT_REF(¶m->parameter);
533 1 : Z_TYPE_P(param->parameter) = IS_STRING;
534 1 : Z_STRLEN_P(param->parameter) = php_stream_copy_to_mem(stm,
535 : &Z_STRVAL_P(param->parameter), PHP_STREAM_COPY_ALL, 0);
536 : } else {
537 0 : pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource" TSRMLS_CC);
538 0 : return 0;
539 : }
540 : }
541 : /* fall through */
542 :
543 : default:
544 : ;
545 : }
546 :
547 : #if PDO_USE_MYSQLND
548 : /* Is it really correct to check the zval's type? - But well, that's what the old code below does, too */
549 265 : PDO_DBG_INF_FMT("param->parameter->type=%d", Z_TYPE_P(param->parameter));
550 265 : switch (Z_TYPE_P(param->parameter)) {
551 : case IS_STRING:
552 258 : mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, param->parameter, MYSQL_TYPE_VAR_STRING);
553 258 : break;
554 : case IS_LONG:
555 : #if SIZEOF_LONG==8
556 : mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, param->parameter, MYSQL_TYPE_LONGLONG);
557 : #elif SIZEOF_LONG==4
558 6 : mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, param->parameter, MYSQL_TYPE_LONG);
559 : #endif /* SIZEOF_LONG */
560 6 : break;
561 : case IS_DOUBLE:
562 0 : mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, param->parameter, MYSQL_TYPE_DOUBLE);
563 0 : break;
564 : default:
565 1 : PDO_DBG_RETURN(0);
566 : }
567 :
568 264 : PDO_DBG_RETURN(1);
569 : #else
570 : PDO_DBG_INF_FMT("param->parameter->type=%d", Z_TYPE_P(param->parameter));
571 : switch (Z_TYPE_P(param->parameter)) {
572 : case IS_STRING:
573 : b->buffer_type = MYSQL_TYPE_STRING;
574 : b->buffer = Z_STRVAL_P(param->parameter);
575 : b->buffer_length = Z_STRLEN_P(param->parameter);
576 : *b->length = Z_STRLEN_P(param->parameter);
577 : PDO_DBG_RETURN(1);
578 :
579 : case IS_LONG:
580 : b->buffer_type = MYSQL_TYPE_LONG;
581 : b->buffer = &Z_LVAL_P(param->parameter);
582 : PDO_DBG_RETURN(1);
583 :
584 : case IS_DOUBLE:
585 : b->buffer_type = MYSQL_TYPE_DOUBLE;
586 : b->buffer = &Z_DVAL_P(param->parameter);
587 : PDO_DBG_RETURN(1);
588 :
589 : default:
590 : PDO_DBG_RETURN(0);
591 : }
592 : #endif /* PDO_USE_MYSQLND */
593 : case PDO_PARAM_EVT_FREE:
594 : case PDO_PARAM_EVT_EXEC_POST:
595 : case PDO_PARAM_EVT_FETCH_PRE:
596 : case PDO_PARAM_EVT_FETCH_POST:
597 : case PDO_PARAM_EVT_NORMALIZE:
598 : /* do nothing */
599 : break;
600 : }
601 : }
602 : #endif /* HAVE_MYSQL_STMT_PREPARE || PDO_USE_MYSQLND */
603 34952 : PDO_DBG_RETURN(1);
604 : }
605 : /* }}} */
606 :
607 : static int pdo_mysql_stmt_fetch(pdo_stmt_t *stmt,
608 : enum pdo_fetch_orientation ori, long offset TSRMLS_DC) /* {{{ */
609 1111 : {
610 1111 : pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
611 : #if PDO_USE_MYSQLND
612 : zend_bool fetched_anything;
613 :
614 1111 : PDO_DBG_ENTER("pdo_mysql_stmt_fetch");
615 1111 : PDO_DBG_INF_FMT("stmt=%p", S->stmt);
616 1111 : if (S->stmt) {
617 420 : if (FAIL == mysqlnd_stmt_fetch(S->stmt, &fetched_anything) || fetched_anything == FALSE) {
618 56 : PDO_DBG_RETURN(0);
619 : }
620 :
621 364 : PDO_DBG_RETURN(1);
622 : }
623 : #else
624 : # if HAVE_MYSQL_STMT_PREPARE
625 : int ret;
626 :
627 : if (S->stmt) {
628 : ret = mysql_stmt_fetch(S->stmt);
629 :
630 : # ifdef MYSQL_DATA_TRUNCATED
631 : if (ret == MYSQL_DATA_TRUNCATED) {
632 : ret = 0;
633 : }
634 : # endif
635 :
636 : if (ret) {
637 : if (ret != MYSQL_NO_DATA) {
638 : pdo_mysql_error_stmt(stmt);
639 : }
640 : PDO_DBG_RETURN(0);
641 : }
642 :
643 : PDO_DBG_RETURN(1);
644 : }
645 : # endif /* HAVE_MYSQL_STMT_PREPARE */
646 : #endif /* PDO_USE_MYSQLND */
647 :
648 691 : if (!S->result) {
649 1 : strcpy(stmt->error_code, "HY000");
650 1 : PDO_DBG_RETURN(0);
651 : }
652 : #if PDO_USE_MYSQLND
653 690 : if (!S->stmt && S->current_data) {
654 375 : free(S->current_data);
655 : }
656 : #endif /* PDO_USE_MYSQLND */
657 :
658 690 : if ((S->current_data = mysql_fetch_row(S->result)) == NULL) {
659 145 : if (mysql_errno(S->H->server)) {
660 2 : pdo_mysql_error_stmt(stmt);
661 : }
662 145 : PDO_DBG_RETURN(0);
663 : }
664 :
665 545 : S->current_lengths = mysql_fetch_lengths(S->result);
666 545 : PDO_DBG_RETURN(1);
667 : }
668 : /* }}} */
669 :
670 : static int pdo_mysql_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC) /* {{{ */
671 1129 : {
672 1129 : pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
673 1129 : struct pdo_column_data *cols = stmt->columns;
674 : int i;
675 :
676 1129 : PDO_DBG_ENTER("pdo_mysql_stmt_describe");
677 1129 : PDO_DBG_INF_FMT("stmt=%p", S->stmt);
678 1129 : if (!S->result) {
679 0 : PDO_DBG_RETURN(0);
680 : }
681 :
682 1129 : if (colno >= stmt->column_count) {
683 : /* error invalid column */
684 0 : PDO_DBG_RETURN(0);
685 : }
686 :
687 : /* fetch all on demand, this seems easiest
688 : ** if we've been here before bail out
689 : */
690 1129 : if (cols[0].name) {
691 520 : PDO_DBG_RETURN(1);
692 : }
693 1738 : for (i = 0; i < stmt->column_count; i++) {
694 : int namelen;
695 :
696 1129 : if (S->H->fetch_table_names) {
697 1 : namelen = spprintf(&cols[i].name, 0, "%s.%s", S->fields[i].table, S->fields[i].name);
698 1 : cols[i].namelen = namelen;
699 : } else {
700 1128 : namelen = strlen(S->fields[i].name);
701 1128 : cols[i].namelen = namelen;
702 1128 : cols[i].name = estrndup(S->fields[i].name, namelen);
703 : }
704 :
705 1129 : cols[i].precision = S->fields[i].decimals;
706 1129 : cols[i].maxlen = S->fields[i].length;
707 :
708 : #ifdef PDO_USE_MYSQLND
709 1129 : if (S->stmt) {
710 554 : cols[i].param_type = PDO_PARAM_ZVAL;
711 : } else
712 : #endif
713 : {
714 575 : cols[i].param_type = PDO_PARAM_STR;
715 : }
716 : }
717 609 : PDO_DBG_RETURN(1);
718 : }
719 : /* }}} */
720 :
721 : static int pdo_mysql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees TSRMLS_DC) /* {{{ */
722 1982 : {
723 1982 : pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
724 :
725 1982 : PDO_DBG_ENTER("pdo_mysql_stmt_get_col");
726 1982 : PDO_DBG_INF_FMT("stmt=%p", S->stmt);
727 1982 : if (!S->result) {
728 0 : PDO_DBG_RETURN(0);
729 : }
730 :
731 : /* With mysqlnd data is stored inside mysqlnd, not S->current_data */
732 : #if HAVE_MYSQL_STMT_PREPARE || PDO_USE_MYSQLND
733 1982 : if (!S->stmt) {
734 : #endif
735 1140 : if (S->current_data == NULL || !S->result) {
736 0 : PDO_DBG_RETURN(0);
737 : }
738 : #if HAVE_MYSQL_STMT_PREPARE || PDO_USE_MYSQLND
739 : }
740 : #endif
741 1982 : if (colno >= stmt->column_count) {
742 : /* error invalid column */
743 0 : PDO_DBG_RETURN(0);
744 : }
745 : #if PDO_USE_MYSQLND
746 1982 : if (S->stmt) {
747 842 : Z_ADDREF_P(S->stmt->result_bind[colno].zv);
748 842 : *ptr = (char*)&S->stmt->result_bind[colno].zv;
749 842 : *len = sizeof(zval);
750 842 : PDO_DBG_RETURN(1);
751 : }
752 : #elif HAVE_MYSQL_STMT_PREPARE
753 : if (S->stmt) {
754 : if (S->out_null[colno]) {
755 : *ptr = NULL;
756 : *len = 0;
757 : PDO_DBG_RETURN(1);
758 : }
759 : *ptr = S->bound_result[colno].buffer;
760 : if (S->out_length[colno] > S->bound_result[colno].buffer_length) {
761 : /* mysql lied about the column width */
762 : strcpy(stmt->error_code, "01004"); /* truncated */
763 : S->out_length[colno] = S->bound_result[colno].buffer_length;
764 : *len = S->out_length[colno];
765 : PDO_DBG_RETURN(0);
766 : }
767 : *len = S->out_length[colno];
768 : PDO_DBG_RETURN(1);
769 : }
770 : #endif /* PDO_USE_MYSQLND else HAVE_MYSQL_STMT_PREPARE */
771 1140 : *ptr = S->current_data[colno];
772 1140 : *len = S->current_lengths[colno];
773 1140 : PDO_DBG_RETURN(1);
774 : } /* }}} */
775 :
776 : static char *type_to_name_native(int type) /* }}} */
777 68 : {
778 : #define PDO_MYSQL_NATIVE_TYPE_NAME(x) case FIELD_TYPE_##x: return #x;
779 :
780 68 : switch (type) {
781 7 : PDO_MYSQL_NATIVE_TYPE_NAME(STRING)
782 5 : PDO_MYSQL_NATIVE_TYPE_NAME(VAR_STRING)
783 : #ifdef MYSQL_HAS_TINY
784 : PDO_MYSQL_NATIVE_TYPE_NAME(TINY)
785 : #endif
786 2 : PDO_MYSQL_NATIVE_TYPE_NAME(SHORT)
787 8 : PDO_MYSQL_NATIVE_TYPE_NAME(LONG)
788 2 : PDO_MYSQL_NATIVE_TYPE_NAME(LONGLONG)
789 2 : PDO_MYSQL_NATIVE_TYPE_NAME(INT24)
790 4 : PDO_MYSQL_NATIVE_TYPE_NAME(FLOAT)
791 8 : PDO_MYSQL_NATIVE_TYPE_NAME(DOUBLE)
792 0 : PDO_MYSQL_NATIVE_TYPE_NAME(DECIMAL)
793 : #ifdef FIELD_TYPE_NEWDECIMAL
794 8 : PDO_MYSQL_NATIVE_TYPE_NAME(NEWDECIMAL)
795 : #endif
796 : #ifdef FIELD_TYPE_GEOMETRY
797 0 : PDO_MYSQL_NATIVE_TYPE_NAME(GEOMETRY)
798 : #endif
799 1 : PDO_MYSQL_NATIVE_TYPE_NAME(TIMESTAMP)
800 : #ifdef MYSQL_HAS_YEAR
801 : PDO_MYSQL_NATIVE_TYPE_NAME(YEAR)
802 : #endif
803 0 : PDO_MYSQL_NATIVE_TYPE_NAME(SET)
804 0 : PDO_MYSQL_NATIVE_TYPE_NAME(ENUM)
805 1 : PDO_MYSQL_NATIVE_TYPE_NAME(DATE)
806 : #ifdef FIELD_TYPE_NEWDATE
807 0 : PDO_MYSQL_NATIVE_TYPE_NAME(NEWDATE)
808 : #endif
809 1 : PDO_MYSQL_NATIVE_TYPE_NAME(TIME)
810 1 : PDO_MYSQL_NATIVE_TYPE_NAME(DATETIME)
811 0 : PDO_MYSQL_NATIVE_TYPE_NAME(TINY_BLOB)
812 0 : PDO_MYSQL_NATIVE_TYPE_NAME(MEDIUM_BLOB)
813 0 : PDO_MYSQL_NATIVE_TYPE_NAME(LONG_BLOB)
814 12 : PDO_MYSQL_NATIVE_TYPE_NAME(BLOB)
815 1 : PDO_MYSQL_NATIVE_TYPE_NAME(NULL)
816 : default:
817 5 : return NULL;
818 : }
819 : #undef PDO_MYSQL_NATIVE_TYPE_NAME
820 : } /* }}} */
821 :
822 : static int pdo_mysql_stmt_col_meta(pdo_stmt_t *stmt, long colno, zval *return_value TSRMLS_DC) /* {{{ */
823 71 : {
824 71 : pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
825 : const MYSQL_FIELD *F;
826 : zval *flags;
827 : char *str;
828 :
829 71 : PDO_DBG_ENTER("pdo_mysql_stmt_col_meta");
830 71 : PDO_DBG_INF_FMT("stmt=%p", S->stmt);
831 71 : if (!S->result) {
832 2 : PDO_DBG_RETURN(FAILURE);
833 : }
834 69 : if (colno >= stmt->column_count) {
835 : /* error invalid column */
836 1 : PDO_DBG_RETURN(FAILURE);
837 : }
838 :
839 68 : array_init(return_value);
840 68 : MAKE_STD_ZVAL(flags);
841 68 : array_init(flags);
842 :
843 68 : F = S->fields + colno;
844 :
845 68 : if (F->def) {
846 0 : add_assoc_string(return_value, "mysql:def", F->def, 1);
847 : }
848 68 : if (IS_NOT_NULL(F->flags)) {
849 5 : add_next_index_string(flags, "not_null", 1);
850 : }
851 68 : if (IS_PRI_KEY(F->flags)) {
852 3 : add_next_index_string(flags, "primary_key", 1);
853 : }
854 68 : if (F->flags & MULTIPLE_KEY_FLAG) {
855 1 : add_next_index_string(flags, "multiple_key", 1);
856 : }
857 68 : if (F->flags & UNIQUE_KEY_FLAG) {
858 1 : add_next_index_string(flags, "unique_key", 1);
859 : }
860 68 : if (IS_BLOB(F->flags)) {
861 12 : add_next_index_string(flags, "blob", 1);
862 : }
863 68 : str = type_to_name_native(F->type);
864 68 : if (str) {
865 63 : add_assoc_string(return_value, "native_type", str, 1);
866 : }
867 :
868 : #ifdef PDO_USE_MYSQLND
869 68 : switch (F->type) {
870 : case MYSQL_TYPE_BIT:
871 : case MYSQL_TYPE_YEAR:
872 : case MYSQL_TYPE_TINY:
873 : case MYSQL_TYPE_SHORT:
874 : case MYSQL_TYPE_INT24:
875 : case MYSQL_TYPE_LONG:
876 : #if SIZEOF_LONG==8
877 : case MYSQL_TYPE_LONGLONG:
878 : #endif
879 17 : add_assoc_long(return_value, "pdo_type", PDO_PARAM_INT);
880 17 : break;
881 : default:
882 51 : add_assoc_long(return_value, "pdo_type", PDO_PARAM_STR);
883 : break;
884 : }
885 : #endif
886 :
887 68 : add_assoc_zval(return_value, "flags", flags);
888 68 : add_assoc_string(return_value, "table",(F->table?F->table:""), 1);
889 68 : PDO_DBG_RETURN(SUCCESS);
890 : } /* }}} */
891 :
892 : static int pdo_mysql_stmt_cursor_closer(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
893 251 : {
894 251 : pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
895 :
896 251 : PDO_DBG_ENTER("pdo_mysql_stmt_cursor_closer");
897 251 : PDO_DBG_INF_FMT("stmt=%p", S->stmt);
898 251 : if (S->result) {
899 242 : mysql_free_result(S->result);
900 242 : S->result = NULL;
901 : }
902 : #if HAVE_MYSQL_STMT_PREPARE || PDO_USE_MYSQLND
903 251 : if (S->stmt) {
904 : int retval;
905 144 : retval = mysql_stmt_free_result(S->stmt);
906 144 : PDO_DBG_RETURN(retval ? 0 : 1);
907 : }
908 : #endif
909 :
910 : #if HAVE_MYSQL_NEXT_RESULT || PDO_USE_MYSQLND
911 215 : while (mysql_more_results(S->H->server)) {
912 : MYSQL_RES *res;
913 1 : if (mysql_next_result(S->H->server) != 0) {
914 0 : break;
915 : }
916 1 : res = mysql_store_result(S->H->server);
917 1 : if (res) {
918 0 : mysql_free_result(res);
919 : }
920 : }
921 : #endif
922 107 : PDO_DBG_RETURN(1);
923 : }
924 : /* }}} */
925 :
926 : struct pdo_stmt_methods mysql_stmt_methods = {
927 : pdo_mysql_stmt_dtor,
928 : pdo_mysql_stmt_execute,
929 : pdo_mysql_stmt_fetch,
930 : pdo_mysql_stmt_describe,
931 : pdo_mysql_stmt_get_col,
932 : pdo_mysql_stmt_param_hook,
933 : NULL, /* set_attr */
934 : NULL, /* get_attr */
935 : pdo_mysql_stmt_col_meta,
936 : pdo_mysql_stmt_next_rowset,
937 : pdo_mysql_stmt_cursor_closer
938 : };
939 :
940 : /*
941 : * Local variables:
942 : * tab-width: 4
943 : * c-basic-offset: 4
944 : * End:
945 : * vim600: noet sw=4 ts=4 fdm=marker
946 : * vim<600: noet sw=4 ts=4
947 : */
|