Ayende @ Rahien

Hi!
My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

, @ Q c

Posts: 5,955 | Comments: 44,412

filter by tags archive

The REALLY long way to query with NHibernate


I am doing some work on NHibernate, mostly rediscovering how things works. It is quite amazing how those things work, to tell you the truth. A few days ago I had a conversation about the NH source and I made three rapid discoveries that reduce the complexity of a feature by three orders of magnitude.

The code that I am showing is how NHibernate manages its queries internally (well, one way of doing that). This is internal information, so not only you would never do that, but you canot even do that.

   1: using (var s = sessions.OpenSession())
   2: using (var tx = s.BeginTransaction())
   3: {
   4:     var sessionImplementor = ((ISessionImplementor)s);
   5:     var q = new QueryTranslator("query",
   6:                                 "query",
   7:                                 sessionImplementor.EnabledFilters,
   8:                                 sessionImplementor.Factory);
   9:  
  10:     var p = q.GetPersisterUsingImports("Animal");
  11:     var entityName = q.CreateNameFor(p.EntityName);
  12:     q.AddFromClass(entityName, p);
  13:  
  14:     q.SetAliasName("a", entityName);
  15:  
  16:     var currentName = q.Unalias("a.Id");
  17:     var pathParser = new PathExpressionParser();
  18:     pathParser.Start(q);
  19:     foreach (var token in new StringTokenizer(currentName, ".", true))
  20:     {
  21:         pathParser.Token(token, q);
  22:     }
  23:     pathParser.End(q);
  24:  
  25:     q.AddFromJoinOnly(pathParser.Name, pathParser.WhereJoin);
  26:  
  27:     q.AddNamedParameter("id");
  28:  
  29:     q.AppendWhereToken(new SqlString(pathParser.WhereColumns[0]));
  30:     q.AppendWhereToken(new SqlString("="));
  31:     q.AppendWhereToken(new SqlString(Parameter.Placeholder));
  32:  
  33:     q.RenderSqlAndPostInstansiate();
  34:     var fromDb = q.List(sessionImplementor, 
  35:         CreateQueryParameters(sessionImplementor, 
  36:             "id", 
  37:             new TypedValue(NHibernateUtil.Int32, 1, sessionImplementor.EntityMode)));
  38:  
  39:     var contains = s.Contains(fromDb[0]);
  40:     Assert.IsTrue(contains);
  41:  
  42:     tx.Commit();
  43: }

I just thought that this is an interesting piece of code.


Comments

Brian Chavez

NHibernate for LIFE now and FOREVER! Long live NHIBERNATE!!!

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

  1. What is new in RavenDB 3.5–Intro - 2 hours from now
  2. Production postmortem: The case of the infected cluster - about one day from now

There are posts all the way to Jul 09, 2015

RECENT SERIES

  1. The RavenDB Comic Strip (3):
    28 May 2015 - Part III – High availability & sleeping soundly
  2. Special Offer (2):
    27 May 2015 - 29% discount for all our products
  3. RavenDB Sharding (3):
    22 May 2015 - Adding a new shard to an existing cluster, splitting the shard
  4. Challenge (45):
    28 Apr 2015 - What is the meaning of this change?
  5. Interview question (2):
    30 Mar 2015 - fix the index
View all series

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats