Skip to content

Commit

Permalink
Fix SQLite unsigned support.
Browse files Browse the repository at this point in the history
unsigned needs to precede the type name if there are default values.
I'd rather not complicate the code with additional complexity so we'll
just always put unsigned first which seems to work from what I can see.
  • Loading branch information
markstory committed Nov 28, 2013
1 parent d765dda commit dd95ba9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
30 changes: 18 additions & 12 deletions Cake/Database/Schema/SqliteSchema.php
Expand Up @@ -40,16 +40,21 @@ class SqliteSchema extends BaseSchema {
* @return array Array of column information.
*/
protected function _convertColumn($column) {
preg_match('/([a-z]+)(?:\(([0-9,]+)\))?\s*([a-z]+)?/i', $column, $matches);
preg_match('/(unsigned)?\s*([a-z]+)(?:\(([0-9,]+)\))?/i', $column, $matches);
if (empty($matches)) {
throw new Exception(__d('cake_dev', 'Unable to parse column type from "%s"', $column));
}
$col = strtolower($matches[1]);

$unsigned = false;
if (strtolower($matches[1]) === 'unsigned') {
$unsigned = true;
}

$col = strtolower($matches[2]);
$length = null;
if (isset($matches[2])) {
$length = (int)$matches[2];
if (isset($matches[3])) {
$length = (int)$matches[3];
}
$unsigned = (isset($matches[3]) && strtolower($matches[3]) === 'unsigned');

if ($col === 'bigint') {
return ['type' => 'biginteger', 'length' => $length, 'unsigned' => $unsigned];
Expand Down Expand Up @@ -228,6 +233,14 @@ public function columnSql(Table $table, $name) {
}

$out = $this->_driver->quoteIdentifier($name);
$hasUnsigned = ['biginteger', 'integer', 'float', 'decimal'];

if (
in_array($data['type'], $hasUnsigned, true) &&
isset($data['unsigned']) && $data['unsigned'] === true
) {
$out .= ' UNSIGNED';
}
$out .= $typeMap[$data['type']];

$hasLength = ['integer', 'string'];
Expand All @@ -241,13 +254,6 @@ public function columnSql(Table $table, $name) {
) {
$out .= '(' . (int)$data['length'] . ',' . (int)$data['precision'] . ')';
}
$hasUnsigned = ['biginteger', 'integer', 'float', 'decimal'];
if (
in_array($data['type'], $hasUnsigned, true) &&
isset($data['unsigned']) && $data['unsigned'] === true
) {
$out .= ' UNSIGNED';
}

if (isset($data['null']) && $data['null'] === false) {
$out .= ' NOT NULL';
Expand Down
14 changes: 7 additions & 7 deletions Cake/Test/TestCase/Database/Schema/SqliteSchemaTest.php
Expand Up @@ -64,7 +64,7 @@ public static function convertColumnProvider() {
['type' => 'biginteger', 'length' => null, 'unsigned' => false]
],
[
'BIGINT UNSIGNED',
'UNSIGNED BIGINT',
['type' => 'biginteger', 'length' => null, 'unsigned' => true]
],
[
Expand All @@ -88,7 +88,7 @@ public static function convertColumnProvider() {
['type' => 'integer', 'length' => 11, 'unsigned' => false]
],
[
'INTEGER(11) UNSIGNED',
'UNSIGNED INTEGER(11)',
['type' => 'integer', 'length' => 11, 'unsigned' => true]
],
[
Expand All @@ -108,7 +108,7 @@ public static function convertColumnProvider() {
['type' => 'float', 'length' => null, 'unsigned' => false]
],
[
'DOUBLE UNSIGNED',
'UNSIGNED DOUBLE',
['type' => 'float', 'length' => null, 'unsigned' => true]
],
[
Expand All @@ -120,7 +120,7 @@ public static function convertColumnProvider() {
['type' => 'decimal', 'length' => null, 'unsigned' => false]
],
[
'DECIMAL(11,2) UNSIGNED',
'UNSIGNED DECIMAL(11,2)',
['type' => 'decimal', 'length' => null, 'unsigned' => true]
],
];
Expand Down Expand Up @@ -391,7 +391,7 @@ public static function columnSqlProvider() {
[
'post_id',
['type' => 'biginteger', 'length' => 20, 'unsigned' => true],
'"post_id" BIGINT UNSIGNED'
'"post_id" UNSIGNED BIGINT'
],
// Decimal
[
Expand All @@ -407,7 +407,7 @@ public static function columnSqlProvider() {
[
'value',
['type' => 'decimal', 'length' => 11, 'unsigned' => true],
'"value" DECIMAL(11,0) UNSIGNED'
'"value" UNSIGNED DECIMAL(11,0)'
],
[
'value',
Expand All @@ -428,7 +428,7 @@ public static function columnSqlProvider() {
[
'value',
['type' => 'float', 'length' => 11, 'precision' => 3, 'unsigned' => true],
'"value" FLOAT(11,3) UNSIGNED'
'"value" UNSIGNED FLOAT(11,3)'
],
// Boolean
[
Expand Down

0 comments on commit dd95ba9

Please sign in to comment.