Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TDbMetaData class file.
4
 *
5
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TDbMetaData.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Data.Common
11
 */
12
 
13
/**
14
 * TDbMetaData is the base class for retrieving metadata information, such as
15
 * table and columns information, from a database connection.
16
 *
17
 * Use the {@link getTableInfo} method to retrieve a table information.
18
 *
19
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
20
 * @version $Id: TDbMetaData.php 2541 2008-10-21 15:05:13Z qiang.xue $
21
 * @package System.Data.Common
22
 * @since 3.1
23
 */
24
abstract class TDbMetaData extends TComponent
25
{
26
	private $_tableInfoCache=array();
27
	private $_connection;
28
 
29
	/**
30
	 * @param TDbConnection database connection.
31
	 */
32
	public function __construct($conn)
33
	{
34
		$this->_connection=$conn;
35
	}
36
 
37
	/**
38
	 * @return TDbConnection database connection.
39
	 */
40
	public function getDbConnection()
41
	{
42
		return $this->_connection;
43
	}
44
 
45
	/**
46
	 * Obtain database specific TDbMetaData class using the driver name of the database connection.
47
	 * @param TDbConnection database connection.
48
	 * @return TDbMetaData database specific TDbMetaData.
49
	 */
50
	public static function getInstance($conn)
51
	{
52
		$conn->setActive(true); //must be connected before retrieving driver name
53
		$driver = $conn->getDriverName();
54
		switch(strtolower($driver))
55
		{
56
			case 'pgsql':
57
				Prado::using('System.Data.Common.Pgsql.TPgsqlMetaData');
58
				return new TPgsqlMetaData($conn);
59
			case 'mysqli':
60
			case 'mysql':
61
				Prado::using('System.Data.Common.Mysql.TMysqlMetaData');
62
				return new TMysqlMetaData($conn);
63
			case 'sqlite': //sqlite 3
64
			case 'sqlite2': //sqlite 2
65
				Prado::using('System.Data.Common.Sqlite.TSqliteMetaData');
66
				return new TSqliteMetaData($conn);
67
			case 'mssql': // Mssql driver on windows hosts
68
			case 'dblib': // dblib drivers on linux (and maybe others os) hosts
69
				Prado::using('System.Data.Common.Mssql.TMssqlMetaData');
70
				return new TMssqlMetaData($conn);
71
			case 'oci':
72
				Prado::using('System.Data.Common.Oracle.TOracleMetaData');
73
				return new TOracleMetaData($conn);
74
//			case 'ibm':
75
//				Prado::using('System.Data.Common.IbmDb2.TIbmDb2MetaData');
76
//				return new TIbmDb2MetaData($conn);
77
			default:
78
				throw new TDbException('ar_invalid_database_driver',$driver);
79
		}
80
	}
81
 
82
	/**
83
	 * Obtains table meta data information for the current connection and given table name.
84
	 * @param string table or view name
85
	 * @return TDbTableInfo table information.
86
	 */
87
	public function getTableInfo($tableName=null)
88
	{
89
		$key = $tableName===null?$this->getDbConnection()->getConnectionString():$tableName;
90
		if(!isset($this->_tableInfoCache[$key]))
91
		{
92
			$class = $this->getTableInfoClass();
93
			$tableInfo = $tableName===null ? new $class : $this->createTableInfo($tableName);
94
			$this->_tableInfoCache[$key] = $tableInfo;
95
		}
96
		return $this->_tableInfoCache[$key];
97
	}
98
 
99
	/**
100
	 * Creates a command builder for a given table name.
101
	 * @param string table name.
102
	 * @return TDbCommandBuilder command builder instance for the given table.
103
	 */
104
	public function createCommandBuilder($tableName=null)
105
	{
106
		return $this->getTableInfo($tableName)->createCommandBuilder($this->getDbConnection());
107
	}
108
 
109
	/**
110
	 * This method should be implemented by decendent classes.
111
	 * @return TDbTableInfo driver dependent create builder.
112
	 */
113
	abstract protected function createTableInfo($tableName);
114
 
115
	/**
116
	 * @return string TDbTableInfo class name.
117
	 */
118
	protected function getTableInfoClass()
119
	{
120
		return 'TDbTableInfo';
121
	}
122
}
123