Ruckusing_Adapter_PgSQL_Base::column_info PHP Method

column_info() public method

Get a column info
public column_info ( string $table, string $column ) : array
$table string the table name
$column string the column name
return array
    public function column_info($table, $column)
    {
        if (empty($table)) {
            throw new Ruckusing_Exception("Missing table name parameter", Ruckusing_Exception::INVALID_ARGUMENT);
        }
        if (empty($column)) {
            throw new Ruckusing_Exception("Missing original column name parameter", Ruckusing_Exception::INVALID_ARGUMENT);
        }
        try {
            $sql = <<<SQL
      SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
        FROM pg_attribute a LEFT JOIN pg_attrdef d
          ON a.attrelid = d.adrelid AND a.attnum = d.adnum
       WHERE a.attrelid = '%s'::regclass
         AND a.attname = '%s'
         AND a.attnum > 0 AND NOT a.attisdropped
       ORDER BY a.attnum
SQL;
            $sql = sprintf($sql, $this->quote_table_name($table), $column);
            $result = $this->select_one($sql);
            $data = array();
            if (is_array($result)) {
                $data['type'] = $result['format_type'];
                $data['name'] = $column;
                $data['field'] = $column;
                $data['null'] = $result['attnotnull'] == 'f';
                $data['default'] = $result['adsrc'];
            } else {
                $data = null;
            }
            return $data;
        } catch (Exception $e) {
            return null;
        }
    }

Usage Example

 /**
  * test remove named timestamps
  */
 public function test_remove_named_timestamps()
 {
     $bm = new Ruckusing_Migration_Base($this->adapter);
     $table_name = 'users';
     $created_name = 'created';
     $updated_name = 'updated';
     //create it
     $this->adapter->execute_ddl("CREATE TABLE {$table_name} ( name varchar(20), {$created_name} timestamp not null, {$updated_name} timestamp not null );");
     //verify they exists
     $col = $this->adapter->column_info($table_name, $created_name);
     $this->assertEquals($created_name, $col['field']);
     $col = $this->adapter->column_info($table_name, $updated_name);
     $this->assertEquals($updated_name, $col['field']);
     //drop them
     $bm->remove_timestamps($table_name, $created_name, $updated_name);
     //verify they does not exist
     $col = $this->adapter->column_info($table_name, $created_name);
     fwrite(STDERR, print_r($col, TRUE));
     $this->assertEquals(null, $col);
     $col = $this->adapter->column_info($table_name, $updated_name);
     fwrite(STDERR, print_r($col, TRUE));
     $this->assertEquals(null, $col);
     $this->drop_table($table_name);
 }