Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
BaseSchema is abstract now. Add abstract methods and a constructor to…
… BaseSchema.

_convertColumn is now protected, no need to expose to the public API.
  • Loading branch information
renan committed Oct 5, 2013
1 parent c9f8b06 commit f6cf15e
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 280 deletions.
2 changes: 0 additions & 2 deletions Cake/Database/Driver.php
Expand Up @@ -16,8 +16,6 @@
*/
namespace Cake\Database;

use \Cake\Database\SqlDialectTrait;

/**
* Represents a database diver containing all specificities for
* a database engine including its SQL dialect
Expand Down
135 changes: 133 additions & 2 deletions Cake/Database/Schema/BaseSchema.php
Expand Up @@ -16,13 +16,33 @@
*/
namespace Cake\Database\Schema;

use Cake\Database\Driver;
use Cake\Database\Schema\Table;

/**
* Base class for schema implementations.
*
* This class contains methods that are common across
* the various SQL dialects.
*/
class BaseSchema {
abstract class BaseSchema {

/**
* The driver instance being used.
*
* @var Cake\Database\Driver
*/
protected $_driver;

/**
* Constructor
*
* @param Cake\Database\Driver $driver The driver to use.
* @return void
*/
public function __construct(Driver $driver) {
$this->_driver = $driver;
}

/**
* Generate an ON clause for a foreign key.
Expand Down Expand Up @@ -65,7 +85,7 @@ protected function _convertOnClause($clause) {
* Generate the SQL to drop a table.
*
* @param Cake\Database\Schema\Table $table Table instance
* @return array SQL statements to drop DROP a table.
* @return array SQL statements to drop a table.
*/
public function dropTableSql(Table $table) {
$sql = sprintf(
Expand All @@ -75,4 +95,115 @@ public function dropTableSql(Table $table) {
return [$sql];
}

/**
* Generate the SQL to list the tables.
*
* @param array $config The connection configuration to use for
* getting tables from.
* @return array An array of (sql, params) to execute.
*/
abstract public function listTablesSql($config);

/**
* Generate the SQL to describe a table.
*
* @param string $name The table name to get information on.
* @param array $config The connection configuration.
* @return array An array of (sql, params) to execute.
*/
abstract public function describeTableSql($name, $config);

/**
* Generate the SQL to describe the indexes in a table.
*
* @param string $table The table name to get information on.
* @param array $config The connection configuration.
* @return array An array of (sql, params) to execute.
*/
abstract public function describeIndexSql($table, $config);

/**
* Generate the SQL to describe the foreign keys in a table.
*
* @param string $table The table name to get information on.
* @param array $config The connection configuration.
* @return array An array of (sql, params) to execute.
*/
abstract public function describeForeignKeySql($table, $config);

/**
* Convert field description results into abstract schema fields.
*
* @param Cake\Database\Schema\Table $table The table object to append fields to.
* @param array $row The row data from `describeTableSql`.
* @return void
*/
abstract public function convertFieldDescription(Table $table, $row);

/**
* Convert an index description results into abstract schema indexes or constraints.
*
* @param Cake\Database\Schema\Table $table The table object to append
* an index or constraint to.
* @param array $row The row data from `describeIndexSql`.
* @return void
*/
abstract public function convertIndexDescription(Table $table, $row);

/**
* Convert a foreign key description into constraints on the Table object.
*
* @param Cake\Database\Schema\Table $table The table object to append
* a constraint to.
* @param array $row The row data from `describeForeignKeySql`.
* @return void
*/
abstract public function convertForeignKeyDescription(Table $table, $row);

/**
* Generate the SQL to create a table.
*
* @param Cake\Database\Schema\Table $table Table instance.
* @param array $columns The columns to go inside the table.
* @param array $constraints The constraints for the table.
* @param array $indexes The indexes for the table.
* @return array SQL statements to create a table.
*/
abstract public function createTableSql(Table $table, $columns, $constraints, $indexes);

/**
* Generate the SQL fragment for a single column in a table.
*
* @param Cake\Database\Schema\Table $table The table instance the column is in.
* @param string $name The name of the column.
* @return string SQL fragment.
*/
abstract public function columnSql(Table $table, $name);

/**
* Generate the SQL fragments for defining table constraints.
*
* @param Cake\Database\Schema\Table $table The table instance the column is in.
* @param string $name The name of the column.
* @return string SQL fragment.
*/
abstract public function constraintSql(Table $table, $name);

/**
* Generate the SQL fragment for a single index in a table.
*
* @param Cake\Database\Schema\Table $table The table object the column is in.
* @param string $name The name of the column.
* @return string SQL fragment.
*/
abstract public function indexSql(Table $table, $name);

/**
* Generate the SQL to truncate a table.
*
* @param Cake\Database\Schema\Table $table Table instance.
* @return array SQL statements to truncate a table.
*/
abstract public function truncateTableSql(Table $table);

}
21 changes: 7 additions & 14 deletions Cake/Database/Schema/Collection.php
Expand Up @@ -71,14 +71,13 @@ public function listTables() {
* Get the column metadata for a table.
*
* @param string $name The name of the table to describe.
* @return Cake\Schema\Table|null Object with column metadata, or null.
* @return Cake\Database\Schema\Table Object with column metadata.
* @throws Cake\Database\Exception when table cannot be described.
*/
public function describe($name) {
list($sql, $params) = $this->_dialect->describeTableSql(
$name,
$this->_connection->config()
);
$config = $this->_connection->config();

list($sql, $params) = $this->_dialect->describeTableSql($name, $config);
$statement = $this->_executeSql($sql, $params);
if (count($statement) === 0) {
throw new Exception(__d('cake_dev', 'Cannot describe %s. It has 0 columns.', $name));
Expand All @@ -89,22 +88,16 @@ public function describe($name) {
$this->_dialect->convertFieldDescription($table, $row);
}

list($sql, $params) = $this->_dialect->describeIndexSql(
$name,
$this->_connection->config()
);
list($sql, $params) = $this->_dialect->describeIndexSql($name, $config);
$statement = $this->_executeSql($sql, $params);
foreach ($statement->fetchAll('assoc') as $row) {
$this->_dialect->convertIndexDescription($table, $row);
}

list($sql, $params) = $this->_dialect->describeForeignKeySql(
$name,
$this->_connection->config()
);
list($sql, $params) = $this->_dialect->describeForeignKeySql($name, $config);
$statement = $this->_executeSql($sql, $params);
foreach ($statement->fetchAll('assoc') as $row) {
$this->_dialect->convertForeignKey($table, $row);
$this->_dialect->convertForeignKeyDescription($table, $row);
}
return $table;
}
Expand Down

0 comments on commit f6cf15e

Please sign in to comment.