Quantcast
Channel: Development With A Dot
Viewing all articles
Browse latest Browse all 404

Getting the SQL for HQL and Criteria Queries

$
0
0

OK, so, I already showed how we can get the SQL that was generated from a LINQ query. Of course, we can do the same for both HQL and Criteria APIs as well (QueryOver is just a wrapper around Criteria, mind you).

So, for HQL (and SQL), it goes like this:

   1:publicstaticclass QueryExtensions
   2: {
   3:privatestaticreadonly PropertyInfo sessionProperty = typeof (QueryImpl).GetProperty("Session", BindingFlags.NonPublic | BindingFlags.Instance);
   4:  
   5:publicstatic String ToSql(this IQuery query)
   6:     {
   7:if (query is QueryImpl)
   8:         {
   9:             var session = sessionProperty.GetValue(query, null) as ISession;
  10:             var sessionImpl = session.GetSessionImplementation();
  11:             var translatorFactory = new ASTQueryTranslatorFactory();
  12:             var translators = translatorFactory.CreateQueryTranslators(query.QueryString, null, false, sessionImpl.EnabledFilters, sessionImpl.Factory);
  13:  
  14:return translators[0].SQLString;
  15:         }
  16:elseif (query is SqlQueryImpl)
  17:         {
  18:return (query.QueryString);
  19:         }
  20:  
  21:throw (new NotSupportedException("Query type is not supported."));
  22:     }
  23: }

You can pass any implementation if IQuery, such as one produced from ISession.CreateQuery() or ISession.CreateSQLQuery(). The static field is merely for performance reasons.

As for Criteria:

   1:publicstaticclass CriteriaExtensions
   2: {
   3:publicstatic String ToSql(this ICriteria criteria)
   4:     {
   5:         var criteriaImpl = criteria as CriteriaImpl;
   6:         var sessionImpl = criteriaImpl.Session;
   7:         var factory = sessionImpl.Factory;
   8:         var implementors = factory.GetImplementors(criteriaImpl.EntityOrClassName);
   9:         var loader = new CriteriaLoader(factory.GetEntityPersister(implementors[0]) as IOuterJoinLoadable, factory, criteriaImpl, implementors[0], sessionImpl.EnabledFilters);
  10:  
  11:return loader.SqlString.ToString();
  12:     }
  13: }

And finally, QueryOver, just a small wrapper around the Criteria version:

   1:publicstaticclass QueryOverExtensions
   2: {
   3:publicstatic String ToSql(this IQueryOver queryOver)
   4:     {
   5:         var criteria = queryOver.UnderlyingCriteria;
   6:return (criteria.ToSql());
   7:     }
   8: }

Hope you find this useful! Winking smile


Viewing all articles
Browse latest Browse all 404

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>