Skip to content

Commit e7f2d52

Browse files
author
Sergey Koshcheyev
committed
NH-891 - a small fix to allow parameters as array indices in HQL.
SVN: trunk@2604
1 parent 89572e4 commit e7f2d52

File tree

5 files changed

+37
-22
lines changed

5 files changed

+37
-22
lines changed

src/NHibernate.Test/Legacy/FooBarTest.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -744,10 +744,13 @@ public void Query()
744744
Assert.IsTrue(IsEmpty(s.CreateQuery("selecT baz from baz in class Baz where baz.StringDateMap['foo'] is not null or baz.StringDateMap['bar'] = ?")
745745
.SetDateTime(0, DateTime.Today).Enumerable()));
746746

747-
list = s.CreateQuery( "select baz from baz in class Baz where baz.StringDateMap['now'] is not null").List();
747+
list = s.CreateQuery("select baz from baz in class Baz where baz.StringDateMap['now'] is not null").List();
748748
Assert.AreEqual( 1, list.Count );
749749

750-
list = s.CreateQuery( "select baz from baz in class Baz where baz.StringDateMap['now'] is not null and baz.StringDateMap['big bang'] < baz.StringDateMap['now']").List();
750+
list = s.CreateQuery("select baz from baz in class Baz where baz.StringDateMap[:now] is not null").SetString("now", "now").List();
751+
Assert.AreEqual(1, list.Count);
752+
753+
list = s.CreateQuery("select baz from baz in class Baz where baz.StringDateMap['now'] is not null and baz.StringDateMap['big bang'] < baz.StringDateMap['now']").List();
751754
Assert.AreEqual( 1, list.Count );
752755

753756
list = s.CreateQuery( "select index(date) from Baz baz join baz.StringDateMap date").List();

src/NHibernate/Hql/Classic/PathExpressionParser.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ public sealed class CollectionElement // struct?
462462
public JoinSequence JoinSequence;
463463

464464
/// <summary></summary>
465-
public StringBuilder IndexValue = new StringBuilder();
465+
public SqlStringBuilder IndexValue = new SqlStringBuilder();
466466
}
467467

468468
private bool expectingCollectionIndex;
@@ -477,12 +477,9 @@ public CollectionElement LastCollectionElement()
477477
}
478478

479479
/// <summary></summary>
480-
public string LastCollectionElementIndexValue
480+
public void SetLastCollectionElementIndexValue(SqlString value)
481481
{
482-
set
483-
{
484-
( ( CollectionElement ) collectionElements[ collectionElements.Count - 1 ] ).IndexValue.Append( value ); //getlast
485-
}
482+
( ( CollectionElement ) collectionElements[ collectionElements.Count - 1 ] ).IndexValue.Add( value ); //getlast
486483
}
487484

488485
/// <summary></summary>

src/NHibernate/Hql/Classic/WhereParser.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,16 @@ private void CloseExpression( QueryTranslator q, string lcToken )
336336
}
337337

338338
// Add any joins
339-
StringBuilder lastJoin = ( StringBuilder ) joins[ joins.Count - 1 ];
339+
SqlStringBuilder lastJoin = ( SqlStringBuilder ) joins[ joins.Count - 1 ];
340340
joins.RemoveAt( joins.Count - 1 );
341-
AppendToken( q, lastJoin.ToString() );
341+
AppendToken( q, lastJoin.ToSqlString() );
342342
}
343343
else
344344
{
345345
//unaryCounts.removeLast(); //check that its zero? (As an assertion)
346-
StringBuilder join = ( StringBuilder ) joins[ joins.Count - 1 ];
346+
SqlStringBuilder join = ( SqlStringBuilder ) joins[ joins.Count - 1 ];
347347
joins.RemoveAt( joins.Count - 1 );
348-
( ( StringBuilder ) joins[ joins.Count - 1 ] ).Append( join.ToString() );
348+
( ( SqlStringBuilder ) joins[ joins.Count - 1 ] ).Add( join.ToSqlString() );
349349
}
350350

351351
bool lastNots = ( bool ) nots[ nots.Count - 1 ];
@@ -365,7 +365,7 @@ private void OpenExpression( QueryTranslator q, string lcToken )
365365
{
366366
nots.Add( false );
367367
booleanTests.Add( false );
368-
joins.Add( new StringBuilder() );
368+
joins.Add( new SqlStringBuilder() );
369369
if( !StringHelper.OpenParen.Equals( lcToken ) )
370370
{
371371
AppendToken( q, StringHelper.OpenParen );
@@ -431,7 +431,7 @@ private void AddJoin(JoinSequence joinSequence, QueryTranslator q)
431431
q.AddFromJoinOnly(pathExpressionParser.Name, joinSequence);
432432
try
433433
{
434-
AddToCurrentJoin(joinSequence.ToJoinFragment(q.EnabledFilters, true).ToWhereFragmentString.ToString());
434+
AddToCurrentJoin(joinSequence.ToJoinFragment(q.EnabledFilters, true).ToWhereFragmentString);
435435
}
436436
catch (MappingException me)
437437
{
@@ -537,16 +537,16 @@ private void DoToken( string token, QueryTranslator q )
537537
}
538538
}
539539

540-
private void AddToCurrentJoin( string sql )
540+
private void AddToCurrentJoin( SqlString sql )
541541
{
542-
( ( StringBuilder ) joins[ joins.Count - 1 ] ).Append( sql );
542+
( ( SqlStringBuilder ) joins[ joins.Count - 1 ] ).Add( sql );
543543
}
544544

545545
private void AddToCurrentJoin( PathExpressionParser.CollectionElement ce )
546546
{
547547
try
548548
{
549-
AddToCurrentJoin(ce.JoinSequence.ToJoinFragment().ToWhereFragmentString + ce.IndexValue.ToString());
549+
AddToCurrentJoin(ce.JoinSequence.ToJoinFragment().ToWhereFragmentString + ce.IndexValue.ToSqlString());
550550
}
551551
catch (MappingException me)
552552
{
@@ -579,7 +579,7 @@ protected virtual void AppendToken( QueryTranslator q, string token )
579579
{
580580
if( expectingIndex > 0 )
581581
{
582-
pathExpressionParser.LastCollectionElementIndexValue = token;
582+
pathExpressionParser.SetLastCollectionElementIndexValue(new SqlString(token));
583583
}
584584
else
585585
{
@@ -602,7 +602,7 @@ protected virtual void AppendToken( QueryTranslator q, SqlString token )
602602
{
603603
if( expectingIndex > 0 )
604604
{
605-
pathExpressionParser.LastCollectionElementIndexValue = token.ToString();
605+
pathExpressionParser.SetLastCollectionElementIndexValue(token);
606606
}
607607
else
608608
{

src/NHibernate/Impl/EnumerableImpl.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using System.Data;
44
using log4net;
55
using NHibernate.Engine;
6+
using NHibernate.Exceptions;
67
using NHibernate.Hql;
8+
using NHibernate.SqlCommand;
79
using NHibernate.Type;
810

911
namespace NHibernate.Impl
@@ -154,8 +156,16 @@ public object Current
154156
///</returns>
155157
public bool MoveNext()
156158
{
157-
PostMoveNext( _reader.Read() );
158-
159+
bool readResult;
160+
try
161+
{
162+
readResult = _reader.Read();
163+
}
164+
catch (Exception e)
165+
{
166+
throw ADOExceptionHelper.Convert(e, "Error executing Enumerable() query", new SqlString(this._cmd.CommandText));
167+
}
168+
PostMoveNext( readResult );
159169
return _hasNext;
160170
}
161171

src/NHibernate/SqlCommand/SqlStringBuilder.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,12 @@ public SqlStringBuilder AddObject( object part )
142142

143143
// remarks - we should not get to here - this is a problem with the
144144
// SQL being generated.
145-
throw new ArgumentException( "Part was not a Parameter, String, or SqlString." );
145+
if (paramPart == null && stringPart == null && sqlPart == null)
146+
{
147+
throw new ArgumentException("Part was not a Parameter, String, or SqlString.");
148+
}
149+
150+
return this;
146151
}
147152

148153
/// <summary>

0 commit comments

Comments
 (0)