|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByJG\AnyDataset\Database; |
|
4
|
|
|
|
|
5
|
|
|
use ByJG\AnyDataset\ConnectionManagement; |
|
6
|
|
|
use ByJG\AnyDataset\Exception\DatabaseException; |
|
7
|
|
|
use ByJG\AnyDataset\Repository\Oci8Iterator; |
|
8
|
|
|
|
|
9
|
|
|
class DbOci8Driver implements DbDriverInterface |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Enter description here... |
|
14
|
|
|
* |
|
15
|
|
|
* @var ConnectionManagement |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $connectionManagement; |
|
18
|
|
|
|
|
19
|
|
|
/** Used for OCI8 connections * */ |
|
20
|
|
|
protected $conn; |
|
21
|
|
|
protected $transaction = OCI_COMMIT_ON_SUCCESS; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Ex. |
|
25
|
|
|
* |
|
26
|
|
|
* oci8://username:password@host:1521/servicename?protocol=TCP&codepage=WE8MSWIN1252 |
|
27
|
|
|
* |
|
28
|
|
|
* @param ConnectionManagement $connMngt |
|
29
|
|
|
* @throws DatabaseException |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct($connMngt) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->connectionManagement = $connMngt; |
|
34
|
|
|
|
|
35
|
|
|
$codePage = $this->connectionManagement->getExtraParam("codepage"); |
|
36
|
|
|
$codePage = ($codePage == "") ? 'UTF8' : $codePage; |
|
37
|
|
|
|
|
38
|
|
|
$tns = DbOci8Driver::getTnsString($connMngt); |
|
39
|
|
|
|
|
40
|
|
|
$this->conn = oci_connect( |
|
41
|
|
|
$this->connectionManagement->getUsername(), |
|
42
|
|
|
$this->connectionManagement->getPassword(), |
|
43
|
|
|
$tns, |
|
44
|
|
|
$codePage |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
if (!$this->conn) { |
|
48
|
|
|
$error = oci_error(); |
|
49
|
|
|
throw new DatabaseException($error['message']); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* |
|
55
|
|
|
* @param ConnectionManagement $connMngt |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function getTnsString($connMngt) |
|
59
|
|
|
{ |
|
60
|
|
|
$protocol = $connMngt->getExtraParam("protocol"); |
|
61
|
|
|
$protocol = ($protocol == "") ? 'TCP' : $protocol; |
|
62
|
|
|
|
|
63
|
|
|
$port = $connMngt->getPort(); |
|
64
|
|
|
$port = ($port == "") ? 1521 : $port; |
|
65
|
|
|
|
|
66
|
|
|
$svcName = $connMngt->getDatabase(); |
|
67
|
|
|
|
|
68
|
|
|
$host = $connMngt->getServer(); |
|
69
|
|
|
|
|
70
|
|
|
$tns = "(DESCRIPTION = " . |
|
71
|
|
|
" (ADDRESS = (PROTOCOL = $protocol)(HOST = $host)(PORT = $port)) " . |
|
72
|
|
|
" (CONNECT_DATA = (SERVICE_NAME = $svcName)) " . |
|
73
|
|
|
")"; |
|
74
|
|
|
|
|
75
|
|
|
return $tns; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function __destruct() |
|
79
|
|
|
{ |
|
80
|
|
|
$this->conn = null; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function getOci8Cursor($sql, $array = null) |
|
84
|
|
|
{ |
|
85
|
|
|
list($query, $array) = SqlBind::parseSQL($this->connectionManagement, $sql, $array); |
|
86
|
|
|
|
|
87
|
|
|
// Prepare the statement |
|
88
|
|
|
$stid = oci_parse($this->conn, $query); |
|
89
|
|
|
if (!$stid) { |
|
90
|
|
|
$error = oci_error($this->conn); |
|
91
|
|
|
throw new DatabaseException($error['message']); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// Bind the parameters |
|
95
|
|
|
if (is_array($array)) { |
|
96
|
|
|
foreach ($array as $key => $value) { |
|
97
|
|
|
oci_bind_by_name($stid, ":$key", $value); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// Perform the logic of the query |
|
102
|
|
|
$result = oci_execute($stid, $this->transaction); |
|
103
|
|
|
|
|
104
|
|
|
// Check if is OK; |
|
105
|
|
|
if (!$result) { |
|
106
|
|
|
$error = oci_error($stid); |
|
107
|
|
|
throw new DatabaseException($error['message']); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $stid; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param $sql |
|
115
|
|
|
* @param null $array |
|
116
|
|
|
* @return \ByJG\AnyDataset\Repository\Oci8Iterator |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getIterator($sql, $array = null) |
|
119
|
|
|
{ |
|
120
|
|
|
$cur = $this->getOci8Cursor($sql, $array); |
|
121
|
|
|
$iterator = new Oci8Iterator($cur); |
|
122
|
|
|
return $iterator; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function getScalar($sql, $array = null) |
|
126
|
|
|
{ |
|
127
|
|
|
$cur = $this->getOci8Cursor($sql, $array); |
|
128
|
|
|
|
|
129
|
|
|
$row = oci_fetch_array($cur, OCI_RETURN_NULLS); |
|
130
|
|
|
if ($row) { |
|
|
|
|
|
|
131
|
|
|
$scalar = $row[0]; |
|
132
|
|
|
} else { |
|
133
|
|
|
$scalar = null; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
oci_free_cursor($cur); |
|
137
|
|
|
|
|
138
|
|
|
return $scalar; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function getAllFields($tablename) |
|
142
|
|
|
{ |
|
143
|
|
|
$cur = $this->getOci8Cursor(SqlHelper::createSafeSQL("select * from :table", array(':table' => $tablename))); |
|
144
|
|
|
|
|
145
|
|
|
$ncols = oci_num_fields($cur); |
|
146
|
|
|
|
|
147
|
|
|
$fields = array(); |
|
148
|
|
|
for ($i = 1; $i <= $ncols; $i++) { |
|
149
|
|
|
$fields[] = strtolower(oci_field_name($cur, $i)); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
oci_free_statement($cur); |
|
153
|
|
|
|
|
154
|
|
|
return $fields; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function beginTransaction() |
|
158
|
|
|
{ |
|
159
|
|
|
$this->transaction = OCI_NO_AUTO_COMMIT; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function commitTransaction() |
|
163
|
|
|
{ |
|
164
|
|
|
if ($this->transaction == OCI_COMMIT_ON_SUCCESS) { |
|
165
|
|
|
throw new DatabaseException('No transaction for commit'); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$this->transaction = OCI_COMMIT_ON_SUCCESS; |
|
169
|
|
|
|
|
170
|
|
|
$result = oci_commit($this->conn); |
|
171
|
|
|
if (!$result) { |
|
172
|
|
|
$error = oci_error($this->conn); |
|
173
|
|
|
throw new DatabaseException($error['message']); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function rollbackTransaction() |
|
178
|
|
|
{ |
|
179
|
|
|
if ($this->transaction == OCI_COMMIT_ON_SUCCESS) { |
|
180
|
|
|
throw new DatabaseException('No transaction for rollback'); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
$this->transaction = OCI_COMMIT_ON_SUCCESS; |
|
184
|
|
|
|
|
185
|
|
|
oci_rollback($this->conn); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function executeSql($sql, $array = null) |
|
189
|
|
|
{ |
|
190
|
|
|
$cur = $this->getOci8Cursor($sql, $array); |
|
191
|
|
|
oci_free_cursor($cur); |
|
192
|
|
|
return true; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* |
|
197
|
|
|
* @return resource |
|
198
|
|
|
*/ |
|
199
|
|
|
public function getDbConnection() |
|
200
|
|
|
{ |
|
201
|
|
|
return $this->conn; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
public function getAttribute($name) |
|
205
|
|
|
{ |
|
206
|
|
|
throw new \Exception('Method not implemented for OCI Driver'); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
public function setAttribute($name, $value) |
|
210
|
|
|
{ |
|
211
|
|
|
throw new \Exception('Method not implemented for OCI Driver'); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.