Skip to content

Commit 06f00d1

Browse files
committed
Decision on connection string ambiguity (DNET-892).
Dash supported for host.
1 parent de367e1 commit 06f00d1

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

Provider/src/FirebirdSql.Data.FirebirdClient.Tests/ConnectionStringTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,5 +719,44 @@ public void ParsingDatabaseURLStyleWithoutHostnameDrivePath()
719719
Assert.AreEqual("localhost", cs.DataSource);
720720
Assert.AreEqual("C:\\test.fdb", cs.Database);
721721
}
722+
723+
[Test]
724+
public void ParsingDatabaseNoStyleWithoutPath()
725+
{
726+
const string ConnectionString = "database=test.fdb";
727+
var cs = new ConnectionString(ConnectionString);
728+
Assert.AreEqual("test.fdb", cs.Database);
729+
Assert.AreEqual(string.Empty, cs.DataSource);
730+
}
731+
732+
[Test]
733+
public void ParsingDatabaseNoStyleRootPath()
734+
{
735+
const string ConnectionString = "database=/test.fdb";
736+
var cs = new ConnectionString(ConnectionString);
737+
Assert.AreEqual("/test.fdb", cs.Database);
738+
Assert.AreEqual(string.Empty, cs.DataSource);
739+
}
740+
741+
[Test]
742+
public void ParsingDatabaseNoStyleDrivePath()
743+
{
744+
const string ConnectionString = "database=C:\\test.fdb";
745+
var cs = new ConnectionString(ConnectionString);
746+
Assert.AreEqual("C:\\test.fdb", cs.Database);
747+
Assert.AreEqual(string.Empty, cs.DataSource);
748+
}
749+
750+
[TestCase("test")]
751+
[TestCase("test12")]
752+
[TestCase("32test")]
753+
[TestCase("test-12")]
754+
public void ParsingDatabaseHostnames(string hostname)
755+
{
756+
var ConnectionString = $"database={hostname}:test.fdb";
757+
var cs = new ConnectionString(ConnectionString);
758+
Assert.AreEqual(hostname, cs.DataSource);
759+
Assert.AreEqual("test.fdb", cs.Database);
760+
}
722761
}
723762
}

Provider/src/FirebirdSql.Data.FirebirdClient/Common/ConnectionString.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,14 @@ private void SetDefaultOptions()
352352
_options = new Dictionary<string, object>(DefaultValues);
353353
}
354354

