]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Data/Common/TDbMetaData.php
Add Baculum
[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-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
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 3284 2013-04-11 07:14:59Z ctrlaltca $
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          * @var array
31          */
32         protected static $delimiterIdentifier = array('[', ']', '"', '`', "'");
33
34         /**
35          * @param TDbConnection database connection.
36          */
37         public function __construct($conn)
38         {
39                 $this->_connection=$conn;
40         }
41
42         /**
43          * @return TDbConnection database connection.
44          */
45         public function getDbConnection()
46         {
47                 return $this->_connection;
48         }
49
50         /**
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.
54          */
55         public static function getInstance($conn)
56         {
57                 $conn->setActive(true); //must be connected before retrieving driver name
58                 $driver = $conn->getDriverName();
59                 switch(strtolower($driver))
60                 {
61                         case 'pgsql':
62                                 Prado::using('System.Data.Common.Pgsql.TPgsqlMetaData');
63                                 return new TPgsqlMetaData($conn);
64                         case 'mysqli':
65                         case 'mysql':
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);
77                         case 'oci':
78                                 Prado::using('System.Data.Common.Oracle.TOracleMetaData');
79                                 return new TOracleMetaData($conn);
80 //                      case 'ibm':
81 //                              Prado::using('System.Data.Common.IbmDb2.TIbmDb2MetaData');
82 //                              return new TIbmDb2MetaData($conn);
83                         default:
84                                 throw new TDbException('ar_invalid_database_driver',$driver);
85                 }
86         }
87
88         /**
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.
92          */
93         public function getTableInfo($tableName=null)
94         {
95                 $key = $tableName===null?$this->getDbConnection()->getConnectionString():$tableName;
96                 if(!isset($this->_tableInfoCache[$key]))
97                 {
98                         $class = $this->getTableInfoClass();
99                         $tableInfo = $tableName===null ? new $class : $this->createTableInfo($tableName);
100                         $this->_tableInfoCache[$key] = $tableInfo;
101                 }
102                 return $this->_tableInfoCache[$key];
103         }
104
105         /**
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.
109          */
110         public function createCommandBuilder($tableName=null)
111         {
112                 return $this->getTableInfo($tableName)->createCommandBuilder($this->getDbConnection());
113         }
114
115         /**
116          * This method should be implemented by decendent classes.
117          * @return TDbTableInfo driver dependent create builder.
118          */
119         abstract protected function createTableInfo($tableName);
120
121         /**
122          * @return string TDbTableInfo class name.
123          */
124         protected function getTableInfoClass()
125         {
126                 return 'TDbTableInfo';
127         }
128
129         /**
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
135          */
136         public function quoteTableName($name)
137         {
138                 $name = str_replace(self::$delimiterIdentifier, '', $name);
139
140                 $args = func_get_args();
141                 $rgt = $lft = isset($args[1]) ? $args[1] : '';
142                 $rgt = isset($args[2]) ? $args[2] : $rgt;
143
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);
150         }
151
152         /**
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
158          */
159         public function quoteColumnName($name)
160         {
161                 $args = func_get_args();
162                 $rgt = $lft = isset($args[1]) ? $args[1] : '';
163                 $rgt = isset($args[2]) ? $args[2] : $rgt;
164
165                 return $lft . str_replace(self::$delimiterIdentifier, '', $name) . $rgt;
166         }
167
168         /**
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
174          */
175         public function quoteColumnAlias($name)
176         {
177                 $args = func_get_args();
178                 $rgt = $lft = isset($args[1]) ? $args[1] : '';
179                 $rgt = isset($args[2]) ? $args[2] : $rgt;
180
181                 return $lft . str_replace(self::$delimiterIdentifier, '', $name) . $rgt;
182         }
183 }
184