Postgres::addColumn PHP Method

addColumn() public method

Add a new column to a table
public addColumn ( $table, $column, $type, $array, $length, $notnull, $default, $comment )
$table The table to add to
$column The name of the new column
$type The type of the column
$array True if array type, false otherwise
$length The optional size of the column (ie. 30 for varchar(30))
$notnull True if NOT NULL, false otherwise
$default The default for the column. '' for none.
    function addColumn($table, $column, $type, $array, $length, $notnull, $default, $comment)
    {
        $f_schema = $this->_schema;
        $this->fieldClean($f_schema);
        $this->fieldClean($table);
        $this->fieldClean($column);
        $this->clean($type);
        $this->clean($length);
        if ($length == '') {
            $sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" ADD COLUMN \"{$column}\" {$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);
                    $sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" ADD COLUMN \"{$column}\" timestamp({$length}){$qual}";
                    break;
                case 'time with time zone':
                case 'time without time zone':
                    $qual = substr($type, 4);
                    $sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" ADD COLUMN \"{$column}\" time({$length}){$qual}";
                    break;
                default:
                    $sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" ADD COLUMN \"{$column}\" {$type}({$length})";
            }
        }
        // Add array qualifier, if requested
        if ($array) {
            $sql .= '[]';
        }
        // If we have advanced column adding, add the extra qualifiers
        if ($this->hasCreateFieldWithConstraints()) {
            // NOT NULL clause
            if ($notnull) {
                $sql .= ' NOT NULL';
            }
            // DEFAULT clause
            if ($default != '') {
                $sql .= ' DEFAULT ' . $default;
            }
        }
        $status = $this->beginTransaction();
        if ($status != 0) {
            return -1;
        }
        $status = $this->execute($sql);
        if ($status != 0) {
            $this->rollbackTransaction();
            return -1;
        }
        $status = $this->setComment('COLUMN', $column, $table, $comment);
        if ($status != 0) {
            $this->rollbackTransaction();
            return -1;
        }
        return $this->endTransaction();
    }
Postgres