summaryrefslogtreecommitdiff
path: root/framework/Data/ActiveRecord/Vendor/TMysqlMetaDataInspector.php
blob: 23c483d1ffe38d8569a58850f77da754307cf125 (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
<?php
/**
 * TMysqlMetaDataInspector 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
 */

Prado::using('System.Data.ActiveRecord.Vendor.TDbMetaDataInspector');
Prado::using('System.Data.ActiveRecord.Vendor.TMysqlColumnMetaData');
Prado::using('System.Data.ActiveRecord.Vendor.TMysqlMetaData');

/**
 * TMysqlMetaDataInspector class.
 *
 * Gathers table column properties for Mysql database.
 *
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
 * @version $Id$
 * @package System.Data.ActiveRecord.Vendor
 * @since 3.1
 */
class TMysqlMetaDataInspector extends TDbMetaDataInspector
{
	/**
	 * Get the column definitions for given table.
	 * @param string table name.
	 * @return array column name value pairs of column meta data.
	 */
	protected function getColumnDefinitions($table)
	{
		$sql="SHOW FULL FIELDS FROM {$table}";
		$conn = $this->getDbConnection();
		$conn->setActive(true);
		$command = $conn->createCommand($sql);
		$command->prepare();
		foreach($command->query() as $col)
			$cols[strtolower($col['Field'])] = $this->getColumnMetaData($col);
		return $cols;
	}

	protected function getColumnMetaData($col)
	{
		$name = '`'.$col['Field'].'`'; //quote the column names!
		$type = $col['Type'];
		$notNull = $col['Null']==='NO';
		$autoIncrement=is_int(strpos(strtolower($col['Extra']), 'auto_increment'));
		$default = $col['Default'];
		$primaryKey = $col['Key']==='PRI';
		return new TMysqlColumnMetaData(strtolower($col['Field']),$name,$type,
						$notNull,$autoIncrement,$default,$primaryKey);
	}

	/**
	 * Not implemented, Mysql does not always have foreign key constraints.
	 */
	protected function getConstraintKeys($table)
	{
		return array('primary'=>array(), 'foreign'=>array());
	}

	/**
	 * Create a new instance of meta data.
	 * @param string table name
	 * @param array column meta data
	 * @param array primary key meta data
	 * @param array foreign key meta data.
	 * @return TDbMetaData table meta data.
	 */
	protected function createMetaData($table, $columns, $primary, $foreign)
	{
		$pks = array();
		foreach($columns as $name=>$column)
			if($column->getIsPrimaryKey())
				$pks[] = $name;
		return new TMysqlMetaData($table,$columns,$pks);
	}
}

?>