Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new function renameField into the manager to change a column without … #114

Open
wants to merge 2 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/xPDO/Om/mysql/xPDOManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,39 @@ public function alterField($class, $name, array $options = array()) {
return $result;
}

public function renameField($class, $oldName, $newName, array $options = array()) {
$result = false;
if ($this->xpdo->getConnection(array(xPDO::OPT_CONN_MUTABLE => true))) {
$className = $this->xpdo->loadClass($class);
if ($className) {
$meta = $this->xpdo->getFieldMeta($className, true);
if (is_array($meta) && array_key_exists($newName, $meta)) {
$colDef = $this->getColumnDef($className, $newName, $meta[$newName]);
if (!empty($colDef)) {
$sql = "ALTER TABLE {$this->xpdo->getTableName($className)} CHANGE COLUMN {$oldName} {$colDef}";
if (isset($options['first']) && !empty($options['first'])) {
$sql .= " FIRST";
} elseif (isset($options['after']) && array_key_exists($options['after'], $meta)) {
$sql .= " AFTER {$this->xpdo->escape($options['after'])}";
}
if ($this->xpdo->exec($sql) !== false) {
$result = true;
} else {
$this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Error altering field {$class}->{$newName}: " . print_r($this->xpdo->errorInfo(), true), '', __METHOD__, __FILE__, __LINE__);
}
} else {
$this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Error altering field {$class}->{$newName}: Could not get column definition");
}
} else {
$this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Error altering field {$class}->{$newName}: No metadata defined");
}
}
} else {
$this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Could not get writable connection", '', __METHOD__, __FILE__, __LINE__);
}
return $result;
}

public function removeConstraint($class, $name, array $options = array()) {
if ($this->xpdo->getConnection(array(xPDO::OPT_CONN_MUTABLE => true))) {
// TODO: Implement removeConstraint() method.
Expand Down
8 changes: 8 additions & 0 deletions src/xPDO/Om/sqlite/xPDOManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ public function alterField($class, $name, array $options = array()) {
return $result;
}

public function renameField($class, $oldName, $newName, array $options = array()) {
$result = false;
if ($this->xpdo->getConnection(array(xPDO::OPT_CONN_MUTABLE => true))) {
// TODO: Implement renameField() method somehow, no support in sqlite for altering existing columns
}
return $result;
}

public function removeConstraint($class, $name, array $options = array()) {
$result = false;
if ($this->xpdo->getConnection(array(xPDO::OPT_CONN_MUTABLE => true))) {
Expand Down
33 changes: 33 additions & 0 deletions src/xPDO/Om/sqlsrv/xPDOManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,39 @@ public function alterField($class, $name, array $options = array()) {
return $result;
}

public function renameField($class, $oldName, $newName, array $options = array()) {
$result = false;
if ($this->xpdo->getConnection(array(xPDO::OPT_CONN_MUTABLE => true))) {
$className = $this->xpdo->loadClass($class);
if ($className) {
$meta = $this->xpdo->getFieldMeta($className, true);
if (is_array($meta) && array_key_exists($newName, $meta)) {
$colDef = $this->getColumnDef($className, $newName, $meta[$newName]);
if (!empty($colDef)) {
$sql = "ALTER TABLE {$this->xpdo->getTableName($className)} CHANGE COLUMN {$oldName} {$colDef}";
if (isset($options['first']) && !empty($options['first'])) {
$sql .= " FIRST";
} elseif (isset($options['after']) && array_key_exists($options['after'], $meta)) {
$sql .= " AFTER {$this->xpdo->escape($options['after'])}";
}
if ($this->xpdo->exec($sql) !== false) {
$result = true;
} else {
$this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Error altering field {$class}->{$newName}: " . print_r($this->xpdo->errorInfo(), true), '', __METHOD__, __FILE__, __LINE__);
}
} else {
$this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Error altering field {$class}->{$newName}: Could not get column definition");
}
} else {
$this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Error altering field {$class}->{$newName}: No metadata defined");
}
}
} else {
$this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Could not get writable connection", '', __METHOD__, __FILE__, __LINE__);
}
return $result;
}

public function removeConstraint($class, $name, array $options = array()) {
$result = false;
if ($this->xpdo->getConnection(array(xPDO::OPT_CONN_MUTABLE => true))) {
Expand Down
11 changes: 11 additions & 0 deletions src/xPDO/Om/xPDOManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ abstract public function removeObjectContainer($className);
*/
abstract public function addField($class, $name, array $options = array());

/**
* Change an existing field of an object container, e.g. CHANGE COLUMN.
*
* @param string $class The object class to alter the field of.
* @param string $oldName The name of the old field you want change.
* @param string $newName The name of the new field you want set.
* @param array $options An array of options for the process for the new field.
* @return boolean True if the column is altered successfully, otherwise false.
*/
abstract public function renameField($class, $oldName, $newName, array $options = array());

/**
* Alter an existing field of an object container, e.g. ALTER COLUMN.
*
Expand Down