1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Driver\SQLAnywhere; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Driver\ResultStatement; |
8
|
|
|
use Doctrine\DBAL\Driver\ServerInfoAwareConnection; |
9
|
|
|
use Doctrine\DBAL\Driver\Statement as DriverStatement; |
10
|
|
|
use function assert; |
11
|
|
|
use function is_resource; |
12
|
|
|
use function is_string; |
13
|
|
|
use function sasql_affected_rows; |
|
|
|
|
14
|
|
|
use function sasql_commit; |
|
|
|
|
15
|
|
|
use function sasql_connect; |
|
|
|
|
16
|
|
|
use function sasql_escape_string; |
|
|
|
|
17
|
|
|
use function sasql_insert_id; |
|
|
|
|
18
|
|
|
use function sasql_pconnect; |
|
|
|
|
19
|
|
|
use function sasql_real_query; |
|
|
|
|
20
|
|
|
use function sasql_rollback; |
|
|
|
|
21
|
|
|
use function sasql_set_option; |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* SAP Sybase SQL Anywhere implementation of the Connection interface. |
25
|
|
|
*/ |
26
|
|
|
class SQLAnywhereConnection implements ServerInfoAwareConnection |
27
|
|
|
{ |
28
|
|
|
/** @var resource The SQL Anywhere connection resource. */ |
29
|
|
|
private $connection; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Connects to database with given connection string. |
33
|
|
|
* |
34
|
|
|
* @param string $dsn The connection string. |
35
|
|
|
* @param bool $persistent Whether or not to establish a persistent connection. |
36
|
|
|
* |
37
|
|
|
* @throws SQLAnywhereException |
38
|
|
|
*/ |
39
|
|
|
public function __construct(string $dsn, bool $persistent = false) |
40
|
|
|
{ |
41
|
|
|
$this->connection = $persistent ? @sasql_pconnect($dsn) : @sasql_connect($dsn); |
|
|
|
|
42
|
|
|
|
43
|
|
|
if (! is_resource($this->connection)) { |
44
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Disable PHP warnings on error. |
48
|
|
|
if (! sasql_set_option($this->connection, 'verbose_errors', false)) { |
|
|
|
|
49
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError($this->connection); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Enable auto committing by default. |
53
|
|
|
if (! sasql_set_option($this->connection, 'auto_commit', 'on')) { |
54
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError($this->connection); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
* |
61
|
|
|
* @throws SQLAnywhereException |
62
|
|
|
*/ |
63
|
|
|
public function beginTransaction() : void |
64
|
|
|
{ |
65
|
|
|
if (! sasql_set_option($this->connection, 'auto_commit', 'off')) { |
|
|
|
|
66
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError($this->connection); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
* |
73
|
|
|
* @throws SQLAnywhereException |
74
|
|
|
*/ |
75
|
|
|
public function commit() : void |
76
|
|
|
{ |
77
|
|
|
if (! sasql_commit($this->connection)) { |
|
|
|
|
78
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError($this->connection); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->endTransaction(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function exec(string $statement) : int |
88
|
|
|
{ |
89
|
|
|
if (sasql_real_query($this->connection, $statement) === false) { |
|
|
|
|
90
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError($this->connection); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return sasql_affected_rows($this->connection); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function getServerVersion() : string |
100
|
|
|
{ |
101
|
|
|
$version = $this->query("SELECT PROPERTY('ProductVersion')")->fetchColumn(); |
102
|
|
|
|
103
|
|
|
assert(is_string($version)); |
104
|
|
|
|
105
|
|
|
return $version; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public function lastInsertId() |
112
|
|
|
{ |
113
|
|
|
$result = sasql_insert_id($this->connection); |
|
|
|
|
114
|
|
|
|
115
|
|
|
// See the following link for possible return values: |
116
|
|
|
// http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc01776.1600/doc/html/san1357754986165.html |
117
|
|
|
|
118
|
|
|
if ($result === false) { |
119
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError($this->connection); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($result === 0) { |
123
|
|
|
throw new SQLAnywhereException('No identity value was generated by the last statement.'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $result; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
|
|
public function getSequenceNumber(string $name) |
133
|
|
|
{ |
134
|
|
|
$result = $this->query('SELECT ' . $name . '.CURRVAL')->fetchColumn(); |
135
|
|
|
|
136
|
|
|
if ($result === false) { |
137
|
|
|
throw new SQLAnywhereException('No sequence with name "' . $name . '" found.'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $result; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
|
|
public function prepare(string $sql) : DriverStatement |
147
|
|
|
{ |
148
|
|
|
return new SQLAnywhereStatement($this->connection, $sql); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
|
|
public function query(string $sql) : ResultStatement |
155
|
|
|
{ |
156
|
|
|
$stmt = $this->prepare($sql); |
157
|
|
|
$stmt->execute(); |
158
|
|
|
|
159
|
|
|
return $stmt; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
|
|
public function quote(string $input) : string |
166
|
|
|
{ |
167
|
|
|
return "'" . sasql_escape_string($this->connection, $input) . "'"; |
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
|
|
public function requiresQueryForServerVersion() : bool |
174
|
|
|
{ |
175
|
|
|
return true; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritdoc} |
180
|
|
|
* |
181
|
|
|
* @throws SQLAnywhereException |
182
|
|
|
*/ |
183
|
|
|
public function rollBack() : void |
184
|
|
|
{ |
185
|
|
|
if (! sasql_rollback($this->connection)) { |
|
|
|
|
186
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError($this->connection); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$this->endTransaction(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Ends transactional mode and enables auto commit again. |
194
|
|
|
* |
195
|
|
|
* @throws SQLAnywhereException |
196
|
|
|
*/ |
197
|
|
|
private function endTransaction() : void |
198
|
|
|
{ |
199
|
|
|
if (! sasql_set_option($this->connection, 'auto_commit', 'on')) { |
|
|
|
|
200
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError($this->connection); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|