Skip to content
This repository
Browse code

Add MySQL FULLTEXT support.

Minor optimizations and testing added.
Merge pull request #862 from bar/mysql-fulltext

Fixes #262
  • Loading branch information...
commit aaefbf1c2f927b0a7f7a2a1c487c2cffa6bc795f 1 parent e931211
Ber Clausen authored September 20, 2012 markstory committed September 20, 2012
35  lib/Cake/Model/Datasource/Database/Mysql.php
@@ -423,17 +423,21 @@ public function index($model) {
423 423
 		$table = $this->fullTableName($model);
424 424
 		$old = version_compare($this->getVersion(), '4.1', '<=');
425 425
 		if ($table) {
426  
-			$indices = $this->_execute('SHOW INDEX FROM ' . $table);
  426
+			$indexes = $this->_execute('SHOW INDEX FROM ' . $table);
427 427
 			// @codingStandardsIgnoreStart
428 428
 			// MySQL columns don't match the cakephp conventions.
429  
-			while ($idx = $indices->fetch(PDO::FETCH_OBJ)) {
  429
+			while ($idx = $indexes->fetch(PDO::FETCH_OBJ)) {
430 430
 				if ($old) {
431 431
 					$idx = (object)current((array)$idx);
432 432
 				}
433 433
 				if (!isset($index[$idx->Key_name]['column'])) {
434 434
 					$col = array();
435 435
 					$index[$idx->Key_name]['column'] = $idx->Column_name;
436  
-					$index[$idx->Key_name]['unique'] = intval($idx->Non_unique == 0);
  436
+					if ($idx->Index_type === 'FULLTEXT') {
  437
+						$index[$idx->Key_name]['type'] = strtolower($idx->Index_type);
  438
+					} else {
  439
+						$index[$idx->Key_name]['unique'] = intval($idx->Non_unique == 0);
  440
+					}
437 441
 				} else {
438 442
 					if (!empty($index[$idx->Key_name]['column']) && !is_array($index[$idx->Key_name]['column'])) {
439 443
 						$col[] = $index[$idx->Key_name]['column'];
@@ -443,7 +447,7 @@ public function index($model) {
443 447
 				}
444 448
 			}
445 449
 			// @codingStandardsIgnoreEnd
446  
-			$indices->closeCursor();
  450
+			$indexes->closeCursor();
447 451
 		}
448 452
 		return $index;
449 453
 	}
@@ -553,31 +557,18 @@ protected function _alterIndexes($table, $indexes) {
553 557
 		if (isset($indexes['drop'])) {
554 558
 			foreach ($indexes['drop'] as $name => $value) {
555 559
 				$out = 'DROP ';
556  
-				if ($name == 'PRIMARY') {
  560
+				if ($name === 'PRIMARY') {
557 561
 					$out .= 'PRIMARY KEY';
558 562
 				} else {
559  
-					$out .= 'KEY ' . $name;
  563
+					$out .= 'KEY ' . $this->startQuote . $name . $this->endQuote;
560 564
 				}
561 565
 				$alter[] = $out;
562 566
 			}
563 567
 		}
564 568
 		if (isset($indexes['add'])) {
565  
-			foreach ($indexes['add'] as $name => $value) {
566  
-				$out = 'ADD ';
567  
-				if ($name == 'PRIMARY') {
568  
-					$out .= 'PRIMARY ';
569  
-					$name = null;
570  
-				} else {
571  
-					if (!empty($value['unique'])) {
572  
-						$out .= 'UNIQUE ';
573  
-					}
574  
-				}
575  
-				if (is_array($value['column'])) {
576  
-					$out .= 'KEY ' . $name . ' (' . implode(', ', array_map(array(&$this, 'name'), $value['column'])) . ')';
577  
-				} else {
578  
-					$out .= 'KEY ' . $name . ' (' . $this->name($value['column']) . ')';
579  
-				}
580  
-				$alter[] = $out;
  569
+			$add = $this->buildIndex($indexes['add']);
  570
+			foreach ($add as $index) {
  571
+				$alter[] = 'ADD ' . $index;
581 572
 			}
582 573
 		}
583 574
 		return $alter;
4  lib/Cake/Model/Datasource/DboSource.php
@@ -3125,7 +3125,7 @@ protected function _buildFieldParameters($columnString, $columnData, $position)
3125 3125
 	}
3126 3126
 
3127 3127
 /**
3128  
- * Format indexes for create table
  3128
+ * Format indexes for create table.
3129 3129
  *
3130 3130
  * @param array $indexes
3131 3131
  * @param string $table
@@ -3141,6 +3141,8 @@ public function buildIndex($indexes, $table = null) {
3141 3141
 			} else {
3142 3142
 				if (!empty($value['unique'])) {
3143 3143
 					$out .= 'UNIQUE ';
  3144
+				} elseif (!empty($value['type']) && strtoupper($value['type']) === 'FULLTEXT') {
  3145
+					$out .= 'FULLTEXT ';
3144 3146
 				}
3145 3147
 				$name = $this->startQuote . $name . $this->endQuote;
3146 3148
 			}
17  lib/Cake/Test/Case/Model/CakeSchemaTest.php
@@ -763,6 +763,23 @@ public function testGenerateTable() {
763 763
 		);
764 764
 		$result = $this->Schema->generateTable('posts', $posts);
765 765
 		$this->assertRegExp('/public \$posts/', $result);
  766
+
  767
+		$posts = array(
  768
+			'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
  769
+			'author_id' => array('type' => 'integer', 'null' => false),
  770
+			'title' => array('type' => 'string', 'null' => false),
  771
+			'body' => array('type' => 'text', 'null' => true, 'default' => null),
  772
+			'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1),
  773
+			'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
  774
+			'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
  775
+			'indexes' => array(
  776
+				'PRIMARY' => array('column' => 'id', 'unique' => true),
  777
+				'MyFtIndex' => array('column' => array('title', 'body'), 'type' => 'fulltext')
  778
+			)
  779
+		);
  780
+		$result = $this->Schema->generateTable('fields', $posts);
  781
+		$this->assertRegExp('/public \$fields/', $result);
  782
+		$this->assertPattern('/\'type\' \=\> \'fulltext\'/', $result);
766 783
 	}
767 784
 
768 785
 /**
45  lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php
@@ -320,6 +320,16 @@ public function testIndexDetection() {
320 320
 		$result = $this->Dbo->index('with_multiple_compound_keys', false);
321 321
 		$this->Dbo->rawQuery('DROP TABLE ' . $name);
322 322
 		$this->assertEquals($expected, $result);
  323
+
  324
+		$name = $this->Dbo->fullTableName('with_fulltext');
  325
+		$this->Dbo->rawQuery('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, name varchar(255), description text, primary key(id), FULLTEXT KEY `MyFtIndex` ( `name`, `description` )) ENGINE=MyISAM;');
  326
+		$expected = array(
  327
+			'PRIMARY' => array('column' => 'id', 'unique' => 1),
  328
+			'MyFtIndex' => array('column' => array('name', 'description'), 'type' => 'fulltext')
  329
+		);
  330
+		$result = $this->Dbo->index('with_fulltext', false);
  331
+		$this->Dbo->rawQuery('DROP TABLE ' . $name);
  332
+		$this->assertEquals($expected, $result);
323 333
 	}
324 334
 
325 335
 /**
@@ -559,9 +569,9 @@ public function testAlterSchemaIndexes() {
559 569
 
560 570
 		$result = $this->Dbo->alterSchema($schemaB->compare($schemaA));
561 571
 		$this->assertContains("ALTER TABLE $table", $result);
562  
-		$this->assertContains('ADD KEY name_idx (`name`),', $result);
563  
-		$this->assertContains('ADD KEY group_idx (`group1`),', $result);
564  
-		$this->assertContains('ADD KEY compound_idx (`group1`, `group2`),', $result);
  572
+		$this->assertContains('ADD KEY `name_idx` (`name`),', $result);
  573
+		$this->assertContains('ADD KEY `group_idx` (`group1`),', $result);
  574
+		$this->assertContains('ADD KEY `compound_idx` (`group1`, `group2`),', $result);
565 575
 		$this->assertContains('ADD PRIMARY KEY  (`id`);', $result);
566 576
 
567 577
 		//Test that the string is syntactically correct
@@ -587,13 +597,13 @@ public function testAlterSchemaIndexes() {
587 597
 		$result = $this->Dbo->alterSchema($schemaC->compare($schemaB));
588 598
 		$this->assertContains("ALTER TABLE $table", $result);
589 599
 		$this->assertContains('DROP PRIMARY KEY,', $result);
590  
-		$this->assertContains('DROP KEY name_idx,', $result);
591  
-		$this->assertContains('DROP KEY group_idx,', $result);
592  
-		$this->assertContains('DROP KEY compound_idx,', $result);
593  
-		$this->assertContains('ADD KEY id_name_idx (`id`, `name`),', $result);
594  
-		$this->assertContains('ADD UNIQUE KEY name_idx (`name`),', $result);
595  
-		$this->assertContains('ADD KEY group_idx (`group2`),', $result);
596  
-		$this->assertContains('ADD KEY compound_idx (`group2`, `group1`);', $result);
  600
+		$this->assertContains('DROP KEY `name_idx`,', $result);
  601
+		$this->assertContains('DROP KEY `group_idx`,', $result);
  602
+		$this->assertContains('DROP KEY `compound_idx`,', $result);
  603
+		$this->assertContains('ADD KEY `id_name_idx` (`id`, `name`),', $result);
  604
+		$this->assertContains('ADD UNIQUE KEY `name_idx` (`name`),', $result);
  605
+		$this->assertContains('ADD KEY `group_idx` (`group2`),', $result);
  606
+		$this->assertContains('ADD KEY `compound_idx` (`group2`, `group1`);', $result);
597 607
 
598 608
 		$query = $this->Dbo->getConnection()->prepare($result);
599 609
 		$this->assertEquals($query->queryString, $result);
@@ -605,10 +615,10 @@ public function testAlterSchemaIndexes() {
605 615
 		$result = $this->Dbo->alterSchema($schemaA->compare($schemaC));
606 616
 
607 617
 		$this->assertContains("ALTER TABLE $table", $result);
608  
-		$this->assertContains('DROP KEY name_idx,', $result);
609  
-		$this->assertContains('DROP KEY group_idx,', $result);
610  
-		$this->assertContains('DROP KEY compound_idx,', $result);
611  
-		$this->assertContains('DROP KEY id_name_idx;', $result);
  618
+		$this->assertContains('DROP KEY `name_idx`,', $result);
  619
+		$this->assertContains('DROP KEY `group_idx`,', $result);
  620
+		$this->assertContains('DROP KEY `compound_idx`,', $result);
  621
+		$this->assertContains('DROP KEY `id_name_idx`;', $result);
612 622
 
613 623
 		$query = $this->Dbo->getConnection()->prepare($result);
614 624
 		$this->assertEquals($query->queryString, $result);
@@ -2867,6 +2877,13 @@ public function testBuildIndex() {
2867 2877
 		$result = $this->Dbo->buildIndex($data);
2868 2878
 		$expected = array('UNIQUE KEY `MyIndex` (`id`, `name`)');
2869 2879
 		$this->assertEquals($expected, $result);
  2880
+
  2881
+		$data = array(
  2882
+			'MyFtIndex' => array('column' => array('name', 'description'), 'type' => 'fulltext')
  2883
+		);
  2884
+		$result = $this->Dbo->buildIndex($data);
  2885
+		$expected = array('FULLTEXT KEY `MyFtIndex` (`name`, `description`)');
  2886
+		$this->assertEquals($expected, $result);
2870 2887
 	}
2871 2888
 
2872 2889
 /**

0 notes on commit aaefbf1

Please sign in to comment.
Something went wrong with that request. Please try again.