diff --git a/VERSION b/VERSION index 5d42e00..df3de91 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2408.32027 +0.2408.32202 diff --git a/src/Data/IDbStatement.php b/src/Data/IDbStatement.php index a536ab6..1322936 100644 --- a/src/Data/IDbStatement.php +++ b/src/Data/IDbStatement.php @@ -1,10 +1,12 @@ $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. * diff --git a/src/Data/NullDb/NullDbStatement.php b/src/Data/NullDb/NullDbStatement.php index 9b141ee..a6af889 100644 --- a/src/Data/NullDb/NullDbStatement.php +++ b/src/Data/NullDb/NullDbStatement.php @@ -1,7 +1,7 @@ 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())