oci8_statement.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Stig Sæther Bakken <ssb@php.net> |
  14. | Thies C. Arntzen <thies@thieso.net> |
  15. | |
  16. | Collection support by Andy Sautins <asautins@veripost.net> |
  17. | Temporary LOB support by David Benson <dbenson@mancala.com> |
  18. | ZTS per process OCIPLogon by Harald Radi <harald.radi@nme.at> |
  19. | |
  20. | Redesigned by: Antony Dovgal <antony@zend.com> |
  21. | Andi Gutmans <andi@php.net> |
  22. | Wez Furlong <wez@omniti.com> |
  23. +----------------------------------------------------------------------+
  24. */
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28. #include "php.h"
  29. #include "ext/standard/info.h"
  30. #include "php_ini.h"
  31. #ifdef HAVE_OCI8
  32. #include "php_oci8.h"
  33. #include "php_oci8_int.h"
  34. #if defined(OCI_MAJOR_VERSION) && (OCI_MAJOR_VERSION > 10) && \
  35. (defined(__x86_64__) || defined(__LP64__) || defined(_LP64) || defined(_WIN64))
  36. typedef ub8 oci_phpsized_int;
  37. #else
  38. typedef ub4 oci_phpsized_int;
  39. #endif
  40. /* {{{ php_oci_statement_create()
  41. Create statemend handle and allocate necessary resources */
  42. php_oci_statement *php_oci_statement_create(php_oci_connection *connection, char *query, int query_len)
  43. {
  44. php_oci_statement *statement;
  45. sword errstatus;
  46. connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  47. statement = ecalloc(1,sizeof(php_oci_statement));
  48. if (!query_len) {
  49. /* do not allocate stmt handle for refcursors, we'll get it from OCIStmtPrepare2() */
  50. PHP_OCI_CALL(OCIHandleAlloc, (connection->env, (dvoid **)&(statement->stmt), OCI_HTYPE_STMT, 0, NULL));
  51. }
  52. PHP_OCI_CALL(OCIHandleAlloc, (connection->env, (dvoid **)&(statement->err), OCI_HTYPE_ERROR, 0, NULL));
  53. if (query_len > 0) {
  54. PHP_OCI_CALL_RETURN(errstatus, OCIStmtPrepare2,
  55. (
  56. connection->svc,
  57. &(statement->stmt),
  58. connection->err,
  59. (text *)query,
  60. query_len,
  61. NULL,
  62. 0,
  63. OCI_NTV_SYNTAX,
  64. OCI_DEFAULT
  65. )
  66. );
  67. #ifdef HAVE_OCI8_DTRACE
  68. if (DTRACE_OCI8_SQLTEXT_ENABLED()) {
  69. DTRACE_OCI8_SQLTEXT(connection, connection->client_id, statement, query);
  70. }
  71. #endif /* HAVE_OCI8_DTRACE */
  72. if (errstatus != OCI_SUCCESS) {
  73. connection->errcode = php_oci_error(connection->err, errstatus);
  74. PHP_OCI_CALL(OCIStmtRelease, (statement->stmt, statement->err, NULL, 0, OCI_STRLS_CACHE_DELETE));
  75. PHP_OCI_CALL(OCIHandleFree,(statement->err, OCI_HTYPE_ERROR));
  76. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  77. efree(statement);
  78. return NULL;
  79. }
  80. }
  81. if (query && query_len) {
  82. statement->last_query = ecalloc(1, query_len + 1);
  83. memcpy(statement->last_query, query, query_len);
  84. statement->last_query_len = query_len;
  85. }
  86. else {
  87. statement->last_query = NULL;
  88. statement->last_query_len = 0;
  89. }
  90. statement->connection = connection;
  91. statement->has_data = 0;
  92. statement->has_descr = 0;
  93. statement->parent_stmtid = 0;
  94. statement->impres_child_stmt = NULL;
  95. statement->impres_count = 0;
  96. statement->impres_flag = PHP_OCI_IMPRES_UNKNOWN; /* may or may not have Implicit Result Set children */
  97. GC_ADDREF(statement->connection->id);
  98. if (OCI_G(default_prefetch) >= 0) {
  99. php_oci_statement_set_prefetch(statement, (ub4)OCI_G(default_prefetch));
  100. } else {
  101. php_oci_statement_set_prefetch(statement, (ub4)100); /* semi-arbitrary, "sensible default" */
  102. }
  103. PHP_OCI_REGISTER_RESOURCE(statement, le_statement);
  104. OCI_G(num_statements)++;
  105. return statement;
  106. }
  107. /* }}} */
  108. /* {{{ php_oci_get_implicit_resultset()
  109. Fetch implicit result set statement resource */
  110. php_oci_statement *php_oci_get_implicit_resultset(php_oci_statement *statement)
  111. {
  112. #if (OCI_MAJOR_VERSION < 12)
  113. php_error_docref(NULL, E_WARNING, "Implicit results are available in Oracle Database 12c onwards");
  114. return NULL;
  115. #else
  116. void *result;
  117. ub4 rtype;
  118. php_oci_statement *statement2; /* implicit result set statement handle */
  119. sword errstatus;
  120. PHP_OCI_CALL_RETURN(errstatus, OCIStmtGetNextResult, (statement->stmt, statement->err, &result, &rtype, OCI_DEFAULT));
  121. if (errstatus == OCI_NO_DATA) {
  122. return NULL;
  123. }
  124. if (rtype != OCI_RESULT_TYPE_SELECT) {
  125. /* Only OCI_RESULT_TYPE_SELECT is supported by Oracle DB 12cR1 */
  126. php_error_docref(NULL, E_WARNING, "Unexpected implicit result type returned from Oracle Database");
  127. return NULL;
  128. } else {
  129. statement2 = ecalloc(1,sizeof(php_oci_statement));
  130. PHP_OCI_CALL(OCIHandleAlloc, (statement->connection->env, (dvoid **)&(statement2->err), OCI_HTYPE_ERROR, 0, NULL));
  131. statement2->stmt = (OCIStmt *)result;
  132. statement2->parent_stmtid = statement->id;
  133. statement2->impres_child_stmt = NULL;
  134. statement2->impres_count = 0;
  135. statement2->impres_flag = PHP_OCI_IMPRES_IS_CHILD;
  136. statement2->connection = statement->connection;
  137. statement2->errcode = 0;
  138. statement2->last_query = NULL;
  139. statement2->last_query_len = 0;
  140. statement2->columns = NULL;
  141. statement2->binds = NULL;
  142. statement2->defines = NULL;
  143. statement2->ncolumns = 0;
  144. statement2->executed = 0;
  145. statement2->has_data = 0;
  146. statement2->has_descr = 0;
  147. statement2->stmttype = 0;
  148. GC_ADDREF(statement->id);
  149. GC_ADDREF(statement2->connection->id);
  150. php_oci_statement_set_prefetch(statement2, statement->prefetch_count);
  151. PHP_OCI_REGISTER_RESOURCE(statement2, le_statement);
  152. OCI_G(num_statements)++;
  153. return statement2;
  154. }
  155. #endif /* OCI_MAJOR_VERSION < 12 */
  156. }
  157. /* }}} */
  158. /* {{{ php_oci_statement_set_prefetch()
  159. Set prefetch buffer size for the statement */
  160. int php_oci_statement_set_prefetch(php_oci_statement *statement, ub4 prefetch )
  161. {
  162. sword errstatus;
  163. if (prefetch > 20000) {
  164. prefetch = 20000; /* keep it somewhat sane */
  165. }
  166. PHP_OCI_CALL_RETURN(errstatus, OCIAttrSet, (statement->stmt, OCI_HTYPE_STMT, &prefetch, 0, OCI_ATTR_PREFETCH_ROWS, statement->err));
  167. if (errstatus != OCI_SUCCESS) {
  168. statement->errcode = php_oci_error(statement->err, errstatus);
  169. PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
  170. statement->prefetch_count = 0;
  171. return 1;
  172. }
  173. statement->prefetch_count = prefetch;
  174. statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  175. return 0;
  176. }
  177. /* }}} */
  178. /* {{{ php_oci_cleanup_pre_fetch()
  179. Helper function to cleanup ref-cursors and descriptors from the previous row */
  180. int php_oci_cleanup_pre_fetch(zval *data)
  181. {
  182. php_oci_out_column *outcol = (php_oci_out_column*) Z_PTR_P(data);
  183. if (!outcol->is_descr && !outcol->is_cursor)
  184. return ZEND_HASH_APPLY_KEEP;
  185. switch(outcol->data_type) {
  186. case SQLT_CLOB:
  187. case SQLT_BLOB:
  188. case SQLT_RDD:
  189. case SQLT_BFILE:
  190. if (outcol->descid) {
  191. zend_list_delete(outcol->descid);
  192. outcol->descid = 0;
  193. }
  194. break;
  195. case SQLT_RSET:
  196. if (outcol->stmtid) {
  197. zend_list_delete(outcol->stmtid);
  198. outcol->stmtid = 0;
  199. outcol->nested_statement = NULL;
  200. }
  201. break;
  202. default:
  203. break;
  204. }
  205. return ZEND_HASH_APPLY_KEEP;
  206. }
  207. /* }}} */
  208. /* {{{ php_oci_statement_fetch()
  209. Fetch a row from the statement */
  210. int php_oci_statement_fetch(php_oci_statement *statement, ub4 nrows)
  211. {
  212. int i;
  213. void *handlepp;
  214. ub4 typep, iterp, idxp;
  215. ub1 in_outp, piecep;
  216. bool piecewisecols = 0;
  217. php_oci_out_column *column;
  218. sword errstatus;
  219. statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  220. if (statement->has_descr && statement->columns) {
  221. zend_hash_apply(statement->columns, php_oci_cleanup_pre_fetch);
  222. }
  223. #if ((OCI_MAJOR_VERSION > 10) || ((OCI_MAJOR_VERSION == 10) && (OCI_MINOR_VERSION >= 2)))
  224. PHP_OCI_CALL_RETURN(errstatus, OCIStmtFetch2, (statement->stmt, statement->err, nrows, OCI_FETCH_NEXT, 0, OCI_DEFAULT));
  225. #else
  226. PHP_OCI_CALL_RETURN(errstatus, OCIStmtFetch, (statement->stmt, statement->err, nrows, OCI_FETCH_NEXT, OCI_DEFAULT));
  227. #endif
  228. if (errstatus == OCI_NO_DATA || nrows == 0) {
  229. if (statement->last_query == NULL) {
  230. /* reset define-list for refcursors */
  231. if (statement->columns) {
  232. zend_hash_destroy(statement->columns);
  233. efree(statement->columns);
  234. statement->columns = NULL;
  235. statement->ncolumns = 0;
  236. }
  237. statement->executed = 0;
  238. }
  239. statement->has_data = 0;
  240. if (nrows == 0) {
  241. /* this is exactly what we requested */
  242. return 0;
  243. }
  244. return 1;
  245. }
  246. /* reset length for all piecewise columns */
  247. for (i = 0; i < statement->ncolumns; i++) {
  248. column = php_oci_statement_get_column(statement, i + 1, NULL, 0);
  249. if (column && column->piecewise) {
  250. column->retlen4 = 0;
  251. piecewisecols = 1;
  252. }
  253. }
  254. while (errstatus == OCI_NEED_DATA) {
  255. if (piecewisecols) {
  256. PHP_OCI_CALL_RETURN(errstatus,
  257. OCIStmtGetPieceInfo,
  258. (
  259. statement->stmt,
  260. statement->err,
  261. &handlepp,
  262. &typep,
  263. &in_outp,
  264. &iterp,
  265. &idxp,
  266. &piecep
  267. )
  268. );
  269. /* scan through our columns for a piecewise column with a matching handle */
  270. for (i = 0; i < statement->ncolumns; i++) {
  271. column = php_oci_statement_get_column(statement, i + 1, NULL, 0);
  272. if (column && column->piecewise && handlepp == column->oci_define) {
  273. if (!column->data) {
  274. column->data = (text *) ecalloc(1, PHP_OCI_PIECE_SIZE + 1);
  275. } else {
  276. column->data = erealloc(column->data, column->retlen4 + PHP_OCI_PIECE_SIZE + 1);
  277. }
  278. column->cb_retlen = PHP_OCI_PIECE_SIZE;
  279. /* and instruct fetch to fetch waiting piece into our buffer */
  280. PHP_OCI_CALL(OCIStmtSetPieceInfo,
  281. (
  282. (void *) column->oci_define,
  283. OCI_HTYPE_DEFINE,
  284. statement->err,
  285. ((char*)column->data) + column->retlen4,
  286. &(column->cb_retlen),
  287. piecep,
  288. &column->indicator,
  289. &column->retcode
  290. )
  291. );
  292. }
  293. }
  294. }
  295. #if ((OCI_MAJOR_VERSION > 10) || ((OCI_MAJOR_VERSION == 10) && (OCI_MINOR_VERSION >= 2)))
  296. PHP_OCI_CALL_RETURN(errstatus, OCIStmtFetch2, (statement->stmt, statement->err, nrows, OCI_FETCH_NEXT, 0, OCI_DEFAULT));
  297. #else
  298. PHP_OCI_CALL_RETURN(errstatus, OCIStmtFetch, (statement->stmt, statement->err, nrows, OCI_FETCH_NEXT, OCI_DEFAULT));
  299. #endif
  300. if (piecewisecols) {
  301. for (i = 0; i < statement->ncolumns; i++) {
  302. column = php_oci_statement_get_column(statement, i + 1, NULL, 0);
  303. if (column && column->piecewise && handlepp == column->oci_define) {
  304. column->retlen4 += column->cb_retlen;
  305. }
  306. }
  307. }
  308. }
  309. if (errstatus == OCI_SUCCESS_WITH_INFO || errstatus == OCI_SUCCESS) {
  310. statement->has_data = 1;
  311. /* do the stuff needed for OCIDefineByName */
  312. for (i = 0; i < statement->ncolumns; i++) {
  313. column = php_oci_statement_get_column(statement, i + 1, NULL, 0);
  314. if (column == NULL) {
  315. continue;
  316. }
  317. if (!column->define) {
  318. continue;
  319. }
  320. ZEND_ASSERT(Z_ISREF(column->define->val));
  321. zval_ptr_dtor(Z_REFVAL(column->define->val));
  322. ZVAL_NULL(Z_REFVAL(column->define->val));
  323. php_oci_column_to_zval(column, Z_REFVAL(column->define->val), 0);
  324. }
  325. return 0;
  326. }
  327. statement->errcode = php_oci_error(statement->err, errstatus);
  328. PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
  329. statement->has_data = 0;
  330. return 1;
  331. }
  332. /* }}} */
  333. /* {{{ php_oci_statement_get_column()
  334. Get column from the result set */
  335. php_oci_out_column *php_oci_statement_get_column(php_oci_statement *statement, zend_long column_index, char *column_name, int column_name_len)
  336. {
  337. php_oci_out_column *column = NULL;
  338. int i;
  339. if (statement->columns == NULL) { /* we release the columns at the end of a fetch */
  340. return NULL;
  341. }
  342. if (column_name) {
  343. for (i = 0; i < statement->ncolumns; i++) {
  344. column = php_oci_statement_get_column(statement, i + 1, NULL, 0);
  345. if (column == NULL) {
  346. continue;
  347. } else if (((int) column->name_len == column_name_len) && (!strncmp(column->name, column_name, column_name_len))) {
  348. return column;
  349. }
  350. }
  351. } else if (column_index != -1) {
  352. if ((column = zend_hash_index_find_ptr(statement->columns, column_index)) == NULL) {
  353. return NULL;
  354. }
  355. return column;
  356. }
  357. return NULL;
  358. }
  359. /* }}} */
  360. /* {{{ php_oci_define_callback() */
  361. sb4 php_oci_define_callback(dvoid *ctx, OCIDefine *define, ub4 iter, dvoid **bufpp, ub4 **alenpp, ub1 *piecep, dvoid **indpp, ub2 **rcpp)
  362. {
  363. php_oci_out_column *outcol = (php_oci_out_column *)ctx;
  364. if (!outcol) {
  365. php_error_docref(NULL, E_WARNING, "Invalid context pointer value");
  366. return OCI_ERROR;
  367. }
  368. switch(outcol->data_type) {
  369. case SQLT_RSET: {
  370. php_oci_statement *nested_stmt;
  371. nested_stmt = php_oci_statement_create(outcol->statement->connection, NULL, 0);
  372. if (!nested_stmt) {
  373. return OCI_ERROR;
  374. }
  375. nested_stmt->parent_stmtid = outcol->statement->id;
  376. GC_ADDREF(outcol->statement->id);
  377. outcol->nested_statement = nested_stmt;
  378. outcol->stmtid = nested_stmt->id;
  379. *bufpp = nested_stmt->stmt;
  380. *alenpp = &(outcol->retlen4);
  381. *piecep = OCI_ONE_PIECE;
  382. *indpp = &(outcol->indicator);
  383. *rcpp = &(outcol->retcode);
  384. return OCI_CONTINUE;
  385. }
  386. break;
  387. case SQLT_RDD:
  388. case SQLT_BLOB:
  389. case SQLT_CLOB:
  390. case SQLT_BFILE: {
  391. php_oci_descriptor *descr;
  392. int dtype;
  393. if (outcol->data_type == SQLT_BFILE) {
  394. dtype = OCI_DTYPE_FILE;
  395. } else if (outcol->data_type == SQLT_RDD ) {
  396. dtype = OCI_DTYPE_ROWID;
  397. } else {
  398. dtype = OCI_DTYPE_LOB;
  399. }
  400. descr = php_oci_lob_create(outcol->statement->connection, dtype);
  401. if (!descr) {
  402. return OCI_ERROR;
  403. }
  404. outcol->descid = descr->id;
  405. descr->charset_form = outcol->charset_form;
  406. *bufpp = descr->descriptor;
  407. *alenpp = &(outcol->retlen4);
  408. *piecep = OCI_ONE_PIECE;
  409. *indpp = &(outcol->indicator);
  410. *rcpp = &(outcol->retcode);
  411. return OCI_CONTINUE;
  412. }
  413. break;
  414. }
  415. return OCI_ERROR;
  416. }
  417. /* }}} */
  418. /* {{{ php_oci_statement_execute()
  419. Execute statement */
  420. int php_oci_statement_execute(php_oci_statement *statement, ub4 mode)
  421. {
  422. php_oci_out_column *outcol;
  423. OCIParam *param = NULL;
  424. text *colname;
  425. ub4 counter;
  426. ub2 define_type;
  427. ub4 iters;
  428. ub4 colcount;
  429. ub2 dynamic;
  430. dvoid *buf;
  431. sword errstatus;
  432. switch (mode) {
  433. case OCI_COMMIT_ON_SUCCESS:
  434. case OCI_DESCRIBE_ONLY:
  435. case OCI_DEFAULT:
  436. /* only these are allowed */
  437. #ifdef HAVE_OCI8_DTRACE
  438. if (DTRACE_OCI8_EXECUTE_MODE_ENABLED()) {
  439. DTRACE_OCI8_EXECUTE_MODE(statement->connection, statement->connection->client_id, statement, mode);
  440. }
  441. #endif /* HAVE_OCI8_DTRACE */
  442. break;
  443. default:
  444. php_error_docref(NULL, E_WARNING, "Invalid execute mode given: %d", mode);
  445. return 1;
  446. break;
  447. }
  448. if (!statement->stmttype) {
  449. /* get statement type */
  450. PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)statement->stmt, OCI_HTYPE_STMT, (ub2 *)&statement->stmttype, (ub4 *)0, OCI_ATTR_STMT_TYPE, statement->err));
  451. if (errstatus != OCI_SUCCESS) {
  452. statement->errcode = php_oci_error(statement->err, errstatus);
  453. PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
  454. return 1;
  455. } else {
  456. statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  457. }
  458. }
  459. if (statement->stmttype == OCI_STMT_SELECT) {
  460. iters = 0;
  461. } else {
  462. iters = 1;
  463. }
  464. if (statement->last_query) { /* Don't execute REFCURSORS or Implicit Result Set handles */
  465. if (statement->binds) {
  466. int result = 0;
  467. zend_hash_apply_with_argument(statement->binds, php_oci_bind_pre_exec, (void *)&result);
  468. if (result) {
  469. return 1;
  470. }
  471. }
  472. /* execute statement */
  473. PHP_OCI_CALL_RETURN(errstatus, OCIStmtExecute, (statement->connection->svc, statement->stmt, statement->err, iters, 0, NULL, NULL, mode));
  474. if (errstatus != OCI_SUCCESS) {
  475. statement->errcode = php_oci_error(statement->err, errstatus);
  476. PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
  477. return 1;
  478. }
  479. if (statement->binds) {
  480. zend_hash_apply(statement->binds, php_oci_bind_post_exec);
  481. }
  482. if (mode & OCI_COMMIT_ON_SUCCESS) {
  483. /* No need to rollback on disconnect */
  484. statement->connection->rb_on_disconnect = 0;
  485. } else if (statement->stmttype != OCI_STMT_SELECT) {
  486. /* Assume some uncommitted DML occurred */
  487. statement->connection->rb_on_disconnect = 1;
  488. }
  489. /* else for SELECT with OCI_NO_AUTO_COMMIT, leave
  490. * "rb_on_disconnect" at its previous value. SELECT can't
  491. * initiate uncommitted DML. (An AUTONOMOUS_TRANSACTION in
  492. * invoked PL/SQL must explicitly rollback/commit else the
  493. * SELECT fails).
  494. */
  495. statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  496. }
  497. if (statement->stmttype == OCI_STMT_SELECT && statement->executed == 0) {
  498. /* we only need to do the define step is this very statement is executed the first time! */
  499. statement->executed = 1;
  500. ALLOC_HASHTABLE(statement->columns);
  501. zend_hash_init(statement->columns, 13, NULL, php_oci_column_hash_dtor, 0);
  502. counter = 1;
  503. /* get number of columns */
  504. PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)statement->stmt, OCI_HTYPE_STMT, (dvoid *)&colcount, (ub4 *)0, OCI_ATTR_PARAM_COUNT, statement->err));
  505. if (errstatus != OCI_SUCCESS) {
  506. statement->errcode = php_oci_error(statement->err, errstatus);
  507. PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
  508. return 1;
  509. }
  510. statement->ncolumns = colcount;
  511. for (counter = 1; counter <= colcount; counter++) {
  512. outcol = (php_oci_out_column *) ecalloc(1, sizeof(php_oci_out_column));
  513. outcol = zend_hash_index_update_ptr(statement->columns, counter, outcol);
  514. /* get column */
  515. PHP_OCI_CALL_RETURN(errstatus, OCIParamGet, ((dvoid *)statement->stmt, OCI_HTYPE_STMT, statement->err, (dvoid**)&param, counter));
  516. if (errstatus != OCI_SUCCESS) {
  517. statement->errcode = php_oci_error(statement->err, errstatus);
  518. PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
  519. return 1;
  520. }
  521. /* get column datatype */
  522. PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)param, OCI_DTYPE_PARAM, (dvoid *)&outcol->data_type, (ub4 *)0, OCI_ATTR_DATA_TYPE, statement->err));
  523. if (errstatus != OCI_SUCCESS) {
  524. PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
  525. statement->errcode = php_oci_error(statement->err, errstatus);
  526. PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
  527. return 1;
  528. }
  529. /* get character set form */
  530. PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)param, OCI_DTYPE_PARAM, (dvoid *)&outcol->charset_form, (ub4 *)0, OCI_ATTR_CHARSET_FORM, statement->err));
  531. if (errstatus != OCI_SUCCESS) {
  532. PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
  533. statement->errcode = php_oci_error(statement->err, errstatus);
  534. PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
  535. return 1;
  536. }
  537. /* get character set id */
  538. PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)param, OCI_DTYPE_PARAM, (dvoid *)&outcol->charset_id, (ub4 *)0, OCI_ATTR_CHARSET_ID, statement->err));
  539. if (errstatus != OCI_SUCCESS) {
  540. PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
  541. statement->errcode = php_oci_error(statement->err, errstatus);
  542. PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
  543. return 1;
  544. }
  545. /* get size of the column */
  546. PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)param, OCI_DTYPE_PARAM, (dvoid *)&outcol->data_size, (dvoid *)0, OCI_ATTR_DATA_SIZE, statement->err));
  547. if (errstatus != OCI_SUCCESS) {
  548. PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
  549. statement->errcode = php_oci_error(statement->err, errstatus);
  550. PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
  551. return 1;
  552. }
  553. outcol->storage_size4 = outcol->data_size;
  554. outcol->retlen = outcol->data_size;
  555. /* get scale of the column */
  556. PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)param, OCI_DTYPE_PARAM, (dvoid *)&outcol->scale, (dvoid *)0, OCI_ATTR_SCALE, statement->err));
  557. if (errstatus != OCI_SUCCESS) {
  558. PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
  559. statement->errcode = php_oci_error(statement->err, errstatus);
  560. PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
  561. return 1;
  562. }
  563. /* get precision of the column */
  564. PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)param, OCI_DTYPE_PARAM, (dvoid *)&outcol->precision, (dvoid *)0, OCI_ATTR_PRECISION, statement->err));
  565. if (errstatus != OCI_SUCCESS) {
  566. PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
  567. statement->errcode = php_oci_error(statement->err, errstatus);
  568. PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
  569. return 1;
  570. }
  571. /* get name of the column */
  572. PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)param, OCI_DTYPE_PARAM, (dvoid **)&colname, (ub4 *)&outcol->name_len, (ub4)OCI_ATTR_NAME, statement->err));
  573. if (errstatus != OCI_SUCCESS) {
  574. PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
  575. statement->errcode = php_oci_error(statement->err, errstatus);
  576. PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
  577. return 1;
  578. }
  579. PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
  580. outcol->name = ecalloc(1, outcol->name_len + 1);
  581. memcpy(outcol->name, colname, outcol->name_len);
  582. /* find a user-set define */
  583. if (statement->defines) {
  584. if ((outcol->define = zend_hash_str_find_ptr(statement->defines, outcol->name, outcol->name_len)) != NULL) {
  585. if (outcol->define->type) {
  586. outcol->data_type = outcol->define->type;
  587. }
  588. }
  589. }
  590. buf = 0;
  591. switch (outcol->data_type) {
  592. case SQLT_RSET:
  593. outcol->statement = statement; /* parent handle */
  594. define_type = SQLT_RSET;
  595. outcol->is_cursor = 1;
  596. outcol->statement->has_descr = 1;
  597. outcol->storage_size4 = -1;
  598. outcol->retlen = -1;
  599. dynamic = OCI_DYNAMIC_FETCH;
  600. break;
  601. case SQLT_RDD: /* ROWID */
  602. case SQLT_BLOB: /* binary LOB */
  603. case SQLT_CLOB: /* character LOB */
  604. case SQLT_BFILE: /* binary file LOB */
  605. outcol->statement = statement; /* parent handle */
  606. define_type = outcol->data_type;
  607. outcol->is_descr = 1;
  608. outcol->statement->has_descr = 1;
  609. outcol->storage_size4 = -1;
  610. outcol->chunk_size = 0;
  611. dynamic = OCI_DYNAMIC_FETCH;
  612. break;
  613. case SQLT_LNG:
  614. case SQLT_LBI:
  615. if (outcol->data_type == SQLT_LBI) {
  616. define_type = SQLT_BIN;
  617. } else {
  618. define_type = SQLT_CHR;
  619. }
  620. outcol->storage_size4 = PHP_OCI_MAX_DATA_SIZE;
  621. outcol->piecewise = 1;
  622. dynamic = OCI_DYNAMIC_FETCH;
  623. break;
  624. case SQLT_BIN:
  625. default:
  626. define_type = SQLT_CHR;
  627. if (outcol->data_type == SQLT_BIN) {
  628. define_type = SQLT_BIN;
  629. }
  630. if ((outcol->data_type == SQLT_DAT) || (outcol->data_type == SQLT_NUM)
  631. #ifdef SQLT_TIMESTAMP
  632. || (outcol->data_type == SQLT_TIMESTAMP)
  633. #endif
  634. #ifdef SQLT_TIMESTAMP_TZ
  635. || (outcol->data_type == SQLT_TIMESTAMP_TZ)
  636. #endif
  637. #ifdef SQLT_TIMESTAMP_LTZ
  638. || (outcol->data_type == SQLT_TIMESTAMP_LTZ)
  639. #endif
  640. #ifdef SQLT_INTERVAL_YM
  641. || (outcol->data_type == SQLT_INTERVAL_YM)
  642. #endif
  643. #ifdef SQLT_INTERVAL_DS
  644. || (outcol->data_type == SQLT_INTERVAL_DS)
  645. #endif
  646. ) {
  647. outcol->storage_size4 = 512; /* XXX this should fit "most" NLS date-formats and Numbers */
  648. #if defined(SQLT_IBFLOAT) && defined(SQLT_IBDOUBLE)
  649. } else if (outcol->data_type == SQLT_IBFLOAT || outcol->data_type == SQLT_IBDOUBLE) {
  650. outcol->storage_size4 = 1024;
  651. #endif
  652. } else {
  653. outcol->storage_size4++; /* add one for string terminator */
  654. }
  655. outcol->storage_size4 *= 3;
  656. dynamic = OCI_DEFAULT;
  657. buf = outcol->data = (text *) safe_emalloc(1, outcol->storage_size4, 0);
  658. memset(buf, 0, outcol->storage_size4);
  659. break;
  660. }
  661. if (dynamic == OCI_DYNAMIC_FETCH) {
  662. PHP_OCI_CALL_RETURN(errstatus,
  663. OCIDefineByPos,
  664. (
  665. statement->stmt, /* IN/OUT handle to the requested SQL query */
  666. (OCIDefine **)&outcol->oci_define, /* IN/OUT pointer to a pointer to a define handle */
  667. statement->err, /* IN/OUT An error handle */
  668. counter, /* IN position in the select list */
  669. (dvoid *)NULL, /* IN/OUT pointer to a buffer */
  670. outcol->storage_size4, /* IN The size of each valuep buffer in bytes */
  671. define_type, /* IN The data type */
  672. (dvoid *)&outcol->indicator, /* IN pointer to an indicator variable or arr */
  673. (ub2 *)NULL, /* IN/OUT Pointer to array of length of data fetched */
  674. (ub2 *)NULL, /* OUT Pointer to array of column-level return codes */
  675. OCI_DYNAMIC_FETCH /* IN mode (OCI_DEFAULT, OCI_DYNAMIC_FETCH) */
  676. )
  677. );
  678. } else {
  679. PHP_OCI_CALL_RETURN(errstatus,
  680. OCIDefineByPos,
  681. (
  682. statement->stmt, /* IN/OUT handle to the requested SQL query */
  683. (OCIDefine **)&outcol->oci_define, /* IN/OUT pointer to a pointer to a define handle */
  684. statement->err, /* IN/OUT An error handle */
  685. counter, /* IN position in the select list */
  686. (dvoid *)buf, /* IN/OUT pointer to a buffer */
  687. outcol->storage_size4, /* IN The size of each valuep buffer in bytes */
  688. define_type, /* IN The data type */
  689. (dvoid *)&outcol->indicator, /* IN pointer to an indicator variable or arr */
  690. (ub2 *)&outcol->retlen, /* IN/OUT Pointer to array of length of data fetched */
  691. (ub2 *)&outcol->retcode, /* OUT Pointer to array of column-level return codes */
  692. OCI_DEFAULT /* IN mode (OCI_DEFAULT, OCI_DYNAMIC_FETCH) */
  693. )
  694. );
  695. }
  696. if (errstatus != OCI_SUCCESS) {
  697. statement->errcode = php_oci_error(statement->err, errstatus);
  698. PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
  699. return 1;
  700. }
  701. /* additional OCIDefineDynamic() call */
  702. switch (outcol->data_type) {
  703. case SQLT_RSET:
  704. case SQLT_RDD:
  705. case SQLT_BLOB:
  706. case SQLT_CLOB:
  707. case SQLT_BFILE:
  708. PHP_OCI_CALL_RETURN(errstatus,
  709. OCIDefineDynamic,
  710. (
  711. outcol->oci_define,
  712. statement->err,
  713. (dvoid *)outcol,
  714. php_oci_define_callback
  715. )
  716. );
  717. if (errstatus != OCI_SUCCESS) {
  718. statement->errcode = php_oci_error(statement->err, errstatus);
  719. PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
  720. return 1;
  721. }
  722. break;
  723. }
  724. }
  725. statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  726. }
  727. return 0;
  728. }
  729. /* }}} */
  730. /* {{{ php_oci_statement_cancel()
  731. Cancel statement */
  732. int php_oci_statement_cancel(php_oci_statement *statement)
  733. {
  734. return php_oci_statement_fetch(statement, 0);
  735. }
  736. /* }}} */
  737. /* {{{ php_oci_statement_free()
  738. Destroy statement handle and free associated resources */
  739. void php_oci_statement_free(php_oci_statement *statement)
  740. {
  741. if (statement->stmt) {
  742. if (statement->last_query_len) { /* FIXME: magical */
  743. PHP_OCI_CALL(OCIStmtRelease, (statement->stmt, statement->err, NULL, 0, statement->errcode ? OCI_STRLS_CACHE_DELETE : OCI_DEFAULT));
  744. } else if (statement->impres_flag != PHP_OCI_IMPRES_IS_CHILD) { /* Oracle doc says don't free Implicit Result Set handles */
  745. PHP_OCI_CALL(OCIHandleFree, (statement->stmt, OCI_HTYPE_STMT));
  746. }
  747. statement->stmt = NULL;
  748. }
  749. if (statement->err) {
  750. PHP_OCI_CALL(OCIHandleFree, (statement->err, OCI_HTYPE_ERROR));
  751. statement->err = NULL;
  752. }
  753. if (statement->last_query) {
  754. efree(statement->last_query);
  755. }
  756. if (statement->binds) {
  757. zend_hash_destroy(statement->binds);
  758. efree(statement->binds);
  759. }
  760. if (statement->defines) {
  761. zend_hash_destroy(statement->defines);
  762. efree(statement->defines);
  763. }
  764. if (statement->columns) {
  765. zend_hash_destroy(statement->columns);
  766. efree(statement->columns);
  767. }
  768. if (statement->parent_stmtid) {
  769. zend_list_delete(statement->parent_stmtid);
  770. }
  771. zend_list_delete(statement->connection->id);
  772. efree(statement);
  773. OCI_G(num_statements)--;
  774. }
  775. /* }}} */
  776. /* {{{ php_oci_bind_pre_exec()
  777. Helper function */
  778. int php_oci_bind_pre_exec(zval *data, void *result)
  779. {
  780. php_oci_bind *bind = (php_oci_bind *) Z_PTR_P(data);
  781. zval *zv = &bind->val;
  782. *(int *)result = 0;
  783. ZVAL_DEREF(zv);
  784. if (Z_TYPE_P(zv) == IS_ARRAY) {
  785. /* These checks are currently valid for oci_bind_by_name, not
  786. * oci_bind_array_by_name. Also bind->type and
  787. * bind->indicator are not used for oci_bind_array_by_name.
  788. */
  789. return 0;
  790. }
  791. switch (bind->type) {
  792. case SQLT_NTY:
  793. case SQLT_BFILEE:
  794. case SQLT_CFILEE:
  795. case SQLT_CLOB:
  796. case SQLT_BLOB:
  797. case SQLT_RDD:
  798. if (Z_TYPE_P(zv) != IS_OBJECT) {
  799. php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
  800. *(int *)result = 1;
  801. }
  802. break;
  803. case SQLT_CHR:
  804. case SQLT_AFC:
  805. case SQLT_INT:
  806. case SQLT_NUM:
  807. #if defined(OCI_MAJOR_VERSION) && OCI_MAJOR_VERSION >= 12
  808. case SQLT_BOL:
  809. #endif
  810. case SQLT_LBI:
  811. case SQLT_BIN:
  812. case SQLT_LNG:
  813. if (Z_TYPE_P(zv) == IS_RESOURCE || Z_TYPE_P(zv) == IS_OBJECT) {
  814. php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
  815. *(int *)result = 1;
  816. }
  817. break;
  818. case SQLT_RSET:
  819. if (Z_TYPE_P(zv) != IS_RESOURCE) {
  820. php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
  821. *(int *)result = 1;
  822. }
  823. break;
  824. }
  825. /* reset all bind stuff to a normal state... */
  826. bind->indicator = 0;
  827. return 0;
  828. }
  829. /* }}} */
  830. /* {{{ php_oci_bind_post_exec()
  831. Helper function */
  832. int php_oci_bind_post_exec(zval *data)
  833. {
  834. php_oci_bind *bind = (php_oci_bind *) Z_PTR_P(data);
  835. php_oci_connection *connection = bind->parent_statement->connection;
  836. sword errstatus;
  837. zval *zv = &bind->val;
  838. ZVAL_DEREF(zv);
  839. if (bind->indicator == -1) { /* NULL */
  840. if (Z_TYPE_P(zv) == IS_STRING) {
  841. *Z_STRVAL_P(zv) = '\0'; /* XXX avoid warning in debug mode */
  842. }
  843. zval_ptr_dtor(zv);
  844. ZVAL_NULL(zv);
  845. } else if (Z_TYPE_P(zv) == IS_STRING
  846. && Z_STRLEN_P(zv) > 0
  847. && Z_STRVAL_P(zv)[ Z_STRLEN_P(zv) ] != '\0') {
  848. /* The post- PHP 5.3 feature for "interned" strings disallows
  849. * their reallocation but (i) any IN binds either interned or
  850. * not should already be null terminated and (ii) for OUT
  851. * binds, php_oci_bind_out_callback() should have allocated a
  852. * new string that we can modify here.
  853. */
  854. ZVAL_NEW_STR(zv, zend_string_extend(Z_STR_P(zv), Z_STRLEN_P(zv)+1, 0));
  855. Z_STRVAL_P(zv)[ Z_STRLEN_P(zv) ] = '\0';
  856. } else if (Z_TYPE_P(zv) == IS_ARRAY) {
  857. int i;
  858. zval *entry = NULL;
  859. HashTable *hash;
  860. SEPARATE_ARRAY(zv);
  861. hash = Z_ARRVAL_P(zv);
  862. zend_hash_internal_pointer_reset(hash);
  863. switch (bind->array.type) {
  864. case SQLT_NUM:
  865. case SQLT_INT:
  866. case SQLT_LNG:
  867. for (i = 0; i < (int) bind->array.current_length; i++) {
  868. if ((i < (int) bind->array.old_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
  869. zval_ptr_dtor(entry);
  870. ZVAL_LONG(entry, ((oci_phpsized_int *)(bind->array.elements))[i]);
  871. zend_hash_move_forward(hash);
  872. } else {
  873. add_next_index_long(zv, ((oci_phpsized_int *)(bind->array.elements))[i]);
  874. }
  875. }
  876. break;
  877. case SQLT_FLT:
  878. for (i = 0; i < (int) bind->array.current_length; i++) {
  879. if ((i < (int) bind->array.old_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
  880. zval_ptr_dtor(entry);
  881. ZVAL_DOUBLE(entry, ((double *)(bind->array.elements))[i]);
  882. zend_hash_move_forward(hash);
  883. } else {
  884. add_next_index_double(zv, ((double *)(bind->array.elements))[i]);
  885. }
  886. }
  887. break;
  888. case SQLT_ODT:
  889. for (i = 0; i < (int) bind->array.current_length; i++) {
  890. oratext buff[1024];
  891. ub4 buff_len = 1024;
  892. memset((void*)buff,0,sizeof(buff));
  893. if ((i < (int) bind->array.old_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
  894. PHP_OCI_CALL_RETURN(errstatus, OCIDateToText, (connection->err, &(((OCIDate *)(bind->array.elements))[i]), 0, 0, 0, 0, &buff_len, buff));
  895. zval_ptr_dtor(entry);
  896. if (errstatus != OCI_SUCCESS) {
  897. connection->errcode = php_oci_error(connection->err, errstatus);
  898. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  899. ZVAL_NULL(entry);
  900. } else {
  901. connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  902. ZVAL_STRINGL(entry, (char *)buff, buff_len);
  903. }
  904. zend_hash_move_forward(hash);
  905. } else {
  906. PHP_OCI_CALL_RETURN(errstatus, OCIDateToText, (connection->err, &(((OCIDate *)(bind->array.elements))[i]), 0, 0, 0, 0, &buff_len, buff));
  907. if (errstatus != OCI_SUCCESS) {
  908. connection->errcode = php_oci_error(connection->err, errstatus);
  909. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  910. add_next_index_null(zv);
  911. } else {
  912. connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  913. add_next_index_stringl(zv, (char *)buff, buff_len);
  914. }
  915. }
  916. }
  917. break;
  918. case SQLT_AFC:
  919. case SQLT_CHR:
  920. case SQLT_VCS:
  921. case SQLT_AVC:
  922. case SQLT_STR:
  923. case SQLT_LVC:
  924. for (i = 0; i < (int) bind->array.current_length; i++) {
  925. /* int curr_element_length = strlen(((text *)bind->array.elements)+i*bind->array.max_length); */
  926. int curr_element_length = bind->array.element_lengths[i];
  927. if ((i < (int) bind->array.old_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
  928. zval_ptr_dtor(entry);
  929. ZVAL_STRINGL(entry, (char *)(((text *)bind->array.elements)+i*bind->array.max_length), curr_element_length);
  930. zend_hash_move_forward(hash);
  931. } else {
  932. add_next_index_stringl(zv, (char *)(((text *)bind->array.elements)+i*bind->array.max_length), curr_element_length);
  933. }
  934. }
  935. break;
  936. }
  937. } else if ((Z_TYPE_P(zv) == IS_TRUE) || (Z_TYPE_P(zv) == IS_FALSE)) {
  938. /* This convetrsion is done on purpose (ext/oci8 uses LVAL as a temporary value) */
  939. if (Z_LVAL_P(zv) == 0)
  940. ZVAL_BOOL(zv, FALSE);
  941. else if (Z_LVAL_P(zv) == 1)
  942. ZVAL_BOOL(zv, TRUE);
  943. }
  944. return 0;
  945. }
  946. /* }}} */
  947. /* {{{ php_oci_bind_by_name()
  948. Bind zval to the given placeholder */
  949. int php_oci_bind_by_name(php_oci_statement *statement, char *name, size_t name_len, zval *var, zend_long maxlength, ub2 type)
  950. {
  951. php_oci_collection *bind_collection = NULL;
  952. php_oci_descriptor *bind_descriptor = NULL;
  953. php_oci_statement *bind_statement = NULL;
  954. dvoid *oci_desc = NULL;
  955. /* dvoid *php_oci_collection = NULL; */
  956. OCIStmt *oci_stmt = NULL;
  957. dvoid *bind_data = NULL;
  958. php_oci_bind *old_bind, *bindp;
  959. int mode = OCI_DATA_AT_EXEC;
  960. sb4 value_sz = -1;
  961. sword errstatus;
  962. zval *param = NULL;
  963. ZEND_ASSERT(Z_ISREF_P(var));
  964. param = Z_REFVAL_P(var);
  965. switch (type) {
  966. case SQLT_NTY:
  967. {
  968. zval *tmp;
  969. if (Z_TYPE_P(param) != IS_OBJECT || (tmp = zend_hash_str_find(Z_OBJPROP_P(param), "collection", sizeof("collection")-1)) == NULL) {
  970. php_error_docref(NULL, E_WARNING, "Unable to find collection property");
  971. return 1;
  972. }
  973. PHP_OCI_ZVAL_TO_COLLECTION_EX(tmp, bind_collection);
  974. value_sz = sizeof(void*);
  975. mode = OCI_DEFAULT;
  976. if (!bind_collection->collection) {
  977. return 1;
  978. }
  979. }
  980. break;
  981. case SQLT_BFILEE:
  982. case SQLT_CFILEE:
  983. case SQLT_CLOB:
  984. case SQLT_BLOB:
  985. case SQLT_RDD:
  986. {
  987. zval *tmp;
  988. if (Z_TYPE_P(param) != IS_OBJECT || (tmp = zend_hash_str_find(Z_OBJPROP_P(param), "descriptor", sizeof("descriptor")-1)) == NULL) {
  989. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
  990. return 1;
  991. }
  992. PHP_OCI_ZVAL_TO_DESCRIPTOR_EX(tmp, bind_descriptor);
  993. value_sz = sizeof(void*);
  994. oci_desc = bind_descriptor->descriptor;
  995. if (!oci_desc) {
  996. return 1;
  997. }
  998. }
  999. break;
  1000. case SQLT_INT:
  1001. case SQLT_NUM:
  1002. if (Z_TYPE_P(param) == IS_RESOURCE || Z_TYPE_P(param) == IS_OBJECT) {
  1003. php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
  1004. return 1;
  1005. }
  1006. convert_to_long(param);
  1007. bind_data = (oci_phpsized_int *)&Z_LVAL_P(param);
  1008. value_sz = sizeof(oci_phpsized_int);
  1009. mode = OCI_DEFAULT;
  1010. break;
  1011. case SQLT_LBI:
  1012. case SQLT_BIN:
  1013. case SQLT_LNG:
  1014. case SQLT_AFC:
  1015. case SQLT_CHR: /* SQLT_CHR is the default value when type was not specified */
  1016. if (Z_TYPE_P(param) == IS_RESOURCE || Z_TYPE_P(param) == IS_OBJECT) {
  1017. php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
  1018. return 1;
  1019. }
  1020. if (Z_TYPE_P(param) != IS_NULL) {
  1021. if (!try_convert_to_string(param)) {
  1022. return 1;
  1023. }
  1024. }
  1025. if ((maxlength == -1) || (maxlength == 0)) {
  1026. if (type == SQLT_LNG) {
  1027. value_sz = SB4MAXVAL;
  1028. } else if (Z_TYPE_P(param) == IS_STRING) {
  1029. value_sz = (sb4) Z_STRLEN_P(param);
  1030. } else {
  1031. /* Bug-72524: revert value_sz from PHP_OCI_PIECE_SIZE to 0. This restores PHP 5.6 behavior */
  1032. value_sz = 0;
  1033. }
  1034. } else {
  1035. value_sz = (sb4) maxlength;
  1036. }
  1037. break;
  1038. case SQLT_RSET:
  1039. if (Z_TYPE_P(param) != IS_RESOURCE) {
  1040. php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
  1041. return 1;
  1042. }
  1043. PHP_OCI_ZVAL_TO_STATEMENT_EX(param, bind_statement);
  1044. value_sz = sizeof(void*);
  1045. oci_stmt = bind_statement->stmt;
  1046. if (!oci_stmt) {
  1047. return 1;
  1048. }
  1049. break;
  1050. #if defined(OCI_MAJOR_VERSION) && OCI_MAJOR_VERSION >= 12
  1051. case SQLT_BOL:
  1052. if (Z_TYPE_P(param) == IS_RESOURCE || Z_TYPE_P(param) == IS_OBJECT) {
  1053. php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
  1054. return 1;
  1055. }
  1056. convert_to_boolean(param);
  1057. bind_data = (zend_long *)&Z_LVAL_P(param);
  1058. if (Z_TYPE_P(param) == IS_TRUE)
  1059. *(zend_long *)bind_data = 1;
  1060. else if (Z_TYPE_P(param) == IS_FALSE)
  1061. *(zend_long *)bind_data = 0;
  1062. else {
  1063. php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
  1064. return 1;
  1065. }
  1066. value_sz = sizeof(zend_long);
  1067. mode = OCI_DEFAULT;
  1068. break;
  1069. #endif
  1070. default:
  1071. php_error_docref(NULL, E_WARNING, "Unknown or unsupported datatype given: %d", (int)type);
  1072. return 1;
  1073. break;
  1074. }
  1075. if (!statement->binds) {
  1076. ALLOC_HASHTABLE(statement->binds);
  1077. zend_hash_init(statement->binds, 13, NULL, php_oci_bind_hash_dtor, 0);
  1078. }
  1079. if ((old_bind = zend_hash_str_find_ptr(statement->binds, name, name_len)) != NULL) {
  1080. bindp = old_bind;
  1081. if (!Z_ISUNDEF(bindp->val)) {
  1082. zval_ptr_dtor(&bindp->val);
  1083. ZVAL_UNDEF(&bindp->val);
  1084. }
  1085. } else {
  1086. zend_string *zvtmp;
  1087. zvtmp = zend_string_init(name, name_len, 0);
  1088. bindp = (php_oci_bind *) ecalloc(1, sizeof(php_oci_bind));
  1089. bindp = zend_hash_update_ptr(statement->binds, zvtmp, bindp);
  1090. zend_string_release_ex(zvtmp, 0);
  1091. }
  1092. /* Make sure the minimum of value_sz is 1 to avoid ORA-3149
  1093. * when both in/out parameters are bound with empty strings
  1094. */
  1095. if (value_sz == 0)
  1096. value_sz = 1;
  1097. bindp->descriptor = oci_desc;
  1098. bindp->statement = oci_stmt;
  1099. bindp->parent_statement = statement;
  1100. ZVAL_COPY(&bindp->val, var);
  1101. bindp->type = type;
  1102. /* Storing max length set in OCIBindByName() to check it later in
  1103. * php_oci_bind_in_callback() function to avoid ORA-1406 error while
  1104. * executing OCIStmtExecute()
  1105. */
  1106. bindp->dummy_len = value_sz;
  1107. PHP_OCI_CALL_RETURN(errstatus,
  1108. OCIBindByName,
  1109. (
  1110. statement->stmt, /* statement handle */
  1111. (OCIBind **)&bindp->bind, /* bind hdl (will alloc) */
  1112. statement->err, /* error handle */
  1113. (text*) name, /* placeholder name */
  1114. (sb4) name_len, /* placeholder length */
  1115. (dvoid *)bind_data, /* in/out data */
  1116. value_sz, /* PHP_OCI_MAX_DATA_SIZE, */ /* max size of input/output data */
  1117. type, /* in/out data type */
  1118. (dvoid *)&bindp->indicator, /* indicator (ignored) */
  1119. (ub2 *)0, /* size array (ignored) */
  1120. (ub2 *)&bindp->retcode, /* return code (ignored) */
  1121. (ub4)0, /* maxarr_len (PL/SQL only?) */
  1122. (ub4 *)0, /* actual array size (PL/SQL only?) */
  1123. mode /* mode */
  1124. )
  1125. );
  1126. if (errstatus != OCI_SUCCESS) {
  1127. statement->errcode = php_oci_error(statement->err, errstatus);
  1128. PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
  1129. return 1;
  1130. }
  1131. if (mode == OCI_DATA_AT_EXEC) {
  1132. PHP_OCI_CALL_RETURN(errstatus, OCIBindDynamic,
  1133. (
  1134. bindp->bind,
  1135. statement->err,
  1136. (dvoid *)bindp,
  1137. php_oci_bind_in_callback,
  1138. (dvoid *)bindp,
  1139. php_oci_bind_out_callback
  1140. )
  1141. );
  1142. if (errstatus != OCI_SUCCESS) {
  1143. statement->errcode = php_oci_error(statement->err, errstatus);
  1144. PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
  1145. return 1;
  1146. }
  1147. }
  1148. if (type == SQLT_NTY) {
  1149. /* Bind object */
  1150. PHP_OCI_CALL_RETURN(errstatus, OCIBindObject,
  1151. (
  1152. bindp->bind,
  1153. statement->err,
  1154. bind_collection->tdo,
  1155. (dvoid **) &(bind_collection->collection),
  1156. (ub4 *) 0,
  1157. (dvoid **) 0,
  1158. (ub4 *) 0
  1159. )
  1160. );
  1161. if (errstatus) {
  1162. statement->errcode = php_oci_error(statement->err, errstatus);
  1163. PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
  1164. return 1;
  1165. }
  1166. }
  1167. statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  1168. return 0;
  1169. }
  1170. /* }}} */
  1171. /* {{{ php_oci_bind_in_callback()
  1172. Callback used when binding LOBs and VARCHARs */
  1173. sb4 php_oci_bind_in_callback(
  1174. dvoid *ictxp, /* context pointer */
  1175. OCIBind *bindp, /* bind handle */
  1176. ub4 iter, /* 0-based execute iteration value */
  1177. ub4 index, /* index of current array for PL/SQL or row index for SQL */
  1178. dvoid **bufpp, /* pointer to data */
  1179. ub4 *alenp, /* size after value/piece has been read */
  1180. ub1 *piecep, /* which piece */
  1181. dvoid **indpp) /* indicator value */
  1182. {
  1183. php_oci_bind *phpbind;
  1184. zval *val;
  1185. if (!(phpbind=(php_oci_bind *)ictxp) || Z_ISUNDEF(phpbind->val)) {
  1186. php_error_docref(NULL, E_WARNING, "Invalid phpbind pointer value");
  1187. return OCI_ERROR;
  1188. }
  1189. val = &phpbind->val;
  1190. ZVAL_DEREF(val);
  1191. if (Z_ISNULL_P(val)) {
  1192. /* we're going to insert a NULL column */
  1193. phpbind->indicator = -1;
  1194. *bufpp = 0;
  1195. *alenp = -1;
  1196. *indpp = (dvoid *)&phpbind->indicator;
  1197. } else if ((phpbind->descriptor == 0) && (phpbind->statement == 0)) {
  1198. /* "normal" string bind */
  1199. if (!try_convert_to_string(val)) {
  1200. return OCI_ERROR;
  1201. }
  1202. *bufpp = Z_STRVAL_P(val);
  1203. *alenp = (ub4) Z_STRLEN_P(val);
  1204. /*
  1205. * bind_char_1: If max length set in OCIBindByName is less than the
  1206. * actual length of input string, then we have to overwrite alenp with
  1207. * max value set in OCIBindByName (dummy_len). Or else it will cause
  1208. * ORA-1406 error in OCIStmtExecute
  1209. */
  1210. if ((phpbind->dummy_len > 0) && (phpbind->dummy_len < *alenp))
  1211. *alenp = phpbind->dummy_len;
  1212. *indpp = (dvoid *)&phpbind->indicator;
  1213. } else if (phpbind->statement != 0) {
  1214. /* RSET */
  1215. *bufpp = phpbind->statement;
  1216. *alenp = -1; /* seems to be alright */
  1217. *indpp = (dvoid *)&phpbind->indicator;
  1218. } else {
  1219. /* descriptor bind */
  1220. *bufpp = phpbind->descriptor;
  1221. *alenp = -1; /* seems to be alright */
  1222. *indpp = (dvoid *)&phpbind->indicator;
  1223. }
  1224. *piecep = OCI_ONE_PIECE; /* pass all data in one go */
  1225. return OCI_CONTINUE;
  1226. }
  1227. /* }}} */
  1228. /* {{{ php_oci_bind_out_callback()
  1229. Callback used when binding LOBs and VARCHARs */
  1230. sb4 php_oci_bind_out_callback(
  1231. dvoid *octxp, /* context pointer */
  1232. OCIBind *bindp, /* bind handle */
  1233. ub4 iter, /* 0-based execute iteration value */
  1234. ub4 index, /* index of current array for PL/SQL or row index for SQL */
  1235. dvoid **bufpp, /* pointer to data */
  1236. ub4 **alenpp, /* size after value/piece has been read */
  1237. ub1 *piecep, /* which piece */
  1238. dvoid **indpp, /* indicator value */
  1239. ub2 **rcodepp) /* return code */
  1240. {
  1241. php_oci_bind *phpbind;
  1242. zval *val;
  1243. sb4 retval = OCI_ERROR;
  1244. if (!(phpbind=(php_oci_bind *)octxp) || Z_ISUNDEF(phpbind->val)) {
  1245. php_error_docref(NULL, E_WARNING, "Invalid phpbind pointer value");
  1246. return retval;
  1247. }
  1248. val = &phpbind->val;
  1249. ZVAL_DEREF(val);
  1250. if (Z_TYPE_P(val) == IS_RESOURCE) {
  1251. /* Processing for ref-cursor out binds */
  1252. if (phpbind->statement != NULL) {
  1253. *bufpp = phpbind->statement;
  1254. *alenpp = &phpbind->dummy_len;
  1255. *piecep = OCI_ONE_PIECE;
  1256. *rcodepp = &phpbind->retcode;
  1257. *indpp = &phpbind->indicator;
  1258. }
  1259. retval = OCI_CONTINUE;
  1260. } else if (Z_TYPE_P(val) == IS_OBJECT) {
  1261. zval *tmp;
  1262. php_oci_descriptor *desc;
  1263. if (!phpbind->descriptor) {
  1264. return OCI_ERROR;
  1265. }
  1266. /* Do not use the cached lob size if the descriptor is an
  1267. * out-bind as the contents would have been changed for in/out
  1268. * binds (Bug #46994).
  1269. */
  1270. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(val), "descriptor", sizeof("descriptor")-1)) == NULL) {
  1271. php_error_docref(NULL, E_WARNING, "Unable to find object outbind descriptor property");
  1272. return OCI_ERROR;
  1273. }
  1274. PHP_OCI_ZVAL_TO_DESCRIPTOR_EX(tmp, desc);
  1275. desc->lob_size = -1; /* force OCI8 to update cached size */
  1276. *alenpp = &phpbind->dummy_len;
  1277. *bufpp = phpbind->descriptor;
  1278. *piecep = OCI_ONE_PIECE;
  1279. *rcodepp = &phpbind->retcode;
  1280. *indpp = &phpbind->indicator;
  1281. retval = OCI_CONTINUE;
  1282. } else {
  1283. zval_ptr_dtor(val);
  1284. {
  1285. char *p = ecalloc(1, PHP_OCI_PIECE_SIZE);
  1286. ZVAL_STRINGL(val, p, PHP_OCI_PIECE_SIZE);
  1287. efree(p);
  1288. }
  1289. #if 0
  1290. Z_STRLEN_P(val) = PHP_OCI_PIECE_SIZE; /* 64K-1 is max XXX */
  1291. Z_STRVAL_P(val) = ecalloc(1, Z_STRLEN_P(val) + 1);
  1292. /* XXX is this right? */
  1293. ZVAL_STRINGL(val, NULL, Z_STRLEN(val) + 1);
  1294. #endif
  1295. /* XXX we assume that zend-zval len has 4 bytes */
  1296. *alenpp = (ub4*) &Z_STRLEN_P(val);
  1297. *bufpp = Z_STRVAL_P(val);
  1298. *piecep = OCI_ONE_PIECE;
  1299. *rcodepp = &phpbind->retcode;
  1300. *indpp = &phpbind->indicator;
  1301. retval = OCI_CONTINUE;
  1302. }
  1303. return retval;
  1304. }
  1305. /* }}} */
  1306. /* {{{ php_oci_statement_get_column_helper()
  1307. Helper function to get column by name and index */
  1308. php_oci_out_column *php_oci_statement_get_column_helper(INTERNAL_FUNCTION_PARAMETERS, int need_data)
  1309. {
  1310. zval *z_statement;
  1311. zend_string *column_index_string;
  1312. zend_long column_index_int;
  1313. php_oci_statement *statement;
  1314. php_oci_out_column *column;
  1315. ZEND_PARSE_PARAMETERS_START(2, 2)
  1316. Z_PARAM_RESOURCE(z_statement)
  1317. Z_PARAM_STR_OR_LONG(column_index_string, column_index_int)
  1318. ZEND_PARSE_PARAMETERS_END_EX(return NULL);
  1319. statement = (php_oci_statement *) zend_fetch_resource_ex(z_statement, "oci8 statement", le_statement);
  1320. if (!statement) {
  1321. return NULL;
  1322. }
  1323. if (need_data && !statement->has_data) {
  1324. return NULL;
  1325. }
  1326. if (column_index_string != NULL) {
  1327. column = php_oci_statement_get_column(statement, -1, ZSTR_VAL(column_index_string), (int) ZSTR_LEN(column_index_string));
  1328. if (!column) {
  1329. php_error_docref(NULL, E_WARNING, "Invalid column name \"%s\"", ZSTR_VAL(column_index_string));
  1330. return NULL;
  1331. }
  1332. } else {
  1333. column = php_oci_statement_get_column(statement, column_index_int, NULL, 0);
  1334. if (!column) {
  1335. php_error_docref(NULL, E_WARNING, "Invalid column index \"" ZEND_LONG_FMT "\"", column_index_int);
  1336. return NULL;
  1337. }
  1338. }
  1339. return column;
  1340. }
  1341. /* }}} */
  1342. /* {{{ php_oci_statement_get_type()
  1343. Return type of the statement */
  1344. int php_oci_statement_get_type(php_oci_statement *statement, ub2 *type)
  1345. {
  1346. ub2 statement_type;
  1347. sword errstatus;
  1348. *type = 0;
  1349. PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)statement->stmt, OCI_HTYPE_STMT, (ub2 *)&statement_type, (ub4 *)0, OCI_ATTR_STMT_TYPE, statement->err));
  1350. if (errstatus != OCI_SUCCESS) {
  1351. statement->errcode = php_oci_error(statement->err, errstatus);
  1352. PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
  1353. return 1;
  1354. }
  1355. statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  1356. *type = statement_type;
  1357. return 0;
  1358. }
  1359. /* }}} */
  1360. /* {{{ php_oci_statement_get_numrows()
  1361. Get the number of rows fetched to the clientside (NOT the number of rows in the result set) */
  1362. int php_oci_statement_get_numrows(php_oci_statement *statement, ub4 *numrows)
  1363. {
  1364. ub4 statement_numrows;
  1365. sword errstatus;
  1366. *numrows = 0;
  1367. PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)statement->stmt, OCI_HTYPE_STMT, (ub4 *)&statement_numrows, (ub4 *)0, OCI_ATTR_ROW_COUNT, statement->err));
  1368. if (errstatus != OCI_SUCCESS) {
  1369. statement->errcode = php_oci_error(statement->err, errstatus);
  1370. PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
  1371. return 1;
  1372. }
  1373. statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  1374. *numrows = statement_numrows;
  1375. return 0;
  1376. }
  1377. /* }}} */
  1378. /* {{{ php_oci_bind_array_by_name()
  1379. Bind arrays to PL/SQL types */
  1380. int php_oci_bind_array_by_name(php_oci_statement *statement, char *name, size_t name_len, zval *var, zend_long max_table_length, zend_long maxlength, zend_long type)
  1381. {
  1382. php_oci_bind *bind;
  1383. sword errstatus;
  1384. zend_string *zvtmp;
  1385. zval *val;
  1386. ZEND_ASSERT(Z_ISREF_P(var));
  1387. val = Z_REFVAL_P(var);
  1388. convert_to_array(val);
  1389. SEPARATE_ARRAY(val);
  1390. if (maxlength < -1) {
  1391. php_error_docref(NULL, E_WARNING, "Invalid max length value (" ZEND_LONG_FMT ")", maxlength);
  1392. return 1;
  1393. }
  1394. switch(type) {
  1395. case SQLT_NUM:
  1396. case SQLT_INT:
  1397. case SQLT_LNG:
  1398. bind = php_oci_bind_array_helper_number(val, max_table_length);
  1399. break;
  1400. case SQLT_FLT:
  1401. bind = php_oci_bind_array_helper_double(val, max_table_length);
  1402. break;
  1403. case SQLT_AFC:
  1404. case SQLT_CHR:
  1405. case SQLT_VCS:
  1406. case SQLT_AVC:
  1407. case SQLT_STR:
  1408. case SQLT_LVC:
  1409. if (maxlength == -1 && zend_hash_num_elements(Z_ARRVAL_P(val)) == 0) {
  1410. php_error_docref(NULL, E_WARNING, "You must provide max length value for empty arrays");
  1411. return 1;
  1412. }
  1413. bind = php_oci_bind_array_helper_string(val, max_table_length, maxlength);
  1414. break;
  1415. case SQLT_ODT:
  1416. bind = php_oci_bind_array_helper_date(val, max_table_length, statement->connection);
  1417. break;
  1418. default:
  1419. php_error_docref(NULL, E_WARNING, "Unknown or unsupported datatype given: " ZEND_LONG_FMT, type);
  1420. return 1;
  1421. break;
  1422. }
  1423. if (bind == NULL) {
  1424. /* failed to generate bind struct */
  1425. return 1;
  1426. }
  1427. bind->descriptor = NULL;
  1428. bind->statement = NULL;
  1429. bind->parent_statement = statement;
  1430. bind->bind = NULL;
  1431. ZVAL_COPY(&bind->val, var);
  1432. bind->array.type = type;
  1433. bind->indicator = 0; /* not used for array binds */
  1434. bind->type = 0; /* not used for array binds */
  1435. PHP_OCI_CALL_RETURN(errstatus,
  1436. OCIBindByName,
  1437. (
  1438. statement->stmt,
  1439. (OCIBind **)&bind->bind,
  1440. statement->err,
  1441. (text *)name,
  1442. (sb4) name_len,
  1443. (dvoid *) bind->array.elements,
  1444. (sb4) bind->array.max_length,
  1445. (ub2)type,
  1446. (dvoid *)bind->array.indicators,
  1447. (ub2 *)bind->array.element_lengths,
  1448. (ub2 *)0, /* bindp->array.retcodes, */
  1449. (ub4) max_table_length,
  1450. (ub4 *) &(bind->array.current_length),
  1451. (ub4) OCI_DEFAULT
  1452. )
  1453. );
  1454. if (errstatus != OCI_SUCCESS) {
  1455. if (bind->array.elements) {
  1456. efree(bind->array.elements);
  1457. }
  1458. if (bind->array.element_lengths) {
  1459. efree(bind->array.element_lengths);
  1460. }
  1461. if (bind->array.indicators) {
  1462. efree(bind->array.indicators);
  1463. }
  1464. zval_ptr_dtor(&bind->val);
  1465. efree(bind);
  1466. statement->errcode = php_oci_error(statement->err, errstatus);
  1467. PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
  1468. return 1;
  1469. }
  1470. if (!statement->binds) {
  1471. ALLOC_HASHTABLE(statement->binds);
  1472. zend_hash_init(statement->binds, 13, NULL, php_oci_bind_hash_dtor, 0);
  1473. }
  1474. zvtmp = zend_string_init(name, name_len, 0);
  1475. zend_hash_update_ptr(statement->binds, zvtmp, bind);
  1476. zend_string_release_ex(zvtmp, 0);
  1477. statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  1478. return 0;
  1479. }
  1480. /* }}} */
  1481. /* {{{ php_oci_bind_array_helper_string()
  1482. Bind arrays to PL/SQL types */
  1483. php_oci_bind *php_oci_bind_array_helper_string(zval *var, zend_long max_table_length, zend_long maxlength)
  1484. {
  1485. php_oci_bind *bind;
  1486. ub4 i;
  1487. HashTable *hash;
  1488. zval *entry;
  1489. SEPARATE_ARRAY(var); /* TODO: may be use new HashTable iteration and prevent inplace modification */
  1490. hash = Z_ARRVAL_P(var);
  1491. if (maxlength == -1) {
  1492. zend_hash_internal_pointer_reset(hash);
  1493. while ((entry = zend_hash_get_current_data(hash)) != NULL) {
  1494. if (!try_convert_to_string(entry)) {
  1495. return NULL;
  1496. }
  1497. if (maxlength == -1 || Z_STRLEN_P(entry) > (size_t) maxlength) {
  1498. maxlength = Z_STRLEN_P(entry) + 1;
  1499. }
  1500. zend_hash_move_forward(hash);
  1501. }
  1502. }
  1503. bind = emalloc(sizeof(php_oci_bind));
  1504. ZVAL_UNDEF(&bind->val);
  1505. bind->array.elements = (text *)safe_emalloc(max_table_length * (maxlength + 1), sizeof(text), 0);
  1506. memset(bind->array.elements, 0, max_table_length * (maxlength + 1) * sizeof(text));
  1507. bind->array.current_length = zend_hash_num_elements(Z_ARRVAL_P(var));
  1508. bind->array.old_length = bind->array.current_length;
  1509. bind->array.max_length = (ub4) maxlength;
  1510. bind->array.element_lengths = safe_emalloc(max_table_length, sizeof(ub2), 0);
  1511. memset(bind->array.element_lengths, 0, max_table_length*sizeof(ub2));
  1512. bind->array.indicators = safe_emalloc(max_table_length, sizeof(sb2), 0);
  1513. memset(bind->array.indicators, 0, max_table_length*sizeof(sb2));
  1514. zend_hash_internal_pointer_reset(hash);
  1515. for (i = 0; i < bind->array.current_length; i++) {
  1516. if ((entry = zend_hash_get_current_data(hash)) != NULL) {
  1517. if (!try_convert_to_string(entry)) {
  1518. efree(bind->array.elements);
  1519. efree(bind->array.element_lengths);
  1520. efree(bind->array.indicators);
  1521. efree(bind);
  1522. return NULL;
  1523. }
  1524. bind->array.element_lengths[i] = (ub2) Z_STRLEN_P(entry);
  1525. if (Z_STRLEN_P(entry) == 0) {
  1526. bind->array.indicators[i] = -1;
  1527. }
  1528. zend_hash_move_forward(hash);
  1529. } else {
  1530. break;
  1531. }
  1532. }
  1533. zend_hash_internal_pointer_reset(hash);
  1534. for (i = 0; i < max_table_length; i++) {
  1535. if ((i < bind->array.current_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
  1536. int element_length;
  1537. if (!try_convert_to_string(entry)) {
  1538. efree(bind->array.elements);
  1539. efree(bind->array.element_lengths);
  1540. efree(bind->array.indicators);
  1541. efree(bind);
  1542. return NULL;
  1543. }
  1544. element_length = ((size_t) maxlength > Z_STRLEN_P(entry)) ? (int) Z_STRLEN_P(entry) : (int) maxlength;
  1545. memcpy((text *)bind->array.elements + i*maxlength, Z_STRVAL_P(entry), element_length);
  1546. ((text *)bind->array.elements)[i*maxlength + element_length] = '\0';
  1547. zend_hash_move_forward(hash);
  1548. } else {
  1549. ((text *)bind->array.elements)[i*maxlength] = '\0';
  1550. }
  1551. }
  1552. zend_hash_internal_pointer_reset(hash);
  1553. return bind;
  1554. }
  1555. /* }}} */
  1556. /* {{{ php_oci_bind_array_helper_number()
  1557. Bind arrays to PL/SQL types */
  1558. php_oci_bind *php_oci_bind_array_helper_number(zval *var, zend_long max_table_length)
  1559. {
  1560. php_oci_bind *bind;
  1561. ub4 i;
  1562. HashTable *hash;
  1563. zval *entry;
  1564. SEPARATE_ARRAY(var); /* TODO: may be use new HashTable iteration and prevent inplace modification */
  1565. hash = Z_ARRVAL_P(var);
  1566. bind = emalloc(sizeof(php_oci_bind));
  1567. ZVAL_UNDEF(&bind->val);
  1568. bind->array.elements = (oci_phpsized_int *)safe_emalloc(max_table_length, sizeof(oci_phpsized_int), 0);
  1569. bind->array.current_length = zend_hash_num_elements(Z_ARRVAL_P(var));
  1570. bind->array.old_length = bind->array.current_length;
  1571. bind->array.max_length = sizeof(oci_phpsized_int);
  1572. bind->array.element_lengths = safe_emalloc(max_table_length, sizeof(ub2), 0);
  1573. memset(bind->array.element_lengths, 0, max_table_length * sizeof(ub2));
  1574. bind->array.indicators = NULL;
  1575. zend_hash_internal_pointer_reset(hash);
  1576. for (i = 0; i < max_table_length; i++) {
  1577. if (i < bind->array.current_length) {
  1578. bind->array.element_lengths[i] = sizeof(oci_phpsized_int);
  1579. }
  1580. if ((i < bind->array.current_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
  1581. convert_to_long(entry);
  1582. ((oci_phpsized_int *)bind->array.elements)[i] = (oci_phpsized_int) Z_LVAL_P(entry);
  1583. zend_hash_move_forward(hash);
  1584. } else {
  1585. ((oci_phpsized_int *)bind->array.elements)[i] = 0;
  1586. }
  1587. }
  1588. zend_hash_internal_pointer_reset(hash);
  1589. return bind;
  1590. }
  1591. /* }}} */
  1592. /* {{{ php_oci_bind_array_helper_double()
  1593. Bind arrays to PL/SQL types */
  1594. php_oci_bind *php_oci_bind_array_helper_double(zval *var, zend_long max_table_length)
  1595. {
  1596. php_oci_bind *bind;
  1597. ub4 i;
  1598. HashTable *hash;
  1599. zval *entry;
  1600. SEPARATE_ARRAY(var); /* TODO: may be use new HashTable iteration and prevent inplace modification */
  1601. hash = Z_ARRVAL_P(var);
  1602. bind = emalloc(sizeof(php_oci_bind));
  1603. ZVAL_UNDEF(&bind->val);
  1604. bind->array.elements = (double *)safe_emalloc(max_table_length, sizeof(double), 0);
  1605. bind->array.current_length = zend_hash_num_elements(Z_ARRVAL_P(var));
  1606. bind->array.old_length = bind->array.current_length;
  1607. bind->array.max_length = sizeof(double);
  1608. bind->array.element_lengths = safe_emalloc(max_table_length, sizeof(ub2), 0);
  1609. memset(bind->array.element_lengths, 0, max_table_length * sizeof(ub2));
  1610. bind->array.indicators = NULL;
  1611. zend_hash_internal_pointer_reset(hash);
  1612. for (i = 0; i < max_table_length; i++) {
  1613. if (i < bind->array.current_length) {
  1614. bind->array.element_lengths[i] = sizeof(double);
  1615. }
  1616. if ((i < bind->array.current_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
  1617. convert_to_double(entry);
  1618. ((double *)bind->array.elements)[i] = (double) Z_DVAL_P(entry);
  1619. zend_hash_move_forward(hash);
  1620. } else {
  1621. ((double *)bind->array.elements)[i] = 0;
  1622. }
  1623. }
  1624. zend_hash_internal_pointer_reset(hash);
  1625. return bind;
  1626. }
  1627. /* }}} */
  1628. /* {{{ php_oci_bind_array_helper_date()
  1629. Bind arrays to PL/SQL types */
  1630. php_oci_bind *php_oci_bind_array_helper_date(zval *var, zend_long max_table_length, php_oci_connection *connection)
  1631. {
  1632. php_oci_bind *bind;
  1633. ub4 i;
  1634. HashTable *hash;
  1635. zval *entry;
  1636. sword errstatus;
  1637. SEPARATE_ARRAY(var); /* TODO: may be use new HashTable iteration and prevent inplace modification */
  1638. hash = Z_ARRVAL_P(var);
  1639. bind = emalloc(sizeof(php_oci_bind));
  1640. ZVAL_UNDEF(&bind->val);
  1641. bind->array.elements = (OCIDate *)safe_emalloc(max_table_length, sizeof(OCIDate), 0);
  1642. bind->array.current_length = zend_hash_num_elements(Z_ARRVAL_P(var));
  1643. bind->array.old_length = bind->array.current_length;
  1644. bind->array.max_length = sizeof(OCIDate);
  1645. bind->array.element_lengths = safe_emalloc(max_table_length, sizeof(ub2), 0);
  1646. memset(bind->array.element_lengths, 0, max_table_length * sizeof(ub2));
  1647. bind->array.indicators = NULL;
  1648. zend_hash_internal_pointer_reset(hash);
  1649. for (i = 0; i < max_table_length; i++) {
  1650. OCIDate oci_date;
  1651. if (i < bind->array.current_length) {
  1652. bind->array.element_lengths[i] = sizeof(OCIDate);
  1653. }
  1654. if ((i < bind->array.current_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
  1655. zend_string *entry_str = zval_try_get_string(entry);
  1656. if (UNEXPECTED(!entry_str)) {
  1657. efree(bind->array.element_lengths);
  1658. efree(bind->array.elements);
  1659. efree(bind);
  1660. return NULL;
  1661. }
  1662. PHP_OCI_CALL_RETURN(errstatus, OCIDateFromText, (connection->err, (CONST text *)ZSTR_VAL(entry_str), (ub4) ZSTR_LEN(entry_str), NULL, 0, NULL, 0, &oci_date));
  1663. zend_string_release(entry_str);
  1664. if (errstatus != OCI_SUCCESS) {
  1665. /* failed to convert string to date */
  1666. efree(bind->array.element_lengths);
  1667. efree(bind->array.elements);
  1668. efree(bind);
  1669. connection->errcode = php_oci_error(connection->err, errstatus);
  1670. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  1671. return NULL;
  1672. }
  1673. ((OCIDate *)bind->array.elements)[i] = oci_date;
  1674. zend_hash_move_forward(hash);
  1675. } else {
  1676. PHP_OCI_CALL_RETURN(errstatus, OCIDateFromText, (connection->err, (CONST text *)"01-JAN-00", sizeof("01-JAN-00")-1, NULL, 0, NULL, 0, &oci_date));
  1677. if (errstatus != OCI_SUCCESS) {
  1678. /* failed to convert string to date */
  1679. efree(bind->array.element_lengths);
  1680. efree(bind->array.elements);
  1681. efree(bind);
  1682. connection->errcode = php_oci_error(connection->err, errstatus);
  1683. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  1684. return NULL;
  1685. }
  1686. ((OCIDate *)bind->array.elements)[i] = oci_date;
  1687. }
  1688. connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  1689. }
  1690. zend_hash_internal_pointer_reset(hash);
  1691. return bind;
  1692. }
  1693. /* }}} */
  1694. #endif /* HAVE_OCI8 */