Skip to content

Commit 59dd147

Browse files
committed
Add dropTableSql()
Add tests for MysqlSchema + Table.
1 parent 1de2699 commit 59dd147

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

lib/Cake/Database/Schema/MysqlSchema.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ public function extraSchemaColumns() {
168168
];
169169
}
170170

171+
/**
172+
* Generate the SQL to drop a table.
173+
*
174+
* @param Cake\Database\Schema\Table $table Table instance
175+
* @return string DROP TABLE sql
176+
*/
177+
public function dropTableSql(Table $table) {
178+
return sprintf("DROP TABLE `%s`", $table->name());
179+
}
180+
171181
/**
172182
* Generate the SQL to create a table.
173183
*
@@ -177,7 +187,7 @@ public function extraSchemaColumns() {
177187
* @param array $indexes The indexes for the table.
178188
* @return array Complete CREATE TABLE statement(s)
179189
*/
180-
public function createTableSql($table, $columns, $constraints, $indexes) {
190+
public function createTableSql(Table $table, $columns, $constraints, $indexes) {
181191
$content = implode(",\n", array_merge($columns, $constraints, $indexes));
182192
return [sprintf("CREATE TABLE `%s` (\n%s\n)", $table->name(), $content)];
183193
}

lib/Cake/Database/Schema/Table.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,17 @@ public function createTableSql(Connection $connection) {
349349
return $dialect->createTableSql($this, $columns, $constraints, $indexes);
350350
}
351351

352+
/**
353+
* Generate the SQL to drop a table.
354+
*
355+
* Uses the connection to access the schema dialect to generate platform
356+
* specific SQL.
357+
*
358+
* @param Connection $connection The connection to generate SQL for.
359+
* @return string SQL to drop a table.
360+
*/
361+
public function dropTableSql(Connection $connection) {
362+
$dialect = $connection->driver()->schemaDialect();
363+
return $dialect->dropTableSql($this);
364+
}
352365
}

lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,22 @@ public function testCreateTableSql() {
559559
$this->assertEquals($expected, $result[0]);
560560
}
561561

562+
/**
563+
* test dropTableSql
564+
*
565+
* @return void
566+
*/
567+
public function testDropTableSql() {
568+
$driver = $this->_getMockedDriver();
569+
$connection = $this->getMock('Cake\Database\Connection', array(), array(), '', false);
570+
$connection->expects($this->any())->method('driver')
571+
->will($this->returnValue($driver));
572+
573+
$table = new Table('articles');
574+
$result = $table->dropTableSql($connection);
575+
$this->assertEquals('DROP TABLE `articles`', $result);
576+
}
577+
562578
/**
563579
* Get a schema instance with a mocked driver/pdo instances
564580
*

0 commit comments

Comments
 (0)