Mysql.php 26.5 KB
Newer Older
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.com>.
 */
Doctrine::autoload('Doctrine_Export');
/**
 * Doctrine_Export_Mysql
 *
zYne's avatar
zYne committed
25 26
 * @package     Doctrine
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
27
 * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
zYne's avatar
zYne committed
28 29 30 31 32 33
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 */
lsmith's avatar
lsmith committed
34 35
class Doctrine_Export_Mysql extends Doctrine_Export
{
36 37 38 39
   /**
     * create a new database
     *
     * @param string $name name of the database that should be created
zYne's avatar
zYne committed
40
     * @return string
41
     */
zYne's avatar
zYne committed
42
    public function createDatabaseSql($name)
lsmith's avatar
lsmith committed
43
    {
zYne's avatar
zYne committed
44
        return 'CREATE DATABASE ' . $this->conn->quoteIdentifier($name, true);
45 46 47 48 49
    }
    /**
     * drop an existing database
     *
     * @param string $name name of the database that should be dropped
zYne's avatar
zYne committed
50
     * @return string
51
     */
zYne's avatar
zYne committed
52
    public function dropDatabaseSql($name)
lsmith's avatar
lsmith committed
53
    {
zYne's avatar
zYne committed
54
        return 'DROP DATABASE ' . $this->conn->quoteIdentifier($name);
55
    }
zYne's avatar
zYne committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    /**
     * 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
     *                       The indexes of the array entries are the names of the fields of the table an
     *                       the array entry values are associative arrays like those that are meant to be
     *                       passed with the field definitions to get[Type]Declaration() functions.
     *                          array(
     *                              'id' => array(
     *                                  'type' => 'integer',
     *                                  'unsigned' => 1
     *                                  'notnull' => 1
     *                                  'default' => 0
     *                              ),
     *                              'name' => array(
     *                                  'type' => 'text',
     *                                  'length' => 12
     *                              ),
     *                              'password' => array(
     *                                  'type' => 'text',
     *                                  'length' => 12
     *                              )
     *                          );
     * @param array $options  An associative array of table options:
     *                          array(
     *                              'comment' => 'Foo',
     *                              'charset' => 'utf8',
     *                              'collate' => 'utf8_unicode_ci',
     *                              'type'    => 'innodb',
     *                          );
     *
     * @return void
     */
    public function createTableSql($name, array $fields, array $options = array()) 
    {
lsmith's avatar
lsmith committed
92
        if ( ! $name)
zYne's avatar
zYne committed
93
            throw new Doctrine_Export_Exception('no valid table name specified');
zYne's avatar
zYne committed
94

lsmith's avatar
lsmith committed
95
        if (empty($fields)) {
zYne's avatar
zYne committed
96
            throw new Doctrine_Export_Exception('no fields specified for table "'.$name.'"');
lsmith's avatar
lsmith committed
97
        }
zYne's avatar
zYne committed
98
        $queryFields = $this->getFieldDeclarationList($fields);
99

zYne's avatar
zYne committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
        // build indexes for all foreign key fields (needed in MySQL!!)
        if (isset($options['foreignKeys'])) {
            foreach ($options['foreignKeys'] as $fk) {
                $local = $fk['local'];
                
                $found = false;
                if (isset($options['indexes'])) {
                    foreach ($options['indexes'] as $definition) {
                        if (isset($definition['fields'][$local]) && count($definition['fields']) === 1) {
                            $found = true;
                        }
                    }
                }
                
                if ( ! $found) {
zYne's avatar
zYne committed
115
                    $options['indexes'][$local] = array('fields' => array($local => array()));
zYne's avatar
zYne committed
116 117 118
                }
            }
        }
zYne's avatar
zYne committed
119

zYne's avatar
zYne committed
120
        // add all indexes
zYne's avatar
zYne committed
121 122 123 124 125
        if (isset($options['indexes']) && ! empty($options['indexes'])) {
            foreach($options['indexes'] as $index => $definition) {
                $queryFields .= ', ' . $this->getIndexDeclaration($index, $definition);
            }
        }
126

zYne's avatar
zYne committed
127
        // attach all primary keys
zYne's avatar
zYne committed
128 129 130 131
        if (isset($options['primary']) && ! empty($options['primary'])) {
            $queryFields .= ', PRIMARY KEY(' . implode(', ', array_values($options['primary'])) . ')';
        }

zYne's avatar
zYne committed
132
        $name  = $this->conn->quoteIdentifier($name, true);
zYne's avatar
zYne committed
133
        $query = 'CREATE TABLE ' . $name . ' (' . $queryFields . ')';
134

zYne's avatar
zYne committed
135 136
        $optionStrings = array();

lsmith's avatar
lsmith committed
137
        if (isset($options['comment'])) {
138
            $optionStrings['comment'] = 'COMMENT = ' . $this->dbh->quote($options['comment'], 'text');
lsmith's avatar
lsmith committed
139 140
        }
        if (isset($options['charset'])) {
fabien's avatar
fabien committed
141
            $optionStrings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
lsmith's avatar
lsmith committed
142
            if (isset($options['collate'])) {
zYne's avatar
zYne committed
143
                $optionStrings['charset'] .= ' COLLATE ' . $options['collate'];
144 145 146 147
            }
        }

        $type = false;
zYne's avatar
zYne committed
148

zYne's avatar
zYne committed
149
        // get the type of the table
zYne's avatar
zYne committed
150
        if (isset($options['type'])) {
151
            $type = $options['type'];
152 153
        } else {
            $type = $this->conn->getAttribute(Doctrine::ATTR_DEFAULT_TABLE_TYPE);
154
        }
lsmith's avatar
lsmith committed
155

156
        if ($type) {
157
            $optionStrings[] = 'ENGINE = ' . $type;
158 159
        }

zYne's avatar
zYne committed
160 161
        if (!empty($optionStrings)) {
            $query.= ' '.implode(' ', $optionStrings);
162
        }
zYne's avatar
zYne committed
163 164 165 166 167 168
        $sql[] = $query;

        if (isset($options['foreignKeys'])) {

            foreach ((array) $options['foreignKeys'] as $k => $definition) {
                if (is_array($definition)) {
zYne's avatar
zYne committed
169
                    $sql[] = $this->createForeignKeySql($name, $definition);
zYne's avatar
zYne committed
170 171 172 173
                }
            }
        }   
        return $sql;
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    }
    /**
     * alter an existing table
     *
     * @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 Metabase 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 Metabase 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
zYne's avatar
zYne committed
259 260
     *                           can perform the requested table alterations if the value is true or
     *                           actually perform them otherwise.
261 262
     * @return boolean
     */
zYne's avatar
zYne committed
263
    public function alterTableSql($name, array $changes, $check)
lsmith's avatar
lsmith committed
264
    {
zYne's avatar
zYne committed
265
        if ( ! $name) {
zYne's avatar
zYne committed
266
            throw new Doctrine_Export_Exception('no valid table name specified');
zYne's avatar
zYne committed
267
        }
zYne's avatar
zYne committed
268 269
        foreach ($changes as $changeName => $change) {
            switch ($changeName) {
270 271 272 273 274
                case 'add':
                case 'remove':
                case 'change':
                case 'rename':
                case 'name':
275
                    break;
276
                default:
zYne's avatar
zYne committed
277
                    throw new Doctrine_Export_Exception('change type "' . $changeName . '" not yet supported');
278 279 280
            }
        }

lsmith's avatar
lsmith committed
281
        if ($check) {
282 283 284 285
            return true;
        }

        $query = '';
lsmith's avatar
lsmith committed
286
        if ( ! empty($changes['name'])) {
zYne's avatar
zYne committed
287
            $change_name = $this->conn->quoteIdentifier($changes['name']);
288 289 290
            $query .= 'RENAME TO ' . $change_name;
        }

lsmith's avatar
lsmith committed
291
        if ( ! empty($changes['add']) && is_array($changes['add'])) {
zYne's avatar
zYne committed
292
            foreach ($changes['add'] as $fieldName => $field) {
293 294 295
                if ($query) {
                    $query.= ', ';
                }
zYne's avatar
zYne committed
296
                $query.= 'ADD ' . $this->getDeclaration($field['type'], $fieldName, $field);
297 298 299
            }
        }

lsmith's avatar
lsmith committed
300
        if ( ! empty($changes['remove']) && is_array($changes['remove'])) {
zYne's avatar
zYne committed
301
            foreach ($changes['remove'] as $fieldName => $field) {
302
                if ($query) {
zYne's avatar
zYne committed
303
                    $query .= ', ';
304
                }
zYne's avatar
zYne committed
305 306
                $fieldName = $this->conn->quoteIdentifier($fieldName);
                $query .= 'DROP ' . $fieldName;
307 308 309 310
            }
        }

        $rename = array();
lsmith's avatar
lsmith committed
311
        if ( ! empty($changes['rename']) && is_array($changes['rename'])) {
zYne's avatar
zYne committed
312 313
            foreach ($changes['rename'] as $fieldName => $field) {
                $rename[$field['name']] = $fieldName;
314 315 316
            }
        }

lsmith's avatar
lsmith committed
317
        if ( ! empty($changes['change']) && is_array($changes['change'])) {
zYne's avatar
zYne committed
318
            foreach ($changes['change'] as $fieldName => $field) {
319 320 321
                if ($query) {
                    $query.= ', ';
                }
zYne's avatar
zYne committed
322 323 324
                if (isset($rename[$fieldName])) {
                    $oldFieldName = $rename[$fieldName];
                    unset($rename[$fieldName]);
325
                } else {
zYne's avatar
zYne committed
326
                    $oldFieldName = $fieldName;
327
                }
gnat's avatar
gnat committed
328
                $oldFieldName = $this->conn->quoteIdentifier($oldFieldName, true);
zYne's avatar
zYne committed
329 330
                $query .= 'CHANGE ' . $oldFieldName . ' ' 
                        . $this->getDeclaration($field['definition']['type'], $fieldName, $field['definition']);
331 332 333
            }
        }

lsmith's avatar
lsmith committed
334
        if ( ! empty($rename) && is_array($rename)) {
zYne's avatar
zYne committed
335
            foreach ($rename as $renameName => $renamedField) {
336 337 338
                if ($query) {
                    $query.= ', ';
                }
zYne's avatar
zYne committed
339 340 341 342
                $field = $changes['rename'][$renamedField];
                $renamedField = $this->conn->quoteIdentifier($renamedField, true);
                $query .= 'CHANGE ' . $renamedField . ' '
                        . $this->getDeclaration($field['definition']['type'], $field['name'], $field['definition']);
343 344 345
            }
        }

lsmith's avatar
lsmith committed
346
        if ( ! $query) {
lsmith's avatar
lsmith committed
347
            return false;
348 349 350
        }

        $name = $this->conn->quoteIdentifier($name, true);
351
        return $this->conn->exec('ALTER TABLE ' . $name . ' ' . $query);
352 353 354 355
    }
    /**
     * create sequence
     *
zYne's avatar
zYne committed
356 357
     * @param string    $sequenceName name of the sequence to be created
     * @param string    $start        start value of the sequence; default is 1
zYne's avatar
zYne committed
358 359 360 361 362 363 364
     * @param array     $options  An associative array of table options:
     *                          array(
     *                              'comment' => 'Foo',
     *                              'charset' => 'utf8',
     *                              'collate' => 'utf8_unicode_ci',
     *                              'type'    => 'innodb',
     *                          );
zYne's avatar
zYne committed
365
     * @return boolean
366
     */
zYne's avatar
zYne committed
367
    public function createSequence($sequenceName, $start = 1, array $options = array())
lsmith's avatar
lsmith committed
368
    {
gnat's avatar
gnat committed
369
        $sequenceName   = $this->conn->quoteIdentifier($this->conn->getSequenceName($sequenceName), true);
zYne's avatar
zYne committed
370 371
        $seqcolName     = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true);

zYne's avatar
zYne committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
        $optionsStrings = array();

