Permalink
Browse files

Start to add indexes.

This still needs a pile of work.
  • Loading branch information...
markstory committed Apr 19, 2013
1 parent 612e599 commit 52953504f25ea5f7b79963719c0eb03ec9b6e18d
Showing with 92 additions and 0 deletions.
  1. +58 −0 lib/Cake/Database/Schema/Table.php
  2. +34 −0 lib/Cake/Test/TestCase/Database/Schema/TableTest.php
@@ -42,12 +42,28 @@ class Table {
*/
protected $_columns = [];
/**
* Indexes + Keys in the table.
*
* @var array
*/
protected $_indexes = [];
/**
* The valid keys that can be used in a column
* definition.
*
* @var array
*/
protected $_columnKeys = [
'type' => null,
'length' => null,
'null' => null,
'default' => null,
'fixed' => null,
'comment' => null,
'collate' => null,
'charset' => null,
];
public function __construct($table) {
@@ -57,11 +73,32 @@ public function __construct($table) {
/**
* Add a column to the table.
*
* ### Attributes
*
* Columns can have several attributes:
*
* - `type` The type of the column. This should be
* one of CakePHP's abstract types.
* - `length` The length of the column.
* - `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.
*
* In addition to the above keys, the following keys are
* implemented in some database dialects, but not all:
*
* - `comment` The comment for the column.
* - `charset` The charset for the column.
* - `collate` The collation for the column.
*
* @param string $name The name of the column
* @param array $attrs The attributes for the column.
* @return Table $this
*/
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;
return $this;
@@ -89,4 +126,25 @@ public function column($name) {
return $this->_columns[$name];
}
/**
* Add an index or key.
*
* @param string $name The name of the index.
* @param array $attrs The attributes for the index.
* @return Table $this
*/
public function addIndex($name, $attrs) {
$this->_indexes[$name] = $attrs;
return $this;
}
/**
* Get the names of all the indexes in the table.
*
* @return array
*/
public function indexes() {
return array_keys($this->_indexes);
}
}
@@ -38,6 +38,10 @@ public function testAddColumn() {
]);
$this->assertSame($table, $result);
$this->assertEquals(['title'], $table->columns());
$result = $table->addColumn('body', 'text');
$this->assertSame($table, $result);
$this->assertEquals(['title', 'body'], $table->columns());
}
/**
@@ -57,8 +61,38 @@ public function testAddColumnFiltersAttributes() {
'default' => null,
'null' => null,
'fixed' => null,
'comment' => null,
'collate' => null,
'charset' => null,
];
$this->assertEquals($expected, $result);
}
/**
* Test adding an index.
*
* @return void
*/
public function testAddIndex() {
$table = new Table('articles');
$table->addColumn('id', [
'type' => 'integer'
]);
$result = $table->addIndex('primary', [
'type' => 'primary',
'columns' => ['id']
]);
$this->assertSame($result, $table);
$this->assertEquals(['primary'], $table->indexes());
}
public function testAddIndexErrorWhenFieldIsMissing() {
}
public function testAddIndexForeign() {
}
public function testAddIndexTypes() {
}
}

0 comments on commit 5295350

Please sign in to comment.