/// <summary> /// Gets the Spring IDbProvider given the ISessionFactory. /// </summary> /// <remarks>The matching is performed by comparing the assembly qualified /// name string of the hibernate Driver.ConnectionType to those in /// the DbProviderFactory definitions. No connections are created /// in performing this comparison.</remarks> /// <param name="sessionFactory">The session factory.</param> /// <returns>The corresponding IDbProvider, null if no mapping was found.</returns> /// <exception cref="InvalidOperationException">If DbProviderFactory's ApplicaitonContext is not /// an instance of IConfigurableApplicaitonContext.</exception> public static IDbProvider GetDbProvider(ISessionFactory sessionFactory) { ISessionFactoryImplementor sfi = sessionFactory as ISessionFactoryImplementor; if (sfi != null) { IConnectionProvider cp = sfi.ConnectionProvider; if (cp != null) { IConfigurableApplicationContext ctx = DbProviderFactory.ApplicationContext as IConfigurableApplicationContext; if (ctx == null) { throw new InvalidOperationException( "Implementations of IApplicationContext must also implement IConfigurableApplicationContext"); } DriverBase db = cp.Driver as DriverBase; if (db != null) { Type hibCommandType = db.CreateCommand().GetType(); var providerNames = ctx.GetObjectNamesForType(typeof(DbProvider), true, false); string hibCommandAQN = hibCommandType.AssemblyQualifiedName; string hibCommandAQNWithoutVersion = hibCommandType.FullName + ", " + hibCommandType.Assembly.GetName().Name; foreach (string providerName in providerNames) { IObjectDefinition objectdef = ctx.ObjectFactory.GetObjectDefinition(providerName); ConstructorArgumentValues ctorArgs = objectdef.ConstructorArgumentValues; ConstructorArgumentValues.ValueHolder vh = ctorArgs.NamedArgumentValues["dbmetadata"] as ConstructorArgumentValues.ValueHolder; IObjectDefinition od = ((ObjectDefinitionHolder)vh.Value).ObjectDefinition; ConstructorArgumentValues dbmdCtorArgs = od.ConstructorArgumentValues; string commandType = dbmdCtorArgs.GetArgumentValue("commandType", typeof(string)).Value as string; if (hibCommandAQN.Equals(commandType) || hibCommandAQNWithoutVersion.Equals(commandType)) { IDbProvider prov = DbProviderFactory.GetDbProvider(providerName); return(prov); } } } else { log.Info("Could not derive IDbProvider from SessionFactory"); } } } return(null); }
public void SessionFactoryUtilsWithGetDbProvider() { ISessionFactoryImplementor sessionFactory = A.Fake <ISessionFactoryImplementor>(); DriverBase driver = A.Fake <DriverBase>(); A.CallTo(() => driver.CreateCommand()).Returns(new SqlCommand()); IConnectionProvider cp = A.Fake <IConnectionProvider>(); A.CallTo(() => cp.Driver).Returns(driver); A.CallTo(() => sessionFactory.ConnectionProvider).Returns(cp); IDbProvider provider = SessionFactoryUtils.GetDbProvider(sessionFactory); Assert.AreEqual(typeof(SqlCommand), provider.DbMetadata.CommandType); }
public void SessionFactoryUtilsWithGetDbProvider() { MockRepository mockery = new MockRepository(); ISessionFactoryImplementor sessionFactory = mockery.DynamicMock <ISessionFactoryImplementor>(); DriverBase driver = mockery.DynamicMock <DriverBase>(); Expect.Call(driver.CreateCommand()).Repeat.AtLeastOnce().Return(new SqlCommand()); IConnectionProvider cp = mockery.DynamicMock <IConnectionProvider>(); Expect.Call(cp.Driver).Repeat.AtLeastOnce().Return(driver); Expect.Call(sessionFactory.ConnectionProvider).Repeat.AtLeastOnce().Return(cp); mockery.ReplayAll(); IDbProvider provider = SessionFactoryUtils.GetDbProvider(sessionFactory); Assert.AreEqual(typeof(SqlCommand), provider.DbMetadata.CommandType); mockery.VerifyAll(); }