mysql_statement.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: George Schlossnagle <george@omniti.com> |
  14. | Wez Furlong <wez@php.net> |
  15. | Johannes Schlueter <johannes@mysql.com> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include "php.h"
  22. #include "php_ini.h"
  23. #include "ext/standard/info.h"
  24. #include "pdo/php_pdo.h"
  25. #include "pdo/php_pdo_driver.h"
  26. #include "php_pdo_mysql.h"
  27. #include "php_pdo_mysql_int.h"
  28. #ifdef PDO_USE_MYSQLND
  29. # define pdo_mysql_stmt_execute_prepared(stmt) pdo_mysql_stmt_execute_prepared_mysqlnd(stmt)
  30. #else
  31. # define pdo_mysql_stmt_execute_prepared(stmt) pdo_mysql_stmt_execute_prepared_libmysql(stmt)
  32. #endif
  33. static void pdo_mysql_free_result(pdo_mysql_stmt *S)
  34. {
  35. if (S->result) {
  36. #ifndef PDO_USE_MYSQLND
  37. if (S->bound_result) {
  38. /* We can't use stmt->column_count here, because it gets reset before the
  39. * next_rowset handler is called. */
  40. unsigned column_count = mysql_num_fields(S->result);
  41. for (unsigned i = 0; i < column_count; i++) {
  42. efree(S->bound_result[i].buffer);
  43. }
  44. efree(S->bound_result);
  45. efree(S->out_null);
  46. efree(S->out_length);
  47. S->bound_result = NULL;
  48. }
  49. #else
  50. if (S->current_row) {
  51. unsigned column_count = mysql_num_fields(S->result);
  52. for (unsigned i = 0; i < column_count; i++) {
  53. zval_ptr_dtor_nogc(&S->current_row[i]);
  54. }
  55. efree(S->current_row);
  56. S->current_row = NULL;
  57. }
  58. #endif
  59. mysql_free_result(S->result);
  60. S->result = NULL;
  61. }
  62. }
  63. static int pdo_mysql_stmt_dtor(pdo_stmt_t *stmt) /* {{{ */
  64. {
  65. pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
  66. PDO_DBG_ENTER("pdo_mysql_stmt_dtor");
  67. PDO_DBG_INF_FMT("stmt=%p", S->stmt);
  68. pdo_mysql_free_result(S);
  69. if (S->einfo.errmsg) {
  70. pefree(S->einfo.errmsg, stmt->dbh->is_persistent);
  71. S->einfo.errmsg = NULL;
  72. }
  73. if (S->stmt) {
  74. mysql_stmt_close(S->stmt);
  75. S->stmt = NULL;
  76. }
  77. #ifndef PDO_USE_MYSQLND
  78. if (S->params) {
  79. efree(S->params);
  80. }
  81. if (S->in_null) {
  82. efree(S->in_null);
  83. }
  84. if (S->in_length) {
  85. efree(S->in_length);
  86. }
  87. #endif
  88. if (!S->done && !Z_ISUNDEF(stmt->database_object_handle)
  89. && IS_OBJ_VALID(EG(objects_store).object_buckets[Z_OBJ_HANDLE(stmt->database_object_handle)])
  90. && (!(OBJ_FLAGS(Z_OBJ(stmt->database_object_handle)) & IS_OBJ_FREE_CALLED))) {
  91. while (mysql_more_results(S->H->server)) {
  92. MYSQL_RES *res;
  93. if (mysql_next_result(S->H->server) != 0) {
  94. break;
  95. }
  96. res = mysql_store_result(S->H->server);
  97. if (res) {
  98. mysql_free_result(res);
  99. }
  100. }
  101. }
  102. efree(S);
  103. PDO_DBG_RETURN(1);
  104. }
  105. /* }}} */
  106. static void pdo_mysql_stmt_set_row_count(pdo_stmt_t *stmt) /* {{{ */
  107. {
  108. pdo_mysql_stmt *S = stmt->driver_data;
  109. zend_long row_count = (zend_long) mysql_stmt_affected_rows(S->stmt);
  110. if (row_count != (zend_long)-1) {
  111. stmt->row_count = row_count;
  112. }
  113. }
  114. /* }}} */
  115. static int pdo_mysql_fill_stmt_from_result(pdo_stmt_t *stmt) /* {{{ */
  116. {
  117. pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
  118. pdo_mysql_db_handle *H = S->H;
  119. my_ulonglong row_count;
  120. PDO_DBG_ENTER("pdo_mysql_fill_stmt_from_result");
  121. row_count = mysql_affected_rows(H->server);
  122. if (row_count == (my_ulonglong)-1) {
  123. /* we either have a query that returned a result set or an error occurred
  124. lets see if we have access to a result set */
  125. if (!H->buffered) {
  126. S->result = mysql_use_result(H->server);
  127. } else {
  128. S->result = mysql_store_result(H->server);
  129. }
  130. if (NULL == S->result) {
  131. pdo_mysql_error_stmt(stmt);
  132. PDO_DBG_RETURN(0);
  133. }
  134. stmt->row_count = (zend_long) mysql_num_rows(S->result);
  135. php_pdo_stmt_set_column_count(stmt, (int) mysql_num_fields(S->result));
  136. S->fields = mysql_fetch_fields(S->result);
  137. } else {
  138. /* this was a DML or DDL query (INSERT, UPDATE, DELETE, ... */
  139. stmt->row_count = (zend_long) row_count;
  140. }
  141. PDO_DBG_RETURN(1);
  142. }
  143. /* }}} */
  144. static bool pdo_mysql_stmt_after_execute_prepared(pdo_stmt_t *stmt) {
  145. pdo_mysql_stmt *S = stmt->driver_data;
  146. pdo_mysql_db_handle *H = S->H;
  147. #ifdef PDO_USE_MYSQLND
  148. /* For SHOW/DESCRIBE and others the column/field count is not available before execute. */
  149. php_pdo_stmt_set_column_count(stmt, mysql_stmt_field_count(S->stmt));
  150. for (int i = 0; i < stmt->column_count; i++) {
  151. mysqlnd_stmt_bind_one_result(S->stmt, i);
  152. }
  153. S->result = mysqlnd_stmt_result_metadata(S->stmt);
  154. if (S->result) {
  155. S->fields = mysql_fetch_fields(S->result);
  156. /* If buffered, pre-fetch all the data */
  157. if (H->buffered) {
  158. if (mysql_stmt_store_result(S->stmt)) {
  159. pdo_mysql_error_stmt(stmt);
  160. return false;
  161. }
  162. }
  163. }
  164. #else
  165. /* figure out the result set format, if any */
  166. S->result = mysql_stmt_result_metadata(S->stmt);
  167. if (S->result) {
  168. int calc_max_length = H->buffered && S->max_length == 1;
  169. S->fields = mysql_fetch_fields(S->result);
  170. php_pdo_stmt_set_column_count(stmt, (int)mysql_num_fields(S->result));
  171. S->bound_result = ecalloc(stmt->column_count, sizeof(MYSQL_BIND));
  172. S->out_null = ecalloc(stmt->column_count, sizeof(my_bool));
  173. S->out_length = ecalloc(stmt->column_count, sizeof(zend_ulong));
  174. /* summon memory to hold the row */
  175. for (int i = 0; i < stmt->column_count; i++) {
  176. if (calc_max_length && S->fields[i].type == FIELD_TYPE_BLOB) {
  177. my_bool on = 1;
  178. mysql_stmt_attr_set(S->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &on);
  179. calc_max_length = 0;
  180. }
  181. switch (S->fields[i].type) {
  182. case FIELD_TYPE_INT24:
  183. S->bound_result[i].buffer_length = MAX_MEDIUMINT_WIDTH + 1;
  184. break;
  185. case FIELD_TYPE_LONG:
  186. S->bound_result[i].buffer_length = MAX_INT_WIDTH + 1;
  187. break;
  188. case FIELD_TYPE_LONGLONG:
  189. S->bound_result[i].buffer_length = MAX_BIGINT_WIDTH + 1;
  190. break;
  191. case FIELD_TYPE_TINY:
  192. S->bound_result[i].buffer_length = MAX_TINYINT_WIDTH + 1;
  193. break;
  194. case FIELD_TYPE_SHORT:
  195. S->bound_result[i].buffer_length = MAX_SMALLINT_WIDTH + 1;
  196. break;
  197. default:
  198. S->bound_result[i].buffer_length =
  199. S->fields[i].max_length? S->fields[i].max_length:
  200. S->fields[i].length;
  201. /* work-around for longtext and alike */
  202. if (S->bound_result[i].buffer_length > H->max_buffer_size) {
  203. S->bound_result[i].buffer_length = H->max_buffer_size;
  204. }
  205. }
  206. /* there are cases where the length reported by mysql is too short.
  207. * eg: when describing a table that contains an enum column. Since
  208. * we have no way of knowing the true length either, we'll bump up
  209. * our buffer size to a reasonable size, just in case */
  210. if (S->fields[i].max_length == 0 && S->bound_result[i].buffer_length < 128 && MYSQL_TYPE_VAR_STRING) {
  211. S->bound_result[i].buffer_length = 128;
  212. }
  213. S->out_length[i] = 0;
  214. S->bound_result[i].buffer = emalloc(S->bound_result[i].buffer_length);
  215. S->bound_result[i].is_null = &S->out_null[i];
  216. S->bound_result[i].length = &S->out_length[i];
  217. S->bound_result[i].buffer_type = MYSQL_TYPE_STRING;
  218. }
  219. if (mysql_stmt_bind_result(S->stmt, S->bound_result)) {
  220. pdo_mysql_error_stmt(stmt);
  221. PDO_DBG_RETURN(0);
  222. }
  223. /* if buffered, pre-fetch all the data */
  224. if (H->buffered) {
  225. if (mysql_stmt_store_result(S->stmt)) {
  226. pdo_mysql_error_stmt(stmt);
  227. PDO_DBG_RETURN(0);
  228. }
  229. }
  230. }
  231. #endif
  232. pdo_mysql_stmt_set_row_count(stmt);
  233. return true;
  234. }
  235. #ifndef PDO_USE_MYSQLND
  236. static int pdo_mysql_stmt_execute_prepared_libmysql(pdo_stmt_t *stmt) /* {{{ */
  237. {
  238. pdo_mysql_stmt *S = stmt->driver_data;
  239. PDO_DBG_ENTER("pdo_mysql_stmt_execute_prepared_libmysql");
  240. /* (re)bind the parameters */
  241. if (mysql_stmt_bind_param(S->stmt, S->params) || mysql_stmt_execute(S->stmt)) {
  242. if (S->params) {
  243. memset(S->params, 0, S->num_params * sizeof(MYSQL_BIND));
  244. }
  245. pdo_mysql_error_stmt(stmt);
  246. if (mysql_stmt_errno(S->stmt) == 2057) {
  247. /* CR_NEW_STMT_METADATA makes the statement unusable */
  248. S->stmt = NULL;
  249. }
  250. PDO_DBG_RETURN(0);
  251. }
  252. PDO_DBG_RETURN(pdo_mysql_stmt_after_execute_prepared(stmt));
  253. }
  254. /* }}} */
  255. #endif
  256. #ifdef PDO_USE_MYSQLND
  257. static int pdo_mysql_stmt_execute_prepared_mysqlnd(pdo_stmt_t *stmt) /* {{{ */
  258. {
  259. pdo_mysql_stmt *S = stmt->driver_data;
  260. PDO_DBG_ENTER("pdo_mysql_stmt_execute_prepared_mysqlnd");
  261. if (mysql_stmt_execute(S->stmt)) {
  262. pdo_mysql_error_stmt(stmt);
  263. PDO_DBG_RETURN(0);
  264. }
  265. PDO_DBG_RETURN(pdo_mysql_stmt_after_execute_prepared(stmt));
  266. }
  267. /* }}} */
  268. #endif
  269. static int pdo_mysql_stmt_execute(pdo_stmt_t *stmt) /* {{{ */
  270. {
  271. pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
  272. pdo_mysql_db_handle *H = S->H;
  273. PDO_DBG_ENTER("pdo_mysql_stmt_execute");
  274. PDO_DBG_INF_FMT("stmt=%p", S->stmt);
  275. /* ensure that we free any previous unfetched results */
  276. pdo_mysql_free_result(S);
  277. S->done = 0;
  278. if (S->stmt) {
  279. uint32_t num_bound_params =
  280. stmt->bound_params ? zend_hash_num_elements(stmt->bound_params) : 0;
  281. if (num_bound_params < (uint32_t) S->num_params) {
  282. /* too few parameter bound */
  283. PDO_DBG_ERR("too few parameters bound");
  284. strcpy(stmt->error_code, "HY093");
  285. PDO_DBG_RETURN(0);
  286. }
  287. PDO_DBG_RETURN(pdo_mysql_stmt_execute_prepared(stmt));
  288. }
  289. if (mysql_real_query(H->server, ZSTR_VAL(stmt->active_query_string), ZSTR_LEN(stmt->active_query_string)) != 0) {
  290. pdo_mysql_error_stmt(stmt);
  291. PDO_DBG_RETURN(0);
  292. }
  293. PDO_DBG_RETURN(pdo_mysql_fill_stmt_from_result(stmt));
  294. }
  295. /* }}} */
  296. static int pdo_mysql_stmt_next_rowset(pdo_stmt_t *stmt) /* {{{ */
  297. {
  298. pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
  299. pdo_mysql_db_handle *H = S->H;
  300. PDO_DBG_ENTER("pdo_mysql_stmt_next_rowset");
  301. PDO_DBG_INF_FMT("stmt=%p", S->stmt);
  302. /* ensure that we free any previous unfetched results */
  303. pdo_mysql_free_result(S);
  304. if (S->stmt) {
  305. mysql_stmt_free_result(S->stmt);
  306. if (mysql_stmt_next_result(S->stmt)) {
  307. pdo_mysql_error_stmt(stmt);
  308. S->done = 1;
  309. PDO_DBG_RETURN(0);
  310. }
  311. PDO_DBG_RETURN(pdo_mysql_stmt_after_execute_prepared(stmt));
  312. } else {
  313. if (mysql_next_result(H->server)) {
  314. pdo_mysql_error_stmt(stmt);
  315. S->done = 1;
  316. PDO_DBG_RETURN(0);
  317. }
  318. PDO_DBG_RETURN(pdo_mysql_fill_stmt_from_result(stmt));
  319. }
  320. }
  321. /* }}} */
  322. static const char * const pdo_param_event_names[] =
  323. {
  324. "PDO_PARAM_EVT_ALLOC",
  325. "PDO_PARAM_EVT_FREE",
  326. "PDO_PARAM_EVT_EXEC_PRE",
  327. "PDO_PARAM_EVT_EXEC_POST",
  328. "PDO_PARAM_EVT_FETCH_PRE",
  329. "PDO_PARAM_EVT_FETCH_POST",
  330. "PDO_PARAM_EVT_NORMALIZE",
  331. };
  332. #ifndef PDO_USE_MYSQLND
  333. static unsigned char libmysql_false_buffer = 0;
  334. static unsigned char libmysql_true_buffer = 1;
  335. #endif
  336. static int pdo_mysql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param, enum pdo_param_event event_type) /* {{{ */
  337. {
  338. zval *parameter;
  339. #ifndef PDO_USE_MYSQLND
  340. PDO_MYSQL_PARAM_BIND *b;
  341. #endif
  342. pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
  343. PDO_DBG_ENTER("pdo_mysql_stmt_param_hook");
  344. PDO_DBG_INF_FMT("stmt=%p", S->stmt);
  345. PDO_DBG_INF_FMT("event = %s", pdo_param_event_names[event_type]);
  346. if (S->stmt && param->is_param) {
  347. switch (event_type) {
  348. case PDO_PARAM_EVT_ALLOC:
  349. /* sanity check parameter number range */
  350. if (param->paramno < 0 || param->paramno >= S->num_params) {
  351. strcpy(stmt->error_code, "HY093");
  352. PDO_DBG_RETURN(0);
  353. }
  354. #ifndef PDO_USE_MYSQLND
  355. b = &S->params[param->paramno];
  356. param->driver_data = b;
  357. b->is_null = &S->in_null[param->paramno];
  358. b->length = &S->in_length[param->paramno];
  359. /* recall how many parameters have been provided */
  360. #endif
  361. PDO_DBG_RETURN(1);
  362. case PDO_PARAM_EVT_EXEC_PRE:
  363. if (!Z_ISREF(param->parameter)) {
  364. parameter = &param->parameter;
  365. } else {
  366. parameter = Z_REFVAL(param->parameter);
  367. }
  368. #ifdef PDO_USE_MYSQLND
  369. if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL || (Z_TYPE_P(parameter) == IS_NULL)) {
  370. mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_NULL);
  371. PDO_DBG_RETURN(1);
  372. }
  373. #else
  374. b = (PDO_MYSQL_PARAM_BIND*)param->driver_data;
  375. *b->is_null = 0;
  376. if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL || Z_TYPE_P(parameter) == IS_NULL) {
  377. *b->is_null = 1;
  378. b->buffer_type = MYSQL_TYPE_STRING;
  379. b->buffer = NULL;
  380. b->buffer_length = 0;
  381. *b->length = 0;
  382. PDO_DBG_RETURN(1);
  383. }
  384. #endif /* PDO_USE_MYSQLND */
  385. switch (PDO_PARAM_TYPE(param->param_type)) {
  386. case PDO_PARAM_STMT:
  387. PDO_DBG_RETURN(0);
  388. case PDO_PARAM_LOB:
  389. PDO_DBG_INF("PDO_PARAM_LOB");
  390. if (!Z_ISREF(param->parameter)) {
  391. parameter = &param->parameter;
  392. } else {
  393. parameter = Z_REFVAL(param->parameter);
  394. }
  395. if (Z_TYPE_P(parameter) == IS_RESOURCE) {
  396. php_stream *stm = NULL;
  397. php_stream_from_zval_no_verify(stm, parameter);
  398. if (stm) {
  399. zend_string *mem = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0);
  400. zval_ptr_dtor(parameter);
  401. ZVAL_STR(parameter, mem ? mem : ZSTR_EMPTY_ALLOC());
  402. } else {
  403. pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource");
  404. return 0;
  405. }
  406. }
  407. /* fall through */
  408. default:
  409. ;
  410. }
  411. #ifdef PDO_USE_MYSQLND
  412. /* Is it really correct to check the zval's type? - But well, that's what the old code below does, too */
  413. PDO_DBG_INF_FMT("param->parameter->type=%d", Z_TYPE(param->parameter));
  414. if (!Z_ISREF(param->parameter)) {
  415. parameter = &param->parameter;
  416. } else {
  417. parameter = Z_REFVAL(param->parameter);
  418. }
  419. switch (Z_TYPE_P(parameter)) {
  420. case IS_STRING:
  421. mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_VAR_STRING);
  422. break;
  423. case IS_LONG:
  424. #if SIZEOF_ZEND_LONG==8
  425. mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_LONGLONG);
  426. #elif SIZEOF_ZEND_LONG==4
  427. mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_LONG);
  428. #endif /* SIZEOF_LONG */
  429. break;
  430. case IS_TRUE:
  431. case IS_FALSE:
  432. mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_TINY);
  433. break;
  434. case IS_DOUBLE:
  435. mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_DOUBLE);
  436. break;
  437. default:
  438. PDO_DBG_RETURN(0);
  439. }
  440. PDO_DBG_RETURN(1);
  441. #else
  442. PDO_DBG_INF_FMT("param->parameter->type=%d", Z_TYPE(param->parameter));
  443. if (!Z_ISREF(param->parameter)) {
  444. parameter = &param->parameter;
  445. } else {
  446. parameter = Z_REFVAL(param->parameter);
  447. }
  448. switch (Z_TYPE_P(parameter)) {
  449. case IS_STRING:
  450. b->buffer_type = MYSQL_TYPE_STRING;
  451. b->buffer = Z_STRVAL_P(parameter);
  452. b->buffer_length = Z_STRLEN_P(parameter);
  453. *b->length = Z_STRLEN_P(parameter);
  454. PDO_DBG_RETURN(1);
  455. case IS_FALSE:
  456. b->buffer_type = MYSQL_TYPE_TINY;
  457. b->buffer = &libmysql_false_buffer;
  458. PDO_DBG_RETURN(1);
  459. case IS_TRUE:
  460. b->buffer_type = MYSQL_TYPE_TINY;
  461. b->buffer = &libmysql_true_buffer;
  462. PDO_DBG_RETURN(1);
  463. case IS_LONG:
  464. b->buffer_type = MYSQL_TYPE_LONG;
  465. b->buffer = &Z_LVAL_P(parameter);
  466. PDO_DBG_RETURN(1);
  467. case IS_DOUBLE:
  468. b->buffer_type = MYSQL_TYPE_DOUBLE;
  469. b->buffer = &Z_DVAL_P(parameter);
  470. PDO_DBG_RETURN(1);
  471. default:
  472. PDO_DBG_RETURN(0);
  473. }
  474. #endif /* PDO_USE_MYSQLND */
  475. case PDO_PARAM_EVT_FREE:
  476. case PDO_PARAM_EVT_EXEC_POST:
  477. case PDO_PARAM_EVT_FETCH_PRE:
  478. case PDO_PARAM_EVT_FETCH_POST:
  479. case PDO_PARAM_EVT_NORMALIZE:
  480. /* do nothing */
  481. break;
  482. }
  483. }
  484. PDO_DBG_RETURN(1);
  485. }
  486. /* }}} */
  487. static int pdo_mysql_stmt_fetch(pdo_stmt_t *stmt, enum pdo_fetch_orientation ori, zend_long offset) /* {{{ */
  488. {
  489. pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
  490. if (!S->result) {
  491. PDO_DBG_RETURN(0);
  492. }
  493. #ifdef PDO_USE_MYSQLND
  494. bool fetched_anything;
  495. PDO_DBG_ENTER("pdo_mysql_stmt_fetch");
  496. PDO_DBG_INF_FMT("stmt=%p", S->stmt);
  497. if (S->stmt) {
  498. if (FAIL == mysqlnd_stmt_fetch(S->stmt, &fetched_anything) || !fetched_anything) {
  499. pdo_mysql_error_stmt(stmt);
  500. PDO_DBG_RETURN(0);
  501. }
  502. PDO_DBG_RETURN(1);
  503. }
  504. zval *row_data;
  505. if (mysqlnd_fetch_row_zval(S->result, &row_data, &fetched_anything) == FAIL) {
  506. pdo_mysql_error_stmt(stmt);
  507. PDO_DBG_RETURN(0);
  508. }
  509. if (!fetched_anything) {
  510. PDO_DBG_RETURN(0);
  511. }
  512. if (!S->current_row) {
  513. S->current_row = ecalloc(sizeof(zval), stmt->column_count);
  514. }
  515. for (unsigned i = 0; i < stmt->column_count; i++) {
  516. zval_ptr_dtor_nogc(&S->current_row[i]);
  517. ZVAL_COPY_VALUE(&S->current_row[i], &row_data[i]);
  518. }
  519. PDO_DBG_RETURN(1);
  520. #else
  521. int ret;
  522. if (S->stmt) {
  523. ret = mysql_stmt_fetch(S->stmt);
  524. # ifdef MYSQL_DATA_TRUNCATED
  525. if (ret == MYSQL_DATA_TRUNCATED) {
  526. ret = 0;
  527. }
  528. # endif
  529. if (ret) {
  530. if (ret != MYSQL_NO_DATA) {
  531. pdo_mysql_error_stmt(stmt);
  532. }
  533. PDO_DBG_RETURN(0);
  534. }
  535. PDO_DBG_RETURN(1);
  536. }
  537. if ((S->current_data = mysql_fetch_row(S->result)) == NULL) {
  538. if (!S->H->buffered && mysql_errno(S->H->server)) {
  539. pdo_mysql_error_stmt(stmt);
  540. }
  541. PDO_DBG_RETURN(0);
  542. }
  543. S->current_lengths = mysql_fetch_lengths(S->result);
  544. PDO_DBG_RETURN(1);
  545. #endif /* PDO_USE_MYSQLND */
  546. }
  547. /* }}} */
  548. static int pdo_mysql_stmt_describe(pdo_stmt_t *stmt, int colno) /* {{{ */
  549. {
  550. pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
  551. struct pdo_column_data *cols = stmt->columns;
  552. int i;
  553. PDO_DBG_ENTER("pdo_mysql_stmt_describe");
  554. PDO_DBG_INF_FMT("stmt=%p", S->stmt);
  555. if (!S->result) {
  556. PDO_DBG_RETURN(0);
  557. }
  558. if (colno >= stmt->column_count) {
  559. /* error invalid column */
  560. PDO_DBG_RETURN(0);
  561. }
  562. /* fetch all on demand, this seems easiest
  563. ** if we've been here before bail out
  564. */
  565. if (cols[0].name) {
  566. PDO_DBG_RETURN(1);
  567. }
  568. for (i = 0; i < stmt->column_count; i++) {
  569. if (S->H->fetch_table_names) {
  570. cols[i].name = strpprintf(0, "%s.%s", S->fields[i].table, S->fields[i].name);
  571. } else {
  572. #ifdef PDO_USE_MYSQLND
  573. cols[i].name = zend_string_copy(S->fields[i].sname);
  574. #else
  575. cols[i].name = zend_string_init(S->fields[i].name, S->fields[i].name_length, 0);
  576. #endif
  577. }
  578. cols[i].precision = S->fields[i].decimals;
  579. cols[i].maxlen = S->fields[i].length;
  580. }
  581. PDO_DBG_RETURN(1);
  582. }
  583. /* }}} */
  584. static int pdo_mysql_stmt_get_col(
  585. pdo_stmt_t *stmt, int colno, zval *result, enum pdo_param_type *type) /* {{{ */
  586. {
  587. pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
  588. PDO_DBG_ENTER("pdo_mysql_stmt_get_col");
  589. PDO_DBG_INF_FMT("stmt=%p", S->stmt);
  590. if (!S->result) {
  591. PDO_DBG_RETURN(0);
  592. }
  593. if (colno >= stmt->column_count) {
  594. /* error invalid column */
  595. PDO_DBG_RETURN(0);
  596. }
  597. #ifdef PDO_USE_MYSQLND
  598. if (S->stmt) {
  599. ZVAL_COPY(result, &S->stmt->data->result_bind[colno].zv);
  600. } else {
  601. ZVAL_COPY(result, &S->current_row[colno]);
  602. }
  603. PDO_DBG_RETURN(1);
  604. #else
  605. if (S->stmt) {
  606. if (S->out_null[colno]) {
  607. PDO_DBG_RETURN(1);
  608. }
  609. size_t length = S->out_length[colno];
  610. if (length > S->bound_result[colno].buffer_length) {
  611. /* mysql lied about the column width */
  612. strcpy(stmt->error_code, "01004"); /* truncated */
  613. length = S->out_length[colno] = S->bound_result[colno].buffer_length;
  614. }
  615. ZVAL_STRINGL_FAST(result, S->bound_result[colno].buffer, length);
  616. PDO_DBG_RETURN(1);
  617. }
  618. if (S->current_data == NULL) {
  619. PDO_DBG_RETURN(0);
  620. }
  621. if (S->current_data[colno]) {
  622. ZVAL_STRINGL_FAST(result, S->current_data[colno], S->current_lengths[colno]);
  623. }
  624. PDO_DBG_RETURN(1);
  625. #endif
  626. } /* }}} */
  627. static char *type_to_name_native(int type) /* {{{ */
  628. {
  629. #define PDO_MYSQL_NATIVE_TYPE_NAME(x) case FIELD_TYPE_##x: return #x;
  630. switch (type) {
  631. PDO_MYSQL_NATIVE_TYPE_NAME(STRING)
  632. PDO_MYSQL_NATIVE_TYPE_NAME(VAR_STRING)
  633. #ifdef FIELD_TYPE_TINY
  634. PDO_MYSQL_NATIVE_TYPE_NAME(TINY)
  635. #endif
  636. #ifdef FIELD_TYPE_BIT
  637. PDO_MYSQL_NATIVE_TYPE_NAME(BIT)
  638. #endif
  639. PDO_MYSQL_NATIVE_TYPE_NAME(SHORT)
  640. PDO_MYSQL_NATIVE_TYPE_NAME(LONG)
  641. PDO_MYSQL_NATIVE_TYPE_NAME(LONGLONG)
  642. PDO_MYSQL_NATIVE_TYPE_NAME(INT24)
  643. PDO_MYSQL_NATIVE_TYPE_NAME(FLOAT)
  644. PDO_MYSQL_NATIVE_TYPE_NAME(DOUBLE)
  645. PDO_MYSQL_NATIVE_TYPE_NAME(DECIMAL)
  646. #ifdef FIELD_TYPE_NEWDECIMAL
  647. PDO_MYSQL_NATIVE_TYPE_NAME(NEWDECIMAL)
  648. #endif
  649. #ifdef FIELD_TYPE_GEOMETRY
  650. PDO_MYSQL_NATIVE_TYPE_NAME(GEOMETRY)
  651. #endif
  652. PDO_MYSQL_NATIVE_TYPE_NAME(TIMESTAMP)
  653. #ifdef FIELD_TYPE_YEAR
  654. PDO_MYSQL_NATIVE_TYPE_NAME(YEAR)
  655. #endif
  656. PDO_MYSQL_NATIVE_TYPE_NAME(SET)
  657. PDO_MYSQL_NATIVE_TYPE_NAME(ENUM)
  658. PDO_MYSQL_NATIVE_TYPE_NAME(DATE)
  659. #ifdef FIELD_TYPE_NEWDATE
  660. PDO_MYSQL_NATIVE_TYPE_NAME(NEWDATE)
  661. #endif
  662. PDO_MYSQL_NATIVE_TYPE_NAME(TIME)
  663. PDO_MYSQL_NATIVE_TYPE_NAME(DATETIME)
  664. PDO_MYSQL_NATIVE_TYPE_NAME(TINY_BLOB)
  665. PDO_MYSQL_NATIVE_TYPE_NAME(MEDIUM_BLOB)
  666. PDO_MYSQL_NATIVE_TYPE_NAME(LONG_BLOB)
  667. PDO_MYSQL_NATIVE_TYPE_NAME(BLOB)
  668. PDO_MYSQL_NATIVE_TYPE_NAME(NULL)
  669. default:
  670. return NULL;
  671. }
  672. #undef PDO_MYSQL_NATIVE_TYPE_NAME
  673. } /* }}} */
  674. static int pdo_mysql_stmt_col_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value) /* {{{ */
  675. {
  676. pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
  677. const MYSQL_FIELD *F;
  678. zval flags;
  679. char *str;
  680. PDO_DBG_ENTER("pdo_mysql_stmt_col_meta");
  681. PDO_DBG_INF_FMT("stmt=%p", S->stmt);
  682. if (!S->result) {
  683. PDO_DBG_RETURN(FAILURE);
  684. }
  685. if (colno >= stmt->column_count) {
  686. /* error invalid column */
  687. PDO_DBG_RETURN(FAILURE);
  688. }
  689. array_init(return_value);
  690. array_init(&flags);
  691. F = S->fields + colno;
  692. if (F->def) {
  693. add_assoc_string(return_value, "mysql:def", F->def);
  694. }
  695. if (IS_NOT_NULL(F->flags)) {
  696. add_next_index_string(&flags, "not_null");
  697. }
  698. if (IS_PRI_KEY(F->flags)) {
  699. add_next_index_string(&flags, "primary_key");
  700. }
  701. if (F->flags & MULTIPLE_KEY_FLAG) {
  702. add_next_index_string(&flags, "multiple_key");
  703. }
  704. if (F->flags & UNIQUE_KEY_FLAG) {
  705. add_next_index_string(&flags, "unique_key");
  706. }
  707. if (IS_BLOB(F->flags)) {
  708. add_next_index_string(&flags, "blob");
  709. }
  710. str = type_to_name_native(F->type);
  711. if (str) {
  712. add_assoc_string(return_value, "native_type", str);
  713. }
  714. enum pdo_param_type param_type;
  715. switch (F->type) {
  716. case MYSQL_TYPE_BIT:
  717. case MYSQL_TYPE_YEAR:
  718. case MYSQL_TYPE_TINY:
  719. case MYSQL_TYPE_SHORT:
  720. case MYSQL_TYPE_INT24:
  721. case MYSQL_TYPE_LONG:
  722. #if SIZEOF_ZEND_LONG==8
  723. case MYSQL_TYPE_LONGLONG:
  724. #endif
  725. param_type = PDO_PARAM_INT;
  726. break;
  727. default:
  728. param_type = PDO_PARAM_STR;
  729. break;
  730. }
  731. add_assoc_long(return_value, "pdo_type", param_type);
  732. add_assoc_zval(return_value, "flags", &flags);
  733. add_assoc_string(return_value, "table", (char *) (F->table?F->table : ""));
  734. PDO_DBG_RETURN(SUCCESS);
  735. } /* }}} */
  736. static int pdo_mysql_stmt_cursor_closer(pdo_stmt_t *stmt) /* {{{ */
  737. {
  738. pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
  739. PDO_DBG_ENTER("pdo_mysql_stmt_cursor_closer");
  740. PDO_DBG_INF_FMT("stmt=%p", S->stmt);
  741. S->done = 1;
  742. pdo_mysql_free_result(S);
  743. if (S->stmt) {
  744. mysql_stmt_free_result(S->stmt);
  745. }
  746. while (mysql_more_results(S->H->server)) {
  747. MYSQL_RES *res;
  748. if (mysql_next_result(S->H->server) != 0) {
  749. pdo_mysql_error_stmt(stmt);
  750. PDO_DBG_RETURN(0);
  751. }
  752. res = mysql_store_result(S->H->server);
  753. if (res) {
  754. mysql_free_result(res);
  755. }
  756. }
  757. PDO_DBG_RETURN(1);
  758. }
  759. /* }}} */
  760. const struct pdo_stmt_methods mysql_stmt_methods = {
  761. pdo_mysql_stmt_dtor,
  762. pdo_mysql_stmt_execute,
  763. pdo_mysql_stmt_fetch,
  764. pdo_mysql_stmt_describe,
  765. pdo_mysql_stmt_get_col,
  766. pdo_mysql_stmt_param_hook,
  767. NULL, /* set_attr */
  768. NULL, /* get_attr */
  769. pdo_mysql_stmt_col_meta,
  770. pdo_mysql_stmt_next_rowset,
  771. pdo_mysql_stmt_cursor_closer
  772. };