diff --git a/src/Umbraco.Core/DatabaseContext.cs b/src/Umbraco.Core/DatabaseContext.cs index 8633107845..be7467356a 100644 --- a/src/Umbraco.Core/DatabaseContext.cs +++ b/src/Umbraco.Core/DatabaseContext.cs @@ -1,6 +1,8 @@ using System; using System.Configuration; -using System.Data.Common; +using System.Linq; +using System.Web.Configuration; +using System.Xml.Linq; using Umbraco.Core.Configuration; using Umbraco.Core.Persistence; using Umbraco.Core.Persistence.SqlSyntax; @@ -51,6 +53,115 @@ namespace Umbraco.Core get { return _configured; } } + /// + /// Configure a ConnectionString for the embedded database. + /// + public void ConfigureDatabaseConnection() + { + var providerName = "System.Data.SqlServerCe.4.0"; + var connectionString = "Datasource=|DataDirectory|Umbraco.sdf"; + var appSettingsConnection = @"datalayer=SQLCE4Umbraco.SqlCEHelper,SQLCE4Umbraco;data source=|DataDirectory|\Umbraco.sdf"; + + SaveConnectionString(connectionString, appSettingsConnection, providerName); + Initialize(); + } + + /// + /// Configure a ConnectionString that has been entered manually. + /// + /// + /// Please note that we currently assume that the 'System.Data.SqlClient' provider can be used. + /// + /// + public void ConfigureDatabaseConnection(string connectionString) + { + SaveConnectionString(connectionString, connectionString, string.Empty); + Initialize(); + } + + /// + /// Configures a ConnectionString for the Umbraco database based on the passed in properties from the installer. + /// + /// Name or address of the database server + /// Name of the database + /// Database Username + /// Database Password + /// Type of the provider to be used (Sql, Sql Azure, Sql Ce, MySql) + public void ConfigureDatabaseConnection(string server, string databaseName, string user, string password, string databaseProvider) + { + string connectionString; + string appSettingsConnection; + string providerName = "System.Data.SqlClient"; + if(databaseProvider.ToLower().Contains("mysql")) + { + providerName = "MySql.Data.MySqlClient"; + connectionString = string.Format("Server={0}; Database={1};Uid={2};Pwd={3}", server, databaseName, user, password); + appSettingsConnection = connectionString; + } + else if(databaseProvider.ToLower().Contains("azure")) + { + connectionString = string.Format("Server=tcp:{0}.database.windows.net;Database={1};User ID={2}@{0};Password={3}", server, databaseName, user, password); + appSettingsConnection = connectionString; + } + else + { + connectionString = string.Format("server={0};database={1};user id={2};password={3}", server, databaseName, user, password); + appSettingsConnection = connectionString; + } + + SaveConnectionString(connectionString, appSettingsConnection, providerName); + Initialize(); + } + + /// + /// Saves the connection string as a proper .net ConnectionString and the legacy AppSettings key/value. + /// + /// + /// + /// + private void SaveConnectionString(string connectionString, string appSettingsConnection, string providerName) + { + //Set the connection string for the new datalayer + var connectionStringSettings = string.IsNullOrEmpty(providerName) + ? new ConnectionStringSettings(GlobalSettings.UmbracoConnectionName, + connectionString) + : new ConnectionStringSettings(GlobalSettings.UmbracoConnectionName, + connectionString, providerName); + + //Set the connection string in appsettings used in the legacy datalayer + GlobalSettings.DbDsn = appSettingsConnection; + //ConfigurationManager.ConnectionStrings.Add(conectionString); + + var webConfig = new WebConfigurationFileMap(); + var vDir = GlobalSettings.FullpathToRoot; + foreach (VirtualDirectoryMapping v in webConfig.VirtualDirectories) + { + if (v.IsAppRoot) + { + vDir = v.PhysicalDirectory; + } + } + + string fileName = String.Concat(vDir, "web.config"); + var xml = XDocument.Load(fileName); + var connectionstrings = xml.Root.Descendants("connectionStrings").Single(); + + // Update connectionString if it exists, or else create a new appSetting for the given key and value + var setting = connectionstrings.Descendants("add").FirstOrDefault(s => s.Attribute("name").Value == GlobalSettings.UmbracoConnectionName); + if (setting == null) + connectionstrings.Add(new XElement("add", + new XAttribute("name", GlobalSettings.UmbracoConnectionName), + new XAttribute("connectionString", connectionStringSettings), + new XAttribute("providerName", providerName))); + else + { + setting.Attribute("connectionString").Value = connectionString; + } + + xml.Save(fileName); + ConfigurationManager.RefreshSection("connectionStrings"); + } + /// /// Internal method to initialize the database configuration. /// diff --git a/src/Umbraco.Core/Persistence/DatabaseFactory.cs b/src/Umbraco.Core/Persistence/DatabaseFactory.cs index 718ed94bfd..1b166f5e55 100644 --- a/src/Umbraco.Core/Persistence/DatabaseFactory.cs +++ b/src/Umbraco.Core/Persistence/DatabaseFactory.cs @@ -66,6 +66,7 @@ namespace Umbraco.Core.Persistence if (dbtype.StartsWith("Npgsql")) return DatabaseProviders.PostgreSQL; if (dbtype.StartsWith("Oracle") || dbtype.Contains("OracleClient")) return DatabaseProviders.Oracle; if (dbtype.StartsWith("SQLite")) return DatabaseProviders.SQLite; + if(dbtype.Contains("Azure")) return DatabaseProviders.SqlAzure; return DatabaseProviders.SqlServer; } diff --git a/src/Umbraco.Core/Persistence/DatabaseProviders.cs b/src/Umbraco.Core/Persistence/DatabaseProviders.cs index 473528e38d..e3c0ba125f 100644 --- a/src/Umbraco.Core/Persistence/DatabaseProviders.cs +++ b/src/Umbraco.Core/Persistence/DatabaseProviders.cs @@ -3,6 +3,7 @@ public enum DatabaseProviders { SqlServer, + SqlAzure, SqlServerCE, MySql, PostgreSQL, diff --git a/src/Umbraco.Web/umbraco.presentation/install/steps/database.ascx.cs b/src/Umbraco.Web/umbraco.presentation/install/steps/database.ascx.cs index cb862bb748..50376424f6 100644 --- a/src/Umbraco.Web/umbraco.presentation/install/steps/database.ascx.cs +++ b/src/Umbraco.Web/umbraco.presentation/install/steps/database.ascx.cs @@ -2,6 +2,8 @@ using System; using System.Data.Common; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; +using Umbraco.Core; +using Umbraco.Core.Persistence; using umbraco.DataLayer; using umbraco.DataLayer.Utility.Installer; using System.IO; @@ -154,8 +156,23 @@ namespace umbraco.presentation.install.steps try { - DbConnectionStringBuilder connectionStringBuilder = CreateConnectionString(); - GlobalSettings.DbDSN = connectionStringBuilder.ConnectionString; + /*DbConnectionStringBuilder connectionStringBuilder = CreateConnectionString(); + GlobalSettings.DbDSN = connectionStringBuilder.ConnectionString;*/ + + if (string.IsNullOrEmpty(ConnectionString.Text) == false) + { + DatabaseContext.Current.ConfigureDatabaseConnection(ConnectionString.Text); + } + else if (IsEmbeddedDatabase) + { + DatabaseContext.Current.ConfigureDatabaseConnection(); + } + else + { + DatabaseContext.Current.ConfigureDatabaseConnection(DatabaseServer.Text, DatabaseName.Text, + DatabaseUsername.Text, DatabasePassword.Text, + DatabaseType.SelectedValue); + } } catch (Exception ex) {