Skip to content

Commit 0c19e2d

Browse files
SNOW-834812 added QueryTag in SnowflakeDbCommand (#933)
### Description Please explain the changes you made here. ### Checklist - [x] Code compiles correctly - [x] Code is formatted according to [Coding Conventions](../blob/master/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
1 parent 15f4c55 commit 0c19e2d

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ The following table lists all valid connection properties:
167167
| 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. |
168168
| CLIENT_CONFIG_FILE | No | The location of the client configuration json file. In this file you can configure easy logging feature. |
169169
| 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. |
170-
| 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. |
170+
| 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. |
171171

172172
<br />
173173

Snowflake.Data.Tests/IntegrationTests/SFDbCommandIT.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,5 +1624,25 @@ public void TestGetResultsOfUnknownQueryIdWithConfiguredRetry()
16241624
conn.Close();
16251625
}
16261626
}
1627+
1628+
[Test]
1629+
public void TestSetQueryTagOverridesConnectionString()
1630+
{
1631+
using (var conn = new SnowflakeDbConnection())
1632+
{
1633+
string expectedQueryTag = "Test QUERY_TAG 12345";
1634+
string connectQueryTag = "Test 123";
1635+
conn.ConnectionString = ConnectionString + $";query_tag={connectQueryTag}";
1636+
1637+
conn.Open();
1638+
var command = conn.CreateCommand();
1639+
((SnowflakeDbCommand)command).QueryTag = expectedQueryTag;
1640+
// This query itself will be part of the history and will have the query tag
1641+
command.CommandText = "SELECT QUERY_TAG FROM table(information_schema.query_history_by_session())";
1642+
var queryTag = command.ExecuteScalar();
1643+
1644+
Assert.AreEqual(expectedQueryTag, queryTag);
1645+
}
1646+
}
16271647
}
16281648
}

Snowflake.Data/Client/SnowflakeDbCommand.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public override int CommandTimeout
5454
get; set;
5555
}
5656

57+
public string QueryTag
58+
{
59+
get; set;
60+
}
61+
5762
public override CommandType CommandType
5863
{
5964
get
@@ -132,7 +137,7 @@ protected override DbConnection DbConnection
132137
connection = sfc;
133138
if (sfc.SfSession != null)
134139
{
135-
sfStatement = new SFStatement(sfc.SfSession);
140+
sfStatement = new SFStatement(sfc.SfSession, QueryTag);
136141
}
137142
}
138143
}
@@ -143,7 +148,7 @@ protected override DbParameterCollection DbParameterCollection
143148
{
144149
return this.parameterCollection;
145150
}
146-
}
151+
}
147152

148153
protected override DbTransaction DbTransaction
149154
{
@@ -373,15 +378,15 @@ private static Dictionary<string, BindingDTO> convertToBindList(List<SnowflakeDb
373378

374379
if (parameter.Value.GetType().IsArray &&
375380
// byte array and char array will not be treated as array binding
376-
parameter.Value.GetType().GetElementType() != typeof(char) &&
381+
parameter.Value.GetType().GetElementType() != typeof(char) &&
377382
parameter.Value.GetType().GetElementType() != typeof(byte))
378383
{
379384
List<object> vals = new List<object>();
380385
foreach(object val in (Array)parameter.Value)
381386
{
382-
// if the user is using interface, SFDataType will be None and there will
387+
// if the user is using interface, SFDataType will be None and there will
383388
// a conversion from DbType to SFDataType
384-
// if the user is using concrete class, they should specify SFDataType.
389+
// if the user is using concrete class, they should specify SFDataType.
385390
if (parameter.SFDataType == SFDataType.None)
386391
{
387392
Tuple<string, string> typeAndVal = SFDataConverter
@@ -392,7 +397,7 @@ private static Dictionary<string, BindingDTO> convertToBindList(List<SnowflakeDb
392397
}
393398
else
394399
{
395-
bindingType = parameter.SFDataType.ToString();
400+
bindingType = parameter.SFDataType.ToString();
396401
vals.Add(SFDataConverter.csharpValToSfVal(parameter.SFDataType, val));
397402
}
398403
}
@@ -420,21 +425,21 @@ private static Dictionary<string, BindingDTO> convertToBindList(List<SnowflakeDb
420425
}
421426
}
422427

423-
private void SetStatement()
428+
private void SetStatement()
424429
{
425430
if (connection == null)
426431
{
427432
throw new SnowflakeDbException(SFError.EXECUTE_COMMAND_ON_CLOSED_CONNECTION);
428433
}
429-
434+
430435
var session = (connection as SnowflakeDbConnection).SfSession;
431436

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

437-
this.sfStatement = new SFStatement(session);
442+
this.sfStatement = new SFStatement(session, QueryTag);
438443
}
439444

440445
private SFBaseResultSet ExecuteInternal(bool describeOnly = false, bool asyncExec = false)

Snowflake.Data/Core/SFStatement.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,14 @@ internal SFStatement(SFSession session)
151151
_restRequester = session.restRequester;
152152
_queryTag = session._queryTag;
153153
}
154-
154+
155+
internal SFStatement(SFSession session, string queryTag)
156+
{
157+
SfSession = session;
158+
_restRequester = session.restRequester;
159+
_queryTag = queryTag ?? session._queryTag;
160+
}
161+
155162
internal string GetBindStage() => _bindStage;
156163

157164
private void AssignQueryRequestId()

0 commit comments

Comments
 (0)