// // DatabaseTable.cs - represents a table in a DatabaseObjectModel // // Author: // Michael Becker // // Copyright (c) 2011-2020 Mike Becker's Software // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . using System; using System.Collections.Generic; namespace UniversalEditor.ObjectModels.Database { /// /// Represents a table in a . /// public class DatabaseTable : ICloneable { public class DatabaseTableCollection : System.Collections.ObjectModel.Collection { public DatabaseTable this[string name] { get { if (_itemsByName.ContainsKey(name)) return _itemsByName[name]; return null; } } private Dictionary _itemsByName = new Dictionary(); protected override void ClearItems() { base.ClearItems(); _itemsByName.Clear(); } protected override void InsertItem(int index, DatabaseTable item) { base.InsertItem(index, item); _itemsByName[item.Name] = item; } protected override void RemoveItem(int index) { if (_itemsByName.ContainsKey(this[index].Name)) _itemsByName.Remove(this[index].Name); base.RemoveItem(index); } } /// /// Gets or sets the name of the . /// /// The name of the . public string Name { get; set; } = String.Empty; /// /// Gets a collection of instances representing the fields (columns) in the . /// /// The fields (columns) in the . public DatabaseField.DatabaseFieldCollection Fields { get; } = new DatabaseField.DatabaseFieldCollection(); /// /// Gets a collection of instances representing the records (rows) in the . /// /// The records (rows) in the . public DatabaseRecord.DatabaseRecordCollection Records { get; } = new DatabaseRecord.DatabaseRecordCollection(); public object Clone() { DatabaseTable clone = new DatabaseTable(); clone.Name = (Name.Clone() as string); for (int i = 0; i < Fields.Count; i++) { clone.Fields.Add(Fields[i].Clone() as DatabaseField); } for (int i = 0; i < Records.Count; i++) { clone.Records.Add(Records[i].Clone() as DatabaseRecord); } return clone; } } }