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

SNOW-998036: Add check if command text is set before executing #861

Merged
merged 3 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ public void TestCommandWithConnectionAndCommandText()
Assert.AreEqual(commandText, command.CommandText);
}

[Test]
public void TestCommandExecuteThrowsExceptionWhenCommandTextIsNotSet()
sfc-gh-knozderko marked this conversation as resolved.
Show resolved Hide resolved
{
// Arrange
SnowflakeDbConnection conn = new SnowflakeDbConnection();
sfc-gh-knozderko marked this conversation as resolved.
Show resolved Hide resolved

// Act
var thrown = Assert.Throws<Exception>(() => command.ExecuteScalar());

// Assert
Assert.AreEqual(thrown.Message, "Unable to execute command due to command text not being set");
}

[Test]
public void TestCommandPrepareThrowsNotImplemented()
{
Expand Down
12 changes: 12 additions & 0 deletions Snowflake.Data/Client/SnowflakeDbCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,26 @@ private void SetStatement()

private SFBaseResultSet ExecuteInternal(bool describeOnly = false)
{
CheckIfCommandTextIsSet();
SetStatement();
return sfStatement.Execute(CommandTimeout, CommandText, convertToBindList(parameterCollection.parameterList), describeOnly);
}

private Task<SFBaseResultSet> ExecuteInternalAsync(CancellationToken cancellationToken, bool describeOnly = false)
{
CheckIfCommandTextIsSet();
SetStatement();
return sfStatement.ExecuteAsync(CommandTimeout, CommandText, convertToBindList(parameterCollection.parameterList), describeOnly, cancellationToken);
}

private void CheckIfCommandTextIsSet()
{
if (string.IsNullOrEmpty(CommandText))
{
var errorMessage = "Unable to execute command due to command text not being set";
logger.Error(errorMessage);
throw new Exception(errorMessage);
}
}
}
}
Loading