blob: 95176ea0d299611945d0dcdcb88b4bc191cde0b1 (
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
|
// 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.Collections.Generic;
using System.Text;
/// <summary>
/// Contains information about all the tables in a Windows Installer database.
/// </summary>
public class TableCollection : ICollection<TableInfo>
{
private Database db;
internal TableCollection(Database db)
{
this.db = db;
}
/// <summary>
/// Gets the number of tables in the database.
/// </summary>
public int Count
{
get
{
return this.GetTables().Count;
}
}
/// <summary>
/// Gets a boolean value indicating whether the collection is read-only.
/// A TableCollection is read-only when the database is read-only.
/// </summary>
/// <value>read-only status of the collection</value>
public bool IsReadOnly
{
get
{
return this.db.IsReadOnly;
}
}
/// <summary>
/// Gets information about a given table.
/// </summary>
/// <param name="table">case-sensitive name of the table</param>
/// <returns>information about the requested table, or null if the table does not exist in the database</returns>
public TableInfo this[string table]
{
get
{
if (String.IsNullOrEmpty(table))
{
throw new ArgumentNullException("table");
}
if (!this.Contains(table))
{
return null;
}
return new TableInfo(this.db, table);
}
}
/// <summary>
/// Adds a new table to the database.
/// </summary>
/// <param name="item">information about the table to be added</param>
/// <exception cref="InvalidOperationException">a table with the same name already exists in the database</exception>
public void Add(TableInfo item)
{
if (item == null)
{
throw new ArgumentNullException("item");
}
if (this.Contains(item.Name))
{
throw new InvalidOperationException();
}
this.db.Execute(item.SqlCreateString);
}
/// <summary>
/// Removes all tables (and all data) from the database.
/// </summary>
public void Clear()
{
foreach (string table in this.GetTables())
{
this.Remove(table);
}
}
/// <summary>
/// Checks if the database contains a table with the given name.
/// </summary>
/// <param name="item">case-sensitive name of the table to search for</param>
/// <returns>True if the table exists, false otherwise.</returns>
public bool Contains(string item)
{
if (String.IsNullOrEmpty(item))
{
throw new ArgumentNullException("item");
}
uint ret = RemotableNativeMethods.MsiDatabaseIsTablePersistent((int) this.db.Handle, item);
if (ret == 3) // MSICONDITION_ERROR
{
throw new InstallerException();
}
return ret != 2; // MSICONDITION_NONE
}
bool ICollection<TableInfo>.Contains(TableInfo item)
{
return this.Contains(item.Name);
}
/// <summary>
/// Copies the table information 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(TableInfo[] array, int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException("array");
}
foreach (string table in this.GetTables())
{
array[arrayIndex++] = new TableInfo(this.db, table);
}
}
/// <summary>
/// Removes a table from the database.
/// </summary>
/// <param name="item">case-sensitive name of the table to be removed</param>
/// <returns>true if the table was removed, false if the table did not exist</returns>
public bool Remove(string item)
{
if (String.IsNullOrEmpty(item))
{
throw new ArgumentNullException("item");
}
if (!this.Contains(item))
{
return false;
}
this.db.Execute("DROP TABLE `{0}`", item);
return true;
}
bool ICollection<TableInfo>.Remove(TableInfo item)
{
if (item == null)
{
throw new ArgumentNullException("item");
}
return this.Remove(item.Name);
}
/// <summary>
/// Enumerates the tables in the database.
/// </summary>
public IEnumerator<TableInfo> GetEnumerator()
{
foreach (string table in this.GetTables())
{
yield return new TableInfo(this.db, table);
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
private IList<string> GetTables()
{
return this.db.ExecuteStringQuery("SELECT `Name` FROM `_Tables`");
}
}
}
|