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