Skip to content

Commit

Permalink
Update schema classes to return a hash.
Browse files Browse the repository at this point in the history
This will make adding fixed length strings much easier.
  • Loading branch information
markstory committed May 2, 2013
1 parent eb95869 commit b5f7e59
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 99 deletions.
29 changes: 15 additions & 14 deletions lib/Cake/Database/Schema/MysqlSchema.php
Expand Up @@ -18,6 +18,9 @@

use Cake\Database\Schema\Table;

/**
* Schema dialect/support for MySQL
*/
class MysqlSchema {

/**
Expand Down Expand Up @@ -78,33 +81,33 @@ public function convertColumn($column) {
}

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

/**
Expand All @@ -116,12 +119,10 @@ public function convertColumn($column) {
* @return void
*/
public function convertFieldDescription(Table $table, $row, $fieldParams = []) {
list($type, $length) = $this->convertColumn($row['Type']);
$field = [
'type' => $type,
$field = $this->convertColumn($row['Type']);
$field += [
'null' => $row['Null'] === 'YES' ? true : false,
'default' => $row['Default'],
'length' => $length,
];
foreach ($fieldParams as $key => $metadata) {
if (!empty($row[$metadata['column']])) {
Expand Down
37 changes: 18 additions & 19 deletions lib/Cake/Database/Schema/PostgresSchema.php
Expand Up @@ -74,51 +74,51 @@ public function describeTableSql($table, $config) {
* Cake\Database\Type can handle.
*
* @param string $column The column type + length
* @return array List of (type, length)
* @return array Array of column information.
*/
public function convertColumn($column) {
$col = strtolower($column);
if (in_array($col, array('date', 'time', 'boolean'))) {
return [$col, null];
return ['type' =>$col, 'length' => null];
}
if (strpos($col, 'timestamp') !== false) {
return ['datetime', null];
return ['type' => 'datetime', 'length' => null];
}
if ($col === 'serial' || $col === 'integer') {
return ['integer', 10];
return ['type' => 'integer', 'length' => 10];
}
if ($col === 'bigserial' || $col === 'bigint') {
return ['biginteger', 20];
return ['type' => 'biginteger', 'length' => 20];
}
if ($col === 'smallint') {
return ['integer', 5];
return ['type' => 'integer', 'length' => 5];
}
if ($col === 'inet') {
return ['string', 39];
return ['type' => 'string', 'length' => 39];
}
if ($col === 'uuid') {
return ['string', 36];
return ['type' => 'string', 'length' => 36];
}
if (strpos($col, 'char') !== false) {
return ['string', null];
return ['type' => 'string', 'length' => null];
}
if (strpos($col, 'text') !== false) {
return ['text', null];
return ['type' => 'text', 'length' => null];
}
if ($col === 'bytea') {
return ['binary', null];
return ['type' => 'binary', 'length' => null];
}
if ($col === 'real' || strpos($col, 'double') !== false) {
return ['float', null];
return ['type' => 'float', 'length' => null];
}
if (
strpos($col, 'numeric') !== false ||
strpos($col, 'money') !== false ||
strpos($col, 'decimal') !== false
) {
return ['decimal', null];
return ['type' => 'decimal', 'length' => null];
}
return ['text', null];
return ['type' => 'text', 'length' => null];
}

/**
Expand All @@ -143,9 +143,9 @@ public function extraSchemaColumns() {
* @return void
*/
public function convertFieldDescription(Table $table, $row, $fieldParams = []) {
list($type, $length) = $this->convertColumn($row['type']);
$field = $this->convertColumn($row['type']);

if ($type === 'boolean') {
if ($field['type'] === 'boolean') {
if ($row['default'] === 'true') {
$row['default'] = 1;
}
Expand All @@ -154,12 +154,11 @@ public function convertFieldDescription(Table $table, $row, $fieldParams = []) {
}
}

$field = [
'type' => $type,
$field += [
'null' => $row['null'] === 'YES' ? true : false,
'default' => $row['default'],
'length' => $row['char_length'] ?: $length,
];
$field['length'] = $row['char_length'] ?: $field['length'];
foreach ($fieldParams as $key => $metadata) {
if (!empty($row[$metadata['column']])) {
$field[$key] = $row[$metadata['column']];
Expand Down
26 changes: 12 additions & 14 deletions lib/Cake/Database/Schema/SqliteSchema.php
Expand Up @@ -39,7 +39,7 @@ public function __construct($driver) {
*
* @param string $column The column type + length
* @throws Cake\Error\Exception
* @return array List of (type, length)
* @return array Array of column information.
*/
public function convertColumn($column) {
preg_match('/([a-z]+)(?:\(([0-9,]+)\))?/i', $column, $matches);
Expand All @@ -53,31 +53,31 @@ public function convertColumn($column) {
}

if ($col === 'bigint') {
return ['biginteger', $length];
return ['type' => 'biginteger', 'length' => $length];
}
if (in_array($col, ['blob', 'clob'])) {
return ['binary', null];
return ['type' => 'binary', 'length' => null];
}
if (in_array($col, ['date', 'time', 'timestamp', 'datetime'])) {
return [$col, null];
return ['type' => $col, 'length' => null];
}
if (strpos($col, 'decimal') !== false) {
return ['decimal', null];
return ['type' => 'decimal', 'length' => null];
}

if (strpos($col, 'boolean') !== false) {
return ['boolean', null];
return ['type' => 'boolean', 'length' => null];
}
if (strpos($col, 'int') !== false) {
return ['integer', $length];
return ['type' => 'integer', 'length' => $length];
}
if (strpos($col, 'char') !== false) {
return ['string', $length];
return ['type' => 'string', 'length' => $length];
}
if (in_array($col, ['float', 'real', 'double'])) {
return ['float', null];
return ['type' => 'float', 'length' => null];
}
return ['text', null];
return ['type' => 'text', 'length' => null];
}

/**
Expand Down Expand Up @@ -118,12 +118,10 @@ public function extraSchemaColumns() {
* @param array $fieldParams Additional field parameters to parse.
*/
public function convertFieldDescription(Table $table, $row, $fieldParams = []) {
list($type, $length) = $this->convertColumn($row['type']);
$field = [
'type' => $type,
$field = $this->convertColumn($row['type']);
$field += [
'null' => !$row['notnull'],
'default' => $row['dflt_value'] === null ? null : trim($row['dflt_value'], "'"),
'length' => $length,
];
if ($row['pk'] == true) {
$field['null'] = false;
Expand Down
30 changes: 15 additions & 15 deletions lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php
Expand Up @@ -47,63 +47,63 @@ public static function columnProvider() {
return [
[
'DATETIME',
['datetime', null]
['type' => 'datetime', 'length' => null]
],
[
'DATE',
['date', null]
['type' => 'date', 'length' => null]
],
[
'TIME',
['time', null]
['type' => 'time', 'length' => null]
],
[
'TINYINT(1)',
['boolean', null]
['type' => 'boolean', 'length' => null]
],
[
'TINYINT(2)',
['integer', 2]
['type' => 'integer', 'length' => 2]
],
[
'INTEGER(11)',
['integer', 11]
['type' => 'integer', 'length' => 11]
],
[
'BIGINT',
['biginteger', null]
['type' => 'biginteger', 'length' => null]
],
[
'VARCHAR(255)',
['string', 255]
['type' => 'string', 'length' => 255]
],
[
'CHAR(25)',
['string', 25]
['type' => 'string', 'length' => 25]
],
[
'TINYTEXT',
['string', null]
['type' => 'string', 'length' => null]
],
[
'BLOB',
['binary', null]
['type' => 'binary', 'length' => null]
],
[
'MEDIUMBLOB',
['binary', null]
['type' => 'binary', 'length' => null]
],
[
'FLOAT',
['float', null]
['type' => 'float', 'length' => null]
],
[
'DOUBLE',
['float', null]
['type' => 'float', 'length' => null]
],
[
'DECIMAL(11,2)',
['decimal', null]
['type' => 'decimal', 'length' => null]
],
];
}
Expand Down

0 comments on commit b5f7e59

Please sign in to comment.