ソースを参照

Add small/tiny int to Sqlite schema.

Mark Story 9 年 前
コミット
6d974c986c

+ 16 - 7
src/Database/Schema/SqliteSchema.php

@@ -68,12 +68,18 @@ class SqliteSchema extends BaseSchema
         if ($col === 'bigint') {
             return ['type' => 'biginteger', 'length' => $length, 'unsigned' => $unsigned];
         }
-        if (strpos($col, 'decimal') !== false) {
-            return ['type' => 'decimal', 'length' => null, 'unsigned' => $unsigned];
+        if ($col == 'smallint') {
+            return ['type' => 'smallint', 'length' => $length, 'unsigned' => $unsigned];
+        }
+        if ($col == 'tinyint') {
+            return ['type' => 'tinyint', 'length' => $length, 'unsigned' => $unsigned];
         }
         if (strpos($col, 'int') !== false) {
             return ['type' => 'integer', 'length' => $length, 'unsigned' => $unsigned];
         }
+        if (strpos($col, 'decimal') !== false) {
+            return ['type' => 'decimal', 'length' => null, 'unsigned' => $unsigned];
+        }
         if (in_array($col, ['float', 'real', 'double'])) {
             return ['type' => 'float', 'length' => null, 'unsigned' => $unsigned];
         }
@@ -278,6 +284,9 @@ class SqliteSchema extends BaseSchema
         $data = $schema->column($name);
         $typeMap = [
             'uuid' => ' CHAR(36)',
+            'smallint' => ' SMALLINT',
+            'tinyint' => ' TINYINT',
+            'integer' => ' INTEGER',
             'biginteger' => ' BIGINT',
             'boolean' => ' BOOLEAN',
             'binary' => ' BLOB',
@@ -291,7 +300,7 @@ class SqliteSchema extends BaseSchema
         ];
 
         $out = $this->_driver->quoteIdentifier($name);
-        $hasUnsigned = ['biginteger', 'integer', 'float', 'decimal'];
+        $hasUnsigned = ['smallint', 'tinyint', 'biginteger', 'integer', 'float', 'decimal'];
 
         if (in_array($data['type'], $hasUnsigned, true) &&
             isset($data['unsigned']) && $data['unsigned'] === true
@@ -317,11 +326,11 @@ class SqliteSchema extends BaseSchema
             }
         }
 
-        if ($data['type'] === 'integer') {
-            $out .= ' INTEGER';
-            if (isset($data['length']) && [$name] !== (array)$schema->primaryKey()) {
+        $integerTypes = ['integer', 'smallint', 'tinyint'];
+        if (in_array($data['type'], $integerTypes, true) &&
+            isset($data['length']) && [$name] !== (array)$schema->primaryKey()
+        ) {
                 $out .= '(' . (int)$data['length'] . ')';
-            }
         }
 
         $hasPrecision = ['float', 'decimal'];

+ 34 - 2
tests/TestCase/Database/Schema/SqliteSchemaTest.php

@@ -96,8 +96,20 @@ class SqliteSchemaTest extends TestCase
                 ['type' => 'integer', 'length' => 11, 'unsigned' => true]
             ],
             [
-                'TINYINT(5)',
-                ['type' => 'integer', 'length' => 5, 'unsigned' => false]
+                'TINYINT(3)',
+                ['type' => 'tinyint', 'length' => 3, 'unsigned' => false]
+            ],
+            [
+                'UNSIGNED TINYINT(3)',
+                ['type' => 'tinyint', 'length' => 3, 'unsigned' => true]
+            ],
+            [
+                'SMALLINT(5)',
+                ['type' => 'smallint', 'length' => 5, 'unsigned' => false]
+            ],
+            [
+                'UNSIGNED SMALLINT(5)',
+                ['type' => 'smallint', 'length' => 5, 'unsigned' => true]
             ],
             [
                 'MEDIUMINT(10)',
@@ -499,6 +511,26 @@ SQL;
             // Integers
             [
                 'post_id',
+                ['type' => 'smallint', 'length' => 5, 'unsigned' => false],
+                '"post_id" SMALLINT(5)'
+            ],
+            [
+                'post_id',
+                ['type' => 'smallint', 'length' => 5, 'unsigned' => true],
+                '"post_id" UNSIGNED SMALLINT(5)'
+            ],
+            [
+                'post_id',
+                ['type' => 'tinyint', 'length' => 3, 'unsigned' => false],
+                '"post_id" TINYINT(3)'
+            ],
+            [
+                'post_id',
+                ['type' => 'tinyint', 'length' => 3, 'unsigned' => true],
+                '"post_id" UNSIGNED TINYINT(3)'
+            ],
+            [
+                'post_id',
                 ['type' => 'integer', 'length' => 11, 'unsigned' => false],
                 '"post_id" INTEGER(11)'
             ],