blob: 6305a9de3c9c4c6854ad83034d5474321f194774 (
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
|
// 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.Core.Native.Msi
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
/// <summary>
/// Wrapper class for MSI API views.
/// </summary>
public sealed class View : MsiHandle
{
/// <summary>
/// Constructor that creates a view given a database handle and a query.
/// </summary>
/// <param name="db">Handle to the database to run the query on.</param>
/// <param name="query">Query to be executed.</param>
public View(Database db, string query)
{
if (null == db)
{
throw new ArgumentNullException(nameof(db));
}
if (null == query)
{
throw new ArgumentNullException(nameof(query));
}
var error = MsiInterop.MsiDatabaseOpenView(db.Handle, query, out var handle);
if (0 != error)
{
throw new MsiException(error);
}
this.Handle = handle;
}
/// <summary>
/// Enumerator that automatically disposes of the retrieved Records.
/// </summary>
public IEnumerable<Record> Records => new ViewEnumerable(this);
/// <summary>
/// Executes a view with no customizable parameters.
/// </summary>
public void Execute()
{
this.Execute(null);
}
/// <summary>
/// Executes a query substituing the values from the records into the customizable parameters
/// in the view.
/// </summary>
/// <param name="record">Record containing parameters to be substituded into the view.</param>
public void Execute(Record record)
{
var error = MsiInterop.MsiViewExecute(this.Handle, null == record ? 0 : record.Handle);
if (0 != error)
{
throw new MsiException(error);
}
}
/// <summary>
/// Fetches the next row in the view.
/// </summary>
/// <returns>Returns the fetched record; otherwise null.</returns>
public Record Fetch()
{
var error = MsiInterop.MsiViewFetch(this.Handle, out var recordHandle);
if (259 == error)
{
return null;
}
else if (0 != error)
{
throw new MsiException(error);
}
return new Record(recordHandle);
}
/// <summary>
/// Updates a fetched record.
/// </summary>
/// <param name="type">Type of modification mode.</param>
/// <param name="record">Record to be modified.</param>
public void Modify(ModifyView type, Record record)
{
var error = MsiInterop.MsiViewModify(this.Handle, Convert.ToInt32(type, CultureInfo.InvariantCulture), record.Handle);
if (0 != error)
{
throw new MsiException(error);
}
}
/// <summary>
/// Get the column names in a record.
/// </summary>
/// <returns></returns>
public Record GetColumnNames()
{
return this.GetColumnInfo(MsiInterop.MSICOLINFONAMES);
}
/// <summary>
/// Get the column types in a record.
/// </summary>
/// <returns></returns>
public Record GetColumnTypes()
{
return this.GetColumnInfo(MsiInterop.MSICOLINFOTYPES);
}
/// <summary>
/// Returns a record containing column names or definitions.
/// </summary>
/// <param name="columnType">Specifies a flag indicating what type of information is needed. Either MSICOLINFO_NAMES or MSICOLINFO_TYPES.</param>
/// <returns>The record containing information about the column.</returns>
public Record GetColumnInfo(int columnType)
{
var error = MsiInterop.MsiViewGetColumnInfo(this.Handle, columnType, out var recordHandle);
if (0 != error)
{
throw new MsiException(error);
}
return new Record(recordHandle);
}
private class ViewEnumerable : IEnumerable<Record>
{
private readonly View view;
public ViewEnumerable(View view) => this.view = view;
public IEnumerator<Record> GetEnumerator() => new ViewEnumerator(this.view);
IEnumerator IEnumerable.GetEnumerator() => new ViewEnumerator(this.view);
}
private class ViewEnumerator : IEnumerator<Record>
{
private readonly View view;
private readonly List<Record> records = new List<Record>();
private int position = -1;
private bool disposed;
public ViewEnumerator(View view) => this.view = view;
public Record Current => this.records[this.position];
object IEnumerator.Current => this.records[this.position];
public bool MoveNext()
{
if (this.position + 1 >= this.records.Count)
{
var record = this.view.Fetch();
if (record == null)
{
return false;
}
this.records.Add(record);
this.position = this.records.Count - 1;
}
else
{
++this.position;
}
return true;
}
public void Reset() => this.position = -1;
public void Dispose()
{
this.Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
foreach (var record in this.records)
{
record.Dispose();
}
}
this.disposed = true;
}
}
}
}
}
|