-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
103 lines (84 loc) · 3.73 KB
/
Program.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
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using Newtonsoft.Json;
using SmartScript.Generators;
using System;
using System.IO;
using System.Reflection;
using SM = SmartScript.Models;
namespace SmartScript
{
internal class Program
{
#region Private Properties
private static string FilePath
{
get
{
return Directory.GetParent(Directory.GetParent(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)).FullName).FullName +
"\\" + "datasource.json";
}
}
private static string Header
{
get
{
return "*****************************************************************************" + Environment.NewLine +
"*************************SMART SCRIPT GENERATOR *****************************" + Environment.NewLine +
"*****************************************************************************";
}
}
private static string Finish
{
get
{
return
"*************************SMART SCRIPT COMPLETED *****************************";
}
}
#endregion Private Properties
#region Private Methods
private static string GetOutputPath(string folder, string name)
{
return folder + "\\" + name + ".sql";
}
private static void Main(string[] args)
{
Console.WriteLine(Header);
SM.DatabaseServer datasource = JsonConvert.DeserializeObject<SM.DatabaseServer>(System.IO.File.ReadAllText(FilePath));
Server server = new Server(ConnectionManager.GetConnection(datasource));
server.ConnectionContext.AutoDisconnectMode = AutoDisconnectMode.NoAutoDisconnect;
server.ConnectionContext.Connect();
var database = server.Databases[datasource.Database];
foreach (var tableItem in datasource.Tables)
{
CreateTable table = new CreateTable(database, tableItem.Schema, tableItem.Name);
DropTable dropTable = new DropTable(database, tableItem);
table.IncludeTable()
.IncludePrimaryKey()
.IncludeForeignKey()
.IncludeUniqueKey()
.IncludeDefaultConstraints()
.WriteToFile(GetOutputPath(datasource.OutputFolder, "table_" + tableItem.Name));
dropTable.IncludeForeignKey()
.IncludePrimaryKey()
.IncludeUniqueKey()
.IncludeDefaultConstraints()
.IncludeTable()
.WriteToFile(GetOutputPath(datasource.RollbackOutputFolder, "ROLLBACK_table_" + tableItem.Name));
Console.WriteLine(string.Format("[{0}].[{1}] Completed.", tableItem.Schema, tableItem.Name));
}
foreach (var sp in datasource.StoredProcedures)
{
CreateSp createSp = new CreateSp(database, sp.Name, sp.Schema);
createSp.IncludeSp().WriteToFile(GetOutputPath(datasource.OutputFolder, "sp_" + sp));
DropSp dropSp = new DropSp(database.Name, sp.Name, sp.Schema);
dropSp.IncludeSp().WriteToFile(GetOutputPath(datasource.RollbackOutputFolder, "ROLLBACK_sp_" + sp));
}
server.ConnectionContext.Disconnect();
Console.WriteLine(Finish);
Console.ReadLine();
}
#endregion Private Methods
}
}