-
Notifications
You must be signed in to change notification settings - Fork 0
/
CachedDatabase.cs
58 lines (48 loc) · 1.92 KB
/
CachedDatabase.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
namespace SDF_comparator {
//FIXME: Expose a full IDictionary interface rather than IEnumerable?
class CachedDatabase : IEnumerable<CachedTable> {
public string Filepath { get; private set; }
public SqlCeConnection Connection { get; private set; }
private Dictionary<string, CachedTable> tables;
public CachedTable this[string table_name] => tables[table_name];
public CachedDatabase(string filepath) {
Filepath = filepath;
Connection = new SqlCeConnection($"Data Source = {Filepath};");
tables = new Dictionary<string, CachedTable>();
cache_tables();
}
private void cache_tables() {
try {
tables.Clear();
Connection.Open();
SqlCeCommand cmd = Connection.CreateCommand();
cmd.CommandText = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'TABLE';";
var rdr = cmd.ExecuteReader();
while (rdr.Read()) {
var name = (string)rdr.GetValue(0);
tables.Add(name, new CachedTable(this, name));
}
} finally {
Connection.Close();
}
}
public bool TryGetValue(string table_name, out CachedTable output) {
return tables.TryGetValue(table_name, out output);
}
public bool ContainsKey(string table_name) {
return tables.ContainsKey(table_name);
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
public IEnumerator<CachedTable> GetEnumerator() {
return tables.Values.GetEnumerator();
}
}
}