phplibrary/src/Database/Statement.php

91 lines
1.9 KiB
PHP

<?php
/**
* Класс оболочка для PDOStatement для замены Creole
*/
namespace ctiso\Database;
use PDO;
use ctiso\Database;
class Statement
{
/** @var ?int */
protected $limit = null;
/** @var ?int */
protected $offset = null;
/** @var never */
protected $statement = null;
/** @var array{int|string, mixed, int}[] */
protected $binds = [];
/** @var Database */
protected $conn;
/** @var string */
protected $query;
/**
* @param string $query
* @param Database $conn
*/
function __construct($query, $conn) {
$this->query = $query;
$this->conn = $conn;
}
/**
* @param int|string $n
* @param int $value
*/
function setInt($n, $value): void {
$this->binds [] = [$n, $value, PDO::PARAM_INT];
}
/**
* @param int|string $n
* @param string $value
*/
function setString($n, $value): void {
$this->binds [] = [$n, $value, PDO::PARAM_STR];
}
/**
* @param int|string $n
* @param mixed $value
*/
function setBlob($n, $value): void {
$this->binds [] = [$n, $value, PDO::PARAM_LOB];
}
/**
* @param int $limit
*/
function setLimit($limit): void {
$this->limit = $limit;
}
/**
* @param int $offset
*/
function setOffset($offset): void {
$this->offset = $offset;
}
/**
* @return PDOStatement
*/
function executeQuery() {
if ($this->limit) {
$this->query .= " LIMIT {$this->limit} OFFSET {$this->offset}";
}
$stmt = $this->conn->prepare($this->query);
foreach ($this->binds as $bind) {
list($n, $value, $type) = $bind;
$stmt->bindValue($n, $value, (int) $type);
}
$stmt->execute();
$stmt->cache = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $stmt;
}
}