From 6be56bb820b045945111fa2a3153bb89515b576a Mon Sep 17 00:00:00 2001 From: sfc-gh-ext-simba-lf <115584722+sfc-gh-ext-simba-lf@users.noreply.github.com> Date: Wed, 14 Feb 2024 09:03:54 -0800 Subject: [PATCH] SNOW-998036: Add check if command text is set before executing (#861) ### 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 --- .../UnitTests/SFDbCommandTest.cs | 25 +++++++++++++++++++ Snowflake.Data/Client/SnowflakeDbCommand.cs | 12 +++++++++ 2 files changed, 37 insertions(+) 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); + } + } } }