diff --git a/src/Phinx/Db/Adapter/SqlServerAdapter.php b/src/Phinx/Db/Adapter/SqlServerAdapter.php index bb04ab0fb..6bdc9c695 100644 --- a/src/Phinx/Db/Adapter/SqlServerAdapter.php +++ b/src/Phinx/Db/Adapter/SqlServerAdapter.php @@ -44,6 +44,12 @@ class SqlServerAdapter extends PdoAdapter implements AdapterInterface protected $signedColumnTypes = ['integer' => true, 'biginteger' => true, 'float' => true, 'decimal' => true]; + const INT_TINY = 255; + const INT_SMALL = 65535; + const INT_MEDIUM = 16777215; + const INT_REGULAR = 4294967295; + const INT_BIG = 18446744073709551615; + /** * {@inheritdoc} */ @@ -856,6 +862,22 @@ public function getSqlType($type, $limit = null) case static::PHINX_TYPE_TEXT: return ['name' => 'ntext']; case static::PHINX_TYPE_INTEGER: + if ($limit && $limit >= static::INT_TINY) { + $sizes = [ + // Order matters! Size must always be tested from longest to shortest! + 'bigint' => static::INT_BIG, + 'int' => static::INT_REGULAR, + 'mediumint' => static::INT_MEDIUM, + 'smallint' => static::INT_SMALL, + 'tinyint' => static::INT_TINY, + ]; + foreach ($sizes as $name => $length) { + if ($limit >= $length) { + return ['name' => $name]; + } + } + } + return ['name' => 'int']; case static::PHINX_TYPE_BIG_INTEGER: return ['name' => 'bigint']; @@ -1015,12 +1037,13 @@ protected function getColumnSqlDefinition(Column $column, $create = true) { $buffer = []; - $sqlType = $this->getSqlType($column->getType()); + $sqlType = $this->getSqlType($column->getType(), $column->getLimit()); $buffer[] = strtoupper($sqlType['name']); // integers cant have limits in SQlServer $noLimits = [ 'bigint', 'int', + 'smallint', 'tinyint' ]; if (!in_array($sqlType['name'], $noLimits) && ($column->getLimit() || isset($sqlType['limit']))) { diff --git a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php index 208df7ee0..80d3dd0d5 100644 --- a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php @@ -761,4 +761,55 @@ public function testTruncateTable() $rows = $this->adapter->fetchAll('SELECT * FROM table1'); $this->assertEquals(0, count($rows)); } + + public function testBigIntegerColumn() + { + $table = new \Phinx\Db\Table('t', [], $this->adapter); + $table->addColumn('column1', 'integer', ['limit' => SqlServerAdapter::INT_BIG]) + ->save(); + $columns = $table->getColumns(); + $sqlType = $this->adapter->getSqlType($columns[1]->getType(), $columns[1]->getLimit()); + $this->assertEquals('bigint', $sqlType['name']); + } + + public function testMediumIntegerColumn() + { + $table = new \Phinx\Db\Table('t', [], $this->adapter); + $table->addColumn('column1', 'integer', ['limit' => SqlServerAdapter::INT_MEDIUM]) + ->save(); + $columns = $table->getColumns(); + $sqlType = $this->adapter->getSqlType($columns[1]->getType(), $columns[1]->getLimit()); + $this->assertEquals('mediumint', $sqlType['name']); + } + + public function testSmallIntegerColumn() + { + $table = new \Phinx\Db\Table('t', [], $this->adapter); + $table->addColumn('column1', 'integer', ['limit' => SqlServerAdapter::INT_SMALL]) + ->save(); + $columns = $table->getColumns(); + $sqlType = $this->adapter->getSqlType($columns[1]->getType(), $columns[1]->getLimit()); + $this->assertEquals('smallint', $sqlType['name']); + } + + public function testTinyIntegerColumn() + { + $table = new \Phinx\Db\Table('t', [], $this->adapter); + $table->addColumn('column1', 'integer', ['limit' => SqlServerAdapter::INT_TINY]) + ->save(); + $columns = $table->getColumns(); + $sqlType = $this->adapter->getSqlType($columns[1]->getType(), $columns[1]->getLimit()); + $this->assertEquals('tinyint', $sqlType['name']); + } + + public function testIntegerColumnLimit() + { + $limit = 8; + $table = new \Phinx\Db\Table('t', [], $this->adapter); + $table->addColumn('column1', 'integer', ['limit' => $limit]) + ->save(); + $columns = $table->getColumns(); + $sqlType = $this->adapter->getSqlType($columns[1]->getType(), $columns[1]->getLimit()); + $this->assertEquals($limit, $sqlType['limit']); + } }