summaryrefslogtreecommitdiff
path: root/framework/Data/Common/IbmDb2/TIbmMetaDataInspector.php
blob: a37fad6e3e58a3c4b42621de01fa5fbe19cc0be2 (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
<?php
/**
 * TIbmMetaDataInspector class file.
 *
 * @author Cesar Ramos <cramos[at]gmail[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2008 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Id: TIbmMetaDataInspector.php 1807 2007-03-31 06:42:15Z wei $
 * @package System.Data.ActiveRecord.Vendor
 */
Prado::using('System.Data.ActiveRecord.Vendor.TDbMetaDataInspector');
Prado::using('System.Data.ActiveRecord.Vendor.TIbmColumnMetaData');
Prado::using('System.Data.ActiveRecord.Vendor.TIbmMetaData');

/**
 * TIbmMetaDataInspector class.
 *
 * Column details for IBM DB2 database. Using php_pdo_ibm.dll extension.
 *
 * @author Cesar Ramos <cramos[at]gmail[dot]com>
 * @version $Id: TIbmMetaDataInspector.php 1807 2007-03-31 06:42:15Z wei $
 * @package System.Data.ActiveRecord.Vendor
 * @since 3.1
 */
class TIbmMetaDataInspector extends TDbMetaDataInspector
{
	private $_schema;

	/**
	 * @param string default schema.
	 */
	public function setSchema($schema)
	{
		$this->_schema=$schema;
	}

	/**
	 * @return string default schema.
	 */
	public function getSchema()
	{
		return $this->_schema;
	}
	/**
	 * 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)
	{
		if(count($parts= explode('.', $table)) > 1)
		{
			$tablename = $parts[1];
			$schema = $parts[0];
		}
		else
		{
			$tablename = $parts[0];
			$schema = $this->getSchema();
		}
		$sql="SELECT * FROM SYSCAT.COLUMNS WHERE TABNAME='".strtoupper($tablename)."'";
		if ($schema)
			$sql=$sql." AND TABSCHEMA='".strtoupper($schema)."'";

		$conn = $this->getDbConnection();
		$conn->setActive(true);
		$command = $conn->createCommand($sql);
		$command->prepare();
		$result=$command->query($sql);
		foreach ($result as $col)
    		$cols[strtolower($col['COLNAME'])] = $this->getColumnMetaData($col);
		return $cols;
	}

	protected function getColumnMetaData($col)
	{
		$name = strtolower($col['COLNAME']);
		$type = $col['TYPENAME'];
		$length = $col['LENGTH'];
		$notNull = $col['NULLS']==='N'?1:0;
		$autoIncrement=$col['IDENTITY']==='Y'?1:0;
		$default = $col['DEFAULT'];
		$primaryKey = $col['KEYSEQ']?1:0;
		return new TIbmColumnMetaData($name,$type,$length,$notNull,$autoIncrement,$default,$primaryKey);
	}

	/**
	 * Not implemented, IBM 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 TIbmMetaData($table,$columns,$pks);
	}
}