1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-05 13:47:33 +02:00

DibiResult: small refactoring

This commit is contained in:
David Grudl
2010-02-24 02:43:15 +01:00
parent 576397319a
commit a2323271d4

View File

@@ -37,19 +37,19 @@ class DibiResult extends DibiObject implements IDataSource
private $driver; private $driver;
/** @var array Translate table */ /** @var array Translate table */
private $xlat; private $types;
/** @var DibiResultInfo */ /** @var DibiResultInfo */
private $meta; private $meta;
/** @var array user keys */
private $keys;
/** @var bool Already fetched? Used for allowance for first seek(0) */ /** @var bool Already fetched? Used for allowance for first seek(0) */
private $fetched = FALSE; private $fetched = FALSE;
/** @var array|FALSE Qualifiy each column name with the table name? */
private $withTables = FALSE;
/** @var string returned object class */ /** @var string returned object class */
private $class = 'DibiRow'; private $rowClass = 'DibiRow';
@@ -183,7 +183,7 @@ class DibiResult extends DibiObject implements IDataSource
*/ */
public function setRowClass($class) public function setRowClass($class)
{ {
$this->class = $class; $this->rowClass = $class;
return $this; return $this;
} }
@@ -195,7 +195,7 @@ class DibiResult extends DibiObject implements IDataSource
*/ */
public function getRowClass() public function getRowClass()
{ {
return $this->class; return $this->rowClass;
} }
@@ -207,28 +207,28 @@ class DibiResult extends DibiObject implements IDataSource
*/ */
final public function fetch() final public function fetch()
{ {
if ($this->withTables === FALSE) { if ($this->keys === NULL) {
$row = $this->getDriver()->fetch(TRUE); $row = $this->getDriver()->fetch(TRUE);
if (!is_array($row)) return FALSE; if (!is_array($row)) return FALSE;
} else { } else {
$row = $this->getDriver()->fetch(FALSE); $row = $this->getDriver()->fetch(FALSE);
if (!is_array($row)) return FALSE; if (!is_array($row)) return FALSE;
$row = array_combine($this->withTables, $row); $row = array_combine($this->keys, $row);
} }
$this->fetched = TRUE; $this->fetched = TRUE;
// types-converting? // types-converting?
if ($this->xlat !== NULL) { if ($this->types !== NULL) {
foreach ($this->xlat as $col => $type) { foreach ($this->types as $col => $type) {
if (isset($row[$col])) { if (isset($row[$col])) {
$row[$col] = $this->convert($row[$col], $type['type'], $type['format']); $row[$col] = $this->convert($row[$col], $type['type'], $type['format']);
} }
} }
} }
return new $this->class($row); return new $this->rowClass($row);
} }
@@ -246,8 +246,8 @@ class DibiResult extends DibiObject implements IDataSource
// types-converting? // types-converting?
$key = key($row); $key = key($row);
if (isset($this->xlat[$key])) { if (isset($this->types[$key])) {
$type = $this->xlat[$key]; $type = $this->types[$key];
return $this->convert($value, $type['type'], $type['format']); return $this->convert($value, $type['type'], $type['format']);
} }
@@ -514,10 +514,10 @@ class DibiResult extends DibiObject implements IDataSource
} }
$cols[$name] = TRUE; $cols[$name] = TRUE;
} }
$this->withTables = array_keys($cols); $this->keys = array_keys($cols);
} else { } else {
$this->withTables = FALSE; $this->keys = NULL;
} }
return $this; return $this;
} }
@@ -530,7 +530,7 @@ class DibiResult extends DibiObject implements IDataSource
*/ */
final public function getWithTables() final public function getWithTables()
{ {
return (bool) $this->withTables; return (bool) $this->keys;
} }
@@ -544,7 +544,7 @@ class DibiResult extends DibiObject implements IDataSource
*/ */
final public function setType($col, $type, $format = NULL) final public function setType($col, $type, $format = NULL)
{ {
$this->xlat[$col] = array('type' => $type, 'format' => $format); $this->types[$col] = array('type' => $type, 'format' => $format);
return $this; return $this;
} }
@@ -557,7 +557,7 @@ class DibiResult extends DibiObject implements IDataSource
final public function detectTypes() final public function detectTypes()
{ {
foreach ($this->getInfo()->getColumns() as $col) { foreach ($this->getInfo()->getColumns() as $col) {
$this->xlat[$col->getName()] = array('type' => $col->getType(), 'format' => NULL); $this->types[$col->getName()] = array('type' => $col->getType(), 'format' => NULL);
} }
} }
@@ -571,7 +571,7 @@ class DibiResult extends DibiObject implements IDataSource
*/ */
final public function setTypes(array $types) final public function setTypes(array $types)
{ {
$this->xlat = $types; $this->types = $types;
return $this; return $this;
} }
@@ -583,7 +583,7 @@ class DibiResult extends DibiObject implements IDataSource
*/ */
final public function getType($col) final public function getType($col)
{ {
return isset($this->xlat[$col]) ? $this->xlat[$col] : NULL; return isset($this->types[$col]) ? $this->types[$col] : NULL;
} }