190 lines
5.1 KiB
C#
190 lines
5.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
|
|
namespace Sarand.Net.Standard.Base.Dbas
|
|
{
|
|
//=======================================================================
|
|
//[Serializable()]
|
|
public class DbDfnTable
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
private readonly List<DbDfnColumn> columns = new List<DbDfnColumn>();
|
|
private readonly List<DbDfnIndex> indices = new List<DbDfnIndex>();
|
|
|
|
//-------------------------------------------------------------------
|
|
// 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 Remarks { get; set; } = String.Empty;
|
|
|
|
//-------------------------------------------------------------------
|
|
// Comparison.
|
|
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));
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Columns.
|
|
public bool HasColumnByName(String columnName)
|
|
{
|
|
lock (columns)
|
|
return (columns != null && columns.Exists(col => col.Name.EqualsCI(columnName)));
|
|
}
|
|
public bool HasColumns()
|
|
{
|
|
lock (columns)
|
|
return (columns != null && columns.Count > 0);
|
|
}
|
|
public List<DbDfnColumn> Columns
|
|
{
|
|
get
|
|
{
|
|
lock (columns)
|
|
return new List<DbDfnColumn>(columns);
|
|
}
|
|
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<String> GetPrimaryColumnNames()
|
|
{
|
|
List<String> primaryColumnNames = new List<string>();
|
|
// 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 != null && indices.Exists(idx => idx.Name.EqualsCI(indexName)));
|
|
}
|
|
public bool HasIndices()
|
|
{
|
|
lock (indices)
|
|
return (indices != null && indices.Count > 0);
|
|
}
|
|
public List<DbDfnIndex> Indices
|
|
{
|
|
get
|
|
{
|
|
lock (indices)
|
|
return new List<DbDfnIndex>(indices);
|
|
}
|
|
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
|
|
{
|
|
//-------------------------------------------------------------------
|
|
/// <summary>List of table names.</summary>
|
|
public static List<String> GetNames(this List<DbDfnTable> tables)
|
|
{
|
|
List<String> names = new List<String>();
|
|
foreach (DbDfnTable table in tables)
|
|
{
|
|
names.Add(table.Name);
|
|
}
|
|
return names;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
/// <summary>Extract the table definition by schema and table name.</summary>
|
|
public static DbDfnTable GetTableBySchemaName(this List<DbDfnTable> tables, String schema, String name)
|
|
{
|
|
foreach (DbDfnTable table in tables)
|
|
{
|
|
if (table.MatchesSchemaName(schema, name))
|
|
return table;
|
|
}
|
|
return null;
|
|
}
|
|
/// <summary>Extract the table definition by table name.</summary>
|
|
public static DbDfnTable GetTableByName(this List<DbDfnTable> tables, String name)
|
|
{
|
|
foreach (DbDfnTable table in tables)
|
|
{
|
|
if (table.MatchesName(name))
|
|
return table;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
/// <summary>Get total number of columns for all tables.</summary>
|
|
public static int GetColumnCount(this List<DbDfnTable> tables)
|
|
{
|
|
int columnCount = 0;
|
|
foreach (DbDfnTable table in tables)
|
|
columnCount += table.Columns.Count;
|
|
return columnCount;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|