Commit d4fa4640 authored by zYne's avatar zYne

updated index handling

parent c70a8577
...@@ -92,7 +92,7 @@ class Doctrine_Export_Mysql extends Doctrine_Export ...@@ -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 ...@@ -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 ...@@ -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 ...@@ -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) {
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; $fieldString = $fieldName;
if (is_array($field)) {
if (isset($field['sorting'])) { if (isset($field['sorting'])) {
switch ($field['sorting']) { $sort = strtoupper($field['sorting']);
case 'ascending': switch ($sort) {
$fieldString .= ' ASC'; case 'ASC':
break; case 'DESC':
case 'descending': $fieldString .= ' ' . $sort;
$fieldString .= ' DESC';
break; break;
default:
throw new Doctrine_Export_Exception('Unknown index sorting option given.');
} }
} }
$fields[] = $fieldString; } else {
$fieldString = $field;
} }
$query .= ' (' . implode(', ', $fields) . ')'; $declFields[] = $fieldString;
}
return $this->conn->exec($query); return implode(', ', $declFields);
} }
/** /**
* create a new table * create a new table
...@@ -140,6 +158,12 @@ class Doctrine_Export_Sqlite extends Doctrine_Export ...@@ -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 . ')';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment