summaryrefslogtreecommitdiff
path: root/framework/Data/ActiveRecord/TActiveRecordGateway.php
blob: 85b1c2d189b4b3d9fa60641c9b6eeba74e58152a (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<?php
/**
 * TActiveRecordGateway, TActiveRecordStatementType, TActiveRecordEventParameter classes file.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @link https://github.com/pradosoft/prado
 * @copyright Copyright &copy; 2005-2016 The PRADO Group
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
 * @package System.Data.ActiveRecord
 */

/**
 * TActiveRecordGateway excutes the SQL command queries and returns the data
 * record as arrays (for most finder methods).
 *
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
 * @package System.Data.ActiveRecord
 * @since 3.1
 */
class TActiveRecordGateway extends TComponent
{
	private $_manager;
	private $_tables=array(); //table cache
	private $_meta=array(); //meta data cache.
	private $_commandBuilders=array();
	private $_currentRecord;

	/**
	 * Constant name for specifying optional table name in TActiveRecord.
	 */
	const TABLE_CONST='TABLE';
	/**
	 * Method name for returning optional table name in in TActiveRecord
	 */
	const TABLE_METHOD='table';

	/**
	 * Record gateway constructor.
	 * @param TActiveRecordManager $manager
	 */
	public function __construct(TActiveRecordManager $manager)
	{
		$this->_manager=$manager;
	}

	/**
	 * @return TActiveRecordManager record manager.
	 */
	protected function getManager()
	{
		return $this->_manager;
	}

	/**
	 * Gets the table name from the 'TABLE' constant of the active record
	 * class if defined, otherwise use the class name as table name.
	 * @param TActiveRecord active record instance
	 * @return string table name for the given record class.
	 */
	protected function getRecordTableName(TActiveRecord $record)
	{
		$class = new ReflectionClass($record);
		if($class->hasConstant(self::TABLE_CONST))
		{
			$value = $class->getConstant(self::TABLE_CONST);
			if(empty($value))
				throw new TActiveRecordException('ar_invalid_tablename_property',
					get_class($record),self::TABLE_CONST);
			return $value;
		}
		elseif ($class->hasMethod(self::TABLE_METHOD))
		{
			$value = $record->{self::TABLE_METHOD}();
			if(empty($value))
				throw new TActiveRecordException('ar_invalid_tablename_method',
					get_class($record),self::TABLE_METHOD);
			return $value;
		}
		else
			return strtolower(get_class($record));
	}

	/**
	 * Returns table information, trys the application cache first.
	 * @param TActiveRecord $record
	 * @return TDbTableInfo table information.
	 */
	public function getRecordTableInfo(TActiveRecord $record)
	{
		$tableName = $this->getRecordTableName($record);
		return $this->getTableInfo($record->getDbConnection(), $tableName);
	}

	/**
	 * Returns table information for table in the database connection.
	 * @param TDbConnection database connection
	 * @param string table name
	 * @return TDbTableInfo table details.
	 */
	public function getTableInfo(TDbConnection $connection, $tableName)
	{
		$connStr = $connection->getConnectionString();
		$key = $connStr.$tableName;
		if(!isset($this->_tables[$key]))
		{
			//call this first to ensure that unserializing the cache
			//will find the correct driver dependent classes.
			if(!isset($this->_meta[$connStr]))
			{
				Prado::using('System.Data.Common.TDbMetaData');
				$this->_meta[$connStr] = TDbMetaData::getInstance($connection);
			}

			$tableInfo = null;
			if(($cache=$this->getManager()->getCache())!==null)
				$tableInfo = $cache->get($key);
			if(empty($tableInfo))
			{
				$tableInfo = $this->_meta[$connStr]->getTableInfo($tableName);
				if($cache!==null)
					$cache->set($key, $tableInfo);
			}
			$this->_tables[$key] = $tableInfo;
		}
		return $this->_tables[$key];
	}

	/**
	 * @param TActiveRecord $record
	 * @return TDataGatewayCommand
	 */
	public function getCommand(TActiveRecord $record)
	{
		$conn = $record->getDbConnection();
		$connStr = $conn->getConnectionString();
		$tableInfo = $this->getRecordTableInfo($record);
		if(!isset($this->_commandBuilders[$connStr]))
		{
			$builder = $tableInfo->createCommandBuilder($record->getDbConnection());
			Prado::using('System.Data.DataGateway.TDataGatewayCommand');
			$command = new TDataGatewayCommand($builder);
			$command->OnCreateCommand[] = array($this, 'onCreateCommand');
			$command->OnExecuteCommand[] = array($this, 'onExecuteCommand');
			$this->_commandBuilders[$connStr] = $command;

		}
		$this->_commandBuilders[$connStr]->getBuilder()->setTableInfo($tableInfo);
		$this->_currentRecord=$record;
		return $this->_commandBuilders[$connStr];
	}

	/**
	 * Raised when a command is prepared and parameter binding is completed.
	 * The parameter object is TDataGatewayEventParameter of which the
	 * {@link TDataGatewayEventParameter::getCommand Command} property can be
	 * inspected to obtain the sql query to be executed.
	 * This method also raises the OnCreateCommand event on the ActiveRecord
	 * object calling this gateway.
	 * @param TDataGatewayCommand originator $sender
	 * @param TDataGatewayEventParameter
	 */
	public function onCreateCommand($sender, $param)
	{
		$this->raiseEvent('OnCreateCommand', $this, $param);
		if($this->_currentRecord!==null)
			$this->_currentRecord->onCreateCommand($param);
	}

	/**
	 * Raised when a command is executed and the result from the database was returned.
	 * The parameter object is TDataGatewayResultEventParameter of which the
	 * {@link TDataGatewayEventParameter::getResult Result} property contains
	 * the data return from the database. The data returned can be changed
	 * by setting the {@link TDataGatewayEventParameter::setResult Result} property.
	 * This method also raises the OnCreateCommand event on the ActiveRecord
	 * object calling this gateway.
	 * @param TDataGatewayCommand originator $sender
	 * @param TDataGatewayResultEventParameter
	 */
	public function onExecuteCommand($sender, $param)
	{
		$this->raiseEvent('OnExecuteCommand', $this, $param);
		if($this->_currentRecord!==null)
			$this->_currentRecord->onExecuteCommand($param);
	}

	/**
	 * Returns record data matching the given primary key(s). If the table uses
	 * composite key, specify the name value pairs as an array.
	 * @param TActiveRecord active record instance.
	 * @param array primary name value pairs
	 * @return array record data
	 */
	public function findRecordByPK(TActiveRecord $record,$keys)
	{
		$command = $this->getCommand($record);
		return $command->findByPk($keys);
	}

	/**
	 * Returns records matching the list of given primary keys.
	 * @param TActiveRecord active record instance.
	 * @param array list of primary name value pairs
	 * @return array matching data.
	 */
	public function findRecordsByPks(TActiveRecord $record, $keys)
	{
		return $this->getCommand($record)->findAllByPk($keys);
	}


	/**
	 * Returns record data matching the given critera. If $iterator is true, it will
	 * return multiple rows as TDbDataReader otherwise it returns the <b>first</b> row data.
	 * @param TActiveRecord active record finder instance.
	 * @param TActiveRecordCriteria search criteria.
	 * @param boolean true to return multiple rows as iterator, false returns first row.
	 * @return mixed matching data.
	 */
	public function findRecordsByCriteria(TActiveRecord $record, $criteria, $iterator=false)
	{
		$command = $this->getCommand($record);
		return $iterator ? $command->findAll($criteria) : $command->find($criteria);
	}

	/**
	 * Return record data from sql query.
	 * @param TActiveRecord active record finder instance.
	 * @param TActiveRecordCriteria sql query
	 * @return array result.
	 */
	public function findRecordBySql(TActiveRecord $record, $criteria)
	{
		return $this->getCommand($record)->findBySql($criteria);
	}

	/**
	 * Return record data from sql query.
	 * @param TActiveRecord active record finder instance.
	 * @param TActiveRecordCriteria sql query
	 * @return TDbDataReader result iterator.
	 */
	public function findRecordsBySql(TActiveRecord $record, $criteria)
	{
		return $this->getCommand($record)->findAllBySql($criteria);
	}

	public function findRecordsByIndex(TActiveRecord $record, $criteria, $fields, $values)
	{
		return $this->getCommand($record)->findAllByIndex($criteria,$fields,$values);
	}

	/**
	 * Returns the number of records that match the given criteria.
	 * @param TActiveRecord active record finder instance.
	 * @param TActiveRecordCriteria search criteria
	 * @return int number of records.
	 */
	public function countRecords(TActiveRecord $record, $criteria)
	{
		return $this->getCommand($record)->count($criteria);
	}

	/**
	 * Insert a new record.
	 * @param TActiveRecord new record.
	 * @return int number of rows affected.
	 */
	public function insert(TActiveRecord $record)
	{
		//$this->updateAssociatedRecords($record,true);
		$result = $this->getCommand($record)->insert($this->getInsertValues($record));
		if($result)
			$this->updatePostInsert($record);
		//$this->updateAssociatedRecords($record);
		return $result;
	}

	/**
	 * Sets the last insert ID to the corresponding property of the record if available.
	 * @param TActiveRecord record for insertion
	 */
	protected function updatePostInsert($record)
	{
		$command = $this->getCommand($record);
		$tableInfo = $command->getTableInfo();
		foreach($tableInfo->getColumns() as $name => $column)
		{
			if($column->hasSequence())
				$record->setColumnValue($name,$command->getLastInsertID($column->getSequenceName()));
		}
	}

	/**
	 * @param TActiveRecord record
	 * @return array insert values.
	 */
	protected function getInsertValues(TActiveRecord $record)
	{
		$values=array();
		$tableInfo = $this->getCommand($record)->getTableInfo();
		foreach($tableInfo->getColumns() as $name=>$column)
		{
			if($column->getIsExcluded())
				continue;
			$value = $record->getColumnValue($name);
			if(!$column->getAllowNull() && $value===null && !$column->hasSequence() && ($column->getDefaultValue() === TDbTableColumn::UNDEFINED_VALUE))
			{
				throw new TActiveRecordException(
					'ar_value_must_not_be_null', get_class($record),
					$tableInfo->getTableFullName(), $name);
			}
			if($value!==null)
				$values[$name] = $value;
		}
		return $values;
	}

	/**
	 * Update the record.
	 * @param TActiveRecord dirty record.
	 * @return int number of rows affected.
	 */
	public function update(TActiveRecord $record)
	{
		//$this->updateAssociatedRecords($record,true);
		list($data, $keys) = $this->getUpdateValues($record);
		$result = $this->getCommand($record)->updateByPk($data, $keys);
		//$this->updateAssociatedRecords($record);
		return $result;
	}

	protected function getUpdateValues(TActiveRecord $record)
	{
		$values=array();
		$tableInfo = $this->getCommand($record)->getTableInfo();
		$primary=array();
		foreach($tableInfo->getColumns() as $name=>$column)
		{
			if($column->getIsExcluded())
				continue;
			$value = $record->getColumnValue($name);
			if(!$column->getAllowNull() && $value===null && ($column->getDefaultValue() === TDbTableColumn::UNDEFINED_VALUE))
			{
				throw new TActiveRecordException(
					'ar_value_must_not_be_null', get_class($record),
					$tableInfo->getTableFullName(), $name);
			}
			if($column->getIsPrimaryKey())
				$primary[$name] = $value;
			else
				$values[$name] = $value;
		}
		return array($values,$primary);
	}

	protected function updateAssociatedRecords(TActiveRecord $record,$updateBelongsTo=false)
	{
		$context = new TActiveRecordRelationContext($record);
		return $context->updateAssociatedRecords($updateBelongsTo);
	}

	/**
	 * Delete the record.
	 * @param TActiveRecord record to be deleted.
	 * @return int number of rows affected.
	 */
	public function delete(TActiveRecord $record)
	{
		return $this->getCommand($record)->deleteByPk($this->getPrimaryKeyValues($record));
	}

	protected function getPrimaryKeyValues(TActiveRecord $record)
	{
		$tableInfo = $this->getCommand($record)->getTableInfo();
		$primary=array();
		foreach($tableInfo->getColumns() as $name=>$column)
		{
			if($column->getIsPrimaryKey())
				$primary[$name] = $record->getColumnValue($name);
		}
		return $primary;
	}

	/**
	 * Delete multiple records using primary keys.
	 * @param TActiveRecord finder instance.
	 * @return int number of rows deleted.
	 */
	public function deleteRecordsByPk(TActiveRecord $record, $keys)
	{
		return $this->getCommand($record)->deleteByPk($keys);
	}

	/**
	 * Delete multiple records by criteria.
	 * @param TActiveRecord active record finder instance.
	 * @param TActiveRecordCriteria search criteria
	 * @return int number of records.
	 */
	public function deleteRecordsByCriteria(TActiveRecord $record, $criteria)
	{
		return $this->getCommand($record)->delete($criteria);
	}

	/**
	 * Raise the corresponding command event, insert, update, delete or select.
	 * @param string command type
	 * @param TDbCommand sql command to be executed.
	 * @param TActiveRecord active record
	 * @param TActiveRecordCriteria data for the command.
	 */
	protected function raiseCommandEvent($event,$command,$record,$criteria)
	{
		if(!($criteria instanceof TSqlCriteria))
			$criteria = new TActiveRecordCriteria(null,$criteria);
		$param = new TActiveRecordEventParameter($command,$record,$criteria);
		$manager = $record->getRecordManager();
		$manager->{$event}($param);
		$record->{$event}($param);
	}
}