using Sarand.Net.Standard.Base.Utils; using System; using System.Collections.Generic; using System.Linq; namespace Sarand.Net.Standard.Base.Dbas { //======================================================================= //[Serializable()] public class DbDfnTable { //------------------------------------------------------------------- // Data members. private readonly List columns = new List(); private readonly List indices = new List(); //------------------------------------------------------------------- // Construction. public DbDfnTable() { } public DbDfnTable(String schema, String name, String remarks) { this.Schema = schema; this.Name = name; this.Remarks = remarks; } //------------------------------------------------------------------- // Properties. public String Schema { get; set; } = String.Empty; public String Name { get; set; } = String.Empty; public String QualifiedName { get { return (!StringUtil.IsEmpty(Schema) ? Schema + "." : "") + Name; } } public String QuotedQualifiedName { get { return (!StringUtil.IsEmpty(Schema) ? "[" + Schema + "]." : "") + "[" + Name + "]"; } } public String Remarks { get; set; } = String.Empty; //------------------------------------------------------------------- // Comparison. public bool Matches(DbDfnTable table) { return (this.Schema.EqualsCI(table.Schema) && this.Name.EqualsCI(table.Name)); } public bool MatchesSchemaName(String schema, String name) { return (this.Schema.EqualsCI(schema) && this.Name.EqualsCI(name)); } public bool MatchesName(String name) { return (this.Name.EqualsCI(name)); } //------------------------------------------------------------------- // Utilities. public void ChangeTableName(String newName) { this.Name = newName; foreach (DbDfnColumn columnDfn in Columns) columnDfn.Table = newName; foreach (DbDfnIndex indexDfn in Indices) indexDfn.Table = newName; } //------------------------------------------------------------------- // Columns. public bool HasColumnByName(String columnName) { lock (columns) return columns.Exists(col => col.Name.EqualsCI(columnName)); } public bool HasColumns() { lock (columns) return columns.Any(); } public int ColumnCount { get { lock (columns) return columns.Count; } } public List Columns { get { lock (columns) return columns.ToList(); } set { lock (columns) columns.Clear(); columns.AddRange(value); } } public void CearColumns() { lock (columns) columns.Clear(); } public void AddColumn(DbDfnColumn column) { lock (columns) columns.Add(column); } public List GetPrimaryColumnNames() { List primaryColumnNames = new List(); // Identify primary index. var index = Indices.GetPrimaryIndex(); if (index == null) index = indices.GetClusteredIndex(); if (index != null) { foreach (var column in index.Columns) primaryColumnNames.Add(column.Name); } if (primaryColumnNames.IsEmpty()) throw new NotSupportedException("Table " + Name + " has no primary columns defined."); return primaryColumnNames; } //------------------------------------------------------------------- // Indices. public bool HasIndexByName(String indexName) { lock (indices) return indices.Exists(idx => idx.Name.EqualsCI(indexName)); } public bool HasIndices() { lock (indices) return indices.Any(); } public int IndexCount { get { lock (indices) return indices.Count; } } public List Indices { get { lock (indices) return indices.ToList(); } set { lock (indices) indices.Clear(); indices.AddRange(value); } } public DbDfnIndex GetIndexByName(String name) { lock (indices) return indices.GetIndexByName(name); } public void CearIndices() { lock (indices) indices.Clear(); } public void AddIndex(DbDfnIndex Index) { lock (indices) indices.Add(Index); } //------------------------------------------------------------------- } //======================================================================= // Table list extensions. public static class DbDfnTableList { //------------------------------------------------------------------- /// List of table names. public static List GetNames(this IEnumerable tables) { List names = new List(); foreach (DbDfnTable table in tables) { names.Add(table.Name); } return names; } //------------------------------------------------------------------- /// Extract the table definition by schema and table name. public static DbDfnTable GetTableBySchemaName(this IEnumerable tables, String schema, String name) { foreach (DbDfnTable table in tables) { if (table.MatchesSchemaName(schema, name)) return table; } return null; } /// Extract the table definition by table name. public static DbDfnTable GetTableByName(this IEnumerable tables, String name) { foreach (DbDfnTable table in tables) { if (table.MatchesName(name)) return table; } return null; } //------------------------------------------------------------------- /// Get total number of columns for all tables. public static int GetColumnCount(this IEnumerable tables) { int columnCount = 0; foreach (DbDfnTable table in tables) columnCount += table.Columns.Count; return columnCount; } //------------------------------------------------------------------- } //======================================================================= }