Added nextParameter method to IDbStatement.
This commit is contained in:
parent
6e3e86f416
commit
eeb01d558d
5 changed files with 63 additions and 28 deletions
2
VERSION
2
VERSION
|
@ -1 +1 @@
|
||||||
0.2408.32027
|
0.2408.32202
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
<?php
|
<?php
|
||||||
// IDbStatement.php
|
// IDbStatement.php
|
||||||
// Created: 2021-05-02
|
// Created: 2021-05-02
|
||||||
// Updated: 2022-02-16
|
// Updated: 2024-08-03
|
||||||
|
|
||||||
namespace Index\Data;
|
namespace Index\Data;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use RuntimeException;
|
||||||
use Index\ICloseable;
|
use Index\ICloseable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -24,9 +26,22 @@ interface IDbStatement extends ICloseable {
|
||||||
* @param int $ordinal Index of the target parameter.
|
* @param int $ordinal Index of the target parameter.
|
||||||
* @param mixed $value Value to assign to the 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.
|
* @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;
|
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.
|
* Gets the result after execution.
|
||||||
*
|
*
|
||||||
|
|
|
@ -54,9 +54,18 @@ class MariaDBStatement implements IDbStatement {
|
||||||
public function addParameter(int $ordinal, mixed $value, int $type = DbType::AUTO): void {
|
public function addParameter(int $ordinal, mixed $value, int $type = DbType::AUTO): void {
|
||||||
if($ordinal < 1 || $ordinal > $this->getParameterCount())
|
if($ordinal < 1 || $ordinal > $this->getParameterCount())
|
||||||
throw new InvalidArgumentException('$ordinal is not a valid parameter number.');
|
throw new InvalidArgumentException('$ordinal is not a valid parameter number.');
|
||||||
|
|
||||||
$this->params[$ordinal - 1] = new MariaDBParameter($ordinal, $type, $value);
|
$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.
|
* Gets the last error code.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
// NullDbStatement.php
|
// NullDbStatement.php
|
||||||
// Created: 2021-05-02
|
// Created: 2021-05-02
|
||||||
// Updated: 2024-08-01
|
// Updated: 2024-08-03
|
||||||
|
|
||||||
namespace Index\Data\NullDb;
|
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 addParameter(int $ordinal, mixed $value, int $type = DbType::AUTO): void {}
|
||||||
|
|
||||||
|
public function nextParameter(mixed $value, int $type = DbType::AUTO): void {}
|
||||||
|
|
||||||
public function getResult(): IDbResult {
|
public function getResult(): IDbResult {
|
||||||
return new NullDbResult;
|
return new NullDbResult;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
// SQLiteStatement.php
|
// SQLiteStatement.php
|
||||||
// Created: 2021-05-02
|
// Created: 2021-05-02
|
||||||
// Updated: 2024-08-01
|
// Updated: 2024-08-03
|
||||||
|
|
||||||
namespace Index\Data\SQLite;
|
namespace Index\Data\SQLite;
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@ use Index\IO\Stream;
|
||||||
*/
|
*/
|
||||||
class SQLiteStatement implements IDbStatement {
|
class SQLiteStatement implements IDbStatement {
|
||||||
private ?SQLite3Result $result = null;
|
private ?SQLite3Result $result = null;
|
||||||
|
private int $lastOrdinal = 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new SQLiteStatement instance.
|
* Creates a new SQLiteStatement instance.
|
||||||
|
@ -34,35 +35,42 @@ class SQLiteStatement implements IDbStatement {
|
||||||
return $this->statement->paramCount();
|
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 {
|
public function addParameter(int $ordinal, mixed $value, int $type = DbType::AUTO): void {
|
||||||
if($ordinal < 1 || $ordinal > $this->getParameterCount())
|
if($ordinal < 1 || $ordinal > $this->getParameterCount())
|
||||||
throw new InvalidArgumentException('$ordinal is not a valid parameter number.');
|
throw new InvalidArgumentException('$ordinal is not a valid parameter number.');
|
||||||
if($type === DbType::AUTO)
|
|
||||||
$type = DbTools::detectType($value);
|
|
||||||
|
|
||||||
switch($type) {
|
if($ordinal > $this->lastOrdinal)
|
||||||
case DbType::NULL:
|
$this->lastOrdinal = $ordinal;
|
||||||
$value = null;
|
|
||||||
$type = SQLITE3_NULL;
|
$this->bindParameter($ordinal, $value, $type);
|
||||||
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(!$this->statement->bindValue($ordinal, $value, $type))
|
public function nextParameter(mixed $value, int $type = DbType::AUTO): void {
|
||||||
throw new RuntimeException((string)$this->connection->getLastErrorString(), $this->connection->getLastErrorCode());
|
if(++$this->lastOrdinal > $this->getParameterCount())
|
||||||
|
throw new RuntimeException('Attempted to specify too many parameters.');
|
||||||
|
|
||||||
|
$this->bindParameter($this->lastOrdinal, $value, $type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getResult(): IDbResult {
|
public function getResult(): IDbResult {
|
||||||
|
@ -84,6 +92,7 @@ class SQLiteStatement implements IDbStatement {
|
||||||
|
|
||||||
public function reset(): void {
|
public function reset(): void {
|
||||||
$this->result = null;
|
$this->result = null;
|
||||||
|
$this->lastOrdinal = 1;
|
||||||
if(!$this->statement->clear())
|
if(!$this->statement->clear())
|
||||||
throw new RuntimeException((string)$this->connection->getLastErrorString(), $this->connection->getLastErrorCode());
|
throw new RuntimeException((string)$this->connection->getLastErrorString(), $this->connection->getLastErrorCode());
|
||||||
if(!$this->statement->reset())
|
if(!$this->statement->reset())
|
||||||
|
|
Loading…
Add table
Reference in a new issue