3 * TDbTableInfo class file.
5 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9 * @package System.Data.Common
13 * TDbTableInfo class describes the meta data of a database table.
15 * @author Wei Zhuo <weizho[at]gmail[dot]com>
16 * @package System.Data.Common
19 class TDbTableInfo extends TComponent
21 private $_info=array();
23 private $_primaryKeys;
24 private $_foreignKeys;
34 private $_names = null;
37 * Sets the database table meta data information.
38 * @param array table column information.
40 public function __construct($tableInfo=array(),$primary=array(),$foreign=array())
42 $this->_info=$tableInfo;
43 $this->_primaryKeys=$primary;
44 $this->_foreignKeys=$foreign;
45 $this->_columns=new TMap;
49 * @param TDbConnection database connection.
50 * @return TDbCommandBuilder new command builder
52 public function createCommandBuilder($connection)
54 Prado::using('System.Data.Common.TDbCommandBuilder');
55 return new TDbCommandBuilder($connection,$this);
59 * @param string information array key name
60 * @param mixed default value if information array value is null
61 * @return mixed information array value.
63 protected function getInfo($name,$default=null)
65 return isset($this->_info[$name]) ? $this->_info[$name] : $default;
69 * @param string information array key name
70 * @param mixed new information array value.
72 protected function setInfo($name,$value)
74 $this->_info[$name]=$value;
78 * @return string name of the table this column belongs to.
80 public function getTableName()
82 return $this->getInfo('TableName');
86 * @return string full name of the table, database dependent.
88 public function getTableFullName()
90 return $this->getTableName();
94 * @return boolean whether the table is a view, default is false.
96 public function getIsView()
98 return $this->getInfo('IsView',false);
102 * @return TMap TDbTableColumn column meta data.
104 public function getColumns()
106 return $this->_columns;
110 * @param string column id
111 * @return TDbTableColumn column information.
113 public function getColumn($name)
115 if(($column = $this->_columns->itemAt($name))!==null)
117 throw new TDbException('dbtableinfo_invalid_column_name', $name, $this->getTableFullName());
121 * @param array list of column Id, empty to get all columns.
122 * @return array table column names (identifier quoted)
124 public function getColumnNames()
126 if($this->_names===null)
128 $this->_names=array();
129 foreach($this->getColumns() as $column)
130 $this->_names[] = $column->getColumnName();
132 return $this->_names;
136 * @return string[] names of primary key columns.
138 public function getPrimaryKeys()
140 return $this->_primaryKeys;
144 * @return array tuples of foreign table and column name.
146 public function getForeignKeys()
148 return $this->_foreignKeys;
152 * @return array lowercased column key names mapped to normal column ids.
154 public function getLowerCaseColumnNames()
156 if($this->_lowercase===null)
158 $this->_lowercase=array();
159 foreach($this->getColumns()->getKeys() as $key)
160 $this->_lowercase[strtolower($key)] = $key;
162 return $this->_lowercase;