diff --git a/src/DatabaseImporter.php b/src/DatabaseImporter.php index 21dd3e4ff..dc0d17441 100644 --- a/src/DatabaseImporter.php +++ b/src/DatabaseImporter.php @@ -119,6 +119,41 @@ public function from($from) return $this; } + /** + * Get the SQL syntax to add a column. + * + * @param string $table The table name. + * @param \SimpleXMLElement $field The XML field definition. + * + * @return string + * + * @since 1.0 + */ + protected function getAddColumnSQL($table, \SimpleXMLElement $field) + { + $sql = 'ALTER TABLE ' . $this->db->quoteName($table) . ' ADD COLUMN ' . $this->getColumnSQL($field); + + return $sql; + } + + /** + * Get the syntax to alter a column. + * + * @param string $table The name of the database table to alter. + * @param \SimpleXMLElement $field The XML definition for the field. + * + * @return string + * + * @since 1.0 + */ + protected function getChangeColumnSQL($table, \SimpleXMLElement $field) + { + $sql = 'ALTER TABLE ' . $this->db->quoteName($table) . ' CHANGE COLUMN ' . $this->db->quoteName((string) $field['Field']) . ' ' + . $this->getColumnSQL($field); + + return $sql; + } + /** * Get the SQL syntax to drop a column. * @@ -136,6 +171,42 @@ protected function getDropColumnSQL($table, $name) return $sql; } + /** + * Get the details list of keys for a table. + * + * @param array $keys An array of objects that comprise the keys for the table. + * + * @return array The lookup array. array({key name} => array(object, ...)) + * + * @since 1.0 + */ + protected function getKeyLookup($keys) + { + // First pass, create a lookup of the keys. + $lookup = array(); + + foreach ($keys as $key) + { + if ($key instanceof \SimpleXMLElement) + { + $kName = (string) $key['Key_name']; + } + else + { + $kName = $key->Key_name; + } + + if (empty($lookup[$kName])) + { + $lookup[$kName] = array(); + } + + $lookup[$kName][] = $key; + } + + return $lookup; + } + /** * Get the real name of the table, converting the prefix wildcard string if present. * diff --git a/src/Mysql/MysqlImporter.php b/src/Mysql/MysqlImporter.php index c03aa652f..e64598814 100644 --- a/src/Mysql/MysqlImporter.php +++ b/src/Mysql/MysqlImporter.php @@ -17,23 +17,6 @@ */ class MysqlImporter extends DatabaseImporter { - /** - * Get the SQL syntax to add a column. - * - * @param string $table The table name. - * @param \SimpleXMLElement $field The XML field definition. - * - * @return string - * - * @since 1.0 - */ - protected function getAddColumnSQL($table, \SimpleXMLElement $field) - { - $sql = 'ALTER TABLE ' . $this->db->quoteName($table) . ' ADD COLUMN ' . $this->getColumnSQL($field); - - return $sql; - } - /** * Get the SQL syntax to add a key. * @@ -275,23 +258,6 @@ protected function getColumnSQL(\SimpleXMLElement $field) return $sql; } - /** - * Get the SQL syntax to drop a column. - * - * @param string $table The table name. - * @param string $name The name of the field to drop. - * - * @return string - * - * @since 1.0 - */ - protected function getDropColumnSQL($table, $name) - { - $sql = 'ALTER TABLE ' . $this->db->quoteName($table) . ' DROP COLUMN ' . $this->db->quoteName($name); - - return $sql; - } - /** * Get the SQL syntax to drop a key. * diff --git a/src/Mysqli/MysqliImporter.php b/src/Mysqli/MysqliImporter.php index 8bee16cd1..82698e199 100644 --- a/src/Mysqli/MysqliImporter.php +++ b/src/Mysqli/MysqliImporter.php @@ -42,23 +42,6 @@ public function check() return $this; } - /** - * Get the SQL syntax to add a column. - * - * @param string $table The table name. - * @param \SimpleXMLElement $field The XML field definition. - * - * @return string - * - * @since 1.0 - */ - protected function getAddColumnSQL($table, \SimpleXMLElement $field) - { - $sql = 'ALTER TABLE ' . $this->db->quoteName($table) . ' ADD COLUMN ' . $this->getColumnSQL($field); - - return $sql; - } - /** * Get the SQL syntax to add a key. * diff --git a/src/Postgresql/PostgresqlImporter.php b/src/Postgresql/PostgresqlImporter.php index f99a6eba6..f211ad941 100644 --- a/src/Postgresql/PostgresqlImporter.php +++ b/src/Postgresql/PostgresqlImporter.php @@ -42,23 +42,6 @@ public function check() return $this; } - /** - * Get the SQL syntax to add a column. - * - * @param string $table The table name. - * @param \SimpleXMLElement $field The XML field definition. - * - * @return string - * - * @since 1.0 - */ - protected function getAddColumnSQL($table, \SimpleXMLElement $field) - { - $sql = 'ALTER TABLE ' . $this->db->quoteName($table) . ' ADD COLUMN ' . $this->getColumnSQL($field); - - return $sql; - } - /** * Get the SQL syntax to add an index. * @@ -184,8 +167,8 @@ protected function getAlterTableSQL(\SimpleXMLElement $structure) /* Index section */ // Get the lookups for the old and new keys - $oldLookup = $this->getIdxLookup($oldKeys); - $newLookup = $this->getIdxLookup($newKeys); + $oldLookup = $this->getKeyLookup($oldKeys); + $newLookup = $this->getKeyLookup($newKeys); // Loop through each key in the new structure. foreach ($newLookup as $name => $keys) @@ -492,9 +475,23 @@ protected function getDropPrimaryKeySQL($table, $name) * @return array The lookup array. array({key name} => array(object, ...)) * * @since 1.0 - * @throws \Exception + * @deprecated 2.0 Use {@link getKeyLookup()} instead */ protected function getIdxLookup($keys) + { + return $this->getKeyLookup($keys); + } + + /** + * Get the details list of keys for a table. + * + * @param array $keys An array of objects that comprise the keys for the table. + * + * @return array The lookup array. array({key name} => array(object, ...)) + * + * @since __DEPLOY_VERSION__ + */ + protected function getKeyLookup($keys) { // First pass, create a lookup of the keys. $lookup = array();