1--TEST--
2PDO_OCI: Attribute: beginTransaction and native transactions
3--SKIPIF--
4<?php
5if (!extension_loaded('pdo') || !extension_loaded('pdo_oci')) die('skip not loaded');
6require(__DIR__.'/../../pdo/tests/pdo_test.inc');
7PDOTest::skip();
8?>
9--FILE--
10<?php
11
12require(__DIR__ . '/../../pdo/tests/pdo_test.inc');
13$dbh = PDOTest::factory();
14
15$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
16$dbh->exec("drop table pdo_ac_tab");
17$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
18
19$dbh->exec("create table pdo_ac_tab (col1 varchar2(25))");
20
21echo "Test 1 Check beginTransaction insertion\n";
22
23$dbh->beginTransaction();
24try {
25	$dbh->exec("insert into pdo_ac_tab (col1) values ('data 1')");
26	$dbh->exec("insert into pdo_ac_tab (col1) values ('data 2')");
27	$dbh->commit();
28}
29catch (PDOException $e) {
30	echo "Caught unexpected exception at line " . __LINE__ . "\n";
31	echo $e->getMessage() . "\n";
32	$dbh->rollback();
33}
34
35echo "Test 2 Cause an exception and test beginTransaction rollback\n";
36
37$dbh->beginTransaction();
38try {
39	$dbh->exec("insert into pdo_ac_tab (col1) values ('not committed #1')");
40	$dbh->exec("insert into pdo_ac_tab (col1) values ('data that is too long to fit and will barf')");
41	$dbh->commit();
42}
43catch (PDOException $e) {
44	echo "Caught expected exception at line " . __LINE__ . "\n";
45	echo $e->getMessage() . "\n";
46	$dbh->rollback();
47}
48
49echo "Test 3 Setting ATTR_AUTOCOMMIT to true will commit and end the transaction\n";
50
51$dbh->exec("insert into pdo_ac_tab (col1) values ('data 3')");
52$dbh->setAttribute(PDO::ATTR_AUTOCOMMIT, true);
53print "PDO::ATTR_AUTOCOMMIT: ";
54var_dump($dbh->getAttribute(PDO::ATTR_AUTOCOMMIT));
55try {
56	$dbh->rollback();
57}
58catch (PDOException $e) {
59	echo "Caught expected exception at line " . __LINE__ . "\n";
60	echo $e->getMessage() . "\n";
61}
62
63echo "Test 4 Setting ATTR_AUTOCOMMIT to false will commit and end the transaction\n";
64
65$dbh->beginTransaction();
66$dbh->exec("insert into pdo_ac_tab (col1) values ('data 4')");
67$dbh->setAttribute(PDO::ATTR_AUTOCOMMIT, false);
68print "PDO::ATTR_AUTOCOMMIT: ";
69var_dump($dbh->getAttribute(PDO::ATTR_AUTOCOMMIT));
70try {
71	$dbh->rollback();
72}
73catch (PDOException $e) {
74	echo "Caught expected exception at line " . __LINE__ . "\n";
75	echo $e->getMessage() . "\n";
76}
77
78echo "Test 5 Handle transactions ourselves\n";
79
80print "PDO::ATTR_AUTOCOMMIT: ";
81var_dump($dbh->getAttribute(PDO::ATTR_AUTOCOMMIT));
82
83$dbh->exec("insert into pdo_ac_tab (col1) values ('not committed #2')");
84$dbh->exec("rollback");
85$dbh->exec("insert into pdo_ac_tab (col1) values ('data 5')");
86$dbh->exec("insert into pdo_ac_tab (col1) values ('data 6')");
87
88$dbh->exec("commit");
89
90// Open new connection to really verify what was inserted
91
92$dbh2 = PDOTest::factory();
93
94echo "Query Results are:\n";
95$s = $dbh2->prepare("select col1 from pdo_ac_tab");
96$s->execute();
97while ($r = $s->fetch()) {
98    echo $r[0] . "\n";
99}
100
101echo "Done\n";
102
103?>
104--EXPECTF--
105Test 1 Check beginTransaction insertion
106Test 2 Cause an exception and test beginTransaction rollback
107Caught expected exception at line 35
108SQLSTATE[HY000]: General error: 12899 OCIStmtExecute: ORA-12899: %s
109%s
110Test 3 Setting ATTR_AUTOCOMMIT to true will commit and end the transaction
111PDO::ATTR_AUTOCOMMIT: bool(true)
112Caught expected exception at line %d
113There is no active transaction
114Test 4 Setting ATTR_AUTOCOMMIT to false will commit and end the transaction
115PDO::ATTR_AUTOCOMMIT: bool(false)
116Caught expected exception at line %d
117There is no active transaction
118Test 5 Handle transactions ourselves
119PDO::ATTR_AUTOCOMMIT: bool(false)
120Query Results are:
121data 1
122data 2
123data 3
124data 4
125data 5
126data 6
127Done
128