Postgres::alterColumn PHP Method

alterColumn() public method

Alters a column in a table
public alterColumn ( $table, $column, $name, $notnull, $oldnotnull, $default, $olddefault, $type, $length, $array, $oldtype, $comment ) : -6
$table The table in which the column resides
$column The column to alter
$name The new name for the column
$notnull (boolean) True if not null, false otherwise
$oldnotnull (boolean) True if column is already not null, false otherwise
$default The new default for the column
$olddefault The old default for the column
$type The new type for the column
$length The optional size of the column (ie. 30 for varchar(30))
$array True if array type, false otherwise
$oldtype The old type for the column
$comment Comment for the column
return -6
    function alterColumn($table, $column, $name, $notnull, $oldnotnull, $default, $olddefault, $type, $length, $array, $oldtype, $comment)
    {
        // Begin transaction
        $status = $this->beginTransaction();
        if ($status != 0) {
            $this->rollbackTransaction();
            return -6;
        }
        // Rename the column, if it has been changed
        if ($column != $name) {
            $status = $this->renameColumn($table, $column, $name);
            if ($status != 0) {
                $this->rollbackTransaction();
                return -4;
            }
        }
        $f_schema = $this->_schema;
        $this->fieldClean($f_schema);
        $this->fieldClean($name);
        $this->fieldClean($table);
        $this->fieldClean($column);
        $toAlter = array();
        // Create the command for changing nullability
        if ($notnull != $oldnotnull) {
            $toAlter[] = "ALTER COLUMN \"{$name}\" " . ($notnull ? 'SET' : 'DROP') . " NOT NULL";
        }
        // Add default, if it has changed
        if ($default != $olddefault) {
            if ($default == '') {
                $toAlter[] = "ALTER COLUMN \"{$name}\" DROP DEFAULT";
            } else {
                $toAlter[] = "ALTER COLUMN \"{$name}\" SET DEFAULT {$default}";
            }
        }
        // Add type, if it has changed
        if ($length == '') {
            $ftype = $type;
        } else {
            switch ($type) {
                // Have to account for weird placing of length for with/without
                // time zone types
                case 'timestamp with time zone':
                case 'timestamp without time zone':
                    $qual = substr($type, 9);
                    $ftype = "timestamp({$length}){$qual}";
                    break;
                case 'time with time zone':
                case 'time without time zone':
                    $qual = substr($type, 4);
                    $ftype = "time({$length}){$qual}";
                    break;
                default:
                    $ftype = "{$type}({$length})";
            }
        }
        // Add array qualifier, if requested
        if ($array) {
            $ftype .= '[]';
        }
        if ($ftype != $oldtype) {
            $toAlter[] = "ALTER COLUMN \"{$name}\" TYPE {$ftype}";
        }
        // Attempt to process the batch alteration, if anything has been changed
        if (!empty($toAlter)) {
            // Initialise an empty SQL string
            $sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" " . implode(',', $toAlter);
            $status = $this->execute($sql);
            if ($status != 0) {
                $this->rollbackTransaction();
                return -1;
            }
        }
        // Update the comment on the column
        $status = $this->setComment('COLUMN', $name, $table, $comment);
        if ($status != 0) {
            $this->rollbackTransaction();
            return -5;
        }
        return $this->endTransaction();
    }
Postgres