Passed
Pull Request — master (#3070)
by Sergei
07:45
created

SQLAnywhereStatement::columnCount()   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
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver\SQLAnywhere;
6
7
use Doctrine\DBAL\Driver\Statement;
8
use Doctrine\DBAL\Driver\StatementIterator;
9
use Doctrine\DBAL\Exception\GetVariableType;
10
use Doctrine\DBAL\FetchMode;
11
use Doctrine\DBAL\ParameterType;
12
use IteratorAggregate;
13
use function array_key_exists;
14
use function assert;
15
use function is_array;
16
use function is_int;
17
use function is_resource;
18
use function sasql_fetch_array;
0 ignored issues
show
introduced by
The function sasql_fetch_array was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
19
use function sasql_fetch_assoc;
0 ignored issues
show
introduced by
The function sasql_fetch_assoc was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
20
use function sasql_fetch_row;
0 ignored issues
show
introduced by
The function sasql_fetch_row was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
21
use function sasql_prepare;
0 ignored issues
show
introduced by
The function sasql_prepare was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
22
use function sasql_stmt_affected_rows;
0 ignored issues
show
introduced by
The function sasql_stmt_affected_rows was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
23
use function sasql_stmt_bind_param_ex;
0 ignored issues
show
introduced by
The function sasql_stmt_bind_param_ex was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
24
use function sasql_stmt_execute;
0 ignored issues
show
introduced by
The function sasql_stmt_execute was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
25
use function sasql_stmt_field_count;
0 ignored issues
show
introduced by
The function sasql_stmt_field_count was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
26
use function sasql_stmt_reset;
0 ignored issues
show
introduced by
The function sasql_stmt_reset was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
27
use function sasql_stmt_result_metadata;
0 ignored issues
show
introduced by
The function sasql_stmt_result_metadata was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
28
use function sprintf;
29
use const SASQL_BOTH;
0 ignored issues
show
Bug introduced by
The constant SASQL_BOTH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
30
31
/**
32
 * SAP SQL Anywhere implementation of the Statement interface.
33
 */
34
final class SQLAnywhereStatement implements IteratorAggregate, Statement
35
{
36
    /** @var resource The connection resource. */
37
    private $conn;
38
39
    /** @var int Default fetch mode to use. */
40
    private $defaultFetchMode = FetchMode::MIXED;
41
42
    /** @var resource The result set resource to fetch. */
43
    private $result;
44
45
    /** @var resource The prepared SQL statement to execute. */
46
    private $stmt;
47
48
    /** @var mixed[] The references to bound parameter values. */
49
    private $boundValues = [];
50
51
    /**
52
     * Prepares given statement for given connection.
53
     *
54
     * @param resource $conn The connection resource to use.
55
     * @param string   $sql  The SQL statement to prepare.
56
     *
57
     * @throws SQLAnywhereException
58
     */
59
    public function __construct($conn, string $sql)
60
    {
61
        if (! is_resource($conn)) {
62
            throw new SQLAnywhereException(sprintf(
63
                'Invalid SQL Anywhere connection resource, %s given.',
64
                (new GetVariableType())->__invoke($conn)
65
            ));
66
        }
67
68
        $this->conn = $conn;
69
        $this->stmt = sasql_prepare($conn, $sql);
0 ignored issues
show
Bug introduced by
The function sasql_prepare 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

69
        $this->stmt = /** @scrutinizer ignore-call */ sasql_prepare($conn, $sql);
Loading history...
70
71
        if (! is_resource($this->stmt)) {
72
            throw SQLAnywhereException::fromSQLAnywhereError($conn);
73
        }
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     *
79
     * @throws SQLAnywhereException
80
     */
81
    public function bindParam($param, &$variable, int $type = ParameterType::STRING, ?int $length = null) : void
82
    {
83
        assert(is_int($param));
84
85
        switch ($type) {
86
            case ParameterType::INTEGER:
87
            case ParameterType::BOOLEAN:
88
                $type = 'i';
89
                break;
90
91
            case ParameterType::LARGE_OBJECT:
92
                $type = 'b';
93
                break;
94
95
            case ParameterType::NULL:
96
            case ParameterType::STRING:
97
            case ParameterType::BINARY:
98
                $type = 's';
99
                break;
100
101
            default:
102
                throw new SQLAnywhereException(sprintf('Unknown type %d.', $type));
103
        }
104
105
        $this->boundValues[$param] =& $variable;
106
107
        if (! sasql_stmt_bind_param_ex($this->stmt, $param - 1, $variable, $type, $variable === null)) {
0 ignored issues
show
Bug introduced by
The function sasql_stmt_bind_param_ex 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

107
        if (! /** @scrutinizer ignore-call */ sasql_stmt_bind_param_ex($this->stmt, $param - 1, $variable, $type, $variable === null)) {
Loading history...
108
            throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt);
109
        }
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function bindValue($param, $value, int $type = ParameterType::STRING) : void
116
    {
117
        $this->bindParam($param, $value, $type);
118
    }
119
120
    public function closeCursor() : void
121
    {
122
        sasql_stmt_reset($this->stmt);
123
    }
124
125
    public function columnCount() : int
126
    {
127
        return sasql_stmt_field_count($this->stmt);
0 ignored issues
show
Bug introduced by
The function sasql_stmt_field_count 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

127
        return /** @scrutinizer ignore-call */ sasql_stmt_field_count($this->stmt);
Loading history...
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     *
133
     * @throws SQLAnywhereException
134
     */
135
    public function execute(?array $params = null) : void
136
    {
137
        if (is_array($params)) {
138
            $hasZeroIndex = array_key_exists(0, $params);
139
140
            foreach ($params as $key => $val) {
141
                if ($hasZeroIndex && is_int($key)) {
142
                    $this->bindValue($key + 1, $val);
143
                } else {
144
                    $this->bindValue($key, $val);
145
                }
146
            }
147
        }
148
149
        if (! sasql_stmt_execute($this->stmt)) {
0 ignored issues
show
Bug introduced by
The function sasql_stmt_execute 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

149
        if (! /** @scrutinizer ignore-call */ sasql_stmt_execute($this->stmt)) {
Loading history...
150
            throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt);
151
        }
152
153
        $this->result = sasql_stmt_result_metadata($this->stmt);
0 ignored issues
show
Bug introduced by
The function sasql_stmt_result_metadata 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

153
        $this->result = /** @scrutinizer ignore-call */ sasql_stmt_result_metadata($this->stmt);
Loading history...
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     *
159
     * @throws SQLAnywhereException
160
     */
161
    public function fetch(?int $fetchMode = null)
162
    {
163
        if (! is_resource($this->result)) {
164
            return false;
165
        }
166
167
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;
168
169
        switch ($fetchMode) {
170
            case FetchMode::COLUMN:
171
                return $this->fetchColumn();
172
173
            case FetchMode::ASSOCIATIVE:
174
                return sasql_fetch_assoc($this->result);
0 ignored issues
show
Bug introduced by
The function sasql_fetch_assoc 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

174
                return /** @scrutinizer ignore-call */ sasql_fetch_assoc($this->result);
Loading history...
175
176
            case FetchMode::MIXED:
177
                return sasql_fetch_array($this->result, SASQL_BOTH);
0 ignored issues
show
Bug introduced by
The function sasql_fetch_array 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
                return /** @scrutinizer ignore-call */ sasql_fetch_array($this->result, SASQL_BOTH);
Loading history...
Bug introduced by
The constant SASQL_BOTH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
178
179
            case FetchMode::NUMERIC:
180
                return sasql_fetch_row($this->result);
0 ignored issues
show
Bug introduced by
The function sasql_fetch_row 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

180
                return /** @scrutinizer ignore-call */ sasql_fetch_row($this->result);
Loading history...
181
182
            default:
183
                throw new SQLAnywhereException(sprintf('Fetch mode is not supported %d.', $fetchMode));
184
        }
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190
    public function fetchAll(?int $fetchMode = null) : array
191
    {
192
        $rows = [];
193
194
        switch ($fetchMode) {
195
            case FetchMode::COLUMN:
196
                while (($row = $this->fetchColumn()) !== false) {
197
                    $rows[] = $row;
198
                }
199
200
                break;
201
202
            default:
203
                while (($row = $this->fetch($fetchMode)) !== false) {
204
                    $rows[] = $row;
205
                }
206
        }
207
208
        return $rows;
209
    }
210
211
    /**
212
     * {@inheritdoc}
213
     */
214
    public function fetchColumn()
215
    {
216
        $row = $this->fetch(FetchMode::NUMERIC);
217
218
        if ($row === false) {
219
            return false;
220
        }
221
222
        return $row[0];
223
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228
    public function getIterator()
229
    {
230
        return new StatementIterator($this);
231
    }
232
233
    public function rowCount() : int
234
    {
235
        return sasql_stmt_affected_rows($this->stmt);
0 ignored issues
show
Bug introduced by
The function sasql_stmt_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

235
        return /** @scrutinizer ignore-call */ sasql_stmt_affected_rows($this->stmt);
Loading history...
236
    }
237
238
    public function setFetchMode(int $fetchMode) : void
239
    {
240
        $this->defaultFetchMode = $fetchMode;
241
    }
242
}
243