Refactor creating the TableDefinition into a method before changing the logic
Sean Hall committed
Nov 25, 2019 at 14:35 UTC
229a74c78a403bdd9c065bac372438968a421833
1 file changed
+151
-146
src/WixToolset.Core.WindowsInstaller/Unbind/UnbindDatabaseCommand.cs
+151
-146
@@ -134,152 +134,7 @@ namespace WixToolset.Core.WindowsInstaller.Unbind
134
135
using (var tableView = this.Database.OpenExecuteView(String.Format(CultureInfo.InvariantCulture, "SELECT * FROM `{0}`", tableName)))
136
{
137
- ColumnDefinition[] columns;
138
- using (Record columnNameRecord = tableView.GetColumnInfo(MsiInterop.MSICOLINFONAMES),
139
- columnTypeRecord = tableView.GetColumnInfo(MsiInterop.MSICOLINFOTYPES))
140
- {
141
- // index the primary keys
142
- var tablePrimaryKeys = new HashSet<string>();
143
- using (var primaryKeysRecord = this.Database.PrimaryKeys(tableName))
144
- {
145
- var primaryKeysFieldCount = primaryKeysRecord.GetFieldCount();
146
-
147
- for (var i = 1; i <= primaryKeysFieldCount; i++)
148
- {
149
- tablePrimaryKeys.Add(primaryKeysRecord.GetString(i));
150
- }
151
- }
152
-
153
- var columnCount = columnNameRecord.GetFieldCount();
154
- columns = new ColumnDefinition[columnCount];
155
- for (var i = 1; i <= columnCount; i++)
156
- {
157
- var columnName = columnNameRecord.GetString(i);
158
- var idtType = columnTypeRecord.GetString(i);
159
-
160
- ColumnType columnType;
161
- int length;
162
- bool nullable;
163
-
164
- var columnCategory = ColumnCategory.Unknown;
165
- var columnModularizeType = ColumnModularizeType.None;
166
- var primary = tablePrimaryKeys.Contains(columnName);
167
- int? minValue = null;
168
- int? maxValue = null;
169
- string keyTable = null;
170
- int? keyColumn = null;
171
- string category = null;
172
- string set = null;
173
- string description = null;
174
-
175
- // get the column type, length, and whether its nullable
176
- switch (Char.ToLower(idtType[0], CultureInfo.InvariantCulture))
177
- {
178
- case 'i':
179
- columnType = ColumnType.Number;
180
- break;
181
- case 'l':
182
- columnType = ColumnType.Localized;
183
- break;
184
- case 's':
185
- columnType = ColumnType.String;
186
- break;
187
- case 'v':
188
- columnType = ColumnType.Object;
189
- break;
190
- default:
191
- // TODO: error
192
- columnType = ColumnType.Unknown;
193
- break;
194
- }
195
- length = Convert.ToInt32(idtType.Substring(1), CultureInfo.InvariantCulture);
196
- nullable = Char.IsUpper(idtType[0]);
197
-
198
- // try to get validation information
199
- if (null != validationView)
200
- {
201
- using (var validationRecord = new Record(2))
202
- {
203
- validationRecord.SetString(1, tableName);
204
- validationRecord.SetString(2, columnName);
205
-
206
- validationView.Execute(validationRecord);
207
- }
208
-
209
- using (var validationRecord = validationView.Fetch())
210
- {
211
- if (null != validationRecord)
212
- {
213
- var validationNullable = validationRecord.GetString(3);
214
- minValue = validationRecord.IsNull(4) ? null : (int?)validationRecord.GetInteger(4);
215
- maxValue = validationRecord.IsNull(5) ? null : (int?)validationRecord.GetInteger(5);
216
- keyTable = validationRecord.IsNull(6) ? null : validationRecord.GetString(6);
217
- keyColumn = validationRecord.IsNull(7) ? null : (int?)validationRecord.GetInteger(7);
218
- category = validationRecord.IsNull(8) ? null : validationRecord.GetString(8);
219
- set = validationRecord.IsNull(9) ? null : validationRecord.GetString(9);
220
- description = validationRecord.IsNull(10) ? null : validationRecord.GetString(10);
221
-
222
- // check the validation nullable value against the column definition
223
- if (null == validationNullable)
224
- {
225
- // TODO: warn for illegal validation nullable column
226
- }
227
- else if ((nullable && "Y" != validationNullable) || (!nullable && "N" != validationNullable))
228
- {
229
- // TODO: warn for mismatch between column definition and validation nullable
230
- }
231
-
232
- // convert category to ColumnCategory
233
- if (null != category)
234
- {
235
- try
236
- {
237
- columnCategory = (ColumnCategory)Enum.Parse(typeof(ColumnCategory), category, true);
238
- }
239
- catch (ArgumentException)
240
- {
241
- columnCategory = ColumnCategory.Unknown;
242
- }
243
- }
244
- }
245
- else
246
- {
247
- // TODO: warn about no validation information
248
- }
249
- }
250
- }
251
-
252
- // guess the modularization type
253
- if ("Icon" == keyTable && 1 == keyColumn)
254
- {
255
- columnModularizeType = ColumnModularizeType.Icon;
256
- }
257
- else if ("Condition" == columnName)
258
- {
259
- columnModularizeType = ColumnModularizeType.Condition;
260
- }
261
- else if (ColumnCategory.Formatted == columnCategory || ColumnCategory.FormattedSDDLText == columnCategory)
262
- {
263
- columnModularizeType = ColumnModularizeType.Property;
264
- }
265
- else if (ColumnCategory.Identifier == columnCategory)
266
- {
267
- columnModularizeType = ColumnModularizeType.Column;
268
- }
269
-
270
- columns[i - 1] = new ColumnDefinition(columnName, columnType, length, primary, nullable, columnCategory, minValue, maxValue, keyTable, keyColumn, set, description, columnModularizeType, (ColumnType.Localized == columnType), true);
271
- }
272
- }
273
-
274
- var tableDefinition = new TableDefinition(tableName, columns, false);
275
-
276
- // use our table definitions if core properties are the same; this allows us to take advantage
277
- // of wix concepts like localizable columns which current code assumes
278
- if (this.TableDefinitions.Contains(tableName) && 0 == tableDefinition.CompareTo(this.TableDefinitions[tableName]))
279
- {
280
- tableDefinition = this.TableDefinitions[tableName];
281
- }
282
-
137
+ var tableDefinition = this.GetTableDefinition(tableName, tableView, validationView);
138
var table = new Table(tableDefinition);
139
140
while (true)
@@ -437,6 +292,156 @@ namespace WixToolset.Core.WindowsInstaller.Unbind
292
return output;
293
}
294
295
+ private TableDefinition GetTableDefinition(string tableName, View tableView, View validationView)
296
+ {
297
+ ColumnDefinition[] columns;
298
+ using (Record columnNameRecord = tableView.GetColumnInfo(MsiInterop.MSICOLINFONAMES),
299
+ columnTypeRecord = tableView.GetColumnInfo(MsiInterop.MSICOLINFOTYPES))
300
+ {
301
+ // index the primary keys
302
+ var tablePrimaryKeys = new HashSet<string>();
303
+ using (var primaryKeysRecord = this.Database.PrimaryKeys(tableName))
304
+ {
305
+ var primaryKeysFieldCount = primaryKeysRecord.GetFieldCount();
306
+
307
+ for (var i = 1; i <= primaryKeysFieldCount; i++)
308
+ {
309
+ tablePrimaryKeys.Add(primaryKeysRecord.GetString(i));
310
+ }
311
+ }
312
+
313
+ var columnCount = columnNameRecord.GetFieldCount();
314
+ columns = new ColumnDefinition[columnCount];
315
+ for (var i = 1; i <= columnCount; i++)
316
+ {
317
+ var columnName = columnNameRecord.GetString(i);
318
+ var idtType = columnTypeRecord.GetString(i);
319
+
320
+ ColumnType columnType;
321
+ int length;
322
+ bool nullable;
323
+
324
+ var columnCategory = ColumnCategory.Unknown;
325
+ var columnModularizeType = ColumnModularizeType.None;
326
+ var primary = tablePrimaryKeys.Contains(columnName);
327
+ int? minValue = null;
328
+ int? maxValue = null;
329
+ string keyTable = null;
330
+ int? keyColumn = null;
331
+ string category = null;
332
+ string set = null;
333
+ string description = null;
334
+
335
+ // get the column type, length, and whether its nullable
336
+ switch (Char.ToLower(idtType[0], CultureInfo.InvariantCulture))
337
+ {
338
+ case 'i':
339
+ columnType = ColumnType.Number;
340
+ break;
341
+ case 'l':
342
+ columnType = ColumnType.Localized;
343
+ break;
344
+ case 's':
345
+ columnType = ColumnType.String;
346
+ break;
347
+ case 'v':
348
+ columnType = ColumnType.Object;
349
+ break;
350
+ default:
351
+ // TODO: error
352
+ columnType = ColumnType.Unknown;
353
+ break;
354
+ }
355
+ length = Convert.ToInt32(idtType.Substring(1), CultureInfo.InvariantCulture);
356
+ nullable = Char.IsUpper(idtType[0]);
357
+
358
+ // try to get validation information
359
+ if (null != validationView)
360
+ {
361
+ using (var validationRecord = new Record(2))
362
+ {
363
+ validationRecord.SetString(1, tableName);
364
+ validationRecord.SetString(2, columnName);
365
+
366
+ validationView.Execute(validationRecord);
367
+ }
368
+
369
+ using (var validationRecord = validationView.Fetch())
370
+ {
371
+ if (null != validationRecord)
372
+ {
373
+ var validationNullable = validationRecord.GetString(3);
374
+ minValue = validationRecord.IsNull(4) ? null : (int?)validationRecord.GetInteger(4);
375
+ maxValue = validationRecord.IsNull(5) ? null : (int?)validationRecord.GetInteger(5);
376
+ keyTable = validationRecord.IsNull(6) ? null : validationRecord.GetString(6);
377
+ keyColumn = validationRecord.IsNull(7) ? null : (int?)validationRecord.GetInteger(7);
378
+ category = validationRecord.IsNull(8) ? null : validationRecord.GetString(8);
379
+ set = validationRecord.IsNull(9) ? null : validationRecord.GetString(9);
380
+ description = validationRecord.IsNull(10) ? null : validationRecord.GetString(10);
381
+
382
+ // check the validation nullable value against the column definition
383
+ if (null == validationNullable)
384
+ {
385
+ // TODO: warn for illegal validation nullable column
386
+ }
387
+ else if ((nullable && "Y" != validationNullable) || (!nullable && "N" != validationNullable))
388
+ {
389
+ // TODO: warn for mismatch between column definition and validation nullable
390
+ }
391
+
392
+ // convert category to ColumnCategory
393
+ if (null != category)
394
+ {
395
+ try
396
+ {
397
+ columnCategory = (ColumnCategory)Enum.Parse(typeof(ColumnCategory), category, true);
398
+ }
399
+ catch (ArgumentException)
400
+ {
401
+ columnCategory = ColumnCategory.Unknown;
402
+ }
403
+ }
404
+ }
405
+ else
406
+ {
407
+ // TODO: warn about no validation information
408
+ }
409
+ }
410
+ }
411
+
412
+ // guess the modularization type
413
+ if ("Icon" == keyTable && 1 == keyColumn)
414
+ {
415
+ columnModularizeType = ColumnModularizeType.Icon;
416
+ }
417
+ else if ("Condition" == columnName)
418
+ {
419
+ columnModularizeType = ColumnModularizeType.Condition;
420
+ }
421
+ else if (ColumnCategory.Formatted == columnCategory || ColumnCategory.FormattedSDDLText == columnCategory)
422
+ {
423
+ columnModularizeType = ColumnModularizeType.Property;
424
+ }
425
+ else if (ColumnCategory.Identifier == columnCategory)
426
+ {
427
+ columnModularizeType = ColumnModularizeType.Column;
428
+ }
429
+
430
+ columns[i - 1] = new ColumnDefinition(columnName, columnType, length, primary, nullable, columnCategory, minValue, maxValue, keyTable, keyColumn, set, description, columnModularizeType, (ColumnType.Localized == columnType), true);
431
+ }
432
+ }
433
+
434
+ var tableDefinition = new TableDefinition(tableName, columns, false);
435
+
436
+ // use our table definitions if core properties are the same; this allows us to take advantage
437
+ // of wix concepts like localizable columns which current code assumes
438
+ if (this.TableDefinitions.Contains(tableName) && 0 == tableDefinition.CompareTo(this.TableDefinitions[tableName]))
439
+ {
440
+ tableDefinition = this.TableDefinitions[tableName];
441
+ }
442
+ return tableDefinition;
443
+ }
444
+
445
/// <summary>
446
/// Generates the WixFile table based on a path to an admin image msi and an Output.
447
/// </summary>