diff --git a/src/xPDO/Om/mysql/xPDOManager.php b/src/xPDO/Om/mysql/xPDOManager.php index 1be7ad75..0eb678f3 100644 --- a/src/xPDO/Om/mysql/xPDOManager.php +++ b/src/xPDO/Om/mysql/xPDOManager.php @@ -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. diff --git a/src/xPDO/Om/sqlite/xPDOManager.php b/src/xPDO/Om/sqlite/xPDOManager.php index 8452ea14..2553ac94 100644 --- a/src/xPDO/Om/sqlite/xPDOManager.php +++ b/src/xPDO/Om/sqlite/xPDOManager.php @@ -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))) { diff --git a/src/xPDO/Om/sqlsrv/xPDOManager.php b/src/xPDO/Om/sqlsrv/xPDOManager.php index 845428bf..1be1db58 100644 --- a/src/xPDO/Om/sqlsrv/xPDOManager.php +++ b/src/xPDO/Om/sqlsrv/xPDOManager.php @@ -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))) { diff --git a/src/xPDO/Om/xPDOManager.php b/src/xPDO/Om/xPDOManager.php index 0d248375..4b592b5d 100644 --- a/src/xPDO/Om/xPDOManager.php +++ b/src/xPDO/Om/xPDOManager.php @@ -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. *