For diagnostic purposes only, we use this hack (NHibernate 1.20):
Code:
public string ToSql(string hqlQueryText)
{
string sqlText = String.Empty;
if (!string.IsNullOrEmpty(hqlQueryText))
{
IDictionary enabledFilters = new Hashtable();
NHibernate.Hql.Classic.QueryTranslator translator =
new NHibernate.Hql.Classic.QueryTranslator(
(NHibernate.Engine.ISessionFactoryImplementor) YourSessionFactory,
hqlQueryText,
enabledFilters);
translator.Compile(
YourReplacements,
false);
// Aaargh, the property we want is protected ...
// get at it through reflection
sqlText = GetPropertyValue(translator, "SqlString").ToString();
}
return sqlText;
}
public PropertyInfo GetProperty(object instance, string propertyName)
{
ocArgumentHelper.RequireValue("instance", instance);
ocArgumentHelper.RequireValue("propertyName", propertyName);
Type searchType = instance.GetType();
return GetProperty(searchType, propertyName, BindingFlags.Instance);
}
public PropertyInfo GetProperty(Type searchType, string propertyName)
{
return GetProperty(searchType, propertyName, BindingFlags.Instance);
}
public PropertyInfo GetProperty(Type searchType, string propertyName, BindingFlags bindingFlags)
{
bindingFlags |= BindingFlags.Public;
bindingFlags |= BindingFlags.NonPublic;
bindingFlags |= BindingFlags.DeclaredOnly;
while (searchType != null)
{
foreach (PropertyInfo property in searchType.GetProperties(bindingFlags))
{
if (property.Name == propertyName)
{
return property;
}
}
if (searchType.IsInterface)
{
PropertyInfo inheritedInterfaceProperty;
foreach (Type subInterfaceType in searchType.GetInterfaces())
{
inheritedInterfaceProperty = GetProperty(
subInterfaceType, propertyName, bindingFlags);
if (inheritedInterfaceProperty != null)
{
return inheritedInterfaceProperty;
}
}
}
else
{
searchType = searchType.BaseType;
}
}
return null;
}
public object GetPropertyValue(object instance, string propertyName)
{
PropertyInfo property = GetProperty(instance.GetType(), propertyName, BindingFlags.Instance);
if (property == null)
{
throw new MissingMemberException(instance.GetType().Name, propertyName);
}
return property.GetValue(instance, null);
}