| 1 | // 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. |
| 2 | |
| 3 | namespace WixToolset.Dtf.WindowsInstaller |
| 4 | { |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using System.Text; |
| 8 | |
| 9 | /// <summary> |
| 10 | /// Contains information about all the tables in a Windows Installer database. |
| 11 | /// </summary> |
| 12 | public class TableCollection : ICollection<TableInfo> |
| 13 | { |
| 14 | private Database db; |
| 15 | |
| 16 | internal TableCollection(Database db) |
| 17 | { |
| 18 | this.db = db; |
| 19 | } |
| 20 | |
| 21 | /// <summary> |
| 22 | /// Gets the number of tables in the database. |
| 23 | /// </summary> |
| 24 | public int Count |
| 25 | { |
| 26 | get |
| 27 | { |
| 28 | return this.GetTables().Count; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /// <summary> |
| 33 | /// Gets a boolean value indicating whether the collection is read-only. |
| 34 | /// A TableCollection is read-only when the database is read-only. |
| 35 | /// </summary> |
| 36 | /// <value>read-only status of the collection</value> |
| 37 | public bool IsReadOnly |
| 38 | { |
| 39 | get |
| 40 | { |
| 41 | return this.db.IsReadOnly; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// <summary> |
| 46 | /// Gets information about a given table. |
| 47 | /// </summary> |
| 48 | /// <param name="table">case-sensitive name of the table</param> |
| 49 | /// <returns>information about the requested table, or null if the table does not exist in the database</returns> |
| 50 | public TableInfo this[string table] |
| 51 | { |
| 52 | get |
| 53 | { |
| 54 | if (String.IsNullOrEmpty(table)) |
| 55 | { |
| 56 | throw new ArgumentNullException("table"); |
| 57 | } |
| 58 | |
| 59 | if (!this.Contains(table)) |
| 60 | { |
| 61 | return null; |
| 62 | } |
| 63 | |
| 64 | return new TableInfo(this.db, table); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /// <summary> |
| 69 | /// Adds a new table to the database. |
| 70 | /// </summary> |
| 71 | /// <param name="item">information about the table to be added</param> |
| 72 | /// <exception cref="InvalidOperationException">a table with the same name already exists in the database</exception> |
| 73 | public void Add(TableInfo item) |
| 74 | { |
| 75 | if (item == null) |
| 76 | { |
| 77 | throw new ArgumentNullException("item"); |
| 78 | } |
| 79 | |
| 80 | if (this.Contains(item.Name)) |
| 81 | { |
| 82 | throw new InvalidOperationException(); |
| 83 | } |
| 84 | |
| 85 | this.db.Execute(item.SqlCreateString); |
| 86 | } |
| 87 | |
| 88 | /// <summary> |
| 89 | /// Removes all tables (and all data) from the database. |
| 90 | /// </summary> |
| 91 | public void Clear() |
| 92 | { |
| 93 | foreach (string table in this.GetTables()) |
| 94 | { |
| 95 | this.Remove(table); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /// <summary> |
| 100 | /// Checks if the database contains a table with the given name. |
| 101 | /// </summary> |
| 102 | /// <param name="item">case-sensitive name of the table to search for</param> |
| 103 | /// <returns>True if the table exists, false otherwise.</returns> |
| 104 | public bool Contains(string item) |
| 105 | { |
| 106 | if (String.IsNullOrEmpty(item)) |
| 107 | { |
| 108 | throw new ArgumentNullException("item"); |
| 109 | } |
| 110 | uint ret = RemotableNativeMethods.MsiDatabaseIsTablePersistent((int) this.db.Handle, item); |
| 111 | if (ret == 3) // MSICONDITION_ERROR |
| 112 | { |
| 113 | throw new InstallerException(); |
| 114 | } |
| 115 | return ret != 2; // MSICONDITION_NONE |
| 116 | } |
| 117 | |
| 118 | bool ICollection<TableInfo>.Contains(TableInfo item) |
| 119 | { |
| 120 | return this.Contains(item.Name); |
| 121 | } |
| 122 | |
| 123 | /// <summary> |
| 124 | /// Copies the table information from this collection into an array. |
| 125 | /// </summary> |
| 126 | /// <param name="array">destination array to be filed</param> |
| 127 | /// <param name="arrayIndex">offset into the destination array where copying begins</param> |
| 128 | public void CopyTo(TableInfo[] array, int arrayIndex) |
| 129 | { |
| 130 | if (array == null) |
| 131 | { |
| 132 | throw new ArgumentNullException("array"); |
| 133 | } |
| 134 | |
| 135 | foreach (string table in this.GetTables()) |
| 136 | { |
| 137 | array[arrayIndex++] = new TableInfo(this.db, table); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /// <summary> |
| 142 | /// Removes a table from the database. |
| 143 | /// </summary> |
| 144 | /// <param name="item">case-sensitive name of the table to be removed</param> |
| 145 | /// <returns>true if the table was removed, false if the table did not exist</returns> |
| 146 | public bool Remove(string item) |
| 147 | { |
| 148 | if (String.IsNullOrEmpty(item)) |
| 149 | { |
| 150 | throw new ArgumentNullException("item"); |
| 151 | } |
| 152 | |
| 153 | if (!this.Contains(item)) |
| 154 | { |
| 155 | return false; |
| 156 | } |
| 157 | this.db.Execute("DROP TABLE `{0}`", item); |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | bool ICollection<TableInfo>.Remove(TableInfo item) |
| 162 | { |
| 163 | if (item == null) |
| 164 | { |
| 165 | throw new ArgumentNullException("item"); |
| 166 | } |
| 167 | |
| 168 | return this.Remove(item.Name); |
| 169 | } |
| 170 | |
| 171 | /// <summary> |
| 172 | /// Enumerates the tables in the database. |
| 173 | /// </summary> |
| 174 | public IEnumerator<TableInfo> GetEnumerator() |
| 175 | { |
| 176 | foreach (string table in this.GetTables()) |
| 177 | { |
| 178 | yield return new TableInfo(this.db, table); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() |
| 183 | { |
| 184 | return this.GetEnumerator(); |
| 185 | } |
| 186 | |
| 187 | private IList<string> GetTables() |
| 188 | { |
| 189 | return this.db.ExecuteStringQuery("SELECT `Name` FROM `_Tables`"); |
| 190 | } |
| 191 | } |
| 192 | } |