Skip to content

Commit 5ec3bc9

Browse files
committed
Fix error parsing -- comments in SQL. Fixes #429
1 parent 42f8747 commit 5ec3bc9

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

src/MySqlConnector/Core/SqlParser.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ public int Parse(string sql)
7575
{
7676
if (ch == ' ')
7777
{
78-
beforeCommentState = state;
7978
state = State.EndOfLineComment;
8079
}
8180
else
@@ -121,7 +120,10 @@ public int Parse(string sql)
121120
throw new InvalidOperationException("Unexpected state: {0}".FormatInvariant(state));
122121

123122
if (ch == '-')
123+
{
124+
beforeCommentState = state;
124125
state = State.Hyphen;
126+
}
125127
else if (ch == '/')
126128
state = State.ForwardSlash;
127129
else if (ch == '\'')

tests/MySqlConnector.Tests/MySqlConnector.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
<ItemGroup Condition=" '$(Configuration)' == 'Baseline' ">
3636
<PackageReference Include="MySql.Data" Version="6.9.9" />
37-
<Compile Remove="ConnectionTests.cs;FakeMySqlServer.cs;FakeMySqlServerConnection.cs;LoadBalancerTests.cs;NormalizeTests.cs;TypeMapperTests.cs;Utf8Tests.cs" />
37+
<Compile Remove="ConnectionTests.cs;FakeMySqlServer.cs;FakeMySqlServerConnection.cs;LoadBalancerTests.cs;NormalizeTests.cs;StatementPreparerTests.cs;TypeMapperTests.cs;Utf8Tests.cs" />
3838
</ItemGroup>
3939

4040
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Text;
2+
using MySql.Data.MySqlClient;
3+
using MySqlConnector.Core;
4+
using MySqlConnector.Utilities;
5+
using Xunit;
6+
7+
namespace MySqlConnector.Tests
8+
{
9+
public class StatementPreparerTests
10+
{
11+
[Theory]
12+
[InlineData(GoodSqlText)]
13+
[InlineData(AnotherGoodSqlText)]
14+
[InlineData(AnotherGoodSqlText2)]
15+
[InlineData(BadSqlText)]
16+
public void PrepareQuery(string sql)
17+
{
18+
var parameters = new MySqlParameterCollection();
19+
parameters.AddWithValue("c2", 3);
20+
21+
var parsedRequest1 = Encoding.UTF8.GetString(new StatementPreparer(sql, parameters, StatementPreparerOptions.None).ParseAndBindParameters().Slice(1));
22+
23+
Assert.Matches("column2 = 3", parsedRequest1);
24+
}
25+
26+
private const string BadSqlText = @"SELECT Id
27+
FROM mytable
28+
WHERE column1 = 2 -- mycomment
29+
AND column2 = @c2";
30+
31+
private const string GoodSqlText = @"SELECT Id
32+
FROM mytable
33+
WHERE column1 = 2
34+
AND column2 = @c2";
35+
36+
private const string AnotherGoodSqlText = @"SELECT Id
37+
FROM mytable
38+
WHERE column1 = 2 -- mycomment
39+
AND column2 = @c2";
40+
41+
private const string AnotherGoodSqlText2 = @"SELECT Id
42+
FROM mytable
43+
WHERE column1 = 2 -- mycomment
44+
AND column2 = @c2";
45+
}
46+
}

0 commit comments

Comments
 (0)