Datei: NDOEnhancer/NDOEnhancer/Enhancer/Generator/NdoTransDiffGenerator.cs

Last Commit (ab290a4)
1 //
2 // Copyright (c) 2002-2022 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using System;
24 using System.IO;
25 using System.Data;
26 using System.Collections.Generic;
27 using System.Xml.Linq;
28 using NDO;
29 using NDO.Mapping;
30 using NDOInterfaces;
31 using System.Reflection;
32 using System.Text;
33 using System.Security.Cryptography;
34
35 namespace NDOEnhancer
36 {
37 ····/// <summary>
38 ····/// Zusammenfassung für SQLDiffGenerator.
39 ····/// </summary>
40 ····internal class NdoTransDiffGenerator
41 ····{
42 ········NDOMapping mappings;
43 ········MessageAdapter messages;
44 ········public void Generate(DataSet dsSchema, DataSet dsBak, string filename, NDOMapping mappings, MessageAdapter messages)
45 ········{
46 ············this.mappings = mappings;
47 ············this.messages = messages;
48
49 ············string diffFileName = filename.Replace(".ndo.sql", ".ndodiff." + mappings.SchemaVersion + ".xml");
50 ············bool isNewDiffFile = !File.Exists(diffFileName);
51 ············XElement rootElement = null;
52 ············if (isNewDiffFile)
53 ············{
54 ················rootElement = new XElement( "NdoSchemaTransitions" );
55 ················rootElement.Add( new XComment( "NDO accumulates all schema changes in this diff file. Note: If you change the mapping schema version, a new diff file will be created. You can change the mapping schema version in the NDO configuration dialog. Don't use the Mapping Tool to change the schema information, because it will be overwritten by the value set in the configuration. For automatic builds set the schema version value in the .ndoproj file." ) );
56 ············}
57 ············else
58 ············{
59 ················rootElement = XElement.Load( diffFileName );
60 ············}
61
62 ············XElement transElement = new XElement( "NdoSchemaTransition" );
63 ············rootElement.Add( transElement );
64
65 ············if (GenerateInternal( dsSchema, dsBak, transElement ))
66 ············{
67 ················Guid g = MakeHash( transElement.ToString() );
68 ················transElement.SetAttributeValue( "id", g );
69 ················rootElement.Save( diffFileName );
70 ············}
71 ········}
72
73 ········public static Guid MakeHash( string input )
74 ········{
75 ············var buf = Encoding.UTF8.GetBytes(input);
76
77 ············using (var sha = SHA256.Create())
78 ············{
79 ················var hash = sha.ComputeHash( buf );
80 ················byte[] arr = new byte[16];
81 ················Array.Copy( hash, arr, 16 );
82 ················return new Guid( arr );
83 ············}
84 ········}
85
86 ········bool GenerateInternal(System.Data.DataSet dsNewSchema, System.Data.DataSet dsOldSchema, XElement transElement)
87 ········{
88 ············bool hasChanges = false;
89 ············foreach(DataTable dt in dsNewSchema.Tables)
90 ············{
91 ················if (!dsOldSchema.Tables.Contains(dt.TableName))
92 ················{
93 ····················transElement.Add( CreateTable( dt ) );
 
 
94 ····················hasChanges = true;
95 ················}
96 ················else
97 ················{
98 ····················DataTable dtOld = dsOldSchema.Tables[dt.TableName];
99 ····················List<DataColumn> addedColumns = new List<DataColumn>();
100 ····················List<DataColumn> removedColumns = new List<DataColumn>();
101 ····················List<DataColumn> changedColumns = new List<DataColumn>();
102 ····················foreach(DataColumn dc in dt.Columns)
103 ····················{
104 ························if (!dtOld.Columns.Contains(dc.ColumnName))
105 ························{
106 ····························addedColumns.Add(dc);
107 ························}
108 ························else
109 ························{
110 ····························DataColumn dcOld = dtOld.Columns[dc.ColumnName];
111 ····························if (dc.DataType != dcOld.DataType || dc.MaxLength != dcOld.MaxLength)
112 ································changedColumns.Add(dc);
113 ························}
114 ····················}
115 ····················foreach(DataColumn dc in dtOld.Columns)
116 ····················{
117 ························if (!dt.Columns.Contains(dc.ColumnName))
118 ························{
119 ····························removedColumns.Add(dc);
120 ························}
121 ····················}
122 ····················if (addedColumns.Count > 0 || removedColumns.Count > 0 || changedColumns.Count > 0)
123 ························hasChanges = true;
124
125 ····················if (!hasChanges)
126 ························continue;
127
128 ····················XElement changeTableElement = ChangeTable(dt, addedColumns, removedColumns, changedColumns);
129 ····················if (changeTableElement.HasElements)
130 ························transElement.Add( changeTableElement );
131
132 ················}
133 ············}
134
135 ············foreach (DataTable dt in dsOldSchema.Tables)
136 ············{
137 ················if (!dsNewSchema.Tables.Contains( dt.TableName ))
138 ················{
139 ····················XElement dropTableElement = new XElement( "DropTable", new XAttribute( "name", dt.TableName ) );
140 ····················transElement.Add( dropTableElement );
141 ····················hasChanges = true;
142 ················}
143 ············}
144
145 ············return hasChanges;
146 ········}
147
148 ········XElement ChangeTable(DataTable dt, List<DataColumn> addedColumns, List<DataColumn> removedColums, List<DataColumn> changedColumns)
149 ········{
150 ············XElement alterTableElement = new XElement( "AlterTable", new XAttribute( "name", dt.TableName ) );
151
152 ············foreach(DataColumn dc in addedColumns)
153 ············{
154 ················XElement addColumnElement = new XElement( "AddColumn", new XAttribute( "name", dc.ColumnName ) );
155 ················CreateColumn(addColumnElement, dc, GetClassForTable(dt.TableName, this.mappings), false);
156 ················alterTableElement.Add( addColumnElement );
157 ············}
158
159 ············foreach(DataColumn dc in removedColums)
160 ············{
161 ················XElement dropColumnElement = new XElement( "DropColumn", new XAttribute( "name", dc.ColumnName ) );
162 ················alterTableElement.Add( dropColumnElement );
163 ············}
164
165 ············foreach(DataColumn dc in changedColumns)
166 ············{
167 ················XElement alterColumnElement = new XElement( "AlterColumn", new XAttribute( "name", dc.ColumnName ) );
168 ················CreateColumn(alterColumnElement, dc, GetClassForTable(dt.TableName, this.mappings), false);
169 ················alterTableElement.Add( alterColumnElement );
170 ············}
171
172 ············if (removedColums.Count > 0)
173 ············{
174 ················alterTableElement.Add( new XComment( "If you need to rename a column use the following syntax:" ) );
175 ················alterTableElement.Add( new XComment( "<RenameColumn oldName=\"...\" newName=\"...\" />" ) );
176 ············}
177
178 ············return alterTableElement;············
 
 
 
 
 
 
 
 
 
 
 
 
 
179 ········}
180
181 ········protected XElement CreateTable(DataTable dt)
182 ········{
183 ············XElement createTableElement = new XElement( "CreateTable", new XAttribute( "name", dt.TableName ) );
184
185 ············Class cl = FindClass(dt.TableName, mappings);
186
187 ············DataColumn[] primaryKeyColumns = dt.PrimaryKey;
188 ············bool hasPrimaryKeyColumns = primaryKeyColumns.Length > 0;
189
190 ············for (int i = 0; i < dt.Columns.Count; i++)
191 ············{
192 ················System.Data.DataColumn dc = dt.Columns[i];
193
194 ················bool isPrimary = false;
195 ················foreach (DataColumn pkc in primaryKeyColumns)
196 ················{
197 ····················if (pkc.ColumnName == dc.ColumnName)
198 ····················{
199 ························isPrimary = true;
200 ························break;
201 ····················}
202 ················}
203
204 ················XElement createColumnElement = new XElement( "CreateColumn", new XAttribute( "name", dc.ColumnName ) );
205 ················CreateColumn(createColumnElement, dc, cl, isPrimary);
206 ················createTableElement.Add( createColumnElement );
207 ············}
208
209 ············return createTableElement;
210 ········}
211
212 ········protected NDO.Mapping.Class FindClass(string tableName, NDOMapping mappings)
213 ········{
214 ············Class result = null;
215 ············foreach(Class cl in mappings.Classes)
216 ············{
217 ················if (cl.TableName == tableName)
218 ················{
219 ····················result = cl;
220 ····················break;
221 ················}
222 ············}
223 ············return result;
224 ········}
225
226 ········protected void CreateColumn(XElement addColumnElement, DataColumn dc, Class cl, bool isPrimary)
227 ········{
228 ············Type t = dc.DataType;
229 ············addColumnElement.Add( new XAttribute( "type", t.FullName + "," + new AssemblyName( t.Assembly.FullName ).Name ) );
230
231 ············if (isPrimary)
232 ················addColumnElement.Add( new XAttribute( "isPrimary", "true" ) );
233
234 ············if (cl != null)
235 ············{
236 ················Field field = FindField(dc.ColumnName, cl);
237
238 ················if (null != field)
239 ················{
240 ····················if (0 != field.Column.Size && !field.Column.IgnoreColumnSizeInDDL)
241 ····················{
242 ························if (field.Column.Size != 0)
243 ····························addColumnElement.Add( new XAttribute( "size", field.Column.Size.ToString() ) );
244 ····················}
245 ····················if (0 != field.Column.Precision && !field.Column.IgnoreColumnSizeInDDL)
246 ····················{
247 ························if (field.Column.Precision != 0)
248 ····························addColumnElement.Add( new XAttribute( "precision", field.Column.Precision.ToString() ) );
249 ····················}
250 ····················addColumnElement.Add( new XAttribute( "allowNull", field.Column.AllowDbNull.ToString() ) );····················
251 ················}
252 ················else if (isPrimary && dc.AutoIncrement)
253 ················{
254 ····················addColumnElement.Add( new XAttribute( "autoIncrement", "true" ) );····················
255 ················}
256 ············}············
257 ········}
258
259
260 ········protected Field FindField (string columnName, Class cl)
261 ········{
262 ············Field result = null;
263 ············foreach (Field field in cl.Fields)
264 ············{
265 ················if (field.Column.Name == columnName)
266 ················{
267 ····················result = field;
268 ····················break;
269 ················}
270 ············}
271 ············return result;
272 ········}
273
274
275 ········protected Class GetClassForTable(string tableName, NDOMapping mapping)
276 ········{
277 ············foreach(Class cl in mapping.Classes)
278 ················if (cl.TableName == tableName)
279 ····················return cl;
280 ············return null;
281 ········}
282
283 ····}
284 }
285
New Commit (f5f4fd8)
1 //
2 // Copyright (c) 2002-2022 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using System;
24 using System.IO;
25 using System.Data;
26 using System.Collections.Generic;
27 using System.Xml.Linq;
28 using NDO;
29 using NDO.Mapping;
30 using NDOInterfaces;
31 using System.Reflection;
32 using System.Text;
33 using System.Security.Cryptography;
34
35 namespace NDOEnhancer
36 {
37 ····/// <summary>
38 ····/// Zusammenfassung für SQLDiffGenerator.
39 ····/// </summary>
40 ····internal class NdoTransDiffGenerator
41 ····{
42 ········NDOMapping mappings;
43 ········MessageAdapter messages;
44 ········public void Generate(DataSet dsSchema, DataSet dsBak, string filename, NDOMapping mappings, MessageAdapter messages)
45 ········{
46 ············this.mappings = mappings;
47 ············this.messages = messages;
48
49 ············string diffFileName = filename.Replace(".ndo.sql", ".ndodiff." + mappings.SchemaVersion + ".xml");
50 ············bool isNewDiffFile = !File.Exists(diffFileName);
51 ············XElement rootElement = null;
52 ············if (isNewDiffFile)
53 ············{
54 ················rootElement = new XElement( "NdoSchemaTransitions" );
55 ················rootElement.Add( new XComment( "NDO accumulates all schema changes in this diff file. Note: If you change the mapping schema version, a new diff file will be created. You can change the mapping schema version in the NDO configuration dialog. Don't use the Mapping Tool to change the schema information, because it will be overwritten by the value set in the configuration. For automatic builds set the schema version value in the .ndoproj file." ) );
56 ············}
57 ············else
58 ············{
59 ················rootElement = XElement.Load( diffFileName );
60 ············}
61
62 ············XElement transElement = new XElement( "NdoSchemaTransition" );
63 ············rootElement.Add( transElement );
64
65 ············if (GenerateInternal( dsSchema, dsBak, transElement ))
66 ············{
67 ················Guid g = MakeHash( transElement.ToString() );
68 ················transElement.SetAttributeValue( "id", g );
69 ················rootElement.Save( diffFileName );
70 ············}
71 ········}
72
73 ········public static Guid MakeHash( string input )
74 ········{
75 ············var buf = Encoding.UTF8.GetBytes(input);
76
77 ············using (var sha = SHA256.Create())
78 ············{
79 ················var hash = sha.ComputeHash( buf );
80 ················byte[] arr = new byte[16];
81 ················Array.Copy( hash, arr, 16 );
82 ················return new Guid( arr );
83 ············}
84 ········}
85
86 ········bool GenerateInternal(System.Data.DataSet dsNewSchema, System.Data.DataSet dsOldSchema, XElement transElement)
87 ········{
88 ············bool hasChanges = false;
89 ············foreach(DataTable dt in dsNewSchema.Tables)
90 ············{
91 ················if (!dsOldSchema.Tables.Contains(dt.TableName))
92 ················{
93 ····················transElement.Add( CreateTable( dt ) );
94 ····················foreach (IndexDefinition index in IndexDefinition.ForTable( dt, this.mappings ))
95 ························transElement.Add( CreateIndex( dt, index ) );
96 ····················hasChanges = true;
97 ················}
98 ················else
99 ················{
100 ····················DataTable dtOld = dsOldSchema.Tables[dt.TableName];
101 ····················List<DataColumn> addedColumns = new List<DataColumn>();
102 ····················List<DataColumn> removedColumns = new List<DataColumn>();
103 ····················List<DataColumn> changedColumns = new List<DataColumn>();
104 ····················foreach(DataColumn dc in dt.Columns)
105 ····················{
106 ························if (!dtOld.Columns.Contains(dc.ColumnName))
107 ························{
108 ····························addedColumns.Add(dc);
109 ························}
110 ························else
111 ························{
112 ····························DataColumn dcOld = dtOld.Columns[dc.ColumnName];
113 ····························if (dc.DataType != dcOld.DataType || dc.MaxLength != dcOld.MaxLength)
114 ································changedColumns.Add(dc);
115 ························}
116 ····················}
117 ····················foreach(DataColumn dc in dtOld.Columns)
118 ····················{
119 ························if (!dt.Columns.Contains(dc.ColumnName))
120 ························{
121 ····························removedColumns.Add(dc);
122 ························}
123 ····················}
124 ····················if (addedColumns.Count > 0 || removedColumns.Count > 0 || changedColumns.Count > 0)
125 ························hasChanges = true;
126
127 ····················if (!hasChanges)
128 ························continue;
129
130 ····················XElement changeTableElement = ChangeTable(dt, addedColumns, removedColumns, changedColumns);
131 ····················if (changeTableElement.HasElements)
132 ························transElement.Add( changeTableElement );
133
134 ················}
135 ············}
136
137 ············foreach (DataTable dt in dsOldSchema.Tables)
138 ············{
139 ················if (!dsNewSchema.Tables.Contains( dt.TableName ))
140 ················{
141 ····················XElement dropTableElement = new XElement( "DropTable", new XAttribute( "name", dt.TableName ) );
142 ····················transElement.Add( dropTableElement );
143 ····················hasChanges = true;
144 ················}
145 ············}
146
147 ············return hasChanges;
148 ········}
149
150 ········XElement ChangeTable(DataTable dt, List<DataColumn> addedColumns, List<DataColumn> removedColums, List<DataColumn> changedColumns)
151 ········{
152 ············XElement alterTableElement = new XElement( "AlterTable", new XAttribute( "name", dt.TableName ) );
153
154 ············foreach(DataColumn dc in addedColumns)
155 ············{
156 ················XElement addColumnElement = new XElement( "AddColumn", new XAttribute( "name", dc.ColumnName ) );
157 ················CreateColumn(addColumnElement, dc, GetClassForTable(dt.TableName, this.mappings), false);
158 ················alterTableElement.Add( addColumnElement );
159 ············}
160
161 ············foreach(DataColumn dc in removedColums)
162 ············{
163 ················XElement dropColumnElement = new XElement( "DropColumn", new XAttribute( "name", dc.ColumnName ) );
164 ················alterTableElement.Add( dropColumnElement );
165 ············}
166
167 ············foreach(DataColumn dc in changedColumns)
168 ············{
169 ················XElement alterColumnElement = new XElement( "AlterColumn", new XAttribute( "name", dc.ColumnName ) );
170 ················CreateColumn(alterColumnElement, dc, GetClassForTable(dt.TableName, this.mappings), false);
171 ················alterTableElement.Add( alterColumnElement );
172 ············}
173
174 ············if (removedColums.Count > 0)
175 ············{
176 ················alterTableElement.Add( new XComment( "If you need to rename a column use the following syntax:" ) );
177 ················alterTableElement.Add( new XComment( "<RenameColumn oldName=\"...\" newName=\"...\" />" ) );
178 ············}
179
180 ············return alterTableElement;············
181 ········}
182
183 ········/// <summary>
184 ········/// Creates a CreateIndex element, which can be processed by the SchemaTransitionGenerator.
185 ········/// </summary>
186 ········XElement CreateIndex( DataTable dt, IndexDefinition indexDefinition )
187 ········{
188 ············XElement createIndexElement = new XElement( "CreateIndex", new XAttribute( "name", indexDefinition.Name ), new XAttribute( "onTable", dt.TableName ) );
189 ············if (indexDefinition.IsUnique)
190 ················createIndexElement.Add( new XAttribute( "unique", "True" ) );
191 ············foreach (DataColumn dc in indexDefinition.Columns)
192 ················createIndexElement.Add( new XElement( "Column", new XAttribute( "name", dc.ColumnName ) ) );
193 ············return createIndexElement;
194 ········}
195
196 ········protected XElement CreateTable(DataTable dt)
197 ········{
198 ············XElement createTableElement = new XElement( "CreateTable", new XAttribute( "name", dt.TableName ) );
199
200 ············Class cl = FindClass(dt.TableName, mappings);
201
202 ············DataColumn[] primaryKeyColumns = dt.PrimaryKey;
203 ············bool hasPrimaryKeyColumns = primaryKeyColumns.Length > 0;
204
205 ············for (int i = 0; i < dt.Columns.Count; i++)
206 ············{
207 ················System.Data.DataColumn dc = dt.Columns[i];
208
209 ················bool isPrimary = false;
210 ················foreach (DataColumn pkc in primaryKeyColumns)
211 ················{
212 ····················if (pkc.ColumnName == dc.ColumnName)
213 ····················{
214 ························isPrimary = true;
215 ························break;
216 ····················}
217 ················}
218
219 ················XElement createColumnElement = new XElement( "CreateColumn", new XAttribute( "name", dc.ColumnName ) );
220 ················CreateColumn(createColumnElement, dc, cl, isPrimary);
221 ················createTableElement.Add( createColumnElement );
222 ············}
223
224 ············return createTableElement;
225 ········}
226
227 ········protected NDO.Mapping.Class FindClass(string tableName, NDOMapping mappings)
228 ········{
229 ············Class result = null;
230 ············foreach(Class cl in mappings.Classes)
231 ············{
232 ················if (cl.TableName == tableName)
233 ················{
234 ····················result = cl;
235 ····················break;
236 ················}
237 ············}
238 ············return result;
239 ········}
240
241 ········protected void CreateColumn(XElement addColumnElement, DataColumn dc, Class cl, bool isPrimary)
242 ········{
243 ············Type t = dc.DataType;
244 ············addColumnElement.Add( new XAttribute( "type", t.FullName + "," + new AssemblyName( t.Assembly.FullName ).Name ) );
245
246 ············if (isPrimary)
247 ················addColumnElement.Add( new XAttribute( "isPrimary", "true" ) );
248
249 ············if (cl != null)
250 ············{
251 ················Field field = FindField(dc.ColumnName, cl);
252
253 ················if (null != field)
254 ················{
255 ····················if (0 != field.Column.Size && !field.Column.IgnoreColumnSizeInDDL)
256 ····················{
257 ························if (field.Column.Size != 0)
258 ····························addColumnElement.Add( new XAttribute( "size", field.Column.Size.ToString() ) );
259 ····················}
260 ····················if (0 != field.Column.Precision && !field.Column.IgnoreColumnSizeInDDL)
261 ····················{
262 ························if (field.Column.Precision != 0)
263 ····························addColumnElement.Add( new XAttribute( "precision", field.Column.Precision.ToString() ) );
264 ····················}
265 ····················addColumnElement.Add( new XAttribute( "allowNull", field.Column.AllowDbNull.ToString() ) );····················
266 ················}
267 ················else if (isPrimary && dc.AutoIncrement)
268 ················{
269 ····················addColumnElement.Add( new XAttribute( "autoIncrement", "true" ) );····················
270 ················}
271 ············}············
272 ········}
273
274
275 ········protected Field FindField (string columnName, Class cl)
276 ········{
277 ············Field result = null;
278 ············foreach (Field field in cl.Fields)
279 ············{
280 ················if (field.Column.Name == columnName)
281 ················{
282 ····················result = field;
283 ····················break;
284 ················}
285 ············}
286 ············return result;
287 ········}
288
289
290 ········protected Class GetClassForTable(string tableName, NDOMapping mapping)
291 ········{
292 ············foreach(Class cl in mapping.Classes)
293 ················if (cl.TableName == tableName)
294 ····················return cl;
295 ············return null;
296 ········}
297
298 ····}
299 }
300