Ruckusing_Adapter_PgSQL_Base::add_column PHP Method

add_column() public method

Add a column
public add_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 add_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 column name parameter", Ruckusing_Exception::INVALID_ARGUMENT);
        }
        if (empty($type)) {
            throw new Ruckusing_Exception("Missing type parameter", Ruckusing_Exception::INVALID_ARGUMENT);
        }
        //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 ADD COLUMN %s %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);
        return $this->execute_ddl($sql);
    }

Usage Example

 /**
  * test adding column
  */
 public function test_add_column()
 {
     //create it
     $table = $this->adapter->create_table('users');
     $table->column('name', 'string', array('limit' => 20));
     $table->finish();
     $col = $this->adapter->column_info("users", "name");
     $this->assertEquals("name", $col['field']);
     //add column
     $this->adapter->add_column("users", "fav_color", "string", array('limit' => 32));
     $col = $this->adapter->column_info("users", "fav_color");
     $this->assertEquals("fav_color", $col['field']);
     $this->assertEquals('character varying(32)', $col['type']);
     //add column
     $this->adapter->add_column("users", "latitude", "decimal", array('precision' => 10, 'scale' => 2));
     $col = $this->adapter->column_info("users", "latitude");
     $this->assertEquals("latitude", $col['field']);
     $this->assertEquals('numeric(10,2)', $col['type']);
     //add column with unsigned parameter
     $this->adapter->add_column("users", "age", "integer", array('limit' => 2));
     // the limit will be ignored
     $col = $this->adapter->column_info("users", "age");
     $this->assertEquals("age", $col['field']);
     $this->assertEquals('integer', $col['type']);
     //add column with biginteger datatype
     $this->adapter->add_column("users", "weight", "biginteger");
     $col = $this->adapter->column_info("users", "weight");
     $this->assertEquals("weight", $col['field']);
     $this->assertEquals('bigint', $col['type']);
     $this->drop_table('users');
 }
All Usage Examples Of Ruckusing_Adapter_PgSQL_Base::add_column