1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-24 02:43:09 +01:00
php-dibi/dibi/libs/resultset.php

498 lines
11 KiB
PHP
Raw Normal View History

2006-06-04 23:06:33 +00:00
<?php
/**
2007-04-11 18:30:30 +00:00
* This file is part of the "dibi" project (http://dibi.texy.info/)
2006-06-04 23:06:33 +00:00
*
2007-06-24 23:49:57 +00:00
* @author David Grudl
* @copyright Copyright (c) 2005-2007 David Grudl aka -dgx- (http://www.dgx.cz)
* @license New BSD License
* @version $Revision$ $Date$
* @category Database
* @package Dibi
2006-06-04 23:06:33 +00:00
*/
// security - include dibi.php, not this file
if (!class_exists('dibi', FALSE)) die();
2006-06-04 23:06:33 +00:00
// PHP < 5.1 compatibility
if (!interface_exists('Countable', false)) {
interface Countable
{
function count();
}
}
/**
* dibi result-set abstract class
*
* <code>
* $result = dibi::query('SELECT * FROM [table]');
* $value = $result->fetchSingle();
* $all = $result->fetchAll();
* $assoc = $result->fetchAssoc('id');
* $assoc = $result->fetchAssoc('active', 'id');
2006-06-04 23:06:33 +00:00
* unset($result);
* </code>
*/
abstract class DibiResult implements IteratorAggregate, Countable
{
/**
* Describes columns types
* @var array
*/
protected $convert;
/**
* Describes columns types
* @var array
*/
protected $meta;
2006-06-04 23:06:33 +00:00
static private $types = array(
dibi::FIELD_TEXT => 'string',
dibi::FIELD_BINARY => 'string',
dibi::FIELD_BOOL => 'bool',
dibi::FIELD_INTEGER => 'int',
dibi::FIELD_FLOAT => 'float',
dibi::FIELD_COUNTER => 'int',
);
2006-06-04 23:06:33 +00:00
/**
* Moves cursor position without fetching row
* @param int the 0-based cursor pos to seek to
* @return boolean TRUE on success, FALSE if unable to seek to specified record
*/
abstract public function seek($row);
2006-06-04 23:06:33 +00:00
/**
* Returns the number of rows in a result set
* @return int
*/
abstract public function rowCount();
/**
* Frees the resources allocated for this result set
* @return void
*/
abstract protected function free();
2006-06-04 23:06:33 +00:00
/**
* Fetches the row at current position and moves the internal cursor to the next position
* internal usage only
2007-06-25 17:02:12 +00:00
* @return array|FALSE array on success, FALSE if no next record
2006-06-04 23:06:33 +00:00
*/
abstract protected function doFetch();
2006-06-04 23:06:33 +00:00
/**
* Fetches the row at current position, process optional type conversion
* and moves the internal cursor to the next position
2007-06-25 17:02:12 +00:00
* @return array|FALSE array on success, FALSE if no next record
2006-06-04 23:06:33 +00:00
*/
final public function fetch()
{
2007-06-25 17:02:12 +00:00
$row = $this->doFetch();
if (!is_array($row))
2006-06-04 23:06:33 +00:00
return FALSE;
// types-converting?
if ($t = $this->convert) { // little speed-up
2007-06-25 17:02:12 +00:00
foreach ($row as $key => $value) {
2006-06-04 23:06:33 +00:00
if (isset($t[$key]))
2007-06-25 17:02:12 +00:00
$row[$key] = $this->convert($value, $t[$key]);
2006-06-04 23:06:33 +00:00
}
}
2007-06-25 17:02:12 +00:00
return $row;
2006-06-04 23:06:33 +00:00
}
/**
* Like fetch(), but returns only first field
* @return mixed value on success, FALSE if no next record
*/
final function fetchSingle()
{
2007-06-25 17:02:12 +00:00
$row = $this->doFetch();
if (!is_array($row))
2006-06-04 23:06:33 +00:00
return FALSE;
// types-converting?
if ($t = $this->convert) { // little speed-up
2007-06-25 17:02:12 +00:00
$value = reset($row);
$key = key($row);
2006-06-04 23:06:33 +00:00
return isset($t[$key])
? $this->convert($value, $t[$key])
: $value;
}
2007-06-25 17:02:12 +00:00
return reset($row);
2006-06-04 23:06:33 +00:00
}
/**
2007-02-05 05:14:48 +00:00
* Fetches all records from table.
2006-06-04 23:06:33 +00:00
* @return array
*/
final function fetchAll()
{
@$this->seek(0);
2007-06-25 17:02:12 +00:00
$row = $this->fetch();
if (!$row)
2006-06-04 23:06:33 +00:00
return array(); // empty resultset
2007-06-25 17:02:12 +00:00
$data = array();
if (count($row) === 1) {
$key = key($row);
2006-06-04 23:06:33 +00:00
do {
2007-06-25 17:02:12 +00:00
$data[] = $row[$key];
} while ($row = $this->fetch());
2006-06-04 23:06:33 +00:00
} else {
do {
2007-06-25 17:02:12 +00:00
$data[] = $row;
} while ($row = $this->fetch());
2006-06-04 23:06:33 +00:00
}
2007-06-25 17:02:12 +00:00
return $data;
}
/**
2007-02-05 05:14:48 +00:00
* Fetches all records from table and returns associative tree
* Associative descriptor: assoc1,*,assoc2,#,assco3
2007-06-25 17:02:12 +00:00
* builds a tree: $data[value1][index][value2]['assoc3'][value3] = {record}
2007-02-05 05:14:48 +00:00
*
* @param string associative descriptor
* @return array
*/
2007-02-05 05:14:48 +00:00
final function fetchAssoc($assoc)
{
@$this->seek(0);
2007-06-25 17:02:12 +00:00
$row = $this->fetch();
if (!$row) return array(); // empty resultset
2007-06-25 17:02:12 +00:00
$data = NULL;
2007-02-05 05:14:48 +00:00
$assoc = explode(',', $assoc);
2007-02-05 05:14:48 +00:00
if (count($assoc) === 1) { // speed-up
$as = $assoc[0];
do {
2007-06-25 17:02:12 +00:00
$data[ $row[$as] ] = $row;
} while ($row = $this->fetch());
return $data;
2007-02-05 05:14:48 +00:00
}
2007-02-05 05:14:48 +00:00
$last = count($assoc) - 1;
if ($assoc[$last] === '#') unset($assoc[$last]);
2007-02-05 05:14:48 +00:00
// make associative tree
do {
2007-06-25 17:02:12 +00:00
$x = & $data;
2007-02-05 05:14:48 +00:00
// iterative deepening
foreach ($assoc as $i => $as) {
if ($as === '*') { // indexed-array node
$x = & $x[];
} elseif ($as === '#') { // "record" node
if ($x === NULL) {
2007-06-25 17:02:12 +00:00
$x = $row;
2007-02-05 05:14:48 +00:00
$x = & $x[ $assoc[$i+1] ];
$x = NULL; // prepare child node
} else {
$x = & $x[ $assoc[$i+1] ];
}
} else { // associative-array node
2007-06-25 17:02:12 +00:00
$x = & $x[ $row[ $as ] ];
2007-02-05 05:14:48 +00:00
}
2006-06-04 23:06:33 +00:00
}
2007-06-25 17:02:12 +00:00
if ($x === NULL) $x = $row; // build leaf
2006-06-04 23:06:33 +00:00
2007-06-25 17:02:12 +00:00
} while ($row = $this->fetch());
2006-06-04 23:06:33 +00:00
2007-02-05 05:14:48 +00:00
unset($x);
2007-06-25 17:02:12 +00:00
return $data;
2006-06-04 23:06:33 +00:00
}
2007-02-05 05:14:48 +00:00
2006-06-04 23:06:33 +00:00
/**
* Fetches all records from table like $key => $value pairs
2007-04-25 08:19:03 +00:00
* @param string associative key
* @param string value
2006-06-04 23:06:33 +00:00
* @return array
*/
2007-04-25 08:19:03 +00:00
final function fetchPairs($key=NULL, $value=NULL)
2006-06-04 23:06:33 +00:00
{
@$this->seek(0);
2007-06-25 17:02:12 +00:00
$row = $this->fetch();
if (!$row) return array(); // empty resultset
2006-06-04 23:06:33 +00:00
2007-06-25 17:02:12 +00:00
$data = array();
2007-06-11 00:25:48 +00:00
2007-04-25 08:19:03 +00:00
if ($value === NULL) {
2007-06-11 00:25:48 +00:00
if ($key !== NULL) return FALSE; // error
// autodetect
2007-06-25 17:02:12 +00:00
if (count($row) < 2) return FALSE;
$tmp = array_keys($row);
2007-04-25 08:19:03 +00:00
$key = $tmp[0];
$value = $tmp[1];
2007-06-11 00:25:48 +00:00
2007-04-25 08:19:03 +00:00
} else {
2007-06-25 17:02:12 +00:00
if (!array_key_exists($value, $row)) return FALSE;
2007-06-11 00:25:48 +00:00
if ($key === NULL) { // autodetect
do {
2007-06-25 17:02:12 +00:00
$data[] = $row[$value];
} while ($row = $this->fetch());
return $data;
2007-06-11 00:25:48 +00:00
}
2007-06-25 17:02:12 +00:00
if (!array_key_exists($key, $row)) return FALSE;
2007-04-25 08:19:03 +00:00
}
2006-06-04 23:06:33 +00:00
do {
2007-06-25 17:02:12 +00:00
$data[ $row[$key] ] = $row[$value];
} while ($row = $this->fetch());
2006-06-04 23:06:33 +00:00
2007-06-25 17:02:12 +00:00
return $data;
2006-06-04 23:06:33 +00:00
}
/**
* Automatically frees the resources allocated for this result set
* @return void
*/
public function __destruct()
{
@$this->free();
}
public function setType($field, $type = NULL)
{
if ($field === TRUE)
$this->detectTypes();
elseif (is_array($field))
$this->convert = $field;
else
$this->convert[$field] = $type;
}
2006-06-04 23:06:33 +00:00
/** is this needed? */
public function getType($field)
{
return isset($this->convert[$field]) ? $this->convert[$field] : NULL;
}
2006-06-04 23:06:33 +00:00
public function convert($value, $type)
{
if ($value === NULL || $value === FALSE)
return $value;
if (isset(self::$types[$type])) {
settype($value, self::$types[$type]);
2006-06-04 23:06:33 +00:00
return $value;
}
2007-05-30 00:01:10 +00:00
if ($type === dibi::FIELD_DATE)
2006-06-07 15:50:32 +00:00
return strtotime($value); // !!! not good
2006-06-04 23:06:33 +00:00
2007-05-30 00:01:10 +00:00
if ($type === dibi::FIELD_DATETIME)
2006-06-07 15:50:32 +00:00
return strtotime($value); // !!! not good
2006-06-04 23:06:33 +00:00
return $value;
}
/**
* Gets an array of field names
* @return array
*/
public function getFields()
{
// lazy init
if ($this->meta === NULL) $this->buildMeta();
return array_keys($this->meta);
}
/**
* Gets an array of meta informations about column
* @param string column name
* @return array
*/
public function getMetaData($field)
{
// lazy init
if ($this->meta === NULL) $this->buildMeta();
return isset($this->meta[$field]) ? $this->meta[$field] : FALSE;
}
/**
* Acquires ....
* @return void
*/
protected function detectTypes()
{
if ($this->meta === NULL) $this->buildMeta();
}
/**
* @return void
*/
abstract protected function buildMeta();
2006-06-04 23:06:33 +00:00
/** these are the required IteratorAggregate functions */
public function getIterator($offset = NULL, $count = NULL)
{
return new DibiResultIterator($this, $offset, $count);
}
/** end required IteratorAggregate functions */
2006-06-04 23:06:33 +00:00
/** these are the required Countable functions */
public function count()
{
return $this->rowCount();
}
/** end required Countable functions */
/**
* Access to undeclared property
*/
2007-06-25 17:02:12 +00:00
function __get($name) { throw new Exception("Access to undeclared property: " . get_class($this) . "::$$name"); }
function __set($name, $value) { throw new Exception("Access to undeclared property: " . get_class($this) . "::$$name"); }
function __unset($name) { throw new Exception("Access to undeclared property: " . get_class($this) . "::$$name"); }
2006-06-04 23:06:33 +00:00
} // 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>
*/
class DibiResultIterator implements Iterator
{
private
$result,
$offset,
$count,
2007-06-25 17:02:12 +00:00
$row,
$pointer;
2006-06-04 23:06:33 +00:00
public function __construct(DibiResult $result, $offset = NULL, $count = NULL)
{
$this->result = $result;
$this->offset = (int) $offset;
2006-06-04 23:09:53 +00:00
$this->count = $count === NULL ? 2147483647 /*PHP_INT_MAX till 5.0.5 */ : (int) $count;
2006-06-04 23:06:33 +00:00
}
2006-06-04 23:06:33 +00:00
/** these are the required Iterator functions */
public function rewind()
{
2007-06-25 17:02:12 +00:00
$this->pointer = 0;
2006-06-04 23:06:33 +00:00
@$this->result->seek($this->offset);
2007-06-25 17:02:12 +00:00
$this->row = $this->result->fetch();
2006-06-04 23:06:33 +00:00
}
2006-06-04 23:06:33 +00:00
public function key()
{
2007-06-25 17:02:12 +00:00
return $this->pointer;
2006-06-04 23:06:33 +00:00
}
2006-06-04 23:06:33 +00:00
public function current()
{
2007-06-25 17:02:12 +00:00
return $this->row;
2006-06-04 23:06:33 +00:00
}
2006-06-04 23:06:33 +00:00
public function next()
{
2007-06-25 17:02:12 +00:00
$this->row = $this->result->fetch();
$this->pointer++;
2006-06-04 23:06:33 +00:00
}
2006-06-04 23:06:33 +00:00
public function valid()
{
2007-06-25 17:02:12 +00:00
return is_array($this->row) && ($this->pointer < $this->count);
2006-06-04 23:06:33 +00:00
}
/** end required Iterator functions */
2006-06-04 23:06:33 +00:00
} // class DibiResultIterator