Browse Source

Make it possible to redefine Statement type binds

microsub
J. King 7 years ago
parent
commit
f19f683e38
  1. 8
      lib/Db/Statement.php
  2. 66
      lib/Db/StatementSQLite3.php
  3. 18
      tests/Db/SQLite3/TestDbStatementSQLite3.php

8
lib/Db/Statement.php

@ -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;
}

66
lib/Db/StatementSQLite3.php

@ -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;
}
}

18
tests/Db/SQLite3/TestDbStatementSQLite3.php

@ -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…
Cancel
Save