forked from meancrazy/LINQPadOData4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicDriver.cs
211 lines (168 loc) · 6.4 KB
/
DynamicDriver.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
using System;
using LINQPad.Extensibility.DataContext;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.OData.Client;
using Microsoft.OData.Edm;
using OData4.LINQPadDriver.Templates;
namespace OData4.LINQPadDriver
{
public class DynamicDriver : DynamicDataContextDriver
{
private const string BaseNamespacePrefix = "LINQPad.User";
/// <summary> Assemblies, using both to compile generated code and to load into LINQPad </summary>
private static readonly string[] Assemblies =
{
"Microsoft.OData.Client.dll",
"Microsoft.OData.Core.dll",
"Microsoft.OData.Edm.dll",
"Microsoft.Spatial.dll",
};
private List<string> _namespaces;
public override string Name => "OData v4 Connection";
public override string Author => "Dmitrii Smirnov";
public override string GetConnectionDescription(IConnectionInfo connectionInfo)
{
return connectionInfo.GetConnectionProperties().Uri;
}
public override ParameterDescriptor[] GetContextConstructorParameters(IConnectionInfo connectionInfo)
{
// We need to pass the chosen URI into the DataServiceContext's constructor:
return new[] { new ParameterDescriptor("serviceRoot", "System.Uri") };
}
public override object[] GetContextConstructorArguments(IConnectionInfo connectionInfo)
{
// We need to pass the chosen URI into the DataServiceContext's constructor:
return new object[] { new Uri(connectionInfo.GetConnectionProperties().Uri) };
}
public override IEnumerable<string> GetAssembliesToAdd(IConnectionInfo connectionInfo)
{
// We need the following assembly for compilation and auto-completion:
return Assemblies;
}
public override IDbConnection GetIDbConnection(IConnectionInfo connectionInfo)
{
return null;
}
public override IEnumerable<string> GetNamespacesToAdd(IConnectionInfo connectionInfo)
{
if (_namespaces == null || _namespaces.Count == 0)
{
var model = connectionInfo.GetConnectionProperties().GetModel();
var namespaces = model.DeclaredNamespaces.Select(o => BaseNamespacePrefix + "." + o).ToList();
_namespaces = namespaces.Count > 1 ? namespaces.ToList() : new List<string>(1);
_namespaces.Add("Microsoft.OData.Client");
}
return _namespaces;
}
public override bool ShowConnectionDialog(IConnectionInfo connectionInfo, ConnectionDialogOptions dialogOptions)
{
var connectionProperties = connectionInfo.GetConnectionProperties();
// Populate the default URI with a demo data:
if (dialogOptions.IsNewConnection)
connectionProperties.Uri = "https://services.odata.org/TripPinRESTierService";
return new ConnectionDialog(connectionProperties).ShowDialog() == true;
}
public override void PreprocessObjectToWrite(ref object objectToWrite, ObjectGraphInfo info)
{
if (objectToWrite is DataServiceQuery dataServiceQuery)
{
objectToWrite = dataServiceQuery.ExecuteAsync().GetAwaiter().GetResult();
}
}
// ReSharper disable once RedundantAssignment
public override List<ExplorerItem> GetSchemaAndBuildAssembly(IConnectionInfo connectionInfo, AssemblyName assemblyToBuild, ref string nameSpace, ref string typeName)
{
var properties = connectionInfo.GetConnectionProperties();
var codeGenerator = new ODataT4CodeGenerator
{
MetadataDocumentUri = properties.Uri,
NamespacePrefix = nameSpace,
TargetLanguage = ODataT4CodeGenerator.LanguageOption.CSharp,
UseDataServiceCollection = false,
EnableNamingAlias = false,
IgnoreUnexpectedElementsAndAttributes = true,
Properties = properties,
};
var code = codeGenerator.TransformText();
BuildAssembly(code, assemblyToBuild);
var model = properties.GetModel();
typeName = GetContainerName(model);
var schema = model.GetSchema();
return schema;
}
private static void BuildAssembly(string code, AssemblyName assemblyToBuild)
{
var assemblies = new List<string>
{
typeof(IEdmModel).Assembly.Location,
typeof(DataServiceQuery).Assembly.Location,
typeof(Microsoft.Spatial.Geometry).Assembly.Location,
};
var references = GetCoreFxReferenceAssemblies()
.Concat(assemblies)
.Select(x => MetadataReference.CreateFromFile(x));
var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
var compilation = CSharpCompilation
.Create(assemblyToBuild.FullName)
.WithOptions(options)
.AddReferences(references)
.AddSyntaxTrees(CSharpSyntaxTree.ParseText(code));
using var fileStream = File.OpenWrite(assemblyToBuild.CodeBase);
var results = compilation.Emit(fileStream);
if (results.Success)
{
return;
}
var msg = results
.Diagnostics
.Aggregate("Can't compile typed context:", (s, e) => s + Environment.NewLine + e.GetMessage());
throw new Exception(msg);
}
/// <summary> Get main schema container name for given service uri </summary>
/// <param name="model">Entity Data Model</param>
/// <returns>Container name</returns>
private static string GetContainerName(IEdmModel model)
{
var root = model.EntityContainer;
// FIX ?
var containerName = model.DeclaredNamespaces.Count() > 1 ? root.FullName() : root.Name;
return containerName;
}
public override void InitializeContext(IConnectionInfo connectionInfo, object context, QueryExecutionManager executionManager)
{
var dsContext = (DataServiceContext)context;
var properties = connectionInfo.GetConnectionProperties();
dsContext.Credentials = properties.GetCredentials();
dsContext.Configurations.RequestPipeline.OnMessageCreating += args =>
{
var message = new CustomizedRequestMessage(args, properties);
return message;
};
var writer = executionManager.SqlTranslationWriter;
if (writer == null) // we in lprun and haven't log writer
return;
dsContext.SendingRequest2 += (s, e) =>
{
writer.WriteLine($"URL:\t\t{e.RequestMessage.Url}");
if (properties.LogMethod)
writer.WriteLine($"Method:\t{e.RequestMessage.Method}");
if (properties.LogHeaders)
{
writer.WriteLine("Headers:");
var headers = string.Join("\r\n", e.RequestMessage.Headers.Select(o => $"\t{o.Key}:{o.Value}"));
writer.WriteLine(headers);
}
};
}
public override bool AreRepositoriesEquivalent(IConnectionInfo r1, IConnectionInfo r2)
{
return Equals(r1.DriverData.Element("Uri"), r2.DriverData.Element("Uri"));
}
}
}