Source for file DibiDatabaseInfo.php
Documentation is available at DibiDatabaseInfo.php
4: * dibi - tiny'n'smart database abstraction layer
5: * ----------------------------------------------
7: * @copyright Copyright (c) 2005, 2010 David Grudl
8: * @license http://dibiphp.com/license dibi license
9: * @link http://dibiphp.com
16: * Reflection metadata class for a database.
18: * @copyright Copyright (c) 2005, 2010 David Grudl
21: * @property-read string $name
22: * @property-read array $tables
23: * @property-read array $tableNames
27: /** @var IDibiReflector */
40: $this->reflector =
$reflector;
57: * @return array of DibiTableInfo
68: * @return array of string
74: foreach ($this->tables as $table) {
75: $res[] =
$table->getName();
84: * @return DibiTableInfo
91: if (isset($this->tables[$l])) {
92: return $this->tables[$l];
119: if ($this->tables ===
NULL) {
120: $this->tables =
array();
133: * Reflection metadata class for a database table.
135: * @copyright Copyright (c) 2005, 2010 David Grudl
138: * @property-read string $name
139: * @property-read bool $view
140: * @property-read array $columns
141: * @property-read array $columnNames
142: * @property-read array $foreignKeys
143: * @property-read array $indexes
144: * @property-read DibiIndexInfo $primaryKey
148: /** @var IDibiReflector */
161: private $foreignKeys;
166: /** @var DibiIndexInfo */
167: private $primaryKey;
173: $this->reflector =
$reflector;
174: $this->name =
$info['name'];
175: $this->view =
!empty($info['view']);
201: * @return array of DibiColumnInfo
212: * @return array of string
218: foreach ($this->columns as $column) {
228: * @return DibiColumnInfo
235: if (isset($this->columns[$l])) {
236: return $this->columns[$l];
259: * @return array of DibiForeignKeyInfo
264: return $this->foreignKeys;
270: * @return array of DibiIndexInfo
275: return $this->indexes;
281: * @return DibiIndexInfo
286: return $this->primaryKey;
296: if ($this->columns ===
NULL) {
297: $this->columns =
array();
311: if ($this->indexes ===
NULL) {
313: $this->indexes =
array();
315: foreach ($info['columns'] as $key =>
$name) {
319: if (!empty($info['primary'])) {
342: * Reflection metadata class for a result set.
344: * @copyright Copyright (c) 2005, 2010 David Grudl
347: * @property-read array $columns
348: * @property-read array $columnNames
352: /** @var IDibiResultDriver */
365: $this->driver =
$driver;
371: * @return array of DibiColumnInfo
383: * @return array of string
389: foreach ($this->columns as $column) {
390: $res[] =
$fullNames ?
$column->getFullName() :
$column->getName();
399: * @return DibiColumnInfo
406: if (isset($this->names[$l])) {
407: return $this->names[$l];
434: if ($this->columns ===
NULL) {
435: $this->columns =
array();
438: $this->columns[] =
$this->names[$info['name']] =
new DibiColumnInfo($reflector, $info);
449: * Reflection metadata class for a table or result set column.
451: * @copyright Copyright (c) 2005, 2010 David Grudl
454: * @property-read string $name
455: * @property-read string $fullName
456: * @property-read DibiTableInfo $table
457: * @property-read string $type
458: * @property-read mixed $nativeType
459: * @property-read int $size
460: * @property-read bool $unsigned
461: * @property-read bool $nullable
462: * @property-read bool $autoIncrement
463: * @property-read mixed $default
468: private static $types;
470: /** @var IDibiReflector|NULLwhen created by DibiResultInfo */
473: /** @var array (name, nativetype, [table], [fullname], [size], [nullable], [default], [autoincrement], [vendor]) */
478: public function __construct(IDibiReflector $reflector =
NULL, array $info)
480: $this->reflector =
$reflector;
481: $this->info =
$info;
489: public function getName()
491: return $this->info['name'];
501: return isset($this->info['fullname']) ?
$this->info['fullname'] :
NULL;
511: return !empty($this->info['table']);
517: * @return DibiTableInfo
521: if (empty($this->info['table']) ||
!$this->reflector) {
524: return new DibiTableInfo($this->reflector, array('name' =>
$this->info['table']));
534: return isset($this->info['table']) ?
$this->info['table'] :
NULL;
544: if (self::$types ===
NULL) {
545: self::$types =
new DibiLazyStorage(array(__CLASS__
, 'detectType'));
547: return self::$types->{$this->info['nativetype']};
555: public function getNativeType()
557: return $this->info['nativetype'];
567: return isset($this->info['size']) ? (int)
$this->info['size'] :
NULL;
577: return isset($this->info['unsigned']) ? (bool)
$this->info['unsigned'] :
NULL;
587: return isset($this->info['nullable']) ? (bool)
$this->info['nullable'] :
NULL;
597: return isset($this->info['autoincrement']) ? (bool)
$this->info['autoincrement'] :
NULL;
607: return isset($this->info['default']) ?
$this->info['default'] :
NULL;
618: return isset($this->info['vendor'][$key]) ?
$this->info['vendor'][$key] :
NULL;
624: * Heuristic type detection.
629: public static function detectType($type)
631: static $patterns =
array(
632: 'BYTEA|BLOB|BIN' =>
dibi::BINARY,
633: 'TEXT|CHAR|BIGINT|LONGLONG' =>
dibi::TEXT,
634: 'BYTE|COUNTER|SERIAL|INT|LONG' =>
dibi::INTEGER,
635: 'CURRENCY|REAL|MONEY|FLOAT|DOUBLE|DECIMAL|NUMERIC|NUMBER' =>
dibi::FLOAT,
636: '^TIME$' =>
dibi::TIME,
637: 'TIME' =>
dibi::DATETIME, // DATETIME, TIMESTAMP
638: 'YEAR|DATE' =>
dibi::DATE,
639: 'BOOL|BIT' =>
dibi::BOOL,
642: foreach ($patterns as $s =>
$val) {
643: if (preg_match("#
$s#i
", $type)) {
656: * Reflection metadata class for a foreign key.
658: * @copyright Copyright (c) 2005, 2010 David Grudl
662: * @property-read string $name
663: * @property-read array $references
670: /** @var array of array(local, foreign, onDelete, onUpdate) */
671: private $references;
677: $this->name =
$name;
678: $this->references =
$references;
686: public function getName()
698: return $this->references;
707: * Reflection metadata class for a index or primary key.
709: * @copyright Copyright (c) 2005, 2010 David Grudl
712: * @property-read string $name
713: * @property-read array $columns
714: * @property-read bool $unique
715: * @property-read bool $primary
719: /** @var array (name, columns, [unique], [primary]) */
725: $this->info =
$info;
733: public function getName()
735: return $this->info['name'];
745: return $this->info['columns'];
755: return !empty($this->info['unique']);
765: return !empty($this->info['primary']);