-
Notifications
You must be signed in to change notification settings - Fork 137
/
SwisMetaDataProvider.cs
86 lines (71 loc) · 2.9 KB
/
SwisMetaDataProvider.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
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using SwqlStudio.Metadata;
namespace SwqlStudio
{
class SwisMetaDataProvider : IMetadataProvider
{
private readonly ConnectionInfo info;
private Dictionary<string, Entity> entities = new Dictionary<string, Entity>();
public SwisMetaDataProvider(ConnectionInfo info)
{
this.info = info;
Name = info.Title;
}
public void Refresh()
{
const string queryTemplate =
@"SELECT Entity.FullName, Entity.Namespace, Entity.BaseType, (Entity.Type ISA 'System.Indication') AS IsIndication,
Entity.Properties.Name, Entity.Properties.Type, Entity.Properties.IsNavigable, Entity.Properties.IsInherited, Entity.Properties.IsKey,
Entity.Verbs.EntityName, Entity.Verbs.Name, Entity.IsAbstract{0}
FROM Metadata.Entity";
const string crudFragment = ", Entity.CanCreate, Entity.CanDelete, Entity.CanInvoke, Entity.CanRead, Entity.CanUpdate";
string query = string.Format(queryTemplate, SupportsAccessControl() ? crudFragment : string.Empty);
entities = info.Query<Entity>(query).ToDictionary(entity => entity.FullName);
foreach (var entity in entities.Values)
{
Entity baseEntity;
if (entity.BaseType != null && entities.TryGetValue(entity.BaseType, out baseEntity))
entity.BaseEntity = baseEntity;
}
}
public bool SupportsAccessControl()
{
const string query = @"SELECT Name
FROM Metadata.Property
WHERE EntityName='Metadata.Entity' AND Name='CanCreate'";
DataTable dt = info.Query(query);
return dt != null && dt.Rows.Count == 1;
}
public IEnumerable<VerbArgument> GetVerbArguments(Verb verb)
{
return info.Query<VerbArgument>(
string.Format(
"SELECT VerbArgument.Name, VerbArgument.Type, VerbArgument.Position " +
(XmlTemplateSupported ? ", VerbArgument.XmlTemplate " : "") +
"FROM Metadata.VerbArgument WHERE VerbArgument.EntityName='{0}' AND VerbArgument.VerbName='{1}' " +
"ORDER BY VerbArgument.Position",
verb.EntityName, verb.Name));
}
protected bool XmlTemplateSupported
{
get { return entities["Metadata.VerbArgument"].Properties.Any(p => p.Name == "XmlTemplate"); }
}
public ConnectionInfo ConnectionInfo
{
get { return info; }
}
public string Name { get; private set; }
public IEnumerable<Entity> Tables
{
get
{
if (entities == null)
Refresh();
return entities.Values.OrderBy( t => t.FullName );
}
}
}
}