diff --git a/dibi/libs/DibiDatabaseInfo.php b/dibi/libs/DibiDatabaseInfo.php index 472f3e0d..dac6fb8b 100644 --- a/dibi/libs/DibiDatabaseInfo.php +++ b/dibi/libs/DibiDatabaseInfo.php @@ -332,7 +332,109 @@ class DibiTableInfo extends DibiObject /** - * Reflection metadata class for a table column. + * Reflection metadata class for a result set. + * + * @author David Grudl + * @copyright Copyright (c) 2005, 2009 David Grudl + * @package dibi + */ +class DibiResultInfo extends DibiObject +{ + /** @var IDibiDriver */ + private $driver; + + /** @var array */ + private $columns; + + /** @var array */ + private $names; + + + + public function __construct(IDibiDriver $driver) + { + $this->driver = $driver; + } + + + + /** + * @return array of DibiColumnInfo + */ + public function getColumns() + { + $this->initColumns(); + return array_values($this->columns); + } + + + + /** + * @param bool + * @return array of string + */ + public function getColumnNames($fullNames = FALSE) + { + $this->initColumns(); + $res = array(); + foreach ($this->columns as $column) { + $res[] = $fullNames ? $column->getFullName() : $column->getName(); + } + return $res; + } + + + + /** + * @param string + * @return DibiColumnInfo + */ + public function getColumn($name) + { + $this->initColumns(); + $l = strtolower($name); + if (isset($this->names[$l])) { + return $this->names[$l]; + + } else { + throw new DibiException("Result set has no column '$name'."); + } + } + + + + /** + * @param string + * @return bool + */ + public function hasColumn($name) + { + $this->initColumns(); + return isset($this->names[strtolower($name)]); + } + + + + /** + * @return void + */ + protected function initColumns() + { + if ($this->columns === NULL) { + $this->columns = array(); + foreach ($this->driver->getColumnsMeta() as $info) { + $this->columns[] = $this->names[$info['name']] = new DibiColumnInfo($this->driver, $info); + } + } + } + +} + + + + +/** + * Reflection metadata class for a table or result set column. * * @author David Grudl * @copyright Copyright (c) 2005, 2009 David Grudl @@ -373,6 +475,16 @@ class DibiColumnInfo extends DibiObject + /** + * @return string + */ + public function getFullName() + { + return $this->info['fullname']; + } + + + /** * @return bool */ @@ -472,7 +584,7 @@ class DibiColumnInfo extends DibiObject * @param string * @return string */ - public static function detectType($type) + private static function detectType($type) { static $patterns = array( 'BYTEA|BLOB|BIN' => dibi::BINARY, diff --git a/dibi/libs/DibiResult.php b/dibi/libs/DibiResult.php index 5b762177..df10757f 100644 --- a/dibi/libs/DibiResult.php +++ b/dibi/libs/DibiResult.php @@ -47,7 +47,7 @@ class DibiResult extends DibiObject implements IDataSource /** @var array Translate table */ private $xlat; - /** @var array Cache for $driver->getColumnsMeta() */ + /** @var DibiResultInfo */ private $meta; /** @var bool Already fetched? Used for allowance for first seek(0) */ @@ -461,8 +461,8 @@ class DibiResult extends DibiObject implements IDataSource { if ($val) { $cols = array(); - foreach ($this->getMeta() as $info) { - $name = $info['fullname']; + foreach ($this->getInfo()->getColumns() as $col) { + $name = $col->getFullname(); if (isset($cols[$name])) { $fix = 1; while (isset($cols[$name . '#' . $fix])) $fix++; @@ -511,8 +511,8 @@ class DibiResult extends DibiObject implements IDataSource */ final public function detectTypes() { - foreach ($this->getMeta() as $info) { - $this->xlat[$info['name']] = array('type' => $info['type'], 'format' => NULL); + foreach ($this->getInfo()->getColumns() as $col) { + $this->xlat[$col->getName()] = array('type' => $col->getType(), 'format' => NULL); } } @@ -601,16 +601,13 @@ class DibiResult extends DibiObject implements IDataSource /** - * Meta lazy initialization. - * @return array + * Returns a meta information about the current result set. + * @return DibiResultInfo */ - private function getMeta() + public function getInfo() { if ($this->meta === NULL) { - $this->meta = $this->getDriver()->getColumnsMeta(); - foreach ($this->meta as & $row) { - $row['type'] = DibiColumnInfo::detectType($row['nativetype']); - } + $this->meta = new DibiResultInfo($this->getDriver()); } return $this->meta; } @@ -618,31 +615,21 @@ class DibiResult extends DibiObject implements IDataSource /** - * Gets an array of meta informations about columns. - * @return array of DibiColumnInfo + * @deprecated */ final public function getColumns() { - $cols = array(); - foreach ($this->getMeta() as $info) { - $cols[] = new DibiColumnInfo($this->getDriver(), $info); - } - return $cols; + return $this->getInfo()->getColumns(); } /** - * @param bool - * @return array of string + * @deprecated */ - public function getColumnNames($withTables = FALSE) + public function getColumnNames($fullNames = FALSE) { - $cols = array(); - foreach ($this->getMeta() as $info) { - $cols[] = $info[$withTables ? 'fullname' : 'name']; - } - return $cols; + return $this->getInfo()->getColumnNames($fullNames); }