This source file includes following definitions.
- pdo_mysql_stmt_dtor
- pdo_mysql_stmt_set_row_count
- pdo_mysql_fill_stmt_from_result
- pdo_mysql_stmt_execute_prepared_libmysql
- pdo_mysql_stmt_execute_prepared_mysqlnd
- pdo_mysql_stmt_execute
- pdo_mysql_stmt_next_rowset
- pdo_mysql_stmt_param_hook
- pdo_mysql_stmt_fetch
- pdo_mysql_stmt_describe
- pdo_mysql_stmt_get_col
- type_to_name_native
- pdo_mysql_stmt_col_meta
- pdo_mysql_stmt_cursor_closer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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)
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)
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)
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
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 }
89 #endif
90
91 if (IS_OBJ_VALID(EG(objects_store).object_buckets[Z_OBJ_HANDLE(stmt->database_object_handle)])
92 && (!(GC_FLAGS(Z_OBJ(stmt->database_object_handle)) & IS_OBJ_FREE_CALLED))) {
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 }
104 }
105
106 #if PDO_USE_MYSQLND
107 if (!S->stmt && S->current_data) {
108 mnd_free(S->current_data);
109 }
110 #endif
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)
118 {
119 zend_long row_count;
120 pdo_mysql_stmt *S = stmt->driver_data;
121 row_count = (zend_long) mysql_stmt_affected_rows(S->stmt);
122 if (row_count != (zend_long)-1) {
123 stmt->row_count = row_count;
124 }
125 }
126
127
128 static int pdo_mysql_fill_stmt_from_result(pdo_stmt_t *stmt)
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
138
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 = (zend_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
154 stmt->row_count = (zend_long) row_count;
155 }
156
157 PDO_DBG_RETURN(1);
158 }
159
160
161 #ifndef PDO_USE_MYSQLND
162 static int pdo_mysql_stmt_execute_prepared_libmysql(pdo_stmt_t *stmt)
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
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
177 S->stmt = NULL;
178 }
179 PDO_DBG_RETURN(0);
180 }
181
182 if (!S->result) {
183 int i;
184
185
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(zend_ulong));
204
205
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
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
239
240
241
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
260 if (H->buffered) {
261 mysql_stmt_store_result(S->stmt);
262 }
263 }
264 }
265
266 pdo_mysql_stmt_set_row_count(stmt);
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)
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
288 mysql_free_result(S->result);
289 S->result = NULL;
290 }
291
292
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
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);
310 PDO_DBG_RETURN(1);
311 }
312
313 #endif
314
315 static int pdo_mysql_stmt_execute(pdo_stmt_t *stmt)
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
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
337 PDO_DBG_RETURN(pdo_mysql_fill_stmt_from_result(stmt));
338 }
339
340
341 static int pdo_mysql_stmt_next_rowset(pdo_stmt_t *stmt)
342 {
343 pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
344 pdo_mysql_db_handle *H = S->H;
345 #if PDO_USE_MYSQLND
346 zend_long row_count;
347 #endif
348 PDO_DBG_ENTER("pdo_mysql_stmt_next_rowset");
349 PDO_DBG_INF_FMT("stmt=%p", S->stmt);
350
351 #if PDO_USE_MYSQLND
352 if (!H->emulate_prepare) {
353 if (!mysqlnd_stmt_more_results(S->stmt)) {
354 PDO_DBG_RETURN(0);
355 }
356 if (mysqlnd_stmt_next_result(S->stmt)) {
357 PDO_DBG_RETURN(0);
358 }
359
360 if (!mysqlnd_stmt_more_results(S->stmt)) {
361
362
363
364
365
366
367 PDO_DBG_RETURN(0);
368 }
369
370
371 if (S->result) {
372 mysql_free_result(S->result);
373 S->result = NULL;
374 }
375 {
376
377 int i;
378
379 stmt->column_count = mysql_stmt_field_count(S->stmt);
380 for (i = 0; i < stmt->column_count; i++) {
381 mysqlnd_stmt_bind_one_result(S->stmt, i);
382 }
383 }
384
385 S->result = mysqlnd_stmt_result_metadata(S->stmt);
386 if (S->result) {
387 S->fields = mysql_fetch_fields(S->result);
388
389
390 if (H->buffered) {
391 if (mysql_stmt_store_result(S->stmt)) {
392 PDO_DBG_RETURN(1);
393 }
394 }
395 }
396 row_count = (zend_long) mysql_stmt_affected_rows(S->stmt);
397 if (row_count != (zend_long)-1) {
398 stmt->row_count = row_count;
399 }
400 PDO_DBG_RETURN(1);
401 }
402 #endif
403
404
405 #ifndef PDO_USE_MYSQLND
406 if (S->stmt) {
407 if (S->result) {
408 stmt->column_count = (int)mysql_num_fields(S->result);
409 }
410 mysql_stmt_free_result(S->stmt);
411 }
412 #endif
413 if (S->result) {
414 mysql_free_result(S->result);
415 S->result = NULL;
416 }
417
418 if (!mysql_more_results(H->server)) {
419
420 PDO_DBG_RETURN(0);
421 }
422 #if PDO_USE_MYSQLND
423 if (mysql_next_result(H->server) == FAIL) {
424 pdo_mysql_error_stmt(stmt);
425 PDO_DBG_RETURN(0);
426 } else {
427 PDO_DBG_RETURN(pdo_mysql_fill_stmt_from_result(stmt));
428 }
429 #else
430 if (mysql_next_result(H->server) > 0) {
431 pdo_mysql_error_stmt(stmt);
432 PDO_DBG_RETURN(0);
433 } else {
434 PDO_DBG_RETURN(pdo_mysql_fill_stmt_from_result(stmt));
435 }
436 #endif
437 }
438
439
440
441 static const char * const pdo_param_event_names[] =
442 {
443 "PDO_PARAM_EVT_ALLOC",
444 "PDO_PARAM_EVT_FREE",
445 "PDO_PARAM_EVT_EXEC_PRE",
446 "PDO_PARAM_EVT_EXEC_POST",
447 "PDO_PARAM_EVT_FETCH_PRE",
448 "PDO_PARAM_EVT_FETCH_POST",
449 "PDO_PARAM_EVT_NORMALIZE",
450 };
451
452
453 static int pdo_mysql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param, enum pdo_param_event event_type)
454 {
455 zval *parameter;
456 #ifndef PDO_USE_MYSQLND
457 PDO_MYSQL_PARAM_BIND *b;
458 #endif
459 pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
460
461 PDO_DBG_ENTER("pdo_mysql_stmt_param_hook");
462 PDO_DBG_INF_FMT("stmt=%p", S->stmt);
463 PDO_DBG_INF_FMT("event = %s", pdo_param_event_names[event_type]);
464 if (S->stmt && param->is_param) {
465 switch (event_type) {
466 case PDO_PARAM_EVT_ALLOC:
467
468 if (param->paramno < 0 || param->paramno >= S->num_params) {
469 strcpy(stmt->error_code, "HY093");
470 PDO_DBG_RETURN(0);
471 }
472 S->params_given++;
473
474 #ifndef PDO_USE_MYSQLND
475 b = &S->params[param->paramno];
476 param->driver_data = b;
477 b->is_null = &S->in_null[param->paramno];
478 b->length = &S->in_length[param->paramno];
479
480 #endif
481 PDO_DBG_RETURN(1);
482
483 case PDO_PARAM_EVT_EXEC_PRE:
484 if (S->params_given < (unsigned int) S->num_params) {
485
486 PDO_DBG_ERR("too few parameters bound");
487 strcpy(stmt->error_code, "HY093");
488 PDO_DBG_RETURN(0);
489 }
490
491 if (!Z_ISREF(param->parameter)) {
492 parameter = ¶m->parameter;
493 } else {
494 parameter = Z_REFVAL(param->parameter);
495 }
496
497 #if PDO_USE_MYSQLND
498 if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL || (Z_TYPE_P(parameter) == IS_NULL)) {
499 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_NULL);
500 PDO_DBG_RETURN(1);
501 }
502 #else
503 b = (PDO_MYSQL_PARAM_BIND*)param->driver_data;
504 *b->is_null = 0;
505 if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL || Z_TYPE_P(parameter) == IS_NULL) {
506 *b->is_null = 1;
507 b->buffer_type = MYSQL_TYPE_STRING;
508 b->buffer = NULL;
509 b->buffer_length = 0;
510 *b->length = 0;
511 PDO_DBG_RETURN(1);
512 }
513 #endif
514
515 switch (PDO_PARAM_TYPE(param->param_type)) {
516 case PDO_PARAM_STMT:
517 PDO_DBG_RETURN(0);
518 case PDO_PARAM_LOB:
519 PDO_DBG_INF("PDO_PARAM_LOB");
520 if (!Z_ISREF(param->parameter)) {
521 parameter = ¶m->parameter;
522 } else {
523 parameter = Z_REFVAL(param->parameter);
524 }
525 if (Z_TYPE_P(parameter) == IS_RESOURCE) {
526 php_stream *stm = NULL;
527 php_stream_from_zval_no_verify(stm, parameter);
528 if (stm) {
529 zend_string *mem = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0);
530 zval_ptr_dtor(parameter);
531 ZVAL_STR(parameter, mem ? mem : ZSTR_EMPTY_ALLOC());
532 } else {
533 pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource");
534 return 0;
535 }
536 }
537
538
539 default:
540 ;
541 }
542
543 #if PDO_USE_MYSQLND
544
545 PDO_DBG_INF_FMT("param->parameter->type=%d", Z_TYPE(param->parameter));
546 if (!Z_ISREF(param->parameter)) {
547 parameter = ¶m->parameter;
548 } else {
549 parameter = Z_REFVAL(param->parameter);
550 }
551 switch (Z_TYPE_P(parameter)) {
552 case IS_STRING:
553 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_VAR_STRING);
554 break;
555 case IS_LONG:
556 #if SIZEOF_ZEND_LONG==8
557 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_LONGLONG);
558 #elif SIZEOF_ZEND_LONG==4
559 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_LONG);
560 #endif
561 break;
562 case IS_DOUBLE:
563 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_DOUBLE);
564 break;
565 default:
566 PDO_DBG_RETURN(0);
567 }
568
569 PDO_DBG_RETURN(1);
570 #else
571 PDO_DBG_INF_FMT("param->parameter->type=%d", Z_TYPE(param->parameter));
572 if (!Z_ISREF(param->parameter)) {
573 parameter = ¶m->parameter;
574 } else {
575 parameter = Z_REFVAL(param->parameter);
576 }
577 switch (Z_TYPE_P(parameter)) {
578 case IS_STRING:
579 b->buffer_type = MYSQL_TYPE_STRING;
580 b->buffer = Z_STRVAL_P(parameter);
581 b->buffer_length = Z_STRLEN_P(parameter);
582 *b->length = Z_STRLEN_P(parameter);
583 PDO_DBG_RETURN(1);
584
585 case IS_LONG:
586 b->buffer_type = MYSQL_TYPE_LONG;
587 b->buffer = &Z_LVAL_P(parameter);
588 PDO_DBG_RETURN(1);
589
590 case IS_DOUBLE:
591 b->buffer_type = MYSQL_TYPE_DOUBLE;
592 b->buffer = &Z_DVAL_P(parameter);
593 PDO_DBG_RETURN(1);
594
595 default:
596 PDO_DBG_RETURN(0);
597 }
598 #endif
599 case PDO_PARAM_EVT_FREE:
600 case PDO_PARAM_EVT_EXEC_POST:
601 case PDO_PARAM_EVT_FETCH_PRE:
602 case PDO_PARAM_EVT_FETCH_POST:
603 case PDO_PARAM_EVT_NORMALIZE:
604
605 break;
606 }
607 }
608
609 PDO_DBG_RETURN(1);
610 }
611
612
613 static int pdo_mysql_stmt_fetch(pdo_stmt_t *stmt, enum pdo_fetch_orientation ori, zend_long offset)
614 {
615 pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
616 #if PDO_USE_MYSQLND
617 zend_bool fetched_anything;
618
619 PDO_DBG_ENTER("pdo_mysql_stmt_fetch");
620 PDO_DBG_INF_FMT("stmt=%p", S->stmt);
621 if (S->stmt) {
622 if (FAIL == mysqlnd_stmt_fetch(S->stmt, &fetched_anything) || fetched_anything == FALSE) {
623 PDO_DBG_RETURN(0);
624 }
625
626 PDO_DBG_RETURN(1);
627 }
628 #else
629 int ret;
630
631 if (S->stmt) {
632 ret = mysql_stmt_fetch(S->stmt);
633
634 # ifdef MYSQL_DATA_TRUNCATED
635 if (ret == MYSQL_DATA_TRUNCATED) {
636 ret = 0;
637 }
638 # endif
639
640 if (ret) {
641 if (ret != MYSQL_NO_DATA) {
642 pdo_mysql_error_stmt(stmt);
643 }
644 PDO_DBG_RETURN(0);
645 }
646
647 PDO_DBG_RETURN(1);
648 }
649 #endif
650
651 if (!S->result) {
652 strcpy(stmt->error_code, "HY000");
653 PDO_DBG_RETURN(0);
654 }
655 #if PDO_USE_MYSQLND
656 if (!S->stmt && S->current_data) {
657 mnd_free(S->current_data);
658 }
659 #endif
660
661 if ((S->current_data = mysql_fetch_row(S->result)) == NULL) {
662 #if PDO_USE_MYSQLND
663 if (S->result->unbuf && !S->result->unbuf->eof_reached && mysql_errno(S->H->server)) {
664 #else
665 if (!S->result->eof && mysql_errno(S->H->server)) {
666 #endif
667 pdo_mysql_error_stmt(stmt);
668 }
669 PDO_DBG_RETURN(0);
670 }
671
672 S->current_lengths = mysql_fetch_lengths(S->result);
673 PDO_DBG_RETURN(1);
674 }
675
676
677 static int pdo_mysql_stmt_describe(pdo_stmt_t *stmt, int colno)
678 {
679 pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
680 struct pdo_column_data *cols = stmt->columns;
681 int i;
682
683 PDO_DBG_ENTER("pdo_mysql_stmt_describe");
684 PDO_DBG_INF_FMT("stmt=%p", S->stmt);
685 if (!S->result) {
686 PDO_DBG_RETURN(0);
687 }
688
689 if (colno >= stmt->column_count) {
690
691 PDO_DBG_RETURN(0);
692 }
693
694
695
696
697 if (cols[0].name) {
698 PDO_DBG_RETURN(1);
699 }
700 for (i = 0; i < stmt->column_count; i++) {
701
702 if (S->H->fetch_table_names) {
703 cols[i].name = strpprintf(0, "%s.%s", S->fields[i].table, S->fields[i].name);
704 } else {
705 cols[i].name = zend_string_init(S->fields[i].name, S->fields[i].name_length, 0);
706 }
707
708 cols[i].precision = S->fields[i].decimals;
709 cols[i].maxlen = S->fields[i].length;
710
711 #ifdef PDO_USE_MYSQLND
712 if (S->stmt) {
713 cols[i].param_type = PDO_PARAM_ZVAL;
714 } else
715 #endif
716 {
717 cols[i].param_type = PDO_PARAM_STR;
718 }
719 }
720 PDO_DBG_RETURN(1);
721 }
722
723
724 static int pdo_mysql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, size_t *len, int *caller_frees)
725 {
726 pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
727
728 PDO_DBG_ENTER("pdo_mysql_stmt_get_col");
729 PDO_DBG_INF_FMT("stmt=%p", S->stmt);
730 if (!S->result) {
731 PDO_DBG_RETURN(0);
732 }
733
734
735 if (!S->stmt) {
736 if (S->current_data == NULL || !S->result) {
737 PDO_DBG_RETURN(0);
738 }
739 }
740
741 if (colno >= stmt->column_count) {
742
743 PDO_DBG_RETURN(0);
744 }
745 #if PDO_USE_MYSQLND
746 if (S->stmt) {
747 Z_TRY_ADDREF(S->stmt->data->result_bind[colno].zv);
748 *ptr = (char*)&S->stmt->data->result_bind[colno].zv;
749 *len = sizeof(zval);
750 PDO_DBG_RETURN(1);
751 }
752 #else
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
762 strcpy(stmt->error_code, "01004");
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
771 *ptr = S->current_data[colno];
772 *len = S->current_lengths[colno];
773 PDO_DBG_RETURN(1);
774 }
775
776 static char *type_to_name_native(int type)
777 {
778 #define PDO_MYSQL_NATIVE_TYPE_NAME(x) case FIELD_TYPE_##x: return #x;
779
780 switch (type) {
781 PDO_MYSQL_NATIVE_TYPE_NAME(STRING)
782 PDO_MYSQL_NATIVE_TYPE_NAME(VAR_STRING)
783 #ifdef FIELD_TYPE_TINY
784 PDO_MYSQL_NATIVE_TYPE_NAME(TINY)
785 #endif
786 #ifdef FIELD_TYPE_BIT
787 PDO_MYSQL_NATIVE_TYPE_NAME(BIT)
788 #endif
789 PDO_MYSQL_NATIVE_TYPE_NAME(SHORT)
790 PDO_MYSQL_NATIVE_TYPE_NAME(LONG)
791 PDO_MYSQL_NATIVE_TYPE_NAME(LONGLONG)
792 PDO_MYSQL_NATIVE_TYPE_NAME(INT24)
793 PDO_MYSQL_NATIVE_TYPE_NAME(FLOAT)
794 PDO_MYSQL_NATIVE_TYPE_NAME(DOUBLE)
795 PDO_MYSQL_NATIVE_TYPE_NAME(DECIMAL)
796 #ifdef FIELD_TYPE_NEWDECIMAL
797 PDO_MYSQL_NATIVE_TYPE_NAME(NEWDECIMAL)
798 #endif
799 #ifdef FIELD_TYPE_GEOMETRY
800 PDO_MYSQL_NATIVE_TYPE_NAME(GEOMETRY)
801 #endif
802 PDO_MYSQL_NATIVE_TYPE_NAME(TIMESTAMP)
803 #ifdef FIELD_TYPE_YEAR
804 PDO_MYSQL_NATIVE_TYPE_NAME(YEAR)
805 #endif
806 PDO_MYSQL_NATIVE_TYPE_NAME(SET)
807 PDO_MYSQL_NATIVE_TYPE_NAME(ENUM)
808 PDO_MYSQL_NATIVE_TYPE_NAME(DATE)
809 #ifdef FIELD_TYPE_NEWDATE
810 PDO_MYSQL_NATIVE_TYPE_NAME(NEWDATE)
811 #endif
812 PDO_MYSQL_NATIVE_TYPE_NAME(TIME)
813 PDO_MYSQL_NATIVE_TYPE_NAME(DATETIME)
814 PDO_MYSQL_NATIVE_TYPE_NAME(TINY_BLOB)
815 PDO_MYSQL_NATIVE_TYPE_NAME(MEDIUM_BLOB)
816 PDO_MYSQL_NATIVE_TYPE_NAME(LONG_BLOB)
817 PDO_MYSQL_NATIVE_TYPE_NAME(BLOB)
818 PDO_MYSQL_NATIVE_TYPE_NAME(NULL)
819 default:
820 return NULL;
821 }
822 #undef PDO_MYSQL_NATIVE_TYPE_NAME
823 }
824
825 static int pdo_mysql_stmt_col_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value)
826 {
827 pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
828 const MYSQL_FIELD *F;
829 zval flags;
830 char *str;
831
832 PDO_DBG_ENTER("pdo_mysql_stmt_col_meta");
833 PDO_DBG_INF_FMT("stmt=%p", S->stmt);
834 if (!S->result) {
835 PDO_DBG_RETURN(FAILURE);
836 }
837 if (colno >= stmt->column_count) {
838
839 PDO_DBG_RETURN(FAILURE);
840 }
841
842 array_init(return_value);
843 array_init(&flags);
844
845 F = S->fields + colno;
846
847 if (F->def) {
848 add_assoc_string(return_value, "mysql:def", F->def);
849 }
850 if (IS_NOT_NULL(F->flags)) {
851 add_next_index_string(&flags, "not_null");
852 }
853 if (IS_PRI_KEY(F->flags)) {
854 add_next_index_string(&flags, "primary_key");
855 }
856 if (F->flags & MULTIPLE_KEY_FLAG) {
857 add_next_index_string(&flags, "multiple_key");
858 }
859 if (F->flags & UNIQUE_KEY_FLAG) {
860 add_next_index_string(&flags, "unique_key");
861 }
862 if (IS_BLOB(F->flags)) {
863 add_next_index_string(&flags, "blob");
864 }
865 str = type_to_name_native(F->type);
866 if (str) {
867 add_assoc_string(return_value, "native_type", str);
868 }
869
870 #ifdef PDO_USE_MYSQLND
871 switch (F->type) {
872 case MYSQL_TYPE_BIT:
873 case MYSQL_TYPE_YEAR:
874 case MYSQL_TYPE_TINY:
875 case MYSQL_TYPE_SHORT:
876 case MYSQL_TYPE_INT24:
877 case MYSQL_TYPE_LONG:
878 #if SIZEOF_LONG==8
879 case MYSQL_TYPE_LONGLONG:
880 #endif
881 add_assoc_long(return_value, "pdo_type", PDO_PARAM_INT);
882 break;
883 default:
884 add_assoc_long(return_value, "pdo_type", PDO_PARAM_STR);
885 break;
886 }
887 #endif
888
889 add_assoc_zval(return_value, "flags", &flags);
890 add_assoc_string(return_value, "table", (char *) (F->table?F->table : ""));
891
892 PDO_DBG_RETURN(SUCCESS);
893 }
894
895 static int pdo_mysql_stmt_cursor_closer(pdo_stmt_t *stmt)
896 {
897 pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
898
899 PDO_DBG_ENTER("pdo_mysql_stmt_cursor_closer");
900 PDO_DBG_INF_FMT("stmt=%p", S->stmt);
901 if (S->result) {
902 mysql_free_result(S->result);
903 S->result = NULL;
904 }
905 if (S->stmt) {
906 int retval;
907 retval = mysql_stmt_free_result(S->stmt);
908 PDO_DBG_RETURN(retval ? 0 : 1);
909 }
910
911 while (mysql_more_results(S->H->server)) {
912 MYSQL_RES *res;
913 if (mysql_next_result(S->H->server) != 0) {
914 break;
915 }
916 res = mysql_store_result(S->H->server);
917 if (res) {
918 mysql_free_result(res);
919 }
920 }
921 PDO_DBG_RETURN(1);
922 }
923
924
925 struct pdo_stmt_methods mysql_stmt_methods = {
926 pdo_mysql_stmt_dtor,
927 pdo_mysql_stmt_execute,
928 pdo_mysql_stmt_fetch,
929 pdo_mysql_stmt_describe,
930 pdo_mysql_stmt_get_col,
931 pdo_mysql_stmt_param_hook,
932 NULL,
933 NULL,
934 pdo_mysql_stmt_col_meta,
935 pdo_mysql_stmt_next_rowset,
936 pdo_mysql_stmt_cursor_closer
937 };
938
939
940
941
942
943
944
945
946