Skip to content

Commit

Permalink
Cleaning up some code in the Importer classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Sep 4, 2014
1 parent 31d230a commit bad8825
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 71 deletions.
71 changes: 71 additions & 0 deletions src/DatabaseImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down
34 changes: 0 additions & 34 deletions src/Mysql/MysqlImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
17 changes: 0 additions & 17 deletions src/Mysqli/MysqliImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
37 changes: 17 additions & 20 deletions src/Postgresql/PostgresqlImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit bad8825

Please sign in to comment.