diff --git a/Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs b/Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs index e4350cd61..714d237d2 100644 --- a/Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs +++ b/Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs @@ -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 @@ -34,6 +36,29 @@ public void TestCommandWithConnectionAndCommandText() Assert.AreEqual(commandText, command.CommandText); } + [Test] + public void TestCommandExecuteThrowsExceptionWhenCommandTextIsNotSet() + { + // Act + var thrown = Assert.Throws(() => command.ExecuteScalar()); + + // Assert + Assert.AreEqual(thrown.Message, "Unable to execute command due to command text not being set"); + } + + [Test] + public void TestCommandExecuteAsyncThrowsExceptionWhenCommandTextIsNotSet() + { + // Arrange + Task commandTask = command.ExecuteScalarAsync(CancellationToken.None); + + // Act + var thrown = Assert.Throws(() => commandTask.Wait()); + + // Assert + Assert.AreEqual(thrown.InnerException.Message, "Unable to execute command due to command text not being set"); + } + [Test] public void TestCommandPrepareThrowsNotImplemented() { diff --git a/Snowflake.Data/Client/SnowflakeDbCommand.cs b/Snowflake.Data/Client/SnowflakeDbCommand.cs index ef44a6832..1abd5e998 100755 --- a/Snowflake.Data/Client/SnowflakeDbCommand.cs +++ b/Snowflake.Data/Client/SnowflakeDbCommand.cs @@ -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 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); + } + } } }