Update DB\SqliteStatement

This commit is contained in:
Visman 2021-12-23 13:21:59 +07:00
parent cb04b8657c
commit e893a5d4e5
3 changed files with 67 additions and 161 deletions

View file

@ -1,88 +0,0 @@
<?php
/**
* This file is part of the ForkBB <https://github.com/forkbb>.
*
* @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
* @license The MIT License (MIT)
*/
declare(strict_types=1);
namespace ForkBB\Core\DB;
use ForkBB\Core\DB\AbstractStatement;
use PDO;
abstract class AbstractSqliteStatement extends AbstractStatement
{
/**
* https://github.com/php/php-src/blob/master/ext/pdo_sqlite/sqlite_statement.c
*
* SQLite:
* native_type:
* null - для значения NULL, а не типа столбца
* integer - это INTEGER, NUMERIC(?), BOOLEAN // BOOLEAN тут как-то не к месту, его бы в отдельный тип
* string - это TEXT
* double - это REAL, NUMERIC(?) // NUMERIC может быть и double, и integer
* sqlite:decl_type:
* INTEGER
* TEXT
* REAL
* NUMERIC
* BOOLEAN
* ... (это те типы, которые прописаны в CREATE TABLE и полученные после перекодировки из {driver}::bTypeRepl)
*/
/**
* @var array
*/
protected $nativeTypeRepl = [
'integer' => self::INTEGER,
'double' => self::FLOAT,
];
public function getColumnsType(): array
{
if (isset($this->columnsType)) {
return $this->columnsType;
}
$this->columnsType = [];
$count = $this->columnCount();
$i = 0;
// $dbType = $this->db->getType();
for ($i = 0; $i < $count; $i++) {
$meta = $this->getColumnMeta($i);
$type = null;
// $declType = $meta[$dbType . ':decl_type'] ?? null;
$declType = $meta['sqlite:decl_type'] ?? null;
if (null === $declType) {
$type = $this->nativeTypeRepl[$meta['native_type']] ?? null;
} elseif (\preg_match('%INT%i', $declType)) {
$type = self::INTEGER;
} elseif (\preg_match('%BOOL%i', $declType)) {
$type = self::BOOLEAN;
// } elseif (\preg_match('%REAL|FLOA|DOUB|NUMERIC|DECIMAL%i', $declType)) {
// $type = self::FLOAT;
}
if ($type) {
$this->columnsType[$i] = $type;
if (isset($meta['name'])) { // ????? проверка на тип содержимого? только строки, не числа?
$this->columnsType[$meta['name']] = $type;
}
}
}
return $this->columnsType;
}
protected function convToBoolean(/* mixed */ $value): bool
{
return (bool) $value;
}
}

View file

@ -10,26 +10,79 @@ declare(strict_types=1);
namespace ForkBB\Core\DB;
use ForkBB\Core\DB\AbstractSqliteStatement;
use ForkBB\Core\DB\AbstractStatement;
use PDO;
/**
* For PHP 8
*/
class SqliteStatement extends AbstractSqliteStatement
class SqliteStatement extends AbstractStatement
{
public function fetch(int $mode = 0 /* PDO::FETCH_DEFAULT */, int $orientation = PDO::FETCH_ORI_NEXT, int $offset = 0): mixed
/**
* https://github.com/php/php-src/blob/master/ext/pdo_sqlite/sqlite_statement.c
*
* SQLite:
* native_type:
* null - для значения NULL, а не типа столбца
* integer - это INTEGER, NUMERIC(?), BOOLEAN // BOOLEAN тут как-то не к месту, его бы в отдельный тип
* string - это TEXT
* double - это REAL, NUMERIC(?) // NUMERIC может быть и double, и integer
* sqlite:decl_type:
* INTEGER
* TEXT
* REAL
* NUMERIC
* BOOLEAN
* ... (это те типы, которые прописаны в CREATE TABLE и полученные после перекодировки из {driver}::bTypeRepl)
*/
/**
* @var array
*/
protected $nativeTypeRepl = [
'integer' => self::INTEGER,
'double' => self::FLOAT,
];
public function getColumnsType(): array
{
return $this->dbFetch($mode, $orientation, $offset);
if (isset($this->columnsType)) {
return $this->columnsType;
}
$this->columnsType = [];
$count = $this->columnCount();
$i = 0;
// $dbType = $this->db->getType();
for ($i = 0; $i < $count; $i++) {
$meta = $this->getColumnMeta($i);
$type = null;
// $declType = $meta[$dbType . ':decl_type'] ?? null;
$declType = $meta['sqlite:decl_type'] ?? null;
if (null === $declType) {
$type = $this->nativeTypeRepl[$meta['native_type']] ?? null;
} elseif (\preg_match('%INT%i', $declType)) {
$type = self::INTEGER;
} elseif (\preg_match('%BOOL%i', $declType)) {
$type = self::BOOLEAN;
// } elseif (\preg_match('%REAL|FLOA|DOUB|NUMERIC|DECIMAL%i', $declType)) {
// $type = self::FLOAT;
}
if ($type) {
$this->columnsType[$i] = $type;
if (isset($meta['name'])) { // ????? проверка на тип содержимого? только строки, не числа?
$this->columnsType[$meta['name']] = $type;
}
}
}
return $this->columnsType;
}
public function fetchAll(int $mode = 0 /* PDO::FETCH_DEFAULT */, ...$args): array
protected function convToBoolean(/* mixed */ $value): bool
{
return $this->dbFetchAll($mode, ...$args);
}
public function setFetchMode(int $mode, ...$args): bool
{
return $this->dbSetFetchMode($mode, ...$args);
return (bool) $value;
}
}

View file

@ -1,59 +0,0 @@
<?php
/**
* This file is part of the ForkBB <https://github.com/forkbb>.
*
* @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
* @license The MIT License (MIT)
*/
declare(strict_types=1);
namespace ForkBB\Core\DB;
use ForkBB\Core\DB\AbstractSqliteStatement;
use PDO;
/**
* For PHP 7
*/
class SqliteStatement7 extends AbstractSqliteStatement
{
public function fetch($mode = null, $orientation = null, $offset = null)
{
$mode = $mode ?? 0;
$orientation = $orientation ?? PDO::FETCH_ORI_NEXT;
$offset = $offset ?? 0;
return $this->dbFetch($mode, $orientation, $offset);
}
public function fetchAll($mode = null, $fetchArg = null, $ctorArgs = null)
{
$mode = $mode ?? 0;
$args = $this->returnArgs($fetchArg, $ctorArgs);
return $this->dbFetchAll($mode, ...$args);
}
public function setFetchMode($mode, $fetchArg = null, $ctorArgs = null): bool
{
$args = $this->returnArgs($fetchArg, $ctorArgs);
return $this->dbSetFetchMode($mode, ...$args);
}
protected function returnArgs($fetchArg, $ctorArgs): array
{
$args = [];
if (isset($fetchArg)) {
$args[] = $fetchArg;
if (isset($ctorArgs)) {
$args[] = $ctorArgs;
}
}
return $args;
}
}