Completed
Pull Request — master (#3807)
by Sergei
155:06 queued 90: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 $name = null) : string
112
    {
113
        if ($name === null) {
114
            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

114
            return /** @scrutinizer ignore-call */ sasql_insert_id($this->connection);
Loading history...
115
        }
116
117
        return $this->query('SELECT ' . $name . '.CURRVAL')->fetchColumn();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->query('SEL...URRVAL')->fetchColumn() could return the type false which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function prepare(string $sql) : DriverStatement
124
    {
125
        return new SQLAnywhereStatement($this->connection, $sql);
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function query(string $sql) : ResultStatement
132
    {
133
        $stmt = $this->prepare($sql);
134
        $stmt->execute();
135
136
        return $stmt;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function quote(string $input) : string
143
    {
144
        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

144
        return "'" . /** @scrutinizer ignore-call */ sasql_escape_string($this->connection, $input) . "'";
Loading history...
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     *
150
     * @throws SQLAnywhereException
151
     */
152
    public function rollBack() : void
153
    {
154
        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

154
        if (! /** @scrutinizer ignore-call */ sasql_rollback($this->connection)) {
Loading history...
155
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
156
        }
157
158
        $this->endTransaction();
159
    }
160
161
    /**
162
     * Ends transactional mode and enables auto commit again.
163
     *
164
     * @throws SQLAnywhereException
165
     */
166
    private function endTransaction() : void
167
    {
168
        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

168
        if (! /** @scrutinizer ignore-call */ sasql_set_option($this->connection, 'auto_commit', 'on')) {
Loading history...
169
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
170
        }
171
    }
172
}
173