forked from olejorgensen/CompactView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InformationSchemaCache.cs
261 lines (210 loc) · 10.1 KB
/
InformationSchemaCache.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**************************************************************************
Copyright (C) 2023 Rekkonnect
This file is part of CompactView.
CompactView is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CompactView is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CompactView. If not, see <http://www.gnu.org/licenses/>.
CompactView web site <http://sourceforge.net/p/compactview/>.
**************************************************************************/
using Garyon.Extensions;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Linq;
namespace CompactView
{
public class InformationSchemaCache
{
private StringCollection tableNames;
private ListValueStringDictionary<InformationSchema.Index> indexesByTable;
private ListValueStringDictionary<InformationSchema.Column> columnsByTable;
private ListValueStringDictionary<InformationSchema.KeyColumnUsage> keyColumnUsagesByTable;
private ListValueStringDictionary<InformationSchema.TableConstraint> tableConstraintsByTable;
private ListValueStringDictionary<InformationSchema.ReferentialConstraint> referentialConstraintsByTable;
private Dictionary<string, TableConstraintInfo> tableConstraintInfoByName;
private Dictionary<string, ReferentialConstraintInfo> referentialConstraintInfoByName;
private Dictionary<QualifiedObjectName, IndexInfo> indexInfoByName;
public IEnumerable<string> TableNames
=> tableNames.Cast<string>();
public IReadOnlyDictionary<string, IReadOnlyList<InformationSchema.Index>> IndexesByTable
=> indexesByTable;
public IReadOnlyDictionary<string, IReadOnlyList<InformationSchema.Column>> ColumnsByTable
=> columnsByTable;
public IReadOnlyDictionary<string, IReadOnlyList<InformationSchema.KeyColumnUsage>> KeyColumnUsagesByTable
=> keyColumnUsagesByTable;
public IReadOnlyDictionary<string, IReadOnlyList<InformationSchema.TableConstraint>> TableConstraintsByTable
=> tableConstraintsByTable;
public IReadOnlyDictionary<string, IReadOnlyList<InformationSchema.ReferentialConstraint>> ReferentialConstraintsByTable
=> referentialConstraintsByTable;
public IReadOnlyDictionary<string, TableConstraintInfo> TableConstraintInfoByName
=> tableConstraintInfoByName;
public IReadOnlyDictionary<string, ReferentialConstraintInfo> ReferentialConstraintInfoByName
=> referentialConstraintInfoByName;
public SqlCeInformationSchemaData SchemaData { get; }
public InformationSchemaCache(SqlCeInformationSchemaData schemaData)
{
SchemaData = schemaData;
}
public IReadOnlyList<TableConstraintInfo> TableConstraintInfoForTable(string tableName)
{
return InfoForTable(
tableName,
tableConstraintsByTable,
tableConstraintInfoByName,
c => c.ConstraintName);
}
public IReadOnlyList<IndexInfo> IndexInfoForTable(string tableName)
{
var constraints = indexesByTable[tableName];
var result = new HashSet<IndexInfo>();
foreach (var info in constraints)
{
var indexInfo = indexInfoByName[info.QualifiedName];
result.Add(indexInfo);
}
return result.ToArray();
}
public IReadOnlyList<ReferentialConstraintInfo> ReferentialConstraintInfoForTable(string tableName)
{
return InfoForTable(
tableName,
referentialConstraintsByTable,
referentialConstraintInfoByName,
c => c.ConstraintName);
}
private static IReadOnlyList<TInfo> InfoForTable<TDbObject, TInfo>(
string tableName,
ListValueStringDictionary<TDbObject> dictionaryByTable,
Dictionary<string, TInfo> dictionaryByInfoName,
Func<TDbObject, string> nameGetter)
where TDbObject : class
where TInfo : class
{
var constraints = dictionaryByTable[tableName];
var result = new HashSet<TInfo>();
foreach (var info in constraints)
{
var indexInfo = dictionaryByInfoName[nameGetter(info)];
result.Add(indexInfo);
}
return result.ToArray();
}
public void Clear()
{
tableNames = null;
indexesByTable = null;
columnsByTable = null;
keyColumnUsagesByTable = null;
tableConstraintsByTable = null;
referentialConstraintsByTable = null;
tableConstraintInfoByName = null;
referentialConstraintInfoByName = null;
}
public void Load()
{
// Get table names
tableNames = new StringCollection();
var tableNameList = SchemaData.Tables.Select(d => d.TableName).ToArray();
tableNames.AddRange(tableNameList);
indexesByTable = ToListValueStringDictionary(SchemaData.Indexes, d => d.TableName);
columnsByTable = ToListValueStringDictionary(SchemaData.Columns, d => d.TableName);
keyColumnUsagesByTable = ToListValueStringDictionary(SchemaData.KeyColumnUsages, d => d.TableName);
tableConstraintsByTable = ToListValueStringDictionary(SchemaData.TableConstraints, d => d.TableName);
referentialConstraintsByTable = ToListValueStringDictionary(SchemaData.ReferentialConstraints, d => d.ConstraintTableName);
// Sort the loaded information by their ordinal positions
// to preserve DDL order
SortByOrdinalComparer(
columnsByTable,
InformationSchema.Column.OrdinalComparer.Instance);
SortByOrdinalComparer(
indexesByTable,
InformationSchema.Index.OrdinalComparer.Instance);
SortByOrdinalComparer(
keyColumnUsagesByTable,
InformationSchema.KeyColumnUsage.OrdinalComparer.Instance);
LoadTableConstraintInfo();
LoadReferentialConstraintInfo();
LoadIndexInfo();
}
private void LoadTableConstraintInfo()
{
tableConstraintInfoByName = new Dictionary<string, TableConstraintInfo>();
var keyColumnUsagesByConstraint = new ListValueStringDictionary<InformationSchema.KeyColumnUsage>();
// Flattening and serially iterating is a great option
// without bumping the structural complexity
// while not sacrificing performance
foreach (var constraint in tableConstraintsByTable.Values.Flatten())
{
var constraintInfo = new TableConstraintInfo(
constraint,
keyColumnUsagesByConstraint[constraint.ConstraintName]);
tableConstraintInfoByName.Add(constraint.ConstraintName, constraintInfo);
}
foreach (var keyColumnUsage in keyColumnUsagesByTable.Values.Flatten())
{
string constraintName = keyColumnUsage.ConstraintName;
keyColumnUsagesByConstraint[constraintName].Add(keyColumnUsage);
}
}
private void LoadReferentialConstraintInfo()
{
referentialConstraintInfoByName = new Dictionary<string, ReferentialConstraintInfo>();
var keyColumnUsagesByConstraint = new ListValueStringDictionary<InformationSchema.KeyColumnUsage>();
foreach (var constraint in referentialConstraintsByTable.Values.Flatten())
{
var tableConstraint = tableConstraintInfoByName[constraint.UniqueConstraintName];
var constraintInfo = new ReferentialConstraintInfo(
constraint,
keyColumnUsagesByConstraint[constraint.ConstraintName],
tableConstraint);
referentialConstraintInfoByName.Add(constraint.ConstraintName, constraintInfo);
}
foreach (var keyColumnUsage in keyColumnUsagesByTable.Values.Flatten())
{
string constraintName = keyColumnUsage.ConstraintName;
keyColumnUsagesByConstraint[constraintName].Add(keyColumnUsage);
}
}
private void LoadIndexInfo()
{
// Due to the way the data is stored, a LINQ
// query is very appealing
var indexInfo =
from index in indexesByTable.Values.Flatten()
group index
by new QualifiedObjectName(index.TableName, index.IndexName)
into indexGroup
select new IndexInfo(indexGroup.Key, indexGroup.ToList());
indexInfoByName = indexInfo.ToDictionary(x => x.QualifiedName);
}
private static void SortByOrdinalComparer<TEntity, TComparer>(
ListValueStringDictionary<TEntity> dictionary,
TComparer comparer)
where TComparer : IComparer<TEntity>
{
foreach (var tableColumnList in dictionary.Values)
{
tableColumnList.Sort(comparer);
}
}
private static ListValueStringDictionary<T> ToListValueStringDictionary<T>(
IReadOnlyCollection<T> collection,
Func<T, string> tableNameGetter)
where T : class
{
var dictionary = new ListValueStringDictionary<T>();
foreach (var item in collection)
dictionary.Add(tableNameGetter(item), item);
return dictionary;
}
}
}