Added nextParameter method to IDbStatement.

This commit is contained in:
flash 2024-08-03 22:02:46 +00:00
parent 6e3e86f416
commit eeb01d558d
5 changed files with 63 additions and 28 deletions

View file

@ -1 +1 @@
0.2408.32027
0.2408.32202

View file

@ -1,10 +1,12 @@
<?php
// IDbStatement.php
// Created: 2021-05-02
// Updated: 2022-02-16
// Updated: 2024-08-03
namespace Index\Data;
use InvalidArgumentException;
use RuntimeException;
use Index\ICloseable;
/**
@ -24,9 +26,22 @@ interface IDbStatement extends ICloseable {
* @param int $ordinal Index of the target parameter.
* @param mixed $value Value to assign to the parameter.
* @param int $type Type of the value, if left to DbType::AUTO DbTools::detectType will be used on $value.
* @throws InvalidArgumentException If $ordinal exceeds bounds.
*/
function addParameter(int $ordinal, mixed $value, int $type = DbType::AUTO): void;
/**
* Assigns a value to the next parameter.
*
* Behaviour of this method may be slightly undefined if addParameter is used at the same time.
* Overwriting lower ordinals than the current cursor should be fine, but your mileage may vary.
*
* @param mixed $value Value to assign to the parameter.
* @param int $type Type of the value, if left to DbType::AUTO DbTools::detectType will be used on $value.
* @throws RuntimeException If all parameters have already been specified.
*/
function nextParameter(mixed $value, int $type = DbType::AUTO): void;
/**
* Gets the result after execution.
*

View file

@ -54,9 +54,18 @@ class MariaDBStatement implements IDbStatement {
public function addParameter(int $ordinal, mixed $value, int $type = DbType::AUTO): void {
if($ordinal < 1 || $ordinal > $this->getParameterCount())
throw new InvalidArgumentException('$ordinal is not a valid parameter number.');
$this->params[$ordinal - 1] = new MariaDBParameter($ordinal, $type, $value);
}
public function nextParameter(mixed $value, int $type = DbType::AUTO): void {
$nextIndex = count($this->params);
if($nextIndex >= $this->getParameterCount())
throw new RuntimeException('Attempted to specify too many parameters.');
$this->params[$nextIndex] = new MariaDBParameter($nextIndex + 1, $type, $value);
}
/**
* Gets the last error code.
*

View file

@ -1,7 +1,7 @@
<?php
// NullDbStatement.php
// Created: 2021-05-02
// Updated: 2024-08-01
// Updated: 2024-08-03
namespace Index\Data\NullDb;
@ -17,6 +17,8 @@ class NullDbStatement implements IDbStatement {
public function addParameter(int $ordinal, mixed $value, int $type = DbType::AUTO): void {}
public function nextParameter(mixed $value, int $type = DbType::AUTO): void {}
public function getResult(): IDbResult {
return new NullDbResult;
}

View file

@ -1,7 +1,7 @@
<?php
// SQLiteStatement.php
// Created: 2021-05-02
// Updated: 2024-08-01
// Updated: 2024-08-03
namespace Index\Data\SQLite;
@ -17,6 +17,7 @@ use Index\IO\Stream;
*/
class SQLiteStatement implements IDbStatement {
private ?SQLite3Result $result = null;
private int $lastOrdinal = 1;
/**
* Creates a new SQLiteStatement instance.
@ -34,35 +35,42 @@ class SQLiteStatement implements IDbStatement {
return $this->statement->paramCount();
}
private static function convertType(int $type): int {
return match($type) {
DbType::NULL => SQLITE3_NULL,
DbType::INTEGER => SQLITE3_INTEGER,
DbType::FLOAT => SQLITE3_FLOAT,
DbType::STRING => SQLITE3_TEXT,
DbType::BLOB => SQLITE3_BLOB,
default => throw new InvalidArgumentException('$type is not a supported type.'),
};
}
private function bindParameter(int $ordinal, mixed $value, int $type): void {
if($type === DbType::AUTO)
$type = DbTools::detectType($value);
if($type === DbType::NULL)
$value = null;
if(!$this->statement->bindValue($ordinal, $value, self::convertType($type)))
throw new RuntimeException((string)$this->connection->getLastErrorString(), $this->connection->getLastErrorCode());
}
public function addParameter(int $ordinal, mixed $value, int $type = DbType::AUTO): void {
if($ordinal < 1 || $ordinal > $this->getParameterCount())
throw new InvalidArgumentException('$ordinal is not a valid parameter number.');
if($type === DbType::AUTO)
$type = DbTools::detectType($value);
switch($type) {
case DbType::NULL:
$value = null;
$type = SQLITE3_NULL;
break;
case DbType::INTEGER:
$type = SQLITE3_INTEGER;
break;
case DbType::FLOAT:
$type = SQLITE3_FLOAT;
break;
case DbType::STRING:
$type = SQLITE3_TEXT;
break;
case DbType::BLOB:
$type = SQLITE3_BLOB;
break;
default:
throw new InvalidArgumentException('$type is not a supported type.');
}
if($ordinal > $this->lastOrdinal)
$this->lastOrdinal = $ordinal;
if(!$this->statement->bindValue($ordinal, $value, $type))
throw new RuntimeException((string)$this->connection->getLastErrorString(), $this->connection->getLastErrorCode());
$this->bindParameter($ordinal, $value, $type);
}
public function nextParameter(mixed $value, int $type = DbType::AUTO): void {
if(++$this->lastOrdinal > $this->getParameterCount())
throw new RuntimeException('Attempted to specify too many parameters.');
$this->bindParameter($this->lastOrdinal, $value, $type);
}
public function getResult(): IDbResult {
@ -84,6 +92,7 @@ class SQLiteStatement implements IDbStatement {
public function reset(): void {
$this->result = null;
$this->lastOrdinal = 1;
if(!$this->statement->clear())
throw new RuntimeException((string)$this->connection->getLastErrorString(), $this->connection->getLastErrorCode());
if(!$this->statement->reset())