-
Notifications
You must be signed in to change notification settings - Fork 15
/
Extensions.cs
153 lines (123 loc) · 4.27 KB
/
Extensions.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Xml;
using LINQPad.Extensibility.DataContext;
using Microsoft.OData.Edm;
using Microsoft.OData.Edm.Csdl;
namespace OData4.LINQPadDriver
{
internal static class Extensions
{
public static ConnectionProperties GetConnectionProperties(this IConnectionInfo connectionInfo)
{
return new ConnectionProperties(connectionInfo);
}
public static IEdmModel GetModel(this ConnectionProperties properties)
{
var uri = properties.Uri;
uri += uri.EndsWith("/") ? "$metadata" : "/$metadata";
var settings = new XmlReaderSettings
{
XmlResolver = new XmlUrlResolver
{
Credentials = properties.GetCredentials(),
Proxy = properties.GetWebProxy()
}
};
using var reader = XmlReader.Create(uri, settings);
var model = CsdlReader.Parse(reader);
return model;
}
public static List<ExplorerItem> GetSchema(this IEdmModel model)
{
var schema = new List<ExplorerItem>();
var sets = model.EntityContainer
.EntitySets()
.OrderBy(o => o.Name)
.Select(o => new ExplorerItem(o.Name, ExplorerItemKind.QueryableObject, ExplorerIcon.Table)
{
IsEnumerable = true,
ToolTipText = o.EntityType().Name,
DragText = o.Name,
Tag = o.EntityType()
})
.ToList();
foreach (var set in sets)
{
var parentType = (IEdmEntityType)set.Tag;
var children = new List<ExplorerItem>();
for (; parentType != null; parentType = parentType.BaseEntityType())
{
children.AddRange(parentType.DeclaredStructuralProperties().Select(o => GetStructuralChildItem(parentType, o)));
children.AddRange(parentType.DeclaredNavigationProperties().Select(o => GetNavigationChildItem(o, sets)));
}
set.Children = children;
}
schema.AddRange(sets);
var actions = model.SchemaElements
.OfType<IEdmAction>()
.Select(o => new ExplorerItem(o.Name, ExplorerItemKind.QueryableObject, ExplorerIcon.ScalarFunction));
schema.AddRange(actions);
return schema;
}
private static ExplorerItem GetStructuralChildItem(IEdmEntityType parentType, IEdmStructuralProperty property)
{
var icon = parentType.HasDeclaredKeyProperty(property)
? ExplorerIcon.Key
: ExplorerIcon.Column;
var name = $"{property.Name} ({property.Type.GetTypeName()})";
var item = new ExplorerItem(name, ExplorerItemKind.Property, icon)
{
DragText = property.Name
};
return item;
}
private static ExplorerItem GetNavigationChildItem(IEdmNavigationProperty property, List<ExplorerItem> schema)
{
var partnerType = property.ToEntityType();
var backReferenceType = partnerType.DeclaredNavigationProperties()
.FirstOrDefault(o => o.ToEntityType() == property.DeclaringEntityType());
var isCollection = property.Type.IsCollection();
var kind = isCollection ? ExplorerItemKind.CollectionLink : ExplorerItemKind.ReferenceLink;
ExplorerIcon icon;
if (backReferenceType == null)
icon = ExplorerIcon.Column;
else if (isCollection)
icon = backReferenceType.Type.IsCollection() ? ExplorerIcon.ManyToMany : ExplorerIcon.OneToMany;
else
icon = backReferenceType.Type.IsCollection() ? ExplorerIcon.ManyToOne : ExplorerIcon.OneToOne;
var item = new ExplorerItem(property.Name, kind, icon)
{
ToolTipText = partnerType.Name,
HyperlinkTarget = schema.FirstOrDefault(a => (IEdmEntityType)a.Tag == partnerType),
DragText = property.Name
};
return item;
}
private static string GetTypeName(this IEdmTypeReference type)
{
if (!(type.Definition is IEdmCollectionType edmCollectionType))
{
return (type.Definition as IEdmNamedElement)?.Name;
}
return !(edmCollectionType.ElementType.Definition is IEdmNamedElement element1) ? null : $"Collection({element1.Name})";
}
public static X509Certificate GetClientCertificate(this ConnectionProperties properties)
{
if (string.IsNullOrWhiteSpace(properties.ClientCertificateFile)) return null;
X509Certificate cert;
try
{
cert = X509Certificate.CreateFromCertFile(properties.ClientCertificateFile);
}
catch (Exception)
{
// todo: we can't load certificate file
cert = null;
}
return cert;
}
}
}