Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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