SchemaManager.php 18.8 KB
Newer Older
romanb's avatar
romanb committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
<?php
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.org>.
 */

#namespace Doctrine::DBAL::Schema;

/**
25 26
 * Base class for schema managers. Schema managers are used to inspect and/or
 * modify the database schema.
romanb's avatar
romanb committed
27 28 29 30 31 32
 *
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
 * @version     $Revision$
 * @since       2.0
33
 * @todo Rename to AbstractSchemaManager
romanb's avatar
romanb committed
34
 */
romanb's avatar
romanb committed
35
abstract class Doctrine_DBAL_Schema_AbstractSchemaManager
romanb's avatar
romanb committed
36 37 38 39 40 41 42 43 44 45
{
    protected $_conn;

    /**
     * lists all databases
     *
     * @return array
     */
    public function listDatabases()
    {
46 47
        return $this->_conn->fetchColumn($this->_conn->getDatabasePlatform()
                ->getListDatabasesSql());
romanb's avatar
romanb committed
48 49 50 51 52 53 54 55 56
    }

    /**
     * lists all availible database functions
     *
     * @return array
     */
    public function listFunctions()
    {
57 58
        return $this->_conn->fetchColumn($this->_conn->getDatabasePlatform()
                ->getListFunctionsSql());
romanb's avatar
romanb committed
59 60 61 62 63 64 65 66 67 68
    }

    /**
     * Lists all database triggers.
     *
     * @param string|null $database
     * @return array
     */
    public function listTriggers($database = null)
    {
69 70
        return $this->_conn->fetchColumn($this->_conn->getDatabasePlatform()
                ->getListTriggersSql());
romanb's avatar
romanb committed
71 72 73 74 75 76 77 78 79 80
    }

    /**
     * lists all database sequences
     *
     * @param string|null $database
     * @return array
     */
    public function listSequences($database = null)
    {
81 82
        return $this->_conn->fetchColumn($this->_conn->getDatabasePlatform()
                ->getListSequencesSql());
romanb's avatar
romanb committed
83 84 85 86 87 88 89 90 91 92
    }

    /**
     * Lists table constraints.
     *
     * @param string $table     database table name
     * @return array
     */
    public function listTableConstraints($table)
    {
93 94
        return $this->_conn->fetchColumn($this->_conn->getDatabasePlatform()
                ->getListTableConstraintsSql());
romanb's avatar
romanb committed
95 96 97 98 99 100 101 102 103 104
    }

    /**
     * Lists table columns.
     *
     * @param string $table     database table name
     * @return array
     */
    public function listTableColumns($table)
    {
105 106
        return $this->_conn->fetchColumn($this->_conn->getDatabasePlatform()
                ->getListTableColumnsSql());
romanb's avatar
romanb committed
107 108 109 110 111 112 113 114 115 116
    }

    /**
     * Lists table indexes.
     *
     * @param string $table     database table name
     * @return array
     */
    public function listTableIndexes($table)
    {
117 118
        return $this->_conn->fetchColumn($this->_conn->getDatabasePlatform()
                ->getListTableIndexesSql());
romanb's avatar
romanb committed
119 120 121 122 123 124 125 126 127 128
    }

    /**
     * Lists tables.
     *
     * @param string|null $database
     * @return array
     */
    public function listTables($database = null)
    {
129 130
        return $this->_conn->fetchColumn($this->_conn->getDatabasePlatform()
                ->getListTablesSql());
romanb's avatar
romanb committed
131 132 133 134 135 136 137 138 139
    }

    /**
     * Lists database users.
     *
     * @return array
     */
    public function listUsers()
    {
140 141
        return $this->_conn->fetchColumn($this->_conn->getDatabasePlatform()
                ->getListUsersSql());
romanb's avatar
romanb committed
142 143 144 145 146 147 148 149 150 151
    }

    /**
     * Lists database views.
     *
     * @param string|null $database
     * @return array
     */
    public function listViews($database = null)
    {
152 153
        return $this->_conn->fetchColumn($this->_conn->getDatabasePlatform()
                ->getListViewsSql());
romanb's avatar
romanb committed
154 155 156 157 158 159 160 161 162 163 164
    }

    /**
     * drop an existing database
     * (this method is implemented by the drivers)
     *
     * @param string $name name of the database that should be dropped
     * @return void
     */
    public function dropDatabase($database)
    {
165 166
        $this->_conn->execute($this->_conn->getDatabasePlatform()
                ->getDropDatabaseSql($database));
romanb's avatar
romanb committed
167 168 169 170 171 172 173 174 175 176 177
    }

    /**
     * dropTable
     * drop an existing table
     *
     * @param string $table           name of table that should be dropped from the database
     * @return void
     */
    public function dropTable($table)
    {
178 179
        $this->_conn->execute($this->_conn->getDatabasePlatform()
                ->getDropTableSql($table));
romanb's avatar
romanb committed
180 181 182 183 184 185 186 187 188 189 190
    }

    /**
     * drop existing index
     *
     * @param string    $table        name of table that should be used in method
     * @param string    $name         name of the index to be dropped
     * @return void
     */
    public function dropIndex($table, $name)
    {
191 192
        return $this->_conn->exec($this->_conn->getDatabasePlatform()
                ->getDropIndexSql($table, $name));
romanb's avatar
romanb committed
193 194 195 196 197 198 199 200 201 202 203 204
    }

    /**
     * drop existing constraint
     *
     * @param string    $table        name of table that should be used in method
     * @param string    $name         name of the constraint to be dropped
     * @param string    $primary      hint if the constraint is primary
     * @return void
     */
    public function dropConstraint($table, $name, $primary = false)
    {
205 206
        $table = $this->_conn->getDatabasePlatform()->quoteIdentifier($table);
        $name = $this->_conn->getDatabasePlatform()->quoteIdentifier($name);
romanb's avatar
romanb committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
        
        return $this->_conn->exec('ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $name);
    }

    /**
     * drop existing foreign key
     *
     * @param string    $table        name of table that should be used in method
     * @param string    $name         name of the foreign key to be dropped
     * @return void
     */
    public function dropForeignKey($table, $name)
    {
        return $this->dropConstraint($table, $name);
    }

    /**
     * drop existing sequence
     * (this method is implemented by the drivers)
     *
     * @throws Doctrine_Connection_Exception     if something fails at database level
     * @param string $sequenceName      name of the sequence to be dropped
     * @return void
     */
    public function dropSequence($sequenceName)
    {
233
        $this->_conn->exec($this->_conn->getDatabasePlatform()->getDropSequenceSql($sequenceName));
romanb's avatar
romanb committed
234 235 236 237 238 239 240 241 242 243 244
    }

    /**
     * create a new database
     * (this method is implemented by the drivers)
     *
     * @param string $name name of the database that should be created
     * @return void
     */
    public function createDatabase($database)
    {
245
        $this->_conn->execute($this->_conn->getDatabasePlatform()->getCreateDatabaseSql($database));
romanb's avatar
romanb committed
246 247 248 249 250 251 252 253 254 255 256 257
    }

    /**
     * create a new table
     *
     * @param string $name   Name of the database that should be created
     * @param array $fields  Associative array that contains the definition of each field of the new table
     * @param array $options  An associative array of table options:
     * @see Doctrine_Export::createTableSql()
     *
     * @return void
     */
258
    public function createTable($name, array $columns, array $options = array())
romanb's avatar
romanb committed
259 260 261 262
    {
        // Build array of the primary keys if any of the individual field definitions
        // specify primary => true
        $count = 0;
263 264
        foreach ($columns as $columnName => $definition) {
            if (isset($definition['primary']) && $definition['primary']) {
romanb's avatar
romanb committed
265 266 267 268
                if ($count == 0) {
                    $options['primary'] = array();
                }
                $count++;
269
                $options['primary'][] = $columnName;
romanb's avatar
romanb committed
270 271 272
            }
        }

273 274
        $sql = (array) $this->_conn->getDatabasePlatform()->getCreateTableSql(
                $name, $columns, $options);
romanb's avatar
romanb committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296

        foreach ($sql as $query) {
            $this->_conn->execute($query);
        }
    }

    /**
     * create sequence
     *
     * @throws Doctrine_Connection_Exception     if something fails at database level
     * @param string    $seqName        name of the sequence to be created
     * @param string    $start          start value of the sequence; default is 1
     * @param array     $options  An associative array of table options:
     *                          array(
     *                              'comment' => 'Foo',
     *                              'charset' => 'utf8',
     *                              'collate' => 'utf8_unicode_ci',
     *                          );
     * @return void
     */
    public function createSequence($seqName, $start = 1, array $options = array())
    {
297 298
        return $this->_conn->execute($this->_conn->getDatabasePlatform()
                ->getCreateSequenceSql($seqName, $start, $options));
romanb's avatar
romanb committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
    }

    /**
     * create a constraint on a table
     *
     * @param string    $table         name of the table on which the constraint is to be created
     * @param string    $name          name of the constraint to be created
     * @param array     $definition    associative array that defines properties of the constraint to be created.
     *                                 Currently, only one property named FIELDS is supported. This property
     *                                 is also an associative with the names of the constraint fields as array
     *                                 constraints. Each entry of this array is set to another type of associative
     *                                 array that specifies properties of the constraint that are specific to
     *                                 each field.
     *
     *                                 Example
     *                                    array(
     *                                        'fields' => array(
     *                                            'user_name' => array(),
     *                                            'last_login' => array()
     *                                        )
     *                                    )
     * @return void
     */
    public function createConstraint($table, $name, $definition)
    {
324
        $sql = $this->_conn->getDatabasePlatform()->getCreateConstraintSql($table, $name, $definition);
romanb's avatar
romanb committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
        return $this->_conn->exec($sql);
    }

    /**
     * Get the stucture of a field into an array
     *
     * @param string    $table         name of the table on which the index is to be created
     * @param string    $name          name of the index to be created
     * @param array     $definition    associative array that defines properties of the index to be created.
     *                                 Currently, only one property named FIELDS is supported. This property
     *                                 is also an associative with the names of the index fields as array
     *                                 indexes. Each entry of this array is set to another type of associative
     *                                 array that specifies properties of the index that are specific to
     *                                 each field.
     *
     *                                 Currently, only the sorting property is supported. It should be used
     *                                 to define the sorting direction of the index. It may be set to either
     *                                 ascending or descending.
     *
     *                                 Not all DBMS support index sorting direction configuration. The DBMS
     *                                 drivers of those that do not support it ignore this property. Use the
     *                                 function supports() to determine whether the DBMS driver can manage indexes.
     *
     *                                 Example
     *                                    array(
     *                                        'fields' => array(
     *                                            'user_name' => array(
     *                                                'sorting' => 'ascending'
     *                                            ),
     *                                            'last_login' => array()
     *                                        )
     *                                    )
     * @return void
     */
    public function createIndex($table, $name, array $definition)
    {
361 362
        return $this->_conn->execute($this->_conn->getDatabasePlatform()
                ->getCreateIndexSql($table, $name, $definition));
romanb's avatar
romanb committed
363 364 365 366 367 368 369 370 371 372 373
    }

    /**
     * createForeignKey
     *
     * @param string    $table         name of the table on which the foreign key is to be created
     * @param array     $definition    associative array that defines properties of the foreign key to be created.
     * @return string
     */
    public function createForeignKey($table, array $definition)
    {
374
        $sql = $this->_conn->getDatabasePlatform()->getCreateForeignKeySql($table, $definition);
romanb's avatar
romanb committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
        return $this->_conn->execute($sql);
    }

    /**
     * alter an existing table
     * (this method is implemented by the drivers)
     *
     * @param string $name         name of the table that is intended to be changed.
     * @param array $changes     associative array that contains the details of each type
     *                             of change that is intended to be performed. The types of
     *                             changes that are currently supported are defined as follows:
     *
     *                             name
     *
     *                                New name for the table.
     *
     *                            add
     *
     *                                Associative array with the names of fields to be added as
     *                                 indexes of the array. The value of each entry of the array
     *                                 should be set to another associative array with the properties
     *                                 of the fields to be added. The properties of the fields should
     *                                 be the same as defined by the MDB2 parser.
     *
     *
     *                            remove
     *
     *                                Associative array with the names of fields to be removed as indexes
     *                                 of the array. Currently the values assigned to each entry are ignored.
     *                                 An empty array should be used for future compatibility.
     *
     *                            rename
     *
     *                                Associative array with the names of fields to be renamed as indexes
     *                                 of the array. The value of each entry of the array should be set to
     *                                 another associative array with the entry named name with the new
     *                                 field name and the entry named Declaration that is expected to contain
     *                                 the portion of the field declaration already in DBMS specific SQL code
     *                                 as it is used in the CREATE TABLE statement.
     *
     *                            change
     *
     *                                Associative array with the names of the fields to be changed as indexes
     *                                 of the array. Keep in mind that if it is intended to change either the
     *                                 name of a field and any other properties, the change array entries
     *                                 should have the new names of the fields as array indexes.
     *
     *                                The value of each entry of the array should be set to another associative
     *                                 array with the properties of the fields to that are meant to be changed as
     *                                 array entries. These entries should be assigned to the new values of the
     *                                 respective properties. The properties of the fields should be the same
     *                                 as defined by the MDB2 parser.
     *
     *                            Example
     *                                array(
     *                                    'name' => 'userlist',
     *                                    'add' => array(
     *                                        'quota' => array(
     *                                            'type' => 'integer',
     *                                            'unsigned' => 1
     *                                        )
     *                                    ),
     *                                    'remove' => array(
     *                                        'file_limit' => array(),
     *                                        'time_limit' => array()
     *                                    ),
     *                                    'change' => array(
     *                                        'name' => array(
     *                                            'length' => '20',
     *                                            'definition' => array(
     *                                                'type' => 'text',
     *                                                'length' => 20,
     *                                            ),
     *                                        )
     *                                    ),
     *                                    'rename' => array(
     *                                        'sex' => array(
     *                                            'name' => 'gender',
     *                                            'definition' => array(
     *                                                'type' => 'text',
     *                                                'length' => 1,
     *                                                'default' => 'M',
     *                                            ),
     *                                        )
     *                                    )
     *                                )
     *
     * @param boolean $check     indicates whether the function should just check if the DBMS driver
     *                             can perform the requested table alterations if the value is true or
     *                             actually perform them otherwise.
     * @return void
     */
    public function alterTable($name, array $changes, $check = false)
    {
469
        $sql = $this->_conn->getDatabasePlatform()->getAlterTableSql($name, $changes, $check);
romanb's avatar
romanb committed
470 471 472 473 474 475 476 477
        
        if (is_string($sql) && $sql) {
            $this->_conn->execute($sql);
        }
    }
}

?>