-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCxInfoHelper.cs
291 lines (270 loc) · 10.7 KB
/
CxInfoHelper.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
////////////////////////////////////////////////////////////////////////////////////////////////////////
// LLBLGen Pro LINQPad driver is (c) 2002-2012 Solutions Design. All rights reserved.
// http://www.llblgen.com
////////////////////////////////////////////////////////////////////////////////////////////////////////
// COPYRIGHTS:
// Copyright (c)2002-2012 Solutions Design. All rights reserved.
// http://www.llblgen.com
//
// The LLBLGen Pro LINQPad driver sourcecode is released under the following license (BSD2):
// ----------------------------------------------------------------------
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1) Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
// 2) Redistributions in binary form must reproduce the above copyright notice, this list of
// conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY SOLUTIONS DESIGN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOLUTIONS DESIGN OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the authors
// and should not be interpreted as representing official policies, either expressed or implied,
// of Solutions Design.
//
//////////////////////////////////////////////////////////////////////
// Contributers to the code:
// - Frans Bouma [FB]
//////////////////////////////////////////////////////////////////////
// Special thanks to:
// - Jeremy Thomas
//////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LINQPad.Extensibility.DataContext;
using System.Xml.Linq;
using System.Xml;
using SD.LLBLGen.Pro.LinqSupportClasses;
using System.Reflection;
using System.Diagnostics;
namespace SD.LLBLGen.Pro.LINQPadDriver7Plus
{
/// <summary>
/// Class which contains helper logic to work with the CxInfo object.
/// </summary>
internal class CxInfoHelper
{
/// <summary>
/// Creates a new element in _cxInfo.DriverData with the name specified.
/// </summary>
/// <param name="cxInfo">The cx info.</param>
/// <param name="elementName">Name of the element.</param>
internal static void CreateDriverDataElement(IConnectionInfo cxInfo, string elementName)
{
if(cxInfo.DriverData.Element(elementName) == null)
{
cxInfo.DriverData.Add(new XElement(elementName) { Value = string.Empty });
}
}
/// <summary>
/// Gets the value of the cxDriverData Xelement with the name specified
/// </summary>
/// <param name="elementName">Name of the element.</param>
/// <returns>the value of the element or string.empty if not found</returns>
internal static string GetDriverDataElementValue(IConnectionInfo cxInfo, string elementName)
{
if(cxInfo == null)
{
return string.Empty;
}
var element = cxInfo.DriverData.Element(elementName);
return element == null ? string.Empty : element.Value;
}
/// <summary>
/// Sets the value of the cxinfo.DriverData Xelement with the name specified to the value specified.
/// </summary>
/// <param name="elementName">Name of the element.</param>
/// <param name="value">The value. Has to be converted with Xmlconvert to be able to properly read it back</param>
internal static void SetDriverDataElement(IConnectionInfo cxInfo, string elementName, string value)
{
if(cxInfo == null)
{
return;
}
var element = cxInfo.DriverData.Element(elementName);
if(element != null)
{
element.Value = value;
}
}
/// <summary>
/// Gets whether ORM profiler interceptor should be enabled or not.
/// </summary>
/// <param name="cxInfo">The cx info.</param>
/// <returns></returns>
internal static bool GetEnableORMProfiler(IConnectionInfo cxInfo)
{
string value = GetDriverDataElementValue(cxInfo, DriverDataElements.EnableORMProfilerElement);
if(string.IsNullOrEmpty(value))
{
return false;
}
return XmlConvert.ToBoolean(value);
}
/// <summary>
/// Gets the template group value from the specified cxInfo
/// </summary>
/// <param name="cxInfo">The cx info.</param>
/// <returns></returns>
internal static TemplateGroup GetTemplateGroup(IConnectionInfo cxInfo)
{
if(cxInfo == null)
{
return TemplateGroup.None;
}
string rawValue = CxInfoHelper.GetDriverDataElementValue(cxInfo, DriverDataElements.TemplateGroupElement);
if(string.IsNullOrEmpty(rawValue))
{
return TemplateGroup.None;
}
return (TemplateGroup)XmlConvert.ToInt32(rawValue);
}
/// <summary>
/// Gets the entity assembly filename.
/// </summary>
/// <param name="cxInfo">The cx info.</param>
/// <param name="templateGroup">The template group.</param>
/// <returns></returns>
internal static string GetEntityAssemblyFilename(IConnectionInfo cxInfo, TemplateGroup templateGroup)
{
string assemblyFileNameTouse = string.Empty;
switch(templateGroup)
{
case TemplateGroup.None:
case TemplateGroup.SelfServicing:
assemblyFileNameTouse = CxInfoHelper.GetDriverDataElementValue(cxInfo, DriverDataElements.SelfServicingAssemblyFilenameElement);
break;
case TemplateGroup.Adapter:
assemblyFileNameTouse = CxInfoHelper.GetDriverDataElementValue(cxInfo, DriverDataElements.AdapterDBGenericAssemblyFilenameElement);
break;
}
return assemblyFileNameTouse;
}
internal static DatabaseType GetDatabaseTypeFromCxInfo(IConnectionInfo cxInfo)
{
var dbTypeAsString = CxInfoHelper.GetDriverDataElementValue(cxInfo, DriverDataElements.DatabaseTypeElement);
if(string.IsNullOrEmpty(dbTypeAsString))
{
return DatabaseType.Undefined;
}
return (DatabaseType)XmlConvert.ToInt32(dbTypeAsString);
}
/// <summary>
/// Determines using the DQE assembly referenced by the persistence assembly with the filename specified, the database type. This uses
/// a fixed set of databases supported, i.e. the ones which we support on .NET Core+ and which have a publicly available ADO.NET provider available on nuget.
/// This means MySQL isn't among the databases supported.
/// </summary>
/// <param name="cxInfo"></param>
/// <param name="persistenceAssemblyFilename"></param>
internal static void DetermineAndSetDatabaseType(IConnectionInfo cxInfo, string persistenceAssemblyFilename)
{
Assembly persistenceAssembly = DataContextDriver.LoadAssemblySafely(persistenceAssemblyFilename);
if(persistenceAssembly == null)
{
return;
}
var dqeAssembly = persistenceAssembly.GetReferencedAssemblies().FirstOrDefault(a=>a.Name.StartsWith("SD.LLBLGen.Pro.DQE"));
if(dqeAssembly == null)
{
return;
}
DatabaseType dbType = DatabaseType.Undefined;
switch(dqeAssembly.Name)
{
case "SD.LLBLGen.Pro.DQE.Access":
dbType = DatabaseType.Access;
break;
case "SD.LLBLGen.Pro.DQE.DB2":
dbType = DatabaseType.DB2;
break;
case "SD.LLBLGen.Pro.DQE.Firebird":
dbType = DatabaseType.Firebird;
break;
case "SD.LLBLGen.Pro.DQE.OracleODPNET":
dbType = DatabaseType.Oracle;
break;
case "SD.LLBLGen.Pro.DQE.PostgreSql":
dbType = DatabaseType.PostgreSQL;
break;
case "SD.LLBLGen.Pro.DQE.SqlServer":
dbType = DatabaseType.SQLServer;
break;
default:
return;
}
if(dbType == DatabaseType.Undefined)
{
return;
}
CxInfoHelper.SetDriverDataElement(cxInfo, DriverDataElements.DatabaseTypeElement, XmlConvert.ToString((int)dbType));
}
/// <summary>
/// Creates the driver data elements in cxInfo.DriverData
/// </summary>
/// <param name="cxInfo">The cx info.</param>
internal static void CreateDriverDataElements(IConnectionInfo cxInfo)
{
if(cxInfo == null)
{
return;
}
CreateDriverDataElement(cxInfo, DriverDataElements.AdapterDBGenericAssemblyFilenameElement);
CreateDriverDataElement(cxInfo, DriverDataElements.AdapterDBSpecificAssemblyFilenameElement);
CreateDriverDataElement(cxInfo, DriverDataElements.ConnectionStringElement);
CreateDriverDataElement(cxInfo, DriverDataElements.EnableORMProfilerElement);
CreateDriverDataElement(cxInfo, DriverDataElements.SelfServicingAssemblyFilenameElement);
CreateDriverDataElement(cxInfo, DriverDataElements.TemplateGroupElement);
CreateDriverDataElement(cxInfo, DriverDataElements.EntityAssemblyNamespacesElement);
CreateDriverDataElement(cxInfo, DriverDataElements.DatabaseTypeElement);
}
/// <summary>
/// Gets the ILinqMetaData type from the entity assembly.
/// </summary>
/// <param name="cxInfo">The cx info.</param>
/// <param name="entityAssemblyFilename">The entity assembly filename.</param>
/// <returns>the typename of the ILinqMetaData implementing type or the empty string if not found</returns>
/// <remarks>Sets cxInfo.CustomTypeInfo.CustomAssemblyPath</remarks>
internal static string GetILinqMetaDataTypeFromEntityAssembly(IConnectionInfo cxInfo, string entityAssemblyFilename)
{
if(cxInfo == null)
{
return string.Empty;
}
if(string.IsNullOrEmpty(entityAssemblyFilename))
{
return string.Empty;
}
cxInfo.CustomTypeInfo.CustomAssemblyPath = entityAssemblyFilename;
var linqMetaDataTypes = cxInfo.CustomTypeInfo.GetCustomTypesInAssembly(typeof(ILinqMetaData).FullName);
return linqMetaDataTypes.FirstOrDefault() ?? string.Empty;
}
/// <summary>
/// Sets the custom type info, which is the ILinqMetaData type in the specified assembly.
/// </summary>
/// <param name="cxInfo">The cx info.</param>
/// <param name="templateGroup">The template group.</param>
internal static void SetCustomTypeInfo(IConnectionInfo cxInfo, TemplateGroup templateGroup)
{
if(cxInfo==null)
{
return;
}
string assemblyFilename = GetEntityAssemblyFilename(cxInfo, templateGroup);
if(string.IsNullOrEmpty(assemblyFilename))
{
return;
}
cxInfo.CustomTypeInfo.CustomTypeName = GetILinqMetaDataTypeFromEntityAssembly(cxInfo, assemblyFilename);
}
}
}