1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
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 280838 2009-05-20 08:30:12Z 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 1534 : {
49 1534 : pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
50 :
51 1534 : PDO_DBG_ENTER("pdo_mysql_stmt_dtor");
52 1534 : PDO_DBG_INF_FMT("stmt=%p", S->stmt);
53 1534 : if (S->result) {
54 : /* free the resource */
55 473 : mysql_free_result(S->result);
56 473 : S->result = NULL;
57 : }
58 1534 : if (S->einfo.errmsg) {
59 26 : pefree(S->einfo.errmsg, stmt->dbh->is_persistent);
60 26 : S->einfo.errmsg = NULL;
61 : }
62 : #if HAVE_MYSQL_STMT_PREPARE || PDO_USE_MYSQLND
63 1534 : if (S->stmt) {
64 732 : pdo_mysql_stmt_close(S->stmt);
65 732 : 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 1534 : if (S->H->server) {
99 3070 : while (mysql_more_results(S->H->server)) {
100 : MYSQL_RES *res;
101 4 : if (mysql_next_result(S->H->server) != 0) {
102 2 : break;
103 : }
104 :
105 2 : res = mysql_store_result(S->H->server);
106 2 : if (res) {
107 1 : mysql_free_result(res);
108 : }
109 : }
110 : }
111 : #endif /* HAVE_MYSQL_NEXT_RESULT || PDO_USE_MYSQLND */
112 : #if PDO_USE_MYSQLND
113 1534 : if (!S->stmt && S->current_data) {
114 186 : free(S->current_data);
115 : }
116 : #endif /* PDO_USE_MYSQLND */
117 :
118 1534 : efree(S);
119 1534 : PDO_DBG_RETURN(1);
120 : }
121 : /* }}} */
122 :
123 : static void pdo_mysql_stmt_set_row_count(pdo_stmt_t *stmt) /* {{{ */
124 594 : {
125 : long row_count;
126 594 : pdo_mysql_stmt *S = stmt->driver_data;
127 594 : row_count = (long) mysql_stmt_affected_rows(S->stmt);
128 594 : if (row_count != (long)-1) {
129 580 : stmt->row_count = row_count;
130 : }
131 594 : }
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 597 : {
249 597 : pdo_mysql_stmt *S = stmt->driver_data;
250 597 : pdo_mysql_db_handle *H = S->H;
251 : int i;
252 :
253 597 : PDO_DBG_ENTER("pdo_mysql_stmt_execute_prepared_mysqlnd");
254 :
255 597 : if (mysql_stmt_execute(S->stmt)) {
256 3 : pdo_mysql_error_stmt(stmt);
257 3 : PDO_DBG_RETURN(0);
258 : }
259 :
260 594 : if (S->result) {
261 : /* TODO: add a test to check if we really have zvals here... */
262 15 : mysql_free_result(S->result);
263 15 : S->result = NULL;
264 : }
265 :
266 : /* for SHOW/DESCRIBE and others the column/field count is not available before execute */
267 594 : stmt->column_count = S->stmt->field_count;
268 1345 : for (i = 0; i < stmt->column_count; i++) {
269 751 : mysqlnd_stmt_bind_one_result(S->stmt, i);
270 : }
271 :
272 594 : S->result = mysqlnd_stmt_result_metadata(S->stmt);
273 594 : if (S->result) {
274 366 : S->fields = mysql_fetch_fields(S->result);
275 : /* if buffered, pre-fetch all the data */
276 366 : if (H->buffered) {
277 352 : if (mysql_stmt_store_result(S->stmt)) {
278 0 : PDO_DBG_RETURN(0);
279 : }
280 : }
281 : }
282 :
283 594 : pdo_mysql_stmt_set_row_count(stmt);
284 594 : PDO_DBG_RETURN(1);
285 : }
286 : /* }}} */
287 : #endif
288 :
289 : static int pdo_mysql_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
290 1345 : {
291 1345 : pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
292 1345 : pdo_mysql_db_handle *H = S->H;
293 : my_ulonglong row_count;
294 1345 : PDO_DBG_ENTER("pdo_mysql_stmt_execute");
295 1345 : PDO_DBG_INF_FMT("stmt=%p", S->stmt);
296 :
297 : #if HAVE_MYSQL_STMT_PREPARE || PDO_USE_MYSQLND
298 1345 : if (S->stmt) {
299 597 : PDO_DBG_RETURN(pdo_mysql_stmt_execute_prepared(stmt));
300 : }
301 : #endif
302 :
303 : /* ensure that we free any previous unfetched results */
304 748 : if (S->result) {
305 35 : mysql_free_result(S->result);
306 35 : S->result = NULL;
307 : }
308 :
309 748 : if (mysql_real_query(H->server, stmt->active_query_string, stmt->active_query_stringlen) != 0) {
310 21 : pdo_mysql_error_stmt(stmt);
311 21 : PDO_DBG_RETURN(0);
312 : }
313 :
314 727 : row_count = mysql_affected_rows(H->server);
315 727 : 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 485 : if (!H->buffered) {
319 10 : S->result = mysql_use_result(H->server);
320 : } else {
321 475 : S->result = mysql_store_result(H->server);
322 : }
323 485 : if (NULL == S->result) {
324 0 : pdo_mysql_error_stmt(stmt);
325 0 : PDO_DBG_RETURN(0);
326 : }
327 :
328 485 : stmt->row_count = (long) mysql_num_rows(S->result);
329 485 : stmt->column_count = (int) mysql_num_fields(S->result);
330 485 : S->fields = mysql_fetch_fields(S->result);
331 :
332 : } else {
333 : /* this was a DML or DDL query (INSERT, UPDATE, DELETE, ... */
334 242 : stmt->row_count = (long) row_count;
335 : }
336 :
337 727 : PDO_DBG_RETURN(1);
338 : }
339 : /* {{{ */
340 :
341 : static int pdo_mysql_stmt_next_rowset(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
342 43 : {
343 : #if HAVE_MYSQL_NEXT_RESULT || PDO_USE_MYSQLND
344 43 : pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
345 43 : pdo_mysql_db_handle *H = S->H;
346 : long row_count;
347 : int ret;
348 43 : PDO_DBG_ENTER("pdo_mysql_stmt_next_rowset");
349 43 : PDO_DBG_INF_FMT("stmt=%p", S->stmt);
350 :
351 : #if PDO_USE_MYSQLND
352 43 : if (!H->emulate_prepare) {
353 20 : if (!mysqlnd_stmt_more_results(S->stmt)) {
354 4 : PDO_DBG_RETURN(0);
355 : }
356 16 : if (mysqlnd_stmt_next_result(S->stmt)) {
357 0 : PDO_DBG_RETURN(0);
358 : }
359 :
360 16 : 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 14 : 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 : }
396 2 : row_count = (long) mysql_stmt_affected_rows(S->stmt);
397 2 : if (row_count != (long)-1) {
398 1 : stmt->row_count = row_count;
399 : }
400 2 : PDO_DBG_RETURN(1);
401 : }
402 : #endif
403 :
404 : /* ensure that we free any previous unfetched results */
405 : #if HAVE_MYSQL_STMT_PREPARE
406 : if (S->stmt) {
407 : stmt->column_count = (int)mysql_num_fields(S->result);
408 : mysql_stmt_free_result(S->stmt);
409 : }
410 : #endif
411 23 : if (S->result) {
412 20 : mysql_free_result(S->result);
413 20 : S->result = NULL;
414 : }
415 :
416 23 : ret = mysql_next_result(H->server);
417 :
418 23 : if (ret > 0) {
419 7 : pdo_mysql_error_stmt(stmt);
420 7 : PDO_DBG_RETURN(0);
421 16 : } else if (ret < 0) {
422 : /* No more results */
423 0 : PDO_DBG_RETURN(0);
424 : } else {
425 16 : if (!H->buffered) {
426 2 : S->result = mysql_use_result(H->server);
427 2 : row_count = 0;
428 : } else {
429 14 : S->result = mysql_store_result(H->server);
430 14 : if ((long)-1 == (row_count = (long) mysql_affected_rows(H->server))) {
431 0 : pdo_mysql_error_stmt(stmt);
432 0 : PDO_DBG_RETURN(0);
433 : }
434 : }
435 :
436 16 : if (NULL == S->result) {
437 13 : PDO_DBG_RETURN(0);
438 : }
439 :
440 3 : stmt->row_count = row_count;
441 3 : stmt->column_count = (int) mysql_num_fields(S->result);
442 3 : S->fields = mysql_fetch_fields(S->result);
443 3 : PDO_DBG_RETURN(1);
444 : }
445 : #else
446 : strcpy(stmt->error_code, "HYC00");
447 : PDO_DBG_RETURN(0);
448 : #endif /* HAVE_MYSQL_STMT_PREPARE */
449 : }
450 : /* }}} */
451 :
452 :
453 : static const char * const pdo_param_event_names[] =
454 : {
455 : "PDO_PARAM_EVT_ALLOC",
456 : "PDO_PARAM_EVT_FREE",
457 : "PDO_PARAM_EVT_EXEC_PRE",
458 : "PDO_PARAM_EVT_EXEC_POST",
459 : "PDO_PARAM_EVT_FETCH_PRE",
460 : "PDO_PARAM_EVT_FETCH_POST",
461 : "PDO_PARAM_EVT_NORMALIZE",
462 : };
463 :
464 :
465 : static int pdo_mysql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
466 : enum pdo_param_event event_type TSRMLS_DC) /* {{{ */
467 36179 : {
468 : #ifndef PDO_USE_MYSQLND
469 : PDO_MYSQL_PARAM_BIND *b;
470 : #endif
471 : #if HAVE_MYSQL_STMT_PREPARE || PDO_USE_MYSQLND
472 36179 : pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
473 :
474 36179 : PDO_DBG_ENTER("pdo_mysql_stmt_param_hook");
475 36179 : PDO_DBG_INF_FMT("stmt=%p", S->stmt);
476 36179 : PDO_DBG_INF_FMT("event = %s", pdo_param_event_names[event_type]);
477 36179 : if (S->stmt && param->is_param) {
478 2420 : switch (event_type) {
479 : case PDO_PARAM_EVT_ALLOC:
480 : /* sanity check parameter number range */
481 574 : if (param->paramno < 0 || param->paramno >= S->num_params) {
482 100 : strcpy(stmt->error_code, "HY093");
483 100 : PDO_DBG_RETURN(0);
484 : }
485 474 : S->params_given++;
486 :
487 : #ifndef PDO_USE_MYSQLND
488 : b = &S->params[param->paramno];
489 : param->driver_data = b;
490 : b->is_null = &S->in_null[param->paramno];
491 : b->length = &S->in_length[param->paramno];
492 : /* recall how many parameters have been provided */
493 : #endif
494 474 : PDO_DBG_RETURN(1);
495 :
496 : case PDO_PARAM_EVT_EXEC_PRE:
497 287 : if (S->params_given < (unsigned int) S->num_params) {
498 : /* too few parameter bound */
499 3 : PDO_DBG_ERR("too few parameters bound");
500 3 : strcpy(stmt->error_code, "HY093");
501 3 : PDO_DBG_RETURN(0);
502 : }
503 :
504 : #if PDO_USE_MYSQLND
505 284 : if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL ||
506 : Z_TYPE_P(param->parameter) == IS_NULL) {
507 2 : mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, param->parameter, MYSQL_TYPE_NULL);
508 2 : PDO_DBG_RETURN(1);
509 : }
510 : #else
511 : b = (PDO_MYSQL_PARAM_BIND*)param->driver_data;
512 : *b->is_null = 0;
513 : if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL ||
514 : Z_TYPE_P(param->parameter) == IS_NULL) {
515 : *b->is_null = 1;
516 : b->buffer_type = MYSQL_TYPE_STRING;
517 : b->buffer = NULL;
518 : b->buffer_length = 0;
519 : *b->length = 0;
520 : PDO_DBG_RETURN(1);
521 : }
522 : #endif /* PDO_USE_MYSQLND */
523 :
524 282 : switch (PDO_PARAM_TYPE(param->param_type)) {
525 : case PDO_PARAM_STMT:
526 0 : PDO_DBG_RETURN(0);
527 : case PDO_PARAM_LOB:
528 1 : PDO_DBG_INF("PDO_PARAM_LOB");
529 1 : if (Z_TYPE_P(param->parameter) == IS_RESOURCE) {
530 : php_stream *stm;
531 1 : php_stream_from_zval_no_verify(stm, ¶m->parameter);
532 1 : if (stm) {
533 1 : SEPARATE_ZVAL_IF_NOT_REF(¶m->parameter);
534 1 : Z_TYPE_P(param->parameter) = IS_STRING;
535 1 : Z_STRLEN_P(param->parameter) = php_stream_copy_to_mem(stm,
536 : &Z_STRVAL_P(param->parameter), PHP_STREAM_COPY_ALL, 0);
537 : } else {
538 0 : pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource" TSRMLS_CC);
539 0 : return 0;
540 : }
541 : }
542 : /* fall through */
543 :
544 : default:
545 : ;
546 : }
547 :
548 : #if PDO_USE_MYSQLND
549 : /* Is it really correct to check the zval's type? - But well, that's what the old code below does, too */
550 282 : PDO_DBG_INF_FMT("param->parameter->type=%d", Z_TYPE_P(param->parameter));
551 282 : switch (Z_TYPE_P(param->parameter)) {
552 : case IS_STRING:
553 275 : mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, param->parameter, MYSQL_TYPE_VAR_STRING);
554 275 : break;
555 : case IS_LONG:
556 : #if SIZEOF_LONG==8
557 : mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, param->parameter, MYSQL_TYPE_LONGLONG);
558 : #elif SIZEOF_LONG==4
559 6 : mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, param->parameter, MYSQL_TYPE_LONG);
560 : #endif /* SIZEOF_LONG */
561 6 : break;
562 : case IS_DOUBLE:
563 0 : mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, param->parameter, MYSQL_TYPE_DOUBLE);
564 0 : break;
565 : default:
566 1 : PDO_DBG_RETURN(0);
567 : }
568 :
569 281 : PDO_DBG_RETURN(1);
570 : #else
571 : PDO_DBG_INF_FMT("param->parameter->type=%d", Z_TYPE_P(param->parameter));
572 : switch (Z_TYPE_P(param->parameter)) {
573 : case IS_STRING:
574 : b->buffer_type = MYSQL_TYPE_STRING;
575 : b->buffer = Z_STRVAL_P(param->parameter);
576 : b->buffer_length = Z_STRLEN_P(param->parameter);
577 : *b->length = Z_STRLEN_P(param->parameter);
578 : PDO_DBG_RETURN(1);
579 :
580 : case IS_LONG:
581 : b->buffer_type = MYSQL_TYPE_LONG;
582 : b->buffer = &Z_LVAL_P(param->parameter);
583 : PDO_DBG_RETURN(1);
584 :
585 : case IS_DOUBLE:
586 : b->buffer_type = MYSQL_TYPE_DOUBLE;
587 : b->buffer = &Z_DVAL_P(param->parameter);
588 : PDO_DBG_RETURN(1);
589 :
590 : default:
591 : PDO_DBG_RETURN(0);
592 : }
593 : #endif /* PDO_USE_MYSQLND */
594 : case PDO_PARAM_EVT_FREE:
595 : case PDO_PARAM_EVT_EXEC_POST:
596 : case PDO_PARAM_EVT_FETCH_PRE:
597 : case PDO_PARAM_EVT_FETCH_POST:
598 : case PDO_PARAM_EVT_NORMALIZE:
599 : /* do nothing */
600 : break;
601 : }
602 : }
603 : #endif /* HAVE_MYSQL_STMT_PREPARE || PDO_USE_MYSQLND */
604 35318 : PDO_DBG_RETURN(1);
605 : }
606 : /* }}} */
607 :
608 : static int pdo_mysql_stmt_fetch(pdo_stmt_t *stmt,
609 : enum pdo_fetch_orientation ori, long offset TSRMLS_DC) /* {{{ */
610 1258 : {
611 1258 : pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
612 : #if PDO_USE_MYSQLND
613 : zend_bool fetched_anything;
614 :
615 1258 : PDO_DBG_ENTER("pdo_mysql_stmt_fetch");
616 1258 : PDO_DBG_INF_FMT("stmt=%p", S->stmt);
617 1258 : if (S->stmt) {
618 450 : if (FAIL == mysqlnd_stmt_fetch(S->stmt, &fetched_anything) || fetched_anything == FALSE) {
619 65 : PDO_DBG_RETURN(0);
620 : }
621 :
622 385 : PDO_DBG_RETURN(1);
623 : }
624 : #else
625 : # if HAVE_MYSQL_STMT_PREPARE
626 : int ret;
627 :
628 : if (S->stmt) {
629 : ret = mysql_stmt_fetch(S->stmt);
630 :
631 : # ifdef MYSQL_DATA_TRUNCATED
632 : if (ret == MYSQL_DATA_TRUNCATED) {
633 : ret = 0;
634 : }
635 : # endif
636 :
637 : if (ret) {
638 : if (ret != MYSQL_NO_DATA) {
639 : pdo_mysql_error_stmt(stmt);
640 : }
641 : PDO_DBG_RETURN(0);
642 : }
643 :
644 : PDO_DBG_RETURN(1);
645 : }
646 : # endif /* HAVE_MYSQL_STMT_PREPARE */
647 : #endif /* PDO_USE_MYSQLND */
648 :
649 808 : if (!S->result) {
650 0 : strcpy(stmt->error_code, "HY000");
651 0 : PDO_DBG_RETURN(0);
652 : }
653 : #if PDO_USE_MYSQLND
654 808 : if (!S->stmt && S->current_data) {
655 446 : free(S->current_data);
656 : }
657 : #endif /* PDO_USE_MYSQLND */
658 :
659 808 : if ((S->current_data = mysql_fetch_row(S->result)) == NULL) {
660 175 : if (mysql_errno(S->H->server)) {
661 2 : pdo_mysql_error_stmt(stmt);
662 : }
663 175 : PDO_DBG_RETURN(0);
664 : }
665 :
666 633 : S->current_lengths = mysql_fetch_lengths(S->result);
667 633 : PDO_DBG_RETURN(1);
668 : }
669 : /* }}} */
670 :
671 : static int pdo_mysql_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC) /* {{{ */
672 1262 : {
673 1262 : pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
674 1262 : struct pdo_column_data *cols = stmt->columns;
675 : int i;
676 :
677 1262 : PDO_DBG_ENTER("pdo_mysql_stmt_describe");
678 1262 : PDO_DBG_INF_FMT("stmt=%p", S->stmt);
679 1262 : if (!S->result) {
680 0 : PDO_DBG_RETURN(0);
681 : }
682 :
683 1262 : if (colno >= stmt->column_count) {
684 : /* error invalid column */
685 0 : PDO_DBG_RETURN(0);
686 : }
687 :
688 : /* fetch all on demand, this seems easiest
689 : ** if we've been here before bail out
690 : */
691 1262 : if (cols[0].name) {
692 591 : PDO_DBG_RETURN(1);
693 : }
694 1933 : for (i = 0; i < stmt->column_count; i++) {
695 : int namelen;
696 :
697 1262 : if (S->H->fetch_table_names) {
698 1 : namelen = spprintf(&cols[i].name, 0, "%s.%s", S->fields[i].table, S->fields[i].name);
699 1 : cols[i].namelen = namelen;
700 : } else {
701 1261 : namelen = strlen(S->fields[i].name);
702 1261 : cols[i].namelen = namelen;
703 1261 : cols[i].name = estrndup(S->fields[i].name, namelen);
704 : }
705 :
706 1262 : cols[i].precision = S->fields[i].decimals;
707 1262 : cols[i].maxlen = S->fields[i].length;
708 :
709 : #ifdef PDO_USE_MYSQLND
710 1262 : if (S->stmt) {
711 592 : cols[i].param_type = PDO_PARAM_ZVAL;
712 : } else
713 : #endif
714 : {
715 670 : cols[i].param_type = PDO_PARAM_STR;
716 : }
717 : }
718 671 : PDO_DBG_RETURN(1);
719 : }
720 : /* }}} */
721 :
722 : static int pdo_mysql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees TSRMLS_DC) /* {{{ */
723 2220 : {
724 2220 : pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
725 :
726 2220 : PDO_DBG_ENTER("pdo_mysql_stmt_get_col");
727 2220 : PDO_DBG_INF_FMT("stmt=%p", S->stmt);
728 2220 : if (!S->result) {
729 0 : PDO_DBG_RETURN(0);
730 : }
731 :
732 : /* With mysqlnd data is stored inside mysqlnd, not S->current_data */
733 : #if HAVE_MYSQL_STMT_PREPARE || PDO_USE_MYSQLND
734 2220 : if (!S->stmt) {
735 : #endif
736 1331 : if (S->current_data == NULL || !S->result) {
737 0 : PDO_DBG_RETURN(0);
738 : }
739 : #if HAVE_MYSQL_STMT_PREPARE || PDO_USE_MYSQLND
740 : }
741 : #endif
742 2220 : if (colno >= stmt->column_count) {
743 : /* error invalid column */
744 0 : PDO_DBG_RETURN(0);
745 : }
746 : #if PDO_USE_MYSQLND
747 2220 : if (S->stmt) {
748 889 : Z_ADDREF_P(S->stmt->result_bind[colno].zv);
749 889 : *ptr = (char*)&S->stmt->result_bind[colno].zv;
750 889 : *len = sizeof(zval);
751 889 : PDO_DBG_RETURN(1);
752 : }
753 : #elif HAVE_MYSQL_STMT_PREPARE
754 : if (S->stmt) {
755 : if (S->out_null[colno]) {
756 : *ptr = NULL;
757 : *len = 0;
758 : PDO_DBG_RETURN(1);
759 : }
760 : *ptr = S->bound_result[colno].buffer;
761 : if (S->out_length[colno] > S->bound_result[colno].buffer_length) {
762 : /* mysql lied about the column width */
763 : strcpy(stmt->error_code, "01004"); /* truncated */
764 : S->out_length[colno] = S->bound_result[colno].buffer_length;
765 : *len = S->out_length[colno];
766 : PDO_DBG_RETURN(0);
767 : }
768 : *len = S->out_length[colno];
769 : PDO_DBG_RETURN(1);
770 : }
771 : #endif /* PDO_USE_MYSQLND else HAVE_MYSQL_STMT_PREPARE */
772 1331 : *ptr = S->current_data[colno];
773 1331 : *len = S->current_lengths[colno];
774 1331 : PDO_DBG_RETURN(1);
775 : } /* }}} */
776 :
777 : static char *type_to_name_native(int type) /* }}} */
778 1 : {
779 : #define PDO_MYSQL_NATIVE_TYPE_NAME(x) case FIELD_TYPE_##x: return #x;
780 :
781 1 : switch (type) {
782 0 : PDO_MYSQL_NATIVE_TYPE_NAME(STRING)
783 0 : PDO_MYSQL_NATIVE_TYPE_NAME(VAR_STRING)
784 : #ifdef MYSQL_HAS_TINY
785 : PDO_MYSQL_NATIVE_TYPE_NAME(TINY)
786 : #endif
787 0 : PDO_MYSQL_NATIVE_TYPE_NAME(SHORT)
788 1 : PDO_MYSQL_NATIVE_TYPE_NAME(LONG)
789 0 : PDO_MYSQL_NATIVE_TYPE_NAME(LONGLONG)
790 0 : PDO_MYSQL_NATIVE_TYPE_NAME(INT24)
791 0 : PDO_MYSQL_NATIVE_TYPE_NAME(FLOAT)
792 0 : PDO_MYSQL_NATIVE_TYPE_NAME(DOUBLE)
793 0 : PDO_MYSQL_NATIVE_TYPE_NAME(DECIMAL)
794 : #ifdef FIELD_TYPE_NEWDECIMAL
795 0 : PDO_MYSQL_NATIVE_TYPE_NAME(NEWDECIMAL)
796 : #endif
797 : #ifdef FIELD_TYPE_GEOMETRY
798 0 : PDO_MYSQL_NATIVE_TYPE_NAME(GEOMETRY)
799 : #endif
800 0 : PDO_MYSQL_NATIVE_TYPE_NAME(TIMESTAMP)
801 : #ifdef MYSQL_HAS_YEAR
802 : PDO_MYSQL_NATIVE_TYPE_NAME(YEAR)
803 : #endif
804 0 : PDO_MYSQL_NATIVE_TYPE_NAME(SET)
805 0 : PDO_MYSQL_NATIVE_TYPE_NAME(ENUM)
806 0 : PDO_MYSQL_NATIVE_TYPE_NAME(DATE)
807 : #ifdef FIELD_TYPE_NEWDATE
808 0 : PDO_MYSQL_NATIVE_TYPE_NAME(NEWDATE)
809 : #endif
810 0 : PDO_MYSQL_NATIVE_TYPE_NAME(TIME)
811 0 : PDO_MYSQL_NATIVE_TYPE_NAME(DATETIME)
812 0 : PDO_MYSQL_NATIVE_TYPE_NAME(TINY_BLOB)
813 0 : PDO_MYSQL_NATIVE_TYPE_NAME(MEDIUM_BLOB)
814 0 : PDO_MYSQL_NATIVE_TYPE_NAME(LONG_BLOB)
815 0 : PDO_MYSQL_NATIVE_TYPE_NAME(BLOB)
816 0 : PDO_MYSQL_NATIVE_TYPE_NAME(NULL)
817 : default:
818 0 : return NULL;
819 : }
820 : #undef PDO_MYSQL_NATIVE_TYPE_NAME
821 : } /* }}} */
822 :
823 : static int pdo_mysql_stmt_col_meta(pdo_stmt_t *stmt, long colno, zval *return_value TSRMLS_DC) /* {{{ */
824 2 : {
825 2 : pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
826 : const MYSQL_FIELD *F;
827 : zval *flags;
828 : char *str;
829 :
830 2 : PDO_DBG_ENTER("pdo_mysql_stmt_col_meta");
831 2 : PDO_DBG_INF_FMT("stmt=%p", S->stmt);
832 2 : if (!S->result) {
833 1 : PDO_DBG_RETURN(FAILURE);
834 : }
835 1 : if (colno >= stmt->column_count) {
836 : /* error invalid column */
837 0 : PDO_DBG_RETURN(FAILURE);
838 : }
839 :
840 1 : array_init(return_value);
841 1 : MAKE_STD_ZVAL(flags);
842 1 : array_init(flags);
843 :
844 1 : F = S->fields + colno;
845 :
846 1 : if (F->def) {
847 0 : add_assoc_string(return_value, "mysql:def", F->def, 1);
848 : }
849 1 : if (IS_NOT_NULL(F->flags)) {
850 1 : add_next_index_string(flags, "not_null", 1);
851 : }
852 1 : if (IS_PRI_KEY(F->flags)) {
853 0 : add_next_index_string(flags, "primary_key", 1);
854 : }
855 1 : if (F->flags & MULTIPLE_KEY_FLAG) {
856 0 : add_next_index_string(flags, "multiple_key", 1);
857 : }
858 1 : if (F->flags & UNIQUE_KEY_FLAG) {
859 0 : add_next_index_string(flags, "unique_key", 1);
860 : }
861 1 : if (IS_BLOB(F->flags)) {
862 0 : add_next_index_string(flags, "blob", 1);
863 : }
864 1 : str = type_to_name_native(F->type);
865 1 : if (str) {
866 1 : add_assoc_string(return_value, "native_type", str, 1);
867 : }
868 :
869 : #ifdef PDO_USE_MYSQLND
870 1 : switch (F->type) {
871 : case MYSQL_TYPE_BIT:
872 : case MYSQL_TYPE_YEAR:
873 : case MYSQL_TYPE_TINY:
874 : case MYSQL_TYPE_SHORT:
875 : case MYSQL_TYPE_INT24:
876 : case MYSQL_TYPE_LONG:
877 : #if SIZEOF_LONG==8
878 : case MYSQL_TYPE_LONGLONG:
879 : #endif
880 1 : add_assoc_long(return_value, "pdo_type", PDO_PARAM_INT);
881 1 : break;
882 : default:
883 0 : add_assoc_long(return_value, "pdo_type", PDO_PARAM_STR);
884 : break;
885 : }
886 : #endif
887 :
888 1 : add_assoc_zval(return_value, "flags", flags);
889 1 : add_assoc_string(return_value, "table",(F->table?F->table:""), 1);
890 1 : PDO_DBG_RETURN(SUCCESS);
891 : } /* }}} */
892 :
893 : static int pdo_mysql_stmt_cursor_closer(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
894 319 : {
895 319 : pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
896 :
897 319 : PDO_DBG_ENTER("pdo_mysql_stmt_cursor_closer");
898 319 : PDO_DBG_INF_FMT("stmt=%p", S->stmt);
899 319 : if (S->result) {
900 310 : mysql_free_result(S->result);
901 310 : S->result = NULL;
902 : }
903 : #if HAVE_MYSQL_STMT_PREPARE || PDO_USE_MYSQLND
904 319 : if (S->stmt) {
905 : int retval;
906 211 : retval = mysql_stmt_free_result(S->stmt);
907 211 : PDO_DBG_RETURN(retval ? 0 : 1);
908 : }
909 : #endif
910 :
911 : #if HAVE_MYSQL_NEXT_RESULT || PDO_USE_MYSQLND
912 217 : while (mysql_more_results(S->H->server)) {
913 : MYSQL_RES *res;
914 1 : if (mysql_next_result(S->H->server) != 0) {
915 0 : break;
916 : }
917 1 : res = mysql_store_result(S->H->server);
918 1 : if (res) {
919 0 : mysql_free_result(res);
920 : }
921 : }
922 : #endif
923 108 : PDO_DBG_RETURN(1);
924 : }
925 : /* }}} */
926 :
927 : struct pdo_stmt_methods mysql_stmt_methods = {
928 : pdo_mysql_stmt_dtor,
929 : pdo_mysql_stmt_execute,
930 : pdo_mysql_stmt_fetch,
931 : pdo_mysql_stmt_describe,
932 : pdo_mysql_stmt_get_col,
933 : pdo_mysql_stmt_param_hook,
934 : NULL, /* set_attr */
935 : NULL, /* get_attr */
936 : pdo_mysql_stmt_col_meta,
937 : pdo_mysql_stmt_next_rowset,
938 : pdo_mysql_stmt_cursor_closer
939 : };
940 :
941 : /*
942 : * Local variables:
943 : * tab-width: 4
944 : * c-basic-offset: 4
945 : * End:
946 : * vim600: noet sw=4 ts=4 fdm=marker
947 : * vim<600: noet sw=4 ts=4
948 : */
|