PageRenderTime 64ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/oci8/oci8_lob.c

http://github.com/infusion/PHP
C | 953 lines | 690 code | 160 blank | 103 comment | 153 complexity | aa54effa94e0d6e17b67ce1abca3451b MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2011 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@zend.com> |
  24. | Wez Furlong <wez@omniti.com> |
  25. +----------------------------------------------------------------------+
  26. */
  27. /* $Id: oci8_lob.c 306939 2011-01-01 02:19:59Z felipe $ */
  28. #ifdef HAVE_CONFIG_H
  29. #include "config.h"
  30. #endif
  31. #include "php.h"
  32. #include "ext/standard/info.h"
  33. #include "php_ini.h"
  34. #if HAVE_OCI8
  35. #include "php_oci8.h"
  36. #include "php_oci8_int.h"
  37. /* for import/export functions */
  38. #include <fcntl.h>
  39. #ifndef O_BINARY
  40. #define O_BINARY 0
  41. #endif
  42. /* {{{ php_oci_lob_create()
  43. Create LOB descriptor and allocate all the resources needed */
  44. php_oci_descriptor *php_oci_lob_create (php_oci_connection *connection, long type TSRMLS_DC)
  45. {
  46. php_oci_descriptor *descriptor;
  47. switch (type) {
  48. case OCI_DTYPE_FILE:
  49. case OCI_DTYPE_LOB:
  50. case OCI_DTYPE_ROWID:
  51. /* these three are allowed */
  52. break;
  53. default:
  54. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown descriptor type %ld", type);
  55. return NULL;
  56. break;
  57. }
  58. descriptor = ecalloc(1, sizeof(php_oci_descriptor));
  59. descriptor->type = type;
  60. descriptor->connection = connection;
  61. zend_list_addref(descriptor->connection->rsrc_id);
  62. PHP_OCI_CALL_RETURN(OCI_G(errcode), OCIDescriptorAlloc, (connection->env, (dvoid*)&(descriptor->descriptor), descriptor->type, (size_t) 0, (dvoid **) 0));
  63. if (OCI_G(errcode) != OCI_SUCCESS) {
  64. OCI_G(errcode) = php_oci_error(OCI_G(err), OCI_G(errcode) TSRMLS_CC);
  65. PHP_OCI_HANDLE_ERROR(connection, OCI_G(errcode));
  66. efree(descriptor);
  67. return NULL;
  68. }
  69. PHP_OCI_REGISTER_RESOURCE(descriptor, le_descriptor);
  70. descriptor->lob_current_position = 0;
  71. descriptor->lob_size = -1; /* we should set it to -1 to know, that it's just not initialized */
  72. descriptor->buffering = PHP_OCI_LOB_BUFFER_DISABLED; /* buffering is off by default */
  73. descriptor->charset_form = SQLCS_IMPLICIT; /* default value */
  74. descriptor->charset_id = connection->charset;
  75. descriptor->is_open = 0;
  76. if (descriptor->type == OCI_DTYPE_LOB || descriptor->type == OCI_DTYPE_FILE) {
  77. /* add Lobs & Files to hash. we'll flush them at the end */
  78. if (!connection->descriptors) {
  79. ALLOC_HASHTABLE(connection->descriptors);
  80. zend_hash_init(connection->descriptors, 0, NULL, php_oci_descriptor_flush_hash_dtor, 0);
  81. connection->descriptor_count = 0;
  82. }
  83. descriptor->index = (connection->descriptor_count)++;
  84. if (connection->descriptor_count == LONG_MAX) {
  85. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Internal descriptor counter has reached limit");
  86. php_oci_connection_descriptors_free(connection TSRMLS_CC);
  87. return NULL;
  88. }
  89. zend_hash_index_update(connection->descriptors,descriptor->index,&descriptor,sizeof(php_oci_descriptor *),NULL);
  90. }
  91. return descriptor;
  92. } /* }}} */
  93. /* {{{ php_oci_lob_get_length()
  94. Get length of the LOB. The length is cached so we don't need to ask Oracle every time */
  95. int php_oci_lob_get_length (php_oci_descriptor *descriptor, ub4 *length TSRMLS_DC)
  96. {
  97. php_oci_connection *connection = descriptor->connection;
  98. *length = 0;
  99. if (descriptor->lob_size >= 0) {
  100. *length = descriptor->lob_size;
  101. return 0;
  102. } else {
  103. if (descriptor->type == OCI_DTYPE_FILE) {
  104. PHP_OCI_CALL_RETURN(connection->errcode, OCILobFileOpen, (connection->svc, connection->err, descriptor->descriptor, OCI_FILE_READONLY));
  105. if (connection->errcode != OCI_SUCCESS) {
  106. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  107. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  108. return 1;
  109. }
  110. }
  111. PHP_OCI_CALL_RETURN(connection->errcode, OCILobGetLength, (connection->svc, connection->err, descriptor->descriptor, (ub4 *)length));
  112. if (connection->errcode != OCI_SUCCESS) {
  113. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  114. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  115. return 1;
  116. }
  117. descriptor->lob_size = *length;
  118. if (descriptor->type == OCI_DTYPE_FILE) {
  119. PHP_OCI_CALL_RETURN(connection->errcode, OCILobFileClose, (connection->svc, connection->err, descriptor->descriptor));
  120. if (connection->errcode != OCI_SUCCESS) {
  121. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  122. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  123. return 1;
  124. }
  125. }
  126. }
  127. return 0;
  128. } /* }}} */
  129. /* {{{ php_oci_lob_callback()
  130. Append LOB portion to a memory buffer */
  131. #if defined(HAVE_OCI_LOB_READ2)
  132. sb4 php_oci_lob_callback (dvoid *ctxp, CONST dvoid *bufxp, oraub8 len, ub1 piece, dvoid **changed_bufpp, oraub8 *changed_lenp)
  133. #else
  134. sb4 php_oci_lob_callback (dvoid *ctxp, CONST dvoid *bufxp, ub4 len, ub1 piece)
  135. #endif
  136. {
  137. ub4 lenp = (ub4) len;
  138. php_oci_lob_ctx *ctx = (php_oci_lob_ctx *)ctxp;
  139. switch (piece)
  140. {
  141. case OCI_LAST_PIECE:
  142. if ((*(ctx->lob_len) + lenp) > (ctx->alloc_len)) {
  143. /* this should not happen ever */
  144. *(ctx->lob_data) = NULL;
  145. *(ctx->lob_len) = 0;
  146. return OCI_ERROR;
  147. }
  148. memcpy(*(ctx->lob_data) + *(ctx->lob_len), bufxp, (size_t) lenp);
  149. *(ctx->lob_len) += lenp;
  150. *(*(ctx->lob_data) + *(ctx->lob_len)) = 0x00;
  151. return OCI_CONTINUE;
  152. case OCI_FIRST_PIECE:
  153. case OCI_NEXT_PIECE:
  154. if ((*(ctx->lob_len) + lenp) > ctx->alloc_len) {
  155. /* this should not happen ever */
  156. *(ctx->lob_data) = NULL;
  157. *(ctx->lob_len) = 0;
  158. return OCI_ERROR;
  159. }
  160. memcpy(*(ctx->lob_data) + *(ctx->lob_len), bufxp, (size_t) lenp);
  161. *(ctx->lob_len) += lenp;
  162. return OCI_CONTINUE;
  163. default: {
  164. TSRMLS_FETCH();
  165. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unexpected LOB piece id received (value:%d)", piece);
  166. *(ctx->lob_data) = NULL;
  167. *(ctx->lob_len) = 0;
  168. return OCI_ERROR;
  169. }
  170. }
  171. }
  172. /* }}} */
  173. /* {{{ php_oci_lob_calculate_buffer() */
  174. static inline int php_oci_lob_calculate_buffer(php_oci_descriptor *descriptor, long read_length TSRMLS_DC)
  175. {
  176. php_oci_connection *connection = descriptor->connection;
  177. ub4 chunk_size;
  178. if (descriptor->type == OCI_DTYPE_FILE) {
  179. return read_length;
  180. }
  181. if (!descriptor->chunk_size) {
  182. PHP_OCI_CALL_RETURN(connection->errcode, OCILobGetChunkSize, (connection->svc, connection->err, descriptor->descriptor, &chunk_size));
  183. if (connection->errcode != OCI_SUCCESS) {
  184. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  185. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  186. return read_length; /* we have to return original length here */
  187. }
  188. descriptor->chunk_size = chunk_size;
  189. }
  190. if ((read_length % descriptor->chunk_size) != 0) {
  191. return descriptor->chunk_size * ((read_length / descriptor->chunk_size) + 1);
  192. }
  193. return read_length;
  194. }
  195. /* }}} */
  196. /* {{{ php_oci_lob_read()
  197. Read specified portion of the LOB into the buffer */
  198. int php_oci_lob_read (php_oci_descriptor *descriptor, long read_length, long initial_offset, char **data, ub4 *data_len TSRMLS_DC)
  199. {
  200. php_oci_connection *connection = descriptor->connection;
  201. ub4 length = 0;
  202. int buffer_size = PHP_OCI_LOB_BUFFER_SIZE;
  203. php_oci_lob_ctx ctx;
  204. ub1 *bufp;
  205. #if defined(HAVE_OCI_LOB_READ2)
  206. oraub8 bytes_read, offset = 0;
  207. oraub8 requested_len = read_length; /* this is by default */
  208. oraub8 chars_read = 0;
  209. #else
  210. int bytes_read, offset = 0;
  211. int requested_len = read_length; /* this is by default */
  212. #endif
  213. int is_clob = 0;
  214. sb4 bytes_per_char = 1;
  215. *data_len = 0;
  216. *data = NULL;
  217. ctx.lob_len = data_len;
  218. ctx.lob_data = data;
  219. ctx.alloc_len = 0;
  220. if (php_oci_lob_get_length(descriptor, &length TSRMLS_CC)) {
  221. return 1;
  222. }
  223. if (length <= 0) {
  224. return 0;
  225. }
  226. if (initial_offset > length) {
  227. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset must be less than size of the LOB");
  228. return 1;
  229. }
  230. if (read_length == -1) {
  231. requested_len = length;
  232. }
  233. if (requested_len > (length - initial_offset)) {
  234. requested_len = length - initial_offset;
  235. }
  236. if (requested_len <= 0) {
  237. return 0;
  238. }
  239. offset = initial_offset;
  240. if (descriptor->type == OCI_DTYPE_FILE) {
  241. PHP_OCI_CALL_RETURN(connection->errcode, OCILobFileOpen, (connection->svc, connection->err, descriptor->descriptor, OCI_FILE_READONLY));
  242. if (connection->errcode != OCI_SUCCESS) {
  243. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  244. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  245. return 1;
  246. }
  247. } else {
  248. ub2 charset_id = 0;
  249. PHP_OCI_CALL_RETURN(connection->errcode, OCILobCharSetId, (connection->env, connection->err, descriptor->descriptor, &charset_id));
  250. if (connection->errcode != OCI_SUCCESS) {
  251. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  252. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  253. return 1;
  254. }
  255. if (charset_id > 0) { /* charset_id is always > 0 for [N]CLOBs */
  256. is_clob = 1;
  257. }
  258. }
  259. if (is_clob) {
  260. PHP_OCI_CALL_RETURN(connection->errcode, OCINlsNumericInfoGet, (connection->env, connection->err, &bytes_per_char, OCI_NLS_CHARSET_MAXBYTESZ));
  261. if (connection->errcode != OCI_SUCCESS) {
  262. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  263. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  264. return 1;
  265. }
  266. } else {
  267. /* BLOBs don't have encoding, so bytes_per_char == 1 */
  268. }
  269. ctx.alloc_len = (requested_len + 1) * bytes_per_char;
  270. *data = ecalloc(bytes_per_char, requested_len + 1);
  271. #ifdef HAVE_OCI_LOB_READ2
  272. if (is_clob) {
  273. chars_read = requested_len;
  274. bytes_read = 0;
  275. } else {
  276. chars_read = 0;
  277. bytes_read = requested_len;
  278. }
  279. buffer_size = (requested_len < buffer_size ) ? requested_len : buffer_size; /* optimize buffer size */
  280. buffer_size = php_oci_lob_calculate_buffer(descriptor, buffer_size TSRMLS_CC); /* use chunk size */
  281. bufp = (ub1 *) ecalloc(1, buffer_size);
  282. PHP_OCI_CALL_RETURN(connection->errcode, OCILobRead2,
  283. (
  284. connection->svc,
  285. connection->err,
  286. descriptor->descriptor,
  287. (oraub8 *)&bytes_read, /* IN/OUT bytes toread/read */
  288. (oraub8 *)&chars_read, /* IN/OUT chars toread/read */
  289. (oraub8) offset + 1, /* offset (starts with 1) */
  290. (dvoid *) bufp,
  291. (oraub8) buffer_size, /* size of buffer */
  292. OCI_FIRST_PIECE,
  293. (dvoid *)&ctx,
  294. (OCICallbackLobRead2) php_oci_lob_callback, /* callback... */
  295. (ub2) descriptor->charset_id, /* The character set ID of the buffer data. */
  296. (ub1) descriptor->charset_form /* The character set form of the buffer data. */
  297. )
  298. );
  299. efree(bufp);
  300. if (is_clob) {
  301. offset = descriptor->lob_current_position + chars_read;
  302. } else {
  303. offset = descriptor->lob_current_position + bytes_read;
  304. }
  305. #else
  306. bytes_read = requested_len;
  307. buffer_size = (requested_len < buffer_size ) ? requested_len : buffer_size; /* optimize buffer size */
  308. buffer_size = php_oci_lob_calculate_buffer(descriptor, buffer_size TSRMLS_CC); /* use chunk size */
  309. bufp = (ub1 *) ecalloc(1, buffer_size);
  310. PHP_OCI_CALL_RETURN(connection->errcode, OCILobRead,
  311. (
  312. connection->svc,
  313. connection->err,
  314. descriptor->descriptor,
  315. &bytes_read, /* IN/OUT bytes toread/read */
  316. offset + 1, /* offset (starts with 1) */
  317. (dvoid *) bufp,
  318. (ub4) buffer_size, /* size of buffer */
  319. (dvoid *)&ctx,
  320. (OCICallbackLobRead) php_oci_lob_callback, /* callback... */
  321. (ub2) descriptor->charset_id, /* The character set ID of the buffer data. */
  322. (ub1) descriptor->charset_form /* The character set form of the buffer data. */
  323. )
  324. );
  325. efree(bufp);
  326. offset = descriptor->lob_current_position + bytes_read;
  327. #endif
  328. if (connection->errcode != OCI_SUCCESS) {
  329. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  330. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  331. if (*data) {
  332. efree(*data);
  333. *data = NULL;
  334. }
  335. *data_len = 0;
  336. return 1;
  337. }
  338. descriptor->lob_current_position = (int)offset;
  339. if (descriptor->type == OCI_DTYPE_FILE) {
  340. PHP_OCI_CALL_RETURN(connection->errcode, OCILobFileClose, (connection->svc, connection->err, descriptor->descriptor));
  341. if (connection->errcode != OCI_SUCCESS) {
  342. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  343. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  344. if (*data) {
  345. efree(*data);
  346. *data = NULL;
  347. }
  348. *data_len = 0;
  349. return 1;
  350. }
  351. }
  352. return 0;
  353. } /* }}} */
  354. /* {{{ php_oci_lob_write()
  355. Write data to the LOB */
  356. int php_oci_lob_write (php_oci_descriptor *descriptor, ub4 offset, char *data, int data_len, ub4 *bytes_written TSRMLS_DC)
  357. {
  358. OCILobLocator *lob = (OCILobLocator *) descriptor->descriptor;
  359. php_oci_connection *connection = (php_oci_connection *) descriptor->connection;
  360. ub4 lob_length;
  361. *bytes_written = 0;
  362. if (php_oci_lob_get_length(descriptor, &lob_length TSRMLS_CC)) {
  363. return 1;
  364. }
  365. if (!data || data_len <= 0) {
  366. return 0;
  367. }
  368. if (offset < 0) {
  369. offset = 0;
  370. }
  371. if (offset > descriptor->lob_current_position) {
  372. offset = descriptor->lob_current_position;
  373. }
  374. PHP_OCI_CALL_RETURN(connection->errcode, OCILobWrite,
  375. (
  376. connection->svc,
  377. connection->err,
  378. lob,
  379. (ub4 *)&data_len,
  380. (ub4) offset + 1,
  381. (dvoid *) data,
  382. (ub4) data_len,
  383. OCI_ONE_PIECE,
  384. (dvoid *)0,
  385. (OCICallbackLobWrite) 0,
  386. (ub2) descriptor->charset_id,
  387. (ub1) descriptor->charset_form
  388. )
  389. );
  390. if (connection->errcode) {
  391. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  392. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  393. *bytes_written = 0;
  394. return 1;
  395. }
  396. *bytes_written = data_len;
  397. descriptor->lob_current_position += data_len;
  398. if (descriptor->lob_current_position > descriptor->lob_size) {
  399. descriptor->lob_size = descriptor->lob_current_position;
  400. }
  401. /* marking buffer as used */
  402. if (descriptor->buffering == PHP_OCI_LOB_BUFFER_ENABLED) {
  403. descriptor->buffering = PHP_OCI_LOB_BUFFER_USED;
  404. }
  405. return 0;
  406. } /* }}} */
  407. /* {{{ php_oci_lob_set_buffering()
  408. Turn buffering off/onn for this particular LOB */
  409. int php_oci_lob_set_buffering (php_oci_descriptor *descriptor, int on_off TSRMLS_DC)
  410. {
  411. php_oci_connection *connection = descriptor->connection;
  412. if (!on_off && descriptor->buffering == PHP_OCI_LOB_BUFFER_DISABLED) {
  413. /* disabling when it's already off */
  414. return 0;
  415. }
  416. if (on_off && descriptor->buffering != PHP_OCI_LOB_BUFFER_DISABLED) {
  417. /* enabling when it's already on */
  418. return 0;
  419. }
  420. if (on_off) {
  421. PHP_OCI_CALL_RETURN(connection->errcode, OCILobEnableBuffering, (connection->svc, connection->err, descriptor->descriptor));
  422. } else {
  423. PHP_OCI_CALL_RETURN(connection->errcode, OCILobDisableBuffering, (connection->svc, connection->err, descriptor->descriptor));
  424. }
  425. if (connection->errcode != OCI_SUCCESS) {
  426. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  427. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  428. return 1;
  429. }
  430. descriptor->buffering = on_off ? PHP_OCI_LOB_BUFFER_ENABLED : PHP_OCI_LOB_BUFFER_DISABLED;
  431. return 0;
  432. } /* }}} */
  433. /* {{{ php_oci_lob_get_buffering()
  434. Return current buffering state for the LOB */
  435. int php_oci_lob_get_buffering (php_oci_descriptor *descriptor)
  436. {
  437. if (descriptor->buffering != PHP_OCI_LOB_BUFFER_DISABLED) {
  438. return 1;
  439. } else {
  440. return 0;
  441. }
  442. } /* }}} */
  443. /* {{{ php_oci_lob_copy()
  444. Copy one LOB (or its part) to another one */
  445. int php_oci_lob_copy (php_oci_descriptor *descriptor_dest, php_oci_descriptor *descriptor_from, long length TSRMLS_DC)
  446. {
  447. php_oci_connection *connection = descriptor_dest->connection;
  448. ub4 length_dest, length_from, copy_len;
  449. if (php_oci_lob_get_length(descriptor_dest, &length_dest TSRMLS_CC)) {
  450. return 1;
  451. }
  452. if (php_oci_lob_get_length(descriptor_from, &length_from TSRMLS_CC)) {
  453. return 1;
  454. }
  455. if (length == -1) {
  456. copy_len = length_from - descriptor_from->lob_current_position;
  457. } else {
  458. copy_len = length;
  459. }
  460. if ((int)copy_len <= 0) {
  461. /* silently fail, there is nothing to copy */
  462. return 1;
  463. }
  464. PHP_OCI_CALL_RETURN(connection->errcode, OCILobCopy,
  465. (
  466. connection->svc,
  467. connection->err,
  468. descriptor_dest->descriptor,
  469. descriptor_from->descriptor,
  470. copy_len,
  471. descriptor_dest->lob_current_position+1,
  472. descriptor_from->lob_current_position+1
  473. )
  474. );
  475. if (connection->errcode != OCI_SUCCESS) {
  476. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  477. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  478. return 1;
  479. }
  480. return 0;
  481. } /* }}} */
  482. /* {{{ php_oci_lob_close()
  483. Close LOB */
  484. int php_oci_lob_close (php_oci_descriptor *descriptor TSRMLS_DC)
  485. {
  486. php_oci_connection *connection = descriptor->connection;
  487. if (descriptor->is_open) {
  488. PHP_OCI_CALL_RETURN(connection->errcode, OCILobClose, (connection->svc, connection->err, descriptor->descriptor));
  489. }
  490. if (connection->errcode != OCI_SUCCESS) {
  491. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  492. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  493. return 1;
  494. }
  495. if (php_oci_temp_lob_close(descriptor TSRMLS_CC)) {
  496. return 1;
  497. }
  498. return 0;
  499. } /* }}} */
  500. /* {{{ php_oci_temp_lob_close()
  501. Close Temporary LOB */
  502. int php_oci_temp_lob_close (php_oci_descriptor *descriptor TSRMLS_DC)
  503. {
  504. php_oci_connection *connection = descriptor->connection;
  505. int is_temporary;
  506. PHP_OCI_CALL_RETURN(connection->errcode, OCILobIsTemporary, (connection->env,connection->err, descriptor->descriptor, &is_temporary));
  507. if (connection->errcode != OCI_SUCCESS) {
  508. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  509. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  510. return 1;
  511. }
  512. if (is_temporary) {
  513. PHP_OCI_CALL_RETURN(connection->errcode, OCILobFreeTemporary, (connection->svc, connection->err, descriptor->descriptor));
  514. if (connection->errcode != OCI_SUCCESS) {
  515. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  516. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  517. return 1;
  518. }
  519. }
  520. return 0;
  521. } /* }}} */
  522. /* {{{ php_oci_lob_flush()
  523. Flush buffers for the LOB (only if they have been used) */
  524. int php_oci_lob_flush(php_oci_descriptor *descriptor, long flush_flag TSRMLS_DC)
  525. {
  526. OCILobLocator *lob = descriptor->descriptor;
  527. php_oci_connection *connection = descriptor->connection;
  528. if (!lob) {
  529. return 1;
  530. }
  531. switch (flush_flag) {
  532. case 0:
  533. case OCI_LOB_BUFFER_FREE:
  534. /* only these two are allowed */
  535. break;
  536. default:
  537. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid flag value: %ld", flush_flag);
  538. return 1;
  539. break;
  540. }
  541. /* do not really flush buffer, but report success
  542. * to suppress OCI error when flushing not used buffer
  543. * */
  544. if (descriptor->buffering != PHP_OCI_LOB_BUFFER_USED) {
  545. return 0;
  546. }
  547. PHP_OCI_CALL_RETURN(connection->errcode, OCILobFlushBuffer, (connection->svc, connection->err, lob, flush_flag));
  548. if (connection->errcode != OCI_SUCCESS) {
  549. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  550. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  551. return 1;
  552. }
  553. /* marking buffer as enabled and not used */
  554. descriptor->buffering = PHP_OCI_LOB_BUFFER_ENABLED;
  555. return 0;
  556. } /* }}} */
  557. /* {{{ php_oci_lob_free()
  558. Close LOB descriptor and free associated resources */
  559. void php_oci_lob_free (php_oci_descriptor *descriptor TSRMLS_DC)
  560. {
  561. if (!descriptor || !descriptor->connection) {
  562. return;
  563. }
  564. if (descriptor->connection->descriptors) {
  565. /* delete descriptor from the hash */
  566. zend_hash_index_del(descriptor->connection->descriptors, descriptor->index);
  567. if (zend_hash_num_elements(descriptor->connection->descriptors) == 0) {
  568. descriptor->connection->descriptor_count = 0;
  569. } else {
  570. if (descriptor->index + 1 == descriptor->connection->descriptor_count) {
  571. /* If the descriptor being freed is the end-most one
  572. * allocated, then the descriptor_count is reduced so
  573. * a future descriptor can reuse the hash table index.
  574. * This can prevent the hash index range increasing in
  575. * the common case that each descriptor is
  576. * allocated/used/freed before another descriptor is
  577. * needed. However it is possible that a script frees
  578. * descriptors in arbitrary order which would prevent
  579. * descriptor_count ever being reduced to zero until
  580. * zend_hash_num_elements() returns 0.
  581. */
  582. descriptor->connection->descriptor_count--;
  583. }
  584. }
  585. }
  586. /* flushing Lobs & Files with buffering enabled */
  587. if ((descriptor->type == OCI_DTYPE_FILE || descriptor->type == OCI_DTYPE_LOB) && descriptor->buffering == PHP_OCI_LOB_BUFFER_USED) {
  588. php_oci_lob_flush(descriptor, OCI_LOB_BUFFER_FREE TSRMLS_CC);
  589. }
  590. if (descriptor->type == OCI_DTYPE_LOB) {
  591. php_oci_temp_lob_close(descriptor TSRMLS_CC);
  592. }
  593. PHP_OCI_CALL(OCIDescriptorFree, (descriptor->descriptor, descriptor->type));
  594. zend_list_delete(descriptor->connection->rsrc_id);
  595. efree(descriptor);
  596. } /* }}} */
  597. /* {{{ php_oci_lob_import()
  598. Import LOB contents from the given file */
  599. int php_oci_lob_import (php_oci_descriptor *descriptor, char *filename TSRMLS_DC)
  600. {
  601. int fp;
  602. ub4 loblen;
  603. OCILobLocator *lob = (OCILobLocator *)descriptor->descriptor;
  604. php_oci_connection *connection = descriptor->connection;
  605. char buf[8192];
  606. ub4 offset = 1;
  607. if (php_check_open_basedir(filename TSRMLS_CC)) {
  608. return 1;
  609. }
  610. if ((fp = VCWD_OPEN(filename, O_RDONLY|O_BINARY)) == -1) {
  611. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't open file %s", filename);
  612. return 1;
  613. }
  614. while ((loblen = read(fp, &buf, sizeof(buf))) > 0) {
  615. PHP_OCI_CALL_RETURN(connection->errcode,
  616. OCILobWrite,
  617. (
  618. connection->svc,
  619. connection->err,
  620. lob,
  621. &loblen,
  622. offset,
  623. (dvoid *) &buf,
  624. loblen,
  625. OCI_ONE_PIECE,
  626. (dvoid *)0,
  627. (OCICallbackLobWrite) 0,
  628. (ub2) descriptor->charset_id,
  629. (ub1) descriptor->charset_form
  630. )
  631. );
  632. if (connection->errcode != OCI_SUCCESS) {
  633. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  634. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  635. close(fp);
  636. return 1;
  637. }
  638. offset += loblen;
  639. }
  640. close(fp);
  641. return 0;
  642. } /* }}} */
  643. /* {{{ php_oci_lob_append()
  644. Append data to the end of the LOB */
  645. int php_oci_lob_append (php_oci_descriptor *descriptor_dest, php_oci_descriptor *descriptor_from TSRMLS_DC)
  646. {
  647. php_oci_connection *connection = descriptor_dest->connection;
  648. OCILobLocator *lob_dest = descriptor_dest->descriptor;
  649. OCILobLocator *lob_from = descriptor_from->descriptor;
  650. ub4 dest_len, from_len;
  651. if (php_oci_lob_get_length(descriptor_dest, &dest_len TSRMLS_CC)) {
  652. return 1;
  653. }
  654. if (php_oci_lob_get_length(descriptor_from, &from_len TSRMLS_CC)) {
  655. return 1;
  656. }
  657. if (from_len <= 0) {
  658. return 0;
  659. }
  660. PHP_OCI_CALL_RETURN(connection->errcode, OCILobAppend, (connection->svc, connection->err, lob_dest, lob_from));
  661. if (connection->errcode != OCI_SUCCESS) {
  662. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  663. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  664. return 1;
  665. }
  666. return 0;
  667. } /* }}} */
  668. /* {{{ php_oci_lob_truncate()
  669. Truncate LOB to the given length */
  670. int php_oci_lob_truncate (php_oci_descriptor *descriptor, long new_lob_length TSRMLS_DC)
  671. {
  672. php_oci_connection *connection = descriptor->connection;
  673. OCILobLocator *lob = descriptor->descriptor;
  674. ub4 lob_length;
  675. if (php_oci_lob_get_length(descriptor, &lob_length TSRMLS_CC)) {
  676. return 1;
  677. }
  678. if (lob_length <= 0) {
  679. return 0;
  680. }
  681. if (new_lob_length < 0) {
  682. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Size must be greater than or equal to 0");
  683. return 1;
  684. }
  685. if (new_lob_length > lob_length) {
  686. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Size must be less than or equal to the current LOB size");
  687. return 1;
  688. }
  689. PHP_OCI_CALL_RETURN(connection->errcode, OCILobTrim, (connection->svc, connection->err, lob, new_lob_length));
  690. if (connection->errcode != OCI_SUCCESS) {
  691. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  692. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  693. return 1;
  694. }
  695. descriptor->lob_size = new_lob_length;
  696. return 0;
  697. } /* }}} */
  698. /* {{{ php_oci_lob_erase()
  699. Erase (or fill with whitespaces, depending on LOB type) the LOB (or its part) */
  700. int php_oci_lob_erase (php_oci_descriptor *descriptor, long offset, ub4 length, ub4 *bytes_erased TSRMLS_DC)
  701. {
  702. php_oci_connection *connection = descriptor->connection;
  703. OCILobLocator *lob = descriptor->descriptor;
  704. ub4 lob_length;
  705. *bytes_erased = 0;
  706. if (php_oci_lob_get_length(descriptor, &lob_length TSRMLS_CC)) {
  707. return 1;
  708. }
  709. if (offset == -1) {
  710. offset = descriptor->lob_current_position;
  711. }
  712. if (length == -1) {
  713. length = lob_length;
  714. }
  715. PHP_OCI_CALL_RETURN(connection->errcode, OCILobErase, (connection->svc, connection->err, lob, (ub4 *)&length, offset+1));
  716. if (connection->errcode != OCI_SUCCESS) {
  717. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  718. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  719. return 1;
  720. }
  721. *bytes_erased = length;
  722. return 0;
  723. } /* }}} */
  724. /* {{{ php_oci_lob_is_equal()
  725. Compare two LOB descriptors and figure out if they are pointing to the same LOB */
  726. int php_oci_lob_is_equal (php_oci_descriptor *descriptor_first, php_oci_descriptor *descriptor_second, boolean *result TSRMLS_DC)
  727. {
  728. php_oci_connection *connection = descriptor_first->connection;
  729. OCILobLocator *first_lob = descriptor_first->descriptor;
  730. OCILobLocator *second_lob = descriptor_second->descriptor;
  731. PHP_OCI_CALL_RETURN(connection->errcode, OCILobIsEqual, (connection->env, first_lob, second_lob, result));
  732. if (connection->errcode) {
  733. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  734. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  735. return 1;
  736. }
  737. return 0;
  738. } /* }}} */
  739. /* {{{ php_oci_lob_write_tmp()
  740. Create temporary LOB and write data to it */
  741. int php_oci_lob_write_tmp (php_oci_descriptor *descriptor, ub1 type, char *data, int data_len TSRMLS_DC)
  742. {
  743. php_oci_connection *connection = descriptor->connection;
  744. OCILobLocator *lob = descriptor->descriptor;
  745. ub4 bytes_written = 0;
  746. switch (type) {
  747. case OCI_TEMP_BLOB:
  748. case OCI_TEMP_CLOB:
  749. /* only these two are allowed */
  750. break;
  751. default:
  752. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid temporary lob type: %d", type);
  753. return 1;
  754. break;
  755. }
  756. if (data_len < 0) {
  757. return 1;
  758. }
  759. PHP_OCI_CALL_RETURN(connection->errcode, OCILobCreateTemporary,
  760. (
  761. connection->svc,
  762. connection->err,
  763. lob,
  764. OCI_DEFAULT,
  765. OCI_DEFAULT,
  766. type,
  767. OCI_ATTR_NOCACHE,
  768. OCI_DURATION_SESSION
  769. )
  770. );
  771. if (connection->errcode) {
  772. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  773. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  774. return 1;
  775. }
  776. PHP_OCI_CALL_RETURN(connection->errcode, OCILobOpen, (connection->svc, connection->err, lob, OCI_LOB_READWRITE));
  777. if (connection->errcode) {
  778. connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
  779. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  780. return 1;
  781. }
  782. descriptor->is_open = 1;
  783. return php_oci_lob_write(descriptor, 0, data, data_len, &bytes_written TSRMLS_CC);
  784. } /* }}} */
  785. #endif /* HAVE_OCI8 */
  786. /*
  787. * Local variables:
  788. * tab-width: 4
  789. * c-basic-offset: 4
  790. * End:
  791. * vim600: noet sw=4 ts=4 fdm=marker
  792. * vim<600: noet sw=4 ts=4
  793. */