-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlDatabaseComparisonResultsTextGenerator.cs
More file actions
278 lines (216 loc) · 7.95 KB
/
SqlDatabaseComparisonResultsTextGenerator.cs
File metadata and controls
278 lines (216 loc) · 7.95 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//-----------------------------------------------------------------------
// <copyright file="SqlDatabaseComparisonResultsTextGenerator.cs" company="P.O.S Informatique">
// Copyright (c) P.O.S Informatique. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace PosInformatique.Testing.Databases
{
internal sealed class SqlDatabaseComparisonResultsTextGenerator : ISqlObjectDifferencesVisitor, IDisposable
{
private readonly StringWriter writer;
private int tab;
private bool writeTab;
private SqlDatabaseComparisonResultsTextGenerator()
{
this.writer = new StringWriter();
}
public static string Generate(SqlDatabaseComparisonResults results)
{
using var generator = new SqlDatabaseComparisonResultsTextGenerator();
generator.Generate(results.Tables, "Tables");
generator.Generate(results.StoredProcedures, "Stored procedures");
generator.Generate(results.UserTypes, "User types");
generator.Generate(results.Views, "Views");
return generator.writer.ToString();
}
public static string Generate<TSqlObject>(SqlObjectDifferences<TSqlObject> differences)
where TSqlObject : SqlObject
{
using var generator = new SqlDatabaseComparisonResultsTextGenerator();
generator.GenerateCore(differences);
return generator.writer.ToString();
}
public static string Generate(SqlObjectPropertyDifference difference)
{
using var generator = new SqlDatabaseComparisonResultsTextGenerator();
generator.WriteProperty(difference);
return generator.writer.ToString();
}
public void Dispose()
{
this.writer.Dispose();
}
public void Visit<TSqlObject>(SqlObjectDifferences<TSqlObject> differences)
where TSqlObject : SqlObject
{
if (differences.Source is not null && differences.Target is not null)
{
this.WriteProperties(differences.Properties);
}
}
public void Visit(SqlColumnDifferences differences)
{
this.Visit<SqlColumn>(differences);
this.Generate(differences.DefaultConstraint, "Default constraint");
}
public void Visit(SqlForeignKeyDifferences differences)
{
this.WriteProperties(differences.Properties);
this.Generate(differences.Columns, "Columns");
}
public void Visit(SqlIndexDifferences differences)
{
this.WriteProperties(differences.Properties);
this.Generate(differences.Columns, "Columns");
this.Generate(differences.IncludedColumns, "Included columns");
}
public void Visit(SqlPrimaryKeyDifferences differences)
{
this.WriteProperties(differences.Properties);
this.Generate(differences.Columns, "Columns");
}
public void Visit(SqlTableDifferences differences)
{
if (differences.Source is not null && differences.Target is not null)
{
this.Generate(differences.CheckConstraints, "Check constraints");
this.Generate(differences.Columns, "Columns");
this.Generate(differences.ForeignKeys, "Foreign keys");
this.Generate(differences.Indexes, "Indexes");
this.Generate(differences.PrimaryKey, "Primary key");
this.Generate(differences.Triggers, "Triggers");
this.Generate(differences.UniqueConstraints, "Unique constraints");
this.WriteProperties(differences.Properties);
}
}
public void Visit(SqlUniqueConstraintDifferences differences)
{
this.WriteProperties(differences.Properties);
this.Generate(differences.Columns, "Columns");
}
private void Generate<TSqlObject>(SqlObjectDifferences<TSqlObject>? difference, string typeName)
where TSqlObject : SqlObject
{
if (difference is null)
{
return;
}
this.Generate([difference], typeName);
}
private void Generate<TSqlObject>(IEnumerable<SqlObjectDifferences<TSqlObject>> differences, string typeName)
where TSqlObject : SqlObject
{
if (!differences.Any())
{
return;
}
this.WriteLine($"------ {typeName} ------");
foreach (var difference in differences)
{
this.Write("- ");
this.GenerateCore(difference);
}
}
private void GenerateCore<TSqlObject>(SqlObjectDifferences<TSqlObject> differences)
where TSqlObject : SqlObject
{
this.WriteObjectName(differences);
this.Indent();
differences.Accept(this);
this.Unindent();
}
private void WriteProperties(IEnumerable<SqlObjectPropertyDifference> properties)
{
foreach (var property in properties)
{
this.WriteProperty(property);
}
}
private void WriteProperty(SqlObjectPropertyDifference property)
{
this.WriteLine($"* {property.Name}:");
this.Indent();
this.Indent();
this.WritePropertyValue("Source", property.Source);
this.WritePropertyValue("Target", property.Target);
this.Unindent();
this.Unindent();
}
private void WritePropertyValue(string side, object? value)
{
this.Write($"{side}: ");
if (value is string stringValue)
{
if (stringValue.Contains(Environment.NewLine))
{
this.WriteLine();
this.Indent();
this.WriteLine(stringValue);
this.Unindent();
return;
}
}
this.WriteLine(StringHelper.ToStringNull(value, "<No value>"));
}
private void WriteObjectName<TSqlObject>(SqlObjectDifferences<TSqlObject> difference)
where TSqlObject : SqlObject
{
if (difference.Source is null)
{
this.WriteLine($"{difference.Target} (Missing in the source)");
}
else if (difference.Target is null)
{
this.WriteLine($"{difference.Source} (Missing in the target)");
}
else
{
this.WriteLine($"{difference.Source}");
}
}
private void Write(string value)
{
this.WriteIndent();
this.writer.Write(value);
}
private void WriteLine()
{
this.WriteLine(string.Empty);
}
private void WriteLine(string value)
{
var lines = value.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
if (lines.Length > 1)
{
foreach (var line in lines)
{
this.WriteLine(line);
}
return;
}
this.WriteIndent();
value = value.Replace("\t", " ");
this.writer.WriteLine(value);
this.writeTab = true;
}
private void Indent()
{
this.tab += 2;
}
private void Unindent()
{
this.tab -= 2;
}
private void WriteIndent()
{
if (this.writeTab)
{
this.writeTab = false;
for (var i = 0; i < this.tab; i++)
{
this.writer.Write(' ');
}
}
}
}
}