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