fix: noverify + типы для правил провеки
This commit is contained in:
parent
9680409ba9
commit
90cbd3b2b6
10 changed files with 319 additions and 300 deletions
342
src/Database.php
342
src/Database.php
|
|
@ -1,192 +1,202 @@
|
|||
<?php
|
||||
namespace {
|
||||
if(!function_exists('sqliteLower')){
|
||||
function sqliteLower($str) {
|
||||
if (!function_exists('sqliteLower')) {
|
||||
function sqliteLower($str)
|
||||
{
|
||||
return mb_strtolower($str, 'UTF-8');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace ctiso {
|
||||
use PDO,
|
||||
use PDO,
|
||||
ctiso\Database\Statement,
|
||||
ctiso\Database\PDOStatement,
|
||||
ctiso\Database\IdGenerator;
|
||||
|
||||
/**
|
||||
* Класс оболочка для PDO для замены Creole
|
||||
*/
|
||||
class Database extends PDO
|
||||
{
|
||||
|
||||
public $dsn;
|
||||
public function __construct($dsn, $username = null, $password = null)
|
||||
{
|
||||
parent::__construct($dsn, $username, $password);
|
||||
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
|
||||
$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, [PDOStatement::class, []]);
|
||||
}
|
||||
|
||||
function prepare(string $sql, array $options = []): PDOStatement|false {
|
||||
$result/*: PDOStatement*/ = parent::prepare($sql, $options);
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getDSN()
|
||||
{
|
||||
return $this->dsn;
|
||||
}
|
||||
public function isPostgres(){
|
||||
return ($this->dsn["phptype"] == "pgsql");
|
||||
}
|
||||
/**
|
||||
* Создает соединение с базой данных
|
||||
* Класс оболочка для PDO для замены Creole
|
||||
*/
|
||||
static function getConnection(array $dsn)
|
||||
class Database extends PDO
|
||||
{
|
||||
|
||||
$connection = null;
|
||||
if ($dsn['phptype'] == 'pgsql' || $dsn['phptype'] == 'mysql') {
|
||||
$port = (isset($dsn['port'])) ? "port={$dsn['port']};" : "";
|
||||
$connection/*: Database*/ = new self("{$dsn['phptype']}:host={$dsn['hostspec']}; $port dbname={$dsn['database']}", $dsn['username'], $dsn['password']);
|
||||
if ($dsn['phptype'] == 'pgsql') {
|
||||
$connection->query('SET client_encoding="UTF-8"');
|
||||
}
|
||||
|
||||
if (isset($dsn['schema'])) {
|
||||
$connection->query('SET search_path TO ' . $dsn['schema']);
|
||||
}
|
||||
} elseif ($dsn['phptype'] == 'sqlite::memory') {
|
||||
$connection = new self("{$dsn['phptype']}:");
|
||||
$connection->sqliteCreateFunction('LOWER', 'sqliteLower', 1);
|
||||
} elseif ($dsn['phptype'] == 'sqlite') {
|
||||
$connection/*: Database*/ = new self("{$dsn['phptype']}:{$dsn['database']}");
|
||||
$connection->setAttribute(PDO::ATTR_TIMEOUT, 5);
|
||||
$mode = defined('SQLITE_JOURNAL_MODE') ? SQLITE_JOURNAL_MODE : 'WAL';
|
||||
$connection->query("PRAGMA journal_mode=$mode");
|
||||
$connection->sqliteCreateFunction('LOWER', 'sqliteLower', 1);
|
||||
public $dsn;
|
||||
public function __construct($dsn, $username = null, $password = null)
|
||||
{
|
||||
parent::__construct($dsn, $username, $password);
|
||||
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
|
||||
$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, [PDOStatement::class, []]);
|
||||
}
|
||||
$connection->dsn = $dsn;
|
||||
return $connection;
|
||||
}
|
||||
|
||||
public function executeQuery($query, $values=null): PDOStatement|bool
|
||||
{
|
||||
$stmt = $this->prepare($query);
|
||||
function prepare(string $sql, array $options = []): PDOStatement|false
|
||||
{
|
||||
$result/*: PDOStatement*/ = parent::prepare($sql, $options);
|
||||
return $result;
|
||||
}
|
||||
|
||||
$stmt->execute($values);
|
||||
$stmt->cache = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
return $stmt;
|
||||
}
|
||||
public function getDSN()
|
||||
{
|
||||
return $this->dsn;
|
||||
}
|
||||
public function isPostgres()
|
||||
{
|
||||
return ($this->dsn["phptype"] == "pgsql");
|
||||
}
|
||||
/**
|
||||
* Создает соединение с базой данных
|
||||
*/
|
||||
static function getConnection(array $dsn)
|
||||
{
|
||||
|
||||
public function prepareStatement($query)
|
||||
{
|
||||
return new Statement($query, $this);
|
||||
}
|
||||
$connection = null;
|
||||
if ($dsn['phptype'] == 'pgsql' || $dsn['phptype'] == 'mysql') {
|
||||
$port = (isset($dsn['port'])) ? "port={$dsn['port']};" : "";
|
||||
$connection/*: Database*/ = new self("{$dsn['phptype']}:host={$dsn['hostspec']}; $port dbname={$dsn['database']}", $dsn['username'], $dsn['password']);
|
||||
if ($dsn['phptype'] == 'pgsql') {
|
||||
$connection->query('SET client_encoding="UTF-8"');
|
||||
}
|
||||
|
||||
// Для совместимости со старым представлением баз данных CIS
|
||||
/**
|
||||
* Извлекает из базы все элементы по запросу
|
||||
*/
|
||||
public function fetchAllArray($query, $values = null)
|
||||
{
|
||||
$sth = $this->prepare($query);
|
||||
$prep = $this->prepareValues($values);
|
||||
$sth->execute($prep);
|
||||
return $sth->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
if (isset($dsn['schema'])) {
|
||||
$connection->query('SET search_path TO ' . $dsn['schema']);
|
||||
}
|
||||
} elseif ($dsn['phptype'] == 'sqlite::memory') {
|
||||
$connection = new self("{$dsn['phptype']}:");
|
||||
$connection->sqliteCreateFunction('LOWER', 'sqliteLower', 1);
|
||||
} elseif ($dsn['phptype'] == 'sqlite') {
|
||||
$connection/*: Database*/ = new self("{$dsn['phptype']}:{$dsn['database']}");
|
||||
$connection->setAttribute(PDO::ATTR_TIMEOUT, 5);
|
||||
$mode = defined('SQLITE_JOURNAL_MODE') ? SQLITE_JOURNAL_MODE : 'WAL';
|
||||
$connection->query("PRAGMA journal_mode=$mode");
|
||||
$connection->sqliteCreateFunction('LOWER', 'sqliteLower', 1);
|
||||
}
|
||||
$connection->dsn = $dsn;
|
||||
return $connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Извлекает из базы первый элемент по запросу
|
||||
*/
|
||||
public function fetchOneArray($query, $values = null)
|
||||
{
|
||||
$sth = $this->prepare($query);
|
||||
$prep = $this->prepareValues($values);
|
||||
$sth->execute($prep);
|
||||
return $sth->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
public function executeQuery($query, $values = null): PDOStatement|bool
|
||||
{
|
||||
$stmt = $this->prepare($query);
|
||||
|
||||
private function prepareValues($values)
|
||||
{
|
||||
if (!$values) {
|
||||
$stmt->execute($values);
|
||||
$stmt->cache = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
return $stmt;
|
||||
}
|
||||
|
||||
public function prepareStatement($query)
|
||||
{
|
||||
return new Statement($query, $this);
|
||||
}
|
||||
|
||||
// Для совместимости со старым представлением баз данных CIS
|
||||
/**
|
||||
* Извлекает из базы все элементы по запросу
|
||||
*/
|
||||
public function fetchAllArray($query, $values = null)
|
||||
{
|
||||
$sth = $this->prepare($query);
|
||||
$prep = $this->prepareValues($values);
|
||||
$sth->execute($prep);
|
||||
return $sth->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
/**
|
||||
* Извлекает из базы первый элемент по запросу
|
||||
*/
|
||||
public function fetchOneArray($query, $values = null)
|
||||
{
|
||||
$sth = $this->prepare($query);
|
||||
$prep = $this->prepareValues($values);
|
||||
$sth->execute($prep);
|
||||
return $sth->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
private function prepareValues($values)
|
||||
{
|
||||
if (!$values) {
|
||||
return null;
|
||||
}
|
||||
$pg = $this->isPostgres();
|
||||
$prep = [];
|
||||
foreach ($values as $key => $value) {
|
||||
$result = null;
|
||||
if (is_bool($value)) {
|
||||
if ($pg) {
|
||||
$result = $value ? 'true' : 'false';
|
||||
} else {
|
||||
$result = $value ? 1 : 0;
|
||||
}
|
||||
} else {
|
||||
$result = $value;
|
||||
}
|
||||
$prep[":" . $key] = $result;
|
||||
}
|
||||
return $prep;
|
||||
}
|
||||
/**
|
||||
* Создает INSERT запрос
|
||||
*/
|
||||
function insertQuery($table, array $values, $return_id = false, $index = null)
|
||||
{
|
||||
$prep = $this->prepareValues($values);
|
||||
|
||||
$sql = "INSERT INTO $table (" . implode(",", array_keys($values))
|
||||
. ") VALUES (" . implode(",", array_keys($prep)) . ")";
|
||||
if ($return_id) {
|
||||
if ($this->isPostgres()) {
|
||||
$sql .= " RETURNING $index";
|
||||
}
|
||||
}
|
||||
$stmt = $this->prepare($sql);
|
||||
$stmt->setFetchMode(PDO::FETCH_ASSOC);
|
||||
$stmt->execute($prep);
|
||||
$result = $stmt->fetch();
|
||||
if ($return_id) {
|
||||
if ($this->isPostgres()) {
|
||||
return $result[$index];
|
||||
} else {
|
||||
$result = $this->fetchOneArray("SELECT $index AS lastid FROM $table WHERE OID = last_insert_rowid()");
|
||||
return $result['lastid'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Создает UPDATE запрос
|
||||
*/
|
||||
function updateQuery($table, array $values, $cond)
|
||||
{
|
||||
$prep = $this->prepareValues($values);
|
||||
$sql = "UPDATE $table SET " . implode(
|
||||
",",
|
||||
array_map(function ($k, $v) {
|
||||
return $k . "=" . $v; }, array_keys($values), array_keys($prep))
|
||||
) . " WHERE $cond";
|
||||
|
||||
$stmt = $this->prepare($sql);
|
||||
$stmt->setFetchMode(PDO::FETCH_ASSOC);
|
||||
$stmt->execute($prep);
|
||||
}
|
||||
|
||||
function getIdGenerator()
|
||||
{
|
||||
return new IdGenerator($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Замечание: Только для Postgres SQL
|
||||
* @param string $seq Имя последовательности для ключа таблицы
|
||||
* @return int Идентефикатор следующей записи
|
||||
*/
|
||||
function getNextId($seq)
|
||||
{
|
||||
$result = $this->fetchOneArray("SELECT nextval('$seq')");
|
||||
return $result['nextval'];
|
||||
}
|
||||
|
||||
function close()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
$pg = $this->isPostgres();
|
||||
$prep = [];
|
||||
foreach ($values as $key => $value) {
|
||||
$result = null;
|
||||
if(is_bool($value)) {
|
||||
if ($pg) {
|
||||
$result = $value ? 'true' : 'false';
|
||||
} else {
|
||||
$result = $value ? 1 : 0;
|
||||
}
|
||||
} else {
|
||||
$result = $value;
|
||||
}
|
||||
$prep[":" . $key] = $result;
|
||||
}
|
||||
return $prep;
|
||||
}
|
||||
/**
|
||||
* Создает INSERT запрос
|
||||
*/
|
||||
function insertQuery($table, array $values, $return_id = false, $index = null)
|
||||
{
|
||||
$prep = $this->prepareValues($values);
|
||||
|
||||
$sql = "INSERT INTO $table (" . implode(",", array_keys($values))
|
||||
. ") VALUES (" . implode(",", array_keys($prep)). ")";
|
||||
if ($return_id) {
|
||||
if ($this->isPostgres()){
|
||||
$sql .= " RETURNING $index";
|
||||
}
|
||||
}
|
||||
$stmt = $this->prepare($sql);
|
||||
$stmt->setFetchMode(PDO::FETCH_ASSOC);
|
||||
$stmt->execute($prep);
|
||||
$result = $stmt->fetch();
|
||||
if ($return_id) {
|
||||
if ($this->isPostgres()) {
|
||||
return $result[$index];
|
||||
} else {
|
||||
$result = $this->fetchOneArray("SELECT $index AS lastid FROM $table WHERE OID = last_insert_rowid()");
|
||||
return $result['lastid'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Создает UPDATE запрос
|
||||
*/
|
||||
function updateQuery($table, array $values, $cond)
|
||||
{
|
||||
$prep = $this->prepareValues($values);
|
||||
$sql = "UPDATE $table SET " . implode(",",
|
||||
array_map(function($k,$v){return $k."=".$v;}, array_keys($values), array_keys($prep))) . " WHERE $cond";
|
||||
|
||||
$stmt = $this->prepare($sql);
|
||||
$stmt->setFetchMode(PDO::FETCH_ASSOC);
|
||||
$stmt->execute($prep);
|
||||
}
|
||||
|
||||
function getIdGenerator() {
|
||||
return new IdGenerator($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Замечание: Только для Postgres SQL
|
||||
* @param string $seq Имя последовательности для ключа таблицы
|
||||
* @return int Идентефикатор следующей записи
|
||||
*/
|
||||
function getNextId($seq) {
|
||||
$result = $this->fetchOneArray("SELECT nextval('$seq')");
|
||||
return $result['nextval'];
|
||||
}
|
||||
|
||||
function close() {
|
||||
return null;
|
||||
}
|
||||
}}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue