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 3 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 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
1 change: 1 addition & 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 Down
1 change: 1 addition & 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 Down
5 changes: 4 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
16 changes: 16 additions & 0 deletions src/NHibernate/Driver/DriverBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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");
Copy link
Member

Choose a reason for hiding this comment

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

Please add the dot at the end of the message (as recommended by MS).

}

prop.AddEventHandler(con, handler);
}
}

public virtual void Configure(IDictionary<string, string> settings)
{
// Command timeout
Expand Down
1 change: 1 addition & 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
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
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
1 change: 1 addition & 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 Down
1 change: 1 addition & 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
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;

namespace NHibernate.Driver
Expand Down Expand Up @@ -39,6 +40,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 @@ -13,6 +13,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;
}

public override IDbConnection CreateConnection()
{
return new OdbcConnection();
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 IDbConnection CreateConnection()
{
return new OleDbConnection();
Expand Down
1 change: 1 addition & 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 Down
1 change: 1 addition & 0 deletions src/NHibernate/Driver/OracleLiteDataClientDriver.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Data;
using NHibernate.AdoNet;
using NHibernate.SqlTypes;
Expand Down
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.SqlClient;
using NHibernate.AdoNet;
Expand All @@ -23,6 +24,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="IDbConnection" /> object for
/// the SqlClientDriver.
Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Driver/SybaseAsaClientDriver.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
3 changes: 2 additions & 1 deletion src/NHibernate/Driver/SybaseAseClientDriver.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 @@ -17,7 +18,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
5 changes: 4 additions & 1 deletion src/NHibernate/Driver/SybaseSQLAnywhereDotNet4Driver.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>
/// SQL Dialect for SQL Anywhere 12 - for the NHibernate 3.2.0 distribution
Expand Down
5 changes: 4 additions & 1 deletion src/NHibernate/Driver/SybaseSQLAnywhereDriver.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>
/// The SybaseSQLAnywhereDriver Driver provides a database driver for Sybase SQL Anywhere 10 and above
Expand Down