2006-10-20 00:56:59 +04:00
|
|
|
<?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
|
|
|
|
*
|
2006-11-17 17:59:57 +03:00
|
|
|
* @package Doctrine
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
2006-10-20 00:56:59 +04:00
|
|
|
* @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
|
2006-11-17 17:59:57 +03:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @category Object Relational Mapping
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision$
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
class Doctrine_Export_Mysql extends Doctrine_Export
|
|
|
|
{
|
2006-10-20 00:56:59 +04:00
|
|
|
/**
|
|
|
|
* create a new database
|
|
|
|
*
|
|
|
|
* @param string $name name of the database that should be created
|
2007-06-15 00:29:33 +04:00
|
|
|
* @return string
|
2006-10-20 00:56:59 +04:00
|
|
|
*/
|
2007-06-15 00:29:33 +04:00
|
|
|
public function createDatabaseSql($name)
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2007-06-15 00:29:33 +04:00
|
|
|
return 'CREATE DATABASE ' . $this->conn->quoteIdentifier($name, true);
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* drop an existing database
|
|
|
|
*
|
|
|
|
* @param string $name name of the database that should be dropped
|
2007-06-15 00:29:33 +04:00
|
|
|
* @return string
|
2006-10-20 00:56:59 +04:00
|
|
|
*/
|
2007-06-15 00:29:33 +04:00
|
|
|
public function dropDatabaseSql($name)
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2007-06-15 00:29:33 +04:00
|
|
|
return 'DROP DATABASE ' . $this->conn->quoteIdentifier($name);
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
2007-03-28 03:00:07 +04:00
|
|
|
/**
|
|
|
|
* 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())
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! $name)
|
2007-01-15 21:48:50 +03:00
|
|
|
throw new Doctrine_Export_Exception('no valid table name specified');
|
2006-11-17 17:59:57 +03:00
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if (empty($fields)) {
|
2007-01-15 21:48:50 +03:00
|
|
|
throw new Doctrine_Export_Exception('no fields specified for table "'.$name.'"');
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2007-01-15 21:48:50 +03:00
|
|
|
$queryFields = $this->getFieldDeclarationList($fields);
|
2006-10-20 00:56:59 +04:00
|
|
|
|
2007-06-18 23:20:49 +04:00
|
|
|
// 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) {
|
2007-06-19 14:43:19 +04:00
|
|
|
$options['indexes'] = array_merge($options['indexes'], array($local => array('fields' => array($local => array()))));
|
2007-06-18 23:20:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-06-19 14:43:19 +04:00
|
|
|
|
2007-06-18 23:20:49 +04:00
|
|
|
// add all indexes
|
2007-02-10 23:48:11 +03:00
|
|
|
if (isset($options['indexes']) && ! empty($options['indexes'])) {
|
|
|
|
foreach($options['indexes'] as $index => $definition) {
|
|
|
|
$queryFields .= ', ' . $this->getIndexDeclaration($index, $definition);
|
|
|
|
}
|
|
|
|
}
|
2007-02-17 01:54:59 +03:00
|
|
|
|
2007-06-18 23:20:49 +04:00
|
|
|
// attach all primary keys
|
2007-02-11 00:07:47 +03:00
|
|
|
if (isset($options['primary']) && ! empty($options['primary'])) {
|
|
|
|
$queryFields .= ', PRIMARY KEY(' . implode(', ', array_values($options['primary'])) . ')';
|
|
|
|
}
|
|
|
|
|
2006-11-17 17:59:57 +03:00
|
|
|
$name = $this->conn->quoteIdentifier($name, true);
|
2007-01-15 21:48:50 +03:00
|
|
|
$query = 'CREATE TABLE ' . $name . ' (' . $queryFields . ')';
|
2006-10-20 00:56:59 +04:00
|
|
|
|
2006-11-17 17:59:57 +03:00
|
|
|
$optionStrings = array();
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if (isset($options['comment'])) {
|
2007-01-12 01:39:32 +03:00
|
|
|
$optionStrings['comment'] = 'COMMENT = ' . $this->dbh->quote($options['comment'], 'text');
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
|
|
|
if (isset($options['charset'])) {
|
2007-02-07 16:09:12 +03:00
|
|
|
$optionStrings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
|
2006-12-29 17:01:31 +03:00
|
|
|
if (isset($options['collate'])) {
|
2007-06-18 22:54:48 +04:00
|
|
|
$optionStrings['charset'] .= ' COLLATE ' . $options['collate'];
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = false;
|
2006-11-17 17:59:57 +03:00
|
|
|
|
2007-06-18 23:20:49 +04:00
|
|
|
// get the type of the table
|
2007-06-19 14:43:19 +04:00
|
|
|
if (isset($options['type'])) {
|
2006-10-20 00:56:59 +04:00
|
|
|
$type = $options['type'];
|
2006-11-29 02:26:44 +03:00
|
|
|
} else {
|
|
|
|
$type = $this->conn->getAttribute(Doctrine::ATTR_DEFAULT_TABLE_TYPE);
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
2006-12-29 17:01:31 +03:00
|
|
|
|
2006-10-20 00:56:59 +04:00
|
|
|
if ($type) {
|
2006-11-29 02:26:44 +03:00
|
|
|
$optionStrings[] = 'ENGINE = ' . $type;
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
|
|
|
|
2006-11-17 17:59:57 +03:00
|
|
|
if (!empty($optionStrings)) {
|
|
|
|
$query.= ' '.implode(' ', $optionStrings);
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
2007-06-18 22:54:48 +04:00
|
|
|
$sql[] = $query;
|
|
|
|
|
|
|
|
if (isset($options['foreignKeys'])) {
|
|
|
|
|
|
|
|
foreach ((array) $options['foreignKeys'] as $k => $definition) {
|
|
|
|
if (is_array($definition)) {
|
|
|
|
if ( ! isset($definition['table'])) {
|
2007-06-18 23:20:49 +04:00
|
|
|
$definition['table'] = $name;
|
2007-06-18 22:54:48 +04:00
|
|
|
}
|
|
|
|
$sql[] = $this->createForeignKeySql($definition['table'], $definition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $sql;
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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
|
2006-11-17 17:59:57 +03:00
|
|
|
* can perform the requested table alterations if the value is true or
|
|
|
|
* actually perform them otherwise.
|
2006-10-20 00:56:59 +04:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2007-02-11 00:07:47 +03:00
|
|
|
public function alterTableSql($name, array $changes, $check)
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2007-04-18 15:14:38 +04:00
|
|
|
if ( ! $name) {
|
2007-01-15 21:48:50 +03:00
|
|
|
throw new Doctrine_Export_Exception('no valid table name specified');
|
2007-04-18 15:14:38 +04:00
|
|
|
}
|
2006-11-17 17:59:57 +03:00
|
|
|
foreach ($changes as $changeName => $change) {
|
|
|
|
switch ($changeName) {
|
2006-10-20 00:56:59 +04:00
|
|
|
case 'add':
|
|
|
|
case 'remove':
|
|
|
|
case 'change':
|
|
|
|
case 'rename':
|
|
|
|
case 'name':
|
2006-12-30 00:30:37 +03:00
|
|
|
break;
|
2006-10-20 00:56:59 +04:00
|
|
|
default:
|
2007-04-18 15:14:38 +04:00
|
|
|
throw new Doctrine_Export_Exception('change type "' . $changeName . '" not yet supported');
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if ($check) {
|
2006-10-20 00:56:59 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = '';
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! empty($changes['name'])) {
|
2007-04-18 15:14:38 +04:00
|
|
|
$change_name = $this->conn->quoteIdentifier($changes['name']);
|
2006-10-20 00:56:59 +04:00
|
|
|
$query .= 'RENAME TO ' . $change_name;
|
|
|
|
}
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! empty($changes['add']) && is_array($changes['add'])) {
|
2007-04-18 15:14:38 +04:00
|
|
|
foreach ($changes['add'] as $fieldName => $field) {
|
2006-10-20 00:56:59 +04:00
|
|
|
if ($query) {
|
|
|
|
$query.= ', ';
|
|
|
|
}
|
2007-04-18 15:14:38 +04:00
|
|
|
$query.= 'ADD ' . $this->getDeclaration($field['type'], $fieldName, $field);
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! empty($changes['remove']) && is_array($changes['remove'])) {
|
2007-04-18 15:14:38 +04:00
|
|
|
foreach ($changes['remove'] as $fieldName => $field) {
|
2006-10-20 00:56:59 +04:00
|
|
|
if ($query) {
|
2007-04-18 15:14:38 +04:00
|
|
|
$query .= ', ';
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
2007-04-18 15:14:38 +04:00
|
|
|
$fieldName = $this->conn->quoteIdentifier($fieldName);
|
|
|
|
$query .= 'DROP ' . $fieldName;
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$rename = array();
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! empty($changes['rename']) && is_array($changes['rename'])) {
|
2007-04-18 15:14:38 +04:00
|
|
|
foreach ($changes['rename'] as $fieldName => $field) {
|
|
|
|
$rename[$field['name']] = $fieldName;
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! empty($changes['change']) && is_array($changes['change'])) {
|
2007-02-11 00:07:47 +03:00
|
|
|
foreach ($changes['change'] as $fieldName => $field) {
|
2006-10-20 00:56:59 +04:00
|
|
|
if ($query) {
|
|
|
|
$query.= ', ';
|
|
|
|
}
|
2007-02-11 00:07:47 +03:00
|
|
|
if (isset($rename[$fieldName])) {
|
|
|
|
$oldFieldName = $rename[$fieldName];
|
|
|
|
unset($rename[$fieldName]);
|
2006-10-20 00:56:59 +04:00
|
|
|
} else {
|
2007-02-11 00:07:47 +03:00
|
|
|
$oldFieldName = $fieldName;
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
2007-03-21 02:22:51 +03:00
|
|
|
$oldFieldName = $this->conn->quoteIdentifier($oldFieldName, true);
|
2007-04-18 15:14:38 +04:00
|
|
|
$query .= 'CHANGE ' . $oldFieldName . ' '
|
|
|
|
. $this->getDeclaration($field['definition']['type'], $fieldName, $field['definition']);
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! empty($rename) && is_array($rename)) {
|
2007-04-18 15:14:38 +04:00
|
|
|
foreach ($rename as $renameName => $renamedField) {
|
2006-10-20 00:56:59 +04:00
|
|
|
if ($query) {
|
|
|
|
$query.= ', ';
|
|
|
|
}
|
2007-04-18 15:14:38 +04:00
|
|
|
$field = $changes['rename'][$renamedField];
|
|
|
|
$renamedField = $this->conn->quoteIdentifier($renamedField, true);
|
|
|
|
$query .= 'CHANGE ' . $renamedField . ' '
|
|
|
|
. $this->getDeclaration($field['definition']['type'], $field['name'], $field['definition']);
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! $query) {
|
2007-01-06 01:08:05 +03:00
|
|
|
return false;
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$name = $this->conn->quoteIdentifier($name, true);
|
2007-01-26 01:36:43 +03:00
|
|
|
return $this->conn->exec('ALTER TABLE ' . $name . ' ' . $query);
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* create sequence
|
|
|
|
*
|
2006-11-17 17:59:57 +03:00
|
|
|
* @param string $sequenceName name of the sequence to be created
|
|
|
|
* @param string $start start value of the sequence; default is 1
|
2007-06-14 01:29:18 +04:00
|
|
|
* @param array $options An associative array of table options:
|
|
|
|
* array(
|
|
|
|
* 'comment' => 'Foo',
|
|
|
|
* 'charset' => 'utf8',
|
|
|
|
* 'collate' => 'utf8_unicode_ci',
|
|
|
|
* 'type' => 'innodb',
|
|
|
|
* );
|
2006-11-17 17:59:57 +03:00
|
|
|
* @return boolean
|
2006-10-20 00:56:59 +04:00
|
|
|
*/
|
2007-06-14 01:29:18 +04:00
|
|
|
public function createSequence($sequenceName, $start = 1, array $options = array())
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2007-01-27 03:07:59 +03:00
|
|
|
$sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($sequenceName), true);
|
2007-01-15 22:02:00 +03:00
|
|
|
$seqcolName = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true);
|
|
|
|
|
2007-06-14 01:29:18 +04:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-17 01:54:59 +03:00
|
|
|
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 : '';
|
2007-06-14 01:29:18 +04:00
|
|
|
|
2007-02-17 01:54:59 +03:00
|
|
|
$res = $this->conn->exec($query);
|
|
|
|
} catch(Doctrine_Connection_Exception $e) {
|
|
|
|
throw new Doctrine_Export_Exception('could not create sequence table');
|
|
|
|
}
|
2006-10-20 00:56:59 +04:00
|
|
|
|
2006-11-17 17:59:57 +03:00
|
|
|
if ($start == 1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
$query = 'INSERT INTO ' . $sequenceName
|
2007-01-15 21:48:50 +03:00
|
|
|
. ' (' . $seqcolName . ') VALUES (' . ($start - 1) . ')';
|
2006-10-20 00:56:59 +04:00
|
|
|
|
2007-01-06 01:08:05 +03:00
|
|
|
$res = $this->conn->exec($query);
|
2006-10-20 00:56:59 +04:00
|
|
|
|
|
|
|
// Handle error
|
2006-11-17 17:59:57 +03:00
|
|
|
try {
|
2007-01-06 01:08:05 +03:00
|
|
|
$result = $this->conn->exec('DROP TABLE ' . $sequenceName);
|
2007-02-17 01:54:59 +03:00
|
|
|
} catch(Doctrine_Connection_Exception $e) {
|
2007-01-15 21:48:50 +03:00
|
|
|
throw new Doctrine_Export_Exception('could not drop inconsistent sequence table');
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
|
|
|
|
2007-02-17 01:54:59 +03:00
|
|
|
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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(
|
2007-02-11 00:07:47 +03:00
|
|
|
* 'sorting' => 'ASC'
|
2006-10-20 00:56:59 +04:00
|
|
|
* 'length' => 10
|
|
|
|
* ),
|
|
|
|
* 'last_login' => array()
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* @throws PDOException
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-02-10 17:41:11 +03:00
|
|
|
public function createIndexSql($table, $name, array $definition)
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2006-10-20 00:56:59 +04:00
|
|
|
$table = $table;
|
2006-11-24 01:54:10 +03:00
|
|
|
$name = $this->conn->getIndexName($name);
|
2007-02-10 17:41:11 +03:00
|
|
|
$type = '';
|
2007-04-18 15:14:38 +04:00
|
|
|
if (isset($definition['type'])) {
|
2007-02-10 17:41:11 +03:00
|
|
|
switch (strtolower($definition['type'])) {
|
2007-02-10 17:10:51 +03:00
|
|
|
case 'fulltext':
|
|
|
|
case 'unique':
|
2007-02-10 17:41:11 +03:00
|
|
|
$type = strtoupper($definition['type']) . ' ';
|
2007-02-10 17:10:51 +03:00
|
|
|
break;
|
|
|
|
default:
|
2007-02-10 17:41:11 +03:00
|
|
|
throw new Doctrine_Export_Exception('Unknown index type ' . $definition['type']);
|
2007-02-10 17:10:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$query = 'CREATE ' . $type . 'INDEX ' . $name . ' ON ' . $table;
|
2007-04-18 15:14:38 +04:00
|
|
|
$query .= ' (' . $this->getIndexFieldDeclarationList() . ')';
|
2007-02-10 17:10:51 +03:00
|
|
|
|
2007-02-11 00:07:47 +03:00
|
|
|
return $query;
|
|
|
|
}
|
2007-02-11 11:42:13 +03:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2007-02-11 00:07:47 +03:00
|
|
|
/**
|
|
|
|
* 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']);
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
|
|
|
}
|
2007-02-11 00:07:47 +03:00
|
|
|
|
2007-02-17 01:54:59 +03:00
|
|
|
if ( ! isset($definition['fields'])) {
|
2007-02-11 00:07:47 +03:00
|
|
|
throw new Doctrine_Export_Exception('No index columns given.');
|
|
|
|
}
|
2007-02-17 01:54:59 +03:00
|
|
|
if ( ! is_array($definition['fields'])) {
|
|
|
|
$definition['fields'] = array($definition['fields']);
|
|
|
|
}
|
2007-02-11 00:07:47 +03:00
|
|
|
|
2007-06-18 23:20:49 +04:00
|
|
|
$query = $type . 'INDEX ' . $this->conn->formatter->getIndexName($name);
|
2006-11-24 01:54:10 +03:00
|
|
|
|
2007-02-11 00:07:47 +03:00
|
|
|
$query .= ' (' . $this->getIndexFieldDeclarationList($definition['fields']) . ')';
|
|
|
|
|
2007-02-10 17:41:11 +03:00
|
|
|
return $query;
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
2007-02-11 00:07:47 +03:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2007-06-14 01:11:36 +04:00
|
|
|
/**
|
|
|
|
* getAdvancedForeignKeyOptions
|
|
|
|
* Return the FOREIGN KEY query section dealing with non-standard options
|
|
|
|
* as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
|
|
|
|
*
|
|
|
|
* @param array $definition
|
|
|
|
* @return string
|
|
|
|
*/
|
2007-06-14 01:29:18 +04:00
|
|
|
public function getAdvancedForeignKeyOptions(array $definition)
|
2007-06-14 01:11:36 +04:00
|
|
|
{
|
|
|
|
$query = '';
|
|
|
|
if (!empty($definition['match'])) {
|
|
|
|
$query .= ' MATCH ' . $definition['match'];
|
|
|
|
}
|
2007-06-15 00:18:25 +04:00
|
|
|
if (!empty($definition['onUpdate'])) {
|
2007-06-15 00:29:33 +04:00
|
|
|
$query .= ' ON UPDATE ' . $this->getForeignKeyReferentialAction($definition['onUpdate']);
|
2007-06-14 01:11:36 +04:00
|
|
|
}
|
2007-06-15 00:18:25 +04:00
|
|
|
if (!empty($definition['onDelete'])) {
|
2007-06-15 00:29:33 +04:00
|
|
|
$query .= ' ON DELETE ' . $this->getForeignKeyReferentialAction($definition['onDelete']);
|
2007-06-14 01:11:36 +04:00
|
|
|
}
|
|
|
|
return $query;
|
|
|
|
}
|
2006-10-20 00:56:59 +04:00
|
|
|
/**
|
|
|
|
* 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
|
2006-11-17 17:59:57 +03:00
|
|
|
* @return void
|
2006-10-20 00:56:59 +04:00
|
|
|
*/
|
2007-06-15 00:18:25 +04:00
|
|
|
public function dropIndexSql($table, $name)
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2006-10-20 00:56:59 +04:00
|
|
|
$table = $this->conn->quoteIdentifier($table, true);
|
2007-06-12 03:25:46 +04:00
|
|
|
$name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name), true);
|
2007-06-15 00:18:25 +04:00
|
|
|
return 'DROP INDEX ' . $name . ' ON ' . $table;
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* dropTable
|
|
|
|
*
|
|
|
|
* @param string $table name of table that should be dropped from the database
|
|
|
|
* @throws PDOException
|
2006-11-17 17:59:57 +03:00
|
|
|
* @return void
|
2006-10-20 00:56:59 +04:00
|
|
|
*/
|
2007-06-15 00:18:25 +04:00
|
|
|
public function dropTableSql($table)
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2006-11-17 17:59:57 +03:00
|
|
|
$table = $this->conn->quoteIdentifier($table, true);
|
2007-06-15 00:18:25 +04:00
|
|
|
return 'DROP TABLE ' . $table;
|
2006-10-20 00:56:59 +04:00
|
|
|
}
|
|
|
|
}
|
2006-12-29 17:01:31 +03:00
|
|
|
|