Skip to content

Commit

Permalink
SNOW-834812 added QueryTag in SnowflakeDbCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jsieminska committed Apr 26, 2024
1 parent 14cf8a5 commit 9997b43
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ The following table lists all valid connection properties:
| FILE_TRANSFER_MEMORY_THRESHOLD | No | The maximum number of bytes to store in memory used in order to provide a file encryption. If encrypting/decrypting file size exceeds provided value a temporary file will be created and the work will be continued in the temporary file instead of memory. <br/> If no value provided 1MB will be used as a default value (that is 1048576 bytes). <br/> It is possible to configure any integer value bigger than zero representing maximal number of bytes to reside in memory. |
| CLIENT_CONFIG_FILE | No | The location of the client configuration json file. In this file you can configure easy logging feature. |
| ALLOWUNDERSCORESINHOST | No | Specifies whether to allow underscores in account names. This impacts PrivateLink customers whose account names contain underscores. In this situation, you must override the default value by setting allowUnderscoresInHost to true. |
| QUERY_TAG | No | Optional string that can be used to tag queries and other SQL statements executed within a connection. The tags are displayed in the output of the QUERY_HISTORY , QUERY_HISTORY_BY_* functions. |
| QUERY_TAG | No | Optional string that can be used to tag queries and other SQL statements executed within a connection. The tags are displayed in the output of the QUERY_HISTORY , QUERY_HISTORY_BY_* functions.<br/> To set QUERY_TAG on the statement level you can use SnowflakeDbCommand.QueryTag. |

<br />

Expand Down
20 changes: 20 additions & 0 deletions Snowflake.Data.Tests/IntegrationTests/SFDbCommandIT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1624,5 +1624,25 @@ public void TestGetResultsOfUnknownQueryIdWithConfiguredRetry()
conn.Close();
}
}

[Test]
public void TestSetQueryTagOverridesConnectionString()
{
using (var conn = new SnowflakeDbConnection())
{
string expectedQueryTag = "Test QUERY_TAG 12345";
string connectQueryTag = "Test 123";
conn.ConnectionString = ConnectionString + $";query_tag={connectQueryTag}";

conn.Open();
var command = conn.CreateCommand();
((SnowflakeDbCommand)command).QueryTag = expectedQueryTag;
// This query itself will be part of the history and will have the query tag
command.CommandText = "SELECT QUERY_TAG FROM table(information_schema.query_history_by_session())";
var queryTag = command.ExecuteScalar();

Assert.AreEqual(expectedQueryTag, queryTag);
}
}
}
}
23 changes: 14 additions & 9 deletions Snowflake.Data/Client/SnowflakeDbCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public override int CommandTimeout
get; set;
}

public string QueryTag
{
get; set;
}

public override CommandType CommandType
{
get
Expand Down Expand Up @@ -132,7 +137,7 @@ protected override DbConnection DbConnection
connection = sfc;
if (sfc.SfSession != null)
{
sfStatement = new SFStatement(sfc.SfSession);
sfStatement = new SFStatement(sfc.SfSession, QueryTag);
}
}
}
Expand All @@ -143,7 +148,7 @@ protected override DbParameterCollection DbParameterCollection
{
return this.parameterCollection;
}
}
}

protected override DbTransaction DbTransaction
{
Expand Down Expand Up @@ -373,15 +378,15 @@ private static Dictionary<string, BindingDTO> convertToBindList(List<SnowflakeDb

if (parameter.Value.GetType().IsArray &&
// byte array and char array will not be treated as array binding
parameter.Value.GetType().GetElementType() != typeof(char) &&
parameter.Value.GetType().GetElementType() != typeof(char) &&
parameter.Value.GetType().GetElementType() != typeof(byte))
{
List<object> vals = new List<object>();
foreach(object val in (Array)parameter.Value)
{
// if the user is using interface, SFDataType will be None and there will
// if the user is using interface, SFDataType will be None and there will
// a conversion from DbType to SFDataType
// if the user is using concrete class, they should specify SFDataType.
// if the user is using concrete class, they should specify SFDataType.
if (parameter.SFDataType == SFDataType.None)
{
Tuple<string, string> typeAndVal = SFDataConverter
Expand All @@ -392,7 +397,7 @@ private static Dictionary<string, BindingDTO> convertToBindList(List<SnowflakeDb
}
else
{
bindingType = parameter.SFDataType.ToString();
bindingType = parameter.SFDataType.ToString();
vals.Add(SFDataConverter.csharpValToSfVal(parameter.SFDataType, val));
}
}
Expand Down Expand Up @@ -420,21 +425,21 @@ private static Dictionary<string, BindingDTO> convertToBindList(List<SnowflakeDb
}
}

private void SetStatement()
private void SetStatement()
{
if (connection == null)
{
throw new SnowflakeDbException(SFError.EXECUTE_COMMAND_ON_CLOSED_CONNECTION);
}

var session = (connection as SnowflakeDbConnection).SfSession;

// SetStatement is called when executing a command. If SfSession is null
// the connection has never been opened. Exception might be a bit vague.
if (session == null)
throw new SnowflakeDbException(SFError.EXECUTE_COMMAND_ON_CLOSED_CONNECTION);

this.sfStatement = new SFStatement(session);
this.sfStatement = new SFStatement(session, QueryTag);
}

private SFBaseResultSet ExecuteInternal(bool describeOnly = false, bool asyncExec = false)
Expand Down
9 changes: 8 additions & 1 deletion Snowflake.Data/Core/SFStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,14 @@ internal SFStatement(SFSession session)
_restRequester = session.restRequester;
_queryTag = session._queryTag;
}


internal SFStatement(SFSession session, string queryTag)
{
SfSession = session;
_restRequester = session.restRequester;
_queryTag = queryTag ?? session._queryTag;
}

internal string GetBindStage() => _bindStage;

private void AssignQueryRequestId()
Expand Down

0 comments on commit 9997b43

Please sign in to comment.