Merge pull request #997 from rhelms/2.2.5-text-index-in-fixture

Cater for length part of TEXT field in indexes for MySQL'

Fixes #3428
This commit is contained in:
Mark Story 2012-12-12 17:27:30 -08:00
commit 45f6ade800
4 changed files with 133 additions and 36 deletions

View file

@ -574,13 +574,17 @@ class CakeSchema extends Object {
if (is_array($values)) { if (is_array($values)) {
foreach ($values as $key => $val) { foreach ($values as $key => $val) {
if (is_array($val)) { if (is_array($val)) {
$vals[] = "'{$key}' => array('" . implode("', '", $val) . "')"; $vals[] = "'{$key}' => array(" . implode(", ", $this->_values($val)) . ")";
} elseif (!is_numeric($key)) { } else {
$val = var_export($val, true); $val = var_export($val, true);
if ($val === 'NULL') { if ($val === 'NULL') {
$val = 'null'; $val = 'null';
} }
$vals[] = "'{$key}' => {$val}"; if (!is_numeric($key)) {
$vals[] = "'{$key}' => {$val}";
} else {
$vals[] = "{$val}";
}
} }
} }
} }

View file

@ -443,6 +443,12 @@ class Mysql extends DboSource {
$col[] = $idx->Column_name; $col[] = $idx->Column_name;
$index[$idx->Key_name]['column'] = $col; $index[$idx->Key_name]['column'] = $col;
} }
if (!empty($idx->Sub_part)) {
if (!isset($index[$idx->Key_name]['length'])) {
$index[$idx->Key_name]['length'] = array();
}
$index[$idx->Key_name]['length'][$idx->Column_name] = $idx->Sub_part;
}
} }
// @codingStandardsIgnoreEnd // @codingStandardsIgnoreEnd
$indices->closeCursor(); $indices->closeCursor();
@ -543,6 +549,55 @@ class Mysql extends DboSource {
return array(); return array();
} }
/**
* Format indexes for create table
*
* @param array $indexes An array of indexes to generate SQL from
* @param string $table Optional table name, not used
* @return array An array of SQL statements for indexes
* @see DboSource::buildIndex()
*/
public function buildIndex($indexes, $table = null) {
$join = array();
foreach ($indexes as $name => $value) {
$out = '';
if ($name === 'PRIMARY') {
$out .= 'PRIMARY ';
$name = null;
} else {
if (!empty($value['unique'])) {
$out .= 'UNIQUE ';
}
$name = $this->startQuote . $name . $this->endQuote;
}
// length attribute only used for MySQL datasource, for TEXT/BLOB index columns
$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;
}
return $join;
}
/** /**
* Generate MySQL index alteration statements for a table. * Generate MySQL index alteration statements for a table.
* *
@ -558,33 +613,37 @@ class Mysql extends DboSource {
if ($name == 'PRIMARY') { if ($name == 'PRIMARY') {
$out .= 'PRIMARY KEY'; $out .= 'PRIMARY KEY';
} else { } else {
$out .= 'KEY ' . $name; $out .= 'KEY ' . $this->startQuote . $name . $this->endQuote;
} }
$alter[] = $out; $alter[] = $out;
} }
} }
if (isset($indexes['add'])) { if (isset($indexes['add'])) {
foreach ($indexes['add'] as $name => $value) { $add = $this->buildIndex($indexes['add']);
$out = 'ADD '; foreach ($add as $index) {
if ($name == 'PRIMARY') { $alter[] = 'ADD ' . $index;
$out .= 'PRIMARY ';
$name = null;
} else {
if (!empty($value['unique'])) {
$out .= 'UNIQUE ';
}
}
if (is_array($value['column'])) {
$out .= 'KEY ' . $name . ' (' . implode(', ', array_map(array(&$this, 'name'), $value['column'])) . ')';
} else {
$out .= 'KEY ' . $name . ' (' . $this->name($value['column']) . ')';
}
$alter[] = $out;
} }
} }
return $alter; return $alter;
} }
/**
* Format length for text indexes
*
* @param array $lengths An array of lengths for a single index
* @param string $column The column for which to generate the index length
* @return string Formatted length part of an index field
*/
protected function _buildIndexSubPart($lengths, $column) {
if (is_null($lengths)) {
return '';
}
if (!isset($lengths[$column])) {
return '';
}
return '(' . $lengths[$column] . ')';
}
/** /**
* Returns an detailed array of sources (tables) in the database. * Returns an detailed array of sources (tables) in the database.
* *

View file

@ -830,7 +830,7 @@ class DboSource extends DataSource {
} }
if (preg_match('/^([\w-]+)\((.*)\)$/', $data, $matches)) { // Functions if (preg_match('/^([\w-]+)\((.*)\)$/', $data, $matches)) { // Functions
return $this->cacheMethod(__FUNCTION__, $cacheKey, return $this->cacheMethod(__FUNCTION__, $cacheKey,
$matches[1] . '(' . $this->name($matches[2]) . ')' $matches[1] . '(' . $this->name($matches[2]) . ')'
); );
} }
if ( if (

View file

@ -313,6 +313,26 @@ class MysqlTest extends CakeTestCase {
$result = $this->Dbo->index('with_multiple_compound_keys', false); $result = $this->Dbo->index('with_multiple_compound_keys', false);
$this->Dbo->rawQuery('DROP TABLE ' . $name); $this->Dbo->rawQuery('DROP TABLE ' . $name);
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$name = $this->Dbo->fullTableName('with_text_index');
$this->Dbo->rawQuery('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, text_field text, primary key(id), KEY `text_index` ( `text_field`(20) ));');
$expected = array(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'text_index' => array('column' => 'text_field', 'unique' => 0, 'length' => array('text_field' => 20)),
);
$result = $this->Dbo->index('with_text_index', false);
$this->Dbo->rawQuery('DROP TABLE ' . $name);
$this->assertEquals($expected, $result);
$name = $this->Dbo->fullTableName('with_compound_text_index');
$this->Dbo->rawQuery('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, text_field1 text, text_field2 text, primary key(id), KEY `text_index` ( `text_field1`(20), `text_field2`(20) ));');
$expected = array(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'text_index' => array('column' => array('text_field1', 'text_field2'), 'unique' => 0, 'length' => array('text_field1' => 20, 'text_field2' => 20)),
);
$result = $this->Dbo->index('with_compound_text_index', false);
$this->Dbo->rawQuery('DROP TABLE ' . $name);
$this->assertEquals($expected, $result);
} }
/** /**
@ -548,9 +568,9 @@ class MysqlTest extends CakeTestCase {
$result = $this->Dbo->alterSchema($schemaB->compare($schemaA)); $result = $this->Dbo->alterSchema($schemaB->compare($schemaA));
$this->assertContains("ALTER TABLE $table", $result); $this->assertContains("ALTER TABLE $table", $result);
$this->assertContains('ADD KEY name_idx (`name`),', $result); $this->assertContains('ADD KEY `name_idx` (`name`),', $result);
$this->assertContains('ADD KEY group_idx (`group1`),', $result); $this->assertContains('ADD KEY `group_idx` (`group1`),', $result);
$this->assertContains('ADD KEY compound_idx (`group1`, `group2`),', $result); $this->assertContains('ADD KEY `compound_idx` (`group1`, `group2`),', $result);
$this->assertContains('ADD PRIMARY KEY (`id`);', $result); $this->assertContains('ADD PRIMARY KEY (`id`);', $result);
//Test that the string is syntactically correct //Test that the string is syntactically correct
@ -576,13 +596,13 @@ class MysqlTest extends CakeTestCase {
$result = $this->Dbo->alterSchema($schemaC->compare($schemaB)); $result = $this->Dbo->alterSchema($schemaC->compare($schemaB));
$this->assertContains("ALTER TABLE $table", $result); $this->assertContains("ALTER TABLE $table", $result);
$this->assertContains('DROP PRIMARY KEY,', $result); $this->assertContains('DROP PRIMARY KEY,', $result);
$this->assertContains('DROP KEY name_idx,', $result); $this->assertContains('DROP KEY `name_idx`,', $result);
$this->assertContains('DROP KEY group_idx,', $result); $this->assertContains('DROP KEY `group_idx`,', $result);
$this->assertContains('DROP KEY compound_idx,', $result); $this->assertContains('DROP KEY `compound_idx`,', $result);
$this->assertContains('ADD KEY id_name_idx (`id`, `name`),', $result); $this->assertContains('ADD KEY `id_name_idx` (`id`, `name`),', $result);
$this->assertContains('ADD UNIQUE KEY name_idx (`name`),', $result); $this->assertContains('ADD UNIQUE KEY `name_idx` (`name`),', $result);
$this->assertContains('ADD KEY group_idx (`group2`),', $result); $this->assertContains('ADD KEY `group_idx` (`group2`),', $result);
$this->assertContains('ADD KEY compound_idx (`group2`, `group1`);', $result); $this->assertContains('ADD KEY `compound_idx` (`group2`, `group1`);', $result);
$query = $this->Dbo->getConnection()->prepare($result); $query = $this->Dbo->getConnection()->prepare($result);
$this->assertEquals($query->queryString, $result); $this->assertEquals($query->queryString, $result);
@ -594,10 +614,10 @@ class MysqlTest extends CakeTestCase {
$result = $this->Dbo->alterSchema($schemaA->compare($schemaC)); $result = $this->Dbo->alterSchema($schemaA->compare($schemaC));
$this->assertContains("ALTER TABLE $table", $result); $this->assertContains("ALTER TABLE $table", $result);
$this->assertContains('DROP KEY name_idx,', $result); $this->assertContains('DROP KEY `name_idx`,', $result);
$this->assertContains('DROP KEY group_idx,', $result); $this->assertContains('DROP KEY `group_idx`,', $result);
$this->assertContains('DROP KEY compound_idx,', $result); $this->assertContains('DROP KEY `compound_idx`,', $result);
$this->assertContains('DROP KEY id_name_idx;', $result); $this->assertContains('DROP KEY `id_name_idx`;', $result);
$query = $this->Dbo->getConnection()->prepare($result); $query = $this->Dbo->getConnection()->prepare($result);
$this->assertEquals($query->queryString, $result); $this->assertEquals($query->queryString, $result);
@ -2908,6 +2928,20 @@ class MysqlTest extends CakeTestCase {
$result = $this->Dbo->buildIndex($data); $result = $this->Dbo->buildIndex($data);
$expected = array('UNIQUE KEY `MyIndex` (`id`, `name`)'); $expected = array('UNIQUE KEY `MyIndex` (`id`, `name`)');
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$data = array(
'MyTextIndex' => array('column' => 'text_field', 'length' => array('text_field' => 20))
);
$result = $this->Dbo->buildIndex($data);
$expected = array('KEY `MyTextIndex` (`text_field`(20))');
$this->assertEquals($expected, $result);
$data = array(
'MyMultiTextIndex' => array('column' => array('text_field1', 'text_field2'), 'length' => array('text_field1' => 20, 'text_field2' => 20))
);
$result = $this->Dbo->buildIndex($data);
$expected = array('KEY `MyMultiTextIndex` (`text_field1`(20), `text_field2`(20))');
$this->assertEquals($expected, $result);
} }
/** /**
@ -2978,7 +3012,7 @@ class MysqlTest extends CakeTestCase {
'type' => 'timestamp', 'type' => 'timestamp',
'default' => 'current_timestamp', 'default' => 'current_timestamp',
'null' => false, 'null' => false,
); );
$result = $this->Dbo->buildColumn($data); $result = $this->Dbo->buildColumn($data);
$expected = '`created` timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL'; $expected = '`created` timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);