Skip to content

Commit 92cc9fd

Browse files
Merge branch 'master' into dbbatch
2 parents c1fc0ef + 492c0bf commit 92cc9fd

File tree

2 files changed

+66
-32
lines changed

2 files changed

+66
-32
lines changed

src/NHibernate.Test/EngineTest/ParameterParserFixture.cs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using NHibernate.Engine.Query;
2+
using NSubstitute;
23
using NUnit.Framework;
34

45
namespace NHibernate.Test.EngineTest
@@ -19,8 +20,8 @@ FROM tablea
1920

2021
var recognizer = new ParamLocationRecognizer();
2122
ParameterParser.Parse(query, recognizer);
22-
ParamLocationRecognizer.NamedParameterDescription p;
23-
Assert.DoesNotThrow(() => p = recognizer.NamedParameterDescriptionMap["name"]);
23+
Assert.That(recognizer.NamedParameterDescriptionMap, Has.Count.EqualTo(1));
24+
Assert.That(recognizer.NamedParameterDescriptionMap, Contains.Key("name"));
2425
}
2526

2627
[Test]
@@ -30,14 +31,14 @@ public void CanFindParameterAfterInlineComment()
3031
@"
3132
SELECT id
3233
FROM tablea
33-
-- Comment with ' number 1
34+
-- Comment with ' :number 1
3435
WHERE Name = :name
3536
ORDER BY Name";
3637

3738
var recognizer = new ParamLocationRecognizer();
3839
ParameterParser.Parse(query, recognizer);
39-
ParamLocationRecognizer.NamedParameterDescription p;
40-
Assert.DoesNotThrow(() => p = recognizer.NamedParameterDescriptionMap["name"]);
40+
Assert.That(recognizer.NamedParameterDescriptionMap, Has.Count.EqualTo(1));
41+
Assert.That(recognizer.NamedParameterDescriptionMap, Contains.Key("name"));
4142
}
4243

4344
[Test]
@@ -54,9 +55,41 @@ FROM tablea
5455

5556
var recognizer = new ParamLocationRecognizer();
5657
ParameterParser.Parse(query, recognizer);
57-
ParamLocationRecognizer.NamedParameterDescription p;
58-
Assert.DoesNotThrow(() => p = recognizer.NamedParameterDescriptionMap["name"]);
59-
Assert.DoesNotThrow(() => p = recognizer.NamedParameterDescriptionMap["pizza"]);
58+
Assert.That(recognizer.NamedParameterDescriptionMap, Has.Count.EqualTo(2));
59+
Assert.That(recognizer.NamedParameterDescriptionMap, Contains.Key("name"));
60+
Assert.That(recognizer.NamedParameterDescriptionMap, Contains.Key("pizza"));
61+
}
62+
63+
[Test]
64+
public void IgnoresCommentsWithinQuotes()
65+
{
66+
string query =
67+
@"
68+
SELECT id
69+
FROM tablea WHERE Name = '
70+
-- :comment1
71+
' OR Name = '
72+
/* :comment2 */
73+
'
74+
-- :comment3
75+
/* :comment4 */
76+
ORDER BY Name + :pizza -- :comment5";
77+
78+
var recognizer = Substitute.For<ParameterParser.IRecognizer>();
79+
ParameterParser.Parse(query, recognizer);
80+
81+
//Only one parameter in the query
82+
recognizer.ReceivedWithAnyArgs(1).NamedParameter(default, default);
83+
recognizer.Received(1).NamedParameter("pizza", Arg.Any<int>());
84+
85+
//comment1 and comment2 are not really comments and therefore not parsed as blocks
86+
recognizer.DidNotReceive().Other(Arg.Is<string>(x => x.Contains("comment1")));
87+
recognizer.DidNotReceive().Other(Arg.Is<string>(x => x.Contains("comment2")));
88+
89+
//comment 3-5 are actual comments and therefore parsed as blocks
90+
recognizer.Received(1).Other(Arg.Is<string>(x => x.StartsWith("-- :comment3")));
91+
recognizer.Received(1).Other("/* :comment4 */");
92+
recognizer.Received(1).Other(Arg.Is<string>(x => x.StartsWith("-- :comment5")));
6093
}
6194
}
6295
}

src/NHibernate/Engine/Query/ParameterParser.cs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,47 +48,48 @@ public static void Parse(string sqlString, IRecognizer recognizer)
4848

4949
int stringLength = sqlString.Length;
5050
bool inQuote = false;
51-
bool afterNewLine = false;
5251
for (int indx = 0; indx < stringLength; indx++)
5352
{
5453
int currentNewLineLength;
5554

56-
// check comments
57-
if (indx + 1 < stringLength && sqlString.Substring(indx,2) == "/*")
55+
// check comments, unless in quote or at end of string
56+
if (!inQuote && indx + 1 < stringLength)
5857
{
59-
var closeCommentIdx = sqlString.IndexOf("*/", indx + 2, StringComparison.Ordinal);
60-
recognizer.Other(sqlString.Substring(indx, (closeCommentIdx- indx)+2));
61-
indx = closeCommentIdx + 1;
62-
continue;
63-
}
64-
65-
if (afterNewLine && (indx + 1 < stringLength) && sqlString.Substring(indx, 2) == "--")
66-
{
67-
var closeCommentIdx = sqlString.IndexOfAnyNewLine(indx + 2, out currentNewLineLength);
68-
69-
string comment;
70-
if (closeCommentIdx == -1)
58+
var candidateOpenCommentToken = sqlString.Substring(indx, 2);
59+
if (candidateOpenCommentToken == "/*")
7160
{
72-
closeCommentIdx = sqlString.Length;
73-
comment = sqlString.Substring(indx);
61+
var closeCommentIdx = sqlString.IndexOf("*/", indx + 2, StringComparison.Ordinal);
62+
recognizer.Other(sqlString.Substring(indx, (closeCommentIdx - indx) + 2));
63+
indx = closeCommentIdx + 1;
64+
continue;
7465
}
75-
else
66+
67+
if (candidateOpenCommentToken == "--")
7668
{
77-
comment = sqlString.Substring(indx, closeCommentIdx - indx + currentNewLineLength);
69+
var closeCommentIdx = sqlString.IndexOfAnyNewLine(indx + 2, out currentNewLineLength);
70+
71+
string comment;
72+
if (closeCommentIdx == -1)
73+
{
74+
closeCommentIdx = sqlString.Length;
75+
comment = sqlString.Substring(indx);
76+
}
77+
else
78+
{
79+
comment = sqlString.Substring(indx, closeCommentIdx - indx + currentNewLineLength);
80+
}
81+
recognizer.Other(comment);
82+
indx = closeCommentIdx + currentNewLineLength - 1;
83+
continue;
7884
}
79-
recognizer.Other(comment);
80-
indx = closeCommentIdx + currentNewLineLength - 1;
81-
continue;
8285
}
8386

8487
if (sqlString.IsAnyNewLine(indx, out currentNewLineLength))
8588
{
86-
afterNewLine = true;
8789
indx += currentNewLineLength - 1;
8890
recognizer.Other(Environment.NewLine);
8991
continue;
9092
}
91-
afterNewLine = false;
9293

9394
char c = sqlString[indx];
9495
if (inQuote)

0 commit comments

Comments
 (0)