doctrine /
dbal
| 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_int; |
||||
| 16 | use function is_resource; |
||||
| 17 | use function sasql_fetch_array; |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 18 | use function sasql_fetch_assoc; |
||||
|
0 ignored issues
–
show
|
|||||
| 19 | use function sasql_fetch_row; |
||||
|
0 ignored issues
–
show
|
|||||
| 20 | use function sasql_prepare; |
||||
|
0 ignored issues
–
show
|
|||||
| 21 | use function sasql_stmt_affected_rows; |
||||
|
0 ignored issues
–
show
|
|||||
| 22 | use function sasql_stmt_bind_param_ex; |
||||
|
0 ignored issues
–
show
|
|||||
| 23 | use function sasql_stmt_execute; |
||||
|
0 ignored issues
–
show
|
|||||
| 24 | use function sasql_stmt_field_count; |
||||
|
0 ignored issues
–
show
|
|||||
| 25 | use function sasql_stmt_reset; |
||||
|
0 ignored issues
–
show
|
|||||
| 26 | use function sasql_stmt_result_metadata; |
||||
|
0 ignored issues
–
show
|
|||||
| 27 | use function sprintf; |
||||
| 28 | use const SASQL_BOTH; |
||||
|
0 ignored issues
–
show
|
|||||
| 29 | |||||
| 30 | /** |
||||
| 31 | * SAP SQL Anywhere implementation of the Statement interface. |
||||
| 32 | */ |
||||
| 33 | final class SQLAnywhereStatement implements IteratorAggregate, Statement |
||||
| 34 | { |
||||
| 35 | /** @var resource The connection resource. */ |
||||
| 36 | private $conn; |
||||
| 37 | |||||
| 38 | /** @var int Default fetch mode to use. */ |
||||
| 39 | private $defaultFetchMode = FetchMode::MIXED; |
||||
| 40 | |||||
| 41 | /** @var resource|null The result set resource to fetch. */ |
||||
| 42 | private $result; |
||||
| 43 | |||||
| 44 | /** @var resource The prepared SQL statement to execute. */ |
||||
| 45 | private $stmt; |
||||
| 46 | |||||
| 47 | /** @var mixed[] The references to bound parameter values. */ |
||||
| 48 | private $boundValues = []; |
||||
| 49 | |||||
| 50 | /** |
||||
| 51 | * Prepares given statement for given connection. |
||||
| 52 | * |
||||
| 53 | * @param resource $conn The connection resource to use. |
||||
| 54 | * @param string $sql The SQL statement to prepare. |
||||
| 55 | * |
||||
| 56 | * @throws SQLAnywhereException |
||||
| 57 | */ |
||||
| 58 | public function __construct($conn, string $sql) |
||||
| 59 | { |
||||
| 60 | if (! is_resource($conn)) { |
||||
| 61 | throw new SQLAnywhereException(sprintf( |
||||
| 62 | 'Invalid SQL Anywhere connection resource, %s given.', |
||||
| 63 | (new GetVariableType())->__invoke($conn) |
||||
| 64 | )); |
||||
| 65 | } |
||||
| 66 | |||||
| 67 | $this->conn = $conn; |
||||
| 68 | $this->stmt = sasql_prepare($conn, $sql); |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 69 | |||||
| 70 | if (! is_resource($this->stmt)) { |
||||
| 71 | throw SQLAnywhereException::fromSQLAnywhereError($conn); |
||||
| 72 | } |
||||
| 73 | } |
||||
| 74 | |||||
| 75 | /** |
||||
| 76 | * {@inheritdoc} |
||||
| 77 | * |
||||
| 78 | * @throws SQLAnywhereException |
||||
| 79 | */ |
||||
| 80 | public function bindParam($param, &$variable, int $type = ParameterType::STRING, ?int $length = null) : void |
||||
| 81 | { |
||||
| 82 | assert(is_int($param)); |
||||
| 83 | |||||
| 84 | switch ($type) { |
||||
| 85 | case ParameterType::INTEGER: |
||||
| 86 | case ParameterType::BOOLEAN: |
||||
| 87 | $type = 'i'; |
||||
| 88 | break; |
||||
| 89 | |||||
| 90 | case ParameterType::LARGE_OBJECT: |
||||
| 91 | $type = 'b'; |
||||
| 92 | break; |
||||
| 93 | |||||
| 94 | case ParameterType::NULL: |
||||
| 95 | case ParameterType::STRING: |
||||
| 96 | case ParameterType::BINARY: |
||||
| 97 | $type = 's'; |
||||
| 98 | break; |
||||
| 99 | |||||
| 100 | default: |
||||
| 101 | throw new SQLAnywhereException(sprintf('Unknown type %d.', $type)); |
||||
| 102 | } |
||||
| 103 | |||||
| 104 | $this->boundValues[$param] =& $variable; |
||||
| 105 | |||||
| 106 | if (! sasql_stmt_bind_param_ex($this->stmt, $param - 1, $variable, $type, $variable === null)) { |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 107 | throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt); |
||||
| 108 | } |
||||
| 109 | } |
||||
| 110 | |||||
| 111 | /** |
||||
| 112 | * {@inheritdoc} |
||||
| 113 | */ |
||||
| 114 | public function bindValue($param, $value, int $type = ParameterType::STRING) : void |
||||
| 115 | { |
||||
| 116 | $this->bindParam($param, $value, $type); |
||||
| 117 | } |
||||
| 118 | |||||
| 119 | public function closeCursor() : void |
||||
| 120 | { |
||||
| 121 | sasql_stmt_reset($this->stmt); |
||||
| 122 | } |
||||
| 123 | |||||
| 124 | public function columnCount() : int |
||||
| 125 | { |
||||
| 126 | return sasql_stmt_field_count($this->stmt); |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 127 | } |
||||
| 128 | |||||
| 129 | /** |
||||
| 130 | * {@inheritdoc} |
||||
| 131 | * |
||||
| 132 | * @throws SQLAnywhereException |
||||
| 133 | */ |
||||
| 134 | public function execute(?array $params = null) : void |
||||
| 135 | { |
||||
| 136 | if ($params !== null) { |
||||
| 137 | $hasZeroIndex = array_key_exists(0, $params); |
||||
| 138 | |||||
| 139 | foreach ($params as $key => $val) { |
||||
| 140 | if ($hasZeroIndex && is_int($key)) { |
||||
| 141 | $this->bindValue($key + 1, $val); |
||||
| 142 | } else { |
||||
| 143 | $this->bindValue($key, $val); |
||||
| 144 | } |
||||
| 145 | } |
||||
| 146 | } |
||||
| 147 | |||||
| 148 | if (! sasql_stmt_execute($this->stmt)) { |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 149 | throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt); |
||||
| 150 | } |
||||
| 151 | |||||
| 152 | $this->result = sasql_stmt_result_metadata($this->stmt); |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 153 | } |
||||
| 154 | |||||
| 155 | /** |
||||
| 156 | * {@inheritdoc} |
||||
| 157 | * |
||||
| 158 | * @throws SQLAnywhereException |
||||
| 159 | */ |
||||
| 160 | public function fetch(?int $fetchMode = null) |
||||
| 161 | { |
||||
| 162 | if (! is_resource($this->result)) { |
||||
| 163 | return false; |
||||
| 164 | } |
||||
| 165 | |||||
| 166 | $fetchMode = $fetchMode ?? $this->defaultFetchMode; |
||||
| 167 | |||||
| 168 | switch ($fetchMode) { |
||||
| 169 | case FetchMode::COLUMN: |
||||
| 170 | return $this->fetchColumn(); |
||||
| 171 | |||||
| 172 | case FetchMode::ASSOCIATIVE: |
||||
| 173 | return sasql_fetch_assoc($this->result); |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 174 | |||||
| 175 | case FetchMode::MIXED: |
||||
| 176 | return sasql_fetch_array($this->result, SASQL_BOTH); |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 177 | |||||
| 178 | case FetchMode::NUMERIC: |
||||
| 179 | return sasql_fetch_row($this->result); |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 180 | |||||
| 181 | default: |
||||
| 182 | throw new SQLAnywhereException(sprintf('Fetch mode is not supported %d.', $fetchMode)); |
||||
| 183 | } |
||||
| 184 | } |
||||
| 185 | |||||
| 186 | /** |
||||
| 187 | * {@inheritdoc} |
||||
| 188 | */ |
||||
| 189 | public function fetchAll(?int $fetchMode = null) : array |
||||
| 190 | { |
||||
| 191 | $rows = []; |
||||
| 192 | |||||
| 193 | switch ($fetchMode) { |
||||
| 194 | case FetchMode::COLUMN: |
||||
| 195 | while (($row = $this->fetchColumn()) !== false) { |
||||
| 196 | $rows[] = $row; |
||||
| 197 | } |
||||
| 198 | |||||
| 199 | break; |
||||
| 200 | |||||
| 201 | default: |
||||
| 202 | while (($row = $this->fetch($fetchMode)) !== false) { |
||||
| 203 | $rows[] = $row; |
||||
| 204 | } |
||||
| 205 | } |
||||
| 206 | |||||
| 207 | return $rows; |
||||
| 208 | } |
||||
| 209 | |||||
| 210 | /** |
||||
| 211 | * {@inheritdoc} |
||||
| 212 | */ |
||||
| 213 | public function fetchColumn() |
||||
| 214 | { |
||||
| 215 | $row = $this->fetch(FetchMode::NUMERIC); |
||||
| 216 | |||||
| 217 | if ($row === false) { |
||||
| 218 | return false; |
||||
| 219 | } |
||||
| 220 | |||||
| 221 | return $row[0]; |
||||
| 222 | } |
||||
| 223 | |||||
| 224 | /** |
||||
| 225 | * {@inheritdoc} |
||||
| 226 | */ |
||||
| 227 | public function getIterator() |
||||
| 228 | { |
||||
| 229 | return new StatementIterator($this); |
||||
| 230 | } |
||||
| 231 | |||||
| 232 | public function rowCount() : int |
||||
| 233 | { |
||||
| 234 | return sasql_stmt_affected_rows($this->stmt); |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 235 | } |
||||
| 236 | |||||
| 237 | public function setFetchMode(int $fetchMode) : void |
||||
| 238 | { |
||||
| 239 | $this->defaultFetchMode = $fetchMode; |
||||
| 240 | } |
||||
| 241 | } |
||||
| 242 |