oci8_statement.c 59 KB

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