mysqlnd_ps.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2006-2018 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. | Authors: Andrey Hristov <andrey@php.net> |
  16. | Ulf Wendel <uw@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "php.h"
  20. #include "mysqlnd.h"
  21. #include "mysqlnd_wireprotocol.h"
  22. #include "mysqlnd_connection.h"
  23. #include "mysqlnd_priv.h"
  24. #include "mysqlnd_ps.h"
  25. #include "mysqlnd_result.h"
  26. #include "mysqlnd_result_meta.h"
  27. #include "mysqlnd_statistics.h"
  28. #include "mysqlnd_debug.h"
  29. #include "mysqlnd_block_alloc.h"
  30. #include "mysqlnd_ext_plugin.h"
  31. const char * const mysqlnd_not_bound_as_blob = "Can't send long data for non-string/non-binary data types";
  32. const char * const mysqlnd_stmt_not_prepared = "Statement not prepared";
  33. /* Exported by mysqlnd_ps_codec.c */
  34. enum_func_status mysqlnd_stmt_execute_generate_request(MYSQLND_STMT * const s, zend_uchar ** request, size_t *request_len, zend_bool * free_buffer);
  35. enum_func_status mysqlnd_stmt_execute_batch_generate_request(MYSQLND_STMT * const s, zend_uchar ** request, size_t *request_len, zend_bool * free_buffer);
  36. static void mysqlnd_stmt_separate_result_bind(MYSQLND_STMT * const stmt);
  37. static void mysqlnd_stmt_separate_one_result_bind(MYSQLND_STMT * const stmt, const unsigned int param_no);
  38. /* {{{ mysqlnd_stmt::store_result */
  39. static MYSQLND_RES *
  40. MYSQLND_METHOD(mysqlnd_stmt, store_result)(MYSQLND_STMT * const s)
  41. {
  42. enum_func_status ret;
  43. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  44. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  45. MYSQLND_RES * result;
  46. DBG_ENTER("mysqlnd_stmt::store_result");
  47. if (!stmt || !conn || !stmt->result) {
  48. DBG_RETURN(NULL);
  49. }
  50. DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
  51. /* be compliant with libmysql - NULL will turn */
  52. if (!stmt->field_count) {
  53. DBG_RETURN(NULL);
  54. }
  55. if (stmt->cursor_exists) {
  56. /* Silently convert buffered to unbuffered, for now */
  57. DBG_RETURN(s->m->use_result(s));
  58. }
  59. /* Nothing to store for UPSERT/LOAD DATA*/
  60. if (GET_CONNECTION_STATE(&conn->state) != CONN_FETCHING_DATA || stmt->state != MYSQLND_STMT_WAITING_USE_OR_STORE)
  61. {
  62. SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
  63. DBG_RETURN(NULL);
  64. }
  65. stmt->default_rset_handler = s->m->store_result;
  66. SET_EMPTY_ERROR(stmt->error_info);
  67. SET_EMPTY_ERROR(conn->error_info);
  68. MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_PS_BUFFERED_SETS);
  69. result = stmt->result;
  70. result->type = MYSQLND_RES_PS_BUF;
  71. /* result->m.row_decoder = php_mysqlnd_rowp_read_binary_protocol; */
  72. result->stored_data = (MYSQLND_RES_BUFFERED *) mysqlnd_result_buffered_zval_init(result, result->field_count, TRUE);
  73. if (!result->stored_data) {
  74. SET_OOM_ERROR(conn->error_info);
  75. DBG_RETURN(NULL);
  76. }
  77. ret = result->m.store_result_fetch_data(conn, result, result->meta, &result->stored_data->row_buffers, TRUE);
  78. result->stored_data->m.fetch_row = mysqlnd_stmt_fetch_row_buffered;
  79. if (PASS == ret) {
  80. if (result->stored_data->type == MYSQLND_BUFFERED_TYPE_ZVAL) {
  81. MYSQLND_RES_BUFFERED_ZVAL * set = (MYSQLND_RES_BUFFERED_ZVAL *) result->stored_data;
  82. if (result->stored_data->row_count) {
  83. /* don't try to allocate more than possible - mnd_XXalloc expects size_t, and it can have narrower range than uint64_t */
  84. if (result->stored_data->row_count * result->meta->field_count * sizeof(zval *) > SIZE_MAX) {
  85. SET_OOM_ERROR(conn->error_info);
  86. DBG_RETURN(NULL);
  87. }
  88. /* if pecalloc is used valgrind barks gcc version 4.3.1 20080507 (prerelease) [gcc-4_3-branch revision 135036] (SUSE Linux) */
  89. set->data = mnd_emalloc((size_t)(result->stored_data->row_count * result->meta->field_count * sizeof(zval)));
  90. if (!set->data) {
  91. SET_OOM_ERROR(conn->error_info);
  92. DBG_RETURN(NULL);
  93. }
  94. memset(set->data, 0, (size_t)(result->stored_data->row_count * result->meta->field_count * sizeof(zval)));
  95. }
  96. /* Position at the first row */
  97. set->data_cursor = set->data;
  98. } else if (result->stored_data->type == MYSQLND_BUFFERED_TYPE_ZVAL) {
  99. /*TODO*/
  100. }
  101. /* libmysql API docs say it should be so for SELECT statements */
  102. UPSERT_STATUS_SET_AFFECTED_ROWS(stmt->upsert_status, stmt->result->stored_data->row_count);
  103. stmt->state = MYSQLND_STMT_USE_OR_STORE_CALLED;
  104. } else {
  105. COPY_CLIENT_ERROR(conn->error_info, result->stored_data->error_info);
  106. stmt->result->m.free_result_contents(stmt->result);
  107. mysqlnd_mempool_destroy(stmt->result->memory_pool);
  108. stmt->result = NULL;
  109. stmt->state = MYSQLND_STMT_PREPARED;
  110. }
  111. DBG_RETURN(result);
  112. }
  113. /* }}} */
  114. /* {{{ mysqlnd_stmt::get_result */
  115. static MYSQLND_RES *
  116. MYSQLND_METHOD(mysqlnd_stmt, get_result)(MYSQLND_STMT * const s)
  117. {
  118. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  119. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  120. MYSQLND_RES * result;
  121. DBG_ENTER("mysqlnd_stmt::get_result");
  122. if (!stmt || !conn || !stmt->result) {
  123. DBG_RETURN(NULL);
  124. }
  125. DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
  126. /* be compliant with libmysql - NULL will turn */
  127. if (!stmt->field_count) {
  128. DBG_RETURN(NULL);
  129. }
  130. if (stmt->cursor_exists) {
  131. /* Silently convert buffered to unbuffered, for now */
  132. DBG_RETURN(s->m->use_result(s));
  133. }
  134. /* Nothing to store for UPSERT/LOAD DATA*/
  135. if (GET_CONNECTION_STATE(&conn->state) != CONN_FETCHING_DATA || stmt->state != MYSQLND_STMT_WAITING_USE_OR_STORE) {
  136. SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
  137. DBG_RETURN(NULL);
  138. }
  139. SET_EMPTY_ERROR(stmt->error_info);
  140. SET_EMPTY_ERROR(conn->error_info);
  141. MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_BUFFERED_SETS);
  142. do {
  143. result = conn->m->result_init(stmt->result->field_count);
  144. if (!result) {
  145. SET_OOM_ERROR(conn->error_info);
  146. break;
  147. }
  148. result->meta = stmt->result->meta->m->clone_metadata(result, stmt->result->meta);
  149. if (!result->meta) {
  150. SET_OOM_ERROR(conn->error_info);
  151. break;
  152. }
  153. if ((result = result->m.store_result(result, conn, MYSQLND_STORE_PS | MYSQLND_STORE_NO_COPY))) {
  154. UPSERT_STATUS_SET_AFFECTED_ROWS(stmt->upsert_status, result->stored_data->row_count);
  155. stmt->state = MYSQLND_STMT_PREPARED;
  156. result->type = MYSQLND_RES_PS_BUF;
  157. } else {
  158. COPY_CLIENT_ERROR(stmt->error_info, *conn->error_info);
  159. stmt->state = MYSQLND_STMT_PREPARED;
  160. break;
  161. }
  162. DBG_RETURN(result);
  163. } while (0);
  164. if (result) {
  165. result->m.free_result(result, TRUE);
  166. }
  167. DBG_RETURN(NULL);
  168. }
  169. /* }}} */
  170. /* {{{ mysqlnd_stmt::more_results */
  171. static zend_bool
  172. MYSQLND_METHOD(mysqlnd_stmt, more_results)(const MYSQLND_STMT * s)
  173. {
  174. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  175. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  176. DBG_ENTER("mysqlnd_stmt::more_results");
  177. /* (conn->state == CONN_NEXT_RESULT_PENDING) too */
  178. DBG_RETURN((stmt && conn && (conn->m->get_server_status(conn) & SERVER_MORE_RESULTS_EXISTS))? TRUE: FALSE);
  179. }
  180. /* }}} */
  181. /* {{{ mysqlnd_stmt::next_result */
  182. static enum_func_status
  183. MYSQLND_METHOD(mysqlnd_stmt, next_result)(MYSQLND_STMT * s)
  184. {
  185. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  186. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  187. DBG_ENTER("mysqlnd_stmt::next_result");
  188. if (!stmt || !conn || !stmt->result) {
  189. DBG_RETURN(FAIL);
  190. }
  191. DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
  192. if (GET_CONNECTION_STATE(&conn->state) != CONN_NEXT_RESULT_PENDING || !(UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status) & SERVER_MORE_RESULTS_EXISTS)) {
  193. DBG_RETURN(FAIL);
  194. }
  195. DBG_INF_FMT("server_status=%u cursor=%u", UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status), UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status) & SERVER_STATUS_CURSOR_EXISTS);
  196. /* Free space for next result */
  197. s->m->free_stmt_result(s);
  198. {
  199. enum_func_status ret = s->m->parse_execute_response(s, MYSQLND_PARSE_EXEC_RESPONSE_IMPLICIT_NEXT_RESULT);
  200. DBG_RETURN(ret);
  201. }
  202. }
  203. /* }}} */
  204. /* {{{ mysqlnd_stmt_skip_metadata */
  205. static enum_func_status
  206. mysqlnd_stmt_skip_metadata(MYSQLND_STMT * s)
  207. {
  208. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  209. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  210. /* Follows parameter metadata, we have just to skip it, as libmysql does */
  211. unsigned int i = 0;
  212. enum_func_status ret = FAIL;
  213. MYSQLND_PACKET_RES_FIELD field_packet;
  214. MYSQLND_MEMORY_POOL * pool;
  215. DBG_ENTER("mysqlnd_stmt_skip_metadata");
  216. if (!stmt || !conn) {
  217. DBG_RETURN(FAIL);
  218. }
  219. pool = mysqlnd_mempool_create(MYSQLND_G(mempool_default_size));
  220. if (!pool) {
  221. DBG_RETURN(FAIL);
  222. }
  223. DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
  224. conn->payload_decoder_factory->m.init_result_field_packet(&field_packet);
  225. field_packet.memory_pool = pool;
  226. ret = PASS;
  227. field_packet.skip_parsing = TRUE;
  228. for (;i < stmt->param_count; i++) {
  229. if (FAIL == PACKET_READ(conn, &field_packet)) {
  230. ret = FAIL;
  231. break;
  232. }
  233. }
  234. PACKET_FREE(&field_packet);
  235. mysqlnd_mempool_destroy(pool);
  236. DBG_RETURN(ret);
  237. }
  238. /* }}} */
  239. /* {{{ mysqlnd_stmt_read_prepare_response */
  240. static enum_func_status
  241. mysqlnd_stmt_read_prepare_response(MYSQLND_STMT * s)
  242. {
  243. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  244. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  245. MYSQLND_PACKET_PREPARE_RESPONSE prepare_resp;
  246. enum_func_status ret = FAIL;
  247. DBG_ENTER("mysqlnd_stmt_read_prepare_response");
  248. if (!stmt || !conn) {
  249. DBG_RETURN(FAIL);
  250. }
  251. DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
  252. conn->payload_decoder_factory->m.init_prepare_response_packet(&prepare_resp);
  253. if (FAIL == PACKET_READ(conn, &prepare_resp)) {
  254. goto done;
  255. }
  256. if (0xFF == prepare_resp.error_code) {
  257. COPY_CLIENT_ERROR(stmt->error_info, prepare_resp.error_info);
  258. COPY_CLIENT_ERROR(conn->error_info, prepare_resp.error_info);
  259. goto done;
  260. }
  261. ret = PASS;
  262. stmt->stmt_id = prepare_resp.stmt_id;
  263. UPSERT_STATUS_SET_WARNINGS(conn->upsert_status, prepare_resp.warning_count);
  264. UPSERT_STATUS_SET_AFFECTED_ROWS(stmt->upsert_status, 0); /* be like libmysql */
  265. stmt->field_count = conn->field_count = prepare_resp.field_count;
  266. stmt->param_count = prepare_resp.param_count;
  267. done:
  268. PACKET_FREE(&prepare_resp);
  269. DBG_RETURN(ret);
  270. }
  271. /* }}} */
  272. /* {{{ mysqlnd_stmt_prepare_read_eof */
  273. static enum_func_status
  274. mysqlnd_stmt_prepare_read_eof(MYSQLND_STMT * s)
  275. {
  276. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  277. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  278. MYSQLND_PACKET_EOF fields_eof;
  279. enum_func_status ret = FAIL;
  280. DBG_ENTER("mysqlnd_stmt_prepare_read_eof");
  281. if (!stmt || !conn) {
  282. DBG_RETURN(FAIL);
  283. }
  284. DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
  285. conn->payload_decoder_factory->m.init_eof_packet(&fields_eof);
  286. if (FAIL == (ret = PACKET_READ(conn, &fields_eof))) {
  287. if (stmt->result) {
  288. stmt->result->m.free_result_contents(stmt->result);
  289. mnd_efree(stmt->result);
  290. /* XXX: This will crash, because we will null also the methods.
  291. But seems it happens in extreme cases or doesn't. Should be fixed by exporting a function
  292. (from mysqlnd_driver.c?) to do the reset.
  293. This bad handling is also in mysqlnd_result.c
  294. */
  295. memset(stmt, 0, sizeof(MYSQLND_STMT_DATA));
  296. stmt->state = MYSQLND_STMT_INITTED;
  297. }
  298. } else {
  299. UPSERT_STATUS_SET_SERVER_STATUS(stmt->upsert_status, fields_eof.server_status);
  300. UPSERT_STATUS_SET_WARNINGS(stmt->upsert_status, fields_eof.warning_count);
  301. stmt->state = MYSQLND_STMT_PREPARED;
  302. }
  303. DBG_RETURN(ret);
  304. }
  305. /* }}} */
  306. /* {{{ mysqlnd_stmt::prepare */
  307. static enum_func_status
  308. MYSQLND_METHOD(mysqlnd_stmt, prepare)(MYSQLND_STMT * const s, const char * const query, const size_t query_len)
  309. {
  310. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  311. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  312. MYSQLND_STMT * s_to_prepare = s;
  313. MYSQLND_STMT_DATA * stmt_to_prepare = stmt;
  314. DBG_ENTER("mysqlnd_stmt::prepare");
  315. if (!stmt || !conn) {
  316. DBG_RETURN(FAIL);
  317. }
  318. DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
  319. DBG_INF_FMT("query=%s", query);
  320. UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(stmt->upsert_status);
  321. UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(conn->upsert_status);
  322. SET_EMPTY_ERROR(stmt->error_info);
  323. SET_EMPTY_ERROR(conn->error_info);
  324. if (stmt->state > MYSQLND_STMT_INITTED) {
  325. /* See if we have to clean the wire */
  326. if (stmt->state == MYSQLND_STMT_WAITING_USE_OR_STORE) {
  327. /* Do implicit use_result and then flush the result */
  328. stmt->default_rset_handler = s->m->use_result;
  329. stmt->default_rset_handler(s);
  330. }
  331. /* No 'else' here please :) */
  332. if (stmt->state > MYSQLND_STMT_WAITING_USE_OR_STORE && stmt->result) {
  333. stmt->result->m.skip_result(stmt->result);
  334. }
  335. /*
  336. Create a new test statement, which we will prepare, but if anything
  337. fails, we will scrap it.
  338. */
  339. s_to_prepare = conn->m->stmt_init(conn);
  340. if (!s_to_prepare) {
  341. goto fail;
  342. }
  343. stmt_to_prepare = s_to_prepare->data;
  344. }
  345. {
  346. enum_func_status ret = FAIL;
  347. const MYSQLND_CSTRING query_string = {query, query_len};
  348. ret = conn->run_command(COM_STMT_PREPARE, conn, query_string);
  349. if (FAIL == ret) {
  350. goto fail;
  351. }
  352. }
  353. if (FAIL == mysqlnd_stmt_read_prepare_response(s_to_prepare)) {
  354. goto fail;
  355. }
  356. if (stmt_to_prepare->param_count) {
  357. if (FAIL == mysqlnd_stmt_skip_metadata(s_to_prepare) ||
  358. FAIL == mysqlnd_stmt_prepare_read_eof(s_to_prepare))
  359. {
  360. goto fail;
  361. }
  362. }
  363. /*
  364. Read metadata only if there is actual result set.
  365. Beware that SHOW statements bypass the PS framework and thus they send
  366. no metadata at prepare.
  367. */
  368. if (stmt_to_prepare->field_count) {
  369. MYSQLND_RES * result = conn->m->result_init(stmt_to_prepare->field_count);
  370. if (!result) {
  371. SET_OOM_ERROR(conn->error_info);
  372. goto fail;
  373. }
  374. /* Allocate the result now as it is needed for the reading of metadata */
  375. stmt_to_prepare->result = result;
  376. result->conn = conn->m->get_reference(conn);
  377. result->type = MYSQLND_RES_PS_BUF;
  378. if (FAIL == result->m.read_result_metadata(result, conn) ||
  379. FAIL == mysqlnd_stmt_prepare_read_eof(s_to_prepare))
  380. {
  381. goto fail;
  382. }
  383. }
  384. if (stmt_to_prepare != stmt) {
  385. /* swap */
  386. size_t real_size = sizeof(MYSQLND_STMT) + mysqlnd_plugin_count() * sizeof(void *);
  387. char * tmp_swap = mnd_malloc(real_size);
  388. memcpy(tmp_swap, s, real_size);
  389. memcpy(s, s_to_prepare, real_size);
  390. memcpy(s_to_prepare, tmp_swap, real_size);
  391. mnd_free(tmp_swap);
  392. {
  393. MYSQLND_STMT_DATA * tmp_swap_data = stmt_to_prepare;
  394. stmt_to_prepare = stmt;
  395. stmt = tmp_swap_data;
  396. }
  397. s_to_prepare->m->dtor(s_to_prepare, TRUE);
  398. }
  399. stmt->state = MYSQLND_STMT_PREPARED;
  400. DBG_INF("PASS");
  401. DBG_RETURN(PASS);
  402. fail:
  403. if (stmt_to_prepare != stmt && s_to_prepare) {
  404. s_to_prepare->m->dtor(s_to_prepare, TRUE);
  405. }
  406. stmt->state = MYSQLND_STMT_INITTED;
  407. DBG_INF("FAIL");
  408. DBG_RETURN(FAIL);
  409. }
  410. /* }}} */
  411. /* {{{ mysqlnd_stmt_execute_parse_response */
  412. static enum_func_status
  413. mysqlnd_stmt_execute_parse_response(MYSQLND_STMT * const s, enum_mysqlnd_parse_exec_response_type type)
  414. {
  415. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  416. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  417. enum_func_status ret;
  418. DBG_ENTER("mysqlnd_stmt_execute_parse_response");
  419. if (!stmt || !conn) {
  420. DBG_RETURN(FAIL);
  421. }
  422. SET_CONNECTION_STATE(&conn->state, CONN_QUERY_SENT);
  423. ret = conn->m->query_read_result_set_header(conn, s);
  424. if (ret == FAIL) {
  425. COPY_CLIENT_ERROR(stmt->error_info, *conn->error_info);
  426. UPSERT_STATUS_RESET(stmt->upsert_status);
  427. UPSERT_STATUS_SET_AFFECTED_ROWS(stmt->upsert_status, UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status));
  428. if (GET_CONNECTION_STATE(&conn->state) == CONN_QUIT_SENT) {
  429. /* close the statement here, the connection has been closed */
  430. }
  431. stmt->state = MYSQLND_STMT_PREPARED;
  432. stmt->send_types_to_server = 1;
  433. } else {
  434. /*
  435. stmt->send_types_to_server has already been set to 0 in
  436. mysqlnd_stmt_execute_generate_request / mysqlnd_stmt_execute_store_params
  437. In case there is a situation in which binding was done for integer and the
  438. value is > LONG_MAX or < LONG_MIN, there is string conversion and we have
  439. to resend the types. Next execution will also need to resend the type.
  440. */
  441. SET_EMPTY_ERROR(stmt->error_info);
  442. SET_EMPTY_ERROR(conn->error_info);
  443. UPSERT_STATUS_SET_WARNINGS(stmt->upsert_status, UPSERT_STATUS_GET_WARNINGS(conn->upsert_status));
  444. UPSERT_STATUS_SET_AFFECTED_ROWS(stmt->upsert_status, UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status));
  445. UPSERT_STATUS_SET_SERVER_STATUS(stmt->upsert_status, UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status));
  446. UPSERT_STATUS_SET_LAST_INSERT_ID(stmt->upsert_status, UPSERT_STATUS_GET_LAST_INSERT_ID(conn->upsert_status));
  447. stmt->state = MYSQLND_STMT_EXECUTED;
  448. if (conn->last_query_type == QUERY_UPSERT || conn->last_query_type == QUERY_LOAD_LOCAL) {
  449. DBG_INF("PASS");
  450. DBG_RETURN(PASS);
  451. }
  452. stmt->result->type = MYSQLND_RES_PS_BUF;
  453. if (!stmt->result->conn) {
  454. /*
  455. For SHOW we don't create (bypasses PS in server)
  456. a result set at prepare and thus a connection was missing
  457. */
  458. stmt->result->conn = conn->m->get_reference(conn);
  459. }
  460. /* Update stmt->field_count as SHOW sets it to 0 at prepare */
  461. stmt->field_count = stmt->result->field_count = conn->field_count;
  462. if (stmt->result->stored_data) {
  463. stmt->result->stored_data->lengths = NULL;
  464. } else if (stmt->result->unbuf) {
  465. stmt->result->unbuf->lengths = NULL;
  466. }
  467. if (stmt->field_count) {
  468. stmt->state = MYSQLND_STMT_WAITING_USE_OR_STORE;
  469. /*
  470. We need to set this because the user might not call
  471. use_result() or store_result() and we should be able to scrap the
  472. data on the line, if he just decides to close the statement.
  473. */
  474. DBG_INF_FMT("server_status=%u cursor=%u", UPSERT_STATUS_GET_SERVER_STATUS(stmt->upsert_status),
  475. UPSERT_STATUS_GET_SERVER_STATUS(stmt->upsert_status) & SERVER_STATUS_CURSOR_EXISTS);
  476. if (UPSERT_STATUS_GET_SERVER_STATUS(stmt->upsert_status) & SERVER_STATUS_CURSOR_EXISTS) {
  477. DBG_INF("cursor exists");
  478. stmt->cursor_exists = TRUE;
  479. SET_CONNECTION_STATE(&conn->state, CONN_READY);
  480. /* Only cursor read */
  481. stmt->default_rset_handler = s->m->use_result;
  482. DBG_INF("use_result");
  483. } else if (stmt->flags & CURSOR_TYPE_READ_ONLY) {
  484. DBG_INF("asked for cursor but got none");
  485. /*
  486. We have asked for CURSOR but got no cursor, because the condition
  487. above is not fulfilled. Then...
  488. This is a single-row result set, a result set with no rows, EXPLAIN,
  489. SHOW VARIABLES, or some other command which either a) bypasses the
  490. cursors framework in the server and writes rows directly to the
  491. network or b) is more efficient if all (few) result set rows are
  492. precached on client and server's resources are freed.
  493. */
  494. /* preferred is buffered read */
  495. stmt->default_rset_handler = s->m->store_result;
  496. DBG_INF("store_result");
  497. } else {
  498. DBG_INF("no cursor");
  499. /* preferred is unbuffered read */
  500. stmt->default_rset_handler = s->m->use_result;
  501. DBG_INF("use_result");
  502. }
  503. }
  504. }
  505. #ifndef MYSQLND_DONT_SKIP_OUT_PARAMS_RESULTSET
  506. if (UPSERT_STATUS_GET_SERVER_STATUS(stmt->upsert_status) & SERVER_PS_OUT_PARAMS) {
  507. s->m->free_stmt_content(s);
  508. DBG_INF("PS OUT Variable RSet, skipping");
  509. /* OUT params result set. Skip for now to retain compatibility */
  510. ret = mysqlnd_stmt_execute_parse_response(s, MYSQLND_PARSE_EXEC_RESPONSE_IMPLICIT_OUT_VARIABLES);
  511. }
  512. #endif
  513. DBG_INF_FMT("server_status=%u cursor=%u", UPSERT_STATUS_GET_SERVER_STATUS(stmt->upsert_status), UPSERT_STATUS_GET_SERVER_STATUS(stmt->upsert_status) & SERVER_STATUS_CURSOR_EXISTS);
  514. if (ret == PASS && conn->last_query_type == QUERY_UPSERT && UPSERT_STATUS_GET_AFFECTED_ROWS(stmt->upsert_status)) {
  515. MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn->stats, STAT_ROWS_AFFECTED_PS, UPSERT_STATUS_GET_AFFECTED_ROWS(stmt->upsert_status));
  516. }
  517. DBG_INF(ret == PASS? "PASS":"FAIL");
  518. DBG_RETURN(ret);
  519. }
  520. /* }}} */
  521. /* {{{ mysqlnd_stmt::execute */
  522. static enum_func_status
  523. MYSQLND_METHOD(mysqlnd_stmt, execute)(MYSQLND_STMT * const s)
  524. {
  525. DBG_ENTER("mysqlnd_stmt::execute");
  526. if (FAIL == s->m->send_execute(s, MYSQLND_SEND_EXECUTE_IMPLICIT, NULL, NULL) ||
  527. FAIL == s->m->parse_execute_response(s, MYSQLND_PARSE_EXEC_RESPONSE_IMPLICIT))
  528. {
  529. DBG_RETURN(FAIL);
  530. }
  531. DBG_RETURN(PASS);
  532. }
  533. /* }}} */
  534. /* {{{ mysqlnd_stmt::send_execute */
  535. static enum_func_status
  536. MYSQLND_METHOD(mysqlnd_stmt, send_execute)(MYSQLND_STMT * const s, const enum_mysqlnd_send_execute_type type, zval * read_cb, zval * err_cb)
  537. {
  538. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  539. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  540. enum_func_status ret;
  541. zend_uchar *request = NULL;
  542. size_t request_len;
  543. zend_bool free_request;
  544. DBG_ENTER("mysqlnd_stmt::send_execute");
  545. if (!stmt || !conn) {
  546. DBG_RETURN(FAIL);
  547. }
  548. DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
  549. UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(stmt->upsert_status);
  550. UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(conn->upsert_status);
  551. if (stmt->result && stmt->state >= MYSQLND_STMT_PREPARED && stmt->field_count) {
  552. /*
  553. We don need to copy the data from the buffers which we will clean.
  554. Because it has already been copied. See
  555. #ifndef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
  556. */
  557. #ifdef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
  558. if (stmt->result_bind &&
  559. stmt->result_zvals_separated_once == TRUE &&
  560. stmt->state >= MYSQLND_STMT_USER_FETCHING)
  561. {
  562. /*
  563. We need to copy the data from the buffers which we will clean.
  564. The bound variables point to them only if the user has started
  565. to fetch data (MYSQLND_STMT_USER_FETCHING).
  566. We need to check 'result_zvals_separated_once' or we will leak
  567. in the following scenario
  568. prepare("select 1 from dual");
  569. execute();
  570. fetch(); <-- no binding, but that's not a problem
  571. bind_result();
  572. execute(); <-- here we will leak because we separate without need
  573. */
  574. unsigned int i;
  575. for (i = 0; i < stmt->field_count; i++) {
  576. if (stmt->result_bind[i].bound == TRUE) {
  577. zval *result = &stmt->result_bind[i].zv;
  578. ZVAL_DEREF(result);
  579. Z_TRY_ADDREF_P(result);
  580. }
  581. }
  582. }
  583. #endif
  584. s->m->flush(s);
  585. /*
  586. Executed, but the user hasn't started to fetch
  587. This will clean also the metadata, but after the EXECUTE call we will
  588. have it again.
  589. */
  590. stmt->result->m.free_result_buffers(stmt->result);
  591. stmt->state = MYSQLND_STMT_PREPARED;
  592. } else if (stmt->state < MYSQLND_STMT_PREPARED) {
  593. /* Only initted - error */
  594. SET_CLIENT_ERROR(stmt->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
  595. DBG_INF("FAIL");
  596. DBG_RETURN(FAIL);
  597. }
  598. if (stmt->param_count) {
  599. unsigned int i, not_bound = 0;
  600. if (!stmt->param_bind) {
  601. SET_CLIENT_ERROR(stmt->error_info, CR_PARAMS_NOT_BOUND, UNKNOWN_SQLSTATE, "No data supplied for parameters in prepared statement");
  602. DBG_INF("FAIL");
  603. DBG_RETURN(FAIL);
  604. }
  605. for (i = 0; i < stmt->param_count; i++) {
  606. if (Z_ISUNDEF(stmt->param_bind[i].zv)) {
  607. not_bound++;
  608. }
  609. }
  610. if (not_bound) {
  611. char * msg;
  612. mnd_sprintf(&msg, 0, "No data supplied for %u parameter%s in prepared statement",
  613. not_bound, not_bound>1 ?"s":"");
  614. SET_CLIENT_ERROR(stmt->error_info, CR_PARAMS_NOT_BOUND, UNKNOWN_SQLSTATE, msg);
  615. if (msg) {
  616. mnd_sprintf_free(msg);
  617. }
  618. DBG_INF("FAIL");
  619. DBG_RETURN(FAIL);
  620. }
  621. }
  622. ret = s->m->generate_execute_request(s, &request, &request_len, &free_request);
  623. if (ret == PASS) {
  624. const MYSQLND_CSTRING payload = {(const char*) request, request_len};
  625. ret = conn->run_command(COM_STMT_EXECUTE, conn, payload);
  626. } else {
  627. SET_CLIENT_ERROR(stmt->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Couldn't generate the request. Possibly OOM.");
  628. }
  629. if (free_request) {
  630. mnd_efree(request);
  631. }
  632. if (ret == FAIL) {
  633. COPY_CLIENT_ERROR(stmt->error_info, *conn->error_info);
  634. DBG_INF("FAIL");
  635. DBG_RETURN(FAIL);
  636. }
  637. stmt->execute_count++;
  638. DBG_RETURN(PASS);
  639. }
  640. /* }}} */
  641. /* {{{ mysqlnd_stmt_fetch_row_buffered */
  642. enum_func_status
  643. mysqlnd_stmt_fetch_row_buffered(MYSQLND_RES * result, void * param, const unsigned int flags, zend_bool * fetched_anything)
  644. {
  645. MYSQLND_STMT * s = (MYSQLND_STMT *) param;
  646. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  647. const MYSQLND_RES_METADATA * const meta = result->meta;
  648. unsigned int field_count = meta->field_count;
  649. DBG_ENTER("mysqlnd_stmt_fetch_row_buffered");
  650. *fetched_anything = FALSE;
  651. DBG_INF_FMT("stmt=%lu", stmt != NULL ? stmt->stmt_id : 0L);
  652. /* If we haven't read everything */
  653. if (result->stored_data->type == MYSQLND_BUFFERED_TYPE_ZVAL) {
  654. MYSQLND_RES_BUFFERED_ZVAL * set = (MYSQLND_RES_BUFFERED_ZVAL *) result->stored_data;
  655. if (set->data_cursor &&
  656. (set->data_cursor - set->data) < (result->stored_data->row_count * field_count))
  657. {
  658. /* The user could have skipped binding - don't crash*/
  659. if (stmt->result_bind) {
  660. unsigned int i;
  661. zval *current_row = set->data_cursor;
  662. if (Z_ISUNDEF(current_row[0])) {
  663. uint64_t row_num = (set->data_cursor - set->data) / field_count;
  664. enum_func_status rc = result->stored_data->m.row_decoder(&result->stored_data->row_buffers[row_num],
  665. current_row,
  666. meta->field_count,
  667. meta->fields,
  668. result->conn->options->int_and_float_native,
  669. result->conn->stats);
  670. if (PASS != rc) {
  671. DBG_RETURN(FAIL);
  672. }
  673. result->stored_data->initialized_rows++;
  674. if (stmt->update_max_length) {
  675. for (i = 0; i < result->field_count; i++) {
  676. /*
  677. NULL fields are 0 length, 0 is not more than 0
  678. String of zero size, definitely can't be the next max_length.
  679. Thus for NULL and zero-length we are quite efficient.
  680. */
  681. if (Z_TYPE(current_row[i]) == IS_STRING) {
  682. zend_ulong len = Z_STRLEN(current_row[i]);
  683. if (meta->fields[i].max_length < len) {
  684. meta->fields[i].max_length = len;
  685. }
  686. }
  687. }
  688. }
  689. }
  690. for (i = 0; i < result->field_count; i++) {
  691. zval *result = &stmt->result_bind[i].zv;
  692. ZVAL_DEREF(result);
  693. /* Clean what we copied last time */
  694. #ifndef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
  695. zval_ptr_dtor(result);
  696. #endif
  697. /* copy the type */
  698. if (stmt->result_bind[i].bound == TRUE) {
  699. DBG_INF_FMT("i=%u type=%u", i, Z_TYPE(current_row[i]));
  700. if (Z_TYPE(current_row[i]) != IS_NULL) {
  701. /*
  702. Copy the value.
  703. Pre-condition is that the zvals in the result_bind buffer
  704. have been ZVAL_NULL()-ed or to another simple type
  705. (int, double, bool but not string). Because of the reference
  706. counting the user can't delete the strings the variables point to.
  707. */
  708. ZVAL_COPY_VALUE(result, &current_row[i]);
  709. #ifndef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
  710. Z_TRY_ADDREF_P(result);
  711. #endif
  712. } else {
  713. ZVAL_NULL(result);
  714. }
  715. }
  716. }
  717. }
  718. set->data_cursor += field_count;
  719. *fetched_anything = TRUE;
  720. /* buffered result sets don't have a connection */
  721. MYSQLND_INC_GLOBAL_STATISTIC(STAT_ROWS_FETCHED_FROM_CLIENT_PS_BUF);
  722. DBG_INF("row fetched");
  723. } else {
  724. set->data_cursor = NULL;
  725. DBG_INF("no more data");
  726. }
  727. } else if (result->stored_data->type == MYSQLND_BUFFERED_TYPE_C) {
  728. /*TODO*/
  729. }
  730. DBG_INF("PASS");
  731. DBG_RETURN(PASS);
  732. }
  733. /* }}} */
  734. /* {{{ mysqlnd_stmt_fetch_row_unbuffered */
  735. enum_func_status
  736. mysqlnd_stmt_fetch_row_unbuffered(MYSQLND_RES * result, void * param, const unsigned int flags, zend_bool * fetched_anything)
  737. {
  738. enum_func_status ret;
  739. MYSQLND_STMT * s = (MYSQLND_STMT *) param;
  740. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  741. MYSQLND_PACKET_ROW * row_packet;
  742. MYSQLND_CONN_DATA * conn = result->conn;
  743. const MYSQLND_RES_METADATA * const meta = result->meta;
  744. void *checkpoint;
  745. DBG_ENTER("mysqlnd_stmt_fetch_row_unbuffered");
  746. *fetched_anything = FALSE;
  747. if (result->unbuf->eof_reached) {
  748. /* No more rows obviously */
  749. DBG_INF("EOF already reached");
  750. DBG_RETURN(PASS);
  751. }
  752. if (GET_CONNECTION_STATE(&conn->state) != CONN_FETCHING_DATA) {
  753. SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
  754. DBG_ERR("command out of sync");
  755. DBG_RETURN(FAIL);
  756. }
  757. if (!(row_packet = result->unbuf->row_packet)) {
  758. DBG_RETURN(FAIL);
  759. }
  760. /* Let the row packet fill our buffer and skip additional malloc + memcpy */
  761. row_packet->skip_extraction = stmt && stmt->result_bind? FALSE:TRUE;
  762. checkpoint = result->memory_pool->checkpoint;
  763. mysqlnd_mempool_save_state(result->memory_pool);
  764. /*
  765. If we skip rows (stmt == NULL || stmt->result_bind == NULL) we have to
  766. result->unbuf->m.free_last_data() before it. The function returns always true.
  767. */
  768. if (PASS == (ret = PACKET_READ(conn, row_packet)) && !row_packet->eof) {
  769. unsigned int i, field_count = result->field_count;
  770. if (!row_packet->skip_extraction) {
  771. result->unbuf->m.free_last_data(result->unbuf, conn->stats);
  772. result->unbuf->last_row_data = row_packet->fields;
  773. result->unbuf->last_row_buffer = row_packet->row_buffer;
  774. row_packet->fields = NULL;
  775. row_packet->row_buffer.ptr = NULL;
  776. if (PASS != result->unbuf->m.row_decoder(&result->unbuf->last_row_buffer,
  777. result->unbuf->last_row_data,
  778. row_packet->field_count,
  779. row_packet->fields_metadata,
  780. conn->options->int_and_float_native,
  781. conn->stats))
  782. {
  783. mysqlnd_mempool_restore_state(result->memory_pool);
  784. result->memory_pool->checkpoint = checkpoint;
  785. DBG_RETURN(FAIL);
  786. }
  787. for (i = 0; i < field_count; i++) {
  788. if (stmt->result_bind[i].bound == TRUE) {
  789. zval *data = &result->unbuf->last_row_data[i];
  790. zval *result = &stmt->result_bind[i].zv;
  791. ZVAL_DEREF(result);
  792. /*
  793. stmt->result_bind[i].zv has been already destructed
  794. in result->unbuf->m.free_last_data()
  795. */
  796. #ifndef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
  797. zval_ptr_dtor(result);
  798. #endif
  799. if (!Z_ISNULL_P(data)) {
  800. if ((Z_TYPE_P(data) == IS_STRING) && (meta->fields[i].max_length < (zend_ulong) Z_STRLEN_P(data))){
  801. meta->fields[i].max_length = Z_STRLEN_P(data);
  802. }
  803. ZVAL_COPY_VALUE(result, data);
  804. /* copied data, thus also the ownership. Thus null data */
  805. ZVAL_NULL(data);
  806. } else {
  807. ZVAL_NULL(result);
  808. }
  809. }
  810. }
  811. MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_ROWS_FETCHED_FROM_CLIENT_PS_UNBUF);
  812. } else {
  813. DBG_INF("skipping extraction");
  814. /*
  815. Data has been allocated and usually result->unbuf->m.free_last_data()
  816. frees it but we can't call this function as it will cause problems with
  817. the bound variables. Thus we need to do part of what it does or Zend will
  818. report leaks.
  819. */
  820. row_packet->result_set_memory_pool->free_chunk(
  821. row_packet->result_set_memory_pool, row_packet->row_buffer.ptr);
  822. row_packet->row_buffer.ptr = NULL;
  823. }
  824. result->unbuf->row_count++;
  825. *fetched_anything = TRUE;
  826. } else if (ret == FAIL) {
  827. if (row_packet->error_info.error_no) {
  828. COPY_CLIENT_ERROR(conn->error_info, row_packet->error_info);
  829. COPY_CLIENT_ERROR(stmt->error_info, row_packet->error_info);
  830. }
  831. SET_CONNECTION_STATE(&conn->state, CONN_READY);
  832. result->unbuf->eof_reached = TRUE; /* so next time we won't get an error */
  833. } else if (row_packet->eof) {
  834. DBG_INF("EOF");
  835. /* Mark the connection as usable again */
  836. result->unbuf->eof_reached = TRUE;
  837. UPSERT_STATUS_RESET(conn->upsert_status);
  838. UPSERT_STATUS_SET_WARNINGS(conn->upsert_status, row_packet->warning_count);
  839. UPSERT_STATUS_SET_SERVER_STATUS(conn->upsert_status, row_packet->server_status);
  840. /*
  841. result->row_packet will be cleaned when
  842. destroying the result object
  843. */
  844. if (UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status) & SERVER_MORE_RESULTS_EXISTS) {
  845. SET_CONNECTION_STATE(&conn->state, CONN_NEXT_RESULT_PENDING);
  846. } else {
  847. SET_CONNECTION_STATE(&conn->state, CONN_READY);
  848. }
  849. }
  850. mysqlnd_mempool_restore_state(result->memory_pool);
  851. result->memory_pool->checkpoint = checkpoint;
  852. DBG_INF_FMT("ret=%s fetched_anything=%u", ret == PASS? "PASS":"FAIL", *fetched_anything);
  853. DBG_RETURN(ret);
  854. }
  855. /* }}} */
  856. /* {{{ mysqlnd_stmt::use_result */
  857. static MYSQLND_RES *
  858. MYSQLND_METHOD(mysqlnd_stmt, use_result)(MYSQLND_STMT * s)
  859. {
  860. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  861. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  862. MYSQLND_RES * result;
  863. DBG_ENTER("mysqlnd_stmt::use_result");
  864. if (!stmt || !conn || !stmt->result) {
  865. DBG_RETURN(NULL);
  866. }
  867. DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
  868. if (!stmt->field_count ||
  869. (!stmt->cursor_exists && GET_CONNECTION_STATE(&conn->state) != CONN_FETCHING_DATA) ||
  870. (stmt->cursor_exists && GET_CONNECTION_STATE(&conn->state) != CONN_READY) ||
  871. (stmt->state != MYSQLND_STMT_WAITING_USE_OR_STORE))
  872. {
  873. SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
  874. DBG_ERR("command out of sync");
  875. DBG_RETURN(NULL);
  876. }
  877. SET_EMPTY_ERROR(stmt->error_info);
  878. MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_PS_UNBUFFERED_SETS);
  879. result = stmt->result;
  880. result->m.use_result(stmt->result, TRUE);
  881. result->unbuf->m.fetch_row = stmt->cursor_exists? mysqlnd_fetch_stmt_row_cursor:
  882. mysqlnd_stmt_fetch_row_unbuffered;
  883. stmt->state = MYSQLND_STMT_USE_OR_STORE_CALLED;
  884. DBG_INF_FMT("%p", result);
  885. DBG_RETURN(result);
  886. }
  887. /* }}} */
  888. /* {{{ mysqlnd_fetch_row_cursor */
  889. enum_func_status
  890. mysqlnd_fetch_stmt_row_cursor(MYSQLND_RES * result, void * param, const unsigned int flags, zend_bool * fetched_anything)
  891. {
  892. enum_func_status ret;
  893. MYSQLND_STMT * s = (MYSQLND_STMT *) param;
  894. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  895. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  896. zend_uchar buf[MYSQLND_STMT_ID_LENGTH /* statement id */ + 4 /* number of rows to fetch */];
  897. MYSQLND_PACKET_ROW * row_packet;
  898. DBG_ENTER("mysqlnd_fetch_stmt_row_cursor");
  899. if (!stmt || !stmt->conn || !result || !result->conn || !result->unbuf) {
  900. DBG_ERR("no statement");
  901. DBG_RETURN(FAIL);
  902. }
  903. DBG_INF_FMT("stmt=%lu flags=%u", stmt->stmt_id, flags);
  904. if (stmt->state < MYSQLND_STMT_USER_FETCHING) {
  905. /* Only initted - error */
  906. SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
  907. DBG_ERR("command out of sync");
  908. DBG_RETURN(FAIL);
  909. }
  910. if (!(row_packet = result->unbuf->row_packet)) {
  911. DBG_RETURN(FAIL);
  912. }
  913. SET_EMPTY_ERROR(stmt->error_info);
  914. SET_EMPTY_ERROR(conn->error_info);
  915. int4store(buf, stmt->stmt_id);
  916. int4store(buf + MYSQLND_STMT_ID_LENGTH, 1); /* for now fetch only one row */
  917. {
  918. const MYSQLND_CSTRING payload = {(const char*) buf, sizeof(buf)};
  919. ret = conn->run_command(COM_STMT_FETCH, conn, payload);
  920. if (ret == FAIL) {
  921. COPY_CLIENT_ERROR(stmt->error_info, *conn->error_info);
  922. DBG_RETURN(FAIL);
  923. }
  924. }
  925. row_packet->skip_extraction = stmt->result_bind? FALSE:TRUE;
  926. UPSERT_STATUS_RESET(stmt->upsert_status);
  927. if (PASS == (ret = PACKET_READ(conn, row_packet)) && !row_packet->eof) {
  928. const MYSQLND_RES_METADATA * const meta = result->meta;
  929. unsigned int i, field_count = result->field_count;
  930. if (!row_packet->skip_extraction) {
  931. result->unbuf->m.free_last_data(result->unbuf, conn->stats);
  932. result->unbuf->last_row_data = row_packet->fields;
  933. result->unbuf->last_row_buffer = row_packet->row_buffer;
  934. row_packet->fields = NULL;
  935. row_packet->row_buffer.ptr = NULL;
  936. if (PASS != result->unbuf->m.row_decoder(&result->unbuf->last_row_buffer,
  937. result->unbuf->last_row_data,
  938. row_packet->field_count,
  939. row_packet->fields_metadata,
  940. conn->options->int_and_float_native,
  941. conn->stats))
  942. {
  943. DBG_RETURN(FAIL);
  944. }
  945. /* If no result bind, do nothing. We consumed the data */
  946. for (i = 0; i < field_count; i++) {
  947. if (stmt->result_bind[i].bound == TRUE) {
  948. zval *data = &result->unbuf->last_row_data[i];
  949. zval *result = &stmt->result_bind[i].zv;
  950. ZVAL_DEREF(result);
  951. /*
  952. stmt->result_bind[i].zv has been already destructed
  953. in result->unbuf->m.free_last_data()
  954. */
  955. #ifndef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
  956. zval_ptr_dtor(result);
  957. #endif
  958. DBG_INF_FMT("i=%u bound_var=%p type=%u refc=%u", i, &stmt->result_bind[i].zv,
  959. Z_TYPE_P(data), Z_REFCOUNTED(stmt->result_bind[i].zv)?
  960. Z_REFCOUNT(stmt->result_bind[i].zv) : 0);
  961. if (!Z_ISNULL_P(data)) {
  962. if ((Z_TYPE_P(data) == IS_STRING) &&
  963. (meta->fields[i].max_length < (zend_ulong) Z_STRLEN_P(data))) {
  964. meta->fields[i].max_length = Z_STRLEN_P(data);
  965. }
  966. ZVAL_COPY_VALUE(result, data);
  967. /* copied data, thus also the ownership. Thus null data */
  968. ZVAL_NULL(data);
  969. } else {
  970. ZVAL_NULL(result);
  971. }
  972. }
  973. }
  974. } else {
  975. DBG_INF("skipping extraction");
  976. /*
  977. Data has been allocated and usually result->unbuf->m.free_last_data()
  978. frees it but we can't call this function as it will cause problems with
  979. the bound variables. Thus we need to do part of what it does or Zend will
  980. report leaks.
  981. */
  982. row_packet->result_set_memory_pool->free_chunk(
  983. row_packet->result_set_memory_pool, row_packet->row_buffer.ptr);
  984. row_packet->row_buffer.ptr = NULL;
  985. }
  986. /* We asked for one row, the next one should be EOF, eat it */
  987. ret = PACKET_READ(conn, row_packet);
  988. if (row_packet->row_buffer.ptr) {
  989. row_packet->result_set_memory_pool->free_chunk(
  990. row_packet->result_set_memory_pool, row_packet->row_buffer.ptr);
  991. row_packet->row_buffer.ptr = NULL;
  992. }
  993. MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_ROWS_FETCHED_FROM_CLIENT_PS_CURSOR);
  994. result->unbuf->row_count++;
  995. *fetched_anything = TRUE;
  996. } else {
  997. *fetched_anything = FALSE;
  998. UPSERT_STATUS_SET_WARNINGS(stmt->upsert_status, row_packet->warning_count);
  999. UPSERT_STATUS_SET_WARNINGS(conn->upsert_status, row_packet->warning_count);
  1000. UPSERT_STATUS_SET_SERVER_STATUS(stmt->upsert_status, row_packet->server_status);
  1001. UPSERT_STATUS_SET_SERVER_STATUS(conn->upsert_status, row_packet->server_status);
  1002. result->unbuf->eof_reached = row_packet->eof;
  1003. }
  1004. UPSERT_STATUS_SET_WARNINGS(stmt->upsert_status, row_packet->warning_count);
  1005. UPSERT_STATUS_SET_WARNINGS(conn->upsert_status, row_packet->warning_count);
  1006. UPSERT_STATUS_SET_SERVER_STATUS(stmt->upsert_status, row_packet->server_status);
  1007. UPSERT_STATUS_SET_SERVER_STATUS(conn->upsert_status, row_packet->server_status);
  1008. DBG_INF_FMT("ret=%s fetched=%u server_status=%u warnings=%u eof=%u",
  1009. ret == PASS? "PASS":"FAIL", *fetched_anything,
  1010. row_packet->server_status, row_packet->warning_count,
  1011. result->unbuf->eof_reached);
  1012. DBG_RETURN(ret);
  1013. }
  1014. /* }}} */
  1015. /* {{{ mysqlnd_stmt::fetch */
  1016. static enum_func_status
  1017. MYSQLND_METHOD(mysqlnd_stmt, fetch)(MYSQLND_STMT * const s, zend_bool * const fetched_anything)
  1018. {
  1019. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1020. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  1021. enum_func_status ret;
  1022. DBG_ENTER("mysqlnd_stmt::fetch");
  1023. if (!stmt || !stmt->conn) {
  1024. DBG_RETURN(FAIL);
  1025. }
  1026. DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
  1027. if (!stmt->result || stmt->state < MYSQLND_STMT_WAITING_USE_OR_STORE) {
  1028. SET_CLIENT_ERROR(stmt->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
  1029. DBG_ERR("command out of sync");
  1030. DBG_RETURN(FAIL);
  1031. } else if (stmt->state == MYSQLND_STMT_WAITING_USE_OR_STORE) {
  1032. /* Execute only once. We have to free the previous contents of user's bound vars */
  1033. stmt->default_rset_handler(s);
  1034. }
  1035. stmt->state = MYSQLND_STMT_USER_FETCHING;
  1036. SET_EMPTY_ERROR(stmt->error_info);
  1037. SET_EMPTY_ERROR(conn->error_info);
  1038. DBG_INF_FMT("result_bind=%p separated_once=%u", &stmt->result_bind, stmt->result_zvals_separated_once);
  1039. /*
  1040. The user might have not bound any variables for result.
  1041. Do the binding once she does it.
  1042. */
  1043. if (stmt->result_bind && !stmt->result_zvals_separated_once) {
  1044. unsigned int i;
  1045. /*
  1046. mysqlnd_stmt_store_result() has been called free the bind
  1047. variables to prevent leaking of their previous content.
  1048. */
  1049. for (i = 0; i < stmt->result->field_count; i++) {
  1050. if (stmt->result_bind[i].bound == TRUE) {
  1051. zval *result = &stmt->result_bind[i].zv;
  1052. ZVAL_DEREF(result);
  1053. zval_ptr_dtor(result);
  1054. ZVAL_NULL(result);
  1055. }
  1056. }
  1057. stmt->result_zvals_separated_once = TRUE;
  1058. }
  1059. ret = stmt->result->m.fetch_row(stmt->result, (void*)s, 0, fetched_anything);
  1060. DBG_RETURN(ret);
  1061. }
  1062. /* }}} */
  1063. /* {{{ mysqlnd_stmt::reset */
  1064. static enum_func_status
  1065. MYSQLND_METHOD(mysqlnd_stmt, reset)(MYSQLND_STMT * const s)
  1066. {
  1067. enum_func_status ret = PASS;
  1068. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1069. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  1070. DBG_ENTER("mysqlnd_stmt::reset");
  1071. if (!stmt || !conn) {
  1072. DBG_RETURN(FAIL);
  1073. }
  1074. DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
  1075. SET_EMPTY_ERROR(stmt->error_info);
  1076. SET_EMPTY_ERROR(conn->error_info);
  1077. if (stmt->stmt_id) {
  1078. MYSQLND_CONN_DATA * conn = stmt->conn;
  1079. if (stmt->param_bind) {
  1080. unsigned int i;
  1081. DBG_INF("resetting long data");
  1082. /* Reset Long Data */
  1083. for (i = 0; i < stmt->param_count; i++) {
  1084. if (stmt->param_bind[i].flags & MYSQLND_PARAM_BIND_BLOB_USED) {
  1085. stmt->param_bind[i].flags &= ~MYSQLND_PARAM_BIND_BLOB_USED;
  1086. }
  1087. }
  1088. }
  1089. s->m->flush(s);
  1090. /*
  1091. Don't free now, let the result be usable. When the stmt will again be
  1092. executed then the result set will be cleaned, the bound variables will
  1093. be separated before that.
  1094. */
  1095. if (GET_CONNECTION_STATE(&conn->state) == CONN_READY) {
  1096. size_t stmt_id = stmt->stmt_id;
  1097. ret = stmt->conn->run_command(COM_STMT_RESET, stmt->conn, stmt_id);
  1098. if (ret == FAIL) {
  1099. COPY_CLIENT_ERROR(stmt->error_info, *conn->error_info);
  1100. }
  1101. }
  1102. *stmt->upsert_status = *conn->upsert_status;
  1103. }
  1104. DBG_INF(ret == PASS? "PASS":"FAIL");
  1105. DBG_RETURN(ret);
  1106. }
  1107. /* }}} */
  1108. /* {{{ mysqlnd_stmt::flush */
  1109. static enum_func_status
  1110. MYSQLND_METHOD(mysqlnd_stmt, flush)(MYSQLND_STMT * const s)
  1111. {
  1112. enum_func_status ret = PASS;
  1113. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1114. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  1115. DBG_ENTER("mysqlnd_stmt::flush");
  1116. if (!stmt || !conn) {
  1117. DBG_RETURN(FAIL);
  1118. }
  1119. DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
  1120. if (stmt->stmt_id) {
  1121. /*
  1122. If the user decided to close the statement right after execute()
  1123. We have to call the appropriate use_result() or store_result() and
  1124. clean.
  1125. */
  1126. do {
  1127. if (stmt->state == MYSQLND_STMT_WAITING_USE_OR_STORE) {
  1128. DBG_INF("fetching result set header");
  1129. stmt->default_rset_handler(s);
  1130. stmt->state = MYSQLND_STMT_USER_FETCHING;
  1131. }
  1132. if (stmt->result) {
  1133. DBG_INF("skipping result");
  1134. stmt->result->m.skip_result(stmt->result);
  1135. }
  1136. } while (mysqlnd_stmt_more_results(s) && mysqlnd_stmt_next_result(s) == PASS);
  1137. }
  1138. DBG_INF(ret == PASS? "PASS":"FAIL");
  1139. DBG_RETURN(ret);
  1140. }
  1141. /* }}} */
  1142. /* {{{ mysqlnd_stmt::send_long_data */
  1143. static enum_func_status
  1144. MYSQLND_METHOD(mysqlnd_stmt, send_long_data)(MYSQLND_STMT * const s, unsigned int param_no,
  1145. const char * const data, zend_ulong data_length)
  1146. {
  1147. enum_func_status ret = FAIL;
  1148. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1149. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  1150. zend_uchar * cmd_buf;
  1151. DBG_ENTER("mysqlnd_stmt::send_long_data");
  1152. if (!stmt || !conn) {
  1153. DBG_RETURN(FAIL);
  1154. }
  1155. DBG_INF_FMT("stmt=%lu param_no=%u data_len=%lu", stmt->stmt_id, param_no, data_length);
  1156. SET_EMPTY_ERROR(stmt->error_info);
  1157. SET_EMPTY_ERROR(conn->error_info);
  1158. if (stmt->state < MYSQLND_STMT_PREPARED) {
  1159. SET_CLIENT_ERROR(stmt->error_info, CR_NO_PREPARE_STMT, UNKNOWN_SQLSTATE, mysqlnd_stmt_not_prepared);
  1160. DBG_ERR("not prepared");
  1161. DBG_RETURN(FAIL);
  1162. }
  1163. if (!stmt->param_bind) {
  1164. SET_CLIENT_ERROR(stmt->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
  1165. DBG_ERR("command out of sync");
  1166. DBG_RETURN(FAIL);
  1167. }
  1168. if (param_no >= stmt->param_count) {
  1169. SET_CLIENT_ERROR(stmt->error_info, CR_INVALID_PARAMETER_NO, UNKNOWN_SQLSTATE, "Invalid parameter number");
  1170. DBG_ERR("invalid param_no");
  1171. DBG_RETURN(FAIL);
  1172. }
  1173. if (stmt->param_bind[param_no].type != MYSQL_TYPE_LONG_BLOB) {
  1174. SET_CLIENT_ERROR(stmt->error_info, CR_INVALID_BUFFER_USE, UNKNOWN_SQLSTATE, mysqlnd_not_bound_as_blob);
  1175. DBG_ERR("param_no is not of a blob type");
  1176. DBG_RETURN(FAIL);
  1177. }
  1178. if (GET_CONNECTION_STATE(&conn->state) == CONN_READY) {
  1179. const size_t packet_len = MYSQLND_STMT_ID_LENGTH + 2 + data_length;
  1180. cmd_buf = mnd_emalloc(packet_len);
  1181. if (cmd_buf) {
  1182. stmt->param_bind[param_no].flags |= MYSQLND_PARAM_BIND_BLOB_USED;
  1183. int4store(cmd_buf, stmt->stmt_id);
  1184. int2store(cmd_buf + MYSQLND_STMT_ID_LENGTH, param_no);
  1185. memcpy(cmd_buf + MYSQLND_STMT_ID_LENGTH + 2, data, data_length);
  1186. /* COM_STMT_SEND_LONG_DATA doesn't acknowledge with an OK packet */
  1187. {
  1188. const MYSQLND_CSTRING payload = {(const char *) cmd_buf, packet_len};
  1189. ret = conn->run_command(COM_STMT_SEND_LONG_DATA, conn, payload);
  1190. if (ret == FAIL) {
  1191. COPY_CLIENT_ERROR(stmt->error_info, *conn->error_info);
  1192. }
  1193. }
  1194. mnd_efree(cmd_buf);
  1195. } else {
  1196. ret = FAIL;
  1197. SET_OOM_ERROR(stmt->error_info);
  1198. SET_OOM_ERROR(conn->error_info);
  1199. }
  1200. /*
  1201. Cover protocol error: COM_STMT_SEND_LONG_DATA was designed to be quick and not
  1202. sent response packets. According to documentation the only way to get an error
  1203. is to have out-of-memory on the server-side. However, that's not true, as if
  1204. max_allowed_packet_size is smaller than the chunk being sent to the server, the
  1205. latter will complain with an error message. However, normally we don't expect
  1206. an error message, thus we continue. When sending the next command, which expects
  1207. response we will read the unexpected data and error message will look weird.
  1208. Therefore we do non-blocking read to clean the line, if there is a need.
  1209. Nevertheless, there is a built-in protection when sending a command packet, that
  1210. checks if the line is clear - useful for debug purposes and to be switched off
  1211. in release builds.
  1212. Maybe we can make it automatic by checking what's the value of
  1213. max_allowed_packet_size on the server and resending the data.
  1214. */
  1215. #ifdef MYSQLND_DO_WIRE_CHECK_BEFORE_COMMAND
  1216. #if HAVE_USLEEP && !defined(PHP_WIN32)
  1217. usleep(120000);
  1218. #endif
  1219. if ((packet_len = conn->protocol_frame_codec->m.consume_uneaten_data(conn->protocol_frame_codec, COM_STMT_SEND_LONG_DATA))) {
  1220. php_error_docref(NULL, E_WARNING, "There was an error "
  1221. "while sending long data. Probably max_allowed_packet_size "
  1222. "is smaller than the data. You have to increase it or send "
  1223. "smaller chunks of data. Answer was "MYSQLND_SZ_T_SPEC" bytes long.", packet_len);
  1224. SET_CLIENT_ERROR(stmt->error_info, CR_CONNECTION_ERROR, UNKNOWN_SQLSTATE,
  1225. "Server responded to COM_STMT_SEND_LONG_DATA.");
  1226. ret = FAIL;
  1227. }
  1228. #endif
  1229. }
  1230. DBG_INF(ret == PASS? "PASS":"FAIL");
  1231. DBG_RETURN(ret);
  1232. }
  1233. /* }}} */
  1234. /* {{{ mysqlnd_stmt::bind_parameters */
  1235. static enum_func_status
  1236. MYSQLND_METHOD(mysqlnd_stmt, bind_parameters)(MYSQLND_STMT * const s, MYSQLND_PARAM_BIND * const param_bind)
  1237. {
  1238. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1239. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  1240. DBG_ENTER("mysqlnd_stmt::bind_param");
  1241. if (!stmt || !conn) {
  1242. DBG_RETURN(FAIL);
  1243. }
  1244. DBG_INF_FMT("stmt=%lu param_count=%u", stmt->stmt_id, stmt->param_count);
  1245. if (stmt->state < MYSQLND_STMT_PREPARED) {
  1246. SET_CLIENT_ERROR(stmt->error_info, CR_NO_PREPARE_STMT, UNKNOWN_SQLSTATE, mysqlnd_stmt_not_prepared);
  1247. DBG_ERR("not prepared");
  1248. if (param_bind) {
  1249. s->m->free_parameter_bind(s, param_bind);
  1250. }
  1251. DBG_RETURN(FAIL);
  1252. }
  1253. SET_EMPTY_ERROR(stmt->error_info);
  1254. SET_EMPTY_ERROR(conn->error_info);
  1255. if (stmt->param_count) {
  1256. unsigned int i = 0;
  1257. if (!param_bind) {
  1258. SET_CLIENT_ERROR(stmt->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, "Re-binding (still) not supported");
  1259. DBG_ERR("Re-binding (still) not supported");
  1260. DBG_RETURN(FAIL);
  1261. } else if (stmt->param_bind) {
  1262. DBG_INF("Binding");
  1263. /*
  1264. There is already result bound.
  1265. Forbid for now re-binding!!
  1266. */
  1267. for (i = 0; i < stmt->param_count; i++) {
  1268. /*
  1269. We may have the last reference, then call zval_ptr_dtor() or we may leak memory.
  1270. Switching from bind_one_parameter to bind_parameters may result in zv being NULL
  1271. */
  1272. zval_ptr_dtor(&stmt->param_bind[i].zv);
  1273. }
  1274. if (stmt->param_bind != param_bind) {
  1275. s->m->free_parameter_bind(s, stmt->param_bind);
  1276. }
  1277. }
  1278. stmt->param_bind = param_bind;
  1279. for (i = 0; i < stmt->param_count; i++) {
  1280. /* The client will use stmt_send_long_data */
  1281. DBG_INF_FMT("%u is of type %u", i, stmt->param_bind[i].type);
  1282. /* Prevent from freeing */
  1283. /* Don't update is_ref, or we will leak during conversion */
  1284. Z_TRY_ADDREF(stmt->param_bind[i].zv);
  1285. stmt->param_bind[i].flags = 0;
  1286. if (stmt->param_bind[i].type == MYSQL_TYPE_LONG_BLOB) {
  1287. stmt->param_bind[i].flags &= ~MYSQLND_PARAM_BIND_BLOB_USED;
  1288. }
  1289. }
  1290. stmt->send_types_to_server = 1;
  1291. }
  1292. DBG_INF("PASS");
  1293. DBG_RETURN(PASS);
  1294. }
  1295. /* }}} */
  1296. /* {{{ mysqlnd_stmt::bind_one_parameter */
  1297. static enum_func_status
  1298. MYSQLND_METHOD(mysqlnd_stmt, bind_one_parameter)(MYSQLND_STMT * const s, unsigned int param_no,
  1299. zval * const zv, zend_uchar type)
  1300. {
  1301. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1302. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  1303. DBG_ENTER("mysqlnd_stmt::bind_one_parameter");
  1304. if (!stmt || !conn) {
  1305. DBG_RETURN(FAIL);
  1306. }
  1307. DBG_INF_FMT("stmt=%lu param_no=%u param_count=%u type=%u", stmt->stmt_id, param_no, stmt->param_count, type);
  1308. if (stmt->state < MYSQLND_STMT_PREPARED) {
  1309. SET_CLIENT_ERROR(stmt->error_info, CR_NO_PREPARE_STMT, UNKNOWN_SQLSTATE, mysqlnd_stmt_not_prepared);
  1310. DBG_ERR("not prepared");
  1311. DBG_RETURN(FAIL);
  1312. }
  1313. if (param_no >= stmt->param_count) {
  1314. SET_CLIENT_ERROR(stmt->error_info, CR_INVALID_PARAMETER_NO, UNKNOWN_SQLSTATE, "Invalid parameter number");
  1315. DBG_ERR("invalid param_no");
  1316. DBG_RETURN(FAIL);
  1317. }
  1318. SET_EMPTY_ERROR(stmt->error_info);
  1319. SET_EMPTY_ERROR(conn->error_info);
  1320. if (stmt->param_count) {
  1321. if (!stmt->param_bind) {
  1322. stmt->param_bind = mnd_ecalloc(stmt->param_count, sizeof(MYSQLND_PARAM_BIND));
  1323. if (!stmt->param_bind) {
  1324. DBG_RETURN(FAIL);
  1325. }
  1326. }
  1327. /* Prevent from freeing */
  1328. /* Don't update is_ref, or we will leak during conversion */
  1329. Z_TRY_ADDREF_P(zv);
  1330. DBG_INF("Binding");
  1331. /* Release what we had, if we had */
  1332. zval_ptr_dtor(&stmt->param_bind[param_no].zv);
  1333. if (type == MYSQL_TYPE_LONG_BLOB) {
  1334. /* The client will use stmt_send_long_data */
  1335. stmt->param_bind[param_no].flags &= ~MYSQLND_PARAM_BIND_BLOB_USED;
  1336. }
  1337. ZVAL_COPY_VALUE(&stmt->param_bind[param_no].zv, zv);
  1338. stmt->param_bind[param_no].type = type;
  1339. stmt->send_types_to_server = 1;
  1340. }
  1341. DBG_INF("PASS");
  1342. DBG_RETURN(PASS);
  1343. }
  1344. /* }}} */
  1345. /* {{{ mysqlnd_stmt::refresh_bind_param */
  1346. static enum_func_status
  1347. MYSQLND_METHOD(mysqlnd_stmt, refresh_bind_param)(MYSQLND_STMT * const s)
  1348. {
  1349. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1350. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  1351. DBG_ENTER("mysqlnd_stmt::refresh_bind_param");
  1352. if (!stmt || !conn) {
  1353. DBG_RETURN(FAIL);
  1354. }
  1355. DBG_INF_FMT("stmt=%lu param_count=%u", stmt->stmt_id, stmt->param_count);
  1356. if (stmt->state < MYSQLND_STMT_PREPARED) {
  1357. SET_CLIENT_ERROR(stmt->error_info, CR_NO_PREPARE_STMT, UNKNOWN_SQLSTATE, mysqlnd_stmt_not_prepared);
  1358. DBG_ERR("not prepared");
  1359. DBG_RETURN(FAIL);
  1360. }
  1361. SET_EMPTY_ERROR(stmt->error_info);
  1362. SET_EMPTY_ERROR(conn->error_info);
  1363. if (stmt->param_count) {
  1364. stmt->send_types_to_server = 1;
  1365. }
  1366. DBG_RETURN(PASS);
  1367. }
  1368. /* }}} */
  1369. /* {{{ mysqlnd_stmt::bind_result */
  1370. static enum_func_status
  1371. MYSQLND_METHOD(mysqlnd_stmt, bind_result)(MYSQLND_STMT * const s,
  1372. MYSQLND_RESULT_BIND * const result_bind)
  1373. {
  1374. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1375. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  1376. DBG_ENTER("mysqlnd_stmt::bind_result");
  1377. if (!stmt || !conn) {
  1378. DBG_RETURN(FAIL);
  1379. }
  1380. DBG_INF_FMT("stmt=%lu field_count=%u", stmt->stmt_id, stmt->field_count);
  1381. if (stmt->state < MYSQLND_STMT_PREPARED) {
  1382. SET_CLIENT_ERROR(stmt->error_info, CR_NO_PREPARE_STMT, UNKNOWN_SQLSTATE, mysqlnd_stmt_not_prepared);
  1383. if (result_bind) {
  1384. s->m->free_result_bind(s, result_bind);
  1385. }
  1386. DBG_ERR("not prepared");
  1387. DBG_RETURN(FAIL);
  1388. }
  1389. SET_EMPTY_ERROR(stmt->error_info);
  1390. SET_EMPTY_ERROR(conn->error_info);
  1391. if (stmt->field_count) {
  1392. unsigned int i = 0;
  1393. if (!result_bind) {
  1394. DBG_ERR("no result bind passed");
  1395. DBG_RETURN(FAIL);
  1396. }
  1397. mysqlnd_stmt_separate_result_bind(s);
  1398. stmt->result_zvals_separated_once = FALSE;
  1399. stmt->result_bind = result_bind;
  1400. for (i = 0; i < stmt->field_count; i++) {
  1401. /* Prevent from freeing */
  1402. Z_TRY_ADDREF(stmt->result_bind[i].zv);
  1403. DBG_INF_FMT("ref of %p = %u", &stmt->result_bind[i].zv,
  1404. Z_REFCOUNTED(stmt->result_bind[i].zv)? Z_REFCOUNT(stmt->result_bind[i].zv) : 0);
  1405. /*
  1406. Don't update is_ref !!! it's not our job
  1407. Otherwise either 009.phpt or mysqli_stmt_bind_result.phpt
  1408. will fail.
  1409. */
  1410. stmt->result_bind[i].bound = TRUE;
  1411. }
  1412. } else if (result_bind) {
  1413. s->m->free_result_bind(s, result_bind);
  1414. }
  1415. DBG_INF("PASS");
  1416. DBG_RETURN(PASS);
  1417. }
  1418. /* }}} */
  1419. /* {{{ mysqlnd_stmt::bind_result */
  1420. static enum_func_status
  1421. MYSQLND_METHOD(mysqlnd_stmt, bind_one_result)(MYSQLND_STMT * const s, unsigned int param_no)
  1422. {
  1423. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1424. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  1425. DBG_ENTER("mysqlnd_stmt::bind_result");
  1426. if (!stmt || !conn) {
  1427. DBG_RETURN(FAIL);
  1428. }
  1429. DBG_INF_FMT("stmt=%lu field_count=%u", stmt->stmt_id, stmt->field_count);
  1430. if (stmt->state < MYSQLND_STMT_PREPARED) {
  1431. SET_CLIENT_ERROR(stmt->error_info, CR_NO_PREPARE_STMT, UNKNOWN_SQLSTATE, mysqlnd_stmt_not_prepared);
  1432. DBG_ERR("not prepared");
  1433. DBG_RETURN(FAIL);
  1434. }
  1435. if (param_no >= stmt->field_count) {
  1436. SET_CLIENT_ERROR(stmt->error_info, CR_INVALID_PARAMETER_NO, UNKNOWN_SQLSTATE, "Invalid parameter number");
  1437. DBG_ERR("invalid param_no");
  1438. DBG_RETURN(FAIL);
  1439. }
  1440. SET_EMPTY_ERROR(stmt->error_info);
  1441. SET_EMPTY_ERROR(conn->error_info);
  1442. if (stmt->field_count) {
  1443. mysqlnd_stmt_separate_one_result_bind(s, param_no);
  1444. /* Guaranteed is that stmt->result_bind is NULL */
  1445. if (!stmt->result_bind) {
  1446. stmt->result_bind = mnd_ecalloc(stmt->field_count, sizeof(MYSQLND_RESULT_BIND));
  1447. } else {
  1448. stmt->result_bind = mnd_erealloc(stmt->result_bind, stmt->field_count * sizeof(MYSQLND_RESULT_BIND));
  1449. }
  1450. if (!stmt->result_bind) {
  1451. DBG_RETURN(FAIL);
  1452. }
  1453. ZVAL_NULL(&stmt->result_bind[param_no].zv);
  1454. /*
  1455. Don't update is_ref !!! it's not our job
  1456. Otherwise either 009.phpt or mysqli_stmt_bind_result.phpt
  1457. will fail.
  1458. */
  1459. stmt->result_bind[param_no].bound = TRUE;
  1460. }
  1461. DBG_INF("PASS");
  1462. DBG_RETURN(PASS);
  1463. }
  1464. /* }}} */
  1465. /* {{{ mysqlnd_stmt::insert_id */
  1466. static uint64_t
  1467. MYSQLND_METHOD(mysqlnd_stmt, insert_id)(const MYSQLND_STMT * const s)
  1468. {
  1469. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1470. return stmt? UPSERT_STATUS_GET_LAST_INSERT_ID(stmt->upsert_status) : 0;
  1471. }
  1472. /* }}} */
  1473. /* {{{ mysqlnd_stmt::affected_rows */
  1474. static uint64_t
  1475. MYSQLND_METHOD(mysqlnd_stmt, affected_rows)(const MYSQLND_STMT * const s)
  1476. {
  1477. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1478. return stmt? UPSERT_STATUS_GET_AFFECTED_ROWS(stmt->upsert_status) : 0;
  1479. }
  1480. /* }}} */
  1481. /* {{{ mysqlnd_stmt::num_rows */
  1482. static uint64_t
  1483. MYSQLND_METHOD(mysqlnd_stmt, num_rows)(const MYSQLND_STMT * const s)
  1484. {
  1485. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1486. return stmt && stmt->result? mysqlnd_num_rows(stmt->result):0;
  1487. }
  1488. /* }}} */
  1489. /* {{{ mysqlnd_stmt::warning_count */
  1490. static unsigned int
  1491. MYSQLND_METHOD(mysqlnd_stmt, warning_count)(const MYSQLND_STMT * const s)
  1492. {
  1493. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1494. return stmt? UPSERT_STATUS_GET_WARNINGS(stmt->upsert_status) : 0;
  1495. }
  1496. /* }}} */
  1497. /* {{{ mysqlnd_stmt::server_status */
  1498. static unsigned int
  1499. MYSQLND_METHOD(mysqlnd_stmt, server_status)(const MYSQLND_STMT * const s)
  1500. {
  1501. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1502. return stmt? UPSERT_STATUS_GET_SERVER_STATUS(stmt->upsert_status) : 0;
  1503. }
  1504. /* }}} */
  1505. /* {{{ mysqlnd_stmt::field_count */
  1506. static unsigned int
  1507. MYSQLND_METHOD(mysqlnd_stmt, field_count)(const MYSQLND_STMT * const s)
  1508. {
  1509. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1510. return stmt? stmt->field_count : 0;
  1511. }
  1512. /* }}} */
  1513. /* {{{ mysqlnd_stmt::param_count */
  1514. static unsigned int
  1515. MYSQLND_METHOD(mysqlnd_stmt, param_count)(const MYSQLND_STMT * const s)
  1516. {
  1517. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1518. return stmt? stmt->param_count : 0;
  1519. }
  1520. /* }}} */
  1521. /* {{{ mysqlnd_stmt::errno */
  1522. static unsigned int
  1523. MYSQLND_METHOD(mysqlnd_stmt, errno)(const MYSQLND_STMT * const s)
  1524. {
  1525. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1526. return stmt? stmt->error_info->error_no : 0;
  1527. }
  1528. /* }}} */
  1529. /* {{{ mysqlnd_stmt::error */
  1530. static const char *
  1531. MYSQLND_METHOD(mysqlnd_stmt, error)(const MYSQLND_STMT * const s)
  1532. {
  1533. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1534. return stmt? stmt->error_info->error : 0;
  1535. }
  1536. /* }}} */
  1537. /* {{{ mysqlnd_stmt::sqlstate */
  1538. static const char *
  1539. MYSQLND_METHOD(mysqlnd_stmt, sqlstate)(const MYSQLND_STMT * const s)
  1540. {
  1541. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1542. return stmt && stmt->error_info->sqlstate[0] ? stmt->error_info->sqlstate:MYSQLND_SQLSTATE_NULL;
  1543. }
  1544. /* }}} */
  1545. /* {{{ mysqlnd_stmt::data_seek */
  1546. static enum_func_status
  1547. MYSQLND_METHOD(mysqlnd_stmt, data_seek)(const MYSQLND_STMT * const s, uint64_t row)
  1548. {
  1549. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1550. return stmt && stmt->result? stmt->result->m.seek_data(stmt->result, row) : FAIL;
  1551. }
  1552. /* }}} */
  1553. /* {{{ mysqlnd_stmt::param_metadata */
  1554. static MYSQLND_RES *
  1555. MYSQLND_METHOD(mysqlnd_stmt, param_metadata)(MYSQLND_STMT * const s)
  1556. {
  1557. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1558. if (!stmt || !stmt->param_count) {
  1559. return NULL;
  1560. }
  1561. return NULL;
  1562. }
  1563. /* }}} */
  1564. /* {{{ mysqlnd_stmt::result_metadata */
  1565. static MYSQLND_RES *
  1566. MYSQLND_METHOD(mysqlnd_stmt, result_metadata)(MYSQLND_STMT * const s)
  1567. {
  1568. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1569. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  1570. MYSQLND_RES * result_meta = NULL;
  1571. DBG_ENTER("mysqlnd_stmt::result_metadata");
  1572. if (!stmt || ! conn) {
  1573. DBG_RETURN(NULL);
  1574. }
  1575. DBG_INF_FMT("stmt=%u field_count=%u", stmt->stmt_id, stmt->field_count);
  1576. if (!stmt->field_count || !stmt->result || !stmt->result->meta) {
  1577. DBG_INF("NULL");
  1578. DBG_RETURN(NULL);
  1579. }
  1580. if (stmt->update_max_length && stmt->result->stored_data) {
  1581. /* stored result, we have to update the max_length before we clone the meta data :( */
  1582. stmt->result->stored_data->m.initialize_result_set_rest(stmt->result->stored_data,
  1583. stmt->result->meta,
  1584. conn->stats,
  1585. conn->options->int_and_float_native);
  1586. }
  1587. /*
  1588. TODO: This implementation is kind of a hack,
  1589. find a better way to do it. In different functions I have put
  1590. fuses to check for result->m.fetch_row() being NULL. This should
  1591. be handled in a better way.
  1592. */
  1593. do {
  1594. result_meta = conn->m->result_init(stmt->field_count);
  1595. if (!result_meta) {
  1596. break;
  1597. }
  1598. result_meta->type = MYSQLND_RES_NORMAL;
  1599. result_meta->unbuf = mysqlnd_result_unbuffered_init(result_meta, stmt->field_count, TRUE);
  1600. if (!result_meta->unbuf) {
  1601. break;
  1602. }
  1603. result_meta->unbuf->eof_reached = TRUE;
  1604. result_meta->meta = stmt->result->meta->m->clone_metadata(result_meta, stmt->result->meta);
  1605. if (!result_meta->meta) {
  1606. break;
  1607. }
  1608. DBG_INF_FMT("result_meta=%p", result_meta);
  1609. DBG_RETURN(result_meta);
  1610. } while (0);
  1611. SET_OOM_ERROR(conn->error_info);
  1612. if (result_meta) {
  1613. result_meta->m.free_result(result_meta, TRUE);
  1614. }
  1615. DBG_RETURN(NULL);
  1616. }
  1617. /* }}} */
  1618. /* {{{ mysqlnd_stmt::attr_set */
  1619. static enum_func_status
  1620. MYSQLND_METHOD(mysqlnd_stmt, attr_set)(MYSQLND_STMT * const s,
  1621. enum mysqlnd_stmt_attr attr_type,
  1622. const void * const value)
  1623. {
  1624. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1625. DBG_ENTER("mysqlnd_stmt::attr_set");
  1626. if (!stmt) {
  1627. DBG_RETURN(FAIL);
  1628. }
  1629. DBG_INF_FMT("stmt=%lu attr_type=%u", stmt->stmt_id, attr_type);
  1630. switch (attr_type) {
  1631. case STMT_ATTR_UPDATE_MAX_LENGTH:{
  1632. zend_uchar bval = *(zend_uchar *) value;
  1633. /*
  1634. XXX : libmysql uses my_bool, but mysqli uses ulong as storage on the stack
  1635. and mysqlnd won't be used out of the scope of PHP -> use ulong.
  1636. */
  1637. stmt->update_max_length = bval? TRUE:FALSE;
  1638. break;
  1639. }
  1640. case STMT_ATTR_CURSOR_TYPE: {
  1641. unsigned long ival = *(unsigned long *) value;
  1642. if (ival > (unsigned long) CURSOR_TYPE_READ_ONLY) {
  1643. SET_CLIENT_ERROR(stmt->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, "Not implemented");
  1644. DBG_INF("FAIL");
  1645. DBG_RETURN(FAIL);
  1646. }
  1647. stmt->flags = ival;
  1648. break;
  1649. }
  1650. case STMT_ATTR_PREFETCH_ROWS: {
  1651. unsigned long ival = *(unsigned long *) value;
  1652. if (ival == 0) {
  1653. ival = MYSQLND_DEFAULT_PREFETCH_ROWS;
  1654. } else if (ival > 1) {
  1655. SET_CLIENT_ERROR(stmt->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, "Not implemented");
  1656. DBG_INF("FAIL");
  1657. DBG_RETURN(FAIL);
  1658. }
  1659. stmt->prefetch_rows = ival;
  1660. break;
  1661. }
  1662. default:
  1663. SET_CLIENT_ERROR(stmt->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, "Not implemented");
  1664. DBG_RETURN(FAIL);
  1665. }
  1666. DBG_INF("PASS");
  1667. DBG_RETURN(PASS);
  1668. }
  1669. /* }}} */
  1670. /* {{{ mysqlnd_stmt::attr_get */
  1671. static enum_func_status
  1672. MYSQLND_METHOD(mysqlnd_stmt, attr_get)(const MYSQLND_STMT * const s,
  1673. enum mysqlnd_stmt_attr attr_type,
  1674. void * const value)
  1675. {
  1676. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1677. DBG_ENTER("mysqlnd_stmt::attr_set");
  1678. if (!stmt) {
  1679. DBG_RETURN(FAIL);
  1680. }
  1681. DBG_INF_FMT("stmt=%lu attr_type=%u", stmt->stmt_id, attr_type);
  1682. switch (attr_type) {
  1683. case STMT_ATTR_UPDATE_MAX_LENGTH:
  1684. *(zend_bool *) value= stmt->update_max_length;
  1685. break;
  1686. case STMT_ATTR_CURSOR_TYPE:
  1687. *(unsigned long *) value= stmt->flags;
  1688. break;
  1689. case STMT_ATTR_PREFETCH_ROWS:
  1690. *(unsigned long *) value= stmt->prefetch_rows;
  1691. break;
  1692. default:
  1693. DBG_RETURN(FAIL);
  1694. }
  1695. DBG_INF_FMT("value=%lu", value);
  1696. DBG_RETURN(PASS);
  1697. }
  1698. /* }}} */
  1699. /* free_result() doesn't actually free stmt->result but only the buffers */
  1700. /* {{{ mysqlnd_stmt::free_result */
  1701. static enum_func_status
  1702. MYSQLND_METHOD(mysqlnd_stmt, free_result)(MYSQLND_STMT * const s)
  1703. {
  1704. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1705. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  1706. DBG_ENTER("mysqlnd_stmt::free_result");
  1707. if (!stmt || !conn) {
  1708. DBG_RETURN(FAIL);
  1709. }
  1710. DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
  1711. if (!stmt->result) {
  1712. DBG_INF("no result");
  1713. DBG_RETURN(PASS);
  1714. }
  1715. /*
  1716. If right after execute() we have to call the appropriate
  1717. use_result() or store_result() and clean.
  1718. */
  1719. if (stmt->state == MYSQLND_STMT_WAITING_USE_OR_STORE) {
  1720. DBG_INF("fetching result set header");
  1721. /* Do implicit use_result and then flush the result */
  1722. stmt->default_rset_handler = s->m->use_result;
  1723. stmt->default_rset_handler(s);
  1724. }
  1725. if (stmt->state > MYSQLND_STMT_WAITING_USE_OR_STORE) {
  1726. DBG_INF("skipping result");
  1727. /* Flush if anything is left and unbuffered set */
  1728. stmt->result->m.skip_result(stmt->result);
  1729. /*
  1730. Separate the bound variables, which point to the result set, then
  1731. destroy the set.
  1732. */
  1733. mysqlnd_stmt_separate_result_bind(s);
  1734. /* Now we can destroy the result set */
  1735. stmt->result->m.free_result_buffers(stmt->result);
  1736. }
  1737. if (stmt->state > MYSQLND_STMT_PREPARED) {
  1738. /* As the buffers have been freed, we should go back to PREPARED */
  1739. stmt->state = MYSQLND_STMT_PREPARED;
  1740. }
  1741. if (GET_CONNECTION_STATE(&conn->state) != CONN_QUIT_SENT) {
  1742. SET_CONNECTION_STATE(&conn->state, CONN_READY);
  1743. }
  1744. DBG_RETURN(PASS);
  1745. }
  1746. /* }}} */
  1747. /* {{{ mysqlnd_stmt_separate_result_bind */
  1748. static void
  1749. mysqlnd_stmt_separate_result_bind(MYSQLND_STMT * const s)
  1750. {
  1751. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1752. unsigned int i;
  1753. DBG_ENTER("mysqlnd_stmt_separate_result_bind");
  1754. if (!stmt) {
  1755. DBG_VOID_RETURN;
  1756. }
  1757. DBG_INF_FMT("stmt=%lu result_bind=%p field_count=%u", stmt->stmt_id, stmt->result_bind, stmt->field_count);
  1758. if (!stmt->result_bind) {
  1759. DBG_VOID_RETURN;
  1760. }
  1761. /*
  1762. Because only the bound variables can point to our internal buffers, then
  1763. separate or free only them. Free is possible because the user could have
  1764. lost reference.
  1765. */
  1766. for (i = 0; i < stmt->field_count; i++) {
  1767. /* Let's try with no cache */
  1768. if (stmt->result_bind[i].bound == TRUE) {
  1769. DBG_INF_FMT("%u has refcount=%u", i, Z_REFCOUNTED(stmt->result_bind[i].zv)? Z_REFCOUNT(stmt->result_bind[i].zv) : 0);
  1770. zval_ptr_dtor(&stmt->result_bind[i].zv);
  1771. }
  1772. }
  1773. s->m->free_result_bind(s, stmt->result_bind);
  1774. stmt->result_bind = NULL;
  1775. DBG_VOID_RETURN;
  1776. }
  1777. /* }}} */
  1778. /* {{{ mysqlnd_stmt_separate_one_result_bind */
  1779. static void
  1780. mysqlnd_stmt_separate_one_result_bind(MYSQLND_STMT * const s, const unsigned int param_no)
  1781. {
  1782. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1783. DBG_ENTER("mysqlnd_stmt_separate_one_result_bind");
  1784. if (!stmt) {
  1785. DBG_VOID_RETURN;
  1786. }
  1787. DBG_INF_FMT("stmt=%lu result_bind=%p field_count=%u param_no=%u", stmt->stmt_id, stmt->result_bind, stmt->field_count, param_no);
  1788. if (!stmt->result_bind) {
  1789. DBG_VOID_RETURN;
  1790. }
  1791. /*
  1792. Because only the bound variables can point to our internal buffers, then
  1793. separate or free only them. Free is possible because the user could have
  1794. lost reference.
  1795. */
  1796. /* Let's try with no cache */
  1797. if (stmt->result_bind[param_no].bound == TRUE) {
  1798. DBG_INF_FMT("%u has refcount=%u", param_no, Z_REFCOUNTED(stmt->result_bind[param_no].zv)? Z_REFCOUNT(stmt->result_bind[param_no].zv) : 0);
  1799. zval_ptr_dtor(&stmt->result_bind[param_no].zv);
  1800. }
  1801. DBG_VOID_RETURN;
  1802. }
  1803. /* }}} */
  1804. /* {{{ mysqlnd_stmt::free_stmt_result */
  1805. static void
  1806. MYSQLND_METHOD(mysqlnd_stmt, free_stmt_result)(MYSQLND_STMT * const s)
  1807. {
  1808. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1809. DBG_ENTER("mysqlnd_stmt::free_stmt_result");
  1810. if (!stmt) {
  1811. DBG_VOID_RETURN;
  1812. }
  1813. /*
  1814. First separate the bound variables, which point to the result set, then
  1815. destroy the set.
  1816. */
  1817. mysqlnd_stmt_separate_result_bind(s);
  1818. /* Not every statement has a result set attached */
  1819. if (stmt->result) {
  1820. stmt->result->m.free_result_internal(stmt->result);
  1821. stmt->result = NULL;
  1822. }
  1823. zend_llist_clean(&stmt->error_info->error_list);
  1824. DBG_VOID_RETURN;
  1825. }
  1826. /* }}} */
  1827. /* {{{ mysqlnd_stmt::free_stmt_content */
  1828. static void
  1829. MYSQLND_METHOD(mysqlnd_stmt, free_stmt_content)(MYSQLND_STMT * const s)
  1830. {
  1831. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1832. DBG_ENTER("mysqlnd_stmt::free_stmt_content");
  1833. if (!stmt) {
  1834. DBG_VOID_RETURN;
  1835. }
  1836. DBG_INF_FMT("stmt=%lu param_bind=%p param_count=%u", stmt->stmt_id, stmt->param_bind, stmt->param_count);
  1837. /* Destroy the input bind */
  1838. if (stmt->param_bind) {
  1839. unsigned int i;
  1840. /*
  1841. Because only the bound variables can point to our internal buffers, then
  1842. separate or free only them. Free is possible because the user could have
  1843. lost reference.
  1844. */
  1845. for (i = 0; i < stmt->param_count; i++) {
  1846. /*
  1847. If bind_one_parameter was used, but not everything was
  1848. bound and nothing was fetched, then some `zv` could be NULL
  1849. */
  1850. zval_ptr_dtor(&stmt->param_bind[i].zv);
  1851. }
  1852. s->m->free_parameter_bind(s, stmt->param_bind);
  1853. stmt->param_bind = NULL;
  1854. }
  1855. s->m->free_stmt_result(s);
  1856. DBG_VOID_RETURN;
  1857. }
  1858. /* }}} */
  1859. /* {{{ mysqlnd_stmt::close_on_server */
  1860. static enum_func_status
  1861. MYSQLND_METHOD_PRIVATE(mysqlnd_stmt, close_on_server)(MYSQLND_STMT * const s, zend_bool implicit)
  1862. {
  1863. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1864. MYSQLND_CONN_DATA * conn = stmt? stmt->conn : NULL;
  1865. enum_mysqlnd_collected_stats statistic = STAT_LAST;
  1866. DBG_ENTER("mysqlnd_stmt::close_on_server");
  1867. if (!stmt || !conn) {
  1868. DBG_RETURN(FAIL);
  1869. }
  1870. DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
  1871. SET_EMPTY_ERROR(stmt->error_info);
  1872. SET_EMPTY_ERROR(conn->error_info);
  1873. /*
  1874. If the user decided to close the statement right after execute()
  1875. We have to call the appropriate use_result() or store_result() and
  1876. clean.
  1877. */
  1878. do {
  1879. if (stmt->state == MYSQLND_STMT_WAITING_USE_OR_STORE) {
  1880. DBG_INF("fetching result set header");
  1881. stmt->default_rset_handler(s);
  1882. stmt->state = MYSQLND_STMT_USER_FETCHING;
  1883. }
  1884. /* unbuffered set not fetched to the end ? Clean the line */
  1885. if (stmt->result) {
  1886. DBG_INF("skipping result");
  1887. stmt->result->m.skip_result(stmt->result);
  1888. }
  1889. } while (mysqlnd_stmt_more_results(s) && mysqlnd_stmt_next_result(s) == PASS);
  1890. /*
  1891. After this point we are allowed to free the result set,
  1892. as we have cleaned the line
  1893. */
  1894. if (stmt->stmt_id) {
  1895. MYSQLND_INC_GLOBAL_STATISTIC(implicit == TRUE? STAT_FREE_RESULT_IMPLICIT:
  1896. STAT_FREE_RESULT_EXPLICIT);
  1897. if (GET_CONNECTION_STATE(&conn->state) == CONN_READY) {
  1898. enum_func_status ret = FAIL;
  1899. size_t stmt_id = stmt->stmt_id;
  1900. ret = conn->run_command(COM_STMT_CLOSE, conn, stmt_id);
  1901. if (ret == FAIL) {
  1902. COPY_CLIENT_ERROR(stmt->error_info, *conn->error_info);
  1903. DBG_RETURN(FAIL);
  1904. }
  1905. }
  1906. }
  1907. switch (stmt->execute_count) {
  1908. case 0:
  1909. statistic = STAT_PS_PREPARED_NEVER_EXECUTED;
  1910. break;
  1911. case 1:
  1912. statistic = STAT_PS_PREPARED_ONCE_USED;
  1913. break;
  1914. default:
  1915. break;
  1916. }
  1917. if (statistic != STAT_LAST) {
  1918. MYSQLND_INC_CONN_STATISTIC(conn->stats, statistic);
  1919. }
  1920. if (stmt->execute_cmd_buffer.buffer) {
  1921. mnd_efree(stmt->execute_cmd_buffer.buffer);
  1922. stmt->execute_cmd_buffer.buffer = NULL;
  1923. }
  1924. s->m->free_stmt_content(s);
  1925. if (conn) {
  1926. conn->m->free_reference(conn);
  1927. stmt->conn = NULL;
  1928. }
  1929. DBG_RETURN(PASS);
  1930. }
  1931. /* }}} */
  1932. /* {{{ mysqlnd_stmt::dtor */
  1933. static enum_func_status
  1934. MYSQLND_METHOD(mysqlnd_stmt, dtor)(MYSQLND_STMT * const s, zend_bool implicit)
  1935. {
  1936. MYSQLND_STMT_DATA * stmt = (s != NULL) ? s->data:NULL;
  1937. enum_func_status ret = FAIL;
  1938. DBG_ENTER("mysqlnd_stmt::dtor");
  1939. if (stmt) {
  1940. DBG_INF_FMT("stmt=%p", stmt);
  1941. MYSQLND_INC_GLOBAL_STATISTIC(implicit == TRUE? STAT_STMT_CLOSE_IMPLICIT:
  1942. STAT_STMT_CLOSE_EXPLICIT);
  1943. ret = s->m->close_on_server(s, implicit);
  1944. mnd_efree(stmt);
  1945. }
  1946. mnd_efree(s);
  1947. DBG_INF(ret == PASS? "PASS":"FAIL");
  1948. DBG_RETURN(ret);
  1949. }
  1950. /* }}} */
  1951. /* {{{ mysqlnd_stmt::alloc_param_bind */
  1952. static MYSQLND_PARAM_BIND *
  1953. MYSQLND_METHOD(mysqlnd_stmt, alloc_param_bind)(MYSQLND_STMT * const s)
  1954. {
  1955. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1956. DBG_ENTER("mysqlnd_stmt::alloc_param_bind");
  1957. if (!stmt) {
  1958. DBG_RETURN(NULL);
  1959. }
  1960. DBG_RETURN(mnd_ecalloc(stmt->param_count, sizeof(MYSQLND_PARAM_BIND)));
  1961. }
  1962. /* }}} */
  1963. /* {{{ mysqlnd_stmt::alloc_result_bind */
  1964. static MYSQLND_RESULT_BIND *
  1965. MYSQLND_METHOD(mysqlnd_stmt, alloc_result_bind)(MYSQLND_STMT * const s)
  1966. {
  1967. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1968. DBG_ENTER("mysqlnd_stmt::alloc_result_bind");
  1969. if (!stmt) {
  1970. DBG_RETURN(NULL);
  1971. }
  1972. DBG_RETURN(mnd_ecalloc(stmt->field_count, sizeof(MYSQLND_RESULT_BIND)));
  1973. }
  1974. /* }}} */
  1975. /* {{{ param_bind::free_parameter_bind */
  1976. PHPAPI void
  1977. MYSQLND_METHOD(mysqlnd_stmt, free_parameter_bind)(MYSQLND_STMT * const s, MYSQLND_PARAM_BIND * param_bind)
  1978. {
  1979. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1980. if (stmt) {
  1981. mnd_efree(param_bind);
  1982. }
  1983. }
  1984. /* }}} */
  1985. /* {{{ mysqlnd_stmt::free_result_bind */
  1986. PHPAPI void
  1987. MYSQLND_METHOD(mysqlnd_stmt, free_result_bind)(MYSQLND_STMT * const s, MYSQLND_RESULT_BIND * result_bind)
  1988. {
  1989. MYSQLND_STMT_DATA * stmt = s? s->data : NULL;
  1990. if (stmt) {
  1991. mnd_efree(result_bind);
  1992. }
  1993. }
  1994. /* }}} */
  1995. MYSQLND_CLASS_METHODS_START(mysqlnd_stmt)
  1996. MYSQLND_METHOD(mysqlnd_stmt, prepare),
  1997. MYSQLND_METHOD(mysqlnd_stmt, send_execute),
  1998. MYSQLND_METHOD(mysqlnd_stmt, execute),
  1999. MYSQLND_METHOD(mysqlnd_stmt, use_result),
  2000. MYSQLND_METHOD(mysqlnd_stmt, store_result),
  2001. MYSQLND_METHOD(mysqlnd_stmt, get_result),
  2002. MYSQLND_METHOD(mysqlnd_stmt, more_results),
  2003. MYSQLND_METHOD(mysqlnd_stmt, next_result),
  2004. MYSQLND_METHOD(mysqlnd_stmt, free_result),
  2005. MYSQLND_METHOD(mysqlnd_stmt, data_seek),
  2006. MYSQLND_METHOD(mysqlnd_stmt, reset),
  2007. MYSQLND_METHOD_PRIVATE(mysqlnd_stmt, close_on_server),
  2008. MYSQLND_METHOD(mysqlnd_stmt, dtor),
  2009. MYSQLND_METHOD(mysqlnd_stmt, fetch),
  2010. MYSQLND_METHOD(mysqlnd_stmt, bind_parameters),
  2011. MYSQLND_METHOD(mysqlnd_stmt, bind_one_parameter),
  2012. MYSQLND_METHOD(mysqlnd_stmt, refresh_bind_param),
  2013. MYSQLND_METHOD(mysqlnd_stmt, bind_result),
  2014. MYSQLND_METHOD(mysqlnd_stmt, bind_one_result),
  2015. MYSQLND_METHOD(mysqlnd_stmt, send_long_data),
  2016. MYSQLND_METHOD(mysqlnd_stmt, param_metadata),
  2017. MYSQLND_METHOD(mysqlnd_stmt, result_metadata),
  2018. MYSQLND_METHOD(mysqlnd_stmt, insert_id),
  2019. MYSQLND_METHOD(mysqlnd_stmt, affected_rows),
  2020. MYSQLND_METHOD(mysqlnd_stmt, num_rows),
  2021. MYSQLND_METHOD(mysqlnd_stmt, param_count),
  2022. MYSQLND_METHOD(mysqlnd_stmt, field_count),
  2023. MYSQLND_METHOD(mysqlnd_stmt, warning_count),
  2024. MYSQLND_METHOD(mysqlnd_stmt, errno),
  2025. MYSQLND_METHOD(mysqlnd_stmt, error),
  2026. MYSQLND_METHOD(mysqlnd_stmt, sqlstate),
  2027. MYSQLND_METHOD(mysqlnd_stmt, attr_get),
  2028. MYSQLND_METHOD(mysqlnd_stmt, attr_set),
  2029. MYSQLND_METHOD(mysqlnd_stmt, alloc_param_bind),
  2030. MYSQLND_METHOD(mysqlnd_stmt, alloc_result_bind),
  2031. MYSQLND_METHOD(mysqlnd_stmt, free_parameter_bind),
  2032. MYSQLND_METHOD(mysqlnd_stmt, free_result_bind),
  2033. MYSQLND_METHOD(mysqlnd_stmt, server_status),
  2034. mysqlnd_stmt_execute_generate_request,
  2035. mysqlnd_stmt_execute_parse_response,
  2036. MYSQLND_METHOD(mysqlnd_stmt, free_stmt_content),
  2037. MYSQLND_METHOD(mysqlnd_stmt, flush),
  2038. MYSQLND_METHOD(mysqlnd_stmt, free_stmt_result)
  2039. MYSQLND_CLASS_METHODS_END;
  2040. /* {{{ _mysqlnd_init_ps_subsystem */
  2041. void _mysqlnd_init_ps_subsystem()
  2042. {
  2043. mysqlnd_stmt_set_methods(&MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_stmt));
  2044. _mysqlnd_init_ps_fetch_subsystem();
  2045. }
  2046. /* }}} */
  2047. /*
  2048. * Local variables:
  2049. * tab-width: 4
  2050. * c-basic-offset: 4
  2051. * End:
  2052. * vim600: noet sw=4 ts=4 fdm=marker
  2053. * vim<600: noet sw=4 ts=4
  2054. */