Skip to content

Commit 257047a

Browse files
committed
Improve parsing of multi-character sequences. Fixes #563
1 parent 4ff5391 commit 257047a

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/MySqlConnector/Core/SqlParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ public void Parse(string sql)
133133
if (state != State.Beginning && state != State.Statement)
134134
throw new InvalidOperationException("Unexpected state: {0}".FormatInvariant(state));
135135

136-
if (ch == '-')
136+
if (ch == '-' && index < sql.Length - 2 && sql[index + 1] == '-' && sql[index + 2] == ' ')
137137
{
138138
beforeCommentState = state;
139139
state = State.Hyphen;
140140
}
141-
else if (ch == '/')
141+
else if (ch == '/' && index < sql.Length - 1 && sql[index + 1] == '*')
142142
state = State.ForwardSlash;
143143
else if (ch == '\'')
144144
state = State.SingleQuotedString;

tests/MySqlConnector.Tests/StatementPreparerTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@ public void Bug429(string sql)
2323
Assert.Equal(sql.Replace("@param", "123"), parsedSql);
2424
}
2525

26+
[Theory]
27+
[InlineData("UPDATE table SET a=a-@b;")]
28+
[InlineData("UPDATE table SET a=a-/* subtract b */@b;")]
29+
[InlineData("UPDATE table SET a=a+@b;")]
30+
[InlineData("UPDATE table SET a=a/@b;")]
31+
[InlineData("UPDATE table SET a=a-- \n-@b;")]
32+
[InlineData("UPDATE table SET a = a-@b;")]
33+
[InlineData("UPDATE table SET a = a+@b;")]
34+
[InlineData("UPDATE table SET a = a - @b;")]
35+
[InlineData("UPDATE table SET a=@b-a;")]
36+
[InlineData("UPDATE table SET a=@b+a;")]
37+
[InlineData("UPDATE table SET a = @b-a;")]
38+
[InlineData("UPDATE table SET a = @b - a;")]
39+
public void Bug563(string sql)
40+
{
41+
var parameters = new MySqlParameterCollection();
42+
parameters.AddWithValue("@b", 123);
43+
var parsedSql = GetParsedSql(sql, parameters);
44+
Assert.Equal(sql.Replace("@b", "123"), parsedSql);
45+
}
46+
2647
[Theory]
2748
[InlineData(@"SELECT /* * / @param */ 1;")]
2849
[InlineData("SELECT # @param \n1;")]

0 commit comments

Comments
 (0)