]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Extensions/Database/DB/MetaData.php
Add .gitignore
[Github/sugarcrm.git] / tests / PHPUnit / Extensions / Database / DB / MetaData.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
15  *   * Redistributions in binary form must reproduce the above copyright
16  *     notice, this list of conditions and the following disclaimer in
17  *     the documentation and/or other materials provided with the
18  *     distribution.
19  *
20  *   * Neither the name of Sebastian Bergmann nor the names of his
21  *     contributors may be used to endorse or promote products derived
22  *     from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * @category   Testing
38  * @package    PHPUnit
39  * @author     Mike Lively <m@digitalsandwich.com>
40  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42
43  * @link       http://www.phpunit.de/
44  * @since      File available since Release 3.2.0
45  */
46
47 require_once 'PHPUnit/Framework.php';
48 require_once 'PHPUnit/Util/Filter.php';
49 require_once 'PHPUnit/Extensions/Database/DB/IMetaData.php';
50
51 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
52
53 /**
54  * Provides a basic constructor for all meta data classes and a factory for
55  * generating the appropriate meta data class.
56  *
57  * @category   Testing
58  * @package    PHPUnit
59  * @author     Mike Lively <m@digitalsandwich.com>
60  * @copyright  2009 Mike Lively <m@digitalsandwich.com>
61  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
62  * @version    Release: 3.3.17
63  * @link       http://www.phpunit.de/
64  * @since      Class available since Release 3.2.0
65  */
66 abstract class PHPUnit_Extensions_Database_DB_MetaData implements PHPUnit_Extensions_Database_DB_IMetaData
67 {
68     protected static $metaDataClassMap = array(
69         'pgsql'  => 'PHPUnit_Extensions_Database_DB_MetaData_PgSQL',
70         'mysql'  => 'PHPUnit_Extensions_Database_DB_MetaData_MySQL',
71         'oci'    => 'PHPUnit_Extensions_Database_DB_MetaData_Oci',
72         'sqlite' => 'PHPUnit_Extensions_Database_DB_MetaData_Sqlite'
73     );
74
75     /**
76      * The PDO connection used to retreive database meta data
77      *
78      * @var PDO
79      */
80     protected $pdo;
81
82     /**
83      * The default schema name for the meta data object.
84      *
85      * @var string
86      */
87     protected $schema;
88
89     /**
90      * The character used to quote schema objects.
91      */
92     protected $schemaObjectQuoteChar = '"';
93
94     /**
95      * The command used to perform a TRUNCATE operation.
96      */
97     protected $truncateCommand = 'TRUNCATE';
98
99     /**
100      * Creates a new database meta data object using the given pdo connection
101      * and schema name.
102      *
103      * @param PDO $pdo
104      * @param string $schema
105      */
106     public final function __construct(PDO $pdo, $schema)
107     {
108         $this->pdo = $pdo;
109         $this->schema = $schema;
110     }
111
112     /**
113      * Creates a meta data object based on the driver of given $pdo object and
114      * $schema name.
115      *
116      * @param PDO $pdo
117      * @param string $schema
118      * @return PHPUnit_Extensions_Database_DB_MetaData
119      */
120     public static function createMetaData(PDO $pdo, $schema)
121     {
122         $driverName = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
123         if (isset(self::$metaDataClassMap[$driverName])) {
124             $className = self::$metaDataClassMap[$driverName];
125
126             if ($className instanceof ReflectionClass) {
127                 return $className->newInstance($pdo, $schema);
128             } else {
129                 return self::registerClassWithDriver($className, $driverName)->newInstance($pdo, $schema);
130             }
131         } else {
132             throw new Exception("Could not find a meta data driver for {$driverName} pdo driver.");
133         }
134     }
135
136     /**
137      * Validates and registers the given $className with the given $pdoDriver.
138      * It should be noted that this function will not attempt to include /
139      * require the file. The $pdoDriver can be determined by the value of the
140      * PDO::ATTR_DRIVER_NAME attribute for a pdo object.
141      *
142      * A reflection of the $className is returned.
143      *
144      * @param string $className
145      * @param string $pdoDriver
146      * @return ReflectionClass
147      */
148     public static function registerClassWithDriver($className, $pdoDriver)
149     {
150         if (!class_exists($className)) {
151             throw new Exception("Specified class for {$pdoDriver} driver ({$className}) does not exist.");
152         }
153
154         $reflection = new ReflectionClass($className);
155         if ($reflection->isSubclassOf('PHPUnit_Extensions_Database_DB_MetaData')) {
156             return self::$metaDataClassMap[$pdoDriver] = $reflection;
157         } else {
158             throw new Exception("Specified class for {$pdoDriver} driver ({$className}) does not extend PHPUnit_Extensions_Database_DB_MetaData.");
159         }
160     }
161
162     /**
163      * Returns the schema for the connection.
164      *
165      * @return string
166      */
167     public function getSchema()
168     {
169         return $this->schema;
170     }
171
172     /**
173      * Returns a quoted schema object. (table name, column name, etc)
174      *
175      * @param string $object
176      * @return string
177      */
178     public function quoteSchemaObject($object)
179     {
180         return $this->schemaObjectQuoteChar.
181         str_replace($this->schemaObjectQuoteChar, $this->schemaObjectQuoteChar.$this->schemaObjectQuoteChar, $object).
182         $this->schemaObjectQuoteChar;
183     }
184
185     /**
186      * Returns the command for the database to truncate a table.
187      *
188      * @return string
189      */
190     public function getTruncateCommand()
191     {
192         return $this->truncateCommand;
193     }
194
195     /**
196      * Returns true if the rdbms allows cascading
197      *
198      * @return bool
199      */
200     public function allowsCascading()
201     {
202         return FALSE;
203     }
204 }
205
206 /**
207  * I am not sure why these requires can't go above the class, but when they do
208  * the classes can't find the PHPUnit_Extensions_Database_DB_MetaData
209  * class.
210  */
211 require_once 'PHPUnit/Extensions/Database/DB/MetaData/Sqlite.php';
212 require_once 'PHPUnit/Extensions/Database/DB/MetaData/InformationSchema.php';
213 require_once 'PHPUnit/Extensions/Database/DB/MetaData/MySQL.php';
214 require_once 'PHPUnit/Extensions/Database/DB/MetaData/PgSQL.php';
215 ?>