1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Driver\SQLAnywhere; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Driver\Connection; |
6
|
|
|
use Doctrine\DBAL\Driver\DriverException; |
7
|
|
|
use Doctrine\DBAL\Driver\ResultStatement; |
8
|
|
|
use Doctrine\DBAL\Driver\ServerInfoAwareConnection; |
9
|
|
|
use Doctrine\DBAL\Driver\Statement as DriverStatement; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use function assert; |
12
|
|
|
use function is_resource; |
13
|
|
|
use function is_string; |
14
|
|
|
use function sasql_affected_rows; |
|
|
|
|
15
|
|
|
use function sasql_commit; |
|
|
|
|
16
|
|
|
use function sasql_connect; |
|
|
|
|
17
|
|
|
use function sasql_error; |
|
|
|
|
18
|
|
|
use function sasql_errorcode; |
|
|
|
|
19
|
|
|
use function sasql_escape_string; |
|
|
|
|
20
|
|
|
use function sasql_insert_id; |
|
|
|
|
21
|
|
|
use function sasql_pconnect; |
|
|
|
|
22
|
|
|
use function sasql_real_query; |
|
|
|
|
23
|
|
|
use function sasql_rollback; |
|
|
|
|
24
|
|
|
use function sasql_set_option; |
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* SAP Sybase SQL Anywhere implementation of the Connection interface. |
28
|
|
|
*/ |
29
|
|
|
class SQLAnywhereConnection implements Connection, ServerInfoAwareConnection |
30
|
|
|
{ |
31
|
|
|
/** @var resource The SQL Anywhere connection resource. */ |
32
|
|
|
private $connection; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Connects to database with given connection string. |
36
|
|
|
* |
37
|
|
|
* @param string $dsn The connection string. |
38
|
|
|
* @param bool $persistent Whether or not to establish a persistent connection. |
39
|
|
|
* |
40
|
|
|
* @throws DriverException |
41
|
|
|
*/ |
42
|
|
|
public function __construct($dsn, $persistent = false) |
43
|
|
|
{ |
44
|
|
|
$this->connection = $persistent ? @sasql_pconnect($dsn) : @sasql_connect($dsn); |
|
|
|
|
45
|
|
|
|
46
|
|
|
if (! is_resource($this->connection)) { |
47
|
|
|
throw self::exceptionFromSQLAnywhereError(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// Disable PHP warnings on error. |
51
|
|
|
if (! sasql_set_option($this->connection, 'verbose_errors', false)) { |
|
|
|
|
52
|
|
|
throw self::exceptionFromSQLAnywhereError($this->connection); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Enable auto committing by default. |
56
|
|
|
if (! sasql_set_option($this->connection, 'auto_commit', 'on')) { |
57
|
|
|
throw self::exceptionFromSQLAnywhereError($this->connection); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Enable exact, non-approximated row count retrieval. |
61
|
|
|
if (! sasql_set_option($this->connection, 'row_counts', true)) { |
62
|
|
|
throw self::exceptionFromSQLAnywhereError($this->connection); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function beginTransaction() : void |
70
|
|
|
{ |
71
|
|
|
if (! sasql_set_option($this->connection, 'auto_commit', 'off')) { |
|
|
|
|
72
|
|
|
throw self::exceptionFromSQLAnywhereError($this->connection); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function commit() : void |
80
|
|
|
{ |
81
|
|
|
if (! sasql_commit($this->connection)) { |
|
|
|
|
82
|
|
|
throw self::exceptionFromSQLAnywhereError($this->connection); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->endTransaction(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function errorCode() |
92
|
|
|
{ |
93
|
|
|
return sasql_errorcode($this->connection); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function errorInfo() |
100
|
|
|
{ |
101
|
|
|
return sasql_error($this->connection); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function exec(string $statement) : int |
108
|
|
|
{ |
109
|
|
|
if (sasql_real_query($this->connection, $statement) === false) { |
|
|
|
|
110
|
|
|
throw self::exceptionFromSQLAnywhereError($this->connection); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return sasql_affected_rows($this->connection); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function getServerVersion() |
120
|
|
|
{ |
121
|
|
|
$version = $this->query("SELECT PROPERTY('ProductVersion')")->fetchColumn(); |
122
|
|
|
|
123
|
|
|
assert(is_string($version)); |
124
|
|
|
|
125
|
|
|
return $version; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function lastInsertId(?string $name = null) : string |
132
|
|
|
{ |
133
|
|
|
if ($name === null) { |
134
|
|
|
$result = sasql_insert_id($this->connection); |
|
|
|
|
135
|
|
|
|
136
|
|
|
if ($result === false) { |
137
|
|
|
throw self::exceptionFromSQLAnywhereError($this->connection); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if ($result === 0) { |
141
|
|
|
throw DriverException::noInsertId(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return (string) $result; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$result = $this->query('SELECT ' . $name . '.CURRVAL')->fetchColumn(); |
148
|
|
|
|
149
|
|
|
if ($result === false) { |
150
|
|
|
throw DriverException::noSuchSequence($name); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return (string) $result; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritdoc} |
158
|
|
|
*/ |
159
|
|
|
public function prepare(string $sql) : DriverStatement |
160
|
|
|
{ |
161
|
|
|
return new SQLAnywhereStatement($this->connection, $sql); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritdoc} |
166
|
|
|
*/ |
167
|
|
|
public function query(string $sql) : ResultStatement |
168
|
|
|
{ |
169
|
|
|
$stmt = $this->prepare($sql); |
170
|
|
|
$stmt->execute(); |
171
|
|
|
|
172
|
|
|
return $stmt; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* {@inheritdoc} |
177
|
|
|
*/ |
178
|
|
|
public function quote(string $input) : string |
179
|
|
|
{ |
180
|
|
|
return "'" . sasql_escape_string($this->connection, $input) . "'"; |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritdoc} |
185
|
|
|
*/ |
186
|
|
|
public function requiresQueryForServerVersion() |
187
|
|
|
{ |
188
|
|
|
return true; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
|
|
*/ |
194
|
|
|
public function rollBack() : void |
195
|
|
|
{ |
196
|
|
|
if (! sasql_rollback($this->connection)) { |
|
|
|
|
197
|
|
|
throw self::exceptionFromSQLAnywhereError($this->connection); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$this->endTransaction(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Ends transactional mode and enables auto commit again. |
205
|
|
|
* |
206
|
|
|
* @return bool Whether or not ending transactional mode succeeded. |
207
|
|
|
* |
208
|
|
|
* @throws DriverException |
209
|
|
|
*/ |
210
|
|
|
private function endTransaction() |
211
|
|
|
{ |
212
|
|
|
if (! sasql_set_option($this->connection, 'auto_commit', 'on')) { |
|
|
|
|
213
|
|
|
throw self::exceptionFromSQLAnywhereError($this->connection); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return true; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Helper method to turn SQL Anywhere error into exception. |
221
|
|
|
* |
222
|
|
|
* @param resource|null $conn The SQL Anywhere connection resource to retrieve the last error from. |
223
|
|
|
* @param resource|null $stmt The SQL Anywhere statement resource to retrieve the last error from. |
224
|
|
|
* |
225
|
|
|
* @throws InvalidArgumentException |
226
|
|
|
*/ |
227
|
|
|
public static function exceptionFromSQLAnywhereError($conn = null, $stmt = null) : DriverException |
228
|
|
|
{ |
229
|
|
|
if ($conn !== null && ! is_resource($conn)) { |
230
|
|
|
throw new InvalidArgumentException('Invalid SQL Anywhere connection resource given: ' . $conn); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
if ($stmt !== null && ! is_resource($stmt)) { |
234
|
|
|
throw new InvalidArgumentException('Invalid SQL Anywhere statement resource given: ' . $stmt); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
$state = $conn ? sasql_sqlstate($conn) : sasql_sqlstate(); |
|
|
|
|
238
|
|
|
$code = null; |
239
|
|
|
$message = null; |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Try retrieving the last error from statement resource if given |
243
|
|
|
*/ |
244
|
|
|
if ($stmt) { |
|
|
|
|
245
|
|
|
$code = sasql_stmt_errno($stmt); |
|
|
|
|
246
|
|
|
$message = sasql_stmt_error($stmt); |
|
|
|
|
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Try retrieving the last error from the connection resource |
251
|
|
|
* if either the statement resource is not given or the statement |
252
|
|
|
* resource is given but the last error could not be retrieved from it (fallback). |
253
|
|
|
* Depending on the type of error, it is sometimes necessary to retrieve |
254
|
|
|
* it from the connection resource even though it occurred during |
255
|
|
|
* a prepared statement. |
256
|
|
|
*/ |
257
|
|
|
if ($conn && ! $code) { |
|
|
|
|
258
|
|
|
$code = sasql_errorcode($conn); |
|
|
|
|
259
|
|
|
$message = sasql_error($conn); |
|
|
|
|
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Fallback mode if either no connection resource is given |
264
|
|
|
* or the last error could not be retrieved from the given |
265
|
|
|
* connection / statement resource. |
266
|
|
|
*/ |
267
|
|
|
if (! $conn || ! $code) { |
|
|
|
|
268
|
|
|
$code = sasql_errorcode(); |
269
|
|
|
$message = sasql_error(); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
if ($message) { |
273
|
|
|
return new DriverException('SQLSTATE [' . $state . '] [' . $code . '] ' . $message, $state, $code); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return new DriverException('SQL Anywhere error occurred but no error message was retrieved from driver.', $state, $code); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|