Skip to content

Commit

Permalink
Add very basic primary key support.
Browse files Browse the repository at this point in the history
Add basic single and multi column primary key support.
  • Loading branch information
markstory committed May 7, 2013
1 parent 5bfa029 commit 29bffc3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
12 changes: 10 additions & 2 deletions lib/Cake/Database/Schema/MysqlSchema.php
Expand Up @@ -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));
}
}

}
25 changes: 8 additions & 17 deletions lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php
Expand Up @@ -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));
Expand All @@ -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', [
Expand Down Expand Up @@ -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 = <<<SQL
CREATE TABLE `posts` (
Expand All @@ -463,16 +454,16 @@ public function testCreateTableSql() {
*
* @return MysqlSchema
*/
protected function _getMockedSchema() {
protected function _getMockedDriver() {
$driver = new \Cake\Database\Driver\Mysql();
$mock = $this->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;
}

}

0 comments on commit 29bffc3

Please sign in to comment.