using System;
using System.Collections.Generic;
using System.Linq;
using SyncEngine.Configuration;
namespace SyncEngine.Access
{
/// Groups MSysIndexes rows into composite AccessIndexInfo entries.
internal static class AccessIndexGrouper
{
internal sealed class MsysIndexRow
{
public string TableName { get; set; }
public string IndexName { get; set; }
public string ColumnName { get; set; }
public int ColumnOrder { get; set; }
public bool PrimaryKey { get; set; }
public bool Unique { get; set; }
}
public static IList GroupRows(IEnumerable rows, HashSet tableSet)
{
var grouped = new Dictionary(StringComparer.OrdinalIgnoreCase);
foreach (var row in rows.OrderBy(r => r.TableName, StringComparer.OrdinalIgnoreCase)
.ThenBy(r => r.IndexName, StringComparer.OrdinalIgnoreCase)
.ThenBy(r => r.ColumnOrder))
{
if (string.IsNullOrWhiteSpace(row.TableName) ||
string.IsNullOrWhiteSpace(row.IndexName) ||
string.IsNullOrWhiteSpace(row.ColumnName) ||
!tableSet.Contains(row.TableName))
{
continue;
}
if (row.IndexName.StartsWith("~", StringComparison.Ordinal))
{
continue;
}
var key = row.TableName + "|" + row.IndexName;
if (!grouped.TryGetValue(key, out var index))
{
index = new AccessIndexInfo
{
TableName = row.TableName,
IndexName = row.IndexName,
IsPrimary = row.PrimaryKey,
IsUnique = row.Unique || row.PrimaryKey
};
grouped[key] = index;
}
index.Columns.Add(row.ColumnName);
}
return grouped.Values
.Where(i => i.Columns.Count > 0)
.ToList();
}
}
}