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