Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
232 lines (198 loc) · 9.21 KB

File metadata and controls

232 lines (198 loc) · 9.21 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
using System;
using System.Collections.Concurrent;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Configuration;
using System.Data.OleDb;
using System.Reflection;
using System.IO;
using System.Text.RegularExpressions;
namespace Simple.Data.Ado
{
using Schema;
public class ProviderHelper
{
private readonly ConcurrentDictionary<ConnectionToken, IConnectionProvider> _connectionProviderCache = new ConcurrentDictionary<ConnectionToken,IConnectionProvider>();
private readonly ConcurrentDictionary<Type, object> _customProviderCache = new ConcurrentDictionary<Type, object>();
public IConnectionProvider GetProviderByConnectionString(string connectionString)
{
var token = new ConnectionToken(connectionString, string.Empty);
return _connectionProviderCache.GetOrAdd(token, LoadProviderByConnectionString);
}
public IConnectionProvider GetProviderByFilename(string filename)
{
var token = new ConnectionToken(filename, "System.Data.SqlCeClient");
return _connectionProviderCache.GetOrAdd(token, LoadProviderByFilename);
}
private IConnectionProvider LoadProviderByConnectionString(ConnectionToken token)
{
var dataSource = GetDataSourceName(token.ConnectionString);
if (dataSource.EndsWith("sdf", StringComparison.CurrentCultureIgnoreCase) && File.Exists(dataSource))
{
return GetProviderByFilename(dataSource);
}
var provider = ComposeProvider();
provider.SetConnectionString(token.ConnectionString);
return provider;
}
internal static string GetDataSourceName(string connectionString)
{
var match = Regex.Match(connectionString, @"data source=(.*?)(;|\z)");
if (match != null && match.Groups.Count > 1)
{
return match.Groups[1].Value;
}
return string.Empty;
}
private static IConnectionProvider LoadProviderByFilename(ConnectionToken token)
{
string extension = GetFileExtension(token.ConnectionString);
var provider = ComposeProvider(extension);
provider.SetConnectionString(string.Format("data source={0}", token.ConnectionString));
return provider;
}
private static string GetFileExtension(string filename)
{
var extension = Path.GetExtension(filename);
if (extension == null) throw new ArgumentException("Unrecognised file.");
return extension.TrimStart('.').ToLower();
}
private static IConnectionProvider ComposeProvider()
{
return Composer.Default.Compose<IConnectionProvider>();
}
private static IConnectionProvider ComposeProvider(string extension)
{
return Composer.Default.Compose<IConnectionProvider>(extension);
}
public IConnectionProvider GetProviderByConnectionName(string connectionName)
{
var connectionSettings = ConfigurationManager.ConnectionStrings[connectionName];
if (connectionSettings == null)
{
throw new ArgumentOutOfRangeException("connectionName");
}
return GetProviderByConnectionString(connectionSettings.ConnectionString, connectionSettings.ProviderName);
}
public IConnectionProvider GetProviderByConnectionString(string connectionString, string providerName)
{
return _connectionProviderCache.GetOrAdd(new ConnectionToken(connectionString, providerName),
LoadProviderByConnectionToken);
}
private static IConnectionProvider LoadProviderByConnectionToken(ConnectionToken token)
{
var provider = ComposeProvider(token.ProviderName);
if (provider == null)
{
throw new InvalidOperationException("Provider could not be resolved.");
}
provider.SetConnectionString(token.ConnectionString);
return provider;
}
public T GetCustomProvider<T>(IConnectionProvider connectionProvider)
{
return (T)_customProviderCache.GetOrAdd(typeof (T), t => GetCustomProviderExport<T>(connectionProvider.GetType().Assembly) ??
GetCustomProviderServiceProvider(connectionProvider as IServiceProvider, t));
}
private static Object GetCustomProviderExport<T>(Assembly assembly)
{
using (var assemblyCatalog = new AssemblyCatalog(assembly))
{
using (var container = new CompositionContainer(assemblyCatalog))
{
return container.GetExportedValueOrDefault<T>();
}
}
}
private static Object GetCustomProviderServiceProvider(IServiceProvider serviceProvider, Type type)
{
if (serviceProvider != null)
{
return serviceProvider.GetService(type);
}
return null;
}
public T GetCustomProvider<T>(ISchemaProvider schemaProvider)
{
return (T)_customProviderCache.GetOrAdd(typeof(T), t => GetCustomProviderExport<T>(schemaProvider));
}
private static T GetCustomProviderExport<T>(ISchemaProvider schemaProvider)
{
using (var assemblyCatalog = new AssemblyCatalog(schemaProvider.GetType().Assembly))
{
using (var container = new CompositionContainer(assemblyCatalog))
{
return container.GetExportedValueOrDefault<T>();
}
}
}
private class ConnectionToken : IEquatable<ConnectionToken>
{
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <returns>
/// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
/// </returns>
/// <param name="other">An object to compare with this object.</param>
public bool Equals(ConnectionToken other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(other.ConnectionString, ConnectionString) && Equals(other.ProviderName, ProviderName);
}
/// <summary>
/// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
/// </summary>
/// <returns>
/// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
/// </returns>
/// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param><filterpriority>2</filterpriority>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof (ConnectionToken)) return false;
return Equals((ConnectionToken) obj);
}
/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <returns>
/// A hash code for the current <see cref="T:System.Object"/>.
/// </returns>
/// <filterpriority>2</filterpriority>
public override int GetHashCode()
{
unchecked
{
return (ConnectionString.GetHashCode()*397) ^ ProviderName.GetHashCode();
}
}
public static bool operator ==(ConnectionToken left, ConnectionToken right)
{
return Equals(left, right);
}
public static bool operator !=(ConnectionToken left, ConnectionToken right)
{
return !Equals(left, right);
}
private readonly string _connectionString;
private readonly string _providerName;
public ConnectionToken(string connectionString, string providerName)
{
if (connectionString == null) throw new ArgumentNullException("connectionString");
_connectionString = connectionString;
_providerName = providerName ?? string.Empty;
}
public string ConnectionString
{
get { return _connectionString; }
}
public string ProviderName
{
get { return _providerName; }
}
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.