2008-07-17 03:51:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dibi - tiny'n'smart database abstraction layer
|
|
|
|
* ----------------------------------------------
|
|
|
|
*
|
2008-12-31 00:13:40 +00:00
|
|
|
* Copyright (c) 2005, 2009 David Grudl (http://davidgrudl.com)
|
2008-07-17 03:51:29 +00:00
|
|
|
*
|
|
|
|
* 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://dibiphp.com
|
|
|
|
*
|
2008-12-31 00:13:40 +00:00
|
|
|
* @copyright Copyright (c) 2005, 2009 David Grudl
|
2008-07-17 03:51:29 +00:00
|
|
|
* @license http://dibiphp.com/license dibi license
|
|
|
|
* @link http://dibiphp.com
|
|
|
|
* @package dibi
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-08-28 02:02:21 +00:00
|
|
|
* dibi result-set.
|
2008-07-17 03:51:29 +00:00
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* $result = dibi::query('SELECT * FROM [table]');
|
|
|
|
*
|
|
|
|
* $row = $result->fetch();
|
|
|
|
* $value = $result->fetchSingle();
|
|
|
|
* $table = $result->fetchAll();
|
|
|
|
* $pairs = $result->fetchPairs();
|
|
|
|
* $assoc = $result->fetchAssoc('id');
|
|
|
|
* $assoc = $result->fetchAssoc('active,#,id');
|
|
|
|
*
|
|
|
|
* unset($result);
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @author David Grudl
|
2008-12-31 00:13:40 +00:00
|
|
|
* @copyright Copyright (c) 2005, 2009 David Grudl
|
2008-07-17 03:51:29 +00:00
|
|
|
* @package dibi
|
|
|
|
*/
|
2009-02-05 21:08:00 +00:00
|
|
|
class DibiResult extends DibiObject implements IDataSource
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
2008-10-28 15:24:47 +00:00
|
|
|
/** @var array IDibiDriver */
|
2008-07-17 03:51:29 +00:00
|
|
|
private $driver;
|
|
|
|
|
2008-10-28 15:24:47 +00:00
|
|
|
/** @var array Translate table */
|
2008-07-17 03:51:29 +00:00
|
|
|
private $xlat;
|
|
|
|
|
2008-10-28 15:24:47 +00:00
|
|
|
/** @var array Cache for $driver->getColumnsMeta() */
|
2008-10-28 14:37:40 +00:00
|
|
|
private $meta;
|
2008-07-17 03:51:29 +00:00
|
|
|
|
2008-10-28 15:24:47 +00:00
|
|
|
/** @var bool Already fetched? Used for allowance for first seek(0) */
|
2008-07-17 03:51:29 +00:00
|
|
|
private $fetched = FALSE;
|
|
|
|
|
2008-10-28 15:24:47 +00:00
|
|
|
/** @var array|FALSE Qualifiy each column name with the table name? */
|
2008-07-17 03:51:29 +00:00
|
|
|
private $withTables = FALSE;
|
|
|
|
|
2008-10-30 15:40:17 +00:00
|
|
|
/** @var string returned object class */
|
|
|
|
private $class = 'DibiRow';
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IDibiDriver
|
|
|
|
* @param array
|
|
|
|
*/
|
|
|
|
public function __construct($driver, $config)
|
|
|
|
{
|
|
|
|
$this->driver = $driver;
|
|
|
|
|
2008-10-10 01:35:33 +00:00
|
|
|
if (!empty($config[dibi::RESULT_WITH_TABLES])) {
|
2008-07-17 03:51:29 +00:00
|
|
|
$this->setWithTables(TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Automatically frees the resources allocated for this result set.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
@$this->free(); // intentionally @
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the result set resource.
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
final public function getResource()
|
|
|
|
{
|
|
|
|
return $this->getDriver()->getResultResource();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
final public function seek($row)
|
|
|
|
{
|
|
|
|
return ($row !== 0 || $this->fetched) ? (bool) $this->getDriver()->seek($row) : TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of rows in a result set.
|
|
|
|
* @return int
|
|
|
|
*/
|
2009-02-26 20:02:14 +00:00
|
|
|
final public function getRowCount()
|
|
|
|
{
|
|
|
|
return $this->getDriver()->getRowCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of rows in a result set. Alias for getRowCount().
|
|
|
|
* @return int
|
|
|
|
*/
|
2008-07-17 03:51:29 +00:00
|
|
|
final public function rowCount()
|
|
|
|
{
|
2009-02-26 20:02:14 +00:00
|
|
|
return $this->getDriver()->getRowCount();
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Frees the resources allocated for this result set.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
final public function free()
|
|
|
|
{
|
|
|
|
if ($this->driver !== NULL) {
|
|
|
|
$this->driver->free();
|
|
|
|
$this->driver = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Qualifiy each column name with the table name?
|
|
|
|
* @param bool
|
2009-05-24 23:32:42 +00:00
|
|
|
* @return DibiResult provides a fluent interface
|
2008-07-17 03:51:29 +00:00
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
final public function setWithTables($val)
|
|
|
|
{
|
|
|
|
if ($val) {
|
|
|
|
$cols = array();
|
2008-10-28 14:37:40 +00:00
|
|
|
foreach ($this->getMeta() as $info) {
|
2008-10-28 01:03:50 +00:00
|
|
|
$name = $info['fullname'];
|
2008-07-17 03:51:29 +00:00
|
|
|
if (isset($cols[$name])) {
|
|
|
|
$fix = 1;
|
|
|
|
while (isset($cols[$name . '#' . $fix])) $fix++;
|
|
|
|
$name .= '#' . $fix;
|
|
|
|
}
|
|
|
|
$cols[$name] = TRUE;
|
|
|
|
}
|
|
|
|
$this->withTables = array_keys($cols);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$this->withTables = FALSE;
|
|
|
|
}
|
2009-05-24 23:32:42 +00:00
|
|
|
return $this;
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Qualifiy each key with the table name?
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
final public function getWithTables()
|
|
|
|
{
|
|
|
|
return (bool) $this->withTables;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-10-30 15:40:17 +00:00
|
|
|
/**
|
|
|
|
* Set fetched object class. This class should extend the DibiRow class.
|
|
|
|
* @param string
|
2009-05-24 23:32:42 +00:00
|
|
|
* @return DibiResult provides a fluent interface
|
2008-10-30 15:40:17 +00:00
|
|
|
*/
|
|
|
|
public function setRowClass($class)
|
|
|
|
{
|
|
|
|
$this->class = $class;
|
2009-05-24 23:32:42 +00:00
|
|
|
return $this;
|
2008-10-30 15:40:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns fetched object class name.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getRowClass()
|
|
|
|
{
|
|
|
|
return $this->class;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
/**
|
|
|
|
* Fetches the row at current position, process optional type conversion.
|
|
|
|
* and moves the internal cursor to the next position
|
2008-10-10 17:39:33 +00:00
|
|
|
* @return DibiRow|FALSE array on success, FALSE if no next record
|
2008-07-17 03:51:29 +00:00
|
|
|
*/
|
2008-10-10 17:39:33 +00:00
|
|
|
final public function fetch()
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
|
|
|
if ($this->withTables === FALSE) {
|
|
|
|
$row = $this->getDriver()->fetch(TRUE);
|
|
|
|
if (!is_array($row)) return FALSE;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$row = $this->getDriver()->fetch(FALSE);
|
|
|
|
if (!is_array($row)) return FALSE;
|
|
|
|
$row = array_combine($this->withTables, $row);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fetched = TRUE;
|
|
|
|
|
|
|
|
// types-converting?
|
|
|
|
if ($this->xlat !== NULL) {
|
|
|
|
foreach ($this->xlat as $col => $type) {
|
|
|
|
if (isset($row[$col])) {
|
2008-07-22 13:12:53 +00:00
|
|
|
$row[$col] = $this->convert($row[$col], $type['type'], $type['format']);
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-30 15:40:17 +00:00
|
|
|
return new $this->class($row);
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like fetch(), but returns only first field.
|
|
|
|
* @return mixed value on success, FALSE if no next record
|
|
|
|
*/
|
2008-08-28 02:02:21 +00:00
|
|
|
final public function fetchSingle()
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
|
|
|
$row = $this->getDriver()->fetch(TRUE);
|
|
|
|
if (!is_array($row)) return FALSE;
|
|
|
|
$this->fetched = TRUE;
|
|
|
|
$value = reset($row);
|
|
|
|
|
|
|
|
// types-converting?
|
|
|
|
$key = key($row);
|
|
|
|
if (isset($this->xlat[$key])) {
|
|
|
|
$type = $this->xlat[$key];
|
2008-07-22 13:12:53 +00:00
|
|
|
return $this->convert($value, $type['type'], $type['format']);
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches all records from table.
|
|
|
|
* @param int offset
|
|
|
|
* @param int limit
|
2008-10-10 17:39:33 +00:00
|
|
|
* @return array of DibiRow
|
2008-07-17 03:51:29 +00:00
|
|
|
*/
|
2008-10-01 16:04:16 +00:00
|
|
|
final public function fetchAll($offset = NULL, $limit = NULL)
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
|
|
|
$limit = $limit === NULL ? -1 : (int) $limit;
|
|
|
|
$this->seek((int) $offset);
|
|
|
|
$row = $this->fetch();
|
|
|
|
if (!$row) return array(); // empty result set
|
|
|
|
|
|
|
|
$data = array();
|
2008-10-01 16:04:16 +00:00
|
|
|
do {
|
|
|
|
if ($limit === 0) break;
|
|
|
|
$limit--;
|
|
|
|
$data[] = $row;
|
|
|
|
} while ($row = $this->fetch());
|
2008-07-17 03:51:29 +00:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches all records from table and returns associative tree.
|
|
|
|
* Associative descriptor: assoc1,#,assoc2,=,assoc3,@
|
|
|
|
* builds a tree: $data[assoc1][index][assoc2]['assoc3']->value = {record}
|
|
|
|
* @param string associative descriptor
|
2008-10-10 17:39:33 +00:00
|
|
|
* @return DibiRow
|
2008-07-17 03:51:29 +00:00
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2008-08-28 02:02:21 +00:00
|
|
|
final public function fetchAssoc($assoc)
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
|
|
|
$this->seek(0);
|
2008-10-10 17:39:33 +00:00
|
|
|
$row = $this->fetch();
|
2008-07-17 03:51:29 +00:00
|
|
|
if (!$row) return array(); // empty result set
|
|
|
|
|
|
|
|
$data = NULL;
|
|
|
|
$assoc = explode(',', $assoc);
|
|
|
|
|
|
|
|
// check columns
|
|
|
|
foreach ($assoc as $as) {
|
2008-10-21 13:42:53 +00:00
|
|
|
// offsetExists ignores NULL in PHP 5.2.1, isset() surprisingly NULL accepts
|
|
|
|
if ($as !== '#' && $as !== '=' && $as !== '@' && !isset($row[$as])) {
|
2008-07-17 03:51:29 +00:00
|
|
|
throw new InvalidArgumentException("Unknown column '$as' in associative descriptor.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// strip leading = and @
|
2008-10-10 17:39:33 +00:00
|
|
|
$leaf = '@'; // gap
|
2008-07-17 03:51:29 +00:00
|
|
|
$last = count($assoc) - 1;
|
|
|
|
while ($assoc[$last] === '=' || $assoc[$last] === '@') {
|
|
|
|
$leaf = $assoc[$last];
|
|
|
|
unset($assoc[$last]);
|
|
|
|
$last--;
|
|
|
|
|
|
|
|
if ($last < 0) {
|
|
|
|
$assoc[] = '#';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// make associative tree
|
|
|
|
do {
|
2008-10-10 17:39:33 +00:00
|
|
|
$arr = (array) $row;
|
2008-07-17 03:51:29 +00:00
|
|
|
$x = & $data;
|
|
|
|
|
|
|
|
// iterative deepening
|
|
|
|
foreach ($assoc as $i => $as) {
|
|
|
|
if ($as === '#') { // indexed-array node
|
|
|
|
$x = & $x[];
|
|
|
|
|
|
|
|
} elseif ($as === '=') { // "record" node
|
|
|
|
if ($x === NULL) {
|
2008-10-10 17:39:33 +00:00
|
|
|
$x = $arr;
|
2008-07-17 03:51:29 +00:00
|
|
|
$x = & $x[ $assoc[$i+1] ];
|
|
|
|
$x = NULL; // prepare child node
|
|
|
|
} else {
|
|
|
|
$x = & $x[ $assoc[$i+1] ];
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ($as === '@') { // "object" node
|
|
|
|
if ($x === NULL) {
|
2008-10-10 17:39:33 +00:00
|
|
|
$x = clone $row;
|
2008-07-17 03:51:29 +00:00
|
|
|
$x = & $x->{$assoc[$i+1]};
|
|
|
|
$x = NULL; // prepare child node
|
|
|
|
} else {
|
|
|
|
$x = & $x->{$assoc[$i+1]};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} else { // associative-array node
|
2008-10-10 17:39:33 +00:00
|
|
|
$x = & $x[ $arr[ $as ] ];
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($x === NULL) { // build leaf
|
2008-09-03 22:47:49 +00:00
|
|
|
if ($leaf === '=') {
|
2008-10-10 17:39:33 +00:00
|
|
|
$x = $arr;
|
2008-09-03 22:47:49 +00:00
|
|
|
} else {
|
2008-10-10 17:39:33 +00:00
|
|
|
$x = $row;
|
2008-09-03 22:47:49 +00:00
|
|
|
}
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
2008-10-10 17:39:33 +00:00
|
|
|
} while ($row = $this->fetch());
|
2008-07-17 03:51:29 +00:00
|
|
|
|
|
|
|
unset($x);
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches all records from table like $key => $value pairs.
|
|
|
|
* @param string associative key
|
|
|
|
* @param string value
|
|
|
|
* @return array
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2008-08-28 02:02:21 +00:00
|
|
|
final public function fetchPairs($key = NULL, $value = NULL)
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
|
|
|
$this->seek(0);
|
2008-10-10 17:39:33 +00:00
|
|
|
$row = $this->fetch();
|
2008-07-17 03:51:29 +00:00
|
|
|
if (!$row) return array(); // empty result set
|
|
|
|
|
|
|
|
$data = array();
|
|
|
|
|
|
|
|
if ($value === NULL) {
|
|
|
|
if ($key !== NULL) {
|
|
|
|
throw new InvalidArgumentException("Either none or both columns must be specified.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// autodetect
|
2008-10-10 17:39:33 +00:00
|
|
|
$tmp = array_keys((array) $row);
|
2008-07-17 03:51:29 +00:00
|
|
|
$key = $tmp[0];
|
2008-10-01 16:04:16 +00:00
|
|
|
if (count($row) < 2) { // indexed-array
|
|
|
|
do {
|
|
|
|
$data[] = $row[$key];
|
2008-10-10 17:39:33 +00:00
|
|
|
} while ($row = $this->fetch());
|
2008-10-01 16:04:16 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
$value = $tmp[1];
|
|
|
|
|
|
|
|
} else {
|
2008-10-21 13:42:53 +00:00
|
|
|
if (!isset($row[$value])) {
|
2008-07-17 03:51:29 +00:00
|
|
|
throw new InvalidArgumentException("Unknown value column '$value'.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($key === NULL) { // indexed-array
|
|
|
|
do {
|
|
|
|
$data[] = $row[$value];
|
2008-10-10 17:39:33 +00:00
|
|
|
} while ($row = $this->fetch());
|
2008-07-17 03:51:29 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2008-10-21 13:42:53 +00:00
|
|
|
if (!isset($row[$key])) {
|
2008-07-17 03:51:29 +00:00
|
|
|
throw new InvalidArgumentException("Unknown key column '$key'.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
$data[ $row[$key] ] = $row[$value];
|
2008-10-10 17:39:33 +00:00
|
|
|
} while ($row = $this->fetch());
|
2008-07-17 03:51:29 +00:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define column type.
|
2009-02-05 23:13:29 +00:00
|
|
|
* @param string column
|
2009-03-16 06:48:27 +00:00
|
|
|
* @param string type (use constant Dibi::*)
|
2009-02-05 23:13:29 +00:00
|
|
|
* @param string optional format
|
2008-07-17 03:51:29 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
final public function setType($col, $type, $format = NULL)
|
|
|
|
{
|
2008-07-22 13:12:53 +00:00
|
|
|
$this->xlat[$col] = array('type' => $type, 'format' => $format);
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-10-28 14:37:40 +00:00
|
|
|
/**
|
|
|
|
* Autodetect column types.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
final public function detectTypes()
|
|
|
|
{
|
|
|
|
foreach ($this->getMeta() as $info) {
|
|
|
|
$this->xlat[$info['name']] = array('type' => $info['type'], 'format' => NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
/**
|
2008-10-28 15:24:47 +00:00
|
|
|
* Define multiple columns types.
|
2009-02-05 23:13:29 +00:00
|
|
|
* @param array
|
2008-07-17 03:51:29 +00:00
|
|
|
* @return void
|
2008-10-28 15:24:47 +00:00
|
|
|
* @internal
|
2008-07-17 03:51:29 +00:00
|
|
|
*/
|
|
|
|
final public function setTypes(array $types)
|
|
|
|
{
|
|
|
|
$this->xlat = $types;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns column type.
|
|
|
|
* @return array ($type, $format)
|
|
|
|
*/
|
|
|
|
final public function getType($col)
|
|
|
|
{
|
|
|
|
return isset($this->xlat[$col]) ? $this->xlat[$col] : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-02-05 23:13:29 +00:00
|
|
|
* Converts value to specified type and format.
|
2008-07-17 03:51:29 +00:00
|
|
|
* @return array ($type, $format)
|
|
|
|
*/
|
|
|
|
final public function convert($value, $type, $format = NULL)
|
|
|
|
{
|
|
|
|
if ($value === NULL || $value === FALSE) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($type) {
|
2009-03-16 06:48:27 +00:00
|
|
|
case dibi::TEXT:
|
2008-07-17 03:51:29 +00:00
|
|
|
return (string) $value;
|
|
|
|
|
2009-03-16 06:48:27 +00:00
|
|
|
case dibi::BINARY:
|
2008-07-17 03:51:29 +00:00
|
|
|
return $this->getDriver()->unescape($value, $type);
|
|
|
|
|
2009-03-16 06:48:27 +00:00
|
|
|
case dibi::INTEGER:
|
2008-07-17 03:51:29 +00:00
|
|
|
return (int) $value;
|
|
|
|
|
2009-03-16 06:48:27 +00:00
|
|
|
case dibi::FLOAT:
|
2008-07-17 03:51:29 +00:00
|
|
|
return (float) $value;
|
|
|
|
|
2009-03-16 06:48:27 +00:00
|
|
|
case dibi::DATE:
|
|
|
|
case dibi::DATETIME:
|
2008-11-25 20:33:52 +00:00
|
|
|
$value = is_numeric($value) ? (int) $value : strtotime($value);
|
2008-07-17 03:51:29 +00:00
|
|
|
return $format === NULL ? $value : date($format, $value);
|
|
|
|
|
2009-03-16 06:48:27 +00:00
|
|
|
case dibi::BOOL:
|
2008-07-17 03:51:29 +00:00
|
|
|
return ((bool) $value) && $value !== 'f' && $value !== 'F';
|
|
|
|
|
|
|
|
default:
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-10-28 14:37:40 +00:00
|
|
|
* Gets an array of meta informations about columns.
|
2008-10-02 17:13:43 +00:00
|
|
|
* @return array of DibiColumnInfo
|
2008-07-17 03:51:29 +00:00
|
|
|
*/
|
2008-10-02 17:13:43 +00:00
|
|
|
final public function getColumns()
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
|
|
|
$cols = array();
|
2008-10-28 14:37:40 +00:00
|
|
|
foreach ($this->getMeta() as $info) {
|
2009-03-02 02:22:36 +00:00
|
|
|
$cols[] = new DibiColumnInfo($this->getDriver(), $info);
|
2008-10-02 17:13:43 +00:00
|
|
|
}
|
|
|
|
return $cols;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool
|
|
|
|
* @return array of string
|
|
|
|
*/
|
|
|
|
public function getColumnNames($withTables = FALSE)
|
|
|
|
{
|
|
|
|
$cols = array();
|
2008-10-28 14:37:40 +00:00
|
|
|
foreach ($this->getMeta() as $info) {
|
2008-10-28 01:03:50 +00:00
|
|
|
$cols[] = $info[$withTables ? 'fullname' : 'name'];
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
return $cols;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays complete result-set as HTML table for debug purposes.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
final public function dump()
|
|
|
|
{
|
2008-09-03 22:47:49 +00:00
|
|
|
$i = 0;
|
|
|
|
$this->seek(0);
|
2008-10-10 17:39:33 +00:00
|
|
|
while ($row = $this->fetch()) {
|
2008-09-03 22:47:49 +00:00
|
|
|
if ($i === 0) {
|
2008-07-17 03:51:29 +00:00
|
|
|
echo "\n<table class=\"dump\">\n<thead>\n\t<tr>\n\t\t<th>#row</th>\n";
|
|
|
|
|
|
|
|
foreach ($row as $col => $foo) {
|
|
|
|
echo "\t\t<th>" . htmlSpecialChars($col) . "</th>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "\t</tr>\n</thead>\n<tbody>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "\t<tr>\n\t\t<th>", $i, "</th>\n";
|
|
|
|
foreach ($row as $col) {
|
|
|
|
//if (is_object($col)) $col = $col->__toString();
|
|
|
|
echo "\t\t<td>", htmlSpecialChars($col), "</td>\n";
|
|
|
|
}
|
|
|
|
echo "\t</tr>\n";
|
2008-09-03 22:47:49 +00:00
|
|
|
$i++;
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
2008-09-03 22:47:49 +00:00
|
|
|
if ($i === 0) {
|
2008-07-17 03:51:29 +00:00
|
|
|
echo '<p><em>empty result set</em></p>';
|
|
|
|
} else {
|
|
|
|
echo "</tbody>\n</table>\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Required by the IteratorAggregate interface.
|
|
|
|
* @param int offset
|
|
|
|
* @param int limit
|
2009-01-07 13:48:01 +00:00
|
|
|
* @return DibiResultIterator
|
2008-07-17 03:51:29 +00:00
|
|
|
*/
|
|
|
|
final public function getIterator($offset = NULL, $limit = NULL)
|
|
|
|
{
|
2009-01-07 13:48:01 +00:00
|
|
|
return new DibiResultIterator($this, $offset, $limit);
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Required by the Countable interface.
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
final public function count()
|
|
|
|
{
|
2009-02-26 20:02:14 +00:00
|
|
|
return $this->getRowCount();
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Safe access to property $driver.
|
|
|
|
* @return IDibiDriver
|
|
|
|
* @throws InvalidStateException
|
|
|
|
*/
|
|
|
|
private function getDriver()
|
|
|
|
{
|
|
|
|
if ($this->driver === NULL) {
|
2009-03-02 02:22:36 +00:00
|
|
|
throw new InvalidStateException('Result-set was released from memory.');
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->driver;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-28 14:37:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Meta lazy initialization.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getMeta()
|
|
|
|
{
|
|
|
|
if ($this->meta === NULL) {
|
|
|
|
$this->meta = $this->getDriver()->getColumnsMeta();
|
|
|
|
foreach ($this->meta as & $row) {
|
|
|
|
$row['type'] = DibiColumnInfo::detectType($row['nativetype']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->meta;
|
|
|
|
}
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|