- added support for temporary tables, tweaked createSequence and enum support in getDefaultFieldDeclaration()
This commit is contained in:
parent
406c57b53b
commit
16ef556f62
@ -89,16 +89,17 @@ class Doctrine_Export_Mysql extends Doctrine_Export
|
|||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function createTableSql($name, array $fields, array $options = array())
|
public function createTableSql($name, array $fields, array $options = array())
|
||||||
{
|
{
|
||||||
if ( ! $name)
|
if ( ! $name) {
|
||||||
throw new Doctrine_Export_Exception('no valid table name specified');
|
throw new Doctrine_Export_Exception('no valid table name specified');
|
||||||
|
}
|
||||||
|
|
||||||
if (empty($fields)) {
|
if (empty($fields)) {
|
||||||
throw new Doctrine_Export_Exception('no fields specified for table "'.$name.'"');
|
throw new Doctrine_Export_Exception('no fields specified for table "'.$name.'"');
|
||||||
}
|
}
|
||||||
$queryFields = $this->getFieldDeclarationList($fields);
|
$queryFields = $this->getFieldDeclarationList($fields);
|
||||||
|
|
||||||
// build indexes for all foreign key fields (needed in MySQL!!)
|
// build indexes for all foreign key fields (needed in MySQL!!)
|
||||||
if (isset($options['foreignKeys'])) {
|
if (isset($options['foreignKeys'])) {
|
||||||
foreach ($options['foreignKeys'] as $fk) {
|
foreach ($options['foreignKeys'] as $fk) {
|
||||||
@ -107,8 +108,8 @@ class Doctrine_Export_Mysql extends Doctrine_Export
|
|||||||
if (isset($options['indexes'])) {
|
if (isset($options['indexes'])) {
|
||||||
foreach ($options['indexes'] as $definition) {
|
foreach ($options['indexes'] as $definition) {
|
||||||
if (is_string($definition['fields'])) {
|
if (is_string($definition['fields'])) {
|
||||||
// Check if index already exists on the column
|
// Check if index already exists on the column
|
||||||
$found = ($local == $definition['fields']);
|
$found = ($local == $definition['fields']);
|
||||||
} else if (in_array($local, $definition['fields']) && count($definition['fields']) === 1) {
|
} else if (in_array($local, $definition['fields']) && count($definition['fields']) === 1) {
|
||||||
// Index already exists on the column
|
// Index already exists on the column
|
||||||
$found = true;
|
$found = true;
|
||||||
@ -120,7 +121,7 @@ class Doctrine_Export_Mysql extends Doctrine_Export
|
|||||||
// field is part of the PK and therefore already indexed
|
// field is part of the PK and therefore already indexed
|
||||||
$found = true;
|
$found = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! $found) {
|
if ( ! $found) {
|
||||||
$options['indexes'][$local] = array('fields' => array($local => array()));
|
$options['indexes'][$local] = array('fields' => array($local => array()));
|
||||||
}
|
}
|
||||||
@ -141,7 +142,11 @@ class Doctrine_Export_Mysql extends Doctrine_Export
|
|||||||
$queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
|
$queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
$query = 'CREATE TABLE ' . $this->conn->quoteIdentifier($name, true) . ' (' . $queryFields . ')';
|
$query = 'CREATE ';
|
||||||
|
if (!empty($options['temporary'])) {
|
||||||
|
$query .= 'TEMPORARY ';
|
||||||
|
}
|
||||||
|
$query.= 'TABLE ' . $this->conn->quoteIdentifier($name, true) . ' (' . $queryFields . ')';
|
||||||
|
|
||||||
$optionStrings = array();
|
$optionStrings = array();
|
||||||
|
|
||||||
@ -180,7 +185,7 @@ class Doctrine_Export_Mysql extends Doctrine_Export
|
|||||||
$sql[] = $this->createForeignKeySql($name, $definition);
|
$sql[] = $this->createForeignKeySql($name, $definition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $sql;
|
return $sql;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -338,7 +343,7 @@ class Doctrine_Export_Mysql extends Doctrine_Export
|
|||||||
$oldFieldName = $fieldName;
|
$oldFieldName = $fieldName;
|
||||||
}
|
}
|
||||||
$oldFieldName = $this->conn->quoteIdentifier($oldFieldName, true);
|
$oldFieldName = $this->conn->quoteIdentifier($oldFieldName, true);
|
||||||
$query .= 'CHANGE ' . $oldFieldName . ' '
|
$query .= 'CHANGE ' . $oldFieldName . ' '
|
||||||
. $this->getDeclaration($fieldName, $field['definition']);
|
. $this->getDeclaration($fieldName, $field['definition']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -360,7 +365,7 @@ class Doctrine_Export_Mysql extends Doctrine_Export
|
|||||||
}
|
}
|
||||||
|
|
||||||
$name = $this->conn->quoteIdentifier($name, true);
|
$name = $this->conn->quoteIdentifier($name, true);
|
||||||
|
|
||||||
return 'ALTER TABLE ' . $name . ' ' . $query;
|
return 'ALTER TABLE ' . $name . ' ' . $query;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -408,35 +413,37 @@ class Doctrine_Export_Mysql extends Doctrine_Export
|
|||||||
$optionsStrings[] = 'ENGINE = ' . $type;
|
$optionsStrings[] = 'ENGINE = ' . $type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$query = 'CREATE TABLE ' . $sequenceName
|
$query = 'CREATE TABLE ' . $sequenceName
|
||||||
. ' (' . $seqcolName . ' INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ('
|
. ' (' . $seqcolName . ' INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ('
|
||||||
. $seqcolName . '))'
|
. $seqcolName . '))';
|
||||||
. strlen($this->conn->default_table_type) ? ' TYPE = '
|
|
||||||
. $this->conn->default_table_type : '';
|
if (!empty($options_strings)) {
|
||||||
|
$query .= ' '.implode(' ', $options_strings);
|
||||||
|
}
|
||||||
|
|
||||||
$res = $this->conn->exec($query);
|
$res = $this->conn->exec($query);
|
||||||
} catch(Doctrine_Connection_Exception $e) {
|
} catch(Doctrine_Connection_Exception $e) {
|
||||||
throw new Doctrine_Export_Exception('could not create sequence table');
|
throw new Doctrine_Export_Exception('could not create sequence table');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($start == 1)
|
if ($start == 1) {
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
$query = 'INSERT INTO ' . $sequenceName
|
$query = 'INSERT INTO ' . $sequenceName
|
||||||
. ' (' . $seqcolName . ') VALUES (' . ($start - 1) . ')';
|
. ' (' . $seqcolName . ') VALUES (' . ($start - 1) . ')';
|
||||||
|
|
||||||
$res = $this->conn->exec($query);
|
$res = $this->conn->exec($query);
|
||||||
|
|
||||||
// Handle error
|
// Handle error
|
||||||
try {
|
try {
|
||||||
$result = $this->conn->exec('DROP TABLE ' . $sequenceName);
|
$res = $this->conn->exec('DROP TABLE ' . $sequenceName);
|
||||||
} catch(Doctrine_Connection_Exception $e) {
|
} catch(Doctrine_Connection_Exception $e) {
|
||||||
throw new Doctrine_Export_Exception('could not drop inconsistent sequence table');
|
throw new Doctrine_Export_Exception('could not drop inconsistent sequence table');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -495,7 +502,7 @@ class Doctrine_Export_Mysql extends Doctrine_Export
|
|||||||
return $query;
|
return $query;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* getDefaultDeclaration
|
* getDefaultDeclaration
|
||||||
* Obtain DBMS specific SQL code portion needed to set a default value
|
* Obtain DBMS specific SQL code portion needed to set a default value
|
||||||
* declaration to be used in statements like CREATE TABLE.
|
* declaration to be used in statements like CREATE TABLE.
|
||||||
@ -508,8 +515,10 @@ class Doctrine_Export_Mysql extends Doctrine_Export
|
|||||||
$default = '';
|
$default = '';
|
||||||
if (isset($field['default']) && ( ! isset($field['length']) || $field['length'] <= 255)) {
|
if (isset($field['default']) && ( ! isset($field['length']) || $field['length'] <= 255)) {
|
||||||
if ($field['default'] === '') {
|
if ($field['default'] === '') {
|
||||||
$field['default'] = empty($field['notnull'])
|
$field['default'] = null;
|
||||||
? null : $this->valid_default_values[$field['type']];
|
if (! empty($field['notnull']) && array_key_exists($field['type'], $this->valid_default_values)) {
|
||||||
|
$field['default'] = $this->valid_default_values[$field['type']];
|
||||||
|
}
|
||||||
|
|
||||||
if ($field['default'] === ''
|
if ($field['default'] === ''
|
||||||
&& ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_EMPTY_TO_NULL)
|
&& ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_EMPTY_TO_NULL)
|
||||||
@ -517,22 +526,20 @@ class Doctrine_Export_Mysql extends Doctrine_Export
|
|||||||
$field['default'] = ' ';
|
$field['default'] = ' ';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Proposed patch:
|
|
||||||
if ($field['type'] == 'enum' && $this->conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)) {
|
if ($field['type'] == 'enum' && $this->conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)) {
|
||||||
$fieldType = 'varchar';
|
$fieldType = 'varchar';
|
||||||
} else {
|
} else {
|
||||||
$fieldType = $field['type'];
|
$fieldType = $field['type'];
|
||||||
}
|
}
|
||||||
|
|
||||||
$default = ' DEFAULT ' . $this->conn->quote($field['default'], $fieldType);
|
$default = ' DEFAULT ' . $this->conn->quote($field['default'], $fieldType);
|
||||||
//$default = ' DEFAULT ' . $this->conn->quote($field['default'], $field['type']);
|
|
||||||
}
|
}
|
||||||
return $default;
|
return $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Obtain DBMS specific SQL code portion needed to set an index
|
* Obtain DBMS specific SQL code portion needed to set an index
|
||||||
* declaration to be used in statements like CREATE TABLE.
|
* declaration to be used in statements like CREATE TABLE.
|
||||||
*
|
*
|
||||||
* @param string $charset name of the index
|
* @param string $charset name of the index
|
||||||
@ -553,7 +560,7 @@ class Doctrine_Export_Mysql extends Doctrine_Export
|
|||||||
throw new Doctrine_Export_Exception('Unknown index type ' . $definition['type']);
|
throw new Doctrine_Export_Exception('Unknown index type ' . $definition['type']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! isset($definition['fields'])) {
|
if ( ! isset($definition['fields'])) {
|
||||||
throw new Doctrine_Export_Exception('No index columns given.');
|
throw new Doctrine_Export_Exception('No index columns given.');
|
||||||
}
|
}
|
||||||
@ -564,7 +571,7 @@ class Doctrine_Export_Mysql extends Doctrine_Export
|
|||||||
$query = $type . 'INDEX ' . $this->conn->quoteIdentifier($name);
|
$query = $type . 'INDEX ' . $this->conn->quoteIdentifier($name);
|
||||||
|
|
||||||
$query .= ' (' . $this->getIndexFieldDeclarationList($definition['fields']) . ')';
|
$query .= ' (' . $this->getIndexFieldDeclarationList($definition['fields']) . ')';
|
||||||
|
|
||||||
return $query;
|
return $query;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -655,11 +662,11 @@ class Doctrine_Export_Mysql extends Doctrine_Export
|
|||||||
$table = $this->conn->quoteIdentifier($table, true);
|
$table = $this->conn->quoteIdentifier($table, true);
|
||||||
return 'DROP TABLE ' . $table;
|
return 'DROP TABLE ' . $table;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dropForeignKey($table, $name)
|
public function dropForeignKey($table, $name)
|
||||||
{
|
{
|
||||||
$table = $this->conn->quoteIdentifier($table);
|
$table = $this->conn->quoteIdentifier($table);
|
||||||
$name = $this->conn->quoteIdentifier($name);
|
$name = $this->conn->quoteIdentifier($name);
|
||||||
return $this->conn->exec('ALTER TABLE ' . $table . ' DROP FOREIGN KEY ' . $name);
|
return $this->conn->exec('ALTER TABLE ' . $table . ' DROP FOREIGN KEY ' . $name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user