mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-22 13:12:41 +00:00
Make it possible to redefine Statement type binds
This commit is contained in:
parent
b4d178c33f
commit
f19f683e38
3 changed files with 62 additions and 30 deletions
|
@ -3,7 +3,9 @@ declare(strict_types=1);
|
|||
namespace JKingWeb\NewsSync\Db;
|
||||
|
||||
interface Statement {
|
||||
function __invoke(&...$values); // alias of run()
|
||||
function run(&...$values): Result;
|
||||
function runArray(array &$values): Result;
|
||||
function __invoke(...$values); // alias of run()
|
||||
function run(...$values): Result;
|
||||
function runArray(array $values): Result;
|
||||
function rebind(...$bindings): bool;
|
||||
function rebindArray(array $bindings): bool;
|
||||
}
|
|
@ -10,9 +10,47 @@ class StatementSQLite3 implements Statement {
|
|||
public function __construct($db, $st, array $bindings = []) {
|
||||
$this->db = $db;
|
||||
$this->st = $st;
|
||||
$this->rebindArray($bindings);
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
$this->st->close();
|
||||
unset($this->st);
|
||||
}
|
||||
|
||||
public function __invoke(...$values) {
|
||||
return $this->runArray($values);
|
||||
}
|
||||
|
||||
public function run(...$values): Result {
|
||||
return $this->runArray($values);
|
||||
}
|
||||
|
||||
public function runArray(array $values = null): Result {
|
||||
$this->st->clear();
|
||||
$l = sizeof($values);
|
||||
for($a = 0; $a < $l; $a++) {
|
||||
if($values[$a]===null) {
|
||||
$type = \SQLITE3_NULL;
|
||||
} else {
|
||||
$type = (array_key_exists($a,$this->types)) ? $this->types[$a] : \SQLITE3_TEXT;
|
||||
}
|
||||
$this->st->bindParam($a+1, $values[$a], $type);
|
||||
}
|
||||
return new ResultSQLite3($this->st->execute(), $this->db->changes(), $this);
|
||||
}
|
||||
|
||||
public function rebind(...$bindings): bool {
|
||||
return $this->rebindArray($bindings);
|
||||
}
|
||||
|
||||
public function rebindArray(array $bindings): bool {
|
||||
$this->types = [];
|
||||
foreach($bindings as $binding) {
|
||||
switch(trim(strtolower($binding))) {
|
||||
case "null":
|
||||
case "nil":
|
||||
$this->types[] = \SQLITE3_NULL; break;
|
||||
case "int":
|
||||
case "integer":
|
||||
$this->types[] = \SQLITE3_INTEGER; break;
|
||||
|
@ -42,32 +80,6 @@ class StatementSQLite3 implements Statement {
|
|||
$this->types[] = \SQLITE3_TEXT; break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
$this->st->close();
|
||||
unset($this->st);
|
||||
}
|
||||
|
||||
public function __invoke(&...$values) {
|
||||
return $this->runArray($values);
|
||||
}
|
||||
|
||||
public function run(&...$values): Result {
|
||||
return $this->runArray($values);
|
||||
}
|
||||
|
||||
public function runArray(array &$values = null): Result {
|
||||
$this->st->clear();
|
||||
$l = sizeof($values);
|
||||
for($a = 0; $a < $l; $a++) {
|
||||
if($values[$a]===null) {
|
||||
$type = \SQLITE3_NULL;
|
||||
} else {
|
||||
$type = (array_key_exists($a,$this->types)) ? $this->types[$a] : \SQLITE3_TEXT;
|
||||
}
|
||||
$this->st->bindParam($a+1, $values[$a], $type);
|
||||
}
|
||||
return new ResultSQLite3($this->st->execute(), $this->db->changes(), $this);
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -28,4 +28,22 @@ class TestDbStatementSQLite3 extends \PHPUnit\Framework\TestCase {
|
|||
function testConstructStatement() {
|
||||
$this->assertInstanceOf(Db\StatementSQLite3::class, new Db\StatementSQLite3($this->c, $this->s));
|
||||
}
|
||||
|
||||
function testBindMissingValue() {
|
||||
$s = new Db\StatementSQLite3($this->c, $this->s);
|
||||
$val = $s->runArray()->get()['value'];
|
||||
$this->assertSame(null, $val);
|
||||
}
|
||||
|
||||
function testBindNull() {
|
||||
$s = new Db\StatementSQLite3($this->c, $this->s);
|
||||
$val = $s->runArray([null])->get()['value'];
|
||||
$this->assertSame(null, $val);
|
||||
}
|
||||
|
||||
function testBindInteger() {
|
||||
$s = new Db\StatementSQLite3($this->c, $this->s, ["int"]);
|
||||
$val = $s->runArray([2112])->get()['value'];
|
||||
$this->assertSame(2112, $val);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue