summaryrefslogtreecommitdiff
path: root/framework/Data/Common/TDbTableColumn.php
blob: 71988f33ed32b146bc89d368a6c499e3bd4ad359 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
 * TDbTableColumn class file.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2011 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Id$
 * @package System.Data.Common
 */

/**
 * TDbTableColumn class describes the column meta data of the schema for a database table.
 *
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
 * @version $Id$
 * @package System.Data.Common
 * @since 3.1
 */
class TDbTableColumn extends TComponent
{
	const UNDEFINED_VALUE= INF; //use infinity for undefined value

	private $_info=array();

	/**
	 * Sets the table column meta data.
	 * @param array table column information.
	 */
	public function __construct($columnInfo)
	{
		$this->_info=$columnInfo;
	}

	/**
	 * @param string information array key name
	 * @param mixed default value if information array value is null
	 * @return mixed information array value.
	 */
	protected 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;
	}

	/**
	 * Returns the derived PHP primitive type from the db type. Default returns 'string'.
	 * @return string derived PHP primitive type from the column db type.
	 */
	public function getPHPType()
	{
		return 'string';
	}

	/**
	 * @param integer PDO bind param/value types, default returns string.
	 */
	public function getPdoType()
	{
		switch($this->getPHPType())
		{
			case 'boolean': return PDO::PARAM_BOOL;
			case 'integer': return PDO::PARAM_INT;
			case 'string' : return PDO::PARAM_STR;
		}
		return PDO::PARAM_STR;
	}

	/**
	 * @return string name of the column in the table (identifier quoted).
	 */
	public function getColumnName()
	{
		return $this->getInfo('ColumnName');
	}

	/**
	 * @return string name of the column with quoted identifier.
	 */
	public function getColumnId()
	{
		return $this->getInfo('ColumnId');
	}

	/**
	 * @return string size of the column.
	 */
	public function getColumnSize()
	{
		return $this->getInfo('ColumnSize');
	}

	/**
	 * @return integer zero-based ordinal position of the column in the table.
	 */
	public function getColumnIndex()
	{
		return $this->getInfo('ColumnIndex');
	}

	/**
	 * @return string column type.
	 */
	public function getDbType()
	{
		return $this->getInfo('DbType');
	}

	/**
	 * @return boolean specifies whether value Null is allowed, default is false.
	 */
	public function getAllowNull()
	{
		return $this->getInfo('AllowNull',false);
	}

	/**
	 * @return mixed default column value if column value was null.
	 */
	public function getDefaultValue()
	{
		return $this->getInfo('DefaultValue', self::UNDEFINED_VALUE);
	}

	/**
	 * @return string precision of the column data, if the data is numeric.
	 */
	public function getNumericPrecision()
	{
		return $this->getInfo('NumericPrecision');
	}

	/**
	 * @return string scale of the column data, if the data is numeric.
	 */
	public function getNumericScale()
	{
		return $this->getInfo('NumericScale');
	}

	public function getMaxiumNumericConstraint()
	{
		if(($precision=$this->getNumericPrecision())!==null)
		{
			$scale=$this->getNumericScale();
			return $scale===null ? pow(10,$precision) : pow(10,$precision-$scale);
		}
	}

	/**
	 * @return boolean whether this column is a primary key for the table, default is false.
	 */
	public function getIsPrimaryKey()
	{
		return $this->getInfo('IsPrimaryKey',false);
	}

	/**
	 * @return boolean whether this column is a foreign key, default is false.
	 */
	public function getIsForeignKey()
	{
		return $this->getInfo('IsForeignKey',false);
	}

	/**
	 * @param string sequence name, only applicable if column is a sequence
	 */
	public function getSequenceName()
	{
		return $this->getInfo('SequenceName');
	}

	/**
	 * @return boolean whether the column is a sequence.
	 */
	public function hasSequence()
	{
		return $this->getSequenceName()!==null;
	}

	/**
	 * @return boolean whether this column is excluded from insert and update.
	 */
	public function getIsExcluded()
	{
		return false;
	}
}