        if (isset($options['comment']) && ! empty($options['comment'])) {
            $optionsStrings['comment'] = 'COMMENT = ' . $this->conn->quote($options['comment'], 'string');
        }

        if (isset($options['charset']) && ! empty($options['charset'])) {
            $optionsStrings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];

            if (isset($options['collate'])) {
                $optionsStrings['collate'] .= ' COLLATE ' . $options['collate'];
            }
        }

        $type = false;

        if (isset($options['type'])) {
            $type = $options['type'];
        } else {
            $type = $this->conn->default_table_type;
        }
        if ($type) {
            $optionsStrings[] = 'ENGINE = ' . $type;
        }


398 399 400 401 402 403
        try {
            $query  = 'CREATE TABLE ' . $sequenceName
                    . ' (' . $seqcolName . ' INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ('
                    . $seqcolName . '))'
                    . strlen($this->conn->default_table_type) ? ' TYPE = '
                    . $this->conn->default_table_type : '';
zYne's avatar
zYne committed
404

405 406 407 408
            $res    = $this->conn->exec($query);
        } catch(Doctrine_Connection_Exception $e) {
            throw new Doctrine_Export_Exception('could not create sequence table');
        }
409

zYne's avatar
zYne committed
410 411 412 413
        if ($start == 1)
            return true;

        $query  = 'INSERT INTO ' . $sequenceName
