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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
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;
|
|
|
|
|
2009-09-09 17:02:46 +02:00
|
|
|
/** @var DibiResultInfo */
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the result set resource.
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
final public function getResource()
|
|
|
|
{
|
|
|
|
return $this->getDriver()->getResultResource();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-09 17:01:30 +02:00
|
|
|
/**
|
|
|
|
* Frees the resources allocated for this result set.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
final public function free()
|
|
|
|
{
|
|
|
|
if ($this->driver !== NULL) {
|
|
|
|
$this->driver->free();
|
|
|
|
$this->driver = $this->meta = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Safe access to property $driver.
|
|
|
|
* @return IDibiDriver
|
|
|
|
* @throws InvalidStateException
|
|
|
|
*/
|
|
|
|
private function getDriver()
|
|
|
|
{
|
|
|
|
if ($this->driver === NULL) {
|
|
|
|
throw new InvalidStateException('Result-set was released from memory.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->driver;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/********************* rows ****************d*g**/
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +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
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
final public function seek($row)
|
|
|
|
{
|
|
|
|
return ($row !== 0 || $this->fetched) ? (bool) $this->getDriver()->seek($row) : TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-09-09 17:01:30 +02:00
|
|
|
* Required by the Countable interface.
|
2008-07-17 03:51:29 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2009-09-09 17:01:30 +02:00
|
|
|
final public function count()
|
2009-02-26 20:02:14 +00:00
|
|
|
{
|
|
|
|
return $this->getDriver()->getRowCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-09-09 17:01:30 +02:00
|
|
|
* Returns the number of rows in a result set.
|
2009-02-26 20:02:14 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2009-09-09 17:01:30 +02:00
|
|
|
final public function getRowCount()
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
2009-02-26 20:02:14 +00:00
|
|
|
return $this->getDriver()->getRowCount();
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-09-09 17:01:30 +02:00
|
|
|
* Returns the number of rows in a result set. Alias for getRowCount().
|
|
|
|
* @deprecated
|
2008-07-17 03:51:29 +00:00
|
|
|
*/
|
2009-09-09 17:01:30 +02:00
|
|
|
final public function rowCount()
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
2009-09-09 17:01:30 +02:00
|
|
|
return $this->getDriver()->getRowCount();
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-09-09 17:01:30 +02:00
|
|
|
* Required by the IteratorAggregate interface.
|
|
|
|
* @param int offset
|
|
|
|
* @param int limit
|
|
|
|
* @return DibiResultIterator
|
2008-07-17 03:51:29 +00:00
|
|
|
*/
|
2009-09-09 17:01:30 +02:00
|
|
|
final public function getIterator($offset = NULL, $limit = NULL)
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
2009-09-09 17:01:30 +02:00
|
|
|
return new DibiResultIterator($this, $offset, $limit);
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-09 17:01:30 +02:00
|
|
|
/********************* fetching rows ****************d*g**/
|
2008-07-17 03:51:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
2009-10-06 18:09:13 +02:00
|
|
|
* Examples:
|
|
|
|
* - associative descriptor: col1[]col2->col3
|
|
|
|
* builds a tree: $tree[$val1][$index][$val2]->col3[$val3] = {record}
|
|
|
|
* - associative descriptor: col1|col2->col3=col4
|
|
|
|
* builds a tree: $tree[$val1][$val2]->col3[$val3] = val4
|
2008-07-17 03:51:29 +00:00
|
|
|
* @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
|
|
|
{
|
2009-10-06 18:09:13 +02:00
|
|
|
if (strpos($assoc, ',') !== FALSE) {
|
|
|
|
return $this->oldFetchAssoc($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;
|
2009-10-06 18:09:13 +02:00
|
|
|
$assoc = preg_split('#(\[\]|->|=|\|)#', $assoc, NULL, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
|
2008-07-17 03:51:29 +00:00
|
|
|
|
|
|
|
// 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
|
2009-10-06 18:09:13 +02:00
|
|
|
if ($as !== '[]' && $as !== '=' && $as !== '->' && $as !== '|' && !isset($row[$as])) {
|
2008-07-17 03:51:29 +00:00
|
|
|
throw new InvalidArgumentException("Unknown column '$as' in associative descriptor.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-06 18:09:13 +02:00
|
|
|
if ($as === '->') { // must not be last
|
|
|
|
array_pop($assoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($assoc)) {
|
|
|
|
$assoc[] = '[]';
|
|
|
|
}
|
|
|
|
|
|
|
|
// make associative tree
|
|
|
|
do {
|
|
|
|
$x = & $data;
|
|
|
|
|
|
|
|
// iterative deepening
|
|
|
|
foreach ($assoc as $i => $as) {
|
|
|
|
if ($as === '[]') { // indexed-array node
|
|
|
|
$x = & $x[];
|
|
|
|
|
|
|
|
} elseif ($as === '=') { // "value" node
|
|
|
|
$x = $row->{$assoc[$i+1]};
|
|
|
|
break;
|
|
|
|
|
|
|
|
} elseif ($as === '->') { // "object" node
|
|
|
|
if ($x === NULL) {
|
|
|
|
$x = clone $row;
|
|
|
|
$x = & $x->{$assoc[$i+1]};
|
|
|
|
$x = NULL; // prepare child node
|
|
|
|
} else {
|
|
|
|
$x = & $x->{$assoc[$i+1]};
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ($as !== '|') { // associative-array node
|
|
|
|
$x = & $x[$row->$as];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($x === NULL) { // build leaf
|
|
|
|
$x = $row;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while ($row = $this->fetch());
|
|
|
|
|
|
|
|
unset($x);
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
private function oldFetchAssoc($assoc)
|
|
|
|
{
|
|
|
|
$this->seek(0);
|
|
|
|
$row = $this->fetch();
|
|
|
|
if (!$row) return array(); // empty result set
|
|
|
|
|
|
|
|
$data = NULL;
|
|
|
|
$assoc = explode(',', $assoc);
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
$x = & $data;
|
|
|
|
|
|
|
|
foreach ($assoc as $i => $as) {
|
|
|
|
if ($as === '#') { // indexed-array node
|
|
|
|
$x = & $x[];
|
|
|
|
|
|
|
|
} elseif ($as === '=') { // "record" node
|
|
|
|
if ($x === NULL) {
|
2009-10-06 16:51:27 +02:00
|
|
|
$x = (array) $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] ];
|
|
|
|
}
|
|
|
|
|
|
|
|
} 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
|
2009-10-06 16:51:27 +02:00
|
|
|
$x = & $x[$row->$as];
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($x === NULL) { // build leaf
|
2008-09-03 22:47:49 +00:00
|
|
|
if ($leaf === '=') {
|
2009-10-06 16:51:27 +02:00
|
|
|
$x = (array) $row;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-09 17:01:30 +02:00
|
|
|
/********************* meta info ****************d*g**/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Qualifiy each column name with the table name?
|
|
|
|
* @param bool
|
|
|
|
* @return DibiResult provides a fluent interface
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
final public function setWithTables($val)
|
|
|
|
{
|
|
|
|
if ($val) {
|
|
|
|
$cols = array();
|
2009-09-09 17:02:46 +02:00
|
|
|
foreach ($this->getInfo()->getColumns() as $col) {
|
|
|
|
$name = $col->getFullname();
|
2009-09-09 17:01:30 +02: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;
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Qualifiy each key with the table name?
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
final public function getWithTables()
|
|
|
|
{
|
|
|
|
return (bool) $this->withTables;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2009-09-18 03:38:10 +02:00
|
|
|
* @return DibiResult provides a fluent interface
|
2008-07-17 03:51:29 +00:00
|
|
|
*/
|
|
|
|
final public function setType($col, $type, $format = NULL)
|
|
|
|
{
|
2008-07-22 13:12:53 +00:00
|
|
|
$this->xlat[$col] = array('type' => $type, 'format' => $format);
|
2009-09-18 03:38:10 +02:00
|
|
|
return $this;
|
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()
|
|
|
|
{
|
2009-09-09 17:02:46 +02:00
|
|
|
foreach ($this->getInfo()->getColumns() as $col) {
|
|
|
|
$this->xlat[$col->getName()] = array('type' => $col->getType(), 'format' => NULL);
|
2008-10-28 14:37:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2009-09-18 03:38:10 +02:00
|
|
|
* @return DibiResult provides a fluent interface
|
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;
|
2009-09-18 03:38:10 +02:00
|
|
|
return $this;
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2009-06-03 13:42:02 +00:00
|
|
|
* @param mixed value
|
|
|
|
* @param int type
|
|
|
|
* @param string format
|
|
|
|
* @return mixed
|
2008-07-17 03:51:29 +00:00
|
|
|
*/
|
|
|
|
final public function convert($value, $type, $format = NULL)
|
|
|
|
{
|
|
|
|
if ($value === NULL || $value === FALSE) {
|
2009-08-20 23:42:50 +02:00
|
|
|
return NULL;
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2009-08-20 23:42:50 +02:00
|
|
|
if ($value == NULL) { // intentionally ==
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
} elseif ($format === NULL) { // return timestamp (default)
|
|
|
|
return is_numeric($value) ? (int) $value : strtotime($value);
|
|
|
|
|
|
|
|
} elseif ($format === TRUE) { // return DateTime object
|
|
|
|
return new DateTime(is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value);
|
|
|
|
|
|
|
|
} elseif (is_numeric($value)) { // single timestamp
|
|
|
|
return date($format, $value);
|
|
|
|
|
|
|
|
} elseif (class_exists('DateTime', FALSE)) { // since PHP 5.2
|
|
|
|
$value = new DateTime($value);
|
|
|
|
return $value ? $value->format($format) : NULL;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return date($format, strtotime($value));
|
|
|
|
}
|
2008-07-17 03:51:29 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-09 17:01:30 +02:00
|
|
|
/**
|
2009-09-09 17:02:46 +02:00
|
|
|
* Returns a meta information about the current result set.
|
|
|
|
* @return DibiResultInfo
|
2009-09-09 17:01:30 +02:00
|
|
|
*/
|
2009-09-09 17:02:46 +02:00
|
|
|
public function getInfo()
|
2009-09-09 17:01:30 +02:00
|
|
|
{
|
|
|
|
if ($this->meta === NULL) {
|
2009-09-09 17:02:46 +02:00
|
|
|
$this->meta = new DibiResultInfo($this->getDriver());
|
2009-09-09 17:01:30 +02:00
|
|
|
}
|
|
|
|
return $this->meta;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
/**
|
2009-09-09 17:02:46 +02:00
|
|
|
* @deprecated
|
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
|
|
|
{
|
2009-09-09 17:02:46 +02:00
|
|
|
return $this->getInfo()->getColumns();
|
2008-10-02 17:13:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-09-09 17:02:46 +02:00
|
|
|
* @deprecated
|
2008-10-02 17:13:43 +00:00
|
|
|
*/
|
2009-09-09 17:02:46 +02:00
|
|
|
public function getColumnNames($fullNames = FALSE)
|
2008-10-02 17:13:43 +00:00
|
|
|
{
|
2009-09-09 17:02:46 +02:00
|
|
|
return $this->getInfo()->getColumnNames($fullNames);
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-09 17:01:30 +02:00
|
|
|
/********************* misc tools ****************d*g**/
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
/**
|
|
|
|
* 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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|