oci8_interface.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613
  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. #ifndef OCI_STMT_CALL
  37. #define OCI_STMT_CALL 10
  38. #endif
  39. /* {{{ proto bool oci_register_taf_callback( resource connection [, mixed callback] )
  40. Register a callback function for Oracle Transparent Application Failover (TAF) */
  41. PHP_FUNCTION(oci_register_taf_callback)
  42. {
  43. zval *z_connection;
  44. php_oci_connection *connection;
  45. zval *callback;
  46. zend_string *callback_name;
  47. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|z!", &z_connection, &callback) == FAILURE) {
  48. return;
  49. }
  50. if (callback) {
  51. #if PHP_MAJOR_VERSION > 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION >= 2)
  52. if (!zend_is_callable(callback, 0, 0)) {
  53. callback_name = zend_get_callable_name(callback);
  54. php_error_docref(NULL, E_WARNING, "function '%s' is not callable", ZSTR_VAL(callback_name));
  55. #if PHP_VERSION_ID < 70300
  56. zend_string_release(callback_name);
  57. #else
  58. zend_string_release_ex(callback_name, 0);
  59. #endif
  60. RETURN_FALSE;
  61. }
  62. #else
  63. if (!zend_is_callable(callback, 0, &callback_name)) {
  64. php_error_docref(NULL, E_WARNING, "function '%s' is not callable", ZSTR_VAL(callback_name));
  65. #if PHP_VERSION_ID < 70300
  66. zend_string_release(callback_name);
  67. #else
  68. zend_string_release_ex(callback_name, 0);
  69. #endif
  70. RETURN_FALSE;
  71. }
  72. #if PHP_VERSION_ID < 70300
  73. zend_string_release(callback_name);
  74. #else
  75. zend_string_release_ex(callback_name, 0);
  76. #endif
  77. #endif
  78. }
  79. PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
  80. if (php_oci_register_taf_callback(connection, callback) == 0) {
  81. RETURN_TRUE;
  82. } else {
  83. RETURN_FALSE;
  84. }
  85. }
  86. /* }}} */
  87. /* {{{ proto bool oci_unregister_taf_callback( resource connection )
  88. * Unregister a callback function for Oracle Transparent Application Failover (TAF) */
  89. PHP_FUNCTION(oci_unregister_taf_callback)
  90. {
  91. zval *z_connection;
  92. php_oci_connection *connection;
  93. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &z_connection) == FAILURE) {
  94. return;
  95. }
  96. PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
  97. if (php_oci_unregister_taf_callback(connection) == 0) {
  98. RETURN_TRUE;
  99. } else {
  100. RETURN_FALSE;
  101. }
  102. }
  103. /* }}} */
  104. /* {{{ proto bool oci_define_by_name(resource stmt, string name, mixed &var [, int type])
  105. Define a PHP variable to an Oracle column by name */
  106. /* if you want to define a LOB/CLOB etc make sure you allocate it via OCINewDescriptor BEFORE defining!!! */
  107. PHP_FUNCTION(oci_define_by_name)
  108. {
  109. zval *stmt, *var;
  110. char *name;
  111. size_t name_len;
  112. zend_long type = 0;
  113. php_oci_statement *statement;
  114. php_oci_define *define;
  115. zend_string *zvtmp;
  116. ZEND_PARSE_PARAMETERS_START(3, 4)
  117. Z_PARAM_RESOURCE(stmt)
  118. Z_PARAM_STRING(name, name_len)
  119. Z_PARAM_ZVAL(var)
  120. Z_PARAM_OPTIONAL
  121. Z_PARAM_LONG(type)
  122. ZEND_PARSE_PARAMETERS_END();
  123. if (!name_len) {
  124. php_error_docref(NULL, E_WARNING, "Column name cannot be empty");
  125. RETURN_FALSE;
  126. }
  127. PHP_OCI_ZVAL_TO_STATEMENT(stmt, statement);
  128. if (statement->defines == NULL) {
  129. ALLOC_HASHTABLE(statement->defines);
  130. zend_hash_init(statement->defines, 13, NULL, php_oci_define_hash_dtor, 0);
  131. }
  132. else if (zend_hash_str_exists(statement->defines, (const char *)name, name_len)) {
  133. RETURN_FALSE;
  134. }
  135. define = ecalloc(1,sizeof(php_oci_define));
  136. /* if (zend_hash_add(statement->defines, name, name_len, define, sizeof(php_oci_define), (void **)&tmp_define) == SUCCESS) { */
  137. zvtmp = zend_string_init(name, name_len, 0);
  138. if ((define = zend_hash_add_new_ptr(statement->defines, zvtmp, define)) != NULL) {
  139. #if PHP_VERSION_ID < 70300
  140. zend_string_release(zvtmp);
  141. #else
  142. zend_string_release_ex(zvtmp, 0);
  143. #endif
  144. } else {
  145. efree(define);
  146. #if PHP_VERSION_ID < 70300
  147. zend_string_release(zvtmp);
  148. #else
  149. zend_string_release_ex(zvtmp, 0);
  150. #endif
  151. RETURN_FALSE;
  152. }
  153. define->name = (text*) ecalloc(1, name_len+1);
  154. memcpy(define->name, name, name_len);
  155. define->name[name_len] = '\0';
  156. define->name_len = (ub4) name_len;
  157. define->type = (ub4) type;
  158. ZEND_ASSERT(Z_ISREF_P(var));
  159. ZVAL_COPY(&define->val, var);
  160. RETURN_TRUE;
  161. }
  162. /* }}} */
  163. /* {{{ proto bool oci_bind_by_name(resource stmt, string name, mixed &var [, int maxlength [, int type]])
  164. Bind a PHP variable to an Oracle placeholder by name */
  165. /* if you want to bind a LOB/CLOB etc make sure you allocate it via OCINewDescriptor BEFORE binding!!! */
  166. PHP_FUNCTION(oci_bind_by_name)
  167. {
  168. ub2 bind_type = SQLT_CHR; /* unterminated string */
  169. size_t name_len;
  170. zend_long maxlen = -1, type = 0;
  171. char *name;
  172. zval *z_statement;
  173. zval *bind_var = NULL;
  174. php_oci_statement *statement;
  175. ZEND_PARSE_PARAMETERS_START(3, 5)
  176. Z_PARAM_RESOURCE(z_statement)
  177. Z_PARAM_STRING(name, name_len)
  178. Z_PARAM_ZVAL(bind_var)
  179. Z_PARAM_OPTIONAL
  180. Z_PARAM_LONG(maxlen)
  181. Z_PARAM_LONG(type)
  182. ZEND_PARSE_PARAMETERS_END();
  183. if (type) {
  184. bind_type = (ub2) type;
  185. }
  186. PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
  187. if (php_oci_bind_by_name(statement, name, name_len, bind_var, maxlen, bind_type)) {
  188. RETURN_FALSE;
  189. }
  190. RETURN_TRUE;
  191. }
  192. /* }}} */
  193. /* {{{ proto bool oci_bind_array_by_name(resource stmt, string name, array &var, int max_table_length [, int max_item_length [, int type ]])
  194. Bind a PHP array to an Oracle PL/SQL type by name */
  195. PHP_FUNCTION(oci_bind_array_by_name)
  196. {
  197. size_t name_len;
  198. zend_long max_item_len = -1;
  199. zend_long max_array_len = 0;
  200. zend_long type = SQLT_AFC;
  201. char *name;
  202. zval *z_statement;
  203. zval *bind_var = NULL;
  204. php_oci_statement *statement;
  205. ZEND_PARSE_PARAMETERS_START(4, 6)
  206. Z_PARAM_RESOURCE(z_statement)
  207. Z_PARAM_STRING(name, name_len)
  208. Z_PARAM_ZVAL(bind_var)
  209. Z_PARAM_LONG(max_array_len)
  210. Z_PARAM_OPTIONAL
  211. Z_PARAM_LONG(max_item_len)
  212. Z_PARAM_LONG(type)
  213. ZEND_PARSE_PARAMETERS_END();
  214. PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
  215. if (ZEND_NUM_ARGS() == 5 && max_item_len <= 0) {
  216. max_item_len = -1;
  217. }
  218. if (max_array_len <= 0) {
  219. php_error_docref(NULL, E_WARNING, "Maximum array length must be greater than zero");
  220. RETURN_FALSE;
  221. }
  222. if (php_oci_bind_array_by_name(statement, name, (sb4) name_len, bind_var, max_array_len, max_item_len, type)) {
  223. RETURN_FALSE;
  224. }
  225. RETURN_TRUE;
  226. }
  227. /* }}} */
  228. /* {{{ proto bool oci_free_descriptor()
  229. Deletes large object description */
  230. PHP_FUNCTION(oci_free_descriptor)
  231. {
  232. zval *tmp, *z_descriptor = getThis();
  233. php_oci_descriptor *descriptor;
  234. if (!getThis()) {
  235. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &z_descriptor, oci_lob_class_entry_ptr) == FAILURE) {
  236. return;
  237. }
  238. }
  239. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
  240. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
  241. RETURN_FALSE;
  242. }
  243. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);
  244. zend_list_close(descriptor->id);
  245. RETURN_TRUE;
  246. }
  247. /* }}} */
  248. /* {{{ proto bool oci_lob_save( string data [, int offset ])
  249. Saves a large object */
  250. PHP_FUNCTION(oci_lob_save)
  251. {
  252. zval *tmp, *z_descriptor = getThis();
  253. php_oci_descriptor *descriptor;
  254. char *data;
  255. size_t data_len;
  256. zend_long offset = 0;
  257. ub4 bytes_written;
  258. if (getThis()) {
  259. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &data, &data_len, &offset) == FAILURE) {
  260. return;
  261. }
  262. }
  263. else {
  264. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os|l", &z_descriptor, oci_lob_class_entry_ptr, &data, &data_len, &offset) == FAILURE) {
  265. return;
  266. }
  267. }
  268. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
  269. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
  270. RETURN_FALSE;
  271. }
  272. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);
  273. if (offset < 0) {
  274. php_error_docref(NULL, E_WARNING, "Offset parameter must be greater than or equal to 0");
  275. RETURN_FALSE;
  276. }
  277. if (php_oci_lob_write(descriptor, (ub4) offset, data, (ub4) data_len, &bytes_written)) {
  278. RETURN_FALSE;
  279. }
  280. RETURN_TRUE;
  281. }
  282. /* }}} */
  283. /* {{{ proto bool oci_lob_import( string filename )
  284. Loads file into a LOB */
  285. PHP_FUNCTION(oci_lob_import)
  286. {
  287. zval *tmp, *z_descriptor = getThis();
  288. php_oci_descriptor *descriptor;
  289. char *filename;
  290. size_t filename_len;
  291. if (getThis()) {
  292. if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &filename, &filename_len) == FAILURE) {
  293. return;
  294. }
  295. }
  296. else {
  297. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Op", &z_descriptor, oci_lob_class_entry_ptr, &filename, &filename_len) == FAILURE) {
  298. return;
  299. }
  300. }
  301. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
  302. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
  303. RETURN_FALSE;
  304. }
  305. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);
  306. if (php_oci_lob_import(descriptor, filename)) {
  307. RETURN_FALSE;
  308. }
  309. RETURN_TRUE;
  310. }
  311. /* }}} */
  312. /* {{{ proto string oci_lob_load()
  313. Loads a large object */
  314. PHP_FUNCTION(oci_lob_load)
  315. {
  316. zval *tmp, *z_descriptor = getThis();
  317. php_oci_descriptor *descriptor;
  318. char *buffer = NULL;
  319. ub4 buffer_len;
  320. if (!getThis()) {
  321. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &z_descriptor, oci_lob_class_entry_ptr) == FAILURE) {
  322. return;
  323. }
  324. }
  325. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
  326. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
  327. RETURN_FALSE;
  328. }
  329. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);
  330. if (php_oci_lob_read(descriptor, -1, 0, &buffer, &buffer_len)) {
  331. RETURN_FALSE;
  332. }
  333. if (buffer_len > 0) {
  334. zend_string *ret = zend_string_init(buffer, buffer_len, 0);
  335. if (buffer)
  336. efree(buffer);
  337. RETURN_STR(ret);
  338. }
  339. else {
  340. RETURN_EMPTY_STRING();
  341. }
  342. }
  343. /* }}} */
  344. /* {{{ proto string oci_lob_read( int length )
  345. Reads particular part of a large object */
  346. PHP_FUNCTION(oci_lob_read)
  347. {
  348. zval *tmp, *z_descriptor = getThis();
  349. php_oci_descriptor *descriptor;
  350. zend_long length;
  351. char *buffer;
  352. ub4 buffer_len;
  353. if (getThis()) {
  354. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &length) == FAILURE) {
  355. return;
  356. }
  357. }
  358. else {
  359. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &z_descriptor, oci_lob_class_entry_ptr, &length) == FAILURE) {
  360. return;
  361. }
  362. }
  363. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
  364. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
  365. RETURN_FALSE;
  366. }
  367. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);
  368. if (length <= 0) {
  369. php_error_docref(NULL, E_WARNING, "Length parameter must be greater than 0");
  370. RETURN_FALSE;
  371. }
  372. if (php_oci_lob_read(descriptor, length, descriptor->lob_current_position, &buffer, &buffer_len)) {
  373. RETURN_FALSE;
  374. }
  375. if (buffer_len > 0) {
  376. zend_string *ret = zend_string_init(buffer, buffer_len, 0);
  377. efree(buffer);
  378. RETURN_STR(ret);
  379. }
  380. else {
  381. RETURN_EMPTY_STRING();
  382. }
  383. }
  384. /* }}} */
  385. /* {{{ proto bool oci_lob_eof()
  386. Checks if EOF is reached */
  387. PHP_FUNCTION(oci_lob_eof)
  388. {
  389. zval *tmp, *z_descriptor = getThis();
  390. php_oci_descriptor *descriptor;
  391. ub4 lob_length;
  392. if (!getThis()) {
  393. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &z_descriptor, oci_lob_class_entry_ptr) == FAILURE) {
  394. return;
  395. }
  396. }
  397. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
  398. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
  399. RETURN_FALSE;
  400. }
  401. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);
  402. if (!php_oci_lob_get_length(descriptor, &lob_length)) {
  403. if (lob_length == descriptor->lob_current_position) {
  404. RETURN_TRUE;
  405. }
  406. }
  407. RETURN_FALSE;
  408. }
  409. /* }}} */
  410. /* {{{ proto int oci_lob_tell()
  411. Tells LOB pointer position */
  412. PHP_FUNCTION(oci_lob_tell)
  413. {
  414. zval *tmp, *z_descriptor = getThis();
  415. php_oci_descriptor *descriptor;
  416. if (!getThis()) {
  417. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &z_descriptor, oci_lob_class_entry_ptr) == FAILURE) {
  418. return;
  419. }
  420. }
  421. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
  422. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
  423. RETURN_FALSE;
  424. }
  425. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);
  426. RETURN_LONG(descriptor->lob_current_position);
  427. }
  428. /* }}} */
  429. /* {{{ proto bool oci_lob_rewind()
  430. Rewind pointer of a LOB */
  431. PHP_FUNCTION(oci_lob_rewind)
  432. {
  433. zval *tmp, *z_descriptor = getThis();
  434. php_oci_descriptor *descriptor;
  435. if (!getThis()) {
  436. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &z_descriptor, oci_lob_class_entry_ptr) == FAILURE) {
  437. return;
  438. }
  439. }
  440. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
  441. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
  442. RETURN_FALSE;
  443. }
  444. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);
  445. descriptor->lob_current_position = 0;
  446. RETURN_TRUE;
  447. }
  448. /* }}} */
  449. /* {{{ proto bool oci_lob_seek( int offset [, int whence ])
  450. Moves the pointer of a LOB */
  451. PHP_FUNCTION(oci_lob_seek)
  452. {
  453. zval *tmp, *z_descriptor = getThis();
  454. php_oci_descriptor *descriptor;
  455. zend_long offset, whence = PHP_OCI_SEEK_SET;
  456. ub4 lob_length;
  457. if (getThis()) {
  458. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l", &offset, &whence) == FAILURE) {
  459. return;
  460. }
  461. }
  462. else {
  463. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol|l", &z_descriptor, oci_lob_class_entry_ptr, &offset, &whence) == FAILURE) {
  464. return;
  465. }
  466. }
  467. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
  468. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
  469. RETURN_FALSE;
  470. }
  471. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);
  472. if (php_oci_lob_get_length(descriptor, &lob_length)) {
  473. RETURN_FALSE;
  474. }
  475. switch(whence) {
  476. case PHP_OCI_SEEK_CUR:
  477. descriptor->lob_current_position += (ub4) offset;
  478. break;
  479. case PHP_OCI_SEEK_END:
  480. if ((descriptor->lob_size + offset) >= 0) {
  481. descriptor->lob_current_position = descriptor->lob_size + (ub4) offset;
  482. }
  483. else {
  484. descriptor->lob_current_position = 0;
  485. }
  486. break;
  487. case PHP_OCI_SEEK_SET:
  488. default:
  489. descriptor->lob_current_position = (offset > 0) ? (ub4) offset : 0;
  490. break;
  491. }
  492. if (descriptor->lob_current_position > UB4MAXVAL) {
  493. php_error_docref(NULL, E_WARNING, "Invalid offset or LOB position");
  494. RETURN_FALSE;
  495. }
  496. RETURN_TRUE;
  497. }
  498. /* }}} */
  499. /* {{{ proto int oci_lob_size()
  500. Returns size of a large object */
  501. PHP_FUNCTION(oci_lob_size)
  502. {
  503. zval *tmp, *z_descriptor = getThis();
  504. php_oci_descriptor *descriptor;
  505. ub4 lob_length;
  506. if (!getThis()) {
  507. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &z_descriptor, oci_lob_class_entry_ptr) == FAILURE) {
  508. return;
  509. }
  510. }
  511. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
  512. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
  513. RETURN_FALSE;
  514. }
  515. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);
  516. if (php_oci_lob_get_length(descriptor, &lob_length)) {
  517. RETURN_FALSE;
  518. }
  519. RETURN_LONG(lob_length);
  520. }
  521. /* }}} */
  522. /* {{{ proto int oci_lob_write( string string [, int length ])
  523. Writes data to current position of a LOB */
  524. PHP_FUNCTION(oci_lob_write)
  525. {
  526. zval *tmp, *z_descriptor = getThis();
  527. php_oci_descriptor *descriptor;
  528. size_t data_len;
  529. zend_long write_len = 0;
  530. ub4 bytes_written;
  531. char *data;
  532. if (getThis()) {
  533. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &data, &data_len, &write_len) == FAILURE) {
  534. return;
  535. }
  536. if (ZEND_NUM_ARGS() == 2) {
  537. data_len = MIN((zend_long) data_len, write_len);
  538. }
  539. }
  540. else {
  541. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os|l", &z_descriptor, oci_lob_class_entry_ptr, &data, &data_len, &write_len) == FAILURE) {
  542. return;
  543. }
  544. if (ZEND_NUM_ARGS() == 3) {
  545. data_len = MIN((zend_long) data_len, write_len);
  546. }
  547. }
  548. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
  549. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
  550. RETURN_FALSE;
  551. }
  552. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);
  553. if (data_len <= 0) {
  554. RETURN_LONG(0);
  555. }
  556. if (php_oci_lob_write(descriptor, descriptor->lob_current_position, data, (ub4) data_len, &bytes_written)) {
  557. RETURN_FALSE;
  558. }
  559. RETURN_LONG(bytes_written);
  560. }
  561. /* }}} */
  562. /* {{{ proto bool oci_lob_append( object lob )
  563. Appends data from a LOB to another LOB */
  564. PHP_FUNCTION(oci_lob_append)
  565. {
  566. zval *tmp_dest, *tmp_from, *z_descriptor_dest = getThis(), *z_descriptor_from;
  567. php_oci_descriptor *descriptor_dest, *descriptor_from;
  568. if (getThis()) {
  569. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &z_descriptor_from, oci_lob_class_entry_ptr) == FAILURE) {
  570. return;
  571. }
  572. }
  573. else {
  574. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &z_descriptor_dest, oci_lob_class_entry_ptr, &z_descriptor_from, oci_lob_class_entry_ptr) == FAILURE) {
  575. return;
  576. }
  577. }
  578. if ((tmp_dest = zend_hash_str_find(Z_OBJPROP_P(z_descriptor_dest), "descriptor", sizeof("descriptor")-1)) == NULL) {
  579. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property. The first argument should be valid descriptor object");
  580. RETURN_FALSE;
  581. }
  582. if ((tmp_from = zend_hash_str_find(Z_OBJPROP_P(z_descriptor_from), "descriptor", sizeof("descriptor")-1)) == NULL) {
  583. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property. The second argument should be valid descriptor object");
  584. RETURN_FALSE;
  585. }
  586. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp_dest, descriptor_dest);
  587. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp_from, descriptor_from);
  588. if (php_oci_lob_append(descriptor_dest, descriptor_from)) {
  589. RETURN_FALSE;
  590. }
  591. /* XXX should we increase lob_size here ? */
  592. RETURN_TRUE;
  593. }
  594. /* }}} */
  595. /* {{{ proto bool oci_lob_truncate( [ int length ])
  596. Truncates a LOB */
  597. PHP_FUNCTION(oci_lob_truncate)
  598. {
  599. zval *tmp, *z_descriptor = getThis();
  600. php_oci_descriptor *descriptor;
  601. zend_long trim_length = 0;
  602. ub4 ub_trim_length;
  603. if (getThis()) {
  604. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &trim_length) == FAILURE) {
  605. return;
  606. }
  607. }
  608. else {
  609. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l", &z_descriptor, oci_lob_class_entry_ptr, &trim_length) == FAILURE) {
  610. return;
  611. }
  612. }
  613. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
  614. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
  615. RETURN_FALSE;
  616. }
  617. if (trim_length < 0) {
  618. php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to zero");
  619. RETURN_FALSE;
  620. }
  621. ub_trim_length = (ub4) trim_length;
  622. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);
  623. if (php_oci_lob_truncate(descriptor, ub_trim_length)) {
  624. RETURN_FALSE;
  625. }
  626. RETURN_TRUE;
  627. }
  628. /* }}} */
  629. /* {{{ proto int oci_lob_erase( [ int offset [, int length ] ] )
  630. Erases a specified portion of the internal LOB, starting at a specified offset */
  631. PHP_FUNCTION(oci_lob_erase)
  632. {
  633. zval *tmp, *z_descriptor = getThis();
  634. php_oci_descriptor *descriptor;
  635. ub4 bytes_erased;
  636. zend_long offset = -1, length = -1;
  637. if (getThis()) {
  638. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ll", &offset, &length) == FAILURE) {
  639. return;
  640. }
  641. if (ZEND_NUM_ARGS() > 0 && offset < 0) {
  642. php_error_docref(NULL, E_WARNING, "Offset must be greater than or equal to 0");
  643. RETURN_FALSE;
  644. }
  645. if (ZEND_NUM_ARGS() > 1 && length < 0) {
  646. php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to 0");
  647. RETURN_FALSE;
  648. }
  649. }
  650. else {
  651. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|ll", &z_descriptor, oci_lob_class_entry_ptr, &offset, &length) == FAILURE) {
  652. return;
  653. }
  654. if (ZEND_NUM_ARGS() > 1 && offset < 0) {
  655. php_error_docref(NULL, E_WARNING, "Offset must be greater than or equal to 0");
  656. RETURN_FALSE;
  657. }
  658. if (ZEND_NUM_ARGS() > 2 && length < 0) {
  659. php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to 0");
  660. RETURN_FALSE;
  661. }
  662. }
  663. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
  664. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
  665. RETURN_FALSE;
  666. }
  667. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);
  668. if (php_oci_lob_erase(descriptor, offset, (ub4) length, &bytes_erased)) {
  669. RETURN_FALSE;
  670. }
  671. RETURN_LONG(bytes_erased);
  672. }
  673. /* }}} */
  674. /* {{{ proto bool oci_lob_flush( [ int flag ] )
  675. Flushes the LOB buffer */
  676. PHP_FUNCTION(oci_lob_flush)
  677. {
  678. zval *tmp, *z_descriptor = getThis();
  679. php_oci_descriptor *descriptor;
  680. zend_long flush_flag = 0;
  681. if (getThis()) {
  682. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &flush_flag) == FAILURE) {
  683. return;
  684. }
  685. }
  686. else {
  687. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l", &z_descriptor, oci_lob_class_entry_ptr, &flush_flag) == FAILURE) {
  688. return;
  689. }
  690. }
  691. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
  692. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
  693. RETURN_FALSE;
  694. }
  695. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);
  696. if (descriptor->buffering == PHP_OCI_LOB_BUFFER_DISABLED) {
  697. /* buffering wasn't enabled, there is nothing to flush */
  698. RETURN_FALSE;
  699. }
  700. if (php_oci_lob_flush(descriptor, flush_flag)) {
  701. RETURN_FALSE;
  702. }
  703. RETURN_TRUE;
  704. }
  705. /* }}} */
  706. /* {{{ proto bool ocisetbufferinglob( boolean flag )
  707. Enables/disables buffering for a LOB */
  708. PHP_FUNCTION(ocisetbufferinglob)
  709. {
  710. zval *tmp, *z_descriptor = getThis();
  711. php_oci_descriptor *descriptor;
  712. zend_bool flag;
  713. if (getThis()) {
  714. if (zend_parse_parameters(ZEND_NUM_ARGS(), "b", &flag) == FAILURE) {
  715. return;
  716. }
  717. }
  718. else {
  719. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ob", &z_descriptor, oci_lob_class_entry_ptr, &flag) == FAILURE) {
  720. return;
  721. }
  722. }
  723. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
  724. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
  725. RETURN_FALSE;
  726. }
  727. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);
  728. if (php_oci_lob_set_buffering(descriptor, flag)) {
  729. RETURN_FALSE;
  730. }
  731. RETURN_TRUE;
  732. }
  733. /* }}} */
  734. /* {{{ proto bool ocigetbufferinglob()
  735. Returns current state of buffering for a LOB */
  736. PHP_FUNCTION(ocigetbufferinglob)
  737. {
  738. zval *tmp, *z_descriptor = getThis();
  739. php_oci_descriptor *descriptor;
  740. if (!getThis()) {
  741. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &z_descriptor, oci_lob_class_entry_ptr) == FAILURE) {
  742. return;
  743. }
  744. }
  745. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
  746. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
  747. RETURN_FALSE;
  748. }
  749. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);
  750. if (descriptor->buffering != PHP_OCI_LOB_BUFFER_DISABLED) {
  751. RETURN_TRUE;
  752. }
  753. RETURN_FALSE;
  754. }
  755. /* }}} */
  756. /* {{{ proto bool oci_lob_copy( object lob_to, object lob_from [, int length ] )
  757. Copies data from a LOB to another LOB */
  758. PHP_FUNCTION(oci_lob_copy)
  759. {
  760. zval *tmp_dest, *tmp_from, *z_descriptor_dest, *z_descriptor_from;
  761. php_oci_descriptor *descriptor_dest, *descriptor_from;
  762. zend_long length = 0;
  763. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO|l", &z_descriptor_dest, oci_lob_class_entry_ptr, &z_descriptor_from, oci_lob_class_entry_ptr, &length) == FAILURE) {
  764. return;
  765. }
  766. if ((tmp_dest = zend_hash_str_find(Z_OBJPROP_P(z_descriptor_dest), "descriptor", sizeof("descriptor")-1)) == NULL) {
  767. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property. The first argument should be valid descriptor object");
  768. RETURN_FALSE;
  769. }
  770. if ((tmp_from = zend_hash_str_find(Z_OBJPROP_P(z_descriptor_from), "descriptor", sizeof("descriptor")-1)) == NULL) {
  771. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property. The second argument should be valid descriptor object");
  772. RETURN_FALSE;
  773. }
  774. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp_dest, descriptor_dest);
  775. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp_from, descriptor_from);
  776. if (ZEND_NUM_ARGS() == 3 && length < 0) {
  777. php_error_docref(NULL, E_WARNING, "Length parameter must be greater than 0");
  778. RETURN_FALSE;
  779. }
  780. if (ZEND_NUM_ARGS() == 2) {
  781. /* indicate that we want to copy from the current position to the end of the LOB */
  782. length = -1;
  783. }
  784. if (php_oci_lob_copy(descriptor_dest, descriptor_from, length)) {
  785. RETURN_FALSE;
  786. }
  787. RETURN_TRUE;
  788. }
  789. /* }}} */
  790. /* {{{ proto bool oci_lob_is_equal( object lob1, object lob2 )
  791. Tests to see if two LOB/FILE locators are equal */
  792. PHP_FUNCTION(oci_lob_is_equal)
  793. {
  794. zval *tmp_first, *tmp_second, *z_descriptor_first, *z_descriptor_second;
  795. php_oci_descriptor *descriptor_first, *descriptor_second;
  796. boolean is_equal;
  797. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &z_descriptor_first, oci_lob_class_entry_ptr, &z_descriptor_second, oci_lob_class_entry_ptr) == FAILURE) {
  798. return;
  799. }
  800. if ((tmp_first = zend_hash_str_find(Z_OBJPROP_P(z_descriptor_first), "descriptor", sizeof("descriptor")-1)) == NULL) {
  801. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property. The first argument should be valid descriptor object");
  802. RETURN_FALSE;
  803. }
  804. if ((tmp_second = zend_hash_str_find(Z_OBJPROP_P(z_descriptor_second), "descriptor", sizeof("descriptor")-1)) == NULL) {
  805. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property. The second argument should be valid descriptor object");
  806. RETURN_FALSE;
  807. }
  808. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp_first, descriptor_first);
  809. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp_second, descriptor_second);
  810. if (php_oci_lob_is_equal(descriptor_first, descriptor_second, &is_equal)) {
  811. RETURN_FALSE;
  812. }
  813. if (is_equal == TRUE) {
  814. RETURN_TRUE;
  815. }
  816. RETURN_FALSE;
  817. }
  818. /* }}} */
  819. /* {{{ proto bool oci_lob_export([string filename [, int start [, int length]]])
  820. Writes a large object into a file */
  821. PHP_FUNCTION(oci_lob_export)
  822. {
  823. zval *tmp, *z_descriptor = getThis();
  824. php_oci_descriptor *descriptor;
  825. char *filename;
  826. char *buffer;
  827. size_t filename_len;
  828. zend_long start = -1, length = -1, block_length;
  829. php_stream *stream;
  830. ub4 lob_length;
  831. if (getThis()) {
  832. if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|ll", &filename, &filename_len, &start, &length) == FAILURE) {
  833. return;
  834. }
  835. if (ZEND_NUM_ARGS() > 1 && start < 0) {
  836. php_error_docref(NULL, E_WARNING, "Start parameter must be greater than or equal to 0");
  837. RETURN_FALSE;
  838. }
  839. if (ZEND_NUM_ARGS() > 2 && length < 0) {
  840. php_error_docref(NULL, E_WARNING, "Length parameter must be greater than or equal to 0");
  841. RETURN_FALSE;
  842. }
  843. }
  844. else {
  845. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Op|ll", &z_descriptor, oci_lob_class_entry_ptr, &filename, &filename_len, &start, &length) == FAILURE) {
  846. return;
  847. }
  848. if (ZEND_NUM_ARGS() > 2 && start < 0) {
  849. php_error_docref(NULL, E_WARNING, "Start parameter must be greater than or equal to 0");
  850. RETURN_FALSE;
  851. }
  852. if (ZEND_NUM_ARGS() > 3 && length < 0) {
  853. php_error_docref(NULL, E_WARNING, "Length parameter must be greater than or equal to 0");
  854. RETURN_FALSE;
  855. }
  856. }
  857. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
  858. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
  859. RETURN_FALSE;
  860. }
  861. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);
  862. if (php_oci_lob_get_length(descriptor, &lob_length)) {
  863. RETURN_FALSE;
  864. }
  865. if (start == -1) {
  866. start = 0;
  867. }
  868. if (length == -1) {
  869. length = lob_length - descriptor->lob_current_position;
  870. }
  871. if (lob_length == 0) {
  872. length = 0;
  873. }
  874. if (length == 0) {
  875. /* nothing to write, fail silently */
  876. RETURN_FALSE;
  877. }
  878. if (php_check_open_basedir(filename)) {
  879. RETURN_FALSE;
  880. }
  881. stream = php_stream_open_wrapper_ex(filename, "w", REPORT_ERRORS, NULL, NULL);
  882. block_length = PHP_OCI_LOB_BUFFER_SIZE;
  883. if (block_length > length) {
  884. block_length = length;
  885. }
  886. while(length > 0) {
  887. ub4 tmp_bytes_read = 0;
  888. if (php_oci_lob_read(descriptor, block_length, start, &buffer, &tmp_bytes_read)) {
  889. php_stream_close(stream);
  890. RETURN_FALSE;
  891. }
  892. if (tmp_bytes_read && !php_stream_write(stream, buffer, tmp_bytes_read)) {
  893. php_stream_close(stream);
  894. if (buffer)
  895. efree(buffer);
  896. RETURN_FALSE;
  897. }
  898. if (buffer) {
  899. efree(buffer);
  900. }
  901. length -= tmp_bytes_read;
  902. descriptor->lob_current_position += tmp_bytes_read;
  903. start += tmp_bytes_read;
  904. if (block_length > length) {
  905. block_length = length;
  906. }
  907. }
  908. php_stream_close(stream);
  909. RETURN_TRUE;
  910. }
  911. /* }}} */
  912. /* {{{ proto bool oci_lob_write_temporary(string var [, int lob_type])
  913. Writes temporary blob */
  914. PHP_FUNCTION(oci_lob_write_temporary)
  915. {
  916. zval *tmp, *z_descriptor = getThis();
  917. php_oci_descriptor *descriptor;
  918. char *data;
  919. size_t data_len;
  920. zend_long type = OCI_TEMP_CLOB;
  921. if (getThis()) {
  922. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &data, &data_len, &type) == FAILURE) {
  923. return;
  924. }
  925. }
  926. else {
  927. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os|l", &z_descriptor, oci_lob_class_entry_ptr, &data, &data_len, &type) == FAILURE) {
  928. return;
  929. }
  930. }
  931. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
  932. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
  933. RETURN_FALSE;
  934. }
  935. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);
  936. if (php_oci_lob_write_tmp(descriptor, type, data, (int) data_len)) {
  937. RETURN_FALSE;
  938. }
  939. RETURN_TRUE;
  940. }
  941. /* }}} */
  942. /* {{{ proto bool oci_lob_close()
  943. Closes lob descriptor */
  944. PHP_FUNCTION(oci_lob_close)
  945. {
  946. zval *tmp, *z_descriptor = getThis();
  947. php_oci_descriptor *descriptor;
  948. if (!getThis()) {
  949. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &z_descriptor, oci_lob_class_entry_ptr) == FAILURE) {
  950. return;
  951. }
  952. }
  953. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
  954. php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
  955. RETURN_FALSE;
  956. }
  957. PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);
  958. if (php_oci_lob_close(descriptor)) {
  959. RETURN_FALSE;
  960. }
  961. RETURN_TRUE;
  962. }
  963. /* }}} */
  964. /* {{{ proto object oci_new_descriptor(resource connection [, int type])
  965. Initialize a new empty descriptor LOB/FILE (LOB is default) */
  966. PHP_FUNCTION(oci_new_descriptor)
  967. {
  968. zval *z_connection;
  969. php_oci_connection *connection;
  970. php_oci_descriptor *descriptor;
  971. zend_long type = OCI_DTYPE_LOB;
  972. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &z_connection, &type) == FAILURE) {
  973. return;
  974. }
  975. PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
  976. /* php_oci_lob_create() checks type */
  977. descriptor = php_oci_lob_create(connection, type);
  978. if (!descriptor) {
  979. RETURN_NULL();
  980. }
  981. object_init_ex(return_value, oci_lob_class_entry_ptr);
  982. add_property_resource(return_value, "descriptor", descriptor->id);
  983. }
  984. /* }}} */
  985. /* {{{ proto bool oci_rollback(resource connection)
  986. Rollback the current context */
  987. PHP_FUNCTION(oci_rollback)
  988. {
  989. zval *z_connection;
  990. php_oci_connection *connection;
  991. ZEND_PARSE_PARAMETERS_START(1, 1)
  992. Z_PARAM_RESOURCE(z_connection)
  993. ZEND_PARSE_PARAMETERS_END();
  994. PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
  995. if (connection->descriptors) {
  996. php_oci_connection_descriptors_free(connection);
  997. }
  998. if (php_oci_connection_rollback(connection)) {
  999. RETURN_FALSE;
  1000. }
  1001. RETURN_TRUE;
  1002. }
  1003. /* }}} */
  1004. /* {{{ proto bool oci_commit(resource connection)
  1005. Commit the current context */
  1006. PHP_FUNCTION(oci_commit)
  1007. {
  1008. zval *z_connection;
  1009. php_oci_connection *connection;
  1010. ZEND_PARSE_PARAMETERS_START(1, 1)
  1011. Z_PARAM_RESOURCE(z_connection)
  1012. ZEND_PARSE_PARAMETERS_END();
  1013. PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
  1014. if (connection->descriptors) {
  1015. php_oci_connection_descriptors_free(connection);
  1016. }
  1017. if (php_oci_connection_commit(connection)) {
  1018. RETURN_FALSE;
  1019. }
  1020. RETURN_TRUE;
  1021. }
  1022. /* }}} */
  1023. /* {{{ proto string oci_field_name(resource stmt, mixed col)
  1024. Tell the name of a column */
  1025. PHP_FUNCTION(oci_field_name)
  1026. {
  1027. php_oci_out_column *column;
  1028. if ( ( column = php_oci_statement_get_column_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0) ) ) {
  1029. RETURN_STRINGL(column->name, column->name_len);
  1030. }
  1031. RETURN_FALSE;
  1032. }
  1033. /* }}} */
  1034. /* {{{ proto int oci_field_size(resource stmt, mixed col)
  1035. Tell the maximum data size of a column */
  1036. PHP_FUNCTION(oci_field_size)
  1037. {
  1038. php_oci_out_column *column;
  1039. if ( ( column = php_oci_statement_get_column_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0) ) ) {
  1040. /* Handle data type of LONG */
  1041. if (column->data_type == SQLT_LNG){
  1042. RETURN_LONG(column->storage_size4);
  1043. }
  1044. RETURN_LONG(column->data_size);
  1045. }
  1046. RETURN_FALSE;
  1047. }
  1048. /* }}} */
  1049. /* {{{ proto int oci_field_scale(resource stmt, mixed col)
  1050. Tell the scale of a column */
  1051. PHP_FUNCTION(oci_field_scale)
  1052. {
  1053. php_oci_out_column *column;
  1054. if ( ( column = php_oci_statement_get_column_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0) ) ) {
  1055. RETURN_LONG(column->scale);
  1056. }
  1057. RETURN_FALSE;
  1058. }
  1059. /* }}} */
  1060. /* {{{ proto int oci_field_precision(resource stmt, mixed col)
  1061. Tell the precision of a column */
  1062. PHP_FUNCTION(oci_field_precision)
  1063. {
  1064. php_oci_out_column *column;
  1065. if ( ( column = php_oci_statement_get_column_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0) ) ) {
  1066. RETURN_LONG(column->precision);
  1067. }
  1068. RETURN_FALSE;
  1069. }
  1070. /* }}} */
  1071. /* {{{ proto mixed oci_field_type(resource stmt, mixed col)
  1072. Tell the data type of a column */
  1073. PHP_FUNCTION(oci_field_type)
  1074. {
  1075. php_oci_out_column *column;
  1076. column = php_oci_statement_get_column_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  1077. if (!column) {
  1078. RETURN_FALSE;
  1079. }
  1080. switch (column->data_type) {
  1081. #ifdef SQLT_TIMESTAMP
  1082. case SQLT_TIMESTAMP:
  1083. RETVAL_STRING("TIMESTAMP");
  1084. break;
  1085. #endif
  1086. #ifdef SQLT_TIMESTAMP_TZ
  1087. case SQLT_TIMESTAMP_TZ:
  1088. RETVAL_STRING("TIMESTAMP WITH TIMEZONE");
  1089. break;
  1090. #endif
  1091. #ifdef SQLT_TIMESTAMP_LTZ
  1092. case SQLT_TIMESTAMP_LTZ:
  1093. RETVAL_STRING("TIMESTAMP WITH LOCAL TIMEZONE");
  1094. break;
  1095. #endif
  1096. #ifdef SQLT_INTERVAL_YM
  1097. case SQLT_INTERVAL_YM:
  1098. RETVAL_STRING("INTERVAL YEAR TO MONTH");
  1099. break;
  1100. #endif
  1101. #ifdef SQLT_INTERVAL_DS
  1102. case SQLT_INTERVAL_DS:
  1103. RETVAL_STRING("INTERVAL DAY TO SECOND");
  1104. break;
  1105. #endif
  1106. case SQLT_DAT:
  1107. RETVAL_STRING("DATE");
  1108. break;
  1109. case SQLT_NUM:
  1110. RETVAL_STRING("NUMBER");
  1111. break;
  1112. case SQLT_LNG:
  1113. RETVAL_STRING("LONG");
  1114. break;
  1115. case SQLT_BIN:
  1116. RETVAL_STRING("RAW");
  1117. break;
  1118. case SQLT_LBI:
  1119. RETVAL_STRING("LONG RAW");
  1120. break;
  1121. case SQLT_CHR:
  1122. RETVAL_STRING("VARCHAR2");
  1123. break;
  1124. case SQLT_RSET:
  1125. RETVAL_STRING("REFCURSOR");
  1126. break;
  1127. case SQLT_AFC:
  1128. RETVAL_STRING("CHAR");
  1129. break;
  1130. case SQLT_BLOB:
  1131. RETVAL_STRING("BLOB");
  1132. break;
  1133. case SQLT_CLOB:
  1134. RETVAL_STRING("CLOB");
  1135. break;
  1136. case SQLT_BFILE:
  1137. RETVAL_STRING("BFILE");
  1138. break;
  1139. case SQLT_RDD:
  1140. RETVAL_STRING("ROWID");
  1141. break;
  1142. default:
  1143. RETVAL_LONG(column->data_type);
  1144. }
  1145. }
  1146. /* }}} */
  1147. /* {{{ proto int oci_field_type_raw(resource stmt, mixed col)
  1148. Tell the raw oracle data type of a column */
  1149. PHP_FUNCTION(oci_field_type_raw)
  1150. {
  1151. php_oci_out_column *column;
  1152. column = php_oci_statement_get_column_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  1153. if (column) {
  1154. RETURN_LONG(column->data_type);
  1155. }
  1156. RETURN_FALSE;
  1157. }
  1158. /* }}} */
  1159. /* {{{ proto bool oci_field_is_null(resource stmt, mixed col)
  1160. Tell whether a field in the current row is NULL */
  1161. PHP_FUNCTION(oci_field_is_null)
  1162. {
  1163. php_oci_out_column *column;
  1164. if ( ( column = php_oci_statement_get_column_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0) ) ) {
  1165. if (column->indicator == -1) {
  1166. RETURN_TRUE;
  1167. }
  1168. }
  1169. RETURN_FALSE;
  1170. }
  1171. /* }}} */
  1172. /* {{{ proto void oci_internal_debug(int onoff)
  1173. Toggle internal debugging output for the OCI extension */
  1174. PHP_FUNCTION(oci_internal_debug)
  1175. {
  1176. /* NOP in OCI8 2.0. Obsoleted by DTrace probes */
  1177. }
  1178. /* }}} */
  1179. /* {{{ proto bool oci_execute(resource stmt [, int mode])
  1180. Execute a parsed statement */
  1181. PHP_FUNCTION(oci_execute)
  1182. {
  1183. zval *z_statement;
  1184. php_oci_statement *statement;
  1185. zend_long mode = OCI_COMMIT_ON_SUCCESS;
  1186. ZEND_PARSE_PARAMETERS_START(1, 2)
  1187. Z_PARAM_RESOURCE(z_statement)
  1188. Z_PARAM_OPTIONAL
  1189. Z_PARAM_LONG(mode)
  1190. ZEND_PARSE_PARAMETERS_END();
  1191. PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
  1192. if (php_oci_statement_execute(statement, (ub4) mode)) {
  1193. RETURN_FALSE;
  1194. }
  1195. RETURN_TRUE;
  1196. }
  1197. /* }}} */
  1198. /* {{{ proto bool oci_cancel(resource stmt)
  1199. Cancel reading from a cursor */
  1200. PHP_FUNCTION(oci_cancel)
  1201. {
  1202. zval *z_statement;
  1203. php_oci_statement *statement;
  1204. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &z_statement) == FAILURE) {
  1205. return;
  1206. }
  1207. PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
  1208. if (php_oci_statement_cancel(statement)) {
  1209. RETURN_FALSE;
  1210. }
  1211. RETURN_TRUE;
  1212. }
  1213. /* }}} */
  1214. /* {{{ proto bool oci_fetch(resource stmt)
  1215. Prepare a new row of data for reading */
  1216. PHP_FUNCTION(oci_fetch)
  1217. {
  1218. zval *z_statement;
  1219. php_oci_statement *statement;
  1220. ub4 nrows = 1; /* only one row at a time is supported for now */
  1221. ZEND_PARSE_PARAMETERS_START(1, 1)
  1222. Z_PARAM_RESOURCE(z_statement)
  1223. ZEND_PARSE_PARAMETERS_END();
  1224. PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
  1225. if (php_oci_statement_fetch(statement, nrows)) {
  1226. RETURN_FALSE;
  1227. }
  1228. RETURN_TRUE;
  1229. }
  1230. /* }}} */
  1231. /* {{{ proto int ocifetchinto(resource stmt, array &output [, int mode])
  1232. Fetch a row of result data into an array */
  1233. PHP_FUNCTION(ocifetchinto)
  1234. {
  1235. php_oci_fetch_row(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_OCI_NUM, 3);
  1236. }
  1237. /* }}} */
  1238. /* {{{ proto int oci_fetch_all(resource stmt, array &output[, int skip[, int maxrows[, int flags]]])
  1239. Fetch all rows of result data into an array */
  1240. PHP_FUNCTION(oci_fetch_all)
  1241. {
  1242. zval *z_statement, *array;
  1243. zval element, tmp;
  1244. php_oci_statement *statement;
  1245. php_oci_out_column **columns;
  1246. zval **outarrs;
  1247. ub4 nrows = 1;
  1248. int i;
  1249. zend_long rows = 0, flags = 0, skip = 0, maxrows = -1;
  1250. ZEND_PARSE_PARAMETERS_START(2, 5)
  1251. Z_PARAM_RESOURCE(z_statement)
  1252. Z_PARAM_ZVAL_DEREF_EX(array, 0, 1)
  1253. Z_PARAM_OPTIONAL
  1254. Z_PARAM_LONG(skip)
  1255. Z_PARAM_LONG(maxrows)
  1256. Z_PARAM_LONG(flags)
  1257. ZEND_PARSE_PARAMETERS_END();
  1258. PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
  1259. zval_ptr_dtor(array);
  1260. while (skip--) {
  1261. if (php_oci_statement_fetch(statement, nrows)) {
  1262. array_init(array);
  1263. RETURN_LONG(0);
  1264. }
  1265. }
  1266. if (flags & PHP_OCI_FETCHSTATEMENT_BY_ROW) {
  1267. /* Fetch by Row: array will contain one sub-array per query row */
  1268. array_init(array);
  1269. columns = safe_emalloc(statement->ncolumns, sizeof(php_oci_out_column *), 0);
  1270. for (i = 0; i < statement->ncolumns; i++) {
  1271. columns[ i ] = php_oci_statement_get_column(statement, i + 1, NULL, 0);
  1272. }
  1273. while (!php_oci_statement_fetch(statement, nrows)) {
  1274. zval row;
  1275. array_init_size(&row, statement->ncolumns);
  1276. for (i = 0; i < statement->ncolumns; i++) {
  1277. php_oci_column_to_zval(columns[ i ], &element, PHP_OCI_RETURN_LOBS);
  1278. if (flags & PHP_OCI_NUM) {
  1279. zend_hash_next_index_insert(Z_ARRVAL(row), &element);
  1280. } else { /* default to ASSOC */
  1281. zend_string *zvtmp;
  1282. zvtmp = zend_string_init(columns[ i ]->name, columns[ i ]->name_len, 0);
  1283. zend_symtable_update(Z_ARRVAL(row), zvtmp, &element);
  1284. #if PHP_VERSION_ID < 70300
  1285. zend_string_release(zvtmp);
  1286. #else
  1287. zend_string_release_ex(zvtmp, 0);
  1288. #endif
  1289. }
  1290. }
  1291. zend_hash_next_index_insert(Z_ARRVAL_P(array), &row);
  1292. rows++;
  1293. if (maxrows != -1 && rows == maxrows) {
  1294. php_oci_statement_cancel(statement);
  1295. break;
  1296. }
  1297. }
  1298. efree(columns);
  1299. } else { /* default to BY_COLUMN */
  1300. /* Fetch by columns: array will contain one sub-array per query column */
  1301. array_init_size(array, statement->ncolumns);
  1302. columns = safe_emalloc(statement->ncolumns, sizeof(php_oci_out_column *), 0);
  1303. outarrs = safe_emalloc(statement->ncolumns, sizeof(zval*), 0);
  1304. if (flags & PHP_OCI_NUM) {
  1305. for (i = 0; i < statement->ncolumns; i++) {
  1306. columns[ i ] = php_oci_statement_get_column(statement, i + 1, NULL, 0);
  1307. array_init(&tmp);
  1308. outarrs[ i ] = zend_hash_next_index_insert(Z_ARRVAL_P(array), &tmp);
  1309. }
  1310. } else { /* default to ASSOC */
  1311. for (i = 0; i < statement->ncolumns; i++) {
  1312. zend_string *zvtmp;
  1313. columns[ i ] = php_oci_statement_get_column(statement, i + 1, NULL, 0);
  1314. array_init(&tmp);
  1315. zvtmp = zend_string_init(columns[ i ]->name, columns[ i ]->name_len, 0);
  1316. outarrs[ i ] = zend_symtable_update(Z_ARRVAL_P(array), zvtmp, &tmp);
  1317. #if PHP_VERSION_ID < 70300
  1318. zend_string_release(zvtmp);
  1319. #else
  1320. zend_string_release_ex(zvtmp, 0);
  1321. #endif
  1322. }
  1323. }
  1324. while (!php_oci_statement_fetch(statement, nrows)) {
  1325. for (i = 0; i < statement->ncolumns; i++) {
  1326. php_oci_column_to_zval(columns[ i ], &element, PHP_OCI_RETURN_LOBS);
  1327. zend_hash_index_update(Z_ARRVAL_P(outarrs[ i ]), rows, &element);
  1328. }
  1329. rows++;
  1330. if (maxrows != -1 && rows == maxrows) {
  1331. php_oci_statement_cancel(statement);
  1332. break;
  1333. }
  1334. }
  1335. efree(columns);
  1336. efree(outarrs);
  1337. }
  1338. RETURN_LONG(rows);
  1339. }
  1340. /* }}} */
  1341. /* {{{ proto object oci_fetch_object( resource stmt )
  1342. Fetch a result row as an object */
  1343. PHP_FUNCTION(oci_fetch_object)
  1344. {
  1345. php_oci_fetch_row(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_OCI_ASSOC | PHP_OCI_RETURN_NULLS, 2);
  1346. if (Z_TYPE_P(return_value) == IS_ARRAY) {
  1347. object_and_properties_init(return_value, ZEND_STANDARD_CLASS_DEF_PTR, Z_ARRVAL_P(return_value));
  1348. }
  1349. }
  1350. /* }}} */
  1351. /* {{{ proto array oci_fetch_row( resource stmt )
  1352. Fetch a result row as an enumerated array */
  1353. PHP_FUNCTION(oci_fetch_row)
  1354. {
  1355. php_oci_fetch_row(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_OCI_NUM | PHP_OCI_RETURN_NULLS, 1);
  1356. }
  1357. /* }}} */
  1358. /* {{{ proto array oci_fetch_assoc( resource stmt )
  1359. Fetch a result row as an associative array */
  1360. PHP_FUNCTION(oci_fetch_assoc)
  1361. {
  1362. php_oci_fetch_row(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_OCI_ASSOC | PHP_OCI_RETURN_NULLS, 1);
  1363. }
  1364. /* }}} */
  1365. /* {{{ proto array oci_fetch_array( resource stmt [, int mode ])
  1366. Fetch a result row as an array */
  1367. PHP_FUNCTION(oci_fetch_array)
  1368. {
  1369. php_oci_fetch_row(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_OCI_BOTH | PHP_OCI_RETURN_NULLS, 2);
  1370. }
  1371. /* }}} */
  1372. /* {{{ proto bool oci_free_statement(resource stmt)
  1373. Free all resources associated with a statement */
  1374. PHP_FUNCTION(oci_free_statement)
  1375. {
  1376. zval *z_statement;
  1377. php_oci_statement *statement;
  1378. ZEND_PARSE_PARAMETERS_START(1, 1)
  1379. Z_PARAM_RESOURCE(z_statement)
  1380. ZEND_PARSE_PARAMETERS_END();
  1381. PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
  1382. zend_list_close(statement->id);
  1383. RETURN_TRUE;
  1384. }
  1385. /* }}} */
  1386. /* {{{ proto bool oci_close(resource connection)
  1387. Disconnect from database */
  1388. PHP_FUNCTION(oci_close)
  1389. {
  1390. /* oci_close for pconnect (if old_oci_close_semantics not set) would
  1391. * release the connection back to the client-side session pool (and to the
  1392. * server-side pool if Database Resident Connection Pool is being used).
  1393. * Subsequent pconnects in the same script are not guaranteed to get the
  1394. * same database session.
  1395. */
  1396. zval *z_connection;
  1397. php_oci_connection *connection;
  1398. if (OCI_G(old_oci_close_semantics)) {
  1399. /* do nothing to keep BC */
  1400. return;
  1401. }
  1402. ZEND_PARSE_PARAMETERS_START(1, 1)
  1403. Z_PARAM_RESOURCE(z_connection)
  1404. ZEND_PARSE_PARAMETERS_END();
  1405. PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
  1406. if (GC_REFCOUNT(connection->id) == 2) { /* CHANGED VERSION::PHP7
  1407. Changed the refCount to 2 since
  1408. internally Zend engine increments
  1409. RefCount value by 1 */
  1410. /* Unregister Oracle TAF */
  1411. php_oci_unregister_taf_callback(connection);
  1412. zend_list_close(connection->id);
  1413. }
  1414. /* ZVAL_NULL(z_connection); */
  1415. RETURN_TRUE;
  1416. }
  1417. /* }}} */
  1418. /* {{{ proto resource oci_new_connect(string user, string pass [, string db, string charset [, int session_mode ]])
  1419. Connect to an Oracle database and log on. Returns a new session. */
  1420. PHP_FUNCTION(oci_new_connect)
  1421. {
  1422. php_oci_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1);
  1423. }
  1424. /* }}} */
  1425. /* {{{ proto resource oci_connect(string user, string pass [, string db [, string charset [, int session_mode ]])
  1426. Connect to an Oracle database and log on. Returns a new session. */
  1427. PHP_FUNCTION(oci_connect)
  1428. {
  1429. php_oci_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
  1430. }
  1431. /* }}} */
  1432. /* {{{ proto resource oci_pconnect(string user, string pass [, string db [, string charset [, int session_mode ]])
  1433. Connect to an Oracle database using a persistent connection and log on. Returns a new session. */
  1434. PHP_FUNCTION(oci_pconnect)
  1435. {
  1436. php_oci_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, 0);
  1437. }
  1438. /* }}} */
  1439. /* {{{ proto array oci_error([resource stmt|connection|global])
  1440. Return the last error of stmt|connection|global. If no error happened returns false. */
  1441. PHP_FUNCTION(oci_error)
  1442. {
  1443. zval *arg = NULL;
  1444. php_oci_statement *statement;
  1445. php_oci_connection *connection;
  1446. text errbuf[PHP_OCI_ERRBUF_LEN];
  1447. sb4 errcode = 0;
  1448. dvoid *errh = NULL;
  1449. ub2 error_offset = 0;
  1450. text *sqltext = NULL;
  1451. ZEND_PARSE_PARAMETERS_START(0, 1)
  1452. Z_PARAM_OPTIONAL
  1453. Z_PARAM_RESOURCE(arg)
  1454. ZEND_PARSE_PARAMETERS_END();
  1455. if (ZEND_NUM_ARGS() > 0) {
  1456. statement = (php_oci_statement *) zend_fetch_resource_ex(arg, NULL, le_statement);
  1457. if (statement) {
  1458. errh = statement->err;
  1459. errcode = statement->errcode;
  1460. if (php_oci_fetch_sqltext_offset(statement, &sqltext, &error_offset)) {
  1461. RETURN_FALSE;
  1462. }
  1463. goto go_out;
  1464. }
  1465. connection = (php_oci_connection *) zend_fetch_resource_ex(arg, NULL, le_connection);
  1466. if (connection) {
  1467. errh = connection->err;
  1468. errcode = connection->errcode;
  1469. goto go_out;
  1470. }
  1471. connection = (php_oci_connection *) zend_fetch_resource_ex(arg, NULL, le_pconnection);
  1472. if (connection) {
  1473. errh = connection->err;
  1474. errcode = connection->errcode;
  1475. goto go_out;
  1476. }
  1477. } else {
  1478. errh = OCI_G(err);
  1479. errcode = OCI_G(errcode);
  1480. }
  1481. go_out:
  1482. if (errcode == 0) { /* no error set in the handle */
  1483. RETURN_FALSE;
  1484. }
  1485. if (!errh) {
  1486. php_error_docref(NULL, E_WARNING, "Oci_error: unable to find error handle");
  1487. RETURN_FALSE;
  1488. }
  1489. errcode = php_oci_fetch_errmsg(errh, errbuf, sizeof(errbuf));
  1490. if (errcode) {
  1491. array_init(return_value);
  1492. add_assoc_long(return_value, "code", errcode);
  1493. /* TODO: avoid reallocation ??? */
  1494. add_assoc_string(return_value, "message", (char*) errbuf);
  1495. add_assoc_long(return_value, "offset", error_offset);
  1496. add_assoc_string(return_value, "sqltext", sqltext ? (char *) sqltext : "");
  1497. } else {
  1498. RETURN_FALSE;
  1499. }
  1500. }
  1501. /* }}} */
  1502. /* {{{ proto int oci_num_fields(resource stmt)
  1503. Return the number of result columns in a statement */
  1504. PHP_FUNCTION(oci_num_fields)
  1505. {
  1506. zval *z_statement;
  1507. php_oci_statement *statement;
  1508. ZEND_PARSE_PARAMETERS_START(1, 1)
  1509. Z_PARAM_RESOURCE(z_statement)
  1510. ZEND_PARSE_PARAMETERS_END();
  1511. PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
  1512. RETURN_LONG(statement->ncolumns);
  1513. }
  1514. /* }}} */
  1515. /* {{{ proto resource oci_parse(resource connection, string statement)
  1516. Parse a SQL or PL/SQL statement and return a statement resource */
  1517. PHP_FUNCTION(oci_parse)
  1518. {
  1519. zval *z_connection;
  1520. php_oci_connection *connection;
  1521. php_oci_statement *statement;
  1522. char *query;
  1523. size_t query_len;
  1524. ZEND_PARSE_PARAMETERS_START(2, 2)
  1525. Z_PARAM_RESOURCE(z_connection)
  1526. Z_PARAM_STRING(query, query_len)
  1527. ZEND_PARSE_PARAMETERS_END();
  1528. PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
  1529. statement = php_oci_statement_create(connection, query, (int) query_len);
  1530. if (statement) {
  1531. RETURN_RES(statement->id);
  1532. }
  1533. RETURN_FALSE;
  1534. }
  1535. /* }}} */
  1536. /* {{{ proto bool oci_set_prefetch(resource stmt, int prefetch_rows)
  1537. Sets the number of rows to be prefetched on execute to prefetch_rows for stmt */
  1538. PHP_FUNCTION(oci_set_prefetch)
  1539. {
  1540. zval *z_statement;
  1541. php_oci_statement *statement;
  1542. zend_long size;
  1543. ZEND_PARSE_PARAMETERS_START(2, 2)
  1544. Z_PARAM_RESOURCE(z_statement)
  1545. Z_PARAM_LONG(size)
  1546. ZEND_PARSE_PARAMETERS_END();
  1547. PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
  1548. if (size < 0) {
  1549. php_error_docref(NULL, E_WARNING, "Number of rows to be prefetched has to be greater than or equal to 0");
  1550. return;
  1551. }
  1552. if (php_oci_statement_set_prefetch(statement, (ub4)size)) {
  1553. RETURN_FALSE;
  1554. }
  1555. RETURN_TRUE;
  1556. }
  1557. /* }}} */
  1558. /* {{{ proto bool oci_set_client_identifier(resource connection, string value)
  1559. Sets the client identifier attribute on the connection */
  1560. PHP_FUNCTION(oci_set_client_identifier)
  1561. {
  1562. zval *z_connection;
  1563. php_oci_connection *connection;
  1564. char *client_id;
  1565. size_t client_id_len;
  1566. sword errstatus;
  1567. ZEND_PARSE_PARAMETERS_START(2, 2)
  1568. Z_PARAM_RESOURCE(z_connection)
  1569. Z_PARAM_STRING(client_id, client_id_len)
  1570. ZEND_PARSE_PARAMETERS_END();
  1571. PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
  1572. PHP_OCI_CALL_RETURN(errstatus, OCIAttrSet, ((dvoid *) connection->session, (ub4) OCI_HTYPE_SESSION, (dvoid *) client_id, (ub4) client_id_len, (ub4) OCI_ATTR_CLIENT_IDENTIFIER, connection->err));
  1573. if (errstatus != OCI_SUCCESS) {
  1574. connection->errcode = php_oci_error(connection->err, errstatus);
  1575. RETURN_FALSE;
  1576. }
  1577. #ifdef HAVE_OCI8_DTRACE
  1578. /* The alternatives to storing client_id like done below are
  1579. i) display it in a probe here in oci_set_client_identifier and
  1580. let the user D script correlate the connection address probe
  1581. argument and the client_id. This would likely require user D
  1582. script variables, which would use kernel memory.
  1583. ii) call OCIAttrGet for each probe definition that uses
  1584. client_id. This would be slower than storing it.
  1585. */
  1586. if (connection->client_id) {
  1587. pefree(connection->client_id, connection->is_persistent);
  1588. }
  1589. if (client_id) {
  1590. /* this long winded copy allows compatibility with older PHP versions */
  1591. connection->client_id = (char *)pemalloc(client_id_len+1, connection->is_persistent);
  1592. memcpy(connection->client_id, client_id, client_id_len);
  1593. connection->client_id[client_id_len] = '\0';
  1594. } else {
  1595. connection->client_id = NULL;
  1596. }
  1597. #endif /* HAVE_OCI8_DTRACE */
  1598. RETURN_TRUE;
  1599. }
  1600. /* }}} */
  1601. /* {{{ proto bool oci_set_edition(string value)
  1602. Sets the edition attribute for all subsequent connections created */
  1603. PHP_FUNCTION(oci_set_edition)
  1604. {
  1605. #if ((OCI_MAJOR_VERSION > 11) || ((OCI_MAJOR_VERSION == 11) && (OCI_MINOR_VERSION >= 2)))
  1606. char *edition;
  1607. size_t edition_len;
  1608. ZEND_PARSE_PARAMETERS_START(1, 1)
  1609. Z_PARAM_STRING(edition, edition_len)
  1610. ZEND_PARSE_PARAMETERS_END();
  1611. if (OCI_G(edition)) {
  1612. efree(OCI_G(edition));
  1613. }
  1614. if (edition) {
  1615. OCI_G(edition) = (char *)safe_emalloc(edition_len+1, sizeof(char), 0);
  1616. memcpy(OCI_G(edition), edition, edition_len);
  1617. OCI_G(edition)[edition_len] = '\0';
  1618. } else {
  1619. OCI_G(edition) = NULL;
  1620. }
  1621. RETURN_TRUE;
  1622. #else
  1623. php_error_docref(NULL, E_NOTICE, "Unsupported attribute type");
  1624. RETURN_FALSE;
  1625. #endif
  1626. }
  1627. /* }}} */
  1628. /* {{{ proto bool oci_set_module_name(resource connection, string value)
  1629. Sets the module attribute on the connection for end-to-end tracing */
  1630. PHP_FUNCTION(oci_set_module_name)
  1631. {
  1632. #if (OCI_MAJOR_VERSION >= 10)
  1633. zval *z_connection;
  1634. php_oci_connection *connection;
  1635. char *module;
  1636. size_t module_len;
  1637. sword errstatus;
  1638. ZEND_PARSE_PARAMETERS_START(2, 2)
  1639. Z_PARAM_RESOURCE(z_connection)
  1640. Z_PARAM_STRING(module, module_len)
  1641. ZEND_PARSE_PARAMETERS_END();
  1642. PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
  1643. PHP_OCI_CALL_RETURN(errstatus, OCIAttrSet, ((dvoid *) connection->session, (ub4) OCI_HTYPE_SESSION, (dvoid *) module, (ub4) module_len, (ub4) OCI_ATTR_MODULE, connection->err));
  1644. if (errstatus != OCI_SUCCESS) {
  1645. connection->errcode = php_oci_error(connection->err, errstatus);
  1646. RETURN_FALSE;
  1647. }
  1648. RETURN_TRUE;
  1649. #else
  1650. php_error_docref(NULL, E_NOTICE, "Unsupported attribute type");
  1651. RETURN_FALSE;
  1652. #endif
  1653. }
  1654. /* }}} */
  1655. /* {{{ proto bool oci_set_action(resource connection, string value)
  1656. Sets the action attribute on the connection for end-to-end tracing */
  1657. PHP_FUNCTION(oci_set_action)
  1658. {
  1659. #if (OCI_MAJOR_VERSION >= 10)
  1660. zval *z_connection;
  1661. php_oci_connection *connection;
  1662. char *action;
  1663. size_t action_len;
  1664. sword errstatus;
  1665. ZEND_PARSE_PARAMETERS_START(2, 2)
  1666. Z_PARAM_RESOURCE(z_connection)
  1667. Z_PARAM_STRING(action, action_len)
  1668. ZEND_PARSE_PARAMETERS_END();
  1669. PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
  1670. PHP_OCI_CALL_RETURN(errstatus, OCIAttrSet, ((dvoid *) connection->session, (ub4) OCI_HTYPE_SESSION, (dvoid *) action, (ub4) action_len, (ub4) OCI_ATTR_ACTION, connection->err));
  1671. if (errstatus != OCI_SUCCESS) {
  1672. connection->errcode = php_oci_error(connection->err, errstatus);
  1673. RETURN_FALSE;
  1674. }
  1675. RETURN_TRUE;
  1676. #else
  1677. php_error_docref(NULL, E_NOTICE, "Unsupported attribute type");
  1678. RETURN_FALSE;
  1679. #endif
  1680. }
  1681. /* }}} */
  1682. /* {{{ proto bool oci_set_client_info(resource connection, string value)
  1683. Sets the client info attribute on the connection for end-to-end tracing */
  1684. PHP_FUNCTION(oci_set_client_info)
  1685. {
  1686. #if (OCI_MAJOR_VERSION >= 10)
  1687. zval *z_connection;
  1688. php_oci_connection *connection;
  1689. char *client_info;
  1690. size_t client_info_len;
  1691. sword errstatus;
  1692. ZEND_PARSE_PARAMETERS_START(2, 2)
  1693. Z_PARAM_RESOURCE(z_connection)
  1694. Z_PARAM_STRING(client_info, client_info_len)
  1695. ZEND_PARSE_PARAMETERS_END();
  1696. PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
  1697. PHP_OCI_CALL_RETURN(errstatus, OCIAttrSet, ((dvoid *) connection->session, (ub4) OCI_HTYPE_SESSION, (dvoid *) client_info, (ub4) client_info_len, (ub4) OCI_ATTR_CLIENT_INFO, connection->err));
  1698. if (errstatus != OCI_SUCCESS) {
  1699. connection->errcode = php_oci_error(connection->err, errstatus);
  1700. RETURN_FALSE;
  1701. }
  1702. RETURN_TRUE;
  1703. #else
  1704. php_error_docref(NULL, E_NOTICE, "Unsupported attribute type");
  1705. RETURN_FALSE;
  1706. #endif
  1707. }
  1708. /* }}} */
  1709. /* {{{ proto bool oci_set_db_operation(resource connection, string value)
  1710. Sets the "DB operation" on the connection for Oracle end-to-end tracing.
  1711. For history, see Oracle bug 16695981 */
  1712. PHP_FUNCTION(oci_set_db_operation)
  1713. {
  1714. #if (OCI_MAJOR_VERSION > 11)
  1715. zval *z_connection;
  1716. php_oci_connection *connection;
  1717. char *dbop_name;
  1718. size_t dbop_name_len;
  1719. ZEND_PARSE_PARAMETERS_START(2, 2)
  1720. Z_PARAM_RESOURCE(z_connection)
  1721. Z_PARAM_STRING(dbop_name, dbop_name_len)
  1722. ZEND_PARSE_PARAMETERS_END();
  1723. PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
  1724. PHP_OCI_CALL_RETURN(OCI_G(errcode), OCIAttrSet, ((dvoid *) connection->session, (ub4) OCI_HTYPE_SESSION, (dvoid *) dbop_name, (ub4) dbop_name_len, (ub4) OCI_ATTR_DBOP, OCI_G(err)));
  1725. if (OCI_G(errcode) != OCI_SUCCESS) {
  1726. php_oci_error(OCI_G(err), OCI_G(errcode));
  1727. RETURN_FALSE;
  1728. }
  1729. RETURN_TRUE;
  1730. #else
  1731. php_error_docref(NULL, E_NOTICE, "Unsupported attribute type");
  1732. RETURN_FALSE;
  1733. #endif
  1734. }
  1735. /* }}} */
  1736. /* {{{ proto bool oci_set_call_timeout(resource connection, int call_timeout)
  1737. */
  1738. PHP_FUNCTION(oci_set_call_timeout)
  1739. {
  1740. #if (OCI_MAJOR_VERSION >= 18)
  1741. zval *z_connection;
  1742. php_oci_connection *connection;
  1743. zend_long call_timeout; // milliseconds
  1744. ZEND_PARSE_PARAMETERS_START(2, 2)
  1745. Z_PARAM_RESOURCE(z_connection)
  1746. Z_PARAM_LONG(call_timeout)
  1747. ZEND_PARSE_PARAMETERS_END();
  1748. PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
  1749. PHP_OCI_CALL_RETURN(OCI_G(errcode), OCIAttrSet, ((dvoid *) connection->svc, (ub4) OCI_HTYPE_SVCCTX, (ub4 *) &call_timeout, 0, OCI_ATTR_CALL_TIMEOUT, OCI_G(err)));
  1750. if (OCI_G(errcode) != OCI_SUCCESS) {
  1751. php_oci_error(OCI_G(err), OCI_G(errcode));
  1752. RETURN_FALSE;
  1753. }
  1754. RETURN_TRUE;
  1755. #else
  1756. php_error_docref(NULL, E_NOTICE, "Unsupported with this version of Oracle Client");
  1757. RETURN_FALSE;
  1758. #endif
  1759. }
  1760. /* }}} */
  1761. /* {{{ proto bool oci_password_change(resource connection, string username, string old_password, string new_password)
  1762. Changes the password of an account */
  1763. PHP_FUNCTION(oci_password_change)
  1764. {
  1765. zval *z_connection;
  1766. char *user, *pass_old, *pass_new, *dbname;
  1767. size_t user_len, pass_old_len, pass_new_len, dbname_len;
  1768. php_oci_connection *connection;
  1769. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "rsss", &z_connection, &user, &user_len, &pass_old, &pass_old_len, &pass_new, &pass_new_len) == SUCCESS) {
  1770. PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
  1771. if (!user_len) {
  1772. php_error_docref(NULL, E_WARNING, "username cannot be empty");
  1773. RETURN_FALSE;
  1774. }
  1775. if (!pass_old_len) {
  1776. php_error_docref(NULL, E_WARNING, "old password cannot be empty");
  1777. RETURN_FALSE;
  1778. }
  1779. if (!pass_new_len) {
  1780. php_error_docref(NULL, E_WARNING, "new password cannot be empty");
  1781. RETURN_FALSE;
  1782. }
  1783. if (php_oci_password_change(connection, user, (int) user_len, pass_old, (int) pass_old_len, pass_new, (int) pass_new_len)) {
  1784. RETURN_FALSE;
  1785. }
  1786. RETURN_TRUE;
  1787. } else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "ssss", &dbname, &dbname_len, &user, &user_len, &pass_old, &pass_old_len, &pass_new, &pass_new_len) == SUCCESS) {
  1788. if (!user_len) {
  1789. php_error_docref(NULL, E_WARNING, "username cannot be empty");
  1790. RETURN_FALSE;
  1791. }
  1792. if (!pass_old_len) {
  1793. php_error_docref(NULL, E_WARNING, "old password cannot be empty");
  1794. RETURN_FALSE;
  1795. }
  1796. if (!pass_new_len) {
  1797. php_error_docref(NULL, E_WARNING, "new password cannot be empty");
  1798. RETURN_FALSE;
  1799. }
  1800. connection = php_oci_do_connect_ex(user, (int) user_len, pass_old, (int) pass_old_len, pass_new, (int) pass_new_len, dbname, (int) dbname_len, NULL, OCI_DEFAULT, 0, 0);
  1801. if (!connection) {
  1802. RETURN_FALSE;
  1803. }
  1804. RETURN_RES(connection->id);
  1805. }
  1806. WRONG_PARAM_COUNT;
  1807. }
  1808. /* }}} */
  1809. /* {{{ proto resource oci_new_cursor(resource connection)
  1810. Return a new cursor (Statement-Handle) - use this to bind ref-cursors! */
  1811. PHP_FUNCTION(oci_new_cursor)
  1812. {
  1813. zval *z_connection;
  1814. php_oci_connection *connection;
  1815. php_oci_statement *statement;
  1816. ZEND_PARSE_PARAMETERS_START(1, 1)
  1817. Z_PARAM_RESOURCE(z_connection)
  1818. ZEND_PARSE_PARAMETERS_END();
  1819. PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
  1820. statement = php_oci_statement_create(connection, NULL, 0);
  1821. if (statement) {
  1822. RETURN_RES(statement->id);
  1823. }
  1824. RETURN_FALSE;
  1825. }
  1826. /* }}} */
  1827. /* {{{ proto string oci_result(resource stmt, mixed column)
  1828. Return a single column of result data */
  1829. PHP_FUNCTION(oci_result)
  1830. {
  1831. php_oci_out_column *column;
  1832. column = php_oci_statement_get_column_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  1833. if(column) {
  1834. php_oci_column_to_zval(column, return_value, 0);
  1835. }
  1836. else {
  1837. RETURN_FALSE;
  1838. }
  1839. }
  1840. /* }}} */
  1841. /* {{{ proto string oci_client_version()
  1842. Return a string containing runtime client library version information */
  1843. PHP_FUNCTION(oci_client_version)
  1844. {
  1845. char version[256];
  1846. php_oci_client_get_version(version, sizeof(version));
  1847. RETURN_STRING(version);
  1848. }
  1849. /* }}} */
  1850. /* {{{ proto string oci_server_version(resource connection)
  1851. Return a string containing server version information */
  1852. PHP_FUNCTION(oci_server_version)
  1853. {
  1854. zval *z_connection;
  1855. php_oci_connection *connection;
  1856. char version[256];
  1857. zend_string *ret;
  1858. ZEND_PARSE_PARAMETERS_START(1, 1)
  1859. Z_PARAM_RESOURCE(z_connection)
  1860. ZEND_PARSE_PARAMETERS_END();
  1861. PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
  1862. if (php_oci_server_get_version(connection, version, sizeof(version))) {
  1863. RETURN_FALSE;
  1864. }
  1865. ret = zend_string_init(version, strlen(version), 0);
  1866. RETURN_STR(ret);
  1867. }
  1868. /* }}} */
  1869. /* {{{ proto string oci_statement_type(resource stmt)
  1870. Return the query type of an OCI statement */
  1871. PHP_FUNCTION(oci_statement_type)
  1872. {
  1873. zval *z_statement;
  1874. php_oci_statement *statement;
  1875. ub2 type;
  1876. ZEND_PARSE_PARAMETERS_START(1, 1)
  1877. Z_PARAM_RESOURCE(z_statement)
  1878. ZEND_PARSE_PARAMETERS_END();
  1879. PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
  1880. if (php_oci_statement_get_type(statement, &type)) {
  1881. RETURN_FALSE;
  1882. }
  1883. switch (type) {
  1884. case OCI_STMT_SELECT:
  1885. RETVAL_STRING("SELECT");
  1886. break;
  1887. case OCI_STMT_UPDATE:
  1888. RETVAL_STRING("UPDATE");
  1889. break;
  1890. case OCI_STMT_DELETE:
  1891. RETVAL_STRING("DELETE");
  1892. break;
  1893. case OCI_STMT_INSERT:
  1894. RETVAL_STRING("INSERT");
  1895. break;
  1896. case OCI_STMT_CREATE:
  1897. RETVAL_STRING("CREATE");
  1898. break;
  1899. case OCI_STMT_DROP:
  1900. RETVAL_STRING("DROP");
  1901. break;
  1902. case OCI_STMT_ALTER:
  1903. RETVAL_STRING("ALTER");
  1904. break;
  1905. case OCI_STMT_BEGIN:
  1906. RETVAL_STRING("BEGIN");
  1907. break;
  1908. case OCI_STMT_DECLARE:
  1909. RETVAL_STRING("DECLARE");
  1910. break;
  1911. case OCI_STMT_CALL:
  1912. RETVAL_STRING("CALL");
  1913. break;
  1914. default:
  1915. RETVAL_STRING("UNKNOWN");
  1916. }
  1917. }
  1918. /* }}} */
  1919. /* {{{ proto int oci_num_rows(resource stmt)
  1920. Return the row count of an OCI statement */
  1921. PHP_FUNCTION(oci_num_rows)
  1922. {
  1923. zval *z_statement;
  1924. php_oci_statement *statement;
  1925. ub4 rowcount;
  1926. ZEND_PARSE_PARAMETERS_START(1, 1)
  1927. Z_PARAM_RESOURCE(z_statement)
  1928. ZEND_PARSE_PARAMETERS_END();
  1929. PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
  1930. if (php_oci_statement_get_numrows(statement, &rowcount)) {
  1931. RETURN_FALSE;
  1932. }
  1933. RETURN_LONG(rowcount);
  1934. }
  1935. /* }}} */
  1936. /* {{{ proto bool oci_free_collection()
  1937. Deletes collection object*/
  1938. PHP_FUNCTION(oci_free_collection)
  1939. {
  1940. zval *tmp, *z_collection = getThis();
  1941. php_oci_collection *collection;
  1942. if (!getThis()) {
  1943. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &z_collection, oci_coll_class_entry_ptr) == FAILURE) {
  1944. return;
  1945. }
  1946. }
  1947. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_collection), "collection", sizeof("collection")-1)) == NULL) {
  1948. php_error_docref(NULL, E_WARNING, "Unable to find collection property");
  1949. RETURN_FALSE;
  1950. }
  1951. PHP_OCI_ZVAL_TO_COLLECTION(tmp, collection);
  1952. zend_list_close(collection->id);
  1953. RETURN_TRUE;
  1954. }
  1955. /* }}} */
  1956. /* {{{ proto bool oci_collection_append(string value)
  1957. Append an object to the collection */
  1958. PHP_FUNCTION(oci_collection_append)
  1959. {
  1960. zval *tmp, *z_collection = getThis();
  1961. php_oci_collection *collection;
  1962. char *value;
  1963. size_t value_len;
  1964. if (getThis()) {
  1965. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &value, &value_len) == FAILURE) {
  1966. return;
  1967. }
  1968. }
  1969. else {
  1970. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os", &z_collection, oci_coll_class_entry_ptr, &value, &value_len) == FAILURE) {
  1971. return;
  1972. }
  1973. }
  1974. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_collection), "collection", sizeof("collection")-1)) == NULL) {
  1975. php_error_docref(NULL, E_WARNING, "Unable to find collection property");
  1976. RETURN_FALSE;
  1977. }
  1978. PHP_OCI_ZVAL_TO_COLLECTION(tmp, collection);
  1979. if (php_oci_collection_append(collection, value, (int) value_len)) {
  1980. RETURN_FALSE;
  1981. }
  1982. RETURN_TRUE;
  1983. }
  1984. /* }}} */
  1985. /* {{{ proto string oci_collection_element_get(int ndx)
  1986. Retrieve the value at collection index ndx */
  1987. PHP_FUNCTION(oci_collection_element_get)
  1988. {
  1989. zval *tmp, *z_collection = getThis();
  1990. php_oci_collection *collection;
  1991. zend_long element_index;
  1992. zval value;
  1993. if (getThis()) {
  1994. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &element_index) == FAILURE) {
  1995. return;
  1996. }
  1997. }
  1998. else {
  1999. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &z_collection, oci_coll_class_entry_ptr, &element_index) == FAILURE) {
  2000. return;
  2001. }
  2002. }
  2003. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_collection), "collection", sizeof("collection")-1)) == NULL) {
  2004. php_error_docref(NULL, E_WARNING, "Unable to find collection property");
  2005. RETURN_FALSE;
  2006. }
  2007. PHP_OCI_ZVAL_TO_COLLECTION(tmp, collection);
  2008. if (php_oci_collection_element_get(collection, element_index, &value)) {
  2009. RETURN_FALSE;
  2010. }
  2011. RETURN_ZVAL(&value, 1, 1);
  2012. }
  2013. /* }}} */
  2014. /* {{{ proto bool oci_collection_assign(object from)
  2015. Assign a collection from another existing collection */
  2016. PHP_FUNCTION(oci_collection_assign)
  2017. {
  2018. zval *tmp_dest, *tmp_from, *z_collection_dest = getThis(), *z_collection_from;
  2019. php_oci_collection *collection_dest, *collection_from;
  2020. if (getThis()) {
  2021. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &z_collection_from, oci_coll_class_entry_ptr) == FAILURE) {
  2022. return;
  2023. }
  2024. }
  2025. else {
  2026. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &z_collection_dest, oci_coll_class_entry_ptr, &z_collection_from, oci_coll_class_entry_ptr) == FAILURE) {
  2027. return;
  2028. }
  2029. }
  2030. if ((tmp_dest = zend_hash_str_find(Z_OBJPROP_P(z_collection_dest), "collection", sizeof("collection")-1)) == NULL) {
  2031. php_error_docref(NULL, E_WARNING, "Unable to find collection property. The first argument should be valid collection object");
  2032. RETURN_FALSE;
  2033. }
  2034. if ((tmp_from = zend_hash_str_find(Z_OBJPROP_P(z_collection_from), "collection", sizeof("collection")-1)) == NULL) {
  2035. php_error_docref(NULL, E_WARNING, "Unable to find collection property. The second argument should be valid collection object");
  2036. RETURN_FALSE;
  2037. }
  2038. PHP_OCI_ZVAL_TO_COLLECTION(tmp_dest, collection_dest);
  2039. PHP_OCI_ZVAL_TO_COLLECTION(tmp_from, collection_from);
  2040. if (php_oci_collection_assign(collection_dest, collection_from)) {
  2041. RETURN_FALSE;
  2042. }
  2043. RETURN_TRUE;
  2044. }
  2045. /* }}} */
  2046. /* {{{ proto bool oci_collection_element_assign(int index, string val)
  2047. Assign element val to collection at index ndx */
  2048. PHP_FUNCTION(oci_collection_element_assign)
  2049. {
  2050. zval *tmp, *z_collection = getThis();
  2051. php_oci_collection *collection;
  2052. size_t value_len;
  2053. zend_long element_index;
  2054. char *value;
  2055. if (getThis()) {
  2056. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ls", &element_index, &value, &value_len) == FAILURE) {
  2057. return;
  2058. }
  2059. }
  2060. else {
  2061. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ols", &z_collection, oci_coll_class_entry_ptr, &element_index, &value, &value_len) == FAILURE) {
  2062. return;
  2063. }
  2064. }
  2065. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_collection), "collection", sizeof("collection")-1)) == NULL) {
  2066. php_error_docref(NULL, E_WARNING, "Unable to find collection property");
  2067. RETURN_FALSE;
  2068. }
  2069. PHP_OCI_ZVAL_TO_COLLECTION(tmp, collection);
  2070. if (php_oci_collection_element_set(collection, element_index, value, (int) value_len)) {
  2071. RETURN_FALSE;
  2072. }
  2073. RETURN_TRUE;
  2074. }
  2075. /* }}} */
  2076. /* {{{ proto int oci_collection_size()
  2077. Return the size of a collection */
  2078. PHP_FUNCTION(oci_collection_size)
  2079. {
  2080. zval *tmp, *z_collection = getThis();
  2081. php_oci_collection *collection;
  2082. sb4 size = 0;
  2083. if (!getThis()) {
  2084. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &z_collection, oci_coll_class_entry_ptr) == FAILURE) {
  2085. return;
  2086. }
  2087. }
  2088. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_collection), "collection", sizeof("collection")-1)) == NULL) {
  2089. php_error_docref(NULL, E_WARNING, "Unable to find collection property");
  2090. RETURN_FALSE;
  2091. }
  2092. PHP_OCI_ZVAL_TO_COLLECTION(tmp, collection);
  2093. if (php_oci_collection_size(collection, &size)) {
  2094. RETURN_FALSE;
  2095. }
  2096. RETURN_LONG(size);
  2097. }
  2098. /* }}} */
  2099. /* {{{ proto int oci_collection_max()
  2100. Return the max value of a collection. For a varray this is the maximum length of the array */
  2101. PHP_FUNCTION(oci_collection_max)
  2102. {
  2103. zval *tmp, *z_collection = getThis();
  2104. php_oci_collection *collection;
  2105. zend_long max;
  2106. if (!getThis()) {
  2107. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &z_collection, oci_coll_class_entry_ptr) == FAILURE) {
  2108. return;
  2109. }
  2110. }
  2111. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_collection), "collection", sizeof("collection")-1)) == NULL) {
  2112. php_error_docref(NULL, E_WARNING, "Unable to find collection property");
  2113. RETURN_FALSE;
  2114. }
  2115. PHP_OCI_ZVAL_TO_COLLECTION(tmp, collection);
  2116. if (php_oci_collection_max(collection, &max)) {
  2117. RETURN_FALSE;
  2118. }
  2119. RETURN_LONG(max);
  2120. }
  2121. /* }}} */
  2122. /* {{{ proto bool oci_collection_trim(int num)
  2123. Trim num elements from the end of a collection */
  2124. PHP_FUNCTION(oci_collection_trim)
  2125. {
  2126. zval *tmp, *z_collection = getThis();
  2127. php_oci_collection *collection;
  2128. zend_long trim_size;
  2129. if (getThis()) {
  2130. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &trim_size) == FAILURE) {
  2131. return;
  2132. }
  2133. }
  2134. else {
  2135. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &z_collection, oci_coll_class_entry_ptr, &trim_size) == FAILURE) {
  2136. return;
  2137. }
  2138. }
  2139. if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_collection), "collection", sizeof("collection")-1)) == NULL) {
  2140. php_error_docref(NULL, E_WARNING, "Unable to find collection property");
  2141. RETURN_FALSE;
  2142. }
  2143. PHP_OCI_ZVAL_TO_COLLECTION(tmp, collection);
  2144. if (php_oci_collection_trim(collection, trim_size)) {
  2145. RETURN_FALSE;
  2146. }
  2147. RETURN_TRUE;
  2148. }
  2149. /* }}} */
  2150. /* {{{ proto object oci_new_collection(resource connection, string tdo [, string schema])
  2151. Initialize a new collection */
  2152. PHP_FUNCTION(oci_new_collection)
  2153. {
  2154. zval *z_connection;
  2155. php_oci_connection *connection;
  2156. php_oci_collection *collection;
  2157. char *tdo, *schema = NULL;
  2158. size_t tdo_len, schema_len = 0;
  2159. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|s", &z_connection, &tdo, &tdo_len, &schema, &schema_len) == FAILURE) {
  2160. return;
  2161. }
  2162. PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
  2163. if ( (collection = php_oci_collection_create(connection, tdo, (int) tdo_len, schema, (int) schema_len)) ) {
  2164. object_init_ex(return_value, oci_coll_class_entry_ptr);
  2165. add_property_resource(return_value, "collection", collection->id);
  2166. }
  2167. else {
  2168. RETURN_FALSE;
  2169. }
  2170. }
  2171. /* }}} */
  2172. /* {{{ proto bool oci_get_implicit(resource stmt)
  2173. Get the next statement resource from an Oracle 12c PL/SQL Implicit Result Set */
  2174. PHP_FUNCTION(oci_get_implicit_resultset)
  2175. {
  2176. zval *z_statement;
  2177. php_oci_statement *statement;
  2178. php_oci_statement *imp_statement;
  2179. ZEND_PARSE_PARAMETERS_START(1, 1)
  2180. Z_PARAM_RESOURCE(z_statement)
  2181. ZEND_PARSE_PARAMETERS_END();
  2182. PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
  2183. imp_statement = php_oci_get_implicit_resultset(statement);
  2184. if (imp_statement) {
  2185. if (php_oci_statement_execute(imp_statement, (ub4)OCI_DEFAULT))
  2186. RETURN_FALSE;
  2187. RETURN_RES(imp_statement->id);
  2188. }
  2189. RETURN_FALSE;
  2190. }
  2191. /* }}} */
  2192. #endif /* HAVE_OCI8 */
  2193. /*
  2194. * Local variables:
  2195. * tab-width: 4
  2196. * c-basic-offset: 4
  2197. * End:
  2198. * vim600: noet sw=4 ts=4 fdm=marker
  2199. * vim<600: noet sw=4 ts=4
  2200. */