1

I have a project decompiled in IlSpy and code below. Where can I find the raw SQL query? How criteria.List() know which select use to?

    protected object ListOne(ISession session, DbBaseArgs args)
    {
            ICriteria criteria = session.CreateCriteria(((DbListArgs)args).DbType);
            criteria.SetMaxResults(1);
            for (int i = 0; i < ((DbListArgs)args).DbExpressions.Count; i++)
            {
                if (((DbListArgs)args).DbExpressions[i] is Order)
                {
                    criteria.AddOrder((Order)((DbListArgs)args).DbExpressions[i]);

                }
                else
                {
                    criteria.Add((ICriterion)((DbListArgs)args).DbExpressions[i]);

                }
            IList results = criteria.List();
            object result;
            if (results != null && results.Count > 0)
            {
                result = results[0];
            }
            else
            {
                result = null;
            }
            return result;
}
Wachburn
  • 2,842
  • 5
  • 36
  • 59

2 Answers2

0

Decompiling does not help you to find the raw SQL Query. You can try to add a log for the logger named NHibernate.SQL at the debug level and you should see in the appender configured the queries issued to the database. For example add in the log configuration something like:

<logger name="NHibernate.SQL" additivity="false">
    <level value="ALL"/>
    <appender-ref ref="NHibernateSQLFileLog"/>
</logger>
Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
0

Your options are to

  1. Run your app and view the log.
  2. Write a bit of code to get the SQL to be generated.
  3. Try NHProf

(from linked answer):

public String GetGeneratedSql(ICriteria criteria)
{
    var criteriaImpl = (CriteriaImpl) criteria;
    var sessionImpl = (SessionImpl) criteriaImpl.Session;
    var factory = (SessionFactoryImpl) sessionImpl.SessionFactory;
    var implementors = factory.GetImplementors(criteriaImpl.EntityOrClassName);
    var loader = new CriteriaLoader((IOuterJoinLoadable) factory.GetEntityPersister(implementors[0]), factory, criteriaImpl, implementors[0], sessionImpl.EnabledFilters);

    return loader.SqlString.ToString();
}
Community
  • 1
  • 1
hometoast
  • 11,522
  • 5
  • 41
  • 58