3 * TDbMetaData class file.
5 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6 * @link http://www.pradosoft.com/
7 * @copyright Copyright © 2005-2013 PradoSoft
8 * @license http://www.pradosoft.com/license/
9 * @version $Id: TDbMetaData.php 3284 2013-04-11 07:14:59Z ctrlaltca $
10 * @package System.Data.Common
14 * TDbMetaData is the base class for retrieving metadata information, such as
15 * table and columns information, from a database connection.
17 * Use the {@link getTableInfo} method to retrieve a table information.
19 * @author Wei Zhuo <weizho[at]gmail[dot]com>
20 * @version $Id: TDbMetaData.php 3284 2013-04-11 07:14:59Z ctrlaltca $
21 * @package System.Data.Common
24 abstract class TDbMetaData extends TComponent
26 private $_tableInfoCache=array();
32 protected static $delimiterIdentifier = array('[', ']', '"', '`', "'");
35 * @param TDbConnection database connection.
37 public function __construct($conn)
39 $this->_connection=$conn;
43 * @return TDbConnection database connection.
45 public function getDbConnection()
47 return $this->_connection;
51 * Obtain database specific TDbMetaData class using the driver name of the database connection.
52 * @param TDbConnection database connection.
53 * @return TDbMetaData database specific TDbMetaData.
55 public static function getInstance($conn)
57 $conn->setActive(true); //must be connected before retrieving driver name
58 $driver = $conn->getDriverName();
59 switch(strtolower($driver))
62 Prado::using('System.Data.Common.Pgsql.TPgsqlMetaData');
63 return new TPgsqlMetaData($conn);
66 Prado::using('System.Data.Common.Mysql.TMysqlMetaData');
67 return new TMysqlMetaData($conn);
68 case 'sqlite': //sqlite 3
69 case 'sqlite2': //sqlite 2
70 Prado::using('System.Data.Common.Sqlite.TSqliteMetaData');
71 return new TSqliteMetaData($conn);
72 case 'mssql': // Mssql driver on windows hosts
73 case 'sqlsrv': // sqlsrv driver on windows hosts
74 case 'dblib': // dblib drivers on linux (and maybe others os) hosts
75 Prado::using('System.Data.Common.Mssql.TMssqlMetaData');
76 return new TMssqlMetaData($conn);
78 Prado::using('System.Data.Common.Oracle.TOracleMetaData');
79 return new TOracleMetaData($conn);
81 // Prado::using('System.Data.Common.IbmDb2.TIbmDb2MetaData');
82 // return new TIbmDb2MetaData($conn);
84 throw new TDbException('ar_invalid_database_driver',$driver);
89 * Obtains table meta data information for the current connection and given table name.
90 * @param string table or view name
91 * @return TDbTableInfo table information.
93 public function getTableInfo($tableName=null)
95 $key = $tableName===null?$this->getDbConnection()->getConnectionString():$tableName;
96 if(!isset($this->_tableInfoCache[$key]))
98 $class = $this->getTableInfoClass();
99 $tableInfo = $tableName===null ? new $class : $this->createTableInfo($tableName);
100 $this->_tableInfoCache[$key] = $tableInfo;
102 return $this->_tableInfoCache[$key];
106 * Creates a command builder for a given table name.
107 * @param string table name.
108 * @return TDbCommandBuilder command builder instance for the given table.
110 public function createCommandBuilder($tableName=null)
112 return $this->getTableInfo($tableName)->createCommandBuilder($this->getDbConnection());
116 * This method should be implemented by decendent classes.
117 * @return TDbTableInfo driver dependent create builder.
119 abstract protected function createTableInfo($tableName);
122 * @return string TDbTableInfo class name.
124 protected function getTableInfoClass()
126 return 'TDbTableInfo';
130 * Quotes a table name for use in a query.
131 * @param string $name table name
132 * @param string $lft left delimiter
133 * @param string $rgt right delimiter
134 * @return string the properly quoted table name
136 public function quoteTableName($name)
138 $name = str_replace(self::$delimiterIdentifier, '', $name);
140 $args = func_get_args();
141 $rgt = $lft = isset($args[1]) ? $args[1] : '';
142 $rgt = isset($args[2]) ? $args[2] : $rgt;
144 if(strpos($name, '.')===false)
145 return $lft . $name . $rgt;
146 $names=explode('.', $name);
147 foreach($names as &$n)
148 $n = $lft . $n . $rgt;
149 return implode('.', $names);
153 * Quotes a column name for use in a query.
154 * @param string $name column name
155 * @param string $lft left delimiter
156 * @param string $rgt right delimiter
157 * @return string the properly quoted column name
159 public function quoteColumnName($name)
161 $args = func_get_args();
162 $rgt = $lft = isset($args[1]) ? $args[1] : '';
163 $rgt = isset($args[2]) ? $args[2] : $rgt;
165 return $lft . str_replace(self::$delimiterIdentifier, '', $name) . $rgt;
169 * Quotes a column alias for use in a query.
170 * @param string $name column alias
171 * @param string $lft left delimiter
172 * @param string $rgt right delimiter
173 * @return string the properly quoted column alias
175 public function quoteColumnAlias($name)
177 $args = func_get_args();
178 $rgt = $lft = isset($args[1]) ? $args[1] : '';
179 $rgt = isset($args[2]) ? $args[2] : $rgt;
181 return $lft . str_replace(self::$delimiterIdentifier, '', $name) . $rgt;