forked from Viostream/dbup-snowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnowflakeConnectionFactory.cs
48 lines (44 loc) · 1.88 KB
/
SnowflakeConnectionFactory.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
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using System.Linq;
using DbUp.Engine.Output;
using DbUp.Engine.Transactions;
namespace DbUp.Snowflake
{
public class SnowflakeConnectionFactory : IConnectionFactory
{
public string Database { get; }
public string Warehouse { get; }
public string ConnectionString { get; }
public SnowflakeConnectionFactory(string connectionString)
{
var connection = connectionString.Split(';')
.Where(x => !string.IsNullOrWhiteSpace(x))
.Select(x =>
{
var keyValue = x.Split('=');
return new KeyValuePair<string, string>(keyValue[0].ToLowerInvariant(), keyValue[1]);
})
.ToDictionary(x => x.Key, y => y.Value);
if (!connection.ContainsKey("database"))
throw new ArgumentException("Connection string does not have database parameter");
if (!connection.ContainsKey("warehouse"))
throw new ArgumentException("Connection string does not have warehouse parameter");
ConnectionString = connectionString;
Database = connection["database"];
Warehouse = connection["warehouse"];
}
public IDbConnection CreateConnection(IUpgradeLog upgradeLog, DatabaseConnectionManager databaseConnectionManager)
{
var connection = new OdbcConnection(ConnectionString);
connection.Open();
var useWHcommand = new OdbcCommand($"use warehouse {Warehouse}", connection);
useWHcommand.ExecuteNonQuery();
var useDBcommand = new OdbcCommand($"use database {Database}", connection);
useDBcommand.ExecuteScalar();
return connection;
}
}
}