Skip to content

Commit fe3ebe2

Browse files
sdlinssamdark
authored andcommitted
Fixes #17356: MSSQL Schema was not detecting string field size
1 parent 8fb5b35 commit fe3ebe2

File tree

5 files changed

+62
-3
lines changed

5 files changed

+62
-3
lines changed

framework/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Yii Framework 2 Change Log
99
- Enh #17345: Improved performance of `yii\db\Connection::quoteColumnName()` (brandonkelly)
1010
- Enh #17348: Improved performance of `yii\db\Connection::quoteTableName()` (brandonkelly)
1111
- Enh #17353: Added `sameSite` support for `yii\web\Cookie` and `yii\web\Session::cookieParams` (rhertogh)
12+
- Bug #17356: MSSQL Schema was not detecting string field size (ricarnevale, sdlins)
1213
- Bug #17341: Allowed callable objects to be set to `\yii\filters\AccessRule::$roleParams` (alexkart)
1314
- Bug #17070: Striped invalid character from fallback file name in `Content-Disposition` header when using `\yii\web\Response::sendFile` (alexkart)
1415
- Bug #16565: Added missing parts of the context message in `\yii\log\Target::collect` (alexkart)

framework/db/Connection.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,9 @@ protected function initConnection()
706706
{
707707
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
708708
if ($this->emulatePrepare !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
709-
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->emulatePrepare);
709+
if ($this->driverName !== 'sqlsrv') {
710+
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->emulatePrepare);
711+
}
710712
}
711713
if ($this->charset !== null && in_array($this->getDriverName(), ['pgsql', 'mysql', 'mysqli', 'cubrid'], true)) {
712714
$this->pdo->exec('SET NAMES ' . $this->pdo->quote($this->charset));

framework/db/mssql/Schema.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,15 @@ protected function findColumns($table)
399399
SELECT
400400
[t1].[column_name],
401401
[t1].[is_nullable],
402-
[t1].[data_type],
402+
CASE WHEN [t1].[data_type] IN ('char','varchar','nchar','nvarchar','binary','varbinary') THEN
403+
CASE WHEN [t1].[character_maximum_length] = NULL OR [t1].[character_maximum_length] = -1 THEN
404+
[t1].[data_type]
405+
ELSE
406+
[t1].[data_type] + '(' + LTRIM(RTRIM(CONVERT(CHAR,[t1].[character_maximum_length]))) + ')'
407+
END
408+
ELSE
409+
[t1].[data_type]
410+
END AS 'data_type',
403411
[t1].[column_default],
404412
COLUMNPROPERTY(OBJECT_ID([t1].[table_schema] + '.' + [t1].[table_name]), [t1].[column_name], 'IsIdentity') AS is_identity,
405413
(

tests/framework/db/SchemaTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ public function testGetTableNames($pdoAttributes)
5454
{
5555
$connection = $this->getConnection();
5656
foreach ($pdoAttributes as $name => $value) {
57+
if ($name === PDO::ATTR_EMULATE_PREPARES && $connection->driverName === 'sqlsrv') {
58+
continue;
59+
}
5760
$connection->pdo->setAttribute($name, $value);
5861
}
62+
5963
/* @var $schema Schema */
6064
$schema = $connection->schema;
6165

@@ -78,6 +82,9 @@ public function testGetTableSchemas($pdoAttributes)
7882
{
7983
$connection = $this->getConnection();
8084
foreach ($pdoAttributes as $name => $value) {
85+
if ($name === PDO::ATTR_EMULATE_PREPARES && $connection->driverName === 'sqlsrv') {
86+
continue;
87+
}
8188
$connection->pdo->setAttribute($name, $value);
8289
}
8390
/* @var $schema Schema */
@@ -568,7 +575,7 @@ public function testFindUniqueIndexes()
568575
'somecolUnique' => ['somecol'],
569576
'someCol2Unique' => ['someCol2'],
570577
], $uniqueIndexes);
571-
578+
572579
// see https://github.com/yiisoft/yii2/issues/13814
573580
$db->createCommand()->createIndex('another unique index', 'uniqueIndex', 'someCol2', true)->execute();
574581

tests/framework/db/mssql/SchemaTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,45 @@ public function constraintsProvider()
4242
$result['4: default'][2] = [];
4343
return $result;
4444
}
45+
46+
public function testGetStringFieldsSize()
47+
{
48+
/* @var $db Connection */
49+
$db = $this->getConnection();
50+
51+
/* @var $schema Schema */
52+
$schema = $db->schema;
53+
54+
$columns = $schema->getTableSchema('type', false)->columns;
55+
56+
foreach ($columns as $name => $column) {
57+
$type = $column->type;
58+
$size = $column->size;
59+
$dbType = $column->dbType;
60+
61+
if (strpos($name, 'char_') === 0) {
62+
switch ($name) {
63+
case 'char_col':
64+
$expectedType = 'char';
65+
$expectedSize = 100;
66+
$expectedDbType = 'char(100)';
67+
break;
68+
case 'char_col2':
69+
$expectedType = 'string';
70+
$expectedSize = 100;
71+
$expectedDbType = "varchar(100)";
72+
break;
73+
case 'char_col3':
74+
$expectedType = 'text';
75+
$expectedSize = null;
76+
$expectedDbType = 'text';
77+
break;
78+
}
79+
80+
$this->assertEquals($expectedType, $type);
81+
$this->assertEquals($expectedSize, $size);
82+
$this->assertEquals($expectedDbType, $dbType);
83+
}
84+
}
85+
}
4586
}

0 commit comments

Comments
 (0)