355+
// it is expected the hostname do be at least 2 characters to prevent possible ambiguity (DNET-892)
355356
private void ParseConnectionInfo(string connectionInfo)
356357
{
357358
connectionInfo = connectionInfo.Trim();
358359

359360
{
360-
// URL style inet://host:port/database
361-
var match = Regex.Match(connectionInfo, "^inet://(?<host>[A-Za-z0-9\\.]+):(?<port>\\d+)/(?<database>.+)$");
361+
// URL style inet://[hostv6]:port/database
362+
var match = Regex.Match(connectionInfo, "^inet://\\[(?<host>[A-Za-z0-9:]{2,})\\]:(?<port>\\d+)/(?<database>.+)$");
362363
if (match.Success)
363364
{
364365
_options[DefaultKeyCatalog] = match.Groups["database"].Value;
@@ -368,8 +369,8 @@ private void ParseConnectionInfo(string connectionInfo)
368369
}
369370
}
370371
{
371-
// URL style inet://[hostv6]:port/database
372-
var match = Regex.Match(connectionInfo, "^inet://\\[(?<host>[A-Za-z0-9:]+)\\]:(?<port>\\d+)/(?<database>.+)$");
372+
// URL style inet://host:port/database
373+
var match = Regex.Match(connectionInfo, "^inet://(?<host>[A-Za-z0-9\\.-]{2,}):(?<port>\\d+)/(?<database>.+)$");
373374
if (match.Success)
374375
{
375376
_options[DefaultKeyCatalog] = match.Groups["database"].Value;
@@ -380,7 +381,7 @@ private void ParseConnectionInfo(string connectionInfo)
380381
}
381382
{
382383
// URL style inet://host/database
383-
var match = Regex.Match(connectionInfo, "^inet://(?<host>[A-Za-z0-9\\.:]+)/(?<database>.+)$");
384+
var match = Regex.Match(connectionInfo, "^inet://(?<host>[A-Za-z0-9\\.:-]{2,})/(?<database>.+)$");
384385
if (match.Success)
385386
{
386387
_options[DefaultKeyCatalog] = match.Groups["database"].Value;
@@ -399,8 +400,8 @@ private void ParseConnectionInfo(string connectionInfo)
399400
}
400401
}
401402
{
402-
// new style //host:port/database
403-
var match = Regex.Match(connectionInfo, "^//(?<host>[A-Za-z0-9\\.]+):(?<port>\\d+)/(?<database>.+)$");
403+
// new style //[hostv6]:port/database
404+
var match = Regex.Match(connectionInfo, "^//\\[(?<host>[A-Za-z0-9:]{2,})\\]:(?<port>\\d+)/(?<database>.+)$");
404405
if (match.Success)
405406
{
406407
_options[DefaultKeyCatalog] = match.Groups["database"].Value;
@@ -410,8 +411,8 @@ private void ParseConnectionInfo(string connectionInfo)
410411
}
411412
}
412413
{
413-
// new style //[hostv6]:port/database
414-
var match = Regex.Match(connectionInfo, "^//\\[(?<host>[A-Za-z0-9:]+)\\]:(?<port>\\d+)/(?<database>.+)$");
414+
// new style //host:port/database
415+
var match = Regex.Match(connectionInfo, "^//(?<host>[A-Za-z0-9\\.-]{2,}):(?<port>\\d+)/(?<database>.+)$");
415416
if (match.Success)
416417
{
417418
_options[DefaultKeyCatalog] = match.Groups["database"].Value;
@@ -422,7 +423,7 @@ private void ParseConnectionInfo(string connectionInfo)
422423
}
423424
{
424425
// new style //host/database
425-
var match = Regex.Match(connectionInfo, "^//(?<host>[A-Za-z0-9\\.:]+)/(?<database>.+)$");
426+
var match = Regex.Match(connectionInfo, "^//(?<host>[A-Za-z0-9\\.:-]{2,})/(?<database>.+)$");
426427
if (match.Success)
427428
{
428429
_options[DefaultKeyCatalog] = match.Groups["database"].Value;
@@ -432,7 +433,7 @@ private void ParseConnectionInfo(string connectionInfo)
432433
}
433434
{
434435
// old style host:X:\database
435-
var match = Regex.Match(connectionInfo, "^(?<host>[A-Za-z0-9\\.:]+):(?<database>[A-Za-z]:\\\\.+)$");
436+
var match = Regex.Match(connectionInfo, "^(?<host>[A-Za-z0-9\\.:-]{2,}):(?<database>[A-Za-z]:\\\\.+)$");
436437
if (match.Success)
437438
{
438439
_options[DefaultKeyCatalog] = match.Groups["database"].Value;
@@ -442,7 +443,7 @@ private void ParseConnectionInfo(string connectionInfo)
442443
}
443444
{
444445
// old style host/port:database
445-
var match = Regex.Match(connectionInfo, "^(?<host>[A-Za-z0-9\\.:]+)/(?<port>\\d+):(?<database>.+)$");
446+
var match = Regex.Match(connectionInfo, "^(?<host>[A-Za-z0-9\\.:-]{2,})/(?<port>\\d+):(?<database>.+)$");
446447
if (match.Success)
447448
{
448449
_options[DefaultKeyCatalog] = match.Groups["database"].Value;
@@ -453,7 +454,7 @@ private void ParseConnectionInfo(string connectionInfo)
453454
}
454455
{
455456
// old style host:database
456-
var match = Regex.Match(connectionInfo, "^(?<host>[A-Za-z0-9\\.:]+):(?<database>.+)$");
457+
var match = Regex.Match(connectionInfo, "^(?<host>[A-Za-z0-9\\.:-]{2,}):(?<database>.+)$");
457458
if (match.Success)
458459
{
459460
_options[DefaultKeyCatalog] = match.Groups["database"].Value;

0 commit comments

Comments
 (0)