Skip to content

Commit d7f5af2

Browse files
committed
Support left and right functions in hql
1 parent 9a9dc5d commit d7f5af2

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

src/NHibernate.Test/Async/Legacy/SQLFunctionsTest.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,34 @@ public async Task DialectSQLFunctionsAsync()
116116
s.Close();
117117
}
118118

119+
//NH-3893 (GH-1349) left and right functions are broken
120+
[Test]
121+
public async Task LeftAndRightAsync()
122+
{
123+
//left or right functions are supported by most dialects but not registered.
124+
if (!(Dialect is MsSql2000Dialect || Dialect is SQLiteDialect))
125+
Assert.Ignore("left or right function is not supported by dialect");
126+
127+
using (var s = OpenSession())
128+
using (var t = s.BeginTransaction())
129+
{
130+
Simple simple = new Simple();
131+
simple.Name = "Simple Dialect Function Test";
132+
simple.Address = "Simple Address";
133+
simple.Pay = 45.8f;
134+
simple.Count = 2;
135+
await (s.SaveAsync(simple, 10L));
136+
137+
var rset = await (s.CreateQuery("select left('abc', 2), right('abc', 2) from s in class Simple").ListAsync<object[]>());
138+
var row = rset[0];
139+
Assert.AreEqual("ab", row[0], "Left function is broken.");
140+
Assert.AreEqual("bc", row[1], "Right function is broken.");
141+
142+
await (s.DeleteAsync(simple));
143+
await (t.CommitAsync());
144+
}
145+
}
146+
119147
[Test]
120148
public async Task SetPropertiesAsync()
121149
{

src/NHibernate.Test/Legacy/SQLFunctionsTest.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,30 @@ public void DialectSQLFunctions()
105105
s.Close();
106106
}
107107

108+
//NH-3893 (GH-1349) left and right functions are broken
108109
[Test]
109-
[Ignore("NH-3893 is not fixed")]
110110
public void LeftAndRight()
111111
{
112-
// As of NH-3893, left and right functions are broken. Seemed confused with join keyword, and not
113-
// supported on Hibernate side.
112+
//left or right functions are supported by most dialects but not registered.
113+
if (!(Dialect is MsSql2000Dialect || Dialect is SQLiteDialect))
114+
Assert.Ignore("left or right function is not supported by dialect");
115+
114116
using (var s = OpenSession())
115117
using (var t = s.BeginTransaction())
116118
{
119+
Simple simple = new Simple();
120+
simple.Name = "Simple Dialect Function Test";
121+
simple.Address = "Simple Address";
122+
simple.Pay = 45.8f;
123+
simple.Count = 2;
124+
s.Save(simple, 10L);
125+
117126
var rset = s.CreateQuery("select left('abc', 2), right('abc', 2) from s in class Simple").List<object[]>();
118127
var row = rset[0];
119128
Assert.AreEqual("ab", row[0], "Left function is broken.");
120129
Assert.AreEqual("bc", row[1], "Right function is broken.");
130+
131+
s.Delete(simple);
121132
t.Commit();
122133
}
123134
}

src/NHibernate/Hql/Ast/ANTLR/HqlParser.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,18 @@ public void WeakKeywords()
131131
}
132132
}
133133
break;
134+
case LEFT:
135+
case RIGHT:
136+
// Support left and right functions
137+
if (input.LA(2) == OPEN)
138+
{
139+
input.LT(1).Type = IDENT;
140+
if (log.IsDebugEnabled())
141+
{
142+
log.Debug("weakKeywords() : new LT(1) token - {0}", input.LT(1));
143+
}
144+
}
145+
break;
134146
default:
135147
// Case 2: The current token is after FROM and before '.'.
136148
if (t != IDENT && input.LA(-1) == FROM && ((input.LA(2) == DOT) || (input.LA(2) == IDENT) || (input.LA(2) == -1)))

0 commit comments

Comments
 (0)