zYne's avatar
zYne committed
414
                . ' (' . $seqcolName . ') VALUES (' . ($start - 1) . ')';
415

lsmith's avatar
lsmith committed
416
        $res    = $this->conn->exec($query);
417 418

        // Handle error
zYne's avatar
zYne committed
419
        try {
lsmith's avatar
lsmith committed
420
            $result = $this->conn->exec('DROP TABLE ' . $sequenceName);
421
        } catch(Doctrine_Connection_Exception $e) {
zYne's avatar
zYne committed
422
            throw new Doctrine_Export_Exception('could not drop inconsistent sequence table');
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
    }
    /**
     * Get the stucture of a field into an array
     *
     * @author Leoncx
     * @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(
zYne's avatar
zYne committed
452
     *                                                'sorting' => 'ASC'
453 454 455 456 457 458 459 460
     *                                                'length' => 10
     *                                            ),
     *                                            'last_login' => array()
     *                                        )
     *                                    )
     * @throws PDOException
     * @return void
     */
zYne's avatar
zYne committed
461
    public function createIndexSql($table, $name, array $definition)
lsmith's avatar
lsmith committed
462
    {
463
        $table  = $table;
464
        $name   = $this->conn->getIndexName($name);
zYne's avatar
zYne committed
465
        $type   = '';
zYne's avatar
zYne committed
466
        if (isset($definition['type'])) {
zYne's avatar
zYne committed
467
            switch (strtolower($definition['type'])) {
zYne's avatar
zYne committed
468 469
                case 'fulltext':
                case 'unique':
zYne's avatar
zYne committed
470
                    $type = strtoupper($definition['type']) . ' ';
zYne's avatar
zYne committed
471 472
                break;
                default:
zYne's avatar
zYne committed
473
                    throw new Doctrine_Export_Exception('Unknown index type ' . $definition['type']);
zYne's avatar
zYne committed
474 475 476
            }
        }
        $query  = 'CREATE ' . $type . 'INDEX ' . $name . ' ON ' . $table;
zYne's avatar
zYne committed
477
        $query .= ' (' . $this->getIndexFieldDeclarationList() . ')';
zYne's avatar
zYne committed
478

zYne's avatar
zYne committed
479 480
        return $query;
    }
