From d4fa4640cb57361ee638008dafc81bd7fd945b0c Mon Sep 17 00:00:00 2001 From: zYne Date: Sat, 10 Feb 2007 20:48:11 +0000 Subject: [PATCH] updated index handling --- lib/Doctrine/Export/Mysql.php | 11 +++++-- lib/Doctrine/Export/Sqlite.php | 60 ++++++++++++++++++++++++---------- 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/lib/Doctrine/Export/Mysql.php b/lib/Doctrine/Export/Mysql.php index e47753f53..2b03f83d8 100644 --- a/lib/Doctrine/Export/Mysql.php +++ b/lib/Doctrine/Export/Mysql.php @@ -92,7 +92,7 @@ class Doctrine_Export_Mysql extends Doctrine_Export * * @return void */ - public function createTable($name, array $fields, array $options = array()) { + public function createTableSql($name, array $fields, array $options = array()) { if ( ! $name) 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'])) { $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); $query = 'CREATE TABLE ' . $name . ' (' . $queryFields . ')'; @@ -134,7 +141,7 @@ class Doctrine_Export_Mysql extends Doctrine_Export if (!empty($optionStrings)) { $query.= ' '.implode(' ', $optionStrings); } - return $this->conn->exec($query); + return $query; } /** * alter an existing table diff --git a/lib/Doctrine/Export/Sqlite.php b/lib/Doctrine/Export/Sqlite.php index c4b1543b9..a1c2ba851 100644 --- a/lib/Doctrine/Export/Sqlite.php +++ b/lib/Doctrine/Export/Sqlite.php @@ -65,29 +65,47 @@ class Doctrine_Export_Sqlite extends Doctrine_Export * @throws PDOException * @return void */ - public function createIndex($table, $name, array $definition) + public function createIndexSql($table, $name, array $definition) { $table = $this->conn->quoteIdentifier($table, true); $name = $this->conn->getIndexName($name); $query = 'CREATE INDEX ' . $name . ' ON ' . $table; - $fields = array(); - 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) . ')'; + $query .= ' (' . $this->getIndexFieldDeclarationList($definition['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 @@ -140,6 +158,12 @@ class Doctrine_Export_Sqlite extends Doctrine_Export $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); $query = 'CREATE TABLE ' . $name . ' (' . $queryFields . ')';