-
Notifications
You must be signed in to change notification settings - Fork 934
Support criteria aliases in SqlProjection and SQLCriterion #2358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
173dce2
c26e1bc
ef69e28
28afb5a
60f53a9
19fdf99
086bd64
26ceac4
bcf2f11
9c81579
2c23c7e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
using System.Collections.Generic; | ||
using NHibernate.Dialect; | ||
using NHibernate.Criterion; | ||
using NHibernate.Linq; | ||
using NHibernate.SqlCommand; | ||
using NHibernate.Transform; | ||
using NHibernate.Type; | ||
|
@@ -970,7 +971,7 @@ public void ProjectionsTest() | |
ProjectionList p2 = Projections.ProjectionList() | ||
.Add(Projections.Min("StudentNumber")) | ||
.Add(Projections.Avg("StudentNumber")) | ||
.Add(Projections.SqlProjection( | ||
.Add(Projections.Sql( | ||
"1 as constOne, count(*) as countStar", | ||
new String[] { "constOne", "countStar" }, | ||
new IType[] { NHibernateUtil.Int32, NHibernateUtil.Int32 } | ||
|
@@ -1019,6 +1020,92 @@ public void ProjectionsTest() | |
s.Close(); | ||
} | ||
|
||
[Test] | ||
public void TestSQLProjectionWithAliases() | ||
{ | ||
using(ISession s = OpenSession()) | ||
using(ITransaction t = s.BeginTransaction()) | ||
{ | ||
Course course = new Course(); | ||
course.CourseCode = "HIB"; | ||
course.Description = "Hibernate Training"; | ||
s.Save(course); | ||
|
||
Student gavin = new Student(); | ||
gavin.Name = "Gavin King"; | ||
gavin.StudentNumber = 667; | ||
s.Save(gavin); | ||
|
||
Student xam = new Student(); | ||
xam.Name = "Max Rydahl Andersen"; | ||
xam.StudentNumber = 101; | ||
s.Save(xam); | ||
|
||
Enrolment enrolment = new Enrolment(); | ||
enrolment.Course = course; | ||
enrolment.CourseCode = course.CourseCode; | ||
enrolment.Semester = 1; | ||
enrolment.Year = 1999; | ||
enrolment.Student = xam; | ||
enrolment.StudentNumber = xam.StudentNumber; | ||
xam.Enrolments.Add(enrolment); | ||
s.Save(enrolment); | ||
|
||
enrolment = new Enrolment(); | ||
enrolment.Course = course; | ||
enrolment.CourseCode = course.CourseCode; | ||
enrolment.Semester = 3; | ||
enrolment.Year = 1998; | ||
enrolment.Student = gavin; | ||
enrolment.StudentNumber = gavin.StudentNumber; | ||
gavin.Enrolments.Add(enrolment); | ||
s.Save(enrolment); | ||
t.Commit(); | ||
} | ||
|
||
using (var s = OpenSession()) | ||
{ | ||
Student studentSubquery = null; | ||
var subquery = QueryOver.Of(() => studentSubquery) | ||
.And( | ||
Restrictions.Sql("{e}.studentId = 667 and {studentSubquery}.studentId = 667") | ||
.AddAliases("e", "studentSubquery")).Select(Projections.Id()); | ||
|
||
var uniqueResult = s.CreateCriteria(typeof(Student)) | ||
.Add(Subqueries.Exists(subquery.DetachedCriteria)) | ||
.AddOrder(Order.Asc("Name")) | ||
.CreateCriteria("Enrolments", "e") | ||
.AddOrder(Order.Desc("Year")) | ||
.AddOrder(Order.Desc("Semester")) | ||
.CreateCriteria("Course", "c") | ||
.AddOrder(Order.Asc("Description")) | ||
.SetProjection( | ||
Projections.Sql( | ||
"{alias}.studentId as studentNumber, {e}.Semester as semester," | ||
+ " {c}.CourseCode as courseCode, {c}.Description as descr", | ||
new string[] {"studentNumber", "semester", "courseCode", "descr"}, | ||
new[] | ||
{ | ||
TypeFactory.HeuristicType(typeof(long)), | ||
TypeFactory.HeuristicType(typeof(short)), | ||
TypeFactory.HeuristicType(typeof(string)), | ||
TypeFactory.HeuristicType(typeof(string)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A little side-track rant: I know, it is a copy-paste from other test, but There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I agree we should fight it. Though not sure about this place.. This is dictated by method signature. No? To avoid it we need to provide method where all related data is supplied in single parameter. Such wrapping in tests seems excessive... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But this is not only in tests. The test reflect real-like use cases. |
||
}).AddAliases("e", "c")) | ||
.UniqueResult(); | ||
|
||
Assert.That(uniqueResult, Is.Not.Null); | ||
} | ||
|
||
using (var s = OpenSession()) | ||
using (s.BeginTransaction()) | ||
{ | ||
s.Query<Enrolment>().Delete(); | ||
s.Query<Student>().Delete(); | ||
s.Query<Course>().Delete(); | ||
s.GetCurrentTransaction().Commit(); | ||
} | ||
} | ||
|
||
[Test] | ||
public void CloningProjectionsTest() | ||
{ | ||
|
@@ -1163,7 +1250,7 @@ public void CloningProjectionsTest() | |
ProjectionList p2 = Projections.ProjectionList() | ||
.Add(Projections.Min("StudentNumber")) | ||
.Add(Projections.Avg("StudentNumber")) | ||
.Add(Projections.SqlProjection( | ||
.Add(Projections.Sql( | ||
"1 as constOne, count(*) as countStar", | ||
new String[] { "constOne", "countStar" }, | ||
new IType[] { NHibernateUtil.Int32, NHibernateUtil.Int32 } | ||
|
@@ -1463,7 +1550,7 @@ public void ProjectionsUsingProperty() | |
ProjectionList p2 = Projections.ProjectionList() | ||
.Add(Property.ForName("StudentNumber").Min()) | ||
.Add(Property.ForName("StudentNumber").Avg()) | ||
.Add(Projections.SqlProjection( | ||
.Add(Projections.Sql( | ||
"1 as constOne, count(*) as countStar", | ||
new String[] { "constOne", "countStar" }, | ||
new IType[] { NHibernateUtil.Int32, NHibernateUtil.Int32 } | ||
|
@@ -1956,7 +2043,7 @@ public void CloningProjectionsUsingProperty() | |
ProjectionList p2 = Projections.ProjectionList() | ||
.Add(Property.ForName("StudentNumber").Min()) | ||
.Add(Property.ForName("StudentNumber").Avg()) | ||
.Add(Projections.SqlProjection( | ||
.Add(Projections.Sql( | ||
"1 as constOne, count(*) as countStar", | ||
new String[] { "constOne", "countStar" }, | ||
new IType[] { NHibernateUtil.Int32, NHibernateUtil.Int32 } | ||
|
@@ -2575,7 +2662,7 @@ public void SqlExpressionWithParameters() | |
{ | ||
ICriteria c = session.CreateCriteria(typeof(Student)); | ||
c.Add(Expression.Eq("StudentNumber", (long)232)); | ||
c.Add(Expression.Sql("2 = ?", 1, NHibernateUtil.Int32)); | ||
c.Add(Restrictions.Sql("2 = ?", 1, NHibernateUtil.Int32)); | ||
|
||
Student gavin = new Student(); | ||
gavin.Name = "Gavin King"; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will there be cases when user will want to
.AddAliases(...)
later? If not I think it is better to addparams[] aliases
parameter to theRestrictions.Sql
method instead of introducing a new method.