Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding dynamic queries capabilities

  • Loading branch information...
commit 1b9e34da96fe7bbfb3be076f9a73c2371c7f4aa8 1 parent dadd4b3
Ayende Rahien authored
Showing with 96 additions and 14 deletions.
  1. +1 −1  Hibernate.cfg.xml
  2. +95 −13 Program.cs
2  Hibernate.cfg.xml
View
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
- <property name="hbm2ddl.auto">create</property>
+ <!--<property name="hbm2ddl.auto">create</property>-->
<property name="connection.connection_string_name">Default</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
108 Program.cs
View
@@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
+using System.Threading;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;
@@ -30,7 +31,7 @@ static void Main(string[] args)
HibernatingRhinos.Profiler.Appender.ProfilerInfrastructure.FlushAllMessages();
}
- Console.ReadLine();
+ Thread.Sleep(1000);
}
@@ -40,25 +41,106 @@ private static void Action(ISessionFactory sessionFactory)
using (var session = sessionFactory.OpenSession())
using (var tx = session.BeginTransaction())
{
- var enableFilter = session.EnableFilter("ByClientId");
- enableFilter.SetParameterList("clientId", new[] { 12, 23, 4 });
- session.Query<Question>()
- .Where(x=>x.Test.Score == 1)
- .FetchMany(x=>x.Answers)
- .ToList();
+ QueryDynamically<Test>(session,
+ new QueryOp { Field = "Score", Operator = "=", Value = 1 },
+ new QueryOp { Field = "Score", Operator = "=", Value = 5 },
+ new QueryOp { Field = "ClientId", Operator = ">", Value = 1 },
+ new QueryOp { Field = "ClientId", Operator = "<", Value = 3 },
+ new QueryOp { Field = "Questions.Text", Operator = "=", Value = "Hello" },
+ new QueryOp { Field = "Questions.Text", Operator = "=", Value = "Bye" }
+ );
- var test = session.Query<Test>()
- .FetchMany(x => x.Questions)
- .Where(x=>x.Score == 1)
- .ToList();
+ tx.Commit();
+ }
+ }
+ public class QueryOp
+ {
+ public string Field;
+ public object Value;
+ public string Operator;
+
+ }
+ public class FieldHelper
+ {
+ public string Field;
- ObjectDumper.Write(test, 1);
+ public string JustField
+ {
+ get
+ {
+ var lastIndexOf = Field.LastIndexOf('.');
- tx.Commit();
+ return Field.Substring(lastIndexOf + 1);
+
+ }
+ }
+ public string Path
+ {
+ get
+ {
+ var lastIndexOf = Field.LastIndexOf('.');
+ if (lastIndexOf == -1)
+ return null;
+
+ return Field.Substring(0, lastIndexOf);
+ }
+ }
+ }
+
+ private static IList<T> QueryDynamically<T>(ISession session, params QueryOp[] ops) where T : class
+ {
+ var query = session.CreateCriteria<T>();
+ foreach (var operationsOnField in ops.GroupBy(x => x.Field))
+ {
+ var criteria = new List<ICriterion>();
+ var fieldHelper = new FieldHelper { Field = operationsOnField.Key };
+ foreach (var queryOp in operationsOnField)
+ {
+ switch (queryOp.Operator)
+ {
+ case "=":
+ criteria.Add(Restrictions.Eq(fieldHelper.JustField, queryOp.Value));
+ break;
+ case ">":
+ criteria.Add(Restrictions.Gt(fieldHelper.JustField, queryOp.Value));
+ break;
+ case "<":
+ criteria.Add(Restrictions.Lt(fieldHelper.JustField, queryOp.Value));
+ break;
+ default:
+ throw new ArgumentOutOfRangeException(queryOp.Operator);
+ }
+ }
+ var currentQuery = query;
+ if(fieldHelper.Path != null)
+ {
+ currentQuery = query.GetCriteriaByPath(fieldHelper.Path)
+ ?? query.CreateCriteria(fieldHelper.Path);
+ }
+ switch (criteria.Count)
+ {
+ case 0:
+ break;
+ case 1:
+ currentQuery.Add(criteria.First());
+ break;
+ default:
+ var dis = new Disjunction();
+
+ foreach (var criterion in criteria)
+ {
+ dis.Add(criterion);
+ }
+
+ currentQuery.Add(dis);
+ break;
+ }
}
+
+ return query.List<T>();
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.