updated index handling
This commit is contained in:
parent
c70a857711
commit
d4fa4640cb
@ -92,7 +92,7 @@ class Doctrine_Export_Mysql extends Doctrine_Export
|
|||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function createTable($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');
|
||||||
|
|
||||||
@ -104,6 +104,13 @@ class Doctrine_Export_Mysql extends Doctrine_Export
|
|||||||
if (isset($options['primary']) && ! empty($options['primary'])) {
|
if (isset($options['primary']) && ! empty($options['primary'])) {
|
||||||
$queryFields .= ', PRIMARY KEY(' . implode(', ', array_values($options['primary'])) . ')';
|
$queryFields .= ', PRIMARY KEY(' . implode(', ', array_values($options['primary'])) . ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isset($options['indexes']) && ! empty($options['indexes'])) {
|
||||||
|
foreach($options['indexes'] as $index => $definition) {
|
||||||
|
$queryFields .= ', ' . $this->getIndexDeclaration($index, $definition);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$name = $this->conn->quoteIdentifier($name, true);
|
$name = $this->conn->quoteIdentifier($name, true);
|
||||||
$query = 'CREATE TABLE ' . $name . ' (' . $queryFields . ')';
|
$query = 'CREATE TABLE ' . $name . ' (' . $queryFields . ')';
|
||||||
|
|
||||||
@ -134,7 +141,7 @@ class Doctrine_Export_Mysql extends Doctrine_Export
|
|||||||
if (!empty($optionStrings)) {
|
if (!empty($optionStrings)) {
|
||||||
$query.= ' '.implode(' ', $optionStrings);
|
$query.= ' '.implode(' ', $optionStrings);
|
||||||
}
|
}
|
||||||
return $this->conn->exec($query);
|
return $query;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* alter an existing table
|
* alter an existing table
|
||||||
|
@ -65,29 +65,47 @@ class Doctrine_Export_Sqlite extends Doctrine_Export
|
|||||||
* @throws PDOException
|
* @throws PDOException
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function createIndex($table, $name, array $definition)
|
public function createIndexSql($table, $name, array $definition)
|
||||||
{
|
{
|
||||||
$table = $this->conn->quoteIdentifier($table, true);
|
$table = $this->conn->quoteIdentifier($table, true);
|
||||||
$name = $this->conn->getIndexName($name);
|
$name = $this->conn->getIndexName($name);
|
||||||
$query = 'CREATE INDEX ' . $name . ' ON ' . $table;
|
$query = 'CREATE INDEX ' . $name . ' ON ' . $table;
|
||||||
$fields = array();
|
$query .= ' (' . $this->getIndexFieldDeclarationList($definition['fields']) . ')';
|
||||||
foreach ($definition['fields'] as $fieldName => $field) {
|
|
||||||
$fieldString = $fieldName;
|
|
||||||
if (isset($field['sorting'])) {
|
|
||||||
switch ($field['sorting']) {
|
|
||||||
case 'ascending':
|
|
||||||
$fieldString .= ' ASC';
|
|
||||||
break;
|
|
||||||
case 'descending':
|
|
||||||
$fieldString .= ' DESC';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$fields[] = $fieldString;
|
|
||||||
}
|
|
||||||
$query .= ' (' . implode(', ', $fields) . ')';
|
|
||||||
|
|
||||||
return $this->conn->exec($query);
|
return $query;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 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['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);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* create a new table
|
* create a new table
|
||||||
@ -140,6 +158,12 @@ class Doctrine_Export_Sqlite extends Doctrine_Export
|
|||||||
$queryFields.= ', PRIMARY KEY('.implode(', ', array_values($options['primary'])).')';
|
$queryFields.= ', PRIMARY KEY('.implode(', ', array_values($options['primary'])).')';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isset($options['indexes']) && ! empty($options['indexes'])) {
|
||||||
|
foreach($options['indexes'] as $index => $definition) {
|
||||||
|
$queryFields .= ', ' . $this->getIndexDeclaration($index, $definition);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$name = $this->conn->quoteIdentifier($name, true);
|
$name = $this->conn->quoteIdentifier($name, true);
|
||||||
$query = 'CREATE TABLE ' . $name . ' (' . $queryFields . ')';
|
$query = 'CREATE TABLE ' . $name . ' (' . $queryFields . ')';
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user