Completed
Pull Request — master (#3759)
by Benjamin
61:16
created

SQLAnywhereConnection::prepare()   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 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 2
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;
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...
14
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...
15
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...
16
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...
17
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...
18
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...
19
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...
20
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...
21
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...
22
23
/**
24
 * SAP Sybase SQL Anywhere implementation of the Connection interface.
25
 */
26
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);
0 ignored issues
show
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

41
        $this->connection = $persistent ? @sasql_pconnect($dsn) : @/** @scrutinizer ignore-call */ sasql_connect($dsn);
Loading history...
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

41
        $this->connection = $persistent ? @/** @scrutinizer ignore-call */ sasql_pconnect($dsn) : @sasql_connect($dsn);
Loading history...
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)) {
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

48
        if (! /** @scrutinizer ignore-call */ sasql_set_option($this->connection, 'verbose_errors', false)) {
Loading history...
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')) {
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

65
        if (! /** @scrutinizer ignore-call */ sasql_set_option($this->connection, 'auto_commit', 'off')) {
Loading history...
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)) {
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

77
        if (! /** @scrutinizer ignore-call */ sasql_commit($this->connection)) {
Loading history...
78
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
79
        }
80
81
        $this->endTransaction();
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function exec(string $statement) : int
88
    {
89
        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

89
        if (/** @scrutinizer ignore-call */ sasql_real_query($this->connection, $statement) === false) {
Loading history...
90
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
91
        }
92
93
        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

93
        return /** @scrutinizer ignore-call */ sasql_affected_rows($this->connection);
Loading history...
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function getServerVersion() : string
100
    {
101
        $version = $this->query("SELECT PROPERTY('ProductVersion')")->fetchColumn();
102
103
        assert(is_string($version));
104
105
        return $version;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function lastInsertId() : string
112
    {
113
        $result = 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

113
        $result = /** @scrutinizer ignore-call */ sasql_insert_id($this->connection);
Loading history...
114
115
        // See the following link for possible return values:
116
        // http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc01776.1600/doc/html/san1357754986165.html
117
118
        if ($result === false) {
119
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
120
        }
121
122
        if ($result === 0) {
123
            throw new SQLAnywhereException('No identity value was generated by the last statement.');
124
        }
125
126
        return (string) $result;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function getSequenceNumber(string $name) : string
133
    {
134
        $result = $this->query('SELECT ' . $name . '.CURRVAL')->fetchColumn();
135
136
        if ($result === false) {
137
            throw new SQLAnywhereException('No sequence with name "' . $name . '" found.');
138
        }
139
140
        return (string) $result;
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function prepare(string $sql) : DriverStatement
147
    {
148
        return new SQLAnywhereStatement($this->connection, $sql);
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function query(string $sql) : ResultStatement
155
    {
156
        $stmt = $this->prepare($sql);
157
        $stmt->execute();
158
159
        return $stmt;
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function quote(string $input) : string
166
    {
167
        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

167
        return "'" . /** @scrutinizer ignore-call */ sasql_escape_string($this->connection, $input) . "'";
Loading history...
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function requiresQueryForServerVersion() : bool
174
    {
175
        return true;
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     *
181
     * @throws SQLAnywhereException
182
     */
183
    public function rollBack() : void
184
    {
185
        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

185
        if (! /** @scrutinizer ignore-call */ sasql_rollback($this->connection)) {
Loading history...
186
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
187
        }
188
189
        $this->endTransaction();
190
    }
191
192
    /**
193
     * Ends transactional mode and enables auto commit again.
194
     *
195
     * @throws SQLAnywhereException
196
     */
197
    private function endTransaction() : void
198
    {
199
        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

199
        if (! /** @scrutinizer ignore-call */ sasql_set_option($this->connection, 'auto_commit', 'on')) {
Loading history...
200
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
201
        }
202
    }
203
}
204