phplibrary/src/Database/Statement.php

59 lines
1.4 KiB
PHP

<?php
/**
* Класс оболочка для PDOStatement для замены Creole
*/
namespace ctiso\Database;
use PDO,
ctiso\Database;
class Statement
{
protected $limit = null;
protected $offset = null;
protected $statement = null;
protected $binds = array();
protected $conn;
protected $query;
function __construct($query, $conn/*: Database*/) {
$this->query = $query;
$this->conn = $conn;
}
function setInt($n, $value) {
$this->binds [] = [$n, $value, PDO::PARAM_INT];
}
function setString($n, $value) {
$this->binds [] = [$n, $value, PDO::PARAM_STR];
}
function setBlob($n, $value) {
$this->binds [] = [$n, $value, PDO::PARAM_LOB];
}
function setLimit($limit) {
$this->limit = $limit;
}
function setOffset($offset) {
$this->offset = $offset;
}
function executeQuery() {
if ($this->limit) {
$this->query .= " LIMIT {$this->limit} OFFSET {$this->offset}";
}
$stmt/*: PDOStatement*/ = $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;
}
}