Failed Conditions
Push — master ( 656579...2742cd )
by Marco
11:55
created

SQLAnywhereConnection::errorInfo()   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 0
crap 2
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Driver\SQLAnywhere;
21
22
use Doctrine\DBAL\Driver\Connection;
23
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
24
25
/**
26
 * SAP Sybase SQL Anywhere implementation of the Connection interface.
27
 *
28
 * @author Steve Müller <[email protected]>
29
 * @link   www.doctrine-project.org
30
 * @since  2.5
31
 */
32
class SQLAnywhereConnection implements Connection, ServerInfoAwareConnection
33
{
34
    /**
35
     * @var resource The SQL Anywhere connection resource.
36
     */
37
    private $connection;
38
39
    /**
40
     * Constructor.
41
     *
42
     * Connects to database with given connection string.
43
     *
44
     * @param string  $dsn        The connection string.
45
     * @param boolean $persistent Whether or not to establish a persistent connection.
46
     *
47
     * @throws SQLAnywhereException
48
     */
49
    public function __construct($dsn, $persistent = false)
50
    {
51
        $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

51
        $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

51
        $this->connection = $persistent ? @sasql_pconnect($dsn) : @/** @scrutinizer ignore-call */ sasql_connect($dsn);
Loading history...
52
53
        if ( ! is_resource($this->connection)) {
54
            throw SQLAnywhereException::fromSQLAnywhereError();
55
        }
56
57
        // Disable PHP warnings on error.
58
        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

58
        if ( ! /** @scrutinizer ignore-call */ sasql_set_option($this->connection, 'verbose_errors', false)) {
Loading history...
59
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
60
        }
61
62
        // Enable auto committing by default.
63
        if ( ! sasql_set_option($this->connection, 'auto_commit', 'on')) {
64
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
65
        }
66
67
        // Enable exact, non-approximated row count retrieval.
68
        if ( ! sasql_set_option($this->connection, 'row_counts', true)) {
69
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
70
        }
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     *
76
     * @throws SQLAnywhereException
77
     */
78 View Code Duplication
    public function beginTransaction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        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

80
        if ( ! /** @scrutinizer ignore-call */ sasql_set_option($this->connection, 'auto_commit', 'off')) {
Loading history...
81
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
82
        }
83
84
        return true;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     *
90
     * @throws SQLAnywhereException
91
     */
92 View Code Duplication
    public function commit()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94
        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

94
        if ( ! /** @scrutinizer ignore-call */ sasql_commit($this->connection)) {
Loading history...
95
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
96
        }
97
98
        $this->endTransaction();
99
100
        return true;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function errorCode()
107
    {
108
        return sasql_errorcode($this->connection);
0 ignored issues
show
Bug introduced by
The function sasql_errorcode 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

108
        return /** @scrutinizer ignore-call */ sasql_errorcode($this->connection);
Loading history...
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function errorInfo()
115
    {
116
        return sasql_error($this->connection);
0 ignored issues
show
Bug introduced by
The function sasql_error 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

116
        return /** @scrutinizer ignore-call */ sasql_error($this->connection);
Loading history...
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function exec($statement)
123
    {
124
        if (false === sasql_real_query($this->connection, $statement)) {
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

124
        if (false === /** @scrutinizer ignore-call */ sasql_real_query($this->connection, $statement)) {
Loading history...
125
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
126
        }
127
128
        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

128
        return /** @scrutinizer ignore-call */ sasql_affected_rows($this->connection);
Loading history...
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function getServerVersion()
135
    {
136
        return $this->query("SELECT PROPERTY('ProductVersion')")->fetchColumn();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->query('SEL...sion')')->fetchColumn() also could return the type false which is incompatible with the return type mandated by Doctrine\DBAL\Driver\Ser...ion::getServerVersion() of string.
Loading history...
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function lastInsertId($name = null)
143
    {
144
        if (null === $name) {
145
            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

145
            return /** @scrutinizer ignore-call */ sasql_insert_id($this->connection);
Loading history...
146
        }
147
148
        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...
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function prepare($prepareString)
155
    {
156
        return new SQLAnywhereStatement($this->connection, $prepareString);
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162
    public function query()
163
    {
164
        $args = func_get_args();
165
        $stmt = $this->prepare($args[0]);
166
167
        $stmt->execute();
168
169
        return $stmt;
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function quote($input, $type = \PDO::PARAM_STR)
176
    {
177
        if (is_int($input) || is_float($input)) {
178
            return $input;
179
        }
180
181
        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

181
        return "'" . /** @scrutinizer ignore-call */ sasql_escape_string($this->connection, $input) . "'";
Loading history...
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187
    public function requiresQueryForServerVersion()
188
    {
189
        return true;
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     *
195
     * @throws SQLAnywhereException
196
     */
197 View Code Duplication
    public function rollBack()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
198
    {
199
        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

199
        if ( ! /** @scrutinizer ignore-call */ sasql_rollback($this->connection)) {
Loading history...
200
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
201
        }
202
203
        $this->endTransaction();
204
205
        return true;
206
    }
207
208
    /**
209
     * Ends transactional mode and enables auto commit again.
210
     *
211
     * @throws SQLAnywhereException
212
     *
213
     * @return boolean Whether or not ending transactional mode succeeded.
214
     */
215 View Code Duplication
    private function endTransaction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
216
    {
217
        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

217
        if ( ! /** @scrutinizer ignore-call */ sasql_set_option($this->connection, 'auto_commit', 'on')) {
Loading history...
218
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
219
        }
220
221
        return true;
222
    }
223
}
224