aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Data.WindowsInstaller/TableDefinition.cs
blob: 40aaac84717ce0b835de41287a2c942304672257 (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
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
// 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.Data
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Text;
    using System.Xml;

    /// <summary>
    /// Definition of a table in a database.
    /// </summary>
    public sealed class TableDefinition : IComparable<TableDefinition>
    {
        /// <summary>
        /// Tracks the maximum number of columns supported in a real table.
        /// This is a Windows Installer limitation.
        /// </summary>
        public const int MaxColumnsInRealTable = 32;

        /// <summary>
        /// Creates a table definition.
        /// </summary>
        /// <param name="name">Name of table to create.</param>
        /// <param name="createSymbols">Flag if rows in this table create symbols.</param>
        /// <param name="unreal">Flag if table is unreal.</param>
        /// <param name="bootstrapperApplicationData">Flag if table is part of UX Manifest.</param>
        public TableDefinition(string name, IList<ColumnDefinition> columns, bool createSymbols, bool unreal, bool bootstrapperApplicationData = false)
        {
            this.Name = name;
            this.CreateSymbols = createSymbols;
            this.Unreal = unreal;
            this.BootstrapperApplicationData = bootstrapperApplicationData;

            this.Columns = new ReadOnlyCollection<ColumnDefinition>(columns);
        }

        /// <summary>
        /// Gets if rows in this table create symbols.
        /// </summary>
        /// <value>Flag if rows in this table create symbols.</value>
        public bool CreateSymbols { get; private set; }

        /// <summary>
        /// Gets the name of the table.
        /// </summary>
        /// <value>Name of the table.</value>
        public string Name { get; private set; }

        /// <summary>
        /// Gets if the table is unreal.
        /// </summary>
        /// <value>Flag if table is unreal.</value>
        public bool Unreal { get; private set; }

        /// <summary>
        /// Gets if the table is a part of the bootstrapper application data manifest.
        /// </summary>
        /// <value>Flag if table is a part of the bootstrapper application data manifest.</value>
        public bool BootstrapperApplicationData { get; private set; }

        /// <summary>
        /// Gets the collection of column definitions for this table.
        /// </summary>
        /// <value>Collection of column definitions for this table.</value>
        public IList<ColumnDefinition> Columns { get; private set; }

        /// <summary>
        /// Gets the column definition in the table by index.
        /// </summary>
        /// <param name="columnIndex">Index of column to locate.</param>
        /// <value>Column definition in the table by index.</value>
        public ColumnDefinition this[int columnIndex]
        {
            get { return this.Columns[columnIndex]; }
        }

        /// <summary>
        /// Gets the table definition in IDT format.
        /// </summary>
        /// <param name="keepAddedColumns">Whether to keep columns added in a transform.</param>
        /// <returns>Table definition in IDT format.</returns>
        public string ToIdtDefinition(bool keepAddedColumns)
        {
            bool first = true;
            StringBuilder columnString = new StringBuilder();
            StringBuilder dataString = new StringBuilder();
            StringBuilder tableString = new StringBuilder();

            tableString.Append(this.Name);
            foreach (ColumnDefinition column in this.Columns)
            {
                // conditionally keep columns added in a transform; otherwise,
                // break because columns can only be added at the end
                if (column.Added && !keepAddedColumns)
                {
                    break;
                }

                if (!first)
                {
                    columnString.Append('\t');
                    dataString.Append('\t');
                }

                columnString.Append(column.Name);
                dataString.Append(column.IdtType);

                if (column.PrimaryKey)
                {
                    tableString.AppendFormat("\t{0}", column.Name);
                }

                first = false;
            }
            columnString.Append("\r\n");
            columnString.Append(dataString);
            columnString.Append("\r\n");
            columnString.Append(tableString);
            columnString.Append("\r\n");

            return columnString.ToString();
        }

        /// <summary>
        /// Adds the validation rows to the _Validation table.
        /// </summary>
        /// <param name="validationTable">The _Validation table.</param>
        public void AddValidationRows(Table validationTable)
        {
            foreach (ColumnDefinition columnDef in this.Columns)
            {
                Row row = validationTable.CreateRow(null);

                row[0] = this.Name;

                row[1] = columnDef.Name;

                if (columnDef.Nullable)
                {
                    row[2] = "Y";
                }
                else
                {
                    row[2] = "N";
                }

                if (columnDef.IsMinValueSet)
                {
                    row[3] = columnDef.MinValue;
                }

                if (columnDef.IsMaxValueSet)
                {
                    row[4] = columnDef.MaxValue;
                }

                row[5] = columnDef.KeyTable;

                if (columnDef.IsKeyColumnSet)
                {
                    row[6] = columnDef.KeyColumn;
                }

                if (ColumnCategory.Unknown != columnDef.Category)
                {
                    row[7] = columnDef.Category.ToString();
                }

                row[8] = columnDef.Possibilities;

                row[9] = columnDef.Description;
            }
        }

        /// <summary>
        /// Compares this table definition to another table definition.
        /// </summary>
        /// <remarks>
        /// Only Windows Installer traits are compared, allowing for updates to WiX-specific table definitions.
        /// </remarks>
        /// <param name="updated">The updated <see cref="TableDefinition"/> to compare with this target definition.</param>
        /// <returns>0 if the tables' core properties are the same; otherwise, non-0.</returns>
        public int CompareTo(TableDefinition updated)
        {
            // by definition, this object is greater than null
            if (null == updated)
            {
                return 1;
            }

            // compare the table names
            int ret = String.Compare(this.Name, updated.Name, StringComparison.Ordinal);

            // compare the column count
            if (0 == ret)
            {
                // transforms can only add columns
                ret = Math.Min(0, updated.Columns.Count - this.Columns.Count);

                // compare name, type, and length of each column
                for (int i = 0; 0 == ret && this.Columns.Count > i; i++)
                {
                    ColumnDefinition thisColumnDef = this.Columns[i];
                    ColumnDefinition updatedColumnDef = updated.Columns[i];

                    ret = thisColumnDef.CompareTo(updatedColumnDef);
                }
            }

            return ret;
        }

        /// <summary>
        /// Parses table definition from xml reader.
        /// </summary>
        /// <param name="reader">Reader to get data from.</param>
        /// <returns>The TableDefintion represented by the Xml.</returns>
        internal static TableDefinition Read(XmlReader reader)
        {
            bool empty = reader.IsEmptyElement;
            bool createSymbols = false;
            string name = null;
            bool unreal = false;
            bool bootstrapperApplicationData = false;

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

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

            List<ColumnDefinition> columns = new List<ColumnDefinition>();
            bool hasPrimaryKeyColumn = false;

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

                while (!done && reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            switch (reader.LocalName)
                            {
                                case "columnDefinition":
                                    ColumnDefinition columnDefinition = ColumnDefinition.Read(reader);
                                    columns.Add(columnDefinition);

                                    if (columnDefinition.PrimaryKey)
                                    {
                                        hasPrimaryKeyColumn = true;
                                    }
                                    break;
                                default:
                                    throw new XmlException();
                            }
                            break;
                        case XmlNodeType.EndElement:
                            done = true;
                            break;
                    }
                }

                if (!unreal && !bootstrapperApplicationData && !hasPrimaryKeyColumn)
                {
                    throw new WixException(WixDataErrors.RealTableMissingPrimaryKeyColumn(SourceLineNumber.CreateFromUri(reader.BaseURI), name));
                }

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

            TableDefinition tableDefinition = new TableDefinition(name, columns, createSymbols, unreal, bootstrapperApplicationData);
            return tableDefinition;
        }

        /// <summary>
        /// Persists an output in an XML format.
        /// </summary>
        /// <param name="writer">XmlWriter where the Output should persist itself as XML.</param>
        internal void Write(XmlWriter writer)
        {
            writer.WriteStartElement("tableDefinition", TableDefinitionCollection.XmlNamespaceUri);

            writer.WriteAttributeString("name", this.Name);

            if (this.CreateSymbols)
            {
                writer.WriteAttributeString("createSymbols", "yes");
            }

            if (this.Unreal)
            {
                writer.WriteAttributeString("unreal", "yes");
            }

            if (this.BootstrapperApplicationData)
            {
                writer.WriteAttributeString("bootstrapperApplicationData", "yes");
            }

            foreach (ColumnDefinition columnDefinition in this.Columns)
            {
                columnDefinition.Write(writer);
            }

            writer.WriteEndElement();
        }
    }
}