2014-03-13 20:14:56 +11:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Data;
|
|
|
|
|
|
using System.Data.Common;
|
|
|
|
|
|
using System.Linq;
|
2016-04-12 19:55:50 +02:00
|
|
|
|
using NPoco;
|
2016-10-07 14:34:55 +02:00
|
|
|
|
using Umbraco.Core.DI;
|
2016-04-12 19:55:50 +02:00
|
|
|
|
using Umbraco.Core.Logging;
|
2014-03-13 20:14:56 +11:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Persistence
|
|
|
|
|
|
{
|
|
|
|
|
|
internal static class DbConnectionExtensions
|
|
|
|
|
|
{
|
2016-04-12 19:55:50 +02:00
|
|
|
|
public static string DetectProviderNameFromConnectionString(string connectionString)
|
2014-03-13 20:14:56 +11:00
|
|
|
|
{
|
2016-04-12 19:55:50 +02:00
|
|
|
|
var builder = new DbConnectionStringBuilder { ConnectionString = connectionString };
|
2014-03-13 20:14:56 +11:00
|
|
|
|
var allKeys = builder.Keys.Cast<string>();
|
|
|
|
|
|
|
2016-04-12 19:55:50 +02:00
|
|
|
|
var mySql = new[] { "Server", "Database", "Uid", "Pwd" };
|
2014-03-13 20:14:56 +11:00
|
|
|
|
if (mySql.All(x => allKeys.InvariantContains(x)))
|
|
|
|
|
|
{
|
2016-04-12 19:55:50 +02:00
|
|
|
|
return Constants.DbProviderNames.MySql;
|
2014-03-13 20:14:56 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-12 19:55:50 +02:00
|
|
|
|
if (allKeys.InvariantContains("Data Source")
|
2014-03-13 20:14:56 +11:00
|
|
|
|
//this dictionary is case insensitive
|
|
|
|
|
|
&& builder["Data source"].ToString().InvariantContains(".sdf"))
|
|
|
|
|
|
{
|
2016-04-12 19:55:50 +02:00
|
|
|
|
return Constants.DbProviderNames.SqlCe;
|
2014-03-13 20:14:56 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-12 19:55:50 +02:00
|
|
|
|
return Constants.DbProviderNames.SqlServer;
|
2014-03-13 20:14:56 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-12 19:55:50 +02:00
|
|
|
|
public static bool IsConnectionAvailable(string connectionString, string providerName)
|
2014-03-13 20:14:56 +11:00
|
|
|
|
{
|
2016-04-12 19:55:50 +02:00
|
|
|
|
if (providerName != Constants.DbProviderNames.SqlCe
|
|
|
|
|
|
&& providerName != Constants.DbProviderNames.MySql
|
|
|
|
|
|
&& providerName != Constants.DbProviderNames.SqlServer)
|
|
|
|
|
|
throw new NotSupportedException($"Provider \"{providerName}\" is not supported.");
|
2014-03-13 20:14:56 +11:00
|
|
|
|
|
2016-04-12 19:55:50 +02:00
|
|
|
|
var factory = DbProviderFactories.GetFactory(providerName);
|
2016-09-01 19:06:08 +02:00
|
|
|
|
var connection = factory.CreateConnection();
|
2016-04-12 19:55:50 +02:00
|
|
|
|
|
2016-09-01 19:06:08 +02:00
|
|
|
|
if (connection == null)
|
2016-04-12 19:55:50 +02:00
|
|
|
|
throw new InvalidOperationException($"Could not create a connection for provider \"{providerName}\".");
|
|
|
|
|
|
|
2016-09-01 19:06:08 +02:00
|
|
|
|
connection.ConnectionString = connectionString;
|
|
|
|
|
|
using (connection)
|
2014-03-13 20:14:56 +11:00
|
|
|
|
{
|
|
|
|
|
|
return connection.IsAvailable();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static bool IsAvailable(this IDbConnection connection)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
connection.Open();
|
|
|
|
|
|
connection.Close();
|
|
|
|
|
|
}
|
2016-04-12 19:55:50 +02:00
|
|
|
|
catch (DbException e)
|
2014-03-13 20:14:56 +11:00
|
|
|
|
{
|
2015-07-30 15:36:33 +01:00
|
|
|
|
// Don't swallow this error, the exception is super handy for knowing "why" its not available
|
2016-09-11 19:57:33 +02:00
|
|
|
|
Current.Logger.Warn<IDbConnection>(e, "Configured database is reporting as not being available.");
|
2014-03-13 20:14:56 +11:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-12 19:55:50 +02:00
|
|
|
|
|
2014-03-13 20:14:56 +11:00
|
|
|
|
}
|
|
|
|
|
|
}
|