forked from trimble-oss/dba-dash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMOBaseClass.cs
112 lines (94 loc) · 3.04 KB
/
SMOBaseClass.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
105
106
107
108
109
110
111
112
using Microsoft.Data.SqlClient;
using Microsoft.SqlServer.Management.Smo;
using System.Collections.Specialized;
using System.IO;
using System.IO.Compression;
using System.Security.Cryptography;
using System.Text;
namespace DBADash
{
public class SMOBaseClass
{
protected readonly SchemaSnapshotDBOptions options;
protected readonly ScriptingOptions ScriptingOptions;
protected string ConnectionString => SourceConnection.ConnectionString;
protected readonly DBADashConnection SourceConnection;
public SMOBaseClass(DBADashConnection source, SchemaSnapshotDBOptions options)
{
SourceConnection = source;
options ??= new SchemaSnapshotDBOptions();
this.options = options;
ScriptingOptions = options.ScriptOptions();
}
public SMOBaseClass(DBADashConnection source)
{
SourceConnection = source;
options = new SchemaSnapshotDBOptions();
ScriptingOptions = options.ScriptOptions();
}
protected string MasterConnectionString
{
get
{
var builder = new SqlConnectionStringBuilder(ConnectionString)
{
InitialCatalog = "master"
};
return builder.ConnectionString;
}
}
public static byte[] ComputeHash(byte[] obj)
{
using (var crypt = SHA256.Create())
{
return crypt.ComputeHash(obj);
}
}
public static string StringCollectionToString(StringCollection sc)
{
StringBuilder sb = new();
foreach (var s in sc)
{
sb.AppendLine(s);
sb.AppendLine("GO");
}
return sb.ToString();
}
public static void CopyTo(Stream src, Stream dest)
{
byte[] bytes = new byte[4096];
int cnt;
while ((cnt = src.Read(bytes, 0, bytes.Length)) != 0)
{
dest.Write(bytes, 0, cnt);
}
}
public static byte[] Zip(string str)
{
var bytes = Encoding.Unicode.GetBytes(str);
using (var msi = new MemoryStream(bytes))
using (var mso = new MemoryStream())
{
using (var gs = new GZipStream(mso, CompressionMode.Compress))
{
//msi.CopyTo(gs);
CopyTo(msi, gs);
}
return mso.ToArray();
}
}
public static string Unzip(byte[] bytes)
{
using (var msi = new MemoryStream(bytes))
using (var mso = new MemoryStream())
{
using (var gs = new GZipStream(msi, CompressionMode.Decompress))
{
//gs.CopyTo(mso);
CopyTo(gs, mso);
}
return Encoding.Unicode.GetString(mso.ToArray());
}
}
}
}