zYne's avatar
zYne committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
    /** 
     * getDefaultDeclaration
     * Obtain DBMS specific SQL code portion needed to set a default value
     * declaration to be used in statements like CREATE TABLE.
     *
     * @param array $field      field definition array
     * @return string           DBMS specific SQL code portion needed to set a default value
     */
    public function getDefaultFieldDeclaration($field)
    {
        $default = '';
        if (isset($field['default']) && $field['length'] <= 255) {
            if ($field['default'] === '') {
                $field['default'] = empty($field['notnull'])
                    ? null : $this->valid_default_values[$field['type']];

                if ($field['default'] === ''
                    && ($conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_EMPTY_TO_NULL)
                ) {
                    $field['default'] = ' ';
                }
            }
    
            $default = ' DEFAULT ' . $this->conn->quote($field['default'], $field['type']);
        }
        return $default;
    }
zYne's avatar
zYne committed
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
    /**
     * Obtain DBMS specific SQL code portion needed to set an index 
     * declaration to be used in statements like CREATE TABLE.
     *
     * @param string $charset       name of the index
     * @param array $definition     index definition
     * @return string  DBMS specific SQL code portion needed to set an index
     */
    public function getIndexDeclaration($name, array $definition)
    {
        $name   = $this->conn->quoteIdentifier($name);
        $type   = '';
        if(isset($definition['type'])) {
            switch (strtolower($definition['type'])) {
                case 'fulltext':
                case 'unique':
                    $type = strtoupper($definition['type']) . ' ';
                break;
                default:
                    throw new Doctrine_Export_Exception('Unknown index type ' . $definition['type']);
528 529
            }
        }
zYne's avatar
zYne committed
530
        
531
        if ( ! isset($definition['fields'])) {
zYne's avatar
zYne committed
532 533
            throw new Doctrine_Export_Exception('No index columns given.');
        }
534 535 536
        if ( ! is_array($definition['fields'])) {
            $definition['fields'] = array($definition['fields']);
        }
zYne's avatar
zYne committed
537

zYne's avatar
zYne committed
538
        $query = $type . 'INDEX ' . $this->conn->formatter->getIndexName($name);
539

zYne's avatar
zYne committed
540 541
        $query .= ' (' . $this->getIndexFieldDeclarationList($definition['fields']) . ')';
        
zYne's avatar
zYne committed
542
        return $query;
543
    }
