Add TupleIdIsPrimaryKey.
Sean Hall committed
Apr 3, 2020 at 11:01 UTC
9a128569f6ebf7ed43d57b0ecd4ada5ba8a5585a
1 file changed
+30
-10
src/WixToolset.Data/WindowsInstaller/TableDefinition.cs
+30
-10
@@ -24,12 +24,8 @@ namespace WixToolset.Data.WindowsInstaller
24
/// <param name="name">Name of table to create.</param>
25
/// <param name="columns">Column definitions for the table.</param>
26
/// <param name="unreal">Flag if table is unreal.</param>
27
- public TableDefinition(string name, IEnumerable<ColumnDefinition> columns, bool unreal = false)
27
+ public TableDefinition(string name, IEnumerable<ColumnDefinition> columns, bool unreal = false) : this(name, null, columns, unreal)
28
{
29
- this.Name = name;
30
- this.TupleDefinitionName = name;
31
- this.Unreal = unreal;
32
- this.Columns = columns.ToArray();
29
}
30
31
/// <summary>
@@ -39,34 +35,49 @@ namespace WixToolset.Data.WindowsInstaller
35
/// <param name="tupleDefinitionName">Optional name of tuple definition for this table.</param>
36
/// <param name="columns">Column definitions for the table.</param>
37
/// <param name="unreal">Flag if table is unreal.</param>
42
- public TableDefinition(string name, string tupleDefinitionName, IEnumerable<ColumnDefinition> columns, bool unreal = false) : this(name, columns, unreal)
38
+ public TableDefinition(string name, string tupleDefinitionName, IEnumerable<ColumnDefinition> columns, bool unreal = false)
39
{
40
+ this.Name = name;
41
this.TupleDefinitionName = tupleDefinitionName ?? name;
42
+ this.Unreal = unreal;
43
+ this.Columns = columns?.ToArray();
44
+
45
+ if (this.Columns == null || this.Columns.Length == 0)
46
+ {
47
+ throw new ArgumentOutOfRangeException(nameof(columns));
48
+ }
49
+ this.TupleIdIsPrimaryKey = DeriveTupleIdIsPrimaryKey(this.Columns);
50
}
51
52
/// <summary>
53
/// Gets the name of the table.
54
/// </summary>
55
/// <value>Name of the table.</value>
51
- public string Name { get; private set; }
56
+ public string Name { get; }
57
58
/// <summary>
59
/// Gets the name of the tuple definition associated with this table.
60
/// </summary>
61
/// <value>Name of the tuple definition.</value>
57
- public string TupleDefinitionName { get; private set; }
62
+ public string TupleDefinitionName { get; }
63
64
/// <summary>
65
/// Gets if the table is unreal.
66
/// </summary>
67
/// <value>Flag if table is unreal.</value>
63
- public bool Unreal { get; private set; }
68
+ public bool Unreal { get; }
69
70
/// <summary>
71
/// Gets the collection of column definitions for this table.
72
/// </summary>
73
/// <value>Collection of column definitions for this table.</value>
69
- public ColumnDefinition[] Columns { get; private set; }
74
+ public ColumnDefinition[] Columns { get; }
75
+
76
+ /// <summary>
77
+ /// Gets if the primary key is the id of the tuple definition associated with this table.
78
+ /// </summary>
79
+ /// <value>Flag if table is unreal.</value>
80
+ public bool TupleIdIsPrimaryKey { get; }
81
82
/// <summary>
83
/// Gets the column definition in the table by index.
@@ -223,5 +234,14 @@ namespace WixToolset.Data.WindowsInstaller
234
235
writer.WriteEndElement();
236
}
237
+
238
+ private static bool DeriveTupleIdIsPrimaryKey(ColumnDefinition[] columns)
239
+ {
240
+ return columns[0].PrimaryKey &&
241
+ columns[0].Type == ColumnType.String &&
242
+ columns[0].Category == ColumnCategory.Identifier &&
243
+ !columns[0].Name.EndsWith("_") &&
244
+ (columns.Length == 1 || !columns.Skip(1).Any(t => t.PrimaryKey));
245
+ }
246
}
247
}