Обновил модуль pdo
This commit is contained in:
parent
0e475190a2
commit
bc2541644a
1 changed files with 91 additions and 32 deletions
|
|
@ -1,7 +1,8 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Êëàññ îáîëî÷êà äëÿ PDO äëÿ çàìåíû Creole
|
||||
* @package system.db
|
||||
* Класс оболочка для PDO для замены Creole
|
||||
*/
|
||||
class Database extends PDO
|
||||
{
|
||||
|
|
@ -17,9 +18,12 @@ class Database extends PDO
|
|||
return $this->dsn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Создает соединение с базой данных
|
||||
*/
|
||||
static function getConnection(array $dsn)
|
||||
{
|
||||
if ($dsn['phptype'] == 'pgsql') {
|
||||
if ($dsn['phptype'] == 'pgsql' || $dsn['phptype'] == 'mysql') {
|
||||
$port = (isset($dsn['port'])) ? "port={$dsn['port']};" : "";
|
||||
$connection = new Database("{$dsn['phptype']}:host={$dsn['hostspec']}; $port dbname={$dsn['database']}", $dsn['username'], $dsn['password']);
|
||||
}
|
||||
|
|
@ -32,11 +36,14 @@ class Database extends PDO
|
|||
|
||||
public function executeQuery($query)
|
||||
{
|
||||
$sth = $this->prepare($query);
|
||||
$sth->setFetchMode(PDO::FETCH_ASSOC);
|
||||
$sth->execute();
|
||||
// print_r($sth->fetchAll());
|
||||
return $sth;//$sth->fetchAll();
|
||||
if (isset($_GET['debug'])) {
|
||||
fb($query);
|
||||
}
|
||||
$stmt = $this->prepare($query);
|
||||
$stmt->setFetchMode(PDO::FETCH_ASSOC);
|
||||
$stmt->execute();
|
||||
$stmt->cache = $stmt->fetchAll();
|
||||
return $stmt;//$sth->fetchAll();
|
||||
}
|
||||
|
||||
public function prepareStatement($query)
|
||||
|
|
@ -44,7 +51,10 @@ class Database extends PDO
|
|||
return new DatabaseStatement($query, $this);
|
||||
}
|
||||
|
||||
// Äëÿ ñîâìåñòèìîñòè ñî ñòàðûì ïðåäñòàâëåíèåì áàç äàííûõ CIS
|
||||
// Для совместимости со старым представлением баз данных CIS
|
||||
/**
|
||||
* Извлекает из базы все элементы по запросу
|
||||
*/
|
||||
public function fetchAllArray($query)
|
||||
{
|
||||
$sth = $this->prepare($query);
|
||||
|
|
@ -53,6 +63,9 @@ class Database extends PDO
|
|||
return $sth->fetchAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Извлекает из базы первый элемент по запросу
|
||||
*/
|
||||
public function fetchOneArray($query)
|
||||
{
|
||||
$sth = $this->prepare($query);
|
||||
|
|
@ -61,23 +74,38 @@ class Database extends PDO
|
|||
return $sth->fetch();
|
||||
}
|
||||
|
||||
private function assignQuote($x, $y)
|
||||
private static function assignQuote($x, $y)
|
||||
{
|
||||
return $x . "=" . $this->quote($y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Создает INSERT запрос
|
||||
*/
|
||||
function insertQuery($table, array $values)
|
||||
{
|
||||
return $this->query("INSERT INTO $table (" . implode(",", array_keys($values))
|
||||
. ") VALUES (" . implode(",", array_map(array($this, 'quote'), array_values($values))) . ")");
|
||||
}
|
||||
|
||||
/**
|
||||
* Создает UPDATE запрос
|
||||
*/
|
||||
function updateQuery($table, array $values, $cond)
|
||||
{
|
||||
return $this->query("UPDATE $table SET " . implode(",",
|
||||
array_map(array($this, 'assignQuote'), array_keys($values), array_values($values))) . " WHERE $cond");
|
||||
array_map(array('self', 'assignQuote'), array_keys($values), array_values($values))) . " WHERE $cond");
|
||||
}
|
||||
|
||||
function getIdGenerator() {
|
||||
return new IdGenerator($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Замечание: Только для Postgres SQL
|
||||
* @param string $seq Имя последовательности для ключа таблицы
|
||||
* @return int Идентефикатор следующей записи
|
||||
*/
|
||||
function getNextId($seq)
|
||||
{
|
||||
$result = $this->fetchOneArray("SELECT nextval('$seq')");
|
||||
|
|
@ -89,6 +117,30 @@ class Database extends PDO
|
|||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class IdGenerator {
|
||||
private $db;
|
||||
|
||||
function __construct($db) {
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
function isBeforeInsert() {
|
||||
return false;
|
||||
}
|
||||
|
||||
function isAfterInsert() {
|
||||
return true;
|
||||
}
|
||||
|
||||
function getId($seq) {
|
||||
// $result = $this->db->fetchOneArray("SELECT nextval('$seq')");
|
||||
// return $result['nextval'];
|
||||
$result = $this->db->fetchOneArray("SELECT last_insert_rowid() AS nextval");
|
||||
return $result['nextval'];
|
||||
}
|
||||
}
|
||||
|
||||
class PDODatabaseStatementIterator implements Iterator
|
||||
{
|
||||
|
||||
|
|
@ -98,6 +150,7 @@ class PDODatabaseStatementIterator implements Iterator
|
|||
private $row_count;
|
||||
private $rs;
|
||||
|
||||
|
||||
/**
|
||||
* Construct the iterator.
|
||||
* @param PgSQLResultSet $rs
|
||||
|
|
@ -110,13 +163,12 @@ class PDODatabaseStatementIterator implements Iterator
|
|||
|
||||
function rewind()
|
||||
{
|
||||
// echo 'rewind';
|
||||
$this->pos = 0;
|
||||
}
|
||||
|
||||
function valid()
|
||||
{
|
||||
return ( $this->pos < $this->row_count );
|
||||
return ($this->pos < $this->row_count);
|
||||
}
|
||||
|
||||
function key()
|
||||
|
|
@ -126,7 +178,10 @@ class PDODatabaseStatementIterator implements Iterator
|
|||
|
||||
function current()
|
||||
{
|
||||
return $this->result->fetch(PDO::FETCH_ASSOC);
|
||||
if (!isset($this->result->cache[$this->pos])) {
|
||||
$this->result->cache[$this->pos] = $this->result->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
return $this->result->cache[$this->pos];
|
||||
}
|
||||
|
||||
function next()
|
||||
|
|
@ -147,10 +202,10 @@ class PDODatabaseStatementIterator implements Iterator
|
|||
class PDODatabaseStatement extends PDOStatement implements IteratorAggregate
|
||||
{
|
||||
protected $cursorPos = 0;
|
||||
public $cache = array();
|
||||
|
||||
function getIterator()
|
||||
{
|
||||
// echo 'getiterator';
|
||||
return new PDODatabaseStatementIterator($this);
|
||||
}
|
||||
|
||||
|
|
@ -159,13 +214,11 @@ class PDODatabaseStatement extends PDOStatement implements IteratorAggregate
|
|||
|
||||
function rewind()
|
||||
{
|
||||
// echo 'rewind';
|
||||
$this->cursorPos = 0;
|
||||
}
|
||||
|
||||
public function seek($rownum)
|
||||
{
|
||||
// echo 'seek';
|
||||
if ($rownum < 0) {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -178,38 +231,38 @@ class PDODatabaseStatement extends PDOStatement implements IteratorAggregate
|
|||
|
||||
function valid()
|
||||
{
|
||||
// echo 'valid';
|
||||
return ( true );
|
||||
}
|
||||
|
||||
|
||||
public function first()
|
||||
{
|
||||
// echo 'first';
|
||||
if($this->cursorPos !== 0) { $this->seek(0); }
|
||||
return $this->next();
|
||||
}
|
||||
|
||||
function next()
|
||||
{
|
||||
// echo 'next';
|
||||
if ($this->getRecordCount() > $this->cursorPos) {
|
||||
$this->fields = $this->fetch(PDO::FETCH_ASSOC);
|
||||
if (!isset($this->cache[$this->cursorPos])) {
|
||||
$this->cache[$this->cursorPos] = $this->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
$this->fields = $this->cache[$this->cursorPos];
|
||||
|
||||
$this->cursorPos++;
|
||||
return true;
|
||||
} else {
|
||||
$this->fields = null;
|
||||
return false;
|
||||
}
|
||||
// $this->cursorPos++;
|
||||
return true;
|
||||
}
|
||||
|
||||
function key() {
|
||||
// echo 'key';
|
||||
return $this->cursorPos;
|
||||
}
|
||||
|
||||
function current()
|
||||
{
|
||||
// echo 'current';
|
||||
return $this->result->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
|
|
@ -233,20 +286,25 @@ class PDODatabaseStatement extends PDOStatement implements IteratorAggregate
|
|||
return $this->fields[$name];
|
||||
}
|
||||
|
||||
function getBoolean($name)
|
||||
{
|
||||
return (bool)$this->fields[$name];
|
||||
}
|
||||
|
||||
function get($name)
|
||||
{
|
||||
return $this->cursorPos[$name];
|
||||
return $this->fields[$name];
|
||||
}
|
||||
|
||||
function getRecordCount()
|
||||
{
|
||||
return $this->rowCount();
|
||||
return count($this->cache);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Êëàññ îáîëî÷êà äëÿ PDOStatement äëÿ çàìåíû Creole
|
||||
* Класс оболочка для PDOStatement для замены Creole
|
||||
*/
|
||||
class DatabaseStatement
|
||||
{
|
||||
|
|
@ -274,7 +332,7 @@ class DatabaseStatement
|
|||
|
||||
function setBlob($n, $value)
|
||||
{
|
||||
$this->binds [] = array($n, $value, PDO::PARAM_BLOB);
|
||||
$this->binds [] = array($n, $value, PDO::PARAM_LOB);
|
||||
}
|
||||
|
||||
function setLimit($limit)
|
||||
|
|
@ -290,7 +348,7 @@ class DatabaseStatement
|
|||
function executeQuery()
|
||||
{
|
||||
if ($this->limit) {
|
||||
$this->queryString .= " LIMIT {$this->limit} OFFSET {$this->offset}";
|
||||
$this->query .= " LIMIT {$this->limit} OFFSET {$this->offset}";
|
||||
}
|
||||
$stmt = $this->conn->prepare($this->query);
|
||||
foreach ($this->binds as $bind) {
|
||||
|
|
@ -299,7 +357,8 @@ class DatabaseStatement
|
|||
}
|
||||
$stmt->setFetchMode(PDO::FETCH_ASSOC);
|
||||
$stmt->execute();
|
||||
$stmt->cache = $stmt->fetchAll();
|
||||
|
||||
return $stmt;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue