Mysql.php 25.8 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 40 41 42
   /**
     * create a new database
     *
     * @param string $name name of the database that should be created
     * @throws PDOException
     * @return void
     */
lsmith's avatar
lsmith committed
43 44
    public function createDatabase($name)
    {
45
        $query  = 'CREATE DATABASE ' . $this->conn->quoteIdentifier($name, true);
zYne's avatar
zYne committed
46
        $result = $this->conn->exec($query);
47 48 49 50 51 52 53 54
    }
    /**
     * drop an existing database
     *
     * @param string $name name of the database that should be dropped
     * @throws PDOException
     * @access public
     */
lsmith's avatar
lsmith committed
55 56
    public function dropDatabase($name)
    {
57
        $query  = 'DROP DATABASE ' . $this->conn->quoteIdentifier($name);
zYne's avatar
zYne committed
58
        $this->conn->exec($query);
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
    }
    /**
     * 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',
zYne's avatar
zYne committed
87
     *                              'charset' => 'utf8',
88 89 90 91
     *                              'collate' => 'utf8_unicode_ci',
     *                              'type'    => 'innodb',
     *                          );
     *
zYne's avatar
zYne committed
92
     * @return void
93
     */
zYne's avatar
zYne committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    public function createTable($name, array $fields, array $options = array()) 
    {
    	$sql = $this->createTableSql($name, $fields, $options);

        $this->conn->exec('SET FOREIGN_KEY_CHECKS = 0');	
        
        $this->conn->execute($sql);
    
        $this->conn->exec('SET FOREIGN_KEY_CHECKS = 1');
    }
    /**
     * 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
140
        if ( ! $name)
zYne's avatar
zYne committed
141
            throw new Doctrine_Export_Exception('no valid table name specified');
zYne's avatar
zYne committed
142

lsmith's avatar
lsmith committed
143
        if (empty($fields)) {
zYne's avatar
zYne committed
144
            throw new Doctrine_Export_Exception('no fields specified for table "'.$name.'"');
lsmith's avatar
lsmith committed
145
        }
zYne's avatar
zYne committed
146
        $queryFields = $this->getFieldDeclarationList($fields);
147

zYne's avatar
zYne committed
148 149 150 151 152
        if (isset($options['indexes']) && ! empty($options['indexes'])) {
            foreach($options['indexes'] as $index => $definition) {
                $queryFields .= ', ' . $this->getIndexDeclaration($index, $definition);
            }
        }
153 154 155 156 157 158 159

        if (isset($options['foreignKeys']) && ! empty($options['foreignKeys'])) {
            foreach($options['foreignKeys'] as $definition) {
                $queryFields .= ', ' . $this->getForeignKeyDeclaration($definition);
            }
        }

zYne's avatar
zYne committed
160 161 162 163 164
        if (isset($options['primary']) && ! empty($options['primary'])) {
            $queryFields .= ', PRIMARY KEY(' . implode(', ', array_values($options['primary'])) . ')';
        }


zYne's avatar
zYne committed
165

zYne's avatar
zYne committed
166
        $name  = $this->conn->quoteIdentifier($name, true);
zYne's avatar
zYne committed
167
        $query = 'CREATE TABLE ' . $name . ' (' . $queryFields . ')';
168

zYne's avatar
zYne committed
169 170
        $optionStrings = array();

lsmith's avatar
lsmith committed
171
        if (isset($options['comment'])) {
172
            $optionStrings['comment'] = 'COMMENT = ' . $this->dbh->quote($options['comment'], 'text');
lsmith's avatar
lsmith committed
173 174
        }
        if (isset($options['charset'])) {
fabien's avatar
fabien committed
175
            $optionStrings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
lsmith's avatar
lsmith committed
176
            if (isset($options['collate'])) {
177
                $optionStrings['charset'].= ' COLLATE ' . $options['collate'];
178 179 180 181
            }
        }

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

183 184
        if (!empty($options['type'])) {
            $type = $options['type'];
185 186
        } else {
            $type = $this->conn->getAttribute(Doctrine::ATTR_DEFAULT_TABLE_TYPE);
187
        }
lsmith's avatar
lsmith committed
188

189
        if ($type) {
190
            $optionStrings[] = 'ENGINE = ' . $type;
191 192
        }

zYne's avatar
zYne committed
193 194
        if (!empty($optionStrings)) {
            $query.= ' '.implode(' ', $optionStrings);
195
        }
zYne's avatar
zYne committed
196
        return $query;
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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    }
    /**
     * 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
282 283
     *                           can perform the requested table alterations if the value is true or
     *                           actually perform them otherwise.
284 285
     * @return boolean
     */
zYne's avatar
zYne committed
286
    public function alterTableSql($name, array $changes, $check)
lsmith's avatar
lsmith committed
287
    {
zYne's avatar
zYne committed
288
        if ( ! $name) {
zYne's avatar
zYne committed
289
            throw new Doctrine_Export_Exception('no valid table name specified');
zYne's avatar
zYne committed
290
        }
zYne's avatar
zYne committed
291 292
        foreach ($changes as $changeName => $change) {
            switch ($changeName) {
293 294 295 296 297
                case 'add':
                case 'remove':
                case 'change':
                case 'rename':
                case 'name':
298
                    break;
299
                default:
zYne's avatar
zYne committed
300
                    throw new Doctrine_Export_Exception('change type "' . $changeName . '" not yet supported');
301 302 303
            }
        }

lsmith's avatar
lsmith committed
304
        if ($check) {
305 306 307 308
            return true;
        }

        $query = '';
lsmith's avatar
lsmith committed
309
        if ( ! empty($changes['name'])) {
zYne's avatar
zYne committed
310
            $change_name = $this->conn->quoteIdentifier($changes['name']);
311 312 313
            $query .= 'RENAME TO ' . $change_name;
        }

lsmith's avatar
lsmith committed
314
        if ( ! empty($changes['add']) && is_array($changes['add'])) {
zYne's avatar
zYne committed
315
            foreach ($changes['add'] as $fieldName => $field) {
316 317 318
                if ($query) {
                    $query.= ', ';
                }
zYne's avatar
zYne committed
319
                $query.= 'ADD ' . $this->getDeclaration($field['type'], $fieldName, $field);
320 321 322
            }
        }

lsmith's avatar
lsmith committed
323
        if ( ! empty($changes['remove']) && is_array($changes['remove'])) {
zYne's avatar
zYne committed
324
            foreach ($changes['remove'] as $fieldName => $field) {
325
                if ($query) {
zYne's avatar
zYne committed
326
                    $query .= ', ';
327
                }
zYne's avatar
zYne committed
328 329
                $fieldName = $this->conn->quoteIdentifier($fieldName);
                $query .= 'DROP ' . $fieldName;
330 331 332 333
            }
        }

        $rename = array();
lsmith's avatar
lsmith committed
334
        if ( ! empty($changes['rename']) && is_array($changes['rename'])) {
zYne's avatar
zYne committed
335 336
            foreach ($changes['rename'] as $fieldName => $field) {
                $rename[$field['name']] = $fieldName;
337 338 339
            }
        }

lsmith's avatar
lsmith committed
340
        if ( ! empty($changes['change']) && is_array($changes['change'])) {
zYne's avatar
zYne committed
341
            foreach ($changes['change'] as $fieldName => $field) {
342 343 344
                if ($query) {
                    $query.= ', ';
                }
zYne's avatar
zYne committed
345 346 347
                if (isset($rename[$fieldName])) {
                    $oldFieldName = $rename[$fieldName];
                    unset($rename[$fieldName]);
348
                } else {
zYne's avatar
zYne committed
349
                    $oldFieldName = $fieldName;
350
                }
gnat's avatar
gnat committed
351
                $oldFieldName = $this->conn->quoteIdentifier($oldFieldName, true);
zYne's avatar
zYne committed
352 353
                $query .= 'CHANGE ' . $oldFieldName . ' ' 
                        . $this->getDeclaration($field['definition']['type'], $fieldName, $field['definition']);
354 355 356
            }
        }

lsmith's avatar
lsmith committed
357
        if ( ! empty($rename) && is_array($rename)) {
zYne's avatar
zYne committed
358
            foreach ($rename as $renameName => $renamedField) {
359 360 361
                if ($query) {
                    $query.= ', ';
                }
zYne's avatar
zYne committed
362 363 364 365
                $field = $changes['rename'][$renamedField];
                $renamedField = $this->conn->quoteIdentifier($renamedField, true);
                $query .= 'CHANGE ' . $renamedField . ' '
                        . $this->getDeclaration($field['definition']['type'], $field['name'], $field['definition']);
366 367 368
            }
        }

lsmith's avatar
lsmith committed
369
        if ( ! $query) {
lsmith's avatar
lsmith committed
370
            return false;
371 372 373
        }

        $name = $this->conn->quoteIdentifier($name, true);
374
        return $this->conn->exec('ALTER TABLE ' . $name . ' ' . $query);
375 376 377 378
    }
    /**
     * create sequence
     *
zYne's avatar
zYne committed
379 380 381 382
     * @param string    $sequenceName name of the sequence to be created
     * @param string    $seqcol_name  the name of the sequence column
     * @param string    $start        start value of the sequence; default is 1
     * @return boolean
383
     */
zYne's avatar
zYne committed
384
    public function createSequence($sequenceName, $start = 1)
lsmith's avatar
lsmith committed
385
    {
gnat's avatar
gnat committed
386
        $sequenceName   = $this->conn->quoteIdentifier($this->conn->getSequenceName($sequenceName), true);
zYne's avatar
zYne committed
387 388
        $seqcolName     = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true);

389 390 391 392 393 394 395 396 397 398 399
        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 : '';
    
            $res    = $this->conn->exec($query);
        } catch(Doctrine_Connection_Exception $e) {
            throw new Doctrine_Export_Exception('could not create sequence table');
        }
400

zYne's avatar
zYne committed
401 402 403 404
        if ($start == 1)
            return true;

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

lsmith's avatar
lsmith committed
407
        $res    = $this->conn->exec($query);
408 409

        // Handle error
zYne's avatar
zYne committed
410
        try {
lsmith's avatar
lsmith committed
411
            $result = $this->conn->exec('DROP TABLE ' . $sequenceName);
412
        } catch(Doctrine_Connection_Exception $e) {
zYne's avatar
zYne committed
413
            throw new Doctrine_Export_Exception('could not drop inconsistent sequence table');
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
    }
    /**
     * 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
443
     *                                                'sorting' => 'ASC'
444 445 446 447 448 449 450 451
     *                                                'length' => 10
     *                                            ),
     *                                            'last_login' => array()
     *                                        )
     *                                    )
     * @throws PDOException
     * @return void
     */
zYne's avatar
zYne committed
452
    public function createIndexSql($table, $name, array $definition)
lsmith's avatar
lsmith committed
453
    {
454
        $table  = $table;
455
        $name   = $this->conn->getIndexName($name);
zYne's avatar
zYne committed
456
        $type   = '';
zYne's avatar
zYne committed
457
        if (isset($definition['type'])) {
zYne's avatar
zYne committed
458
            switch (strtolower($definition['type'])) {
zYne's avatar
zYne committed
459 460
                case 'fulltext':
                case 'unique':
zYne's avatar
zYne committed
461
                    $type = strtoupper($definition['type']) . ' ';
zYne's avatar
zYne committed
462 463
                break;
                default:
zYne's avatar
zYne committed
464
                    throw new Doctrine_Export_Exception('Unknown index type ' . $definition['type']);
zYne's avatar
zYne committed
465 466 467
            }
        }
        $query  = 'CREATE ' . $type . 'INDEX ' . $name . ' ON ' . $table;
zYne's avatar
zYne committed
468
        $query .= ' (' . $this->getIndexFieldDeclarationList() . ')';
zYne's avatar
zYne committed
469

zYne's avatar
zYne committed
470 471
        return $query;
    }
zYne's avatar
zYne committed
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
    /** 
     * 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
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
    /**
     * 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']);
519 520
            }
        }
zYne's avatar
zYne committed
521
        
522
        if ( ! isset($definition['fields'])) {
zYne's avatar
zYne committed
523 524
            throw new Doctrine_Export_Exception('No index columns given.');
        }
525 526 527
        if ( ! is_array($definition['fields'])) {
            $definition['fields'] = array($definition['fields']);
        }
zYne's avatar
zYne committed
528 529

        $query = $type . 'INDEX ' . $name;
530

zYne's avatar
zYne committed
531 532
        $query .= ' (' . $this->getIndexFieldDeclarationList($definition['fields']) . ')';
        
zYne's avatar
zYne committed
533
        return $query;
534
    }
zYne's avatar
zYne committed
535 536 537 538 539 540 541 542 543 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
    /**
     * 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);
    }
572 573 574 575 576
    /**
     * 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
577
     * @return void
578
     */
lsmith's avatar
lsmith committed
579 580
    public function dropIndex($table, $name)
    {
581
        $table  = $this->conn->quoteIdentifier($table, true);
582
        $name   = $this->conn->quoteIdentifier($this->conn->getIndexName($name), true);
zYne's avatar
zYne committed
583
        return $this->conn->exec('DROP INDEX ' . $name . ' ON ' . $table);
584 585 586 587 588 589
    }
    /**
     * dropTable
     *
     * @param string    $table          name of table that should be dropped from the database
     * @throws PDOException
zYne's avatar
zYne committed
590
     * @return void
591
     */
lsmith's avatar
lsmith committed
592 593
    public function dropTable($table)
    {
zYne's avatar
zYne committed
594
        $table  = $this->conn->quoteIdentifier($table, true);
zYne's avatar
zYne committed
595
        $this->conn->exec('DROP TABLE ' . $table);
596 597
    }
}
lsmith's avatar
lsmith committed
598