Completed
Push — master ( 5dd66e...5b5c2c )
by Marco
114:19 queued 111:15
created

DBAL/Driver/SQLAnywhere/SQLAnywhereConnection.php (13 issues)

1
<?php
2
3
namespace Doctrine\DBAL\Driver\SQLAnywhere;
4
5
use Doctrine\DBAL\Driver\Connection;
6
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
7
use Doctrine\DBAL\ParameterType;
8
use function assert;
9
use function func_get_args;
10
use function is_float;
11
use function is_int;
12
use function is_resource;
13
use function is_string;
14
use function sasql_affected_rows;
15
use function sasql_commit;
16
use function sasql_connect;
17
use function sasql_error;
18
use function sasql_errorcode;
19
use function sasql_escape_string;
20
use function sasql_insert_id;
21
use function sasql_pconnect;
22
use function sasql_real_query;
23
use function sasql_rollback;
24
use function sasql_set_option;
25
26
/**
27
 * SAP Sybase SQL Anywhere implementation of the Connection interface.
28
 */
29
class SQLAnywhereConnection implements Connection, ServerInfoAwareConnection
30
{
31
    /** @var resource The SQL Anywhere connection resource. */
32
    private $connection;
33
34
    /**
35
     * Connects to database with given connection string.
36
     *
37
     * @param string $dsn        The connection string.
38
     * @param bool   $persistent Whether or not to establish a persistent connection.
39
     *
40
     * @throws SQLAnywhereException
41
     */
42
    public function __construct($dsn, $persistent = false)
43
    {
44
        $this->connection = $persistent ? @sasql_pconnect($dsn) : @sasql_connect($dsn);
0 ignored issues
show
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

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

44
        $this->connection = $persistent ? @/** @scrutinizer ignore-call */ sasql_pconnect($dsn) : @sasql_connect($dsn);
Loading history...
45
46
        if (! is_resource($this->connection)) {
47
            throw SQLAnywhereException::fromSQLAnywhereError();
48
        }
49
50
        // Disable PHP warnings on error.
51
        if (! sasql_set_option($this->connection, 'verbose_errors', false)) {
0 ignored issues
show
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

51
        if (! /** @scrutinizer ignore-call */ sasql_set_option($this->connection, 'verbose_errors', false)) {
Loading history...
52
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
53
        }
54
55
        // Enable auto committing by default.
56
        if (! sasql_set_option($this->connection, 'auto_commit', 'on')) {
57
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
58
        }
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     *
64
     * @throws SQLAnywhereException
65
     */
66
    public function beginTransaction()
67
    {
68
        if (! sasql_set_option($this->connection, 'auto_commit', 'off')) {
0 ignored issues
show
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

68
        if (! /** @scrutinizer ignore-call */ sasql_set_option($this->connection, 'auto_commit', 'off')) {
Loading history...
69
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
70
        }
71
72
        return true;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     *
78
     * @throws SQLAnywhereException
79
     */
80
    public function commit()
81
    {
82
        if (! sasql_commit($this->connection)) {
0 ignored issues
show
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

82
        if (! /** @scrutinizer ignore-call */ sasql_commit($this->connection)) {
Loading history...
83
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
84
        }
85
86
        $this->endTransaction();
87
88
        return true;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function errorCode()
95
    {
96
        return sasql_errorcode($this->connection);
0 ignored issues
show
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

96
        return /** @scrutinizer ignore-call */ sasql_errorcode($this->connection);
Loading history...
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function errorInfo()
103
    {
104
        return sasql_error($this->connection);
0 ignored issues
show
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

104
        return /** @scrutinizer ignore-call */ sasql_error($this->connection);
Loading history...
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function exec($statement)
111
    {
112
        if (sasql_real_query($this->connection, $statement) === false) {
113
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
114
        }
115
116
        return sasql_affected_rows($this->connection);
0 ignored issues
show
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

116
        return /** @scrutinizer ignore-call */ sasql_affected_rows($this->connection);
Loading history...
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function getServerVersion()
123
    {
124
        $version = $this->query("SELECT PROPERTY('ProductVersion')")->fetchColumn();
125
126
        assert(is_string($version));
127
128
        return $version;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function lastInsertId($name = null)
135
    {
136
        if ($name === null) {
137
            return sasql_insert_id($this->connection);
0 ignored issues
show
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

137
            return /** @scrutinizer ignore-call */ sasql_insert_id($this->connection);
Loading history...
138
        }
139
140
        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...
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function prepare($prepareString)
147
    {
148
        return new SQLAnywhereStatement($this->connection, $prepareString);
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function query()
155
    {
156
        $args = func_get_args();
157
        $stmt = $this->prepare($args[0]);
158
159
        $stmt->execute();
160
161
        return $stmt;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function quote($input, $type = ParameterType::STRING)
168
    {
169
        if (is_int($input) || is_float($input)) {
170
            return $input;
171
        }
172
173
        return "'" . sasql_escape_string($this->connection, $input) . "'";
0 ignored issues
show
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

173
        return "'" . /** @scrutinizer ignore-call */ sasql_escape_string($this->connection, $input) . "'";
Loading history...
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    public function requiresQueryForServerVersion()
180
    {
181
        return true;
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     *
187
     * @throws SQLAnywhereException
188
     */
189
    public function rollBack()
190
    {
191
        if (! sasql_rollback($this->connection)) {
0 ignored issues
show
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

191
        if (! /** @scrutinizer ignore-call */ sasql_rollback($this->connection)) {
Loading history...
192
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
193
        }
194
195
        $this->endTransaction();
196
197
        return true;
198
    }
199
200
    /**
201
     * Ends transactional mode and enables auto commit again.
202
     *
203
     * @return bool Whether or not ending transactional mode succeeded.
204
     *
205
     * @throws SQLAnywhereException
206
     */
207
    private function endTransaction()
208
    {
209
        if (! sasql_set_option($this->connection, 'auto_commit', 'on')) {
0 ignored issues
show
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

209
        if (! /** @scrutinizer ignore-call */ sasql_set_option($this->connection, 'auto_commit', 'on')) {
Loading history...
210
            throw SQLAnywhereException::fromSQLAnywhereError($this->connection);
211
        }
212
213
        return true;
214
    }
215
}
216