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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
namespace WixToolset.Core.WindowsInstaller.Unbind
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
using WixToolset.Core.Native;
using WixToolset.Data;
using WixToolset.Data.WindowsInstaller;
using WixToolset.Data.WindowsInstaller.Rows;
using WixToolset.Extensibility.Services;
using WixToolset.Msi;
internal class UnbindDatabaseCommand
{
public UnbindDatabaseCommand(IMessaging messaging, Database database, string databasePath, OutputType outputType, string exportBasePath, string intermediateFolder, bool isAdminImage, bool suppressDemodularization, bool skipSummaryInfo)
{
this.Messaging = messaging;
this.Database = database;
this.DatabasePath = databasePath;
this.OutputType = outputType;
this.ExportBasePath = exportBasePath;
this.IntermediateFolder = intermediateFolder;
this.IsAdminImage = isAdminImage;
this.SuppressDemodularization = suppressDemodularization;
this.SkipSummaryInfo = skipSummaryInfo;
this.TableDefinitions = WindowsInstallerStandardInternal.GetTableDefinitions();
}
public IMessaging Messaging { get; }
public Database Database { get; }
public string DatabasePath { get; }
public OutputType OutputType { get; }
public string ExportBasePath { get; }
public string IntermediateFolder { get; }
public bool IsAdminImage { get; }
public bool SuppressDemodularization { get; }
public bool SkipSummaryInfo { get; }
public TableDefinitionCollection TableDefinitions { get; }
private int SectionCount { get; set; }
public Output Execute()
{
string modularizationGuid = null;
Output output = new Output(new SourceLineNumber(this.DatabasePath));
View validationView = null;
// set the output type
output.Type = this.OutputType;
// get the codepage
this.Database.Export("_ForceCodepage", this.IntermediateFolder, "_ForceCodepage.idt");
using (StreamReader sr = File.OpenText(Path.Combine(this.IntermediateFolder, "_ForceCodepage.idt")))
{
string line;
while (null != (line = sr.ReadLine()))
{
string[] data = line.Split('\t');
if (2 == data.Length)
{
output.Codepage = Convert.ToInt32(data[0], CultureInfo.InvariantCulture);
}
}
}
// get the summary information table if it exists; it won't if unbinding a transform
if (!this.SkipSummaryInfo)
{
using (SummaryInformation summaryInformation = new SummaryInformation(this.Database))
{
Table table = new Table(this.TableDefinitions["_SummaryInformation"]);
for (int i = 1; 19 >= i; i++)
{
string value = summaryInformation.GetProperty(i);
if (0 < value.Length)
{
Row row = table.CreateRow(output.SourceLineNumbers);
row[0] = i;
row[1] = value;
}
}
output.Tables.Add(table);
}
}
try
{
// open a view on the validation table if it exists
if (this.Database.TableExists("_Validation"))
{
validationView = this.Database.OpenView("SELECT * FROM `_Validation` WHERE `Table` = ? AND `Column` = ?");
}
// get the normal tables
using (View tablesView = this.Database.OpenExecuteView("SELECT * FROM _Tables"))
{
while (true)
{
using (Record tableRecord = tablesView.Fetch())
{
if (null == tableRecord)
{
break;
}
string tableName = tableRecord.GetString(1);
using (View tableView = this.Database.OpenExecuteView(String.Format(CultureInfo.InvariantCulture, "SELECT * FROM `{0}`", tableName)))
{
ColumnDefinition[] columns;
using (Record columnNameRecord = tableView.GetColumnInfo(MsiInterop.MSICOLINFONAMES),
columnTypeRecord = tableView.GetColumnInfo(MsiInterop.MSICOLINFOTYPES))
{
// index the primary keys
HashSet<string> tablePrimaryKeys = new HashSet<string>();
using (Record primaryKeysRecord = this.Database.PrimaryKeys(tableName))
{
int primaryKeysFieldCount = primaryKeysRecord.GetFieldCount();
for (int i = 1; i <= primaryKeysFieldCount; i++)
{
tablePrimaryKeys.Add(primaryKeysRecord.GetString(i));
}
}
int columnCount = columnNameRecord.GetFieldCount();
columns = new ColumnDefinition[columnCount];
for (int i = 1; i <= columnCount; i++)
{
string columnName = columnNameRecord.GetString(i);
string idtType = columnTypeRecord.GetString(i);
ColumnType columnType;
int length;
bool nullable;
ColumnCategory columnCategory = ColumnCategory.Unknown;
ColumnModularizeType columnModularizeType = ColumnModularizeType.None;
bool primary = tablePrimaryKeys.Contains(columnName);
int? minValue = null;
int? maxValue = null;
string keyTable = null;
int? keyColumn = null;
string category = null;
string set = null;
string description = null;
// get the column type, length, and whether its nullable
switch (Char.ToLower(idtType[0], CultureInfo.InvariantCulture))
{
case 'i':
columnType = ColumnType.Number;
break;
case 'l':
columnType = ColumnType.Localized;
break;
case 's':
columnType = ColumnType.String;
break;
case 'v':
columnType = ColumnType.Object;
break;
default:
// TODO: error
columnType = ColumnType.Unknown;
break;
}
length = Convert.ToInt32(idtType.Substring(1), CultureInfo.InvariantCulture);
nullable = Char.IsUpper(idtType[0]);
// try to get validation information
if (null != validationView)
{
using (Record validationRecord = new Record(2))
{
validationRecord.SetString(1, tableName);
validationRecord.SetString(2, columnName);
validationView.Execute(validationRecord);
}
using (Record validationRecord = validationView.Fetch())
{
if (null != validationRecord)
{
string validationNullable = validationRecord.GetString(3);
minValue = validationRecord.IsNull(4) ? null : (int?)validationRecord.GetInteger(4);
maxValue = validationRecord.IsNull(5) ? null : (int?)validationRecord.GetInteger(5);
keyTable = validationRecord.IsNull(6) ? null : validationRecord.GetString(6);
keyColumn = validationRecord.IsNull(7) ? null : (int?)validationRecord.GetInteger(7);
category = validationRecord.IsNull(8) ? null : validationRecord.GetString(8);
set = validationRecord.IsNull(9) ? null : validationRecord.GetString(9);
description = validationRecord.IsNull(10) ? null : validationRecord.GetString(10);
// check the validation nullable value against the column definition
if (null == validationNullable)
{
// TODO: warn for illegal validation nullable column
}
else if ((nullable && "Y" != validationNullable) || (!nullable && "N" != validationNullable))
{
// TODO: warn for mismatch between column definition and validation nullable
}
// convert category to ColumnCategory
if (null != category)
{
try
{
columnCategory = (ColumnCategory)Enum.Parse(typeof(ColumnCategory), category, true);
}
catch (ArgumentException)
{
columnCategory = ColumnCategory.Unknown;
}
}
}
else
{
// TODO: warn about no validation information
}
}
}
// guess the modularization type
if ("Icon" == keyTable && 1 == keyColumn)
{
columnModularizeType = ColumnModularizeType.Icon;
}
else if ("Condition" == columnName)
{
columnModularizeType = ColumnModularizeType.Condition;
}
else if (ColumnCategory.Formatted == columnCategory || ColumnCategory.FormattedSDDLText == columnCategory)
{
columnModularizeType = ColumnModularizeType.Property;
}
else if (ColumnCategory.Identifier == columnCategory)
{
columnModularizeType = ColumnModularizeType.Column;
}
columns[i - 1] = new ColumnDefinition(columnName, columnType, length, primary, nullable, columnCategory, minValue, maxValue, keyTable, keyColumn, set, description, columnModularizeType, (ColumnType.Localized == columnType), true);
}
}
TableDefinition tableDefinition = new TableDefinition(tableName, columns, false, false);
// use our table definitions if core properties are the same; this allows us to take advantage
// of wix concepts like localizable columns which current code assumes
if (this.TableDefinitions.Contains(tableName) && 0 == tableDefinition.CompareTo(this.TableDefinitions[tableName]))
{
tableDefinition = this.TableDefinitions[tableName];
}
Table table = new Table(tableDefinition);
while (true)
{
using (Record rowRecord = tableView.Fetch())
{
if (null == rowRecord)
{
break;
}
int recordCount = rowRecord.GetFieldCount();
Row row = table.CreateRow(output.SourceLineNumbers);
for (int i = 0; recordCount > i && row.Fields.Length > i; i++)
{
if (rowRecord.IsNull(i + 1))
{
if (!row.Fields[i].Column.Nullable)
{
// TODO: display an error for a null value in a non-nullable field OR
// display a warning and put an empty string in the value to let the compiler handle it
// (the second option is risky because the later code may make certain assumptions about
// the contents of a row value)
}
}
else
{
switch (row.Fields[i].Column.Type)
{
case ColumnType.Number:
bool success = false;
int intValue = rowRecord.GetInteger(i + 1);
if (row.Fields[i].Column.IsLocalizable)
{
success = row.BestEffortSetField(i, Convert.ToString(intValue, CultureInfo.InvariantCulture));
}
else
{
success = row.BestEffortSetField(i, intValue);
}
if (!success)
{
this.Messaging.Write(WarningMessages.BadColumnDataIgnored(row.SourceLineNumbers, Convert.ToString(intValue, CultureInfo.InvariantCulture), tableName, row.Fields[i].Column.Name));
}
break;
case ColumnType.Object:
string sourceFile = "FILE NOT EXPORTED, USE THE dark.exe -x OPTION TO EXPORT BINARIES";
if (null != this.ExportBasePath)
{
string relativeSourceFile = Path.Combine(tableName, row.GetPrimaryKey('.'));
sourceFile = Path.Combine(this.ExportBasePath, relativeSourceFile);
// ensure the parent directory exists
System.IO.Directory.CreateDirectory(Path.Combine(this.ExportBasePath, tableName));
using (FileStream fs = System.IO.File.Create(sourceFile))
{
int bytesRead;
byte[] buffer = new byte[512];
while (0 != (bytesRead = rowRecord.GetStream(i + 1, buffer, buffer.Length)))
{
fs.Write(buffer, 0, bytesRead);
}
}
}
row[i] = sourceFile;
break;
default:
string value = rowRecord.GetString(i + 1);
switch (row.Fields[i].Column.Category)
{
case ColumnCategory.Guid:
value = value.ToUpper(CultureInfo.InvariantCulture);
break;
}
// de-modularize
if (!this.SuppressDemodularization && OutputType.Module == output.Type && ColumnModularizeType.None != row.Fields[i].Column.ModularizeType)
{
Regex modularization = new Regex(@"\.[0-9A-Fa-f]{8}_[0-9A-Fa-f]{4}_[0-9A-Fa-f]{4}_[0-9A-Fa-f]{4}_[0-9A-Fa-f]{12}");
if (null == modularizationGuid)
{
Match match = modularization.Match(value);
if (match.Success)
{
modularizationGuid = String.Concat('{', match.Value.Substring(1).Replace('_', '-'), '}');
}
}
value = modularization.Replace(value, String.Empty);
}
// escape "$(" for the preprocessor
value = value.Replace("$(", "$$(");
// escape things that look like wix variables
MatchCollection matches = Common.WixVariableRegex.Matches(value);
for (int j = matches.Count - 1; 0 <= j; j--)
{
value = value.Insert(matches[j].Index, "!");
}
row[i] = value;
break;
}
}
}
}
}
output.Tables.Add(table);
}
}
}
}
}
finally
{
if (null != validationView)
{
validationView.Close();
}
}
// set the modularization guid as the PackageCode
if (null != modularizationGuid)
{
Table table = output.Tables["_SummaryInformation"];
foreach (Row row in table.Rows)
{
if (9 == (int)row[0]) // PID_REVNUMBER
{
row[1] = modularizationGuid;
}
}
}
if (this.IsAdminImage)
{
GenerateWixFileTable(this.DatabasePath, output);
GenerateSectionIds(output);
}
return output;
}
/// <summary>
/// Generates the WixFile table based on a path to an admin image msi and an Output.
/// </summary>
/// <param name="databaseFile">The path to the msi database file in an admin image.</param>
/// <param name="output">The Output that represents the msi database.</param>
private void GenerateWixFileTable(string databaseFile, Output output)
{
string adminRootPath = Path.GetDirectoryName(databaseFile);
Hashtable componentDirectoryIndex = new Hashtable();
Table componentTable = output.Tables["Component"];
foreach (Row row in componentTable.Rows)
{
componentDirectoryIndex.Add(row[0], row[2]);
}
// Index full source paths for all directories
Hashtable directoryDirectoryParentIndex = new Hashtable();
Hashtable directoryFullPathIndex = new Hashtable();
Hashtable directorySourceNameIndex = new Hashtable();
Table directoryTable = output.Tables["Directory"];
foreach (Row row in directoryTable.Rows)
{
directoryDirectoryParentIndex.Add(row[0], row[1]);
if (null == row[1])
{
directoryFullPathIndex.Add(row[0], adminRootPath);
}
else
{
directorySourceNameIndex.Add(row[0], GetAdminSourceName((string)row[2]));
}
}
foreach (DictionaryEntry directoryEntry in directoryDirectoryParentIndex)
{
if (!directoryFullPathIndex.ContainsKey(directoryEntry.Key))
{
GetAdminFullPath((string)directoryEntry.Key, directoryDirectoryParentIndex, directorySourceNameIndex, directoryFullPathIndex);
}
}
Table fileTable = output.Tables["File"];
Table wixFileTable = output.EnsureTable(this.TableDefinitions["WixFile"]);
foreach (Row row in fileTable.Rows)
{
WixFileRow wixFileRow = new WixFileRow(null, this.TableDefinitions["WixFile"]);
wixFileRow.File = (string)row[0];
wixFileRow.Directory = (string)componentDirectoryIndex[(string)row[1]];
wixFileRow.Source = Path.Combine((string)directoryFullPathIndex[wixFileRow.Directory], GetAdminSourceName((string)row[2]));
if (!File.Exists(wixFileRow.Source))
{
throw new WixException(ErrorMessages.WixFileNotFound(wixFileRow.Source));
}
wixFileTable.Rows.Add(wixFileRow);
}
}
/// <summary>
/// Gets the full path of a directory. Populates the full path index with the directory's full path and all of its parent directorie's full paths.
/// </summary>
/// <param name="directory">The directory identifier.</param>
/// <param name="directoryDirectoryParentIndex">The Hashtable containing all the directory to directory parent mapping.</param>
/// <param name="directorySourceNameIndex">The Hashtable containing all the directory to source name mapping.</param>
/// <param name="directoryFullPathIndex">The Hashtable containing a mapping between all of the directories and their previously calculated full paths.</param>
/// <returns>The full path to the directory.</returns>
private string GetAdminFullPath(string directory, Hashtable directoryDirectoryParentIndex, Hashtable directorySourceNameIndex, Hashtable directoryFullPathIndex)
{
string parent = (string)directoryDirectoryParentIndex[directory];
string sourceName = (string)directorySourceNameIndex[directory];
string parentFullPath;
if (directoryFullPathIndex.ContainsKey(parent))
{
parentFullPath = (string)directoryFullPathIndex[parent];
}
else
{
parentFullPath = GetAdminFullPath(parent, directoryDirectoryParentIndex, directorySourceNameIndex, directoryFullPathIndex);
}
if (null == sourceName)
{
sourceName = String.Empty;
}
string fullPath = Path.Combine(parentFullPath, sourceName);
directoryFullPathIndex.Add(directory, fullPath);
return fullPath;
}
/// <summary>
/// Get the source name in an admin image.
/// </summary>
/// <param name="value">The Filename value.</param>
/// <returns>The source name of the directory in an admin image.</returns>
private static string GetAdminSourceName(string value)
{
string name = null;
string[] names;
string shortname = null;
string shortsourcename = null;
string sourcename = null;
names = Common.GetNames(value);
if (null != names[0] && "." != names[0])
{
if (null != names[1])
{
shortname = names[0];
}
else
{
name = names[0];
}
}
if (null != names[1])
{
name = names[1];
}
if (null != names[2])
{
if (null != names[3])
{
shortsourcename = names[2];
}
else
{
sourcename = names[2];
}
}
if (null != names[3])
{
sourcename = names[3];
}
if (null != sourcename)
{
return sourcename;
}
else if (null != shortsourcename)
{
return shortsourcename;
}
else if (null != name)
{
return name;
}
else
{
return shortname;
}
}
/// <summary>
/// Creates section ids on rows which form logical groupings of resources.
/// </summary>
/// <param name="output">The Output that represents the msi database.</param>
private void GenerateSectionIds(Output output)
{
// First assign and index section ids for the tables that are in their own sections.
AssignSectionIdsToTable(output.Tables["Binary"], 0);
Hashtable componentSectionIdIndex = AssignSectionIdsToTable(output.Tables["Component"], 0);
Hashtable customActionSectionIdIndex = AssignSectionIdsToTable(output.Tables["CustomAction"], 0);
AssignSectionIdsToTable(output.Tables["Directory"], 0);
Hashtable featureSectionIdIndex = AssignSectionIdsToTable(output.Tables["Feature"], 0);
AssignSectionIdsToTable(output.Tables["Icon"], 0);
Hashtable digitalCertificateSectionIdIndex = AssignSectionIdsToTable(output.Tables["MsiDigitalCertificate"], 0);
AssignSectionIdsToTable(output.Tables["Property"], 0);
// Now handle all the tables that rely on the first set of indexes but also produce their own indexes. Order matters here.
Hashtable fileSectionIdIndex = ConnectTableToSectionAndIndex(output.Tables["File"], componentSectionIdIndex, 1, 0);
Hashtable appIdSectionIdIndex = ConnectTableToSectionAndIndex(output.Tables["Class"], componentSectionIdIndex, 2, 5);
Hashtable odbcDataSourceSectionIdIndex = ConnectTableToSectionAndIndex(output.Tables["ODBCDataSource"], componentSectionIdIndex, 1, 0);
Hashtable odbcDriverSectionIdIndex = ConnectTableToSectionAndIndex(output.Tables["ODBCDriver"], componentSectionIdIndex, 1, 0);
Hashtable registrySectionIdIndex = ConnectTableToSectionAndIndex(output.Tables["Registry"], componentSectionIdIndex, 5, 0);
Hashtable serviceInstallSectionIdIndex = ConnectTableToSectionAndIndex(output.Tables["ServiceInstall"], componentSectionIdIndex, 11, 0);
// Now handle all the tables which only rely on previous indexes and order does not matter.
foreach (Table table in output.Tables)
{
switch (table.Name)
{
case "WixFile":
case "MsiFileHash":
ConnectTableToSection(table, fileSectionIdIndex, 0);
break;
case "MsiAssembly":
case "MsiAssemblyName":
ConnectTableToSection(table, componentSectionIdIndex, 0);
break;
case "MsiPackageCertificate":
case "MsiPatchCertificate":
ConnectTableToSection(table, digitalCertificateSectionIdIndex, 1);
break;
case "CreateFolder":
case "FeatureComponents":
case "MoveFile":
case "ReserveCost":
case "ODBCTranslator":
ConnectTableToSection(table, componentSectionIdIndex, 1);
break;
case "TypeLib":
ConnectTableToSection(table, componentSectionIdIndex, 2);
break;
case "Shortcut":
case "Environment":
ConnectTableToSection(table, componentSectionIdIndex, 3);
break;
case "RemoveRegistry":
ConnectTableToSection(table, componentSectionIdIndex, 4);
break;
case "ServiceControl":
ConnectTableToSection(table, componentSectionIdIndex, 5);
break;
case "IniFile":
case "RemoveIniFile":
ConnectTableToSection(table, componentSectionIdIndex, 7);
break;
case "AppId":
ConnectTableToSection(table, appIdSectionIdIndex, 0);
break;
case "Condition":
ConnectTableToSection(table, featureSectionIdIndex, 0);
break;
case "ODBCSourceAttribute":
ConnectTableToSection(table, odbcDataSourceSectionIdIndex, 0);
break;
case "ODBCAttribute":
ConnectTableToSection(table, odbcDriverSectionIdIndex, 0);
break;
case "AdminExecuteSequence":
case "AdminUISequence":
case "AdvtExecuteSequence":
case "AdvtUISequence":
case "InstallExecuteSequence":
case "InstallUISequence":
ConnectTableToSection(table, customActionSectionIdIndex, 0);
break;
case "LockPermissions":
case "MsiLockPermissions":
foreach (Row row in table.Rows)
{
string lockObject = (string)row[0];
string tableName = (string)row[1];
switch (tableName)
{
case "File":
row.SectionId = (string)fileSectionIdIndex[lockObject];
break;
case "Registry":
row.SectionId = (string)registrySectionIdIndex[lockObject];
break;
case "ServiceInstall":
row.SectionId = (string)serviceInstallSectionIdIndex[lockObject];
break;
}
}
break;
}
}
// Now pass the output to each unbinder extension to allow them to analyze the output and determine thier proper section ids.
//foreach (IUnbinderExtension extension in this.unbinderExtensions)
//{
// extension.GenerateSectionIds(output);
//}
}
/// <summary>
/// Creates new section ids on all the rows in a table.
/// </summary>
/// <param name="table">The table to add sections to.</param>
/// <param name="rowPrimaryKeyIndex">The index of the column which is used by other tables to reference this table.</param>
/// <returns>A Hashtable containing the tables key for each row paired with its assigned section id.</returns>
private Hashtable AssignSectionIdsToTable(Table table, int rowPrimaryKeyIndex)
{
Hashtable hashtable = new Hashtable();
if (null != table)
{
foreach (Row row in table.Rows)
{
row.SectionId = GetNewSectionId();
hashtable.Add(row[rowPrimaryKeyIndex], row.SectionId);
}
}
return hashtable;
}
/// <summary>
/// Connects a table's rows to an already sectioned table.
/// </summary>
/// <param name="table">The table containing rows that need to be connected to sections.</param>
/// <param name="sectionIdIndex">A hashtable containing keys to map table to its section.</param>
/// <param name="rowIndex">The index of the column which is used as the foreign key in to the sectionIdIndex.</param>
private static void ConnectTableToSection(Table table, Hashtable sectionIdIndex, int rowIndex)
{
if (null != table)
{
foreach (Row row in table.Rows)
{
if (sectionIdIndex.ContainsKey(row[rowIndex]))
{
row.SectionId = (string)sectionIdIndex[row[rowIndex]];
}
}
}
}
/// <summary>
/// Connects a table's rows to an already sectioned table and produces an index for other tables to connect to it.
/// </summary>
/// <param name="table">The table containing rows that need to be connected to sections.</param>
/// <param name="sectionIdIndex">A hashtable containing keys to map table to its section.</param>
/// <param name="rowIndex">The index of the column which is used as the foreign key in to the sectionIdIndex.</param>
/// <param name="rowPrimaryKeyIndex">The index of the column which is used by other tables to reference this table.</param>
/// <returns>A Hashtable containing the tables key for each row paired with its assigned section id.</returns>
private static Hashtable ConnectTableToSectionAndIndex(Table table, Hashtable sectionIdIndex, int rowIndex, int rowPrimaryKeyIndex)
{
Hashtable newHashTable = new Hashtable();
if (null != table)
{
foreach (Row row in table.Rows)
{
if (!sectionIdIndex.ContainsKey(row[rowIndex]))
{
continue;
}
row.SectionId = (string)sectionIdIndex[row[rowIndex]];
if (null != row[rowPrimaryKeyIndex])
{
newHashTable.Add(row[rowPrimaryKeyIndex], row.SectionId);
}
}
}
return newHashTable;
}
/// <summary>
/// Creates a new section identifier to be used when adding a section to an output.
/// </summary>
/// <returns>A string representing a new section id.</returns>
private string GetNewSectionId()
{
this.SectionCount++;
return "wix.section." + this.SectionCount.ToString(CultureInfo.InvariantCulture);
}
}
}
|