xref: /PHP-8.1/ext/oci8/tests/define6.phpt (revision b5a14e6c)
1--TEST--
2oci_define_by_name tests with REF CURSORs
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--FILE--
11<?php
12
13require(__DIR__.'/connect.inc');
14
15// Initialization
16
17$stmtarray = array(
18    "drop table define6_tab",
19    "create table define6_tab (id number)",
20    "insert into define6_tab values (1)"
21);
22
23oci8_test_sql_execute($c, $stmtarray);
24
25// Run Test
26
27$sql =
28"DECLARE
29  TYPE curtype IS REF CURSOR;
30  cursor_var curtype;
31BEGIN
32  OPEN cursor_var FOR SELECT id FROM define6_tab;
33  :curs := cursor_var;
34END;";
35
36echo "Test 1 - define last\n";
37
38$s1 = oci_parse($c, $sql);
39$cursor1 = oci_new_cursor($c);
40oci_bind_by_name($s1, ":curs", $cursor1, -1, OCI_B_CURSOR);
41oci_execute($s1);
42oci_execute($cursor1);
43oci_define_by_name($cursor1, 'ID', $id1);
44while (oci_fetch_row($cursor1)) {
45    var_dump($id1);
46}
47
48
49echo "Test 2 - define last with preset var\n";
50
51$s2 = oci_parse($c, $sql);
52$cursor2 = oci_new_cursor($c);
53oci_bind_by_name($s2, ":curs", $cursor2, -1, OCI_B_CURSOR);
54oci_execute($s2);
55oci_execute($cursor2);
56$id2 = '';
57oci_define_by_name($cursor2, 'ID', $id2);
58while (oci_fetch_row($cursor2)) {
59    var_dump($id2);
60}
61
62
63echo "Test 3 - define before cursor execute\n";
64
65$s3 = oci_parse($c, $sql);
66$cursor3 = oci_new_cursor($c);
67oci_bind_by_name($s3, ":curs", $cursor3, -1, OCI_B_CURSOR);
68oci_execute($s3);
69oci_define_by_name($cursor3, 'ID', $id3);
70oci_execute($cursor3);
71while (oci_fetch_row($cursor3)) {
72    var_dump($id3);
73}
74
75
76echo "Test 4 - define before top level execute\n";
77
78$s4 = oci_parse($c, $sql);
79$cursor4 = oci_new_cursor($c);
80oci_bind_by_name($s4, ":curs", $cursor4, -1, OCI_B_CURSOR);
81oci_define_by_name($cursor4, 'ID', $id4);
82oci_execute($s4);
83oci_execute($cursor4);
84while (oci_fetch_row($cursor4)) {
85    var_dump($id4);
86}
87
88
89echo "Test 5 - define before bind\n";
90
91$s5 = oci_parse($c, $sql);
92$cursor5 = oci_new_cursor($c);
93oci_define_by_name($cursor5, 'ID', $id5);
94oci_bind_by_name($s5, ":curs", $cursor5, -1, OCI_B_CURSOR);
95oci_execute($s5);
96oci_execute($cursor5);
97while (oci_fetch_row($cursor5)) {
98    var_dump($id5);
99}
100
101
102echo "Test 6 - fetch on wrong handle\n";
103
104$s6 = oci_parse($c, $sql);
105$cursor6 = oci_new_cursor($c);
106oci_define_by_name($cursor6, 'ID', $id6);
107oci_bind_by_name($s6, ":curs", $cursor6, -1, OCI_B_CURSOR);
108oci_execute($s6);
109oci_execute($cursor6);
110while (oci_fetch_row($s6)) {
111    var_dump($id6);
112}
113
114
115// Clean up
116
117$stmtarray = array(
118    "drop table define6_tab"
119);
120
121oci8_test_sql_execute($c, $stmtarray);
122
123?>
124--EXPECTF--
125Test 1 - define last
126NULL
127Test 2 - define last with preset var
128string(0) ""
129Test 3 - define before cursor execute
130string(1) "1"
131Test 4 - define before top level execute
132string(1) "1"
133Test 5 - define before bind
134string(1) "1"
135Test 6 - fetch on wrong handle
136
137Warning: oci_fetch_row(): ORA-24374: %s in %sdefine6.php on line %d
138