3 * TDbMetaData class file.
5 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6 * @link http://www.pradosoft.com/
7 * @copyright Copyright © 2005-2014 PradoSoft
8 * @license http://www.pradosoft.com/license/
9 * @package System.Data.Common
13 * TDbMetaData is the base class for retrieving metadata information, such as
14 * table and columns information, from a database connection.
16 * Use the {@link getTableInfo} method to retrieve a table information.
18 * @author Wei Zhuo <weizho[at]gmail[dot]com>
19 * @package System.Data.Common
22 abstract class TDbMetaData extends TComponent
24 private $_tableInfoCache=array();
30 protected static $delimiterIdentifier = array('[', ']', '"', '`', "'");
33 * @param TDbConnection database connection.
35 public function __construct($conn)
37 $this->_connection=$conn;
41 * @return TDbConnection database connection.
43 public function getDbConnection()
45 return $this->_connection;
49 * Obtain database specific TDbMetaData class using the driver name of the database connection.
50 * @param TDbConnection database connection.
51 * @return TDbMetaData database specific TDbMetaData.
53 public static function getInstance($conn)
55 $conn->setActive(true); //must be connected before retrieving driver name
56 $driver = $conn->getDriverName();
57 switch(strtolower($driver))
60 Prado::using('System.Data.Common.Pgsql.TPgsqlMetaData');
61 return new TPgsqlMetaData($conn);
64 Prado::using('System.Data.Common.Mysql.TMysqlMetaData');
65 return new TMysqlMetaData($conn);
66 case 'sqlite': //sqlite 3
67 case 'sqlite2': //sqlite 2
68 Prado::using('System.Data.Common.Sqlite.TSqliteMetaData');
69 return new TSqliteMetaData($conn);
70 case 'mssql': // Mssql driver on windows hosts
71 case 'sqlsrv': // sqlsrv driver on windows hosts
72 case 'dblib': // dblib drivers on linux (and maybe others os) hosts
73 Prado::using('System.Data.Common.Mssql.TMssqlMetaData');
74 return new TMssqlMetaData($conn);
76 Prado::using('System.Data.Common.Oracle.TOracleMetaData');
77 return new TOracleMetaData($conn);
79 // Prado::using('System.Data.Common.IbmDb2.TIbmDb2MetaData');
80 // return new TIbmDb2MetaData($conn);
82 throw new TDbException('ar_invalid_database_driver',$driver);
87 * Obtains table meta data information for the current connection and given table name.
88 * @param string table or view name
89 * @return TDbTableInfo table information.
91 public function getTableInfo($tableName=null)
93 $key = $tableName===null?$this->getDbConnection()->getConnectionString():$tableName;
94 if(!isset($this->_tableInfoCache[$key]))
96 $class = $this->getTableInfoClass();
97 $tableInfo = $tableName===null ? new $class : $this->createTableInfo($tableName);
98 $this->_tableInfoCache[$key] = $tableInfo;
100 return $this->_tableInfoCache[$key];
104 * Creates a command builder for a given table name.
105 * @param string table name.
106 * @return TDbCommandBuilder command builder instance for the given table.
108 public function createCommandBuilder($tableName=null)
110 return $this->getTableInfo($tableName)->createCommandBuilder($this->getDbConnection());
114 * This method should be implemented by decendent classes.
115 * @return TDbTableInfo driver dependent create builder.
117 abstract protected function createTableInfo($tableName);
120 * @return string TDbTableInfo class name.
122 protected function getTableInfoClass()
124 return 'TDbTableInfo';
128 * Quotes a table name for use in a query.
129 * @param string $name table name
130 * @param string $lft left delimiter
131 * @param string $rgt right delimiter
132 * @return string the properly quoted table name
134 public function quoteTableName($name)
136 $name = str_replace(self::$delimiterIdentifier, '', $name);
138 $args = func_get_args();
139 $rgt = $lft = isset($args[1]) ? $args[1] : '';
140 $rgt = isset($args[2]) ? $args[2] : $rgt;
142 if(strpos($name, '.')===false)
143 return $lft . $name . $rgt;
144 $names=explode('.', $name);
145 foreach($names as &$n)
146 $n = $lft . $n . $rgt;
147 return implode('.', $names);
151 * Quotes a column name for use in a query.
152 * @param string $name column name
153 * @param string $lft left delimiter
154 * @param string $rgt right delimiter
155 * @return string the properly quoted column name
157 public function quoteColumnName($name)
159 $args = func_get_args();
160 $rgt = $lft = isset($args[1]) ? $args[1] : '';
161 $rgt = isset($args[2]) ? $args[2] : $rgt;
163 return $lft . str_replace(self::$delimiterIdentifier, '', $name) . $rgt;
167 * Quotes a column alias for use in a query.
168 * @param string $name column alias
169 * @param string $lft left delimiter
170 * @param string $rgt right delimiter
171 * @return string the properly quoted column alias
173 public function quoteColumnAlias($name)
175 $args = func_get_args();
176 $rgt = $lft = isset($args[1]) ? $args[1] : '';
177 $rgt = isset($args[2]) ? $args[2] : $rgt;
179 return $lft . str_replace(self::$delimiterIdentifier, '', $name) . $rgt;
183 * Returns all table names in the database.
184 * This method should be overridden by child classes in order to support this feature
185 * because the default implementation simply throws an exception.
186 * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
187 * If not empty, the returned table names will be prefixed with the schema name.
188 * @return array all table names in the database.
190 abstract public function findTableNames($schema='');