diff --git a/src/Tgstation.Server.Host/Core/Application.cs b/src/Tgstation.Server.Host/Core/Application.cs index e0182463a2..63c5d3959d 100644 --- a/src/Tgstation.Server.Host/Core/Application.cs +++ b/src/Tgstation.Server.Host/Core/Application.cs @@ -101,6 +101,7 @@ namespace Tgstation.Server.Host.Core //setup stuff for setup wizard services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); //needed here for JWT configuration diff --git a/src/Tgstation.Server.Host/Core/DBConnectionFactory.cs b/src/Tgstation.Server.Host/Core/DBConnectionFactory.cs new file mode 100644 index 0000000000..0b92cad465 --- /dev/null +++ b/src/Tgstation.Server.Host/Core/DBConnectionFactory.cs @@ -0,0 +1,37 @@ +using MySql.Data.MySqlClient; +using System; +using System.Data.Common; +using System.Data.SqlClient; +using System.Globalization; +using Tgstation.Server.Host.Configuration; + +namespace Tgstation.Server.Host.Core +{ + /// + sealed class DBConnectionFactory : IDBConnectionFactory + { + /// + public DbConnection CreateConnection(string connectionString, DatabaseType databaseType) + { + if (connectionString == null) + throw new ArgumentNullException(nameof(connectionString)); + + switch (databaseType) + { + case DatabaseType.MariaDB: + case DatabaseType.MySql: + return new MySqlConnection + { + ConnectionString = connectionString + }; + case DatabaseType.SqlServer: + return new SqlConnection + { + ConnectionString = connectionString + }; + default: + throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Invalid database type ({0})!", databaseType)); + } + } + } +} diff --git a/src/Tgstation.Server.Host/Core/IDBConnectionFactory.cs b/src/Tgstation.Server.Host/Core/IDBConnectionFactory.cs new file mode 100644 index 0000000000..3f378b88b5 --- /dev/null +++ b/src/Tgstation.Server.Host/Core/IDBConnectionFactory.cs @@ -0,0 +1,19 @@ +using System.Data.Common; +using Tgstation.Server.Host.Configuration; + +namespace Tgstation.Server.Host.Core +{ + /// + /// For creating + /// + interface IDBConnectionFactory + { + /// + /// Create a + /// + /// The + /// The to create + /// A new + DbConnection CreateConnection(string connectionString, DatabaseType databaseType); + } +} diff --git a/src/Tgstation.Server.Host/Core/SetupWizard.cs b/src/Tgstation.Server.Host/Core/SetupWizard.cs index 182376fea4..ea9f11d3d8 100644 --- a/src/Tgstation.Server.Host/Core/SetupWizard.cs +++ b/src/Tgstation.Server.Host/Core/SetupWizard.cs @@ -40,6 +40,11 @@ namespace Tgstation.Server.Host.Core /// readonly IApplication application; + /// + /// The for the + /// + readonly IDBConnectionFactory dbConnectionFactory; + /// /// The for the /// @@ -56,14 +61,16 @@ namespace Tgstation.Server.Host.Core /// The value of /// The value of /// The value of + /// The value of /// The value of /// The containing the value of - public SetupWizard(IIOManager ioManager, IConsole console, IHostingEnvironment hostingEnvironment, IApplication application, ILogger logger, IOptions generalConfigurationOptions) + public SetupWizard(IIOManager ioManager, IConsole console, IHostingEnvironment hostingEnvironment, IApplication application, IDBConnectionFactory dbConnectionFactory, ILogger logger, IOptions generalConfigurationOptions) { this.ioManager = ioManager ?? throw new ArgumentNullException(nameof(ioManager)); this.console = console ?? throw new ArgumentNullException(nameof(console)); this.hostingEnvironment = hostingEnvironment ?? throw new ArgumentNullException(nameof(hostingEnvironment)); this.application = application ?? throw new ArgumentNullException(nameof(application)); + this.dbConnectionFactory = dbConnectionFactory ?? throw new ArgumentNullException(nameof(dbConnectionFactory)); this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); generalConfiguration = generalConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(generalConfigurationOptions)); } @@ -189,7 +196,13 @@ namespace Tgstation.Server.Host.Core await console.WriteAsync(null, true, cancellationToken).ConfigureAwait(false); } + DbConnection testConnection; + void CreateTestConnection(string connectionString) + { + testConnection = dbConnectionFactory.CreateConnection(connectionString, databaseConfiguration.DatabaseType); + } + if (databaseConfiguration.DatabaseType == DatabaseType.SqlServer) { var csb = new SqlConnectionStringBuilder @@ -204,11 +217,8 @@ namespace Tgstation.Server.Host.Core csb.UserID = username; csb.Password = password; } - testConnection = new SqlConnection - { - ConnectionString = csb.ConnectionString - }; + CreateTestConnection(csb.ConnectionString); csb.InitialCatalog = databaseName; databaseConfiguration.ConnectionString = csb.ConnectionString; } @@ -220,10 +230,8 @@ namespace Tgstation.Server.Host.Core UserID = username, Password = password }; - testConnection = new MySqlConnection - { - ConnectionString = csb.ConnectionString - }; + + CreateTestConnection(csb.ConnectionString); csb.Database = databaseName; databaseConfiguration.ConnectionString = csb.ConnectionString; }