]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Data/Common/TDbTableInfo.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / framework / Data / Common / TDbTableInfo.php
1 <?php
2 /**
3  * TDbTableInfo class file.
4  *
5  * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6  * @link https://github.com/pradosoft/prado
7  * @copyright Copyright &copy; 2005-2016 The PRADO Group
8  * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9  * @package System.Data.Common
10  */
11
12 /**
13  * TDbTableInfo class describes the meta data of a database table.
14  *
15  * @author Wei Zhuo <weizho[at]gmail[dot]com>
16  * @package System.Data.Common
17  * @since 3.1
18  */
19 class TDbTableInfo extends TComponent
20 {
21         private $_info=array();
22
23         private $_primaryKeys;
24         private $_foreignKeys;
25
26         private $_columns;
27
28         private $_lowercase;
29
30         /**
31          * @var null|array
32          * @since 3.1.7
33          */
34         private $_names = null;
35
36         /**
37          * Sets the database table meta data information.
38          * @param array table column information.
39          */
40         public function __construct($tableInfo=array(),$primary=array(),$foreign=array())
41         {
42                 $this->_info=$tableInfo;
43                 $this->_primaryKeys=$primary;
44                 $this->_foreignKeys=$foreign;
45                 $this->_columns=new TMap;
46         }
47
48         /**
49          * @param TDbConnection database connection.
50          * @return TDbCommandBuilder new command builder
51          */
52         public function createCommandBuilder($connection)
53         {
54                 Prado::using('System.Data.Common.TDbCommandBuilder');
55                 return new TDbCommandBuilder($connection,$this);
56         }
57
58         /**
59          * @param string information array key name
60          * @param mixed default value if information array value is null
61          * @return mixed information array value.
62          */
63         protected function getInfo($name,$default=null)
64         {
65                 return isset($this->_info[$name]) ? $this->_info[$name] : $default;
66         }
67
68         /**
69          * @param string information array key name
70          * @param mixed new information array value.
71          */
72         protected function setInfo($name,$value)
73         {
74                 $this->_info[$name]=$value;
75         }
76
77         /**
78          * @return string name of the table this column belongs to.
79          */
80         public function getTableName()
81         {
82                 return $this->getInfo('TableName');
83         }
84
85         /**
86          * @return string full name of the table, database dependent.
87          */
88         public function getTableFullName()
89         {
90                 return $this->getTableName();
91         }
92
93         /**
94          * @return boolean whether the table is a view, default is false.
95          */
96         public function getIsView()
97         {
98                 return $this->getInfo('IsView',false);
99         }
100
101         /**
102          * @return TMap TDbTableColumn column meta data.
103          */
104         public function getColumns()
105         {
106                 return $this->_columns;
107         }
108
109         /**
110          * @param string column id
111          * @return TDbTableColumn column information.
112          */
113         public function getColumn($name)
114         {
115                 if(($column = $this->_columns->itemAt($name))!==null)
116                         return $column;
117                 throw new TDbException('dbtableinfo_invalid_column_name', $name, $this->getTableFullName());
118         }
119
120         /**
121          * @param array list of column Id, empty to get all columns.
122          * @return array table column names (identifier quoted)
123          */
124         public function getColumnNames()
125         {
126                 if($this->_names===null)
127                 {
128                         $this->_names=array();
129                         foreach($this->getColumns() as $column)
130                                 $this->_names[] = $column->getColumnName();
131                 }
132                 return $this->_names;
133         }
134
135         /**
136          * @return string[] names of primary key columns.
137          */
138         public function getPrimaryKeys()
139         {
140                 return $this->_primaryKeys;
141         }
142
143         /**
144          * @return array tuples of foreign table and column name.
145          */
146         public function getForeignKeys()
147         {
148                 return $this->_foreignKeys;
149         }
150
151         /**
152          * @return array lowercased column key names mapped to normal column ids.
153          */
154         public function getLowerCaseColumnNames()
155         {
156                 if($this->_lowercase===null)
157                 {
158                         $this->_lowercase=array();
159                         foreach($this->getColumns()->getKeys() as $key)
160                                 $this->_lowercase[strtolower($key)] = $key;
161                 }
162                 return $this->_lowercase;
163         }
164 }