forked from fixer-m/snowflake-db-net-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnowflakeUtils.cs
104 lines (91 loc) · 3.93 KB
/
SnowflakeUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Collections.Generic;
using System.Linq;
using Snowflake.Client.Json;
using Snowflake.Client.Model;
namespace Snowflake.Client.Helpers
{
internal static class SnowflakeUtils
{
// Based on https://github.com/snowflakedb/snowflake-connector-net/blob/master/Snowflake.Data/Core/ResultSetUtil.cs
internal static long GetAffectedRowsCount(QueryExecResponse response)
{
var statementTypeId = response.Data.StatementTypeId;
if (!Enum.IsDefined(typeof(SnowflakeStatementType), statementTypeId))
return 0;
long updateCount = 0;
var statementType = (SnowflakeStatementType)statementTypeId;
switch (statementType)
{
case SnowflakeStatementType.INSERT:
case SnowflakeStatementType.UPDATE:
case SnowflakeStatementType.DELETE:
case SnowflakeStatementType.MERGE:
case SnowflakeStatementType.MULTI_INSERT:
updateCount = response.Data.RowSet[0].Sum(cell => long.Parse(cell));
break;
case SnowflakeStatementType.COPY:
var rowsLoadedColumn = response.Data.RowType.FirstOrDefault(c => c.Name == "rows_loaded");
if (rowsLoadedColumn != null)
{
var rowsLoadedColumnIndex = response.Data.RowType.IndexOf(rowsLoadedColumn);
updateCount = long.Parse(response.Data.RowSet[0][rowsLoadedColumnIndex]);
}
break;
case SnowflakeStatementType.COPY_UNLOAD:
var rowsUnloadedColumn = response.Data.RowType.FirstOrDefault(c => c.Name == "rows_unloaded");
if (rowsUnloadedColumn != null)
{
var rowsUnloadedColumnIndex = response.Data.RowType.IndexOf(rowsUnloadedColumn);
updateCount = long.Parse(response.Data.RowSet[0][rowsUnloadedColumnIndex]);
}
break;
case SnowflakeStatementType.SELECT:
updateCount = -1;
break;
default:
updateCount = 0;
break;
}
return updateCount;
}
// Based on: https://docs.snowflake.com/en/user-guide/admin-account-identifier.html#locator-formats-by-cloud-platform-and-region
internal static string GetCloudTagByRegion(string region)
{
if (string.IsNullOrEmpty(region))
return "";
// User can pass "us-east-2.aws"
if (region.Contains("."))
return "";
var regionTags = new Dictionary<string, string>
{
{ "us-west-2", "" }, // "default"
{ "us-east-2", "aws" },
{ "us-east-1", "" },
{ "us-east-1-gov", "aws" },
{ "ca-central-1", "aws" },
{ "eu-west-1", "" },
{ "eu-west-2", "aws" },
{ "eu-central-1", "" },
{ "eu-north-1", "aws" },
{ "ap-northeast-1", "aws" },
{ "ap-south-1", "aws" },
{ "ap-southeast-1", "" },
{ "ap-southeast-2", "" },
{ "us-central1", "gcp" },
{ "europe-west2", "gcp" },
{ "europe-west4", "gcp" },
{ "west-us-2", "azure" },
{ "east-us-2", "azure" },
{ "us-gov-virginia", "azure" },
{ "canada-central", "azure" },
{ "west-europe", "azure" },
{ "switzerland-north", "azure" },
{ "southeast-asia", "azure" },
{ "australia-east", "azure" }
};
regionTags.TryGetValue(region, out var cloudTag);
return cloudTag ?? "";
}
}
}