1 | <?php |
||
2 | |||
3 | namespace Doctrine\DBAL\Driver\SQLAnywhere; |
||
4 | |||
5 | use Doctrine\DBAL\Driver\Connection; |
||
6 | use Doctrine\DBAL\Driver\ServerInfoAwareConnection; |
||
7 | use Doctrine\DBAL\ParameterType; |
||
8 | use function assert; |
||
9 | use function func_get_args; |
||
10 | use function is_float; |
||
11 | use function is_int; |
||
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 SQLAnywhereException |
||
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 SQLAnywhereException::fromSQLAnywhereError(); |
||
48 | } |
||
49 | |||
50 | // Disable PHP warnings on error. |
||
51 | if (! sasql_set_option($this->connection, 'verbose_errors', false)) { |
||
52 | throw SQLAnywhereException::fromSQLAnywhereError($this->connection); |
||
53 | } |
||
54 | |||
55 | // Enable auto committing by default. |
||
56 | if (! sasql_set_option($this->connection, 'auto_commit', 'on')) { |
||
57 | throw SQLAnywhereException::fromSQLAnywhereError($this->connection); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | * |
||
64 | * @throws SQLAnywhereException |
||
65 | */ |
||
66 | public function beginTransaction() |
||
67 | { |
||
68 | if (! sasql_set_option($this->connection, 'auto_commit', 'off')) { |
||
69 | throw SQLAnywhereException::fromSQLAnywhereError($this->connection); |
||
70 | } |
||
71 | |||
72 | return true; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | * |
||
78 | * @throws SQLAnywhereException |
||
79 | */ |
||
80 | public function commit() |
||
81 | { |
||
82 | if (! sasql_commit($this->connection)) { |
||
83 | throw SQLAnywhereException::fromSQLAnywhereError($this->connection); |
||
84 | } |
||
85 | |||
86 | $this->endTransaction(); |
||
87 | |||
88 | return true; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | public function errorCode() |
||
95 | { |
||
96 | return sasql_errorcode($this->connection); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | public function errorInfo() |
||
103 | { |
||
104 | return sasql_error($this->connection); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | public function exec($statement) |
||
111 | { |
||
112 | if (sasql_real_query($this->connection, $statement) === false) { |
||
113 | throw SQLAnywhereException::fromSQLAnywhereError($this->connection); |
||
114 | } |
||
115 | |||
116 | return sasql_affected_rows($this->connection); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | public function getServerVersion() |
||
123 | { |
||
124 | $version = $this->query("SELECT PROPERTY('ProductVersion')")->fetchColumn(); |
||
125 | |||
126 | assert(is_string($version)); |
||
127 | |||
128 | return $version; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | public function lastInsertId($name = null) |
||
135 | { |
||
136 | if ($name === null) { |
||
137 | return sasql_insert_id($this->connection); |
||
138 | } |
||
139 | |||
140 | return $this->query('SELECT ' . $name . '.CURRVAL')->fetchColumn(); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
141 | } |
||
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | public function prepare($prepareString) |
||
147 | { |
||
148 | return new SQLAnywhereStatement($this->connection, $prepareString); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | public function query() |
||
155 | { |
||
156 | $args = func_get_args(); |
||
157 | $stmt = $this->prepare($args[0]); |
||
158 | |||
159 | $stmt->execute(); |
||
160 | |||
161 | return $stmt; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | public function quote($input, $type = ParameterType::STRING) |
||
168 | { |
||
169 | if (is_int($input) || is_float($input)) { |
||
170 | return $input; |
||
171 | } |
||
172 | |||
173 | return "'" . sasql_escape_string($this->connection, $input) . "'"; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * {@inheritdoc} |
||
178 | */ |
||
179 | public function requiresQueryForServerVersion() |
||
180 | { |
||
181 | return true; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * {@inheritdoc} |
||
186 | * |
||
187 | * @throws SQLAnywhereException |
||
188 | */ |
||
189 | public function rollBack() |
||
190 | { |
||
191 | if (! sasql_rollback($this->connection)) { |
||
192 | throw SQLAnywhereException::fromSQLAnywhereError($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 SQLAnywhereException |
||
206 | */ |
||
207 | private function endTransaction() |
||
208 | { |
||
209 | if (! sasql_set_option($this->connection, 'auto_commit', 'on')) { |
||
210 | throw SQLAnywhereException::fromSQLAnywhereError($this->connection); |
||
211 | } |
||
212 | |||
213 | return true; |
||
214 | } |
||
215 | } |
||
216 |