aboutsummaryrefslogtreecommitdiff
path: root/src/TablesAndTuples/WixTableDefinition.cs
blob: baada41dabc0e74fec3e03667dbdf02c1f4d35b1 (plain)
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
using System.Collections.Generic;
using System.Linq;
using System.Xml;

namespace TablesAndTuples
{
    class WixTableDefinition
    {
        public WixTableDefinition(string name, IEnumerable<WixColumnDefinition> columns, bool unreal, string tupleDefinitionName, bool? tupleIdIsPrimaryKey)
        {
            this.Name = name;
            this.Unreal = unreal;
            this.Columns = columns?.ToArray();
            this.TupleDefinitionName = tupleDefinitionName ?? name;
            this.TupleIdIsPrimaryKey = tupleIdIsPrimaryKey ?? DeriveTupleIdIsPrimaryKey(this.Columns);
        }

        public string Name { get; }

        public string TupleDefinitionName { get; }

        public bool Unreal { get; }

        public WixColumnDefinition[] Columns { get; }

        public bool TupleIdIsPrimaryKey { get; }

        static WixTableDefinition Read(XmlReader reader)
        {
            var empty = reader.IsEmptyElement;
            string name = null;
            string tupleDefinitionName = null;
            var unreal = false;
            bool? tupleIdIsPrimaryKey = null;

            while (reader.MoveToNextAttribute())
            {
                switch (reader.LocalName)
                {
                    case "name":
                        name = reader.Value;
                        break;
                    case "tupleDefinitionName":
                        tupleDefinitionName = reader.Value;
                        break;
                    case "tupleIdIsPrimaryKey":
                        tupleIdIsPrimaryKey = reader.Value.Equals("yes");
                        break;
                    case "unreal":
                        unreal = reader.Value.Equals("yes");
                        break;
                }
            }

            if (null == name)
            {
                throw new XmlException();
            }

            var columns = new List<WixColumnDefinition>();

            // parse the child elements
            if (!empty)
            {
                var done = false;

                while (!done && reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            switch (reader.LocalName)
                            {
                                case "columnDefinition":
                                    var columnDefinition = WixColumnDefinition.Read(reader);
                                    columns.Add(columnDefinition);
                                    break;
                                default:
                                    throw new XmlException();
                            }
                            break;
                        case XmlNodeType.EndElement:
                            done = true;
                            break;
                    }
                }

                if (!done)
                {
                    throw new XmlException();
                }
            }

            return new WixTableDefinition(name, columns.ToArray(), unreal, tupleDefinitionName, tupleIdIsPrimaryKey);
        }

        static bool DeriveTupleIdIsPrimaryKey(WixColumnDefinition[] columns)
        {
            return columns[0].PrimaryKey &&
                   columns[0].Type == ColumnType.String &&
                   columns[0].Category == ColumnCategory.Identifier &&
                   !columns[0].Name.EndsWith("_") &&
                   (columns.Length == 1 || !columns.Skip(1).Any(t => t.PrimaryKey));
        }

        public static List<WixTableDefinition> LoadCollection(string inputPath)
        {
            using (var reader = XmlReader.Create(inputPath))
            {
                reader.MoveToContent();

                if ("tableDefinitions" != reader.LocalName)
                {
                    throw new XmlException();
                }

                var empty = reader.IsEmptyElement;
                var tableDefinitions = new List<WixTableDefinition>();

                while (reader.MoveToNextAttribute())
                {
                }

                // parse the child elements
                if (!empty)
                {
                    var done = false;

                    while (!done && reader.Read())
                    {
                        switch (reader.NodeType)
                        {
                            case XmlNodeType.Element:
                                switch (reader.LocalName)
                                {
                                    case "tableDefinition":
                                        tableDefinitions.Add(WixTableDefinition.Read(reader));
                                        break;
                                    default:
                                        throw new XmlException();
                                }
                                break;
                            case XmlNodeType.EndElement:
                                done = true;
                                break;
                        }
                    }

                    if (!done)
                    {
                        throw new XmlException();
                    }
                }

                return tableDefinitions;
            }
        }
    }
}