summaryrefslogtreecommitdiff
path: root/framework/Data/Common/Oracle/TOracleTableInfo.php
blob: f226562439deeb3d17d39b1eb05962fcc6e51b98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php

/**
 * TOracleTableInfo class file.
 *
 * @author Marcos Nobre <marconobre[at]gmail[dot]com>
 * @link https://github.com/pradosoft/prado
 * @copyright Copyright &copy; 2005-2015 The PRADO Group
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
 * @package System.Data.Common
 */

/**
 * TDbTableInfo class describes the meta data of a database table.
 *
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
 * @package System.Data.Common
 * @since 3.1
 */
class TOracleTableInfo extends TComponent
{
	private $_info=array();

	private $_primaryKeys;
	private $_foreignKeys;

	private $_columns;

	private $_lowercase;

	/**
	 * Sets the database table meta data information.
	 * @param array table column information.
	 */
	public function __construct($tableInfo=array(),$primary=array(),$foreign=array())
	{
		$this->_info=$tableInfo;
		$this->_primaryKeys=$primary;
		$this->_foreignKeys=$foreign;
		$this->_columns=new TMap;
	}

	/**
	 * @param TDbConnection database connection.
	 * @return TDbCommandBuilder new command builder
	 */
	public function createCommandBuilder($connection)
	{
		Prado::using('System.Data.Common.Oracle.TOracleCommandBuilder');
		return new TOracleCommandBuilder($connection,$this);
	}

	/**
	 * @param string information array key name
	 * @param mixed default value if information array value is null
	 * @return mixed information array value.
	 */
	public function getInfo($name,$default=null)
	{
		return isset($this->_info[$name]) ? $this->_info[$name] : $default;
	}

	/**
	 * @param string information array key name
	 * @param mixed new information array value.
	 */
	protected function setInfo($name,$value)
	{
		$this->_info[$name]=$value;
	}

	/**
	 * @return string name of the table this column belongs to.
	 */
	public function getTableName()
	{
		return $this->getInfo('TableName');
	}

	/**
	 * @return string full name of the table, database dependent.
	 */
	public function getTableFullName()
	{
		return $this->_info['SchemaName'].'.'.$this->getTableName();
	}

	/**
	 * @return boolean whether the table is a view, default is false.
	 */
	public function getIsView()
	{
		return $this->getInfo('IsView',false);
	}

	/**
	 * @return TMap TDbTableColumn column meta data.
	 */
	public function getColumns()
	{
		return $this->_columns;
	}

	/**
	 * @param string column id
	 * @return TDbTableColumn column information.
	 */
	public function getColumn($name)
	{
		if(($column = $this->_columns->itemAt($name))!==null)
			return $column;
		throw new TDbException('dbtableinfo_invalid_column_name', $name, $this->getTableFullName());
	}

	/**
	 * @param array list of column Id, empty to get all columns.
	 * @return array table column names (identifier quoted)
	 */
	public function getColumnNames()
	{
		foreach($this->getColumns() as $column)
			$names[] = $column->getColumnName();
		return $names;
	}

	/**
	 * @return string[] names of primary key columns.
	 */
	public function getPrimaryKeys()
	{
		return $this->_primaryKeys;
	}

	/**
	 * @return array tuples of foreign table and column name.
	 */
	public function getForeignKeys()
	{
		return $this->_foreignKeys;
	}

	/**
	 * @return array lowercased column key names mapped to normal column ids.
	 */
	public function getLowerCaseColumnNames()
	{
		if($this->_lowercase===null)
		{
			$this->_lowercase=array();
			foreach($this->getColumns()->getKeys() as $key)
				$this->_lowercase[strtolower($key)] = $key;
		}
		return $this->_lowercase;
	}
}