summaryrefslogtreecommitdiff
path: root/framework/Data/ActiveRecord/Vendor/TDbMetaData.php
blob: 38a82aef871f8839c65aed9e5833e373e226e692 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/**
 * TDbMetaData class file.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2007 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Id$
 * @package System.Data.ActiveRecord.Vendor
 */

/**
 * Table meta data for Active Record.
 *
 * TDbMetaData is the base class for database vendor specific that builds
 * the appropriate database commands for active record finder and commit methods.
 *
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
 * @version $Id$
 * @package System.Data.ActiveRecord.Vendor
 * @since 3.1
 */

abstract class TDbMetaData extends TComponent
{
	private $_primaryKeys=array();
	private $_foreignKeys=array();
	private $_columns=array();
	private $_table;
	private $_isView=false;

	/**
	 * Initialize the meta data.
	 * @param string table name
	 * @param array name value pair of column meta data in the table
	 * @param array primary key field names
	 * @param array foriegn key field meta data.
	 */
	public function __construct($table, $cols, $pk, $fk=array(),$view=false)
	{
		$this->_table=$table;
		$this->_columns=$cols;
		$this->_primaryKeys=$pk;
		$this->_foreignKeys=$fk;
		$this->_isView=$view;
	}

	public function getIsView()
	{
		return $this->_isView;
	}

	/**
	 * @return string table name
	 */
	public function getTableName()
	{
		return $this->_table;
	}

	/**
	 * @return array primary key field names.
	 */
	public function getPrimaryKeys()
	{
		return $this->_primaryKeys;
	}

	/**
	 * @return array foreign key meta data.
	 */
	public function getForeignKeys()
	{
		return $this->_foreignKeys;
	}

	/**
	 * @return array name value pair column meta data
	 */
	public function getColumns()
	{
		return $this->_columns;
	}

	/**
	 * @param unknown_type $name
	 */
	public function getColumn($name)
	{
		return $this->_columns[$name];
	}

	/**
	 * Post process the rows after returning from a 1 row query.
	 * @param mixed row data, may be null.
	 * @return mixed processed rows.
	 */
	public function postQueryRow($row)
	{
		return $row;
	}

	/**
	 * Post process the rows after returning from a 1 row query.
	 * @param TDbDataReader multiple row data
	 * @return array post processed data.
	 */
	public function postQuery($rows)
	{
		return $rows;
	}

	/**
	 * @return string command separated list of all fields in the table, field names are quoted.
	 */
	protected function getSelectionColumns()
	{
		$columns = array();
		foreach($this->getColumns() as $column)
			$columns[] = $column->getName();
		return implode(', ', $columns);
	}

	/**
	 * Construct search criteria using primary key names
	 * @return string SQL string for used after WHERE statement.
	 */
	protected function getPrimaryKeyCriteria()
	{
		if(count($this->getPrimaryKeys())===0)
			throw new TActiveRecordException('ar_no_primary_key_found',$this->getTableName());
		$criteria=array();
		foreach($this->getPrimaryKeys() as $key)
			$criteria[] = $this->getColumn($key)->getName(). ' = :'.$key;
		return implode(' AND ', $criteria);
	}

	/**
	 * Construct a "pk IN ('key1', 'key2', ...)" criteria.
	 * @param TDbConnection database connection.
	 * @param array values for IN predicate
	 * @param string SQL string for primary keys IN a list.
	 */
	protected function getCompositeKeysCriteria($conn, $values)
	{
		$count = count($this->getPrimaryKeys());
		if($count===0)
			throw new TActiveRecordException('ar_no_primary_key_found',$this->getTableName());
		if(!is_array($values) || count($values) === 0)
			throw new TActiveRecordException('ar_missing_pk_values', $this->getTableName());
		if($count>1 && !is_array($values[0]))
			$values = array($values);
		if($count > 1 && count($values[0]) !== $count)
			throw new TActiveRecordException('ar_pk_value_count_mismatch', $this->getTableName());

		$columns = array();
		foreach($this->getPrimaryKeys() as $key)
			$columns[] = $this->getColumn($key)->getName();
		return '('.implode(', ',$columns).') IN '.$this->quoteTuple($conn, $values);
	}

	/**
	 * @param TDbConnection database connection.
	 * @param array values
	 * @return string quoted recursive tuple values, e.g. "('val1', 'val2')".
	 */
	protected function quoteTuple($conn, $array)
	{
		$data = array();
		foreach($array as $k=>$v)
			$data[] = is_array($v) ? $this->quoteTuple($conn, $v) : $conn->quoteString($v);
		return '('.implode(', ', $data).')';
	}

