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();
IList<string> providerNames = ctx.GetObjectNamesForType(typeof(DbProvider), true, false);
string hibCommandAQN = hibCommandType.AssemblyQualifiedName;
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))
{
IDbProvider prov = DbProviderFactory.GetDbProvider(providerName);
return prov;
}
}
}
else
{
log.Info("Could not derive IDbProvider from SessionFactory");
}
}
}
return null;
}