Completed
Pull Request — develop (#3507)
by Sergei
63:33
created

SQLAnywhereConnection::errorCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
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;
0 ignored issues
show
introduced by
The function sasql_affected_rows was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
15
use function sasql_commit;
0 ignored issues
show
introduced by
The function sasql_commit was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
16
use function sasql_connect;
0 ignored issues
show
introduced by
The function sasql_connect was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
17
use function sasql_escape_string;
0 ignored issues
show
introduced by
The function sasql_escape_string was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
18
use function sasql_insert_id;
0 ignored issues
show
introduced by
The function sasql_insert_id was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
19
use function sasql_pconnect;
0 ignored issues
show
introduced by
The function sasql_pconnect was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
20
use function sasql_real_query;
0 ignored issues
show
introduced by
The function sasql_real_query was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
21
use function sasql_rollback;
0 ignored issues
show
introduced by
The function sasql_rollback was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
22
use function sasql_set_option;
0 ignored issues
show
introduced by
The function sasql_set_option was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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);
0 ignored issues
show
Bug introduced by
The function sasql_pconnect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        $this->connection = $persistent ? @/** @scrutinizer ignore-call */ sasql_pconnect($dsn) : @sasql_connect($dsn);
Loading history...
Bug introduced by
The function sasql_connect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        $this->connection = $persistent ? @sasql_pconnect($dsn) : @/** @scrutinizer ignore-call */ sasql_connect($dsn);
Loading history...
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)) {
0 ignored issues
show
Bug introduced by
The function sasql_set_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        if (! /** @scrutinizer ignore-call */ sasql_set_option($this->connection, 'verbose_errors', false)) {
Loading history...
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')) {
0 ignored issues
show
Bug introduced by
The function sasql_set_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        if (! /** @scrutinizer ignore-call */ sasql_set_option($this->connection, 'auto_commit', 'off')) {
Loading history...
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)) {
0 ignored issues
show
Bug introduced by
The function sasql_commit was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        if (! /** @scrutinizer ignore-call */ sasql_commit($this->connection)) {
Loading history...
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) {
0 ignored issues
show
Bug introduced by
The function sasql_real_query was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
        if (/** @scrutinizer ignore-call */ sasql_real_query($this->connection, $statement) === false) {
Loading history...
91
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
92
        }
93
94
        return sasql_affected_rows($this->connection);
0 ignored issues
show
Bug introduced by
The function sasql_affected_rows was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
        return /** @scrutinizer ignore-call */ sasql_affected_rows($this->connection);
Loading history...
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);
0 ignored issues
show
Bug introduced by
The function sasql_insert_id was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
            return /** @scrutinizer ignore-call */ sasql_insert_id($this->connection);
Loading history...
116
        }
117
118
        return $this->query('SELECT ' . $name . '.CURRVAL')->fetchColumn();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->query('SEL...URRVAL')->fetchColumn() also could return the type false which is incompatible with the return type mandated by Doctrine\DBAL\Driver\Connection::lastInsertId() of string.
Loading history...
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) . "'";
0 ignored issues
show
Bug introduced by
The function sasql_escape_string was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
        return "'" . /** @scrutinizer ignore-call */ sasql_escape_string($this->connection, $input) . "'";
Loading history...
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)) {
0 ignored issues
show
Bug introduced by
The function sasql_rollback was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

163
        if (! /** @scrutinizer ignore-call */ sasql_rollback($this->connection)) {
Loading history...
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')) {
0 ignored issues
show
Bug introduced by
The function sasql_set_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

177
        if (! /** @scrutinizer ignore-call */ sasql_set_option($this->connection, 'auto_commit', 'on')) {
Loading history...
178
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
179
        }
180
    }
181
}
182