-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoracle.plugin.php
111 lines (95 loc) · 3.55 KB
/
oracle.plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
require_once 'app.php';
class Oracle extends Plugin {
private $broken = false;
private $persistent;
private $connection = false;
// Con/Destructors /////////////////////////////////////////////////
function __construct($connectionString, $username, $password, $persistent = true) {
$this->persistent = $persistent;
if (empty($connectionString) || empty($username) || empty($password)) {
$this->nuke('Connection parameters not fully qualified.');
} else {
if ($this->persistent) {
$this->connection = @oci_pconnect($username, $password, $connectionString);
} else {
$this->connection = @oci_connect($username, $password, $connectionString);
}
if (!$this->connection) {
$this->nuke('Could not connect to '.$username.'@'.$connectionString);
}
}
}
function __destruct() {
if ($this->connection && !$this->persistent) {
@oci_close($this->connection);
}
}
// Error Handling //////////////////////////////////////////////////
private function nuke($message, $context = null) {
$this->broken = true;
if (isset($this->app)) {
$status = 'Oracle: '.$message;
if ($ociErr = @oci_error($context)) $status .= ' ('.$ociErr['message'].')';
$this->app->log($status);
}
}
// This is where the magic happens /////////////////////////////////
public function exec($SQL, &$parameters = null, $autoCommit = true) {
if (!$this->broken && $this->connection) {
// Parameterised queries must use bind variables; these are
// passed by reference as a dictionary of variable => value. We
// use the reference for things like SP OUT parameters, etc.
// Prepare statement
if (!($stid = @oci_parse($this->connection, $SQL))) {
$this->nuke('Could not prepare query.', $this->connection);
return false;
} else {
// Bind any parameters
$bindSuccess = true;
if (count($parameters) > 0) {
foreach ($parameters as $key => $val) {
$bindSuccess = $bindSuccess && @oci_bind_by_name($stid, $key, $parameters[$key], 32767);
}
}
if (!$bindSuccess) {
$this->nuke('Could not bind parameters to query.', $stid);
return false;
} else {
// Execute
if (!@oci_execute($stid, $autoCommit ? OCI_COMMIT_ON_SUCCESS : OCI_DEFAULT)) {
$this->nuke('Could not execute query.', $stid);
return false;
} else {
// Get result set and return
$records = array();
if (@oci_statement_type($stid) == 'SELECT' && @oci_fetch_all($stid, $records, null, null, OCI_FETCHSTATEMENT_BY_ROW) !== false) {
@oci_free_statement($stid);
return $records;
} else {
@oci_free_statement($stid);
return true;
}
}
}
}
} else {
$this->nuke('Cannot execute query; invalid state.');
return false;
}
}
// Commit or rollback a transaction, if necessary
public function finish($commit = true) {
if (!$this->broken && $this->connection) {
if ($commit) {
return @oci_commit($this->connection);
} else {
return @oci_rollback($this->connection);
}
} else {
$this->nuke('Cannot manage transaction; invalid state.');
return false;
}
}
}
?>