1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-22 13:43:12 +02:00

* added DibiTable

* new connection options: "result:objects" & "result:withtables"
* renamed DibiDriverInterface -> IDibiDriver, DibiVariableInterface -> IDibiVariable
This commit is contained in:
David Grudl
2008-01-15 03:43:03 +00:00
parent da0a239d6d
commit 5c045e58dc
21 changed files with 461 additions and 58 deletions

View File

@@ -26,6 +26,7 @@
* $result = dibi::query('SELECT * FROM [table]');
*
* $row = $result->fetch();
* $obj = $result->fetch(TRUE);
* $value = $result->fetchSingle();
* $table = $result->fetchAll();
* $pairs = $result->fetchPairs();
@@ -43,7 +44,7 @@
class DibiResult extends NObject implements IteratorAggregate, Countable
{
/**
* DibiDriverInterface
* IDibiDriver
* @var array
*/
private $driver;
@@ -60,7 +61,6 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
*/
private $metaCache;
/**
* Already fetched? Used for allowance for first seek(0)
* @var bool
@@ -73,6 +73,12 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
*/
private $withTables = FALSE;
/**
* Fetch as object?
* @var bool
*/
public $asObjects = FALSE;
private static $types = array(
@@ -85,13 +91,19 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
public function __construct($driver)
/**
* @param IDibiDriver
* @param array
*/
public function __construct($driver, $config)
{
$this->driver = $driver;
$this->setWithTables(!empty($config['result:withtables']));
$this->asObjects = !empty($config['result:objects']);
}
/**
* Automatically frees the resources allocated for this result set
*
@@ -207,9 +219,10 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
* Fetches the row at current position, process optional type conversion
* and moves the internal cursor to the next position
*
* @param bool fetch as object? Overrides $this->asObjects
* @return array|FALSE array on success, FALSE if no next record
*/
final public function fetch()
final public function fetch($asObject = NULL)
{
if ($this->withTables === FALSE) {
$row = $this->getDriver()->fetch(TRUE);
@@ -232,6 +245,10 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
}
}
if ($asObject || ($asObject === NULL && $this->asObjects)) {
$row = (object) $row;
}
return $row;
}
@@ -268,7 +285,7 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
final function fetchAll()
{
$this->seek(0);
$row = $this->fetch();
$row = $this->fetch(FALSE);
if (!$row) return array(); // empty resultset
$data = array();
@@ -276,10 +293,10 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
$key = key($row);
do {
$data[] = $row[$key];
} while ($row = $this->fetch());
} while ($row = $this->fetch(FALSE));
} else {
if ($this->asObjects) $row = (object) $row;
do {
$data[] = $row;
} while ($row = $this->fetch());
@@ -302,7 +319,7 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
final function fetchAssoc($assoc)
{
$this->seek(0);
$row = $this->fetch();
$row = $this->fetch(FALSE);
if (!$row) return array(); // empty resultset
$data = NULL;
@@ -366,7 +383,7 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
if ($leaf === '=') $x = $row; else $x = (object) $row;
}
} while ($row = $this->fetch());
} while ($row = $this->fetch(FALSE));
unset($x);
return $data;
@@ -385,7 +402,7 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
final function fetchPairs($key = NULL, $value = NULL)
{
$this->seek(0);
$row = $this->fetch();
$row = $this->fetch(FALSE);
if (!$row) return array(); // empty resultset
$data = array();
@@ -412,7 +429,7 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
if ($key === NULL) { // indexed-array
do {
$data[] = $row[$value];
} while ($row = $this->fetch());
} while ($row = $this->fetch(FALSE));
return $data;
}
@@ -423,7 +440,7 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
do {
$data[ $row[$key] ] = $row[$value];
} while ($row = $this->fetch());
} while ($row = $this->fetch(FALSE));
return $data;
}
@@ -558,7 +575,7 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
/**
* Safe access to property $driver
*
* @return DibiDriverInterface
* @return IDibiDriver
* @throws DibiException
*/
private function getDriver()