1--TEST--
2LOB prefetching with oci8.
3--EXTENSIONS--
4oci8
5--SKIPIF--
6<?php
7$target_dbs = array('oracledb' => true, 'timesten' => false);  // test runs on these DBs
8require(__DIR__.'/skipif.inc');
9?>
10--INI--
11oci8.prefetch_lob_size=100000
12--FILE--
13<?php
14
15require __DIR__.'/connect.inc';
16require __DIR__.'/create_table.inc';
17
18define("NUMROWS", 200);
19define("LOBSIZE", 64000);
20
21$ora_sql =
22  "declare
23    c clob;
24    b blob;
25    numrows number := " . NUMROWS . ";
26    dest_offset integer := 1;
27    src_offset  integer := 1;
28    warn        integer;
29    ctx         integer := dbms_lob.default_lang_ctx;
30  begin
31    for j in 1..numrows
32    loop
33      c := DBMS_RANDOM.string('L',TRUNC(DBMS_RANDOM.value(1000,1000)));
34      for i in 1..6
35      loop
36        c := c||c;
37      end loop;
38      dbms_lob.createtemporary(b, false);
39      dbms_lob.converttoblob(b, c, dbms_lob.lobmaxsize, dest_offset, src_offset, dbms_lob.default_csid, ctx, warn);
40      insert /*+ APPEND */ into {$schema}{$table_name} (id, clob, blob) values (j, c, b);
41    end loop;
42    commit;
43  end;";
44
45$statement = oci_parse($c,$ora_sql);
46oci_execute($statement);
47
48function get_clob_loc($c, $sql, $pfl) {
49    $stid = oci_parse($c, $sql);
50    if ($pfl >= 0)
51        oci_set_prefetch_lob($stid, $pfl);
52    oci_execute($stid);
53    $l = [];
54    while (($row = oci_fetch_array($stid, OCI_ASSOC)) != false) {
55        $l[] = $row['CLOB']->load();
56        $row['CLOB']->free();
57        if (strlen($l[0]) != LOBSIZE) { print("strlen(l) failure" .  strlen($l)); exit; }
58    }
59    return($l);
60}
61
62function get_clob_inline($c, $sql, $pfl) {
63    $stid = oci_parse($c, $sql);
64    if ($pfl >= 0)
65        oci_set_prefetch_lob($stid, $pfl);
66    oci_execute($stid);
67    $l = [];
68    while (($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_LOBS)) != false) {
69        $l[] = $row['CLOB'];
70        if (strlen($l[0]) != LOBSIZE) { print("strlen(l) failure" . strlen($l)); exit; }
71    }
72    return($l);
73}
74
75function check_clobs($locarr, $inlinearr) {
76    print("Comparing CLOBS\n");
77    for ($i = 0; $i < NUMROWS; ++$i) {
78        if (strlen($locarr[$i]) != LOBSIZE) {
79            trigger_error("size mismatch at $i " . strlen($locarr[$i]), E_USER_ERROR);
80            exit;
81        }
82        if (strcmp($locarr[$i], $inlinearr[$i])) {
83            trigger_error("data mismatch at $i " . strlen($locarr[$i]) . " " . strlen($inlinearr[$i]), E_USER_ERROR);
84            exit;
85        }
86    }
87}
88
89function get_blob_loc($c, $sql, $pfl) {
90    $stid = oci_parse($c, $sql);
91    if ($pfl >= 0)
92        oci_set_prefetch_lob($stid, $pfl);
93    oci_execute($stid);
94    $l = [];
95    while (($row = oci_fetch_array($stid, OCI_ASSOC)) != false) {
96        $l[] = $row['BLOB']->load();
97        $row['BLOB']->free();
98        if (strlen($l[0]) != LOBSIZE) { print("strlen(l) failure" .  strlen($l)); exit; }
99    }
100    return($l);
101}
102
103
104print("Test 1 - prefetch_lob_size\n");
105
106$r = ini_get("oci8.prefetch_lob_size");
107var_dump($r);
108
109print("Test 2 - CLOB with current oci8.prefetch_lob_size\n");
110
111$sql = "select clob from {$schema}{$table_name}" . " order by id";
112$locarr = get_clob_loc($c, $sql, -1);
113$inlinearr = get_clob_inline($c, $sql, -1);
114
115print(count($locarr) . "\n");
116print(count($inlinearr) . "\n");
117check_clobs($locarr, $inlinearr);
118
119print("Test 3 - CLOB override prefetch_lob_size 0\n");
120
121$locarr = get_clob_loc($c, $sql, 0);
122$inlinearr = get_clob_inline($c, $sql, 0);
123
124print(count($locarr) . "\n");
125print(count($inlinearr) . "\n");
126check_clobs($locarr, $inlinearr);
127
128print("Test 4 - CLOB override prefetch_lob_size 1000\n");
129
130$locarr = get_clob_loc($c, $sql, 1000);
131$inlinearr = get_clob_inline($c, $sql, 1000);
132
133print(count($locarr) . "\n");
134print(count($inlinearr) . "\n");
135check_clobs($locarr, $inlinearr);
136
137print("Test 5 - BLOB with current ocig8.prefetch_lob_size\n");
138
139$sql = "select blob from {$schema}{$table_name}" . " order by id";
140$locarr = get_blob_loc($c, $sql, -1);
141
142print(count($locarr) . "\n");
143
144require __DIR__.'/drop_table.inc';
145
146?>
147DONE
148--EXPECTF--
149Test 1 - prefetch_lob_size
150string(6) "100000"
151Test 2 - CLOB with current oci8.prefetch_lob_size
152200
153200
154Comparing CLOBS
155Test 3 - CLOB override prefetch_lob_size 0
156200
157200
158Comparing CLOBS
159Test 4 - CLOB override prefetch_lob_size 1000
160200
161200
162Comparing CLOBS
163Test 5 - BLOB with current ocig8.prefetch_lob_size
164200
165DONE
166