122 lines
3.4 KiB
C#
122 lines
3.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
|
|
namespace Sarand.Net.Standard.Base.Dbas
|
|
{
|
|
//=======================================================================
|
|
//[Serializable()]
|
|
public class DbDfnIndex
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Definitions.
|
|
public static readonly int SQL_TABLE_STAT = 0;
|
|
public static readonly int SQL_INDEX_CLUSTERED = 1;
|
|
public static readonly int SQL_INDEX_HASHED = 2;
|
|
public static readonly int SQL_INDEX_OTHER = 3;
|
|
// SQL_INDEX_BTREE = ???;
|
|
// SQL_INDEX_CONTENT = ???;
|
|
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
private readonly List<DbDfnIndexColumn> columns = new List<DbDfnIndexColumn>();
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
public DbDfnIndex() { }
|
|
public DbDfnIndex(String schema, String table, String name, bool unique)
|
|
{
|
|
Schema = schema;
|
|
Table = table;
|
|
Name = name;
|
|
Unique = unique;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Attributes.
|
|
public String Schema { get; set; } = String.Empty;
|
|
public String Table { get; set; } = String.Empty;
|
|
public String Name { get; set; } = String.Empty;
|
|
public int IndexType { get; set; } = -1;
|
|
public bool Unique { get; set; } = false;
|
|
public bool Primary { get; set; } = false;
|
|
public bool Clustered { get; set; } = false;
|
|
public bool Nullable { get; set; } = false;
|
|
public int Pages { get; set; } = 0;
|
|
public int UniqueValues { get; set; } = 0;
|
|
public bool HasColumns { get; set; } = false;
|
|
|
|
//-------------------------------------------------------------------
|
|
// Columns.
|
|
public int ColumnCount
|
|
{
|
|
get
|
|
{
|
|
lock (columns)
|
|
return columns.Count();
|
|
}
|
|
}
|
|
public List<DbDfnIndexColumn> 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(DbDfnIndexColumn column)
|
|
{
|
|
lock (columns)
|
|
columns.Add(column);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
|
|
//=======================================================================
|
|
// Index list extensions.
|
|
public static class DbDfnIndexList
|
|
{
|
|
// List of index names.
|
|
public static List<String> GetNames(this IEnumerable<DbDfnIndex> indices)
|
|
{
|
|
List<String> names = new List<String>();
|
|
foreach (var index in indices)
|
|
names.Add(index.Name);
|
|
return names;
|
|
}
|
|
// Locate the index in the list.
|
|
public static DbDfnIndex GetIndexByName(this IEnumerable<DbDfnIndex> indices, String name)
|
|
{
|
|
return indices.FirstOrDefault(idx => idx.Name.EqualsCI(name));
|
|
}
|
|
// Retrieve primary index.
|
|
public static DbDfnIndex GetPrimaryIndex(this IEnumerable<DbDfnIndex> indices)
|
|
{
|
|
return indices.FirstOrDefault(idx => idx.Primary);
|
|
}
|
|
// Retrieve clustered index.
|
|
public static DbDfnIndex GetClusteredIndex(this IEnumerable<DbDfnIndex> indices)
|
|
{
|
|
DbDfnIndex index = indices.FirstOrDefault(idx => idx.Clustered);
|
|
if (index == null)
|
|
index = indices.FirstOrDefault(idx => idx.IndexType == DbDfnIndex.SQL_INDEX_CLUSTERED);
|
|
return index;
|
|
}
|
|
}
|
|
|
|
//=======================================================================
|
|
}
|