zYne's avatar
zYne committed
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
    /**
     * getIndexFieldDeclarationList
     * Obtain DBMS specific SQL code portion needed to set an index
     * declaration to be used in statements like CREATE TABLE.
     *
     * @return string
     */
    public function getIndexFieldDeclarationList(array $fields)
    {
    	$declFields = array();

        foreach ($fields as $fieldName => $field) {
            $fieldString = $fieldName;

            if (is_array($field)) {
                if (isset($field['length'])) {
                    $fieldString .= '(' . $field['length'] . ')';
                }

                if (isset($field['sorting'])) {
                    $sort = strtoupper($field['sorting']);
                    switch ($sort) {
                        case 'ASC':
                        case 'DESC':
                            $fieldString .= ' ' . $sort;
                            break;
                        default:
                            throw new Doctrine_Export_Exception('Unknown index sorting option given.');
                    }
                }
            } else {
                $fieldString = $field;
            }
            $declFields[] = $fieldString;
        }
        return implode(', ', $declFields);
    }
581 582 583 584 585 586 587 588
    /**
     * getAdvancedForeignKeyOptions
     * Return the FOREIGN KEY query section dealing with non-standard options
     * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
     *
     * @param array $definition
     * @return string
     */
zYne's avatar
zYne committed
589
    public function getAdvancedForeignKeyOptions(array $definition)
590 591 592 593 594
    {
        $query = '';
        if (!empty($definition['match'])) {
            $query .= ' MATCH ' . $definition['match'];
        }
zYne's avatar
zYne committed
595
        if (!empty($definition['onUpdate'])) {
zYne's avatar
zYne committed
596
            $query .= ' ON UPDATE ' . $this->getForeignKeyReferentialAction($definition['onUpdate']);
597
        }
zYne's avatar
zYne committed
598
        if (!empty($definition['onDelete'])) {
zYne's avatar
zYne committed
599
            $query .= ' ON DELETE ' . $this->getForeignKeyReferentialAction($definition['onDelete']);
600 601 602
        }
        return $query;
    }
603 604 605 606 607
    /**
     * 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
zYne's avatar
zYne committed
608
     * @return void
609
     */
zYne's avatar
zYne committed
610
    public function dropIndexSql($table, $name)
lsmith's avatar
lsmith committed
611
    {
612
        $table  = $this->conn->quoteIdentifier($table, true);
613
        $name   = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name), true);
zYne's avatar
zYne committed
614
        return 'DROP INDEX ' . $name . ' ON ' . $table;
615 616 617 618 619 620
    }
    /**
     * dropTable
     *
     * @param string    $table          name of table that should be dropped from the database
     * @throws PDOException
zYne's avatar
zYne committed
621
     * @return void
622
     */
zYne's avatar
zYne committed
623
    public function dropTableSql($table)
lsmith's avatar
lsmith committed
624
    {
zYne's avatar
zYne committed
625
        $table  = $this->conn->quoteIdentifier($table, true);
zYne's avatar
zYne committed
626
        return 'DROP TABLE ' . $table;
627 628
    }
}
lsmith's avatar
lsmith committed
629