Skip to content
Permalink
Browse files

Add a BaseSchema class and move methods that are purely duplicated down.

  • Loading branch information...
markstory committed Jun 26, 2013
1 parent 690f890 commit c67357365ddc9d205619066fb1bfeb5cbfe677ec
@@ -0,0 +1,79 @@
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Database\Schema;
/**
* Base class for schema implementations.
*
* This class contains methods that are common across
* the various SQL dialects.
*/
class BaseSchema {
/**
* Generate an ON clause for a foreign key.
*
* @param string|null $on The on clause
* @return string
*/
protected function _foreignOnClause($on) {
if ($on === Table::ACTION_SET_NULL) {
return 'SET NULL';
}
if ($on === Table::ACTION_CASCADE) {
return 'CASCADE';
}
if ($on === Table::ACTION_RESTRICT) {
return 'RESTRICT';
}
if ($on === Table::ACTION_NO_ACTION) {
return 'NO ACTION';
}
}
/**
* Convert string on clauses to the abstract ones.
*
* @param string $clause
* @return string|null
*/
protected function _convertOnClause($clause) {
if ($clause === 'CASCADE' || $clause === 'RESTRICT') {
return strtolower($clause);
}
if ($clause === 'NO ACTION') {
return Table::ACTION_NO_ACTION;
}
return Table::ACTION_SET_NULL;
}
/**
* Generate the SQL to drop a table.
*
* @param Cake\Database\Schema\Table $table Table instance
* @return array SQL statements to drop DROP a table.
*/
public function dropTableSql(Table $table) {
$sql = sprintf(
'DROP TABLE %s',
$this->_driver->quoteIdentifier($table->name())
);
return [$sql];
}
}
@@ -22,7 +22,7 @@
/**
* Schema management/reflection features for MySQL
*/
class MysqlSchema {
class MysqlSchema extends BaseSchema {
/**
* The driver instance being used.
@@ -244,22 +244,6 @@ public function convertForeignKey(Table $table, $row) {
$table->addConstraint($name, $data);
}
/**
* Convert MySQL on clauses to the abstract ones.
*
* @param string $clause
* @return string|null
*/
protected function _convertOnClause($clause) {
if ($clause === 'CASCADE' || $clause === 'RESTRICT') {
return strtolower($clause);
}
if ($clause === 'NO ACTION') {
return Table::ACTION_NO_ACTION;
}
return Table::ACTION_SET_NULL;
}
/**
* Generate the SQL to truncate a table.
*
@@ -270,16 +254,6 @@ public function truncateTableSql(Table $table) {
return [sprintf("TRUNCATE TABLE `%s`", $table->name())];
}
/**
* Generate the SQL to drop a table.
*
* @param Cake\Database\Schema\Table $table Table instance
* @return array DROP TABLE sql
*/
public function dropTableSql(Table $table) {
return [sprintf("DROP TABLE `%s`", $table->name())];
}
/**
* Generate the SQL to create a table.
*
@@ -456,25 +430,4 @@ protected function _keySql($prefix, $data) {
return $prefix . ' (' . implode(', ', $columns) . ')';
}
/**
* Generate an ON clause for a foreign key.
*
* @param string|null $on The on clause
* @return string
*/
protected function _foreignOnClause($on) {
if ($on === Table::ACTION_SET_NULL) {
return 'SET NULL';
}
if ($on === Table::ACTION_CASCADE) {
return 'CASCADE';
}
if ($on === Table::ACTION_RESTRICT) {
return 'RESTRICT';
}
if ($on === Table::ACTION_NO_ACTION) {
return 'NO ACTION';
}
}
}
@@ -22,7 +22,7 @@
/**
* Schema management/reflection features for Postgres.
*/
class PostgresSchema {
class PostgresSchema extends BaseSchema {
/**
* The driver instance being used.
@@ -449,27 +449,6 @@ protected function _keySql($prefix, $data) {
return $prefix . ' (' . implode(', ', $columns) . ')';
}
/**
* Generate an ON clause for a foreign key.
*
* @param string|null $on The on clause
* @return string
*/
protected function _foreignOnClause($on) {
if ($on === Table::ACTION_SET_NULL) {
return 'SET NULL';
}
if ($on === Table::ACTION_CASCADE) {
return 'CASCADE';
}
if ($on === Table::ACTION_RESTRICT) {
return 'RESTRICT';
}
if ($on === Table::ACTION_NO_ACTION) {
return 'NO ACTION';
}
}
/**
* Generate the SQL to create a table.
@@ -502,16 +481,6 @@ public function createTableSql(Table $table, $columns, $constraints, $indexes) {
return $out;
}
/**
* Generate the SQL to drop a table.
*
* @param Cake\Database\Schema\Table $table Table instance
* @return array SQL statements to drop DROP a table.
*/
public function dropTableSql(Table $table) {
return [sprintf('DROP TABLE "%s"', $table->name())];
}
/**
* Generate the SQL to truncate a table.
*
@@ -22,7 +22,7 @@
/**
* Schema management/reflection features for Sqlite
*/
class SqliteSchema {
class SqliteSchema extends BaseSchema {
/**
* The driver instance being used.
@@ -218,22 +218,6 @@ public function convertForeignKey(Table $table, $row) {
$table->addConstraint($name, $data);
}
/**
* Convert Sqlite on clauses to the abstract ones.
*
* @param string $clause
* @return string|null
*/
protected function _convertOnClause($clause) {
if ($clause === 'CASCADE' || $clause === 'RESTRICT') {
return strtolower($clause);
}
if ($clause === 'NO ACTION') {
return Table::ACTION_NO_ACTION;
}
return Table::ACTION_SET_NULL;
}
/**
* Generate the SQL fragment for a single column in Sqlite
*
@@ -340,27 +324,6 @@ public function constraintSql(Table $table, $name) {
);
}
/**
* Generate an ON clause for a foreign key.
*
* @param string|null $on The on clause
* @return string
*/
protected function _foreignOnClause($on) {
if ($on === Table::ACTION_SET_NULL) {
return 'SET NULL';
}
if ($on === Table::ACTION_CASCADE) {
return 'CASCADE';
}
if ($on === Table::ACTION_RESTRICT) {
return 'RESTRICT';
}
if ($on === Table::ACTION_NO_ACTION) {
return 'NO ACTION';
}
}
/**
* Generate the SQL fragment for a single index.
*
@@ -401,16 +364,6 @@ public function createTableSql($table, $columns, $constraints, $indexes) {
return $out;
}
/**
* 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 truncate a table.
*

0 comments on commit c673573

Please sign in to comment.
You can’t perform that action at this time.