2014-03-13 20:14:56 +11:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Data;
|
|
|
|
|
|
using System.Data.Common;
|
|
|
|
|
|
using System.Linq;
|
2020-09-16 13:08:27 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2018-05-30 16:40:25 +02:00
|
|
|
|
using StackExchange.Profiling.Data;
|
2021-02-09 10:22:42 +01:00
|
|
|
|
using Umbraco.Cms.Core;
|
2021-02-12 13:36:50 +01:00
|
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.FaultHandling;
|
2021-02-09 11:26:22 +01:00
|
|
|
|
using Umbraco.Extensions;
|
2014-03-13 20:14:56 +11:00
|
|
|
|
|
2021-02-12 13:36:50 +01:00
|
|
|
|
namespace Umbraco.Extensions
|
2014-03-13 20:14:56 +11:00
|
|
|
|
{
|
2019-12-12 12:55:17 +01:00
|
|
|
|
public static class DbConnectionExtensions
|
2014-03-13 20:14:56 +11:00
|
|
|
|
{
|
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
|
|
|
|
if (allKeys.InvariantContains("Data Source")
|
2014-03-13 20:14:56 +11:00
|
|
|
|
//this dictionary is case insensitive
|
|
|
|
|
|
&& builder["Data source"].ToString().InvariantContains(".sdf"))
|
|
|
|
|
|
{
|
2021-02-09 10:22:42 +01:00
|
|
|
|
return Cms.Core.Constants.DbProviderNames.SqlCe;
|
2014-03-13 20:14:56 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-09 10:22:42 +01:00
|
|
|
|
return Cms.Core.Constants.DbProviderNames.SqlServer;
|
2014-03-13 20:14:56 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-12-12 08:11:23 +01:00
|
|
|
|
public static bool IsConnectionAvailable(string connectionString, DbProviderFactory factory)
|
2014-03-13 20:14:56 +11:00
|
|
|
|
{
|
|
|
|
|
|
|
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)
|
2019-12-12 08:11:23 +01:00
|
|
|
|
throw new InvalidOperationException($"Could not create a connection for provider \"{factory}\".");
|
2016-04-12 19:55:50 +02:00
|
|
|
|
|
2016-09-01 19:06:08 +02:00
|
|
|
|
connection.ConnectionString = connectionString;
|
|
|
|
|
|
using (connection)
|
2014-03-13 20:14:56 +11:00
|
|
|
|
{
|
|
|
|
|
|
return connection.IsAvailable();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-12-12 08:11:23 +01:00
|
|
|
|
|
2014-03-13 20:14:56 +11:00
|
|
|
|
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
|
2020-11-10 08:50:47 +00:00
|
|
|
|
StaticApplicationLogging.Logger.LogWarning(e, "Configured database is reporting as not being available.");
|
2014-03-13 20:14:56 +11:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-12-14 14:06:30 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Unwraps a database connection.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>UmbracoDatabase wraps the original database connection in various layers (see
|
|
|
|
|
|
/// OnConnectionOpened); this unwraps and returns the original database connection.</remarks>
|
|
|
|
|
|
internal static IDbConnection UnwrapUmbraco(this IDbConnection connection)
|
|
|
|
|
|
{
|
2018-05-30 16:40:25 +02:00
|
|
|
|
var unwrapped = connection;
|
|
|
|
|
|
IDbConnection c;
|
2016-12-14 14:06:30 +01:00
|
|
|
|
do
|
|
|
|
|
|
{
|
2018-05-30 16:40:25 +02:00
|
|
|
|
c = unwrapped;
|
2019-02-06 19:04:52 +11:00
|
|
|
|
if (unwrapped is ProfiledDbConnection profiled) unwrapped = profiled.WrappedConnection;
|
2018-05-30 16:40:25 +02:00
|
|
|
|
if (unwrapped is RetryDbConnection retrying) unwrapped = retrying.Inner;
|
2016-12-14 14:06:30 +01:00
|
|
|
|
|
|
|
|
|
|
} while (c != unwrapped);
|
|
|
|
|
|
|
|
|
|
|
|
return unwrapped;
|
|
|
|
|
|
}
|
2019-12-12 12:55:17 +01:00
|
|
|
|
|
2014-03-13 20:14:56 +11:00
|
|
|
|
}
|
|
|
|
|
|
}
|