Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * PHPUnit
4
 *
5
 * Copyright (c) 2002-2010, 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-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
41
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42
 * @link       http://www.phpunit.de/
43
 * @since      File available since Release 3.2.0
44
 */
45
 
46
require_once 'PHPUnit/Framework.php';
47
require_once 'PHPUnit/Util/Filter.php';
48
require_once 'PHPUnit/Extensions/Database/DB/IMetaData.php';
49
 
50
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
51
 
52
/**
53
 * Provides a basic constructor for all meta data classes and a factory for
54
 * generating the appropriate meta data class.
55
 *
56
 * @category   Testing
57
 * @package    PHPUnit
58
 * @author     Mike Lively <m@digitalsandwich.com>
59
 * @copyright  2010 Mike Lively <m@digitalsandwich.com>
60
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
61
 * @version    Release: 3.4.15
62
 * @link       http://www.phpunit.de/
63
 * @since      Class available since Release 3.2.0
64
 */
65
abstract class PHPUnit_Extensions_Database_DB_MetaData implements PHPUnit_Extensions_Database_DB_IMetaData
66
{
67
    protected static $metaDataClassMap = array(
68
        'pgsql'  => 'PHPUnit_Extensions_Database_DB_MetaData_PgSQL',
69
        'mysql'  => 'PHPUnit_Extensions_Database_DB_MetaData_MySQL',
70
        'oci'    => 'PHPUnit_Extensions_Database_DB_MetaData_Oci',
71
        'sqlite' => 'PHPUnit_Extensions_Database_DB_MetaData_Sqlite',
72
        'sqlite2'=> '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
        $parts = explode('.', $object);
181
        $quotedParts = array();
182
 
183
        foreach ($parts as $part) {
184
            $quotedParts[] = $this->schemaObjectQuoteChar .
185
                str_replace($this->schemaObjectQuoteChar, $this->schemaObjectQuoteChar.$this->schemaObjectQuoteChar, $part).
186
                $this->schemaObjectQuoteChar;
187
        }
188
 
189
        return implode('.', $quotedParts);
190
    }
191
 
192
    /**
193
     * Seperates the schema and the table from a fully qualified table name.
194
     *
195
     * Returns an associative array containing the 'schema' and the 'table'.
196
     *
197
     * @param string $fullTableName
198
     * @return array
199
     */
200
    public function splitTableName($fullTableName)
201
    {
202
        if (($dot = strpos($fullTableName, '.')) !== FALSE) {
203
            return array(
204
                'schema' => substr($fullTableName, 0, $dot),
205
                'table' => substr($fullTableName, $dot + 1)
206
            );
207
        } else {
208
            return array(
209
                'schema' => NULL,
210
                'table' => $fullTableName
211
            );
212
        }
213
    }
214
 
215
    /**
216
     * Returns the command for the database to truncate a table.
217
     *
218
     * @return string
219
     */
220
    public function getTruncateCommand()
221
    {
222
        return $this->truncateCommand;
223
    }
224
 
225
    /**
226
     * Returns true if the rdbms allows cascading
227
     *
228
     * @return bool
229
     */
230
    public function allowsCascading()
231
    {
232
        return FALSE;
233
    }
234
}
235
 
236
/**
237
 * I am not sure why these requires can't go above the class, but when they do
238
 * the classes can't find the PHPUnit_Extensions_Database_DB_MetaData
239
 * class.
240
 */
241
require_once 'PHPUnit/Extensions/Database/DB/MetaData/Sqlite.php';
242
require_once 'PHPUnit/Extensions/Database/DB/MetaData/InformationSchema.php';
243
require_once 'PHPUnit/Extensions/Database/DB/MetaData/MySQL.php';
244
require_once 'PHPUnit/Extensions/Database/DB/MetaData/PgSQL.php';
245
?>