From 2ee064b2dc2c4a2b6c4190ad4efa50012d6474ed Mon Sep 17 00:00:00 2001 From: sfc-gh-ext-simba-lf Date: Wed, 7 Feb 2024 14:04:06 -0800 Subject: [PATCH 1/3] SNOW-998036: Add check if command text is set before executing --- Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs | 13 +++++++++++++ Snowflake.Data/Client/SnowflakeDbCommand.cs | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs b/Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs index e4350cd61..1d7fd9f1e 100644 --- a/Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs +++ b/Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs @@ -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(() => command.ExecuteScalar()); + + // Assert + Assert.AreEqual(thrown.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); + } + } } } From e5d2df068c3a960c4457c4690b1b391ed4f08bf9 Mon Sep 17 00:00:00 2001 From: sfc-gh-ext-simba-lf Date: Mon, 12 Feb 2024 12:49:53 -0800 Subject: [PATCH 2/3] SNOW-998036: Add test for async command execute --- .../UnitTests/SFDbCommandTest.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs b/Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs index 1d7fd9f1e..952e0ab96 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 @@ -47,6 +49,20 @@ public void TestCommandExecuteThrowsExceptionWhenCommandTextIsNotSet() Assert.AreEqual(thrown.Message, "Unable to execute command due to command text not being set"); } + [Test] + public void TestCommandExecuteAsyncThrowsExceptionWhenCommandTextIsNotSet() + { + // Arrange + SnowflakeDbConnection conn = new SnowflakeDbConnection(); + 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() { From 99b11cfb137571c4637d4abe3e38c394af05fead Mon Sep 17 00:00:00 2001 From: sfc-gh-ext-simba-lf Date: Tue, 13 Feb 2024 10:51:24 -0800 Subject: [PATCH 3/3] SNOW-998036: Remove unused variable --- Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs b/Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs index 952e0ab96..714d237d2 100644 --- a/Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs +++ b/Snowflake.Data.Tests/UnitTests/SFDbCommandTest.cs @@ -39,9 +39,6 @@ public void TestCommandWithConnectionAndCommandText() [Test] public void TestCommandExecuteThrowsExceptionWhenCommandTextIsNotSet() { - // Arrange - SnowflakeDbConnection conn = new SnowflakeDbConnection(); - // Act var thrown = Assert.Throws(() => command.ExecuteScalar()); @@ -53,7 +50,6 @@ public void TestCommandExecuteThrowsExceptionWhenCommandTextIsNotSet() public void TestCommandExecuteAsyncThrowsExceptionWhenCommandTextIsNotSet() { // Arrange - SnowflakeDbConnection conn = new SnowflakeDbConnection(); Task commandTask = command.ExecuteScalarAsync(CancellationToken.None); // Act