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