-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.cs
187 lines (167 loc) · 7.58 KB
/
engine.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using SQLArchitect.Business;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static SQLArchitect.ExtensionMethods;
namespace SQLArchitect
{
public static class engine
{
public static string DatabaseName { get; set; }
public static string DatabaseConnectionString { get; set; }
public static string FileName { get; set; }
private static DataSet dsTables;
private static DataSet dsProcedures;
private static DataSet dsScalarFunctions;
private static string _newFilePath;
public static async Task ProcessFiles(string path)
{
_newFilePath = path;
foreach (string file in Directory.GetFiles(path, "*.sql"))
{
using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
{
using (StreamReader reader = new StreamReader(fs, Encoding.UTF8))
{
string text = await reader.ReadToEndAsync();
MatchObjects(text);
}
}
}
string outputFile = Path.Combine(_newFilePath, FileName);
if (File.Exists(outputFile))
{
if (File.Exists($"{outputFile}.sql"))
{
File.Move(outputFile, $"{outputFile}_Copy_{DateTime.Now.Year}_{DateTime.Now.Month}_{DateTime.Now.Day}_{DateTime.Now.Hour}_{DateTime.Now.Minute}_{DateTime.Now.Second}.sql");
}
else
File.Move(outputFile, $"{outputFile}.sql");
}
}
private static void MatchObjects(string fileText)
{
string sOutputText = "";
string sObject = "";
if (fileText.ToUpper().Contains("ALTER TABLE"))
{
if (dsTables == null)
// Read the whole SQL statement
dsTables = DataController.GetDataTables(DatabaseConnectionString);
foreach(DataRow item in dsTables.Tables["DataTables"].Rows)
{
string tableName = item["name"].ToString();
if (fileText.ContainsInRelationTo(ScriptType.table, item["name"].ToString()))
{
sOutputText = fileText;
sObject = $"ALTER TABLE {tableName}";
}
}
}
else if (fileText.ToUpper().Contains("CREATE PROCEDURE"))
{
if (dsProcedures == null)
// Get only the Stored Procedure name
dsProcedures = DataController.GetStoredProcedures(DatabaseConnectionString);
foreach (DataRow item in dsProcedures.Tables["Procedures"].Rows)
{
string sprocName = item["name"].ToString();
if (fileText.ContainsInRelationTo(ScriptType.procedure, sprocName))
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(" IF EXISTS ( SELECT * ");
sb.AppendLine(" FROM sysobjects ");
sb.AppendLine($" WHERE id = object_id(N'[dbo].[{sprocName}]') ");
sb.AppendLine(" and OBJECTPROPERTY(id, N'IsProcedure') = 1 ) ");
sb.AppendLine(" BEGIN ");
sb.AppendLine($" DROP PROCEDURE [dbo].[{sprocName}] ");
sb.AppendLine(" END ");
sOutputText = sb.ToString();
sObject = $"CREATE PROCEDURE {sprocName}";
}
}
}
else if (fileText.ToUpper().Contains("CREATE FUNCTION"))
{
if (dsScalarFunctions == null)
// Get only the Function name
dsScalarFunctions = DataController.GetScalarFunctions(DatabaseConnectionString);
foreach (DataRow item in dsScalarFunctions.Tables["ScalarFunctions"].Rows)
{
string funcName = item["name"].ToString();
if (fileText.ContainsInRelationTo(ScriptType.function, funcName))
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(" IF EXISTS ( SELECT * ");
sb.AppendLine(" FROM sysobjects ");
sb.AppendLine($" WHERE id = object_id(N'[dbo].[{funcName}]') ");
sb.AppendLine(" and OBJECTPROPERTY(id, N'IsScalarFunction') = 1 ) ");
sb.AppendLine(" BEGIN ");
sb.AppendLine($" DROP FUNCTION [dbo].[{funcName}] ");
sb.AppendLine(" END ");
sOutputText = sb.ToString();
sObject = $"CREATE FUNCTION {funcName}";
}
}
}
AppendToFile(sOutputText, sObject);
}
private static void AppendToFile(string textToAppend, string objectName)
{
string path = Path.Combine(_newFilePath, FileName);
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine($"-- Script generated by SQL Architect - {DateTime.Now.ToShortDateString() + Environment.NewLine + Environment.NewLine}");
}
}
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine($"-- {objectName}");
sw.WriteLine(textToAppend);
sw.WriteLine(Environment.NewLine + Environment.NewLine);
}
}
}
public static class ExtensionMethods
{
public enum ScriptType
{
procedure,
function,
table
}
public static bool ContainsInRelationTo(this string value, ScriptType type, string name)
{
bool isMatch = false;
switch(type)
{
case ScriptType.procedure:
if (value.Contains($"CREATE PROCEDURE [dbo].[{name}]")) { return true; }
if (value.Contains($"CREATE PROCEDURE dbo.{name}")) { return true; }
if (value.Contains($"CREATE PROCEDURE {name}")) { return true; }
if (value.Contains($"CREATE PROCEDURE [{name}]")) { return true; }
break;
case ScriptType.function:
if (value.Contains($"CREATE FUNCTION [dbo].[{name}]")) { return true; }
if (value.Contains($"CREATE FUNCTION dbo.{name}")) { return true; }
if (value.Contains($"CREATE FUNCTION {name}")) { return true; }
if (value.Contains($"CREATE FUNCTION [{name}]")) { return true; }
break;
case ScriptType.table:
if (value.Contains($"ALTER TABLE [dbo].[{name}]")) { return true; }
if (value.Contains($"ALTER TABLE dbo.{name}")) { return true; }
if (value.Contains($"ALTER TABLE {name}")) { return true; }
if (value.Contains($"ALTER TABLE [{name}]")) { return true; }
break;
}
return isMatch;
}
}
}