Skip to content

Commit

Permalink
Start moving Schema Dialect features to Schema/Dialect
Browse files Browse the repository at this point in the history
Move code + tests for converting columns/indexes.
  • Loading branch information
markstory committed Apr 24, 2013
1 parent 52c7064 commit 927dd0d
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 241 deletions.
133 changes: 6 additions & 127 deletions lib/Cake/Database/Dialect/MysqlDialectTrait.php
Expand Up @@ -43,136 +43,15 @@ trait MysqlDialectTrait {
public $endQuote = '`';

/**
* Get the SQL to list the tables in MySQL
* Get the schema dialect.
*
* @param array $config The connection configuration to use for
* getting tables from.
* @return array An array of (sql, params) to execute.
*/
public function listTablesSql(array $config) {
return ["SHOW TABLES FROM " . $this->quoteIdentifier($config['database']), []];
}

/**
* Get the SQL to describe a table in MySQL.
*
* @param string $table The table name to describe.
* @return array An array of (sql, params) to execute.
*/
public function describeTableSql($table) {
return ["SHOW FULL COLUMNS FROM " . $this->quoteIdentifier($table), []];
}

/**
* Convert a platform specific index type to the abstract type
*
* @param string $key The key type to convert.
* @return string The abstract key type (primary, unique, index)
*/
public function convertIndex($key) {
if ($key === 'PRI') {
return 'primary';
}
if ($key === 'MUL') {
return 'index';
}
if ($key === 'UNI') {
return 'unique';
}
}

/**
* Convert a MySQL column type into an abstract type.
*
* The returnned type will be a type that Cake\Database\Type can handle.
*
* @param string $column The column type + length
* @return array List of (type, length)
*/
public function convertColumn($column) {
preg_match('/([a-z]+)(?:\(([0-9,]+)\))?/i', $column, $matches);
if (empty($matches)) {
throw new Error\Exception(__d('cake_dev', 'Unable to parse column type from "%s"', $column));
}

$col = strtolower($matches[1]);
$length = null;
if (isset($matches[2])) {
$length = (int)$matches[2];
}

if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) {
return [$col, null];
}
if (($col === 'tinyint' && $length === 1) || $col === 'boolean') {
return ['boolean', null];
}
if (strpos($col, 'bigint') !== false || $col === 'bigint') {
return ['biginteger', $length];
}
if (strpos($col, 'int') !== false) {
return ['integer', $length];
}
if (strpos($col, 'char') !== false || $col === 'tinytext') {
return ['string', $length];
}
if (strpos($col, 'text') !== false) {
return ['text', $length];
}
if (strpos($col, 'blob') !== false || $col === 'binary') {
return ['binary', $length];
}
if (strpos($col, 'float') !== false || strpos($col, 'double') !== false) {
return ['float', $length];
}
if (strpos($col, 'decimal') !== false) {
return ['decimal', null];
}
return ['text', null];
}

/**
* Convert field description results into abstract schema fields.
*
* @return array An array of with the key/values of schema data.
*/
public function convertFieldDescription($row, $fieldParams = []) {
list($type, $length) = $this->convertColumn($row['Type']);
$schema = [];
$schema[$row['Field']] = [
'type' => $type,
'null' => $row['Null'] === 'YES' ? true : false,
'default' => $row['Default'],
'length' => $length,
];
if (!empty($row['Key'])) {
$schema[$row['Field']]['key'] = $this->convertIndex($row['Key']);
}
foreach ($fieldParams as $key => $metadata) {
if (!empty($row[$metadata['column']])) {
$schema[$row['Field']][$key] = $row[$metadata['column']];
}
}
return $schema;
}

/**
* Get additional column meta data used in schema reflections.
* Used by Cake\Schema package to reflect schema and
* generate schema.
*
* @return array
* @return Cake\Database\Schema\Dialect\Mysql
*/
public function extraSchemaColumns() {
return [
'charset' => [
'column' => false,
],
'collate' => [
'column' => 'Collation',
],
'comment' => [
'column' => 'Comment',
]
];
public function schemaDialect() {
return new \Cake\Database\Schema\Dialect\Mysql($this);
}

}
160 changes: 160 additions & 0 deletions lib/Cake/Database/Schema/Dialect/Mysql.php
@@ -0,0 +1,160 @@
<?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\Dialect;

