mirror of
https://github.com/dg/dibi.git
synced 2025-08-12 17:14:16 +02:00
* renamed some files libs
* added doc comments to drivers * DibiDriver::prepare() renamed to config() * fixed connection error handling in Postgre driver
This commit is contained in:
@@ -59,7 +59,7 @@ abstract class DibiDriver extends NObject
|
||||
* @param array connect configuration
|
||||
* @throws DibiException
|
||||
*/
|
||||
public function __construct($config)
|
||||
public function __construct(array $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
|
||||
@@ -183,7 +183,7 @@ abstract class DibiDriver extends NObject
|
||||
* @param string alias key
|
||||
* @return void
|
||||
*/
|
||||
protected static function prepare(&$config, $key, $alias=NULL)
|
||||
protected static function config(&$config, $key, $alias=NULL)
|
||||
{
|
||||
if (isset($config[$key])) return;
|
||||
|
||||
@@ -290,7 +290,7 @@ abstract class DibiDriver extends NObject
|
||||
|
||||
|
||||
/**
|
||||
* Experimental - injects LIMIT/OFFSET to the SQL query
|
||||
* Injects LIMIT/OFFSET to the SQL query
|
||||
*
|
||||
* @param string &$sql The SQL query that will be modified.
|
||||
* @param int $limit
|
@@ -22,7 +22,7 @@
|
||||
|
||||
|
||||
/**
|
||||
* dibi basic logger & profiler
|
||||
* dibi basic logger & profiler (experimental)
|
||||
*
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
@@ -26,10 +26,14 @@
|
||||
*
|
||||
* <code>
|
||||
* $result = dibi::query('SELECT * FROM [table]');
|
||||
*
|
||||
* $row = $result->fetch();
|
||||
* $value = $result->fetchSingle();
|
||||
* $all = $result->fetchAll();
|
||||
* $table = $result->fetchAll();
|
||||
* $pairs = $result->fetchPairs();
|
||||
* $assoc = $result->fetchAssoc('id');
|
||||
* $assoc = $result->fetchAssoc('active', 'id');
|
||||
*
|
||||
* unset($result);
|
||||
* </code>
|
||||
*
|
||||
@@ -443,7 +447,7 @@ abstract class DibiResult extends NObject implements IteratorAggregate, Countabl
|
||||
|
||||
|
||||
/**
|
||||
* Displays complete result-set as HTML table
|
||||
* Displays complete result-set as HTML table for debug purposes
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
@@ -475,108 +479,27 @@ abstract class DibiResult extends NObject implements IteratorAggregate, Countabl
|
||||
|
||||
|
||||
|
||||
/** these are the required IteratorAggregate functions */
|
||||
final public function getIterator($offset = NULL, $count = NULL)
|
||||
/**
|
||||
* Required by the IteratorAggregate interface
|
||||
* @param int offset
|
||||
* @param int limit
|
||||
* @return ArrayIterator
|
||||
*/
|
||||
final public function getIterator($offset = NULL, $limit = NULL)
|
||||
{
|
||||
return new DibiResultIterator($this, $offset, $count);
|
||||
return new DibiResultIterator($this, $offset, $limit);
|
||||
}
|
||||
/** end required IteratorAggregate functions */
|
||||
|
||||
|
||||
|
||||
/** these are the required Countable functions */
|
||||
/**
|
||||
* Required by the Countable interface
|
||||
* @return int
|
||||
*/
|
||||
final public function count()
|
||||
{
|
||||
return $this->rowCount();
|
||||
}
|
||||
/** end required Countable functions */
|
||||
|
||||
|
||||
} // class DibiResult
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Basic Result set iterator.
|
||||
*
|
||||
* This can be returned by DibiResult::getIterator() method or directly using foreach:
|
||||
* <code>
|
||||
* $result = dibi::query('SELECT * FROM table');
|
||||
* foreach ($result as $fields) {
|
||||
* print_r($fields);
|
||||
* }
|
||||
* unset($result);
|
||||
* </code>
|
||||
*
|
||||
* Optionally you can specify offset and limit:
|
||||
* <code>
|
||||
* foreach ($result->getIterator(2, 3) as $fields) {
|
||||
* print_r($fields);
|
||||
* }
|
||||
* </code>
|
||||
*/
|
||||
final class DibiResultIterator implements Iterator
|
||||
{
|
||||
private
|
||||
$result,
|
||||
$offset,
|
||||
$count,
|
||||
$row,
|
||||
$pointer;
|
||||
|
||||
|
||||
public function __construct(DibiResult $result, $offset = NULL, $count = NULL)
|
||||
{
|
||||
$this->result = $result;
|
||||
$this->offset = (int) $offset;
|
||||
$this->count = $count === NULL ? 2147483647 /*PHP_INT_MAX till 5.0.5 */ : (int) $count;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** these are the required Iterator functions */
|
||||
public function rewind()
|
||||
{
|
||||
$this->pointer = 0;
|
||||
@$this->result->seek($this->offset);
|
||||
$this->row = $this->result->fetch();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function key()
|
||||
{
|
||||
return $this->pointer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function current()
|
||||
{
|
||||
return $this->row;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function next()
|
||||
{
|
||||
$this->row = $this->result->fetch();
|
||||
$this->pointer++;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function valid()
|
||||
{
|
||||
return is_array($this->row) && ($this->pointer < $this->count);
|
||||
}
|
||||
/** end required Iterator functions */
|
||||
|
||||
|
||||
|
||||
} // class DibiResultIterator
|
125
dibi/libs/DibiResultIterator.php
Normal file
125
dibi/libs/DibiResultIterator.php
Normal file
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* dibi - tiny'n'smart database abstraction layer
|
||||
* ----------------------------------------------
|
||||
*
|
||||
* Copyright (c) 2005, 2007 David Grudl aka -dgx- (http://www.dgx.cz)
|
||||
*
|
||||
* This source file is subject to the "dibi license" that is bundled
|
||||
* with this package in the file license.txt.
|
||||
*
|
||||
* For more information please see http://php7.org/dibi/
|
||||
*
|
||||
* @author David Grudl
|
||||
* @copyright Copyright (c) 2005, 2007 David Grudl
|
||||
* @license http://php7.org/dibi/license (dibi license)
|
||||
* @category Database
|
||||
* @package Dibi
|
||||
* @link http://php7.org/dibi/
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* External result set iterator
|
||||
*
|
||||
* This can be returned by DibiResult::getIterator() method or using foreach
|
||||
* <code>
|
||||
* $result = dibi::query('SELECT * FROM table');
|
||||
* foreach ($result as $row) {
|
||||
* print_r($row);
|
||||
* }
|
||||
* unset($result);
|
||||
* </code>
|
||||
*
|
||||
* Optionally you can specify offset and limit:
|
||||
* <code>
|
||||
* foreach ($result->getIterator(2, 3) as $row) {
|
||||
* print_r($row);
|
||||
* }
|
||||
* </code>
|
||||
*/
|
||||
final class DibiResultIterator implements Iterator
|
||||
{
|
||||
private
|
||||
$result,
|
||||
$offset,
|
||||
$limit,
|
||||
$row,
|
||||
$pointer;
|
||||
|
||||
|
||||
/**
|
||||
* Required by the Iterator interface
|
||||
* @param int offset
|
||||
* @param int limit
|
||||
*/
|
||||
public function __construct(DibiResult $result, $offset, $limit)
|
||||
{
|
||||
$this->result = $result;
|
||||
$this->offset = (int) $offset;
|
||||
$this->limit = $limit === NULL ? -1 : (int) $limit;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Rewinds the Iterator to the first element
|
||||
* @return void
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
$this->pointer = 0;
|
||||
@$this->result->seek($this->offset);
|
||||
$this->row = $this->result->fetch();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns the key of the current element.
|
||||
* @return mixed
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
return $this->pointer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns the current element.
|
||||
* @return mixed
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
return $this->row;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Moves forward to next element.
|
||||
* @return void
|
||||
*/
|
||||
public function next()
|
||||
{
|
||||
$this->row = $this->result->fetch();
|
||||
$this->pointer++;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Checks if there is a current element after calls to rewind() or next().
|
||||
* @return bool
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
return is_array($this->row) && ($this->limit < 0 || $this->pointer < $this->limit);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // class DibiResultIterator
|
@@ -22,7 +22,7 @@
|
||||
|
||||
|
||||
/**
|
||||
* dibi translator
|
||||
* dibi SQL translator
|
||||
*
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
@@ -54,7 +54,7 @@ final class DibiTranslator extends NObject
|
||||
|
||||
|
||||
|
||||
public function __construct($driver)
|
||||
public function __construct(DibiDriver $driver)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
}
|
||||
@@ -67,7 +67,7 @@ final class DibiTranslator extends NObject
|
||||
* @param array
|
||||
* @return bool
|
||||
*/
|
||||
public function translate($args)
|
||||
public function translate(array $args)
|
||||
{
|
||||
$this->hasError = FALSE;
|
||||
$commandIns = NULL;
|
||||
@@ -144,6 +144,12 @@ final class DibiTranslator extends NObject
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Apply modifier to single value
|
||||
* @param mixed
|
||||
* @param string
|
||||
* @return string
|
||||
*/
|
||||
private function formatValue($value, $modifier)
|
||||
{
|
||||
// array processing (with or without modifier)
|
||||
@@ -412,4 +418,4 @@ final class DibiTranslator extends NObject
|
||||
}
|
||||
|
||||
|
||||
} // class DibiParser
|
||||
} // class DibiTranslator
|
@@ -21,6 +21,12 @@
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* compatiblity
|
||||
*/
|
||||
if (!class_exists('NObject', FALSE)) {
|
||||
|
||||
|
||||
/**
|
||||
* NObject is the ultimate ancestor of all instantiable classes.
|
||||
*
|
||||
@@ -227,3 +233,6 @@ abstract class NObject
|
||||
class NPropertyException extends Exception
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user