Skip to content

Commit 9592722

Browse files
committed
Fix on connection string parsing for oracle/mysql.
1 parent cc2ef95 commit 9592722

File tree

2 files changed

+5
-13
lines changed

2 files changed

+5
-13
lines changed

SQLHelper.SpeedTests/Tests/AltImplementations/HelperClasses/Connection.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ public Connection(IConfiguration configuration, DbProviderFactory factory, strin
6666
DatabaseName = DatabaseNameRegex.Match(ConnectionString).Groups[1].Value;
6767
ParameterPrefix = "@";
6868
}
69-
else if (SourceType.Contains("MySql", StringComparison.OrdinalIgnoreCase))
70-
{
71-
ParameterPrefix = "?";
72-
}
7369
else if (SourceType.Contains("Oracle", StringComparison.OrdinalIgnoreCase))
7470
{
7571
ParameterPrefix = ":";

src/SQLHelper.DB/HelperClasses/Connection.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ public Connection(IConfiguration configuration, DbProviderFactory factory, strin
5858
Name = string.IsNullOrEmpty(name) ? "Default" : name;
5959
Factory = factory ?? SqlClientFactory.Instance;
6060
ConnectionString = !string.IsNullOrEmpty(connection) ? connection : (configuration.GetConnectionString(Name) ?? Name);
61-
if (Factory is SqlClientFactory)
62-
DatabaseName = DatabaseNameRegex.Match(ConnectionString).Groups[1].Value;
61+
var DatabaseRegexResult = DatabaseNameRegex.Match(ConnectionString);
62+
if (DatabaseRegexResult.Success)
63+
DatabaseName = DatabaseRegexResult.Groups["name"].Value;
6364
ParameterPrefix = !string.IsNullOrEmpty(parameterPrefix) ? parameterPrefix : GetParameterPrefix(Factory);
6465
CommandTimeout = GetCommandTimeout(ConnectionString);
6566
}
@@ -115,7 +116,7 @@ public Connection(IConfiguration configuration, DbProviderFactory factory, strin
115116
/// Gets the database.
116117
/// </summary>
117118
/// <value>The database.</value>
118-
private static Regex DatabaseNameRegex { get; } = new Regex("Initial Catalog=([^;]*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
119+
private static Regex DatabaseNameRegex { get; } = new Regex("(Initial Catalog|Database|Data Source)=(?<name>[^;]*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
119120

120121
/// <summary>
121122
/// Gets the command timeout.
@@ -140,12 +141,7 @@ private static int GetCommandTimeout(string connectionString)
140141
private static string GetParameterPrefix(DbProviderFactory factory)
141142
{
142143
var SourceType = factory.GetType().FullName ?? string.Empty;
143-
if (SourceType.Contains("MySql", StringComparison.OrdinalIgnoreCase))
144-
return "?";
145-
else if (SourceType.Contains("Oracle", StringComparison.OrdinalIgnoreCase))
146-
return ":";
147-
else
148-
return "@";
144+
return SourceType.Contains("Oracle", StringComparison.OrdinalIgnoreCase) ? ":" : "@";
149145
}
150146
}
151147
}

0 commit comments

Comments
 (0)