Skip to content

Commit

Permalink
#5 Asynchronous query abilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Grauenwolf committed Jun 21, 2022
1 parent c6edb36 commit 3828ec7
Show file tree
Hide file tree
Showing 13 changed files with 570 additions and 49 deletions.
122 changes: 122 additions & 0 deletions Snowflake.Data.Tests/SFDbCommandAsynchronous.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using NUnit.Framework;
using System.Data;

namespace Tortuga.Data.Snowflake.Tests;

[TestFixture]
class SFDbCommandAsynchronous : SFBaseTest
{
SnowflakeDbConnection StartSnowflakeConnection()
{
var conn = new SnowflakeDbConnection();
conn.ConnectionString = ConnectionString;

conn.Open();

return conn;
}

[Test]
public void TestLongRunningQuery()
{
string queryId;
using (var conn = StartSnowflakeConnection())
{
using (var cmd = (SnowflakeDbCommand)conn.CreateCommand())
{
cmd.CommandText = "select count(seq4()) from table(generator(timelimit => 15)) v order by 1";
var status = cmd.StartAsynchronousQuery();
Assert.False(status.IsQueryDone);
Assert.False(status.IsQuerySuccessful);
queryId = status.QueryId;
}

Assert.IsNotEmpty(queryId);
}

// start a new connection to make sure works across sessions
using (var conn = StartSnowflakeConnection())
{
SnowflakeDbQueryStatus status;
do
{
status = SnowflakeDbAsynchronousQueryHelper.GetQueryStatus(conn, queryId);
if (status.IsQueryDone)
{
break;
}
else
{
Assert.False(status.IsQuerySuccessful);
}

Thread.Sleep(5000);
} while (true);

// once it finished, it should be successfull
Assert.True(status.IsQuerySuccessful);
}

// start a new connection to make sure works across sessions
using (var conn = StartSnowflakeConnection())
{
using (var cmd = SnowflakeDbAsynchronousQueryHelper.CreateQueryResultsCommand(conn, queryId))
{
using (IDataReader reader = cmd.ExecuteReader())
{
// only one result is returned
Assert.IsTrue(reader.Read());
}
}

conn.Close();
}
}

[Test]
public void TestSimpleCommand()
{
string queryId;

using (var conn = StartSnowflakeConnection())
{
using (var cmd = (SnowflakeDbCommand)conn.CreateCommand())
{
cmd.CommandText = "select 1";

var status = cmd.StartAsynchronousQuery();
// even a fast asynchronous call will not be done initially
Assert.False(status.IsQueryDone);
Assert.False(status.IsQuerySuccessful);
queryId = status.QueryId;

Assert.IsNotEmpty(queryId);
}
}

// start a new connection to make sure works across sessions
using (var conn = StartSnowflakeConnection())
{
SnowflakeDbQueryStatus status;
status = SnowflakeDbAsynchronousQueryHelper.GetQueryStatus(conn, queryId);
// since query is so fast, expect it to be done the first time we check the status
Assert.True(status.IsQueryDone);
Assert.True(status.IsQuerySuccessful);
}

// start a new connection to make sure works across sessions
using (var conn = StartSnowflakeConnection())
{
// because this query is so quick, we do not need to check the status before fetching the result

using (var cmd = SnowflakeDbAsynchronousQueryHelper.CreateQueryResultsCommand(conn, queryId))
{
var val = cmd.ExecuteScalar();

Assert.AreEqual(1L, (long)val);
}

conn.Close();
}
}
}
6 changes: 3 additions & 3 deletions Snowflake.Data.Tests/SFStatementTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void TestSessionRenew()
SFSession sfSession = new SFSession("account=test;user=test;password=test", null, restRequester, SnowflakeDbConfiguration.Default);
sfSession.Open();
SFStatement statement = new SFStatement(sfSession);
SFBaseResultSet resultSet = statement.Execute(0, "select 1", null, false);
SFBaseResultSet resultSet = statement.Execute(0, "select 1", null, false, false);
Assert.AreEqual(true, resultSet.Next());
Assert.AreEqual("1", resultSet.GetString(0));
Assert.AreEqual("new_session_token", sfSession.m_SessionToken);
Expand All @@ -40,7 +40,7 @@ public void TestSessionRenewDuringQueryExec()
SFSession sfSession = new SFSession("account=test;user=test;password=test", null, restRequester, SnowflakeDbConfiguration.Default);
sfSession.Open();
SFStatement statement = new SFStatement(sfSession);
SFBaseResultSet resultSet = statement.Execute(0, "select 1", null, false);
SFBaseResultSet resultSet = statement.Execute(0, "select 1", null, false, false);
Assert.AreEqual(true, resultSet.Next());
Assert.AreEqual("1", resultSet.GetString(0));
}
Expand All @@ -60,7 +60,7 @@ public void TestServiceName()
for (int i = 0; i < 5; i++)
{
var statement = new SFStatement(sfSession);
statement.Execute(0, "SELECT 1", null, false);
statement.Execute(0, "SELECT 1", null, false, false);
expectServiceName += "a";
Assert.AreEqual(expectServiceName, sfSession.ParameterMap[SFSessionParameter.SERVICE_NAME]);
}
Expand Down
6 changes: 6 additions & 0 deletions Snowflake.Data/Core/Messages/QueryRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ class QueryRequest

[JsonProperty(PropertyName = "bindings")]
internal Dictionary<string, BindingDTO>? ParameterBindings { get; set; }

/// <summary>
/// indicates whether query should be asynchronous
/// </summary>
[JsonProperty(PropertyName = "asyncExec")]
internal bool asyncExec { get; set; }
}
Loading

0 comments on commit 3828ec7

Please sign in to comment.