// 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; /// /// Contains information about all the tables in a Windows Installer database. /// public class TableCollection : ICollection { private Database db; internal TableCollection(Database db) { this.db = db; } /// /// Gets the number of tables in the database. /// public int Count { get { return this.GetTables().Count; } } /// /// Gets a boolean value indicating whether the collection is read-only. /// A TableCollection is read-only when the database is read-only. /// /// read-only status of the collection public bool IsReadOnly { get { return this.db.IsReadOnly; } } /// /// Gets information about a given table. /// /// case-sensitive name of the table /// information about the requested table, or null if the table does not exist in the database 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); } } /// /// Adds a new table to the database. /// /// information about the table to be added /// a table with the same name already exists in the database 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); } /// /// Removes all tables (and all data) from the database. /// public void Clear() { foreach (string table in this.GetTables()) { this.Remove(table); } } /// /// Checks if the database contains a table with the given name. /// /// case-sensitive name of the table to search for /// True if the table exists, false otherwise. 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.Contains(TableInfo item) { return this.Contains(item.Name); } /// /// Copies the table information from this collection into an array. /// /// destination array to be filed /// offset into the destination array where copying begins 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); } } /// /// Removes a table from the database. /// /// case-sensitive name of the table to be removed /// true if the table was removed, false if the table did not exist 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.Remove(TableInfo item) { if (item == null) { throw new ArgumentNullException("item"); } return this.Remove(item.Name); } /// /// Enumerates the tables in the database. /// public IEnumerator 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 GetTables() { return this.db.ExecuteStringQuery("SELECT `Name` FROM `_Tables`"); } } }