Skip to content
Permalink
Browse files

Add various kinds of non length based indexes.

Add support for the main non-foriegn key types used in MySQL.
I still need to add support for indexes with lengths.
  • Loading branch information...
markstory committed May 7, 2013
1 parent 29bffc3 commit 55f7c98fbed3ff6b5fd51a2ae78314940e7db6d7
@@ -287,6 +287,62 @@ public function indexSql(Table $table, $name) {
);
return sprintf('PRIMARY KEY (%s)', implode(', ', $columns));
}
if ($data['type'] === Table::INDEX_UNIQUE) {
$out = 'UNIQUE KEY ';
}
if ($data['type'] === Table::INDEX_INDEX) {
$out = 'KEY ';
}
if ($data['type'] === Table::INDEX_FULLTEXT) {
$out = 'FULLTEXT KEY ';
}
// TODO finish MySQL indexes with length
$out .= $this->_driver->quoteIdentifier($name);
$columns = array_map(
[$this->_driver, 'quoteIdentifier'],
$data['columns']
);
return $out . ' (' . implode(', ', $columns) . ')';
/*
$out = '';
if ($name === 'PRIMARY') {
$out .= 'PRIMARY ';
$name = null;
} else {
if (!empty($value['unique'])) {
$out .= 'UNIQUE ';
}
$name = $this->startQuote . $name . $this->endQuote;
}
if (isset($value['type']) && strtolower($value['type']) === 'fulltext') {
$out .= 'FULLTEXT ';
}
$out .= 'KEY ' . $name . ' (';
if (is_array($value['column'])) {
if (isset($value['length'])) {
$vals = array();
foreach ($value['column'] as $column) {
$name = $this->name($column);
if (isset($value['length'])) {
$name .= $this->_buildIndexSubPart($value['length'], $column);
}
$vals[] = $name;
}
$out .= implode(', ', $vals);
} else {
$out .= implode(', ', array_map(array(&$this, 'name'), $value['column']));
}
} else {
$out .= $this->name($value['column']);
if (isset($value['length'])) {
$out .= $this->_buildIndexSubPart($value['length'], $value['column']);
}
}
$out .= ')';
$join[] = $out;
*/
}
}
@@ -90,11 +90,13 @@ class Table {
self::INDEX_INDEX,
self::INDEX_UNIQUE,
self::INDEX_FOREIGN,
self::INDEX_FULLTEXT,
];
const INDEX_PRIMARY = 'primary';
const INDEX_INDEX = 'index';
const INDEX_UNIQUE = 'unique';
const INDEX_FULLTEXT = 'fulltext';
const INDEX_FOREIGN = 'foreign';
/**
@@ -410,12 +410,12 @@ public function testColumnSqlPrimaryKey() {
}
/**
* Integration test for converting a Schema\Table into MySQL table creates.
* Provider for table creation tests.
*
* @return void
* @return array
*/
public function testCreateTableSql() {
$table = (new Table('posts'))->addColumn('id', [
public static function createTableProvider() {
$basic = (new Table('posts'))->addColumn('id', [
'type' => 'integer',
'null' => false
])
@@ -431,13 +431,7 @@ public function testCreateTableSql() {
'columns' => ['id']
]);
$driver = $this->_getMockedDriver();
$connection = $this->getMock('Cake\Database\Connection', array(), array(), '', false);
$connection->expects($this->any())->method('driver')
->will($this->returnValue($driver));
$result = $table->createTableSql($connection);
$expected = <<<SQL
$basicExpected = <<<SQL
CREATE TABLE `posts` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL COMMENT "The title",
@@ -446,6 +440,77 @@ public function testCreateTableSql() {
PRIMARY KEY (`id`)
);
SQL;
$uniqueKey = (new Table('articles_authors'))
->addColumn('author_id', [
'type' => 'integer',
'null' => false
])
->addColumn('article_id', [
'type' => 'integer',
'null' => false,
])
->addIndex('unique_idx', [
'type' => 'unique',
'columns' => ['author_id', 'article_id']
]);
$uniqueExpected = <<<SQL
CREATE TABLE `articles_authors` (
`author_id` INTEGER NOT NULL,
`article_id` INTEGER NOT NULL,
UNIQUE KEY `unique_idx` (`author_id`, `article_id`)
);
SQL;
$simpleKey = (new Table('things'))
->addColumn('thing_id', [
'type' => 'integer',
])
->addIndex('thing_id_idx', [
'type' => 'index',
'columns' => ['thing_id']
]);
$simpleKeyExpected = <<<SQL
CREATE TABLE `things` (
`thing_id` INTEGER,
KEY `thing_id_idx` (`thing_id`)
);
SQL;
$fullText = (new Table('things'))
->addColumn('stuff', [
'type' => 'text',
])
->addIndex('stuff_idx', [
'type' => 'fulltext',
'columns' => ['stuff']
]);
$fullTextExpected = <<<SQL
CREATE TABLE `things` (
`stuff` TEXT,
FULLTEXT KEY `stuff_idx` (`stuff`)
);
SQL;
return [
[$basic, $basicExpected],
[$uniqueKey, $uniqueExpected],
[$fullText, $fullTextExpected],
[$simpleKey, $simpleKeyExpected],
];
}
/**
* Integration test for converting a Schema\Table into MySQL table creates.
*
* @dataProvider createTableProvider
* @return void
*/
public function testCreateTableSql($table, $expected) {
$driver = $this->_getMockedDriver();
$connection = $this->getMock('Cake\Database\Connection', array(), array(), '', false);
$connection->expects($this->any())->method('driver')
->will($this->returnValue($driver));
$result = $table->createTableSql($connection);
$this->assertEquals($expected, $result);
}
@@ -462,6 +527,11 @@ protected function _getMockedDriver() {
->will($this->returnCallback(function ($value) {
return '"' . $value . '"';
}));
$mock->expects($this->any())
->method('quoteIdentifier')
->will($this->returnCallback(function ($value) {
return '`' . $value . '`';
}));
$driver->connection($mock);
return $driver;
}

0 comments on commit 55f7c98

Please sign in to comment.
You can’t perform that action at this time.