Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim SQL statements #962

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/MySqlConnector/Core/StatementPreparer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Data;
using MySqlConnector.Protocol;
Expand Down Expand Up @@ -68,6 +69,20 @@ public ParameterSqlParser(StatementPreparer preparer, ByteBufferWriter writer)

public bool IsComplete { get; private set; }

protected override void OnStatementBegin(int index)
{
// trim any text (whitespace, comments) before the beginning of the first statement
if (m_lastIndex == 0)
m_lastIndex = index;
}

protected override void OnStatementEnd(int index)
{
var pastStatementEnd = Math.Min(index + 1, Preparer.m_commandText.Length);
m_writer.Write(Preparer.m_commandText, m_lastIndex, pastStatementEnd - m_lastIndex);
m_lastIndex = pastStatementEnd;
}

protected override void OnNamedParameter(int index, int length)
{
var parameterIndex = Preparer.GetParameterIndex(Preparer.m_commandText.Substring(index, length));
Expand All @@ -91,12 +106,13 @@ private void DoAppendParameter(int parameterIndex, int textIndex, int textLength

protected override void OnParsed(FinalParseStates states)
{
m_writer.Write(Preparer.m_commandText, m_lastIndex, Preparer.m_commandText.Length - m_lastIndex);
IsComplete = (states & FinalParseStates.Complete) == FinalParseStates.Complete;
if (!IsComplete && Preparer.m_commandText.Length > m_lastIndex)
m_writer.Write(Preparer.m_commandText, m_lastIndex, Preparer.m_commandText.Length - m_lastIndex);
if ((states & FinalParseStates.NeedsNewline) == FinalParseStates.NeedsNewline)
m_writer.Write((byte) '\n');
if ((states & FinalParseStates.NeedsSemicolon) == FinalParseStates.NeedsSemicolon)
m_writer.Write((byte) ';');
IsComplete = (states & FinalParseStates.Complete) == FinalParseStates.Complete;
}

readonly ByteBufferWriter m_writer;
Expand Down
12 changes: 12 additions & 0 deletions tests/MySqlConnector.Tests/StatementPreparerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ public void GuidFormat(object options, string replacedValue)
[InlineData("SELECT * FROM `SELECT`", "SELECT * FROM `SELECT`;", true)]
[InlineData("SELECT * FROM test WHERE id = ?;", "SELECT * FROM test WHERE id = 0;", true)]
[InlineData("SELECT * FROM test WHERE id = ?", "SELECT * FROM test WHERE id = 0;", true)]
[InlineData(";SELECT 1", "SELECT 1;", true)]
[InlineData("; SELECT 1;", "SELECT 1;", true)]
[InlineData(" ; SELECT 1 ; ", "SELECT 1 ;", true)]
[InlineData(";;;SELECT 1;;;", "SELECT 1;", true)]
[InlineData("-- test\n; SELECT 1;", "SELECT 1;", true)]
[InlineData("SELECT 1; -- test\n", "SELECT 1;", true)]
public void CompleteStatements(string sql, string expectedSql, bool expectedComplete)
{
var parameters = new MySqlParameterCollection { new() { Value = 0 } };
Expand All @@ -224,6 +230,7 @@ public void CompleteStatements(string sql, string expectedSql, bool expectedComp

[Theory]
[InlineData("SELECT 1", new[] { "SELECT 1" }, "")]
[InlineData("SELECT 1 ", new[] { "SELECT 1 " }, "")]
[InlineData("SELECT 1;", new[] { "SELECT 1" }, "")]
[InlineData("\r\n-- leading comment\r\nSELECT 1;\r\n\r\n-- trailing comment", new[] { "SELECT 1" }, "")]
[InlineData("SELECT 1; SELECT 2;", new[] { "SELECT 1", "SELECT 2" }, ";")]
Expand All @@ -234,6 +241,11 @@ public void CompleteStatements(string sql, string expectedSql, bool expectedComp
[InlineData("SELECT @one, @two; SELECT @zero, @three", new[] { "SELECT ?, ?", "SELECT ?, ?" }, "@one,@two;@zero,@three")]
[InlineData("SELECT ?, ?; SELECT ?, ?", new[] { "SELECT ?, ?", "SELECT ?, ?" }, "0,1;2,3")]
[InlineData("SELECT '@one' FROM `@three` WHERE `@zero` = @two;", new[] { "SELECT '@one' FROM `@three` WHERE `@zero` = ?" }, "@two")]
[InlineData(";SELECT 1", new[] { "SELECT 1" }, "")]
[InlineData(" ; SELECT 1 ; ", new[] { "SELECT 1 " }, "")]
[InlineData(";;;SELECT 1;;;", new[] { "SELECT 1" }, "")]
[InlineData("-- test\n; SELECT 1;", new[] { "SELECT 1" }, "")]
[InlineData("SELECT 1; -- test\n", new[] { "SELECT 1" }, "")]
public void SplitStatement(string sql, string[] expectedStatements, string expectedStatementParametersString)
{
// verify InlineData is in the expected format
Expand Down
17 changes: 17 additions & 0 deletions tests/SideBySide/QueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,23 @@ public void SumShorts(bool prepareCommand)
Assert.Equal(65536L, reader.GetInt64(0));
}

[Theory]
[InlineData(false)]
#if !BASELINE
[InlineData(true)]
#endif
public void EmptyStatements(bool prepareCommand)
{
using var connection = new MySqlConnection(AppConfig.ConnectionString);
connection.Open();

using var cmd = connection.CreateCommand();
cmd.CommandText = " ; select 1 ; ; ";
if (prepareCommand)
cmd.Prepare();
Assert.Equal(1L, cmd.ExecuteScalar());
}

[Theory]
[InlineData(false)]
[InlineData(true)]
Expand Down