	/**
	 * Bind a list of variables in the command. The named parameters is taken
	 * from the values of the $keys parameter. The bind value is taken from the
	 * $values parameter using the index taken from the each value of $keys array.
	 * @param TDbCommand SQL database command
	 * @param array named parameters
	 * @param array binding values (index should match that of $keys)
	 */
	protected function bindArrayKeyValues($command, $keys, $values)
	{
		if(!is_array($values)) $values = array($values);
		foreach($keys as $i => $key)
		{
			$value = isset($values[$i]) ? $values[$i] : $values[$key];
			$command->bindValue(':'.$key, $value);
		}
		$command->prepare();
	}

	/**
	 * Returns a list of name value pairs from the object.
	 * @param array named parameters
	 * @param TActiveRecord record object
	 * @return array name value pairs.
	 */
	protected function getObjectKeyValues($keys, $object)
	{
		$properties = array();
		foreach($keys as $key)
			$properties[$key] = $object->{$key};
		return $properties;
	}

	/**
	 * Gets the columns that can be inserted into the database.
	 * @param TActiveRecord record object to be inserted.
	 * @return array name value pairs of fields to be added.
	 */
	protected function getInsertableColumns($record)
	{
		$columns = array();
		foreach($this->getColumns() as $name=>$column)
		{
			$value = $record->{$name};
			if($column->getNotNull() && $value===null && !$column->getIsPrimaryKey())
			{
				throw new TActiveRecordException(
					'ar_value_must_not_be_null', get_class($record),
					$this->getTableName(), $name);
			}
			if($value!==null)
				$columns[$name] = $value;
		}
		return $columns;
	}

	/**
	 * Gets the columns that will be updated, it exculdes primary key columns
	 * and record properties that are null.
	 * @param TActiveRecord record object with new data for update.
	 * @return array name value pairs of fields to be updated.
	 */
	protected function getUpdatableColumns($record)
	{
		$columns = array();
		foreach($this->getColumns() as $name => $column)
		{
			$value = $record->{$name};
			if(!$column->getIsPrimaryKey() && $value !== null)
				$columns[$name] = $value;
		}
		return $columns;
	}

	/**
	 * Gets a comma delimited string of name parameters for update.
x	 * @param array name value pairs of columns for update.
	 * @return string update named parameter string.
	 */
	protected function getUpdateBindings($columns)
	{
		$fields = array();
		foreach($columns as $name=>$value)
			$fields[] = $this->getColumn($name)->getName(). '= :'.$name;
		return implode(', ', $fields);
	}

	/**
	 * Create a new database command based on the given $sql and bind the
	 * named parameters given by $names with values corresponding in $values.
	 * @param TDbConnection database connection.
	 * @param string SQL string.
	 * @param array named parameters
	 * @param array matching named parameter values
	 * @return TDbCommand binded command, ready for execution.
	 */
	protected function createBindedCommand($conn, $sql, $names,$values)
	{
		$conn->setActive(true);
		$command = $conn->createCommand($sql);
		$this->bindArrayKeyValues($command,$names,$values);
		return $command;
	}

	/**
	 * Creates a new database command and bind the values from the criteria object.
	 *
	 * @param TDbConnection database connection.
	 * @param string SQL string.
	 * @param TActiveRecordCriteria search criteria
	 * @return TDbCommand binded command.
	 */
	protected function createCriteriaBindedCommand($conn,$sql,$criteria)
	{
		$conn->setActive(true);
		$command = $conn->createCommand($sql);
		if($criteria!==null)
		{
			if($criteria->getIsNamedParameters())
			{
				foreach($criteria->getParameters() as $name=>$value)
					$command->bindValue($name,$value);
			}
			else
			{
				$index=1;
				foreach($criteria->getParameters() as $value)
					$command->bindValue($index++,$value);
			}
		}
		$command->prepare();
		return $command;
	}

	/**
	 * Bind parameter values.
	 */
	protected function bindParameterValues($conn,$command,$parameters)
	{
		$index=1;
		foreach($parameters as $key=>$value)
		{
			if(is_string($key))
				$command->bindValue($key,$value);
			else
				$command->bindValue($index++,$value);
		}
		$command->prepare();
	}

	/**
	 * Gets the comma delimited string of fields name for insert command.
	 */
	protected function getInsertColumNames($columns)
	{
		$fields = array();
		foreach($columns as $name=>$column)
			$fields[] = $this->getColumn($name)->getName();
		return implode(', ', $fields);
	}

	/**
	 * Gets the comma delimited string of name bindings for insert command.
	 */
	protected function getInsertColumnValues($columns)
	{
		$fields = array();
		foreach(array_keys($columns) as $column)
			$fields[] = ':'.$column;
		return implode(', ', $fields);
	}

}
?>