2012-12-15 08:41:46 +05:00
|
|
|
using System;
|
2013-07-08 17:29:26 +10:00
|
|
|
using System.Collections.Generic;
|
2012-12-12 03:47:04 +05:00
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.Common;
|
2013-07-08 17:29:26 +10:00
|
|
|
using System.Linq;
|
2016-04-12 15:11:07 +02:00
|
|
|
using NPoco;
|
2013-05-10 10:15:30 -02:00
|
|
|
using StackExchange.Profiling;
|
2013-01-18 09:00:18 -01:00
|
|
|
using Umbraco.Core.Logging;
|
2016-04-12 15:11:07 +02:00
|
|
|
using Umbraco.Core.Persistence.FaultHandling;
|
|
|
|
|
using Umbraco.Core.Persistence.Mappers;
|
2012-12-12 03:47:04 +05:00
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Persistence
|
|
|
|
|
{
|
2015-01-09 15:27:47 +11:00
|
|
|
/// <summary>
|
2016-04-12 15:11:07 +02:00
|
|
|
/// Extends NPoco Database for Umbraco.
|
2015-01-09 15:27:47 +11:00
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
2016-04-12 15:11:07 +02:00
|
|
|
/// <para>Is used everywhere in place of the original NPoco Database object, and provides additional features
|
|
|
|
|
/// such as profiling, retry policies, logging, etc.</para>
|
|
|
|
|
/// <para>Is never created directly but obtained from the <see cref="DefaultDatabaseFactory"/>.</para>
|
2015-01-09 15:27:47 +11:00
|
|
|
/// </remarks>
|
2013-09-30 12:02:35 +10:00
|
|
|
public class UmbracoDatabase : Database, IDisposeOnRequestEnd
|
2015-01-09 15:27:47 +11:00
|
|
|
{
|
2016-04-12 15:11:07 +02:00
|
|
|
// Umbraco's default isolation level is RepeatableRead
|
|
|
|
|
private const IsolationLevel DefaultIsolationLevel = IsolationLevel.RepeatableRead;
|
|
|
|
|
|
2015-01-09 15:27:47 +11:00
|
|
|
private readonly ILogger _logger;
|
2013-01-18 09:00:18 -01:00
|
|
|
private readonly Guid _instanceId = Guid.NewGuid();
|
2016-04-12 15:11:07 +02:00
|
|
|
private readonly RetryPolicy _connectionRetryPolicy;
|
|
|
|
|
private readonly RetryPolicy _commandRetryPolicy;
|
2015-07-22 12:10:21 +02:00
|
|
|
private bool _enableCount;
|
|
|
|
|
|
2015-01-09 15:27:47 +11:00
|
|
|
/// <summary>
|
|
|
|
|
/// Used for testing
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal Guid InstanceId
|
|
|
|
|
{
|
|
|
|
|
get { return _instanceId; }
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-22 12:10:21 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Generally used for testing, will output all SQL statements executed to the logger
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal bool EnableSqlTrace { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Used for testing
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal void EnableSqlCount()
|
|
|
|
|
{
|
|
|
|
|
_enableCount = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Used for testing
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal void DisableSqlCount()
|
|
|
|
|
{
|
|
|
|
|
_enableCount = false;
|
|
|
|
|
SqlCount = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Used for testing
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal int SqlCount { get; private set; }
|
|
|
|
|
|
2016-04-12 15:11:07 +02:00
|
|
|
// used by DefaultDatabaseFactory
|
|
|
|
|
// creates one instance per request
|
|
|
|
|
// also used by DatabaseContext for creating DBs and upgrading
|
|
|
|
|
public UmbracoDatabase(string connectionString, string providerName, ILogger logger, RetryPolicy connectionRetryPolicy = null, RetryPolicy commandRetryPolicy = null)
|
|
|
|
|
: base(connectionString, providerName, DefaultIsolationLevel)
|
2015-01-09 15:27:47 +11:00
|
|
|
{
|
|
|
|
|
_logger = logger;
|
2016-04-12 15:11:07 +02:00
|
|
|
_connectionRetryPolicy = connectionRetryPolicy;
|
|
|
|
|
_commandRetryPolicy = commandRetryPolicy;
|
2015-07-22 12:10:21 +02:00
|
|
|
EnableSqlTrace = false;
|
2015-01-09 15:27:47 +11:00
|
|
|
}
|
|
|
|
|
|
2016-04-12 15:11:07 +02:00
|
|
|
// used by DefaultDatabaseFactory
|
|
|
|
|
// creates one instance per request
|
|
|
|
|
public UmbracoDatabase(string connectionStringName, ILogger logger, RetryPolicy connectionRetryPolicy = null, RetryPolicy commandRetryPolicy = null)
|
|
|
|
|
: base(connectionStringName, DefaultIsolationLevel)
|
2015-01-09 15:27:47 +11:00
|
|
|
{
|
|
|
|
|
_logger = logger;
|
2016-04-12 15:11:07 +02:00
|
|
|
_connectionRetryPolicy = connectionRetryPolicy;
|
|
|
|
|
_commandRetryPolicy = commandRetryPolicy;
|
2015-07-22 12:10:21 +02:00
|
|
|
EnableSqlTrace = false;
|
2015-01-09 15:27:47 +11:00
|
|
|
}
|
|
|
|
|
|
2016-04-12 15:11:07 +02:00
|
|
|
protected override DbConnection OnConnectionOpened(DbConnection connection)
|
2015-01-09 15:27:47 +11:00
|
|
|
{
|
2016-04-12 15:11:07 +02:00
|
|
|
if (connection == null) throw new ArgumentNullException("connection");
|
2015-01-09 15:27:47 +11:00
|
|
|
|
2016-04-12 15:11:07 +02:00
|
|
|
// wrap the connection with a profiling connection that tracks timings
|
|
|
|
|
connection = new StackExchange.Profiling.Data.ProfiledDbConnection(connection, MiniProfiler.Current);
|
2013-01-18 09:00:18 -01:00
|
|
|
|
2016-04-12 15:11:07 +02:00
|
|
|
// wrap the connection with a retrying connection
|
|
|
|
|
if (_connectionRetryPolicy != null || _commandRetryPolicy != null)
|
|
|
|
|
connection = new RetryDbConnection(connection, _connectionRetryPolicy, _commandRetryPolicy);
|
2016-02-04 14:51:19 +01:00
|
|
|
|
2016-04-12 15:11:07 +02:00
|
|
|
return connection;
|
2013-05-10 10:15:30 -02:00
|
|
|
}
|
2015-01-09 15:27:47 +11:00
|
|
|
|
2016-04-12 15:11:07 +02:00
|
|
|
protected override void OnException(Exception x)
|
2013-01-18 09:00:18 -01:00
|
|
|
{
|
2015-10-30 11:24:18 +13:00
|
|
|
_logger.Error<UmbracoDatabase>("Database exception occurred", x);
|
2013-01-18 09:00:18 -01:00
|
|
|
base.OnException(x);
|
|
|
|
|
}
|
2015-07-22 12:10:21 +02:00
|
|
|
|
2016-04-12 15:11:07 +02:00
|
|
|
// fixme.poco - has new interceptors?
|
|
|
|
|
|
|
|
|
|
protected override void OnExecutingCommand(DbCommand cmd)
|
2016-02-04 14:51:19 +01:00
|
|
|
{
|
|
|
|
|
// if no timeout is specified, and the connection has a longer timeout, use it
|
|
|
|
|
if (OneTimeCommandTimeout == 0 && CommandTimeout == 0 && cmd.Connection.ConnectionTimeout > 30)
|
|
|
|
|
cmd.CommandTimeout = cmd.Connection.ConnectionTimeout;
|
|
|
|
|
base.OnExecutingCommand(cmd);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-12 15:11:07 +02:00
|
|
|
protected override void OnExecutedCommand(DbCommand cmd)
|
2015-07-22 12:10:21 +02:00
|
|
|
{
|
|
|
|
|
if (EnableSqlTrace)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug<UmbracoDatabase>(cmd.CommandText);
|
|
|
|
|
}
|
|
|
|
|
if (_enableCount)
|
|
|
|
|
{
|
|
|
|
|
SqlCount++;
|
|
|
|
|
}
|
|
|
|
|
base.OnExecutedCommand(cmd);
|
|
|
|
|
}
|
2016-04-12 15:11:07 +02:00
|
|
|
|
|
|
|
|
public IEnumerable<TResult> FetchByGroups<TResult, TSource>(IEnumerable<TSource> source, int groupSize, Func<IEnumerable<TSource>, Sql<SqlContext>> sqlFactory)
|
|
|
|
|
{
|
|
|
|
|
return source.SelectByGroups(x => Fetch<TResult>(sqlFactory(x)), groupSize);
|
|
|
|
|
}
|
2015-01-09 15:27:47 +11:00
|
|
|
}
|
2012-12-12 03:47:04 +05:00
|
|
|
}
|