Ruckusing_Adapter_PgSQL_Base::change_column PHP Method

change_column() public method

Change a column
public change_column ( string $table_name, string $column_name, string $type, array $options = [] ) : boolean
$table_name string the table name
$column_name string the column name
$type string the column type
$options array column options
return boolean
    public function change_column($table_name, $column_name, $type, $options = array())
    {
        if (empty($table_name)) {
            throw new Ruckusing_Exception("Missing table name parameter", Ruckusing_Exception::INVALID_ARGUMENT);
        }
        if (empty($column_name)) {
            throw new Ruckusing_Exception("Missing original column name parameter", Ruckusing_Exception::INVALID_ARGUMENT);
        }
        if (empty($type)) {
            throw new Ruckusing_Exception("Missing type parameter", Ruckusing_Exception::INVALID_ARGUMENT);
        }
        $column_info = $this->column_info($table_name, $column_name);
        //default types
        if (!array_key_exists('limit', $options)) {
            $options['limit'] = null;
        }
        if (!array_key_exists('precision', $options)) {
            $options['precision'] = null;
        }
        if (!array_key_exists('scale', $options)) {
            $options['scale'] = null;
        }
        $sql = sprintf("ALTER TABLE %s ALTER COLUMN %s TYPE %s", $this->quote_table_name($table_name), $this->quote_column_name($column_name), $this->type_to_sql($type, $options));
        $sql .= $this->add_column_options($type, $options, true);
        if (array_key_exists('default', $options)) {
            $this->change_column_default($table_name, $column_name, $options['default']);
        }
        if (array_key_exists('null', $options)) {
            $default = array_key_exists('default', $options) ? $options['default'] : null;
            $this->change_column_null($table_name, $column_name, $options['null'], $default);
        }
        return $this->execute_ddl($sql);
    }

Usage Example

 /**
  * test changing column
  */
 public function test_change_column()
 {
     //create it
     $table = $this->adapter->create_table('users');
     $table->column('name', 'string', array('limit' => 20));
     $table->column('age', 'integer');
     $table->finish();
     //verify its type
     $col = $this->adapter->column_info("users", "name");
     $this->assertEquals('character varying(20)', $col['type']);
     $this->assertEquals('', $col['default']);
     //change it, add a default too!
     $this->adapter->change_column("users", "name", "string", array('default' => 'abc', 'limit' => 128));
     $col = $this->adapter->column_info("users", "name");
     $this->assertEquals('character varying(128)', $col['type']);
     $this->assertEquals("'abc'::character varying", $col['default']);
     $this->drop_table('users');
 }
All Usage Examples Of Ruckusing_Adapter_PgSQL_Base::change_column