This repository has been archived by the owner on Dec 21, 2021. It is now read-only.
forked from FransBouma/Massive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMassiveTableFactory.cs
54 lines (52 loc) · 2.38 KB
/
MassiveTableFactory.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
using System;
using System.Collections.Generic;
using System.Dynamic;
using Massive;
namespace BBM.Web.Data
{
public class MassiveTableFactory : DynamicObject
{
private const String BASIC_CONNECTION = "CONNECTION";
protected virtual String SqlTableScript { get { return "SELECT ccu.TABLE_NAME [Table], COLUMN_NAME [Key], Cast(COLUMNPROPERTY(object_id(tc.TABLE_NAME), COLUMN_NAME, 'IsIdentity') as bit) IsIdentity FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu ON tc.CONSTRAINT_NAME = ccu.Constraint_name WHERE tc.CONSTRAINT_TYPE = 'Primary Key' order by ccu.TABLE_NAME"; } }
private IDictionary<String, dynamic> _tableDefinitions;
private IDictionary<String, DynamicModel> _cachedDefinitions;
private readonly String _connectionName;
public MassiveTableFactory(String connectionName)
{
_connectionName = connectionName;
InitializeDatabase();
}
protected void InitializeDatabase()
{
_cachedDefinitions = new Dictionary<string, DynamicModel>();
_tableDefinitions = new Dictionary<String, dynamic>();
var massive = new DynamicModel(_connectionName);
var tableDefinitions = massive.Query(SqlTableScript);
foreach (dynamic definition in tableDefinitions)
{
_tableDefinitions[definition.Table.ToUpper()] = definition;
}
}
public override sealed bool TryGetMember(GetMemberBinder binder, out object result)
{
var keyName = binder.Name.ToUpper();
if (_cachedDefinitions.ContainsKey(keyName))
{
result = _cachedDefinitions[keyName];
return true;
}
if (keyName == BASIC_CONNECTION)
{
result = _cachedDefinitions[keyName] = new DynamicModel(_connectionName);
return true;
}
if (_tableDefinitions.ContainsKey(keyName))
{
var definition = _tableDefinitions[keyName];
result = _cachedDefinitions[keyName] = new DynamicModel(_connectionName, definition.Table, definition.Key, "", definition.IsIdentity);
return true;
}
throw new InvalidOperationException("Table, " + binder.Name + ", does not exist.");
}
}
}