aboutsummaryrefslogtreecommitdiff
path: root/src/dtf/WixToolset.Dtf.WindowsInstaller/ColumnCollection.cs
blob: 9a452da1e0f3b702e4faf319c559a9f6890e0c5c (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
// 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.Dtf.WindowsInstaller
{
    using System;
    using System.IO;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;
    using System.Diagnostics.CodeAnalysis;

    /// <summary>
    /// Collection of column information related to a <see cref="TableInfo"/> or
    /// <see cref="View"/>.
    /// </summary>
    public sealed class ColumnCollection : ICollection<ColumnInfo>
    {
        private IList<ColumnInfo> columns;
        private string formatString;

        /// <summary>
        /// Creates a new ColumnCollection based on a specified list of columns.
        /// </summary>
        /// <param name="columns">columns to be added to the new collection</param>
        public ColumnCollection(ICollection<ColumnInfo> columns)
        {
            if (columns == null)
            {
                throw new ArgumentNullException("columns");
            }

            this.columns = new List<ColumnInfo>(columns);
        }

        /// <summary>
        /// Creates a new ColumnCollection that is associated with a database table.
        /// </summary>
        /// <param name="view">view that contains the columns</param>
        internal ColumnCollection(View view)
        {
            if (view == null)
            {
                throw new ArgumentNullException("view");
            }

            this.columns = ColumnCollection.GetViewColumns(view);
        }

        /// <summary>
        /// Gets the number of columns in the collection.
        /// </summary>
        /// <value>number of columns in the collection</value>
        public int Count
        {
            get
            {
                return this.columns.Count;
            }
        }

        /// <summary>
        /// Gets a boolean value indicating whether the collection is read-only.
        /// A ColumnCollection is read-only if it is associated with a <see cref="View"/>
        /// or a read-only <see cref="Database"/>.
        /// </summary>
        /// <value>read-only status of the collection</value>
        public bool IsReadOnly
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        /// Gets information about a specific column in the collection.
        /// </summary>
        /// <param name="columnIndex">1-based index into the column collection</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="columnIndex"/> is less
        /// than 1 or greater than the number of columns in the collection</exception>
        public ColumnInfo this[int columnIndex]
        {
            get
            {
                if (columnIndex >= 0 && columnIndex < this.columns.Count)
                {
                    return this.columns[columnIndex];
                }
                else
                {
                    throw new ArgumentOutOfRangeException("columnIndex");
                }
            }
        }

        /// <summary>
        /// Gets information about a specific column in the collection.
        /// </summary>
        /// <param name="columnName">case-sensitive name of a column collection</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="columnName"/> does
        /// not exist in the collection</exception>
        public ColumnInfo this[string columnName]
        {
            get
            {
                if (String.IsNullOrEmpty(columnName))
                {
                    throw new ArgumentNullException("columnName");
                }

                foreach (ColumnInfo colInfo in this.columns)
                {
                    if (colInfo.Name == columnName)
                    {
                        return colInfo;
                    }
                }

                throw new ArgumentOutOfRangeException("columnName");
            }
        }

        /// <summary>
        /// Not supported because the collection is read-only.
        /// </summary>
        /// <param name="item">information about the column being added</param>
        /// <exception cref="InvalidOperationException">the collection is read-only</exception>
        public void Add(ColumnInfo item)
        {
            throw new InvalidOperationException();
        }

        /// <summary>
        /// Not supported because the collection is read-only.
        /// </summary>
        /// <exception cref="InvalidOperationException">the collection is read-only</exception>
        public void Clear()
        {
            throw new InvalidOperationException();
        }

        /// <summary>
        /// Checks if a column with a given name exists in the collection.
        /// </summary>
        /// <param name="columnName">case-sensitive name of the column to look for</param>
        /// <returns>true if the column exists in the collection, false otherwise</returns>
        public bool Contains(string columnName)
        {
            return this.IndexOf(columnName) >= 0;
        }

        /// <summary>
        /// Checks if a column with a given name exists in the collection.
        /// </summary>
        /// <param name="column">column to look for, with case-sensitive name</param>
        /// <returns>true if the column exists in the collection, false otherwise</returns>
        bool ICollection<ColumnInfo>.Contains(ColumnInfo column)
        {
            return this.Contains(column.Name);
        }

        /// <summary>
        /// Gets the index of a column within the collection.
        /// </summary>
        /// <param name="columnName">case-sensitive name of the column to look for</param>
        /// <returns>0-based index of the column, or -1 if not found</returns>
        public int IndexOf(string columnName)
        {
            if (String.IsNullOrEmpty(columnName))
            {
                throw new ArgumentNullException("columnName");
            }

            for (int index = 0; index < this.columns.Count; index++)
            {
                if (this.columns[index].Name == columnName)
                {
                    return index;
                }
            }
            return -1;
        }

        /// <summary>
        /// Copies the columns from this collection into an array.
        /// </summary>
        /// <param name="array">destination array to be filed</param>
        /// <param name="arrayIndex">offset into the destination array where copying begins</param>
        public void CopyTo(ColumnInfo[] array, int arrayIndex)
        {
            if (array == null)
            {
                throw new ArgumentNullException("array");
            }

            this.columns.CopyTo(array, arrayIndex);
        }

        /// <summary>
        /// Not supported because the collection is read-only.
        /// </summary>
        /// <param name="column">column to remove</param>
        /// <returns>true if the column was removed, false if it was not found</returns>
        /// <exception cref="InvalidOperationException">the collection is read-only</exception>
        [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "column")]
        [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
        bool ICollection<ColumnInfo>.Remove(ColumnInfo column)
        {
            throw new InvalidOperationException();
        }

        /// <summary>
        /// Gets an enumerator over the columns in the collection.
        /// </summary>
        /// <returns>An enumerator of ColumnInfo objects.</returns>
        public IEnumerator<ColumnInfo> GetEnumerator()
        {
            return this.columns.GetEnumerator();
        }

        /// <summary>
        /// Gets a string suitable for printing all the values of a record containing these columns.
        /// </summary>
        public string FormatString
        {
            get
            {
                if (this.formatString == null)
                {
                    this.formatString = CreateFormatString(this.columns);
                }
                return this.formatString;
            }
        }

        private static string CreateFormatString(IList<ColumnInfo> columns)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < columns.Count; i++)
            {
                if (columns[i].Type == typeof(Stream))
                {
                    sb.AppendFormat("{0} = [Binary Data]", columns[i].Name);
                }
                else
                {
                    sb.AppendFormat("{0} = [{1}]", columns[i].Name, i + 1);
                }

                if (i < columns.Count - 1)
                {
                    sb.Append(", ");
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// Gets an enumerator over the columns in the collection.
        /// </summary>
        /// <returns>An enumerator of ColumnInfo objects.</returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        /// <summary>
        /// Creates ColumnInfo objects for the associated view.
        /// </summary>
        /// <returns>dynamically-generated list of columns</returns>
        private static IList<ColumnInfo> GetViewColumns(View view)
        {
            IList<string> columnNames = ColumnCollection.GetViewColumns(view, false);
            IList<string> columnTypes = ColumnCollection.GetViewColumns(view, true);

            int count = columnNames.Count;
            if (columnTypes[count - 1] == "O0")
            {
                // Weird.. the "_Tables" table returns a second column with type "O0" -- ignore it.
                count--;
            }

            IList<ColumnInfo> columnsList = new List<ColumnInfo>(count);
            for (int i = 0; i < count; i++)
            {
                columnsList.Add(new ColumnInfo(columnNames[i], columnTypes[i]));
            }

            return columnsList;
        }

        /// <summary>
        /// Gets a list of column names or column-definition-strings for the
        /// associated view.
        /// </summary>
        /// <param name="view">the view to that defines the columns</param>
        /// <param name="types">true to return types (column definition strings),
        /// false to return names</param>
        /// <returns>list of column names or types</returns>
        private static IList<string> GetViewColumns(View view, bool types)
        {
            int recordHandle;
            int typesFlag = types ? 1 : 0;
            uint ret = RemotableNativeMethods.MsiViewGetColumnInfo(
                (int) view.Handle, (uint) typesFlag, out recordHandle);
            if (ret != 0)
            {
                throw InstallerException.ExceptionFromReturnCode(ret);
            }

            using (Record rec = new Record((IntPtr) recordHandle, true, null))
            {
                int count = rec.FieldCount;
                IList<string> columnsList = new List<string>(count);

                // Since we must be getting all strings of limited length, 
                // this code is faster than calling rec.GetString(field).
                for (int field = 1; field <= count; field++)
                {
                    uint bufSize = 256;
                    StringBuilder buf = new StringBuilder((int) bufSize);
                    ret = RemotableNativeMethods.MsiRecordGetString((int) rec.Handle, (uint) field, buf, ref bufSize);
                    if (ret != 0)
                    {
                        throw InstallerException.ExceptionFromReturnCode(ret);
                    }
                    columnsList.Add(buf.ToString());
                }
                return columnsList;
            }
        }
    }
}