Skip to content

Commit

Permalink
Add unsigned support to integer, decimal, and float types.
Browse files Browse the repository at this point in the history
Remove 'fixed' from all columns and make it present only on string
columns.
  • Loading branch information
markstory committed Nov 22, 2013
1 parent 269aeea commit aabc2af
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
36 changes: 32 additions & 4 deletions Cake/Database/Schema/Table.php
Expand Up @@ -78,10 +78,32 @@ class Table {
'precision' => null,
'null' => null,
'default' => null,
'fixed' => null,
'comment' => null,
];

/**
* Additional type specific properties.
*
* @var array
*/
protected $_columnExtras = [
'string' => [
'fixed' => null,
],
'integer' => [
'unsigned' => null,
],
'biginteger' => [
'unsigned' => null,
],
'decimal' => [
'unsigned' => null,
],
'float' => [
'unsigned' => null,
],
];

/**
* The valid keys that can be used in an index
* definition.
Expand Down Expand Up @@ -179,7 +201,9 @@ public function name() {
* - `default` The default value of the column.
* - `null` Whether or not the column can hold nulls.
* - `fixed` Whether or not the column is a fixed length column.
* this is only useful for string colums.
* This is only present/valid with string columns.
* - `unsigned` Whether or not the column is an unsigned column.
* This is only present/valid for integer, decimal, float columns.
*
* In addition to the above keys, the following keys are
* implemented in some database dialects, but not all:
Expand All @@ -194,8 +218,12 @@ public function addColumn($name, $attrs) {
if (is_string($attrs)) {
$attrs = ['type' => $attrs];
}
$attrs = array_intersect_key($attrs, $this->_columnKeys);
$this->_columns[$name] = $attrs + $this->_columnKeys;
$valid = $this->_columnKeys;
if (isset($this->_columnExtras[$attrs['type']])) {
$valid += $this->_columnExtras[$attrs['type']];
}
$attrs = array_intersect_key($attrs, $valid);
$this->_columns[$name] = $attrs + $valid;
return $this;
}

Expand Down
30 changes: 30 additions & 0 deletions Cake/Test/TestCase/Database/Schema/TableTest.php
Expand Up @@ -101,6 +101,36 @@ public function testAddColumnFiltersAttributes() {
'comment' => null,
];
$this->assertEquals($expected, $result);

$table->addColumn('author_id', [
'type' => 'integer'
]);
$result = $table->column('author_id');
$expected = [
'type' => 'integer',
'length' => null,
'precision' => null,
'default' => null,
'null' => null,
'unsigned' => null,
'comment' => null,
];
$this->assertEquals($expected, $result);

$table->addColumn('amount', [
'type' => 'decimal'
]);
$result = $table->column('amount');
$expected = [
'type' => 'decimal',
'length' => null,
'precision' => null,
'default' => null,
'null' => null,
'unsigned' => null,
'comment' => null,
];
$this->assertEquals($expected, $result);
}

/**
Expand Down

0 comments on commit aabc2af

Please sign in to comment.