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