diff options
Diffstat (limited to '')
-rw-r--r-- | src/TablesAndTuples/WixTableDefinition.cs | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/src/TablesAndTuples/WixTableDefinition.cs b/src/TablesAndTuples/WixTableDefinition.cs new file mode 100644 index 00000000..baada41d --- /dev/null +++ b/src/TablesAndTuples/WixTableDefinition.cs | |||
@@ -0,0 +1,159 @@ | |||
1 | using System.Collections.Generic; | ||
2 | using System.Linq; | ||
3 | using System.Xml; | ||
4 | |||
5 | namespace TablesAndTuples | ||
6 | { | ||
7 | class WixTableDefinition | ||
8 | { | ||
9 | public WixTableDefinition(string name, IEnumerable<WixColumnDefinition> columns, bool unreal, string tupleDefinitionName, bool? tupleIdIsPrimaryKey) | ||
10 | { | ||
11 | this.Name = name; | ||
12 | this.Unreal = unreal; | ||
13 | this.Columns = columns?.ToArray(); | ||
14 | this.TupleDefinitionName = tupleDefinitionName ?? name; | ||
15 | this.TupleIdIsPrimaryKey = tupleIdIsPrimaryKey ?? DeriveTupleIdIsPrimaryKey(this.Columns); | ||
16 | } | ||
17 | |||
18 | public string Name { get; } | ||
19 | |||
20 | public string TupleDefinitionName { get; } | ||
21 | |||
22 | public bool Unreal { get; } | ||
23 | |||
24 | public WixColumnDefinition[] Columns { get; } | ||
25 | |||
26 | public bool TupleIdIsPrimaryKey { get; } | ||
27 | |||
28 | static WixTableDefinition Read(XmlReader reader) | ||
29 | { | ||
30 | var empty = reader.IsEmptyElement; | ||
31 | string name = null; | ||
32 | string tupleDefinitionName = null; | ||
33 | var unreal = false; | ||
34 | bool? tupleIdIsPrimaryKey = null; | ||
35 | |||
36 | while (reader.MoveToNextAttribute()) | ||
37 | { | ||
38 | switch (reader.LocalName) | ||
39 | { | ||
40 | case "name": | ||
41 | name = reader.Value; | ||
42 | break; | ||
43 | case "tupleDefinitionName": | ||
44 | tupleDefinitionName = reader.Value; | ||
45 | break; | ||
46 | case "tupleIdIsPrimaryKey": | ||
47 | tupleIdIsPrimaryKey = reader.Value.Equals("yes"); | ||
48 | break; | ||
49 | case "unreal": | ||
50 | unreal = reader.Value.Equals("yes"); | ||
51 | break; | ||
52 | } | ||
53 | } | ||
54 | |||
55 | if (null == name) | ||
56 | { | ||
57 | throw new XmlException(); | ||
58 | } | ||
59 | |||
60 | var columns = new List<WixColumnDefinition>(); | ||
61 | |||
62 | // parse the child elements | ||
63 | if (!empty) | ||
64 | { | ||
65 | var done = false; | ||
66 | |||
67 | while (!done && reader.Read()) | ||
68 | { | ||
69 | switch (reader.NodeType) | ||
70 | { | ||
71 | case XmlNodeType.Element: | ||
72 | switch (reader.LocalName) | ||
73 | { | ||
74 | case "columnDefinition": | ||
75 | var columnDefinition = WixColumnDefinition.Read(reader); | ||
76 | columns.Add(columnDefinition); | ||
77 | break; | ||
78 | default: | ||
79 | throw new XmlException(); | ||
80 | } | ||
81 | break; | ||
82 | case XmlNodeType.EndElement: | ||
83 | done = true; | ||
84 | break; | ||
85 | } | ||
86 | } | ||
87 | |||
88 | if (!done) | ||
89 | { | ||
90 | throw new XmlException(); | ||
91 | } | ||
92 | } | ||
93 | |||
94 | return new WixTableDefinition(name, columns.ToArray(), unreal, tupleDefinitionName, tupleIdIsPrimaryKey); | ||
95 | } | ||
96 | |||
97 | static bool DeriveTupleIdIsPrimaryKey(WixColumnDefinition[] columns) | ||
98 | { | ||
99 | return columns[0].PrimaryKey && | ||
100 | columns[0].Type == ColumnType.String && | ||
101 | columns[0].Category == ColumnCategory.Identifier && | ||
102 | !columns[0].Name.EndsWith("_") && | ||
103 | (columns.Length == 1 || !columns.Skip(1).Any(t => t.PrimaryKey)); | ||
104 | } | ||
105 | |||
106 | public static List<WixTableDefinition> LoadCollection(string inputPath) | ||
107 | { | ||
108 | using (var reader = XmlReader.Create(inputPath)) | ||
109 | { | ||
110 | reader.MoveToContent(); | ||
111 | |||
112 | if ("tableDefinitions" != reader.LocalName) | ||
113 | { | ||
114 | throw new XmlException(); | ||
115 | } | ||
116 | |||
117 | var empty = reader.IsEmptyElement; | ||
118 | var tableDefinitions = new List<WixTableDefinition>(); | ||
119 | |||
120 | while (reader.MoveToNextAttribute()) | ||
121 | { | ||
122 | } | ||
123 | |||
124 | // parse the child elements | ||
125 | if (!empty) | ||
126 | { | ||
127 | var done = false; | ||
128 | |||
129 | while (!done && reader.Read()) | ||
130 | { | ||
131 | switch (reader.NodeType) | ||
132 | { | ||
133 | case XmlNodeType.Element: | ||
134 | switch (reader.LocalName) | ||
135 | { | ||
136 | case "tableDefinition": | ||
137 | tableDefinitions.Add(WixTableDefinition.Read(reader)); | ||
138 | break; | ||
139 | default: | ||
140 | throw new XmlException(); | ||
141 | } | ||
142 | break; | ||
143 | case XmlNodeType.EndElement: | ||
144 | done = true; | ||
145 | break; | ||
146 | } | ||
147 | } | ||
148 | |||
149 | if (!done) | ||
150 | { | ||
151 | throw new XmlException(); | ||
152 | } | ||
153 | } | ||
154 | |||
155 | return tableDefinitions; | ||
156 | } | ||
157 | } | ||
158 | } | ||
159 | } | ||