Skip to content

Commit

Permalink
SNOW-998036: Add check if command text is set before executing
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-lf committed Feb 7, 2024
1 parent 7d478f0 commit 2ee064b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
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()
{
// Arrange
SnowflakeDbConnection conn = new SnowflakeDbConnection();

// 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);
}
}
}
}

0 comments on commit 2ee064b

Please sign in to comment.