1--TEST--
2PDO OCI: Insert and fetch 1K records from a table that contains 1 number and 2 LOB columns (stress test)
3--SKIPIF--
4<?php
5if (!extension_loaded('pdo') || !extension_loaded('pdo_oci')) die('skip not loaded');
6if (getenv('SKIP_SLOW_TESTS')) die('skip slow tests excluded by request');
7require(__DIR__.'/../../pdo/tests/pdo_test.inc');
8PDOTest::skip();
9?>
10--FILE--
11<?php
12
13require(__DIR__ . '/../../pdo/tests/pdo_test.inc');
14
15$db = PDOTest::factory();
16
17$query = "begin execute immediate 'drop table pdo_oci_stream_2'; exception when others then if sqlcode <> -942 then raise; end if; end;";
18$stmt = $db->prepare($query);
19$stmt->execute();
20
21$query = "create table pdo_oci_stream_2 (id number, data1 blob, data2 blob)";
22$stmt = $db->prepare($query);
23$stmt->execute();
24
25function do_insert($db, $id, $data1, $data2)
26{
27    $db->beginTransaction();
28    $stmt = $db->prepare("insert into pdo_oci_stream_2 (id, data1, data2) values (:id, empty_blob(), empty_blob()) returning data1, data2 into :blob1, :blob2");
29    $stmt->bindParam(':id', $id);
30    $stmt->bindParam(':blob1', $blob1, PDO::PARAM_LOB);
31    $stmt->bindParam(':blob2', $blob2, PDO::PARAM_LOB);
32    $blob1 = null;
33    $blob2 = null;
34    $stmt->execute();
35
36    fwrite($blob1, $data1);
37    fclose($blob1);
38    fwrite($blob2, $data2);
39    fclose($blob2);
40    $db->commit();
41}
42
43$a1 = str_repeat('a', 4086);
44$a2 = str_repeat('b', 4087);
45$a3 = str_repeat('c', 4088);
46$a4 = str_repeat('d', 4089);
47$a5 = str_repeat('e', 4090);
48$a6 = str_repeat('f', 4091);
49$a7 = str_repeat('g', 4092);
50$a8 = str_repeat('h', 4093);
51$a9 = str_repeat('i', 4094);
52$a10 = str_repeat('j', 4095);
53
54printf("Inserting 1000 Records ... ");
55for($i=0; $i<100; $i++) {
56    do_insert($db, $i * 10 + 1, $a1, $a10);
57    do_insert($db, $i * 10 + 2, $a2, $a9);
58    do_insert($db, $i * 10 + 3, $a3, $a8);
59    do_insert($db, $i * 10 + 4, $a4, $a7);
60    do_insert($db, $i * 10 + 5, $a5, $a6);
61    do_insert($db, $i * 10 + 6, $a6, $a5);
62    do_insert($db, $i * 10 + 7, $a7, $a4);
63    do_insert($db, $i * 10 + 8, $a8, $a3);
64    do_insert($db, $i * 10 + 9, $a9, $a2);
65    do_insert($db, $i * 10 + 10, $a10, $a1);
66}
67printf("Done\n");
68
69/* Cleanup is done in pdo_oci_stream_2b.phpt */
70//$db->exec("drop table pdo_oci_stream_2");
71
72$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);  // Let's use streams
73
74// Since each column only has one lob descriptor, the last row is
75// shown twice because the lob descriptor for each column is reused in
76// the stream
77
78$i = 0;
79$j = 9;
80$a_val = ord('a');
81foreach($db->query("select data1 as d4_1, data2 as d4_2 from pdo_oci_stream_2 order by id") as $row) {
82    $a = $row['d4_1'];
83    $a1 = $row['d4_2'];
84
85    $str1 = stream_get_contents($a);
86    $str2 = stream_get_contents($a1);
87
88    $str1len = strlen($str1);
89    $str2len = strlen($str2);
90
91    $b = ord($str1[0]);
92    $b1 = ord($str2[0]);
93
94    if (($b != ($a_val + $i)) && ($str1len != (4086 + $i)) &&
95        ($b1 != ($a_val + $j)) && ($str2len != (4086 + $j))) {
96        printf("There is a bug!\n");
97        printf("Col1:\n");
98        printf("a_val = %d\n", $a_val);
99        printf("b     = %d\n", $b);
100        printf("i     = %d\n", $i);
101        printf("str1len = %d\n", $str1len);
102
103        printf("Col2:\n");
104        printf("a_val = %d\n", $a_val);
105        printf("b1    = %d\n", $b1);
106        printf("j     = %d\n", $j);
107        printf("str2len = %d\n", $str1len);
108
109    }
110    $i++;
111    if ($i>9)
112        $i = 0;
113    $j--;
114    if ($j<0)
115        $j = 9;
116}
117echo "Fetch operation done!\n";
118
119/* Cleanup */
120$db->exec("drop table pdo_oci_stream_2");
121
122?>
123--EXPECT--
124Inserting 1000 Records ... Done
125Fetch operation done!
126