From 29bffc3101790ad341012af803db28f9450e401c Mon Sep 17 00:00:00 2001 From: Mark Story Date: Mon, 6 May 2013 22:02:20 -0400 Subject: [PATCH] Add very basic primary key support. Add basic single and multi column primary key support. --- lib/Cake/Database/Schema/MysqlSchema.php | 12 +++++++-- .../Database/Schema/MysqlSchemaTest.php | 25 ++++++------------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/lib/Cake/Database/Schema/MysqlSchema.php b/lib/Cake/Database/Schema/MysqlSchema.php index bf04c772238..8a42adf59bc 100644 --- a/lib/Cake/Database/Schema/MysqlSchema.php +++ b/lib/Cake/Database/Schema/MysqlSchema.php @@ -274,11 +274,19 @@ protected function _value($value) { /** * Generate the SQL fragment for a single index in MySQL * + * @param Cake\Database\Schema\Table $table The table object the column is in. * @param string $name The name of the column. - * @param array $data The attributes for the column. * @return string SQL fragment. */ - public function indexSql($name, $data) { + public function indexSql(Table $table, $name) { + $data = $table->index($name); + if ($data['type'] === Table::INDEX_PRIMARY) { + $columns = array_map( + [$this->_driver, 'quoteIdentifier'], + $data['columns'] + ); + return sprintf('PRIMARY KEY (%s)', implode(', ', $columns)); + } } } diff --git a/lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php b/lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php index 3c1c08fca59..268fd487611 100644 --- a/lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php +++ b/lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php @@ -368,7 +368,8 @@ public static function columnSqlProvider() { * @return void */ public function testColumnSql($name, $data, $expected) { - $schema = $this->_getMockedSchema(); + $driver = $this->_getMockedDriver(); + $schema = new MysqlSchema($driver); $table = (new Table('articles'))->addColumn($name, $data); $this->assertEquals($expected, $schema->columnSql($table, $name)); @@ -380,7 +381,8 @@ public function testColumnSql($name, $data, $expected) { * @return void */ public function testColumnSqlPrimaryKey() { - $schema = $this->_getMockedSchema(); + $driver = $this->_getMockedDriver(); + $schema = new MysqlSchema($driver); $table = new Table('articles'); $table->addColumn('id', [ @@ -429,22 +431,11 @@ public function testCreateTableSql() { 'columns' => ['id'] ]); + $driver = $this->_getMockedDriver(); $connection = $this->getMock('Cake\Database\Connection', array(), array(), '', false); - $driver = new \Cake\Database\Driver\Mysql(); - $mock = $this->getMock('FakePdo', ['quote']); - $driver->connection($mock); - - $dialect = new MysqlSchema($driver); - $connection->expects($this->any())->method('driver') ->will($this->returnValue($driver)); - $mock->expects($this->any()) - ->method('quote') - ->will($this->returnCallback(function ($value) { - return '"' . $value . '"'; - })); - $result = $table->createTableSql($connection); $expected = <<getMock('FakePdo', ['quote']); + $mock = $this->getMock('FakePdo', ['quote', 'quoteIdentifier']); $mock->expects($this->any()) ->method('quote') ->will($this->returnCallback(function ($value) { return '"' . $value . '"'; })); $driver->connection($mock); - return new MysqlSchema($driver); + return $driver; } }