Skip to content

Commit

Permalink
SNOW-998036: Add check if command text is set before executing (#861)
Browse files Browse the repository at this point in the history
### Description
Add check if command text is set before executing

### Checklist
- [x] Code compiles correctly
- [x] Code is formatted according to [Coding
Conventions](../CodingConventions.md)
- [x] Created tests which fail without the change (if possible)
- [x] All tests passing (`dotnet test`)
- [x] Extended the README / documentation, if necessary
- [x] Provide JIRA issue id (if possible) or GitHub issue id in PR name
  • Loading branch information
sfc-gh-ext-simba-lf authored Feb 14, 2024
1 parent a1f1d6f commit 6be56bb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Snowflake.Data.Tests.UnitTests
using NUnit.Framework;
using Snowflake.Data.Client;
using System;
using System.Threading;
using System.Threading.Tasks;

[TestFixture]
class SFDbCommandTest
Expand Down Expand Up @@ -34,6 +36,29 @@ public void TestCommandWithConnectionAndCommandText()
Assert.AreEqual(commandText, command.CommandText);
}

[Test]
public void TestCommandExecuteThrowsExceptionWhenCommandTextIsNotSet()
{
// 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 TestCommandExecuteAsyncThrowsExceptionWhenCommandTextIsNotSet()
{
// Arrange
Task<object> commandTask = command.ExecuteScalarAsync(CancellationToken.None);

// Act
var thrown = Assert.Throws<AggregateException>(() => commandTask.Wait());

// Assert
Assert.AreEqual(thrown.InnerException.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 6be56bb

Please sign in to comment.