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
|
|
|
final 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
|
|
|
public function exec(string $statement) : int |
85
|
|
|
{ |
86
|
|
|
if (sasql_real_query($this->connection, $statement) === false) { |
|
|
|
|
87
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError($this->connection); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return sasql_affected_rows($this->connection); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getServerVersion() : string |
94
|
|
|
{ |
95
|
|
|
$version = $this->query("SELECT PROPERTY('ProductVersion')")->fetchColumn(); |
96
|
|
|
|
97
|
|
|
assert(is_string($version)); |
98
|
|
|
|
99
|
|
|
return $version; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function lastInsertId(?string $name = null) : string |
103
|
|
|
{ |
104
|
|
|
if ($name === null) { |
105
|
|
|
return sasql_insert_id($this->connection); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->query('SELECT ' . $name . '.CURRVAL')->fetchColumn(); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function prepare(string $sql) : DriverStatement |
112
|
|
|
{ |
113
|
|
|
return new SQLAnywhereStatement($this->connection, $sql); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function query(string $sql) : ResultStatement |
117
|
|
|
{ |
118
|
|
|
$stmt = $this->prepare($sql); |
119
|
|
|
$stmt->execute(); |
120
|
|
|
|
121
|
|
|
return $stmt; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function quote(string $input) : string |
125
|
|
|
{ |
126
|
|
|
return "'" . sasql_escape_string($this->connection, $input) . "'"; |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
* |
132
|
|
|
* @throws SQLAnywhereException |
133
|
|
|
*/ |
134
|
|
|
public function rollBack() : void |
135
|
|
|
{ |
136
|
|
|
if (! sasql_rollback($this->connection)) { |
|
|
|
|
137
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError($this->connection); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$this->endTransaction(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Ends transactional mode and enables auto commit again. |
145
|
|
|
* |
146
|
|
|
* @throws SQLAnywhereException |
147
|
|
|
*/ |
148
|
|
|
private function endTransaction() : void |
149
|
|
|
{ |
150
|
|
|
if (! sasql_set_option($this->connection, 'auto_commit', 'on')) { |
|
|
|
|
151
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError($this->connection); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|