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