Skip to content

NH-3724 - Added support for notification handlers #363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/NHibernate.Test/CfgTest/Loquacious/ConfigurationFixture.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
Expand All @@ -12,12 +13,26 @@
using NHibernate.Linq;
using NHibernate.Type;
using NUnit.Framework;
using Environment = NHibernate.Cfg.Environment;

namespace NHibernate.Test.CfgTest.Loquacious
{
[TestFixture]
public class ConfigurationFixture
{
[Test]
public void CanSetNotificationHandler()
{
//NH-3724
EventHandler<SqlInfoMessageEventArgs> handler = (s, e) => {};
var cfg = new Configuration().DataBaseIntegration(x =>
{
x.WithNotificationHandler(handler);
}).Configure();

Assert.IsNotNull(cfg.NotificationHandler);
}

[Test]
public void CompleteConfiguration()
{
Expand Down
5 changes: 5 additions & 0 deletions src/NHibernate/AdoNet/ConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ public DbConnection GetConnection()
if (ownConnection)
{
connection = Factory.ConnectionProvider.GetConnection();
//NH-3724
if (Factory.Settings.NotificationHandler != null)
{
Factory.ConnectionProvider.Driver.AddNotificationHandler(connection, Factory.Settings.NotificationHandler);
}
if (Factory.Statistics.IsStatisticsEnabled)
{
Factory.StatisticsImplementor.Connect();
Expand Down
7 changes: 7 additions & 0 deletions src/NHibernate/Cfg/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ protected void Reset()
tableNameBinding = new Dictionary<string, Mappings.TableDescription>();
columnNameBindingPerTable = new Dictionary<Table, Mappings.ColumnNames>();
filtersSecondPasses = new Queue<FilterSecondPassArgs>();
NotificationHandler = null;
}
[Serializable]
private class Mapping : IMapping
Expand Down Expand Up @@ -1335,6 +1336,10 @@ public Configuration SetDefaultNamespace(string newDefaultNamespace)
return this;
}

//NH-3724
[field: NonSerialized]
public Delegate NotificationHandler { get; set; }

/// <summary>
/// Sets the default interceptor for use by all sessions.
/// </summary>
Expand Down Expand Up @@ -1723,6 +1728,8 @@ private Settings BuildSettings()
// NH : Set configuration for IdGenerator SQL logging
PersistentIdGeneratorParmsNames.SqlStatementLogger.FormatSql = result.SqlStatementLogger.FormatSql;
PersistentIdGeneratorParmsNames.SqlStatementLogger.LogToStdout = result.SqlStatementLogger.LogToStdout;
//NH-3724
result.NotificationHandler = NotificationHandler;
return result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Data;
using NHibernate.AdoNet;
using NHibernate.Connection;
Expand All @@ -19,6 +20,12 @@ public DbIntegrationConfigurationProperties(Configuration configuration)

#region Implementation of IDbIntegrationConfigurationProperties

public void WithNotificationHandler(Delegate handler)
{
//NH-3724
configuration.NotificationHandler = handler;
}

public void Dialect<TDialect>() where TDialect : Dialect.Dialect
{
configuration.SetProperty(Environment.Dialect, typeof(TDialect).AssemblyQualifiedName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Data;
using NHibernate.AdoNet;
using NHibernate.Connection;
Expand All @@ -15,6 +16,9 @@ public interface IDbIntegrationConfigurationProperties
bool LogSqlInConsole { set; }
bool LogFormattedSql { set; }

//NH-3724
void WithNotificationHandler(Delegate handler);

void ConnectionProvider<TProvider>() where TProvider : IConnectionProvider;
void Driver<TDriver>() where TDriver : IDriver;
IsolationLevel IsolationLevel { set; }
Expand Down
3 changes: 3 additions & 0 deletions src/NHibernate/Cfg/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public Settings()
//private bool isJdbcBatchVersionedData;

#endregion

public Delegate NotificationHandler { get; internal set; }

public SqlStatementLogger SqlStatementLogger { get; internal set; }

public int MaximumFetchDepth { get; internal set; }
Expand Down
16 changes: 16 additions & 0 deletions src/NHibernate/Driver/DriverBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ public abstract class DriverBase : IDriver, ISqlParameterFormatter
private int commandTimeout;
private bool prepareSql;

public virtual void AddNotificationHandler(IDbConnection con, Delegate handler)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better to throw not supported exception

{
//NH-3724
if (handler != null)
{
var prop = con.GetType().GetEvent("InfoMessage");

if (prop == null)
{
throw new NotSupportedException("Current driver does not support notifications.");
}

prop.AddEventHandler(con, handler);
}
}

public virtual void Configure(IDictionary<string, string> settings)
{
// Command timeout
Expand Down
8 changes: 8 additions & 0 deletions src/NHibernate/Driver/IDriver.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
Expand Down Expand Up @@ -36,6 +37,13 @@ public interface IDriver
/// </summary>
void Configure(IDictionary<string, string> settings);

//NH-3724
/// <summary>
/// Attaches an event handler for the notification event (InfoMessage in most ADO.NET drivers).
/// </summary>
/// <param name="handler"></param>
void AddNotificationHandler(IDbConnection con, Delegate handler);

/// <summary>
/// Creates an uninitialized DbConnection object for the specific Driver
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions src/NHibernate/Driver/IfxDriver.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using System.Data;

namespace NHibernate.Driver
{
/// <summary>
Expand Down
2 changes: 0 additions & 2 deletions src/NHibernate/Driver/MySqlDataDriver.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace NHibernate.Driver
{
/// <summary>
Expand Down
7 changes: 7 additions & 0 deletions src/NHibernate/Driver/NpgsqlDriver.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Data;
using System.Data.Common;

Expand Down Expand Up @@ -40,6 +41,12 @@ public NpgsqlDriver() : base(
{
}

public override void AddNotificationHandler(IDbConnection con, Delegate handler)
{
//NH-3724
con.GetType().GetEvent("Notification").AddEventHandler(con, handler);
}

public override bool UseNamedPrefixInSql
{
get { return true; }
Expand Down
6 changes: 6 additions & 0 deletions src/NHibernate/Driver/OdbcDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ namespace NHibernate.Driver
/// </remarks>
public class OdbcDriver : DriverBase
{
public override void AddNotificationHandler(IDbConnection con, Delegate handler)
{
//NH-3724
(con as OdbcConnection).InfoMessage += (OdbcInfoMessageEventHandler)handler;
}

private static readonly IInternalLogger Log = LoggerProvider.LoggerFor(typeof(OdbcDriver));

private byte? _dbDateTimeScale;
Expand Down
6 changes: 6 additions & 0 deletions src/NHibernate/Driver/OleDbDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public OleDbDriver()
{
}

public override void AddNotificationHandler(IDbConnection con, Delegate handler)
{
//NH-3724
(con as OleDbConnection).InfoMessage += (OleDbInfoMessageEventHandler)handler;
}

public override DbConnection CreateConnection()
{
return new OleDbConnection();
Expand Down
111 changes: 55 additions & 56 deletions src/NHibernate/Driver/SQLite20Driver.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
using System;
using System.Data;
using System.Data.Common;

namespace NHibernate.Driver
{
/// <summary>
/// NHibernate driver for the System.Data.SQLite data provider for .NET 2.0.
/// </summary>
/// <remarks>
/// <p>
/// In order to use this driver you must have the System.Data.SQLite.dll assembly available
/// for NHibernate to load. This assembly includes the SQLite.dll or SQLite3.dll libraries.
/// </p>
/// <p>
/// You can get the System.Data.SQLite.dll assembly from http://sourceforge.net/projects/sqlite-dotnet2.
/// </p>
/// <p>
/// Please check <a href="http://www.sqlite.org/">http://www.sqlite.org/</a> for more information regarding SQLite.
/// </p>
/// </remarks>
public class SQLite20Driver : ReflectionBasedDriver
{
/// <summary>
/// Initializes a new instance of <see cref="SQLite20Driver"/>.
/// </summary>
/// <exception cref="HibernateException">
/// Thrown when the <c>SQLite.NET</c> assembly can not be loaded.
/// </exception>
public SQLite20Driver() : base(
"System.Data.SQLite",
"System.Data.SQLite",
"System.Data.SQLite.SQLiteConnection",
"System.Data.SQLite.SQLiteCommand")
{
}
/// <summary>
/// NHibernate driver for the System.Data.SQLite data provider for .NET 2.0.
/// </summary>
/// <remarks>
/// <p>
/// In order to use this driver you must have the System.Data.SQLite.dll assembly available
/// for NHibernate to load. This assembly includes the SQLite.dll or SQLite3.dll libraries.
/// </p>
/// <p>
/// You can get the System.Data.SQLite.dll assembly from http://sourceforge.net/projects/sqlite-dotnet2.
/// </p>
/// <p>
/// Please check <a href="http://www.sqlite.org/">http://www.sqlite.org/</a> for more information regarding SQLite.
/// </p>
/// </remarks>
public class SQLite20Driver : ReflectionBasedDriver
{
/// <summary>
/// Initializes a new instance of <see cref="SQLite20Driver"/>.
/// </summary>
/// <exception cref="HibernateException">
/// Thrown when the <c>SQLite.NET</c> assembly can not be loaded.
/// </exception>
public SQLite20Driver() : base(
"System.Data.SQLite",
"System.Data.SQLite",
"System.Data.SQLite.SQLiteConnection",
"System.Data.SQLite.SQLiteCommand")
{
}

public override DbConnection CreateConnection()
{
Expand All @@ -57,34 +56,34 @@ private static void Connection_StateChange(object sender, StateChangeEventArgs e
}
}

public override bool UseNamedPrefixInSql
{
get { return true; }
}
public override bool UseNamedPrefixInSql
{
get { return true; }
}

public override bool UseNamedPrefixInParameter
{
get { return true; }
}
public override bool UseNamedPrefixInParameter
{
get { return true; }
}

public override string NamedPrefix
{
get { return "@"; }
}
public override string NamedPrefix
{
get { return "@"; }
}

public override bool SupportsMultipleOpenReaders
{
get { return false; }
}
public override bool SupportsMultipleOpenReaders
{
get { return false; }
}

public override IResultSetsCommand GetResultSetsCommand(Engine.ISessionImplementor session)
{
return new BasicResultSetsCommand(session);
}
public override IResultSetsCommand GetResultSetsCommand(Engine.ISessionImplementor session)
{
return new BasicResultSetsCommand(session);
}

public override bool SupportsMultipleQueries
{
get { return true; }
}
}
public override bool SupportsMultipleQueries
{
get { return true; }
}
}
}
7 changes: 7 additions & 0 deletions src/NHibernate/Driver/SqlClientDriver.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
Expand Down Expand Up @@ -26,6 +27,12 @@ public class SqlClientDriver : DriverBase, IEmbeddedBatcherFactoryProvider
public const byte MaxDateTime2 = 8;
public const byte MaxDateTimeOffset = 10;

public override void AddNotificationHandler(IDbConnection con, Delegate handler)
{
//NH-3724
(con as SqlConnection).InfoMessage += (SqlInfoMessageEventHandler) handler;
}

/// <summary>
/// Creates an uninitialized <see cref="DbConnection" /> object for
/// the SqlClientDriver.
Expand Down
1 change: 0 additions & 1 deletion src/NHibernate/Driver/SqlServerCeDriver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
Expand Down
6 changes: 2 additions & 4 deletions src/NHibernate/Driver/SybaseAseClientDriver.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace NHibernate.Driver
namespace NHibernate.Driver
{
/// <summary>
/// This provides a driver for Sybase ASE 15 using the ADO.NET driver.
Expand All @@ -17,7 +15,7 @@ public class SybaseAseClientDriver : ReflectionBasedDriver
public SybaseAseClientDriver() : base("Sybase.AdoNet2.AseClient", "Sybase.Data.AseClient.AseConnection", "Sybase.Data.AseClient.AseCommand")
{
}

public override string NamedPrefix
{
get { return "@"; }
Expand Down