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 1 commit
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 NHibernate.AdoNet;
Expand All @@ -10,12 +11,26 @@
using NHibernate.Hql.Ast.ANTLR;
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 @@ -184,6 +184,11 @@ public IDbConnection 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 @@ -185,6 +185,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 @@ -1337,6 +1338,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 @@ -1725,6 +1730,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 @@ -18,6 +19,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 @@ -14,6 +15,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 @@ -31,6 +31,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
9 changes: 9 additions & 0 deletions src/NHibernate/Driver/DB2400Driver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Data;

namespace NHibernate.Driver
{
Expand All @@ -20,6 +21,14 @@ public DB2400Driver() : base(
{
}

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

base.AddNotificationHandler(con, handler);
}

public override bool UseNamedPrefixInSql
{
get { return false; }
Expand Down
9 changes: 9 additions & 0 deletions src/NHibernate/Driver/DB2Driver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Data;

namespace NHibernate.Driver
{
Expand All @@ -21,6 +22,14 @@ public DB2Driver() : base(
{
}

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

base.AddNotificationHandler(con, handler);
}

public override bool UseNamedPrefixInSql
{
get { return false; }
Expand Down
13 changes: 12 additions & 1 deletion src/NHibernate/Driver/DotConnectMySqlDriver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace NHibernate.Driver
using System;
using System.Data;

namespace NHibernate.Driver
{
/// <summary>
/// Provides a database driver for dotConnect for MySQL by DevArt.
Expand Down Expand Up @@ -28,6 +31,14 @@ public DotConnectMySqlDriver() : base(
{
}

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

base.AddNotificationHandler(con, handler);
}

/// <summary>
/// Devart.Data.MySql uses named parameters in the sql.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/NHibernate/Driver/DriverBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ 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
//do nothing by default
}

public virtual void Configure(IDictionary<string, string> settings)
{
// Command timeout
Expand Down
9 changes: 9 additions & 0 deletions src/NHibernate/Driver/FirebirdClientDriver.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.Linq;
Expand Down Expand Up @@ -35,6 +36,14 @@ public FirebirdClientDriver()
{
}

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

base.AddNotificationHandler(con, handler);
}

public override bool UseNamedPrefixInSql
{
get { return true; }
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 NHibernate.Engine;
Expand Down Expand Up @@ -35,6 +36,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 IDbConnection object for the specific Driver
/// </summary>
Expand Down
11 changes: 11 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 All @@ -20,6 +23,14 @@ public IfxDriver()
{
}

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

base.AddNotificationHandler(con, handler);
}

public override bool UseNamedPrefixInSql
{
get { return false; }
Expand Down
9 changes: 9 additions & 0 deletions src/NHibernate/Driver/IngresDriver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Data;

namespace NHibernate.Driver
{
Expand All @@ -11,6 +12,14 @@ public class IngresDriver : ReflectionBasedDriver
{
public IngresDriver() : base("Ingres.Client", "Ingres.Client.IngresConnection", "Ingres.Client.IngresCommand") {}

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

base.AddNotificationHandler(con, handler);
}

public override bool UseNamedPrefixInSql
{
get { return false; }
Expand Down
9 changes: 9 additions & 0 deletions src/NHibernate/Driver/MySqlDataDriver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Data;

namespace NHibernate.Driver
{
Expand Down Expand Up @@ -32,6 +33,14 @@ public MySqlDataDriver() : base(
{
}

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

base.AddNotificationHandler(con, handler);
}

/// <summary>
/// MySql.Data uses named parameters in the sql.
/// </summary>
Expand Down
9 changes: 9 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;

namespace NHibernate.Driver
Expand Down Expand Up @@ -39,6 +40,14 @@ public NpgsqlDriver() : base(
{
}

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

base.AddNotificationHandler(con, handler);
}

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

base.AddNotificationHandler(con, handler);
}

public override IDbConnection CreateConnection()
{
return new OdbcConnection();
Expand Down
11 changes: 11 additions & 0 deletions src/NHibernate/Driver/OleDbDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ public OleDbDriver()
{
}

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

Choose a reason for hiding this comment

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

What if con is not OleDbConnection?

Copy link
Member Author

Choose a reason for hiding this comment

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

Can you tell me please how a connection created by OleDbDriver is not an OleDbConnection?

Copy link
Member

Choose a reason for hiding this comment

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

No ideas.

}

base.AddNotificationHandler(con, handler);
}

public override IDbConnection CreateConnection()
{
return new OleDbConnection();
Expand Down
9 changes: 9 additions & 0 deletions src/NHibernate/Driver/OracleClientDriver.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Data;
using NHibernate.Engine.Query;
using NHibernate.SqlTypes;
Expand All @@ -18,6 +19,14 @@ public OracleClientDriver() :
"System.Data.OracleClient.OracleConnection",
"System.Data.OracleClient.OracleCommand") { }

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

base.AddNotificationHandler(con, handler);
}

public override bool UseNamedPrefixInSql
{
get { return true; }
Expand Down
8 changes: 8 additions & 0 deletions src/NHibernate/Driver/OracleDataClientDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ public OracleDataClientDriver()
oracleDbTypeXmlType = Enum.Parse(oracleDbTypeEnum, "XmlType");
}

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

base.AddNotificationHandler(con, handler);
}

/// <summary></summary>
public override bool UseNamedPrefixInSql
{
Expand Down
Loading