]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Data/Common/TDbMetaData.php
5b4568cb914a9d50515b1bf95ea752656b40884c
[bacula/bacula] / gui / baculum / framework / Data / Common / TDbMetaData.php
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-2014 PradoSoft
8  * @license http://www.pradosoft.com/license/
9  * @package System.Data.Common
10  */
11
12 /**
13  * TDbMetaData is the base class for retrieving metadata information, such as
14  * table and columns information, from a database connection.
15  *
16  * Use the {@link getTableInfo} method to retrieve a table information.
17  *
18  * @author Wei Zhuo <weizho[at]gmail[dot]com>
19  * @package System.Data.Common
20  * @since 3.1
21  */
22 abstract class TDbMetaData extends TComponent
23 {
24         private $_tableInfoCache=array();
25         private $_connection;
26
27         /**
28          * @var array
29          */
30         protected static $delimiterIdentifier = array('[', ']', '"', '`', "'");
31
32         /**
33          * @param TDbConnection database connection.
34          */
35         public function __construct($conn)
36         {
37                 $this->_connection=$conn;
38         }
39
40         /**
41          * @return TDbConnection database connection.
42          */
43         public function getDbConnection()
44         {
45                 return $this->_connection;
46         }
47
48         /**
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.
52          */
53         public static function getInstance($conn)
54         {
55                 $conn->setActive(true); //must be connected before retrieving driver name
56                 $driver = $conn->getDriverName();
57                 switch(strtolower($driver))
58                 {
59                         case 'pgsql':
60                                 Prado::using('System.Data.Common.Pgsql.TPgsqlMetaData');
61                                 return new TPgsqlMetaData($conn);
62                         case 'mysqli':
63                         case 'mysql':
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);
75                         case 'oci':
76                                 Prado::using('System.Data.Common.Oracle.TOracleMetaData');
77                                 return new TOracleMetaData($conn);
78 //                      case 'ibm':
79 //                              Prado::using('System.Data.Common.IbmDb2.TIbmDb2MetaData');
80 //                              return new TIbmDb2MetaData($conn);
81                         default:
82                                 throw new TDbException('ar_invalid_database_driver',$driver);
83                 }
84         }
85
86         /**
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.
90          */
91         public function getTableInfo($tableName=null)
92         {
93                 $key = $tableName===null?$this->getDbConnection()->getConnectionString():$tableName;
94                 if(!isset($this->_tableInfoCache[$key]))
95                 {
96                         $class = $this->getTableInfoClass();
97                         $tableInfo = $tableName===null ? new $class : $this->createTableInfo($tableName);
98                         $this->_tableInfoCache[$key] = $tableInfo;
99                 }
100                 return $this->_tableInfoCache[$key];
101         }
102
103         /**
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.
107          */
108         public function createCommandBuilder($tableName=null)
109         {
110                 return $this->getTableInfo($tableName)->createCommandBuilder($this->getDbConnection());
111         }
112
113         /**
114          * This method should be implemented by decendent classes.
115          * @return TDbTableInfo driver dependent create builder.
116          */
117         abstract protected function createTableInfo($tableName);
118
119         /**
120          * @return string TDbTableInfo class name.
121          */
122         protected function getTableInfoClass()
123         {
124                 return 'TDbTableInfo';
125         }
126
127         /**
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
133          */
134         public function quoteTableName($name)
135         {
136                 $name = str_replace(self::$delimiterIdentifier, '', $name);
137
138                 $args = func_get_args();
139                 $rgt = $lft = isset($args[1]) ? $args[1] : '';
140                 $rgt = isset($args[2]) ? $args[2] : $rgt;
141
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);
148         }
149
150         /**
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
156          */
157         public function quoteColumnName($name)
158         {
159                 $args = func_get_args();
160                 $rgt = $lft = isset($args[1]) ? $args[1] : '';
161                 $rgt = isset($args[2]) ? $args[2] : $rgt;
162
163                 return $lft . str_replace(self::$delimiterIdentifier, '', $name) . $rgt;
164         }
165
166         /**
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
172          */
173         public function quoteColumnAlias($name)
174         {
175                 $args = func_get_args();
176                 $rgt = $lft = isset($args[1]) ? $args[1] : '';
177                 $rgt = isset($args[2]) ? $args[2] : $rgt;
178
179                 return $lft . str_replace(self::$delimiterIdentifier, '', $name) . $rgt;
180         }
181         
182         /**
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.
189          */
190         abstract public function findTableNames($schema='');
191 }
192