-
Notifications
You must be signed in to change notification settings - Fork 1
/
ListTableIndex.cs
45 lines (41 loc) · 1.38 KB
/
ListTableIndex.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using Dapper;
using System;
using System.Collections.Generic;
using System.Text;
namespace Albatross.Database.SqlServer {
public class ListTableIndex : IListTableIndex {
IGetDbConnection getDbConnection;
IListTableIndexColumn listTableIndexColumn;
public ListTableIndex(IGetDbConnection getDbConnection, IListTableIndexColumn listTableIndexColumn) {
this.getDbConnection = getDbConnection;
this.listTableIndexColumn = listTableIndexColumn;
}
public IEnumerable<Index> List(Table table) {
IEnumerable<Index> items;
using (var db = getDbConnection.Get(table.Database)) {
items = db.Query<Index>(GetCommand(table.Schema, table.Name));
}
foreach (var item in items) {
item.Table = table;
item.Columns = listTableIndexColumn.List(item);
}
return items;
}
CommandDefinition GetCommand(string schema, string table) {
return new CommandDefinition(@"
select
indexes.name,
indexes.type,
indexes.type_desc as TypeDesc,
indexes.is_disabled as IsDisabled,
indexes.is_unique as IsUnique,
indexes.is_primary_key as IsPrimaryKey,
indexes.is_unique_constraint as IsUniqueConstraint
from sys.indexes
join sys.tables on tables.object_id = indexes.object_id
join sys.schemas on schemas.schema_id = tables.schema_id
where tables.name = @table and schemas.name = @schema
", new { schema = schema, table = table, });
}
}
}