Skip to content

Commit

Permalink
Add basic index support.
Browse files Browse the repository at this point in the history
Basic indexes can now be added. Foreign keys are not complete yet.
  • Loading branch information
markstory committed Apr 20, 2013
1 parent 5295350 commit 62115e1
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 2 deletions.
58 changes: 58 additions & 0 deletions lib/Cake/Database/Schema/Table.php
Expand Up @@ -16,6 +16,8 @@
*/
namespace Cake\Database\Schema;

use Cake\Error;

/**
* Represents a single table in a database schema.
*
Expand Down Expand Up @@ -66,6 +68,39 @@ class Table {
'charset' => null,
];

/**
* The valid keys that can be used in an index
* definition.
*
* @var array
*/
protected $_indexKeys = [
'type' => null,
'columns' => [],
];

/**
* Names of the valid index types.
*
* @var array
*/
protected $_validIndexTypes = [
self::INDEX_PRIMARY,
self::INDEX_INDEX,
self::INDEX_UNIQUE,
self::INDEX_FOREIGN,
];

const INDEX_PRIMARY = 'primary';
const INDEX_INDEX = 'index';
const INDEX_UNIQUE = 'unique';
const INDEX_FOREIGN = 'foreign';

/**
* Constructor.
*
* @param string $table The table name.
*/
public function __construct($table) {
$this->_table = $table;
}
Expand Down Expand Up @@ -129,11 +164,34 @@ public function column($name) {
/**
* Add an index or key.
*
* Used to add primary keys, indexes, and foreign keys
* to a table.
*
* ### Attributes
*
* - `type` The type of index being added.
* - `columns` The columns in the index.
*
* @param string $name The name of the index.
* @param array $attrs The attributes for the index.
* @return Table $this
* @throws Cake\Error\Exception
*/
public function addIndex($name, $attrs) {
if (is_string($attrs)) {
$attrs = ['type' => $attrs];
}
$attrs = array_intersect_key($attrs, $this->_indexKeys);
$attrs = $attrs + $this->_indexKeys;

if (!in_array($attrs['type'], $this->_validIndexTypes, true)) {
throw new Error\Exception(__d('cake_dev', 'Invalid index type'));
}
foreach ($attrs['columns'] as $field) {
if (empty($this->_columns[$field])) {
throw new Error\Exception(__d('cake_dev', 'Columns used in indexes must already exist.'));
}
}
$this->_indexes[$name] = $attrs;
return $this;
}
Expand Down
48 changes: 46 additions & 2 deletions lib/Cake/Test/TestCase/Database/Schema/TableTest.php
Expand Up @@ -86,13 +86,57 @@ public function testAddIndex() {
$this->assertEquals(['primary'], $table->indexes());
}

/**
* Test that an exception is raised when indexes
* are added for fields that do not exist.
*
* @expectedException Cake\Error\Exception
* @return void
*/
public function testAddIndexErrorWhenFieldIsMissing() {
$table = new Table('articles');
$table->addIndex('author_idx', [
'columns' => ['author_id']
]);
}

public function testAddIndexForeign() {
/**
* Test that exceptions are raised when indexes
* are added with invalid types
*
* @expectedException Cake\Error\Exception
* @return void
*/
public function testAddIndexErrorWrongType() {
$table = new Table('articles');
$table->addColumn('author_id', 'integer')
->addIndex('author_idx', [
'type' => 'derp',
'columns' => ['author_id']
]);
}

/**
* Test adding different kinds of indexes.
*
* @return void
*/
public function testAddIndexTypes() {
$table = new Table('articles');
$table->addColumn('title', 'string')
->addColumn('author_id', 'integer');

$table->addIndex('author_idx', [
'fields' => ['author_id'],
'type' => 'unique'
]);
}

/**
* Test adding foreign keys.
*
* @return void
*/
public function testAddIndexForeign() {
}

}

0 comments on commit 62115e1

Please sign in to comment.