class Mysql {

protected $driver;

public function __construct($driver) {
$this->_driver = $driver;
}

/**
* Get the SQL to list the tables in MySQL
*
* @param array $config The connection configuration to use for
* getting tables from.
* @return array An array of (sql, params) to execute.
*/
public function listTablesSql(array $config) {
return ["SHOW TABLES FROM " . $this->quoteIdentifier($config['database']), []];
}

/**
* Get the SQL to describe a table in MySQL.
*
* @param string $table The table name to describe.
* @return array An array of (sql, params) to execute.
*/
public function describeTableSql($table) {
return ["SHOW FULL COLUMNS FROM " . $this->quoteIdentifier($table), []];
}

/**
* Convert a platform specific index type to the abstract type
*
* @param string $key The key type to convert.
* @return string The abstract key type (primary, unique, index)
*/
public function convertIndex($key) {
if ($key === 'PRI') {
return 'primary';
}
if ($key === 'MUL') {
return 'index';
}
if ($key === 'UNI') {
return 'unique';
}
}

/**
* Convert a MySQL column type into an abstract type.
*
* The returnned type will be a type that Cake\Database\Type can handle.
*
* @param string $column The column type + length
* @return array List of (type, length)
*/
public function convertColumn($column) {
preg_match('/([a-z]+)(?:\(([0-9,]+)\))?/i', $column, $matches);
if (empty($matches)) {
throw new Error\Exception(__d('cake_dev', 'Unable to parse column type from "%s"', $column));
}

$col = strtolower($matches[1]);
$length = null;
if (isset($matches[2])) {
$length = (int)$matches[2];
}

if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) {
return [$col, null];
}
if (($col === 'tinyint' && $length === 1) || $col === 'boolean') {
return ['boolean', null];
}
if (strpos($col, 'bigint') !== false || $col === 'bigint') {
return ['biginteger', $length];
}
if (strpos($col, 'int') !== false) {
return ['integer', $length];
}
if (strpos($col, 'char') !== false || $col === 'tinytext') {
return ['string', $length];
}
if (strpos($col, 'text') !== false) {
return ['text', $length];
}
if (strpos($col, 'blob') !== false || $col === 'binary') {
return ['binary', $length];
}
if (strpos($col, 'float') !== false || strpos($col, 'double') !== false) {
return ['float', $length];
}
if (strpos($col, 'decimal') !== false) {
return ['decimal', null];
}
return ['text', null];
}

/**
* Convert field description results into abstract schema fields.
*
* @return array An array of with the key/values of schema data.
*/
public function convertFieldDescription($row, $fieldParams = []) {
list($type, $length) = $this->convertColumn($row['Type']);
$schema = [];
$schema[$row['Field']] = [
'type' => $type,
'null' => $row['Null'] === 'YES' ? true : false,
'default' => $row['Default'],
'length' => $length,
];
if (!empty($row['Key'])) {
$schema[$row['Field']]['key'] = $this->convertIndex($row['Key']);
}
foreach ($fieldParams as $key => $metadata) {
if (!empty($row[$metadata['column']])) {
$schema[$row['Field']][$key] = $row[$metadata['column']];
}
}
return $schema;
}

/**
* Get additional column meta data used in schema reflections.
*
* @return array
*/
public function extraSchemaColumns() {
return [
'charset' => [
'column' => false,
],
'collate' => [
'column' => 'Collation',
],
'comment' => [
'column' => 'Comment',
]
];
}

}
10 changes: 0 additions & 10 deletions lib/Cake/Database/SqlDialectTrait.php
Expand Up @@ -176,14 +176,4 @@ public function rollbackSavePointSQL($name) {
return 'ROLLBACK TO SAVEPOINT LEVEL' . $name;
}

/**
* Get extra schema metadata columns
*
* This method returns information about additional metadata present in the data
* generated by describeTableSql
*
* @return void
*/
abstract function extraSchemaColumns();

}

0 comments on commit 927dd0d

Please sign in to comment.