|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByJG\AnyDataset\Store; |
|
4
|
|
|
|
|
5
|
|
|
use ByJG\AnyDataset\DbDriverInterface; |
|
6
|
|
|
use ByJG\AnyDataset\Exception\DatabaseException; |
|
7
|
|
|
use ByJG\AnyDataset\Exception\DatasetException; |
|
8
|
|
|
use ByJG\AnyDataset\Dataset\SqlRelayIterator; |
|
9
|
|
|
use ByJG\AnyDataset\Exception\NotImplementedException; |
|
10
|
|
|
use ByJG\AnyDataset\Store\Helpers\SqlBind; |
|
11
|
|
|
use ByJG\AnyDataset\Store\Helpers\SqlHelper; |
|
12
|
|
|
use ByJG\Util\Uri; |
|
13
|
|
|
|
|
14
|
|
|
class DbSqlRelayDriver implements DbDriverInterface |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Enter description here... |
|
19
|
|
|
* |
|
20
|
|
|
* @var Uri |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $connectionUri; |
|
23
|
|
|
|
|
24
|
|
|
/** Used for SQL Relay connections * */ |
|
25
|
|
|
protected $conn; |
|
26
|
|
|
protected $transaction = false; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(Uri $connUri) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->connectionUri = $connUri; |
|
31
|
|
|
|
|
32
|
|
|
$this->conn = sqlrcon_alloc( |
|
33
|
|
|
$this->connectionUri->getScheme(), |
|
34
|
|
|
$this->connectionUri->getPort(), |
|
35
|
|
|
$this->connectionUri->getQueryPart("unixsocket"), |
|
36
|
|
|
$this->connectionUri->getUsername(), |
|
37
|
|
|
$this->connectionUri->getPassword(), |
|
38
|
|
|
0, |
|
39
|
|
|
1 |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
sqlrcon_autoCommitOn($this->conn); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function __destruct() |
|
46
|
|
|
{ |
|
47
|
|
|
if (!is_null($this->conn)) { |
|
48
|
|
|
sqlrcon_free($this->conn); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function getSQLRelayCursor($sql, $array = null) |
|
53
|
|
|
{ |
|
54
|
|
|
$cur = sqlrcur_alloc($this->conn); |
|
55
|
|
|
|
|
56
|
|
|
if ($array) { |
|
57
|
|
|
list($sql, $array) = SqlBind::parseSQL($this->connectionUri, $sql, $array); |
|
58
|
|
|
|
|
59
|
|
|
sqlrcur_prepareQuery($cur, $sql); |
|
60
|
|
|
$bindCount = 1; |
|
61
|
|
|
foreach ($array as $key => $value) { |
|
62
|
|
|
$field = strval($bindCount ++); |
|
63
|
|
|
sqlrcur_inputBind($cur, $field, $value); |
|
64
|
|
|
} |
|
65
|
|
|
$success = sqlrcur_executeQuery($cur); |
|
66
|
|
|
sqlrcon_endSession($this->conn); |
|
67
|
|
|
} else { |
|
68
|
|
|
$success = sqlrcur_sendQuery($cur, $sql); |
|
69
|
|
|
sqlrcon_endSession($this->conn); |
|
70
|
|
|
} |
|
71
|
|
|
if (!$success) { |
|
72
|
|
|
throw new DatasetException(sqlrcur_errorMessage($cur)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
sqlrcur_lowerCaseColumnNames($cur); |
|
76
|
|
|
return $cur; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function getIterator($sql, $params = null) |
|
80
|
|
|
{ |
|
81
|
|
|
$cur = $this->getSQLRelayCursor($sql, $params); |
|
82
|
|
|
$iterator = new SqlRelayIterator($cur); |
|
83
|
|
|
return $iterator; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getScalar($sql, $array = null) |
|
87
|
|
|
{ |
|
88
|
|
|
$cur = $this->getSQLRelayCursor($sql, $array); |
|
89
|
|
|
$scalar = sqlrcur_getField($cur, 0, 0); |
|
90
|
|
|
sqlrcur_free($cur); |
|
91
|
|
|
|
|
92
|
|
|
return $scalar; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function getAllFields($tablename) |
|
96
|
|
|
{ |
|
97
|
|
|
$cur = sqlrcur_alloc($this->conn); |
|
98
|
|
|
|
|
99
|
|
|
$success = sqlrcur_sendQuery( |
|
100
|
|
|
$cur, |
|
101
|
|
|
SqlHelper::createSafeSQL("select * from :table", array(":table" => $tablename)) |
|
102
|
|
|
); |
|
103
|
|
|
sqlrcon_endSession($cur); |
|
104
|
|
|
|
|
105
|
|
|
if (!$success) { |
|
106
|
|
|
throw new DatasetException(sqlrcur_errorMessage($cur)); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$fields = []; |
|
110
|
|
|
$colCount = sqlrcur_colCount($cur); |
|
111
|
|
|
for ($col = 0; $col < $colCount; $col++) { |
|
112
|
|
|
$fields[] = strtolower(sqlrcur_getColumnName($cur, $col)); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
sqlrcur_free($cur); |
|
116
|
|
|
|
|
117
|
|
|
return $fields; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function beginTransaction() |
|
121
|
|
|
{ |
|
122
|
|
|
$this->transaction = true; |
|
123
|
|
|
sqlrcon_autoCommitOff($this->conn); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
View Code Duplication |
public function commitTransaction() |
|
|
|
|
|
|
127
|
|
|
{ |
|
128
|
|
|
if ($this->transaction) { |
|
129
|
|
|
$this->transaction = false; |
|
130
|
|
|
|
|
131
|
|
|
$ret = sqlrcon_commit($this->conn); |
|
132
|
|
|
if ($ret === 0) { |
|
133
|
|
|
throw new DatabaseException('Commit failed'); |
|
134
|
|
|
} elseif ($ret === -1) { |
|
135
|
|
|
throw new DatabaseException('An error occurred. Commit failed'); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
sqlrcon_autoCommitOn($this->conn); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
View Code Duplication |
public function rollbackTransaction() |
|
|
|
|
|
|
143
|
|
|
{ |
|
144
|
|
|
if ($this->transaction) { |
|
145
|
|
|
$this->transaction = false; |
|
146
|
|
|
|
|
147
|
|
|
$ret = sqlrcon_rollback($this->conn); |
|
148
|
|
|
if ($ret === 0) { |
|
149
|
|
|
throw new DatabaseException('Commit failed'); |
|
150
|
|
|
} elseif ($ret === -1) { |
|
151
|
|
|
throw new DatabaseException('An error occurred. Commit failed'); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
sqlrcon_autoCommitOn($this->conn); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function execute($sql, $array = null) |
|
159
|
|
|
{ |
|
160
|
|
|
$cur = $this->getSQLRelayCursor($sql, $array); |
|
161
|
|
|
sqlrcur_free($cur); |
|
162
|
|
|
return true; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* |
|
167
|
|
|
* @return bool |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getDbConnection() |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->conn; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public function getAttribute($name) |
|
175
|
|
|
{ |
|
176
|
|
|
throw new NotImplementedException('Method not implemented for SQL Relay Driver'); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function setAttribute($name, $value) |
|
180
|
|
|
{ |
|
181
|
|
|
throw new NotImplementedException('Method not implemented for SQL Relay Driver'); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public function executeAndGetId($sql, $array = null) |
|
185
|
|
|
{ |
|
186
|
|
|
throw new NotImplementedException('Method not implemented for SQL Relay Driver'); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
public function getDbHelper() |
|
190
|
|
|
{ |
|
191
|
|
|
throw new NotImplementedException('Method not implemented for SQL Relay Driver'); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return Uri |
|
196
|
|
|
*/ |
|
197
|
|
|
public function getUri() |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->connectionUri; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function isSupportMultRowset() |
|
203
|
|
|
{ |
|
204
|
|
|
throw new NotImplementedException('Method not implemented for SQL Relay Driver'); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function setSupportMultRowset($multipleRowSet) |
|
208
|
|
|
{ |
|
209
|
|
|
throw new NotImplementedException('Method not implemented for SQL Relay Driver'); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.