Failed Conditions
Pull Request — develop (#3335)
by
unknown
12:27
created

SQLAnywhereConnection   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 27
eloc 43
dl 0
loc 188
ccs 0
cts 94
cp 0
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\DBAL\Driver\SQLAnywhere;
4
5
use Doctrine\DBAL\Driver\Connection;
6
use Doctrine\DBAL\Driver\ResultStatement;
7
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
8
use Doctrine\DBAL\Driver\Statement as DriverStatement;
9
use function assert;
10
use function is_resource;
11
use function is_string;
12
use function sasql_affected_rows;
13
use function sasql_commit;
14
use function sasql_connect;
15
use function sasql_error;
16
use function sasql_errorcode;
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
        // Enable exact, non-approximated row count retrieval.
59
        if (! sasql_set_option($this->connection, 'row_counts', true)) {
60
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
61
        }
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     *
67
     * @throws SQLAnywhereException
68
     */
69
    public function beginTransaction() : void
70
    {
71
        if (! sasql_set_option($this->connection, 'auto_commit', 'off')) {
72
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
73
        }
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     *
79
     * @throws SQLAnywhereException
80
     */
81
    public function commit() : void
82
    {
83
        if (! sasql_commit($this->connection)) {
84
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
85
        }
86
87
        $this->endTransaction();
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function errorCode()
94
    {
95
        return sasql_errorcode($this->connection);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function errorInfo()
102
    {
103
        return sasql_error($this->connection);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function exec(string $statement) : int
110
    {
111
        if (sasql_real_query($this->connection, $statement) === false) {
112
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
113
        }
114
115
        return sasql_affected_rows($this->connection);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function getServerVersion()
122
    {
123
        $version = $this->query("SELECT PROPERTY('ProductVersion')")->fetchColumn();
124
125
        assert(is_string($version));
126
127
        return $version;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function lastInsertId(?string $name = null) : string
134
    {
135
        if ($name === null) {
136
            $result = sasql_insert_id($this->connection);
137
138
            if ($result === false) {
139
                throw SQLAnywhereException::fromSQLAnywhereError($this->connection)
140
            }
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected '}' on line 140 at column 12
Loading history...
141
142
            if ($result === 0) {
143
                throw new SQLAnywhereException('The last insert did not affect an AUTOINCREMENT column.');
144
            }
145
146
            return (string) $result;
147
        }
148
149
        $result = $this->query('SELECT ' . $name . '.CURRVAL')->fetchColumn();
150
151
        if ($result === false) {
152
            throw new SQLAnywhereException('No sequence with name "' . $name . '" found.');
153
        }
154
155
        return (string) $result;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function prepare(string $sql) : DriverStatement
162
    {
163
        return new SQLAnywhereStatement($this->connection, $sql);
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function query(string $sql) : ResultStatement
170
    {
171
        $stmt = $this->prepare($sql);
172
        $stmt->execute();
173
174
        return $stmt;
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function quote(string $input) : string
181
    {
182
        return "'" . sasql_escape_string($this->connection, $input) . "'";
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188
    public function requiresQueryForServerVersion()
189
    {
190
        return true;
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     *
196
     * @throws SQLAnywhereException
197
     */
198
    public function rollBack() : void
199
    {
200
        if (! sasql_rollback($this->connection)) {
201
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
202
        }
203
204
        $this->endTransaction();
205
    }
206
207
    /**
208
     * Ends transactional mode and enables auto commit again.
209
     *
210
     * @return bool Whether or not ending transactional mode succeeded.
211
     *
212
     * @throws SQLAnywhereException
213
     */
214
    private function endTransaction()
215
    {
216
        if (! sasql_set_option($this->connection, 'auto_commit', 'on')) {
217
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
218
        }
219
220
        return true;
221
    }
222
}
223