diff --git a/src/Dsn.php b/src/Dsn.php index 6bcb7419..1a08ac0f 100644 --- a/src/Dsn.php +++ b/src/Dsn.php @@ -14,12 +14,13 @@ final class Dsn extends AbstractDsn { public function __construct( - private string $driver, - private string $host, - private string|null $databaseName = null, - private string $port = '1433' + string $driver = 'sqlsrv', + string $host = 'localhost', + string|null $databaseName = null, + string $port = '1433', + array $options = [] ) { - parent::__construct($driver, $host, $databaseName, $port); + parent::__construct($driver, $host, $databaseName, $port, $options); } /** @@ -39,16 +40,24 @@ public function __construct( */ public function asString(): string { - if ($this->port !== '') { - $server = "Server=$this->host,$this->port;"; - } else { - $server = "Server=$this->host;"; + $driver = $this->getDriver(); + $host = $this->getHost(); + $port = $this->getPort(); + $databaseName = $this->getDatabaseName(); + $options = $this->getOptions(); + + $dsn = "$driver:Server=$host"; + + if (!empty($port)) { + $dsn .= ",$port"; + } + + if (!empty($databaseName)) { + $dsn .= ";Database=$databaseName"; } - if (!empty($this->databaseName)) { - $dsn = "$this->driver:" . $server . "Database=$this->databaseName"; - } else { - $dsn = "$this->driver:" . $server; + if (!empty($options)) { + $dsn .= ';' . http_build_query($options, '', ';'); } return $dsn; diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 607c632d..560521e4 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -313,7 +313,7 @@ public function testDropDefaultValueSql(): void public function testShowDatabases(): void { - $dsn = new Dsn('sqlsrv', 'localhost'); + $dsn = new Dsn(options: ['Encrypt' => 'no']); $db = new Connection( new Driver($dsn->asString(), 'SA', 'YourStrong!Passw0rd'), DbHelper::getSchemaCache(), @@ -321,7 +321,7 @@ public function testShowDatabases(): void $command = $db->createCommand(); - $this->assertSame('sqlsrv:Server=localhost,1433;', $db->getDriver()->getDsn()); + $this->assertSame('sqlsrv:Server=localhost,1433;Encrypt=no', $db->getDriver()->getDsn()); $this->assertSame(['yiitest'], $command->showDatabases()); } diff --git a/tests/Support/TestTrait.php b/tests/Support/TestTrait.php index 2d1dbe97..517caedc 100644 --- a/tests/Support/TestTrait.php +++ b/tests/Support/TestTrait.php @@ -37,8 +37,7 @@ protected function getConnection(bool $fixture = false): PdoConnectionInterface protected static function getDb(): PdoConnectionInterface { - $dsn = (new Dsn('sqlsrv', 'localhost', 'yiitest'))->asString(); - $dsn .= ';Encrypt=no'; + $dsn = (new Dsn(databaseName: 'yiitest', options: ['Encrypt' => 'no']))->asString(); return new Connection( new Driver($dsn, 'SA', 'YourStrong!Passw0rd'), @@ -49,8 +48,7 @@ protected static function getDb(): PdoConnectionInterface protected function getDsn(): string { if ($this->dsn === '') { - $this->dsn = (new Dsn('sqlsrv', 'localhost', 'yiitest'))->asString(); - $this->dsn .= ';Encrypt=no'; + $this->dsn = (new Dsn(databaseName: 'yiitest', options: ['Encrypt' => 'no']))->asString(); } return $this->dsn; diff --git a/tests/Type/CharTest.php b/tests/Type/CharTest.php index e1bf61da..6b8764da 100644 --- a/tests/Type/CharTest.php +++ b/tests/Type/CharTest.php @@ -191,7 +191,7 @@ public function testValueException(): void $this->expectException(Exception::class); $this->expectExceptionMessage( - '[ODBC Driver 17 for SQL Server][SQL Server]String or binary data would be truncated' + '[ODBC Driver 18 for SQL Server][SQL Server]String or binary data would be truncated' ); $command->insert('char', ['Mychar1' => '01234567891'])->execute(); diff --git a/tests/Type/DateTest.php b/tests/Type/DateTest.php index 042745d3..909c7701 100644 --- a/tests/Type/DateTest.php +++ b/tests/Type/DateTest.php @@ -191,7 +191,7 @@ public function testValueException(): void $this->expectException(Exception::class); $this->expectExceptionMessage( - 'SQLSTATE[22007]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Conversion failed when converting date and/or time from character string.' + 'SQLSTATE[22007]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Conversion failed when converting date and/or time from character string.' ); $db->createCommand()->insert('date', ['Mydate1' => '0000-00-00'])->execute(); diff --git a/tests/Type/DecimalTest.php b/tests/Type/DecimalTest.php index f11d4011..e57d21b8 100644 --- a/tests/Type/DecimalTest.php +++ b/tests/Type/DecimalTest.php @@ -189,7 +189,7 @@ public function testMaxValueException(): void $this->expectException(Exception::class); $this->expectExceptionMessage( - "SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The number '199999999999999997748809823456034029570' is out of the range for numeric representation (maximum precision 38)." + "SQLSTATE[22003]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]The number '199999999999999997748809823456034029570' is out of the range for numeric representation (maximum precision 38)." ); $command->insert( @@ -266,7 +266,7 @@ public function testMinValueException(): void $this->expectException(Exception::class); $this->expectExceptionMessage( - "SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The number '199999999999999997748809823456034029570' is out of the range for numeric representation (maximum precision 38)." + "SQLSTATE[22003]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]The number '199999999999999997748809823456034029570' is out of the range for numeric representation (maximum precision 38)." ); $command->insert( diff --git a/tests/Type/FloatTest.php b/tests/Type/FloatTest.php index 110fe119..23214118 100644 --- a/tests/Type/FloatTest.php +++ b/tests/Type/FloatTest.php @@ -186,7 +186,7 @@ public function testMaxValueException(): void $this->expectException(Exception::class); $this->expectExceptionMessage( - "SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The floating point value '1.80E+308' is out of the range of computer representation (8 bytes)." + "SQLSTATE[22003]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]The floating point value '1.80E+308' is out of the range of computer representation (8 bytes)." ); $command->insert('float', ['Myfloat1' => new Expression('1.80E+308')])->execute(); @@ -256,7 +256,7 @@ public function testMinValueException(): void $this->expectException(Exception::class); $this->expectExceptionMessage( - "SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The floating point value '1.80E+308' is out of the range of computer representation (8 bytes)." + "SQLSTATE[22003]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]The floating point value '1.80E+308' is out of the range of computer representation (8 bytes)." ); $command->insert('float', ['Myfloat1' => new Expression('-1.80E+308')])->execute(); diff --git a/tests/Type/IntTest.php b/tests/Type/IntTest.php index 4d513f97..6c33e2b9 100644 --- a/tests/Type/IntTest.php +++ b/tests/Type/IntTest.php @@ -182,7 +182,7 @@ public function testMaxValueException(): void $this->expectException(Exception::class); $this->expectExceptionMessage( - 'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow' + 'SQLSTATE[22003]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Arithmetic overflow' ); $command->insert('int', ['Myint1' => 2_147_483_648])->execute(); @@ -252,7 +252,7 @@ public function testMinValueException(): void $this->expectException(Exception::class); $this->expectExceptionMessage( - 'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow' + 'SQLSTATE[22003]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Arithmetic overflow' ); $command->insert('int', ['Myint1' => -2_147_483_649])->execute(); diff --git a/tests/Type/MoneyTest.php b/tests/Type/MoneyTest.php index e6998383..275eccc4 100644 --- a/tests/Type/MoneyTest.php +++ b/tests/Type/MoneyTest.php @@ -182,7 +182,7 @@ public function testMaxValueException(): void $this->expectException(Exception::class); $this->expectExceptionMessage( - 'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow error converting expression to data type money.' + 'SQLSTATE[22003]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Arithmetic overflow error converting expression to data type money.' ); $command->insert('money', ['Mymoney1' => '922337203685478.5808'])->execute(); @@ -252,7 +252,7 @@ public function testMinValueException(): void $this->expectException(Exception::class); $this->expectExceptionMessage( - 'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow error converting expression to data type money.' + 'SQLSTATE[22003]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Arithmetic overflow error converting expression to data type money.' ); $command->insert('money', ['Mymoney1' => '-922337203685480.5808'])->execute(); diff --git a/tests/Type/NumericTest.php b/tests/Type/NumericTest.php index 36470b7f..add59114 100644 --- a/tests/Type/NumericTest.php +++ b/tests/Type/NumericTest.php @@ -245,7 +245,7 @@ public function testMinValueException(): void $this->expectException(Exception::class); $this->expectExceptionMessage( - "SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The number '199999999999999997748809823456034029570' is out of the range for numeric representation (maximum precision 38)." + "SQLSTATE[22003]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]The number '199999999999999997748809823456034029570' is out of the range for numeric representation (maximum precision 38)." ); $command->insert( diff --git a/tests/Type/RealTest.php b/tests/Type/RealTest.php index 74ea36c9..1c6bcd55 100644 --- a/tests/Type/RealTest.php +++ b/tests/Type/RealTest.php @@ -183,7 +183,7 @@ public function testMaxValueException(): void $this->expectException(Exception::class); $this->expectExceptionMessage( - 'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow error for type real' + 'SQLSTATE[22003]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Arithmetic overflow error for type real' ); $command->insert('real', ['Myreal1' => new Expression('4.4E+38')])->execute(); @@ -253,7 +253,7 @@ public function testMinValueException(): void $this->expectException(Exception::class); $this->expectExceptionMessage( - 'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow error for type real' + 'SQLSTATE[22003]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Arithmetic overflow error for type real' ); $command->insert('real', ['Myreal1' => new Expression('-4.4E+38')])->execute(); diff --git a/tests/Type/SmallIntTest.php b/tests/Type/SmallIntTest.php index 46a01d27..f001e201 100644 --- a/tests/Type/SmallIntTest.php +++ b/tests/Type/SmallIntTest.php @@ -182,7 +182,7 @@ public function testMaxValueException(): void $this->expectException(Exception::class); $this->expectExceptionMessage( - 'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow' + 'SQLSTATE[22003]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Arithmetic overflow' ); $command->insert('smallint', ['Mysmallint1' => 32768])->execute(); @@ -252,7 +252,7 @@ public function testMinValueException(): void $this->expectException(Exception::class); $this->expectExceptionMessage( - 'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow' + 'SQLSTATE[22003]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Arithmetic overflow' ); $command->insert('smallint', ['Mysmallint1' => -32769])->execute(); diff --git a/tests/Type/SmallMoneyTest.php b/tests/Type/SmallMoneyTest.php index 104e766d..02422465 100644 --- a/tests/Type/SmallMoneyTest.php +++ b/tests/Type/SmallMoneyTest.php @@ -182,7 +182,7 @@ public function testMaxValueException(): void $this->expectException(Exception::class); $this->expectExceptionMessage( - 'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow error converting expression to data type smallmoney.' + 'SQLSTATE[22003]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Arithmetic overflow error converting expression to data type smallmoney.' ); $command->insert('smallmoney', ['Mysmallmoney1' => '214749.3647'])->execute(); @@ -252,7 +252,7 @@ public function testMinValueException(): void $this->expectException(Exception::class); $this->expectExceptionMessage( - 'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow error converting expression to data type smallmoney.' + 'SQLSTATE[22003]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Arithmetic overflow error converting expression to data type smallmoney.' ); $command->insert('smallmoney', ['Mysmallmoney1' => '-214749.3648'])->execute(); diff --git a/tests/Type/TinyIntTest.php b/tests/Type/TinyIntTest.php index 9fc3bc78..a440388d 100644 --- a/tests/Type/TinyIntTest.php +++ b/tests/Type/TinyIntTest.php @@ -197,7 +197,7 @@ public function testMaxValueException(): void $this->expectException(Exception::class); $this->expectExceptionMessage( - 'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow' + 'SQLSTATE[22003]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Arithmetic overflow' ); $command->insert('tinyint', ['Mytinyint1' => 256])->execute(); @@ -267,7 +267,7 @@ public function testMinValueException(): void $this->expectException(Exception::class); $this->expectExceptionMessage( - 'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow' + 'SQLSTATE[22003]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Arithmetic overflow' ); $command->insert('tinyint', ['Mytinyint1' => -1])->execute(); diff --git a/tests/Type/UniqueidentifierTest.php b/tests/Type/UniqueidentifierTest.php index 70a653de..75a02566 100644 --- a/tests/Type/UniqueidentifierTest.php +++ b/tests/Type/UniqueidentifierTest.php @@ -175,7 +175,7 @@ public function testValueException(): void $this->expectException(Exception::class); $this->expectExceptionMessage( - 'SQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Conversion failed when converting from a character string to uniqueidentifier.' + 'SQLSTATE[42000]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Conversion failed when converting from a character string to uniqueidentifier.' ); $command = $db->createCommand(); diff --git a/tests/Type/VarCharTest.php b/tests/Type/VarCharTest.php index 40541718..1cad4910 100644 --- a/tests/Type/VarCharTest.php +++ b/tests/Type/VarCharTest.php @@ -191,7 +191,7 @@ public function testValueException(): void $this->expectException(Exception::class); $this->expectExceptionMessage( - '[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]String or binary data would be truncated' + '[Microsoft][ODBC Driver 18 for SQL Server][SQL Server]String or binary data would be truncated' ); $command->insert('varchar', ['Myvarchar1' => '01234567891'])->execute();