Skip to content

Commit

Permalink
Add dropTableSql()
Browse files Browse the repository at this point in the history
Add tests for MysqlSchema + Table.
  • Loading branch information
markstory committed May 25, 2013
1 parent 1de2699 commit 59dd147
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/Cake/Database/Schema/MysqlSchema.php
Expand Up @@ -168,6 +168,16 @@ public function extraSchemaColumns() {
];
}

/**
* Generate the SQL to drop a table.
*
* @param Cake\Database\Schema\Table $table Table instance
* @return string DROP TABLE sql
*/
public function dropTableSql(Table $table) {
return sprintf("DROP TABLE `%s`", $table->name());
}

/**
* Generate the SQL to create a table.
*
Expand All @@ -177,7 +187,7 @@ public function extraSchemaColumns() {
* @param array $indexes The indexes for the table.
* @return array Complete CREATE TABLE statement(s)
*/
public function createTableSql($table, $columns, $constraints, $indexes) {
public function createTableSql(Table $table, $columns, $constraints, $indexes) {
$content = implode(",\n", array_merge($columns, $constraints, $indexes));
return [sprintf("CREATE TABLE `%s` (\n%s\n)", $table->name(), $content)];
}
Expand Down
13 changes: 13 additions & 0 deletions lib/Cake/Database/Schema/Table.php
Expand Up @@ -349,4 +349,17 @@ public function createTableSql(Connection $connection) {
return $dialect->createTableSql($this, $columns, $constraints, $indexes);
}

/**
* Generate the SQL to drop a table.
*
* Uses the connection to access the schema dialect to generate platform
* specific SQL.
*
* @param Connection $connection The connection to generate SQL for.
* @return string SQL to drop a table.
*/
public function dropTableSql(Connection $connection) {
$dialect = $connection->driver()->schemaDialect();
return $dialect->dropTableSql($this);
}
}
16 changes: 16 additions & 0 deletions lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php
Expand Up @@ -559,6 +559,22 @@ public function testCreateTableSql() {
$this->assertEquals($expected, $result[0]);
}

/**
* test dropTableSql
*
* @return void
*/
public function testDropTableSql() {
$driver = $this->_getMockedDriver();
$connection = $this->getMock('Cake\Database\Connection', array(), array(), '', false);
$connection->expects($this->any())->method('driver')
->will($this->returnValue($driver));

$table = new Table('articles');
$result = $table->dropTableSql($connection);
$this->assertEquals('DROP TABLE `articles`', $result);
}

/**
* Get a schema instance with a mocked driver/pdo instances
*
Expand Down

0 comments on commit 59dd147

Please sign in to comment.