Failed Conditions
Pull Request — master (#4007)
by Sergei
62:50
created

SQLAnywhereStatement::fetchAllColumn()   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
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver\SQLAnywhere;
6
7
use Doctrine\DBAL\Driver\DriverException;
8
use Doctrine\DBAL\Driver\FetchUtils;
9
use Doctrine\DBAL\Driver\Statement;
10
use Doctrine\DBAL\Exception\GetVariableType;
11
use Doctrine\DBAL\ParameterType;
12
use Traversable;
13
use function array_key_exists;
14
use function assert;
15
use function is_int;
16
use function is_resource;
17
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...
18
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...
19
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...
20
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...
21
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...
22
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...
23
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...
24
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...
25
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...
26
use function sprintf;
27
28
/**
29
 * SAP SQL Anywhere implementation of the Statement interface.
30
 */
31
final class SQLAnywhereStatement implements Statement
32
{
33
    /** @var resource The connection resource. */
34
    private $conn;
35
36
    /** @var resource|null The result set resource to fetch. */
37
    private $result;
38
39
    /** @var resource The prepared SQL statement to execute. */
40
    private $stmt;
41
42
    /** @var mixed[] The references to bound parameter values. */
43
    private $boundValues = [];
44
45
    /**
46
     * Prepares given statement for given connection.
47
     *
48
     * @param resource $conn The connection resource to use.
49
     * @param string   $sql  The SQL statement to prepare.
50
     *
51
     * @throws SQLAnywhereException
52
     */
53
    public function __construct($conn, string $sql)
54
    {
55
        if (! is_resource($conn)) {
56
            throw new SQLAnywhereException(sprintf(
57
                'Invalid SQL Anywhere connection resource, %s given.',
58
                (new GetVariableType())->__invoke($conn)
59
            ));
60
        }
61
62
        $this->conn = $conn;
63
        $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

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

101
        if (! /** @scrutinizer ignore-call */ sasql_stmt_bind_param_ex($this->stmt, $param - 1, $variable, $type, $variable === null)) {
Loading history...
102
            throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt);
103
        }
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function bindValue($param, $value, int $type = ParameterType::STRING) : void
110
    {
111
        $this->bindParam($param, $value, $type);
112
    }
113
114
    public function closeCursor() : void
115
    {
116
        sasql_stmt_reset($this->stmt);
117
    }
118
119
    public function columnCount() : int
120
    {
121
        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

121
        return /** @scrutinizer ignore-call */ sasql_stmt_field_count($this->stmt);
Loading history...
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     *
127
     * @throws SQLAnywhereException
128
     */
129
    public function execute(?array $params = null) : void
130
    {
131
        if ($params !== null) {
132
            $hasZeroIndex = array_key_exists(0, $params);
133
134
            foreach ($params as $key => $val) {
135
                if ($hasZeroIndex && is_int($key)) {
136
                    $this->bindValue($key + 1, $val);
137
                } else {
138
                    $this->bindValue($key, $val);
139
                }
140
            }
141
        }
142
143
        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

143
        if (! /** @scrutinizer ignore-call */ sasql_stmt_execute($this->stmt)) {
Loading history...
144
            throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt);
145
        }
146
147
        $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

147
        $this->result = /** @scrutinizer ignore-call */ sasql_stmt_result_metadata($this->stmt);
Loading history...
148
    }
149
150
    /**
151
     * {@inheritDoc}
152
     */
153
    public function fetchNumeric()
154
    {
155
        if (! is_resource($this->result)) {
156
            return false;
157
        }
158
159
        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

159
        return /** @scrutinizer ignore-call */ sasql_fetch_row($this->result);
Loading history...
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function fetchAssociative()
166
    {
167
        if (! is_resource($this->result)) {
168
            return false;
169
        }
170
171
        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

171
        return /** @scrutinizer ignore-call */ sasql_fetch_assoc($this->result);
Loading history...
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     *
177
     * @throws DriverException
178
     */
179
    public function fetchColumn()
180
    {
181
        return FetchUtils::fetchColumnFromNumeric($this);
182
    }
183
184
    /**
185
     * @return array<int,array<int,mixed>>
186
     *
187
     * @throws DriverException
188
     */
189
    public function fetchAllNumeric() : array
190
    {
191
        return FetchUtils::fetchAllNumericByOne($this);
192
    }
193
194
    /**
195
     * @return array<int,array<string,mixed>>
196
     *
197
     * @throws DriverException
198
     */
199
    public function fetchAllAssociative() : array
200
    {
201
        return FetchUtils::fetchAllAssociativeByOne($this);
202
    }
203
204
    /**
205
     * @return array<int,mixed>
206
     *
207
     * @throws DriverException
208
     */
209
    public function fetchAllColumn() : array
210
    {
211
        return FetchUtils::fetchAllColumnByOne($this);
212
    }
213
214
    /**
215
     * @return Traversable<int,array<int,mixed>>
216
     *
217
     * @throws DriverException
218
     */
219
    public function iterateNumeric() : Traversable
220
    {
221
        return FetchUtils::iterateNumericByOne($this);
222
    }
223
224
    /**
225
     * @return Traversable<int,array<string,mixed>>
226
     *
227
     * @throws DriverException
228
     */
229
    public function iterateAssociative() : Traversable
230
    {
231
        return FetchUtils::iterateAssociativeByOne($this);
232
    }
233
234
    /**
235
     * @return Traversable<int,mixed>
236
     *
237
     * @throws DriverException
238
     */
239
    public function iterateColumn() : Traversable
240
    {
241
        return FetchUtils::iterateColumnByOne($this);
242
    }
243
244
    public function rowCount() : int
245
    {
246
        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

246
        return /** @scrutinizer ignore-call */ sasql_stmt_affected_rows($this->stmt);
Loading history...
247
    }
248
}
249