Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/Phinx/Db/Adapter/SqlServerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is is possible to declare those constants as strings? Sorry if I am wrong, but I guess INT_BIG would overflow in 32bit PHP, and if you want to use unsigned bigint, it would overflow even in 64bit PHP.


/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -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']))) {
Expand Down
51 changes: 51 additions & 0 deletions tests/Phinx/Db/Adapter/SqlServerAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}