-
Notifications
You must be signed in to change notification settings - Fork 2
/
DbQuery.cs
346 lines (303 loc) · 13.1 KB
/
DbQuery.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Collections;
using MSS = Microsoft.SqlServer.Server;
using System.Linq.Expressions;
using System.Reflection;
namespace Lab.DbQuery
{
public class ExecuteQueryOptional
{
public object CustomerMapping { get; set; }
public object Parameters { get; set; }
}
public class ExecuteQueryOptional<T> : ExecuteQueryOptional
{
public Func<DbDataReader, T> ReaderFunc { get; set; }
}
public abstract class DbQuery
{
private void EnsureConnectionOpen(DbConnection connection)
{
if (connection.State != ConnectionState.Open)
connection.Open();
}
private bool IsSimpleType<T>()
{
return IsSimpleType(typeof(T));
}
private bool IsSimpleType(Type type)
{
return TypeDescriptor.GetConverter(type).CanConvertFrom(typeof(string));
}
private IDictionary<string, object> AnonymousObjectToDictionary(object anonymousObject)
{
IDictionary<string, object> result = new Dictionary<string, object>(10);
if (anonymousObject != null)
{
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(anonymousObject))
{
result.Add(property.Name, property.GetValue(anonymousObject));
}
}
return result;
}
private DbCommand SetupCommand(DbConnection connection, string commandText, object parameters)
{
EnsureConnectionOpen(connection);
var command = connection.CreateCommand();
var paramsDictionary = AnonymousObjectToDictionary(parameters);
var dbParameters = new List<DbParameter>(paramsDictionary.Count);
foreach (var item in paramsDictionary)
{
if (item.Value == null)
{
var dbParam = command.CreateParameter();
dbParam.Value = DBNull.Value;
dbParam.ParameterName = string.Format("@{0}", item.Key);
command.Parameters.Add(dbParam);
}
else if (item.Value is ICollection)
{
int enumeratorCount = 0;
var listParams = new List<string>(10);
var enumerator = (item.Value as IEnumerable).GetEnumerator();
using (enumerator as IDisposable)
{
while (enumerator.MoveNext())
{
var listParam = string.Format("@{0}#{1}", item.Key, enumeratorCount);
var dbParam = command.CreateParameter();
dbParam.Value = enumerator.Current;
dbParam.ParameterName = listParam;
dbParam.DbType = MSS.SqlMetaData.InferFromValue(enumerator.Current, dbParam.ParameterName).DbType;
if (enumerator.Current is string && ((string)enumerator.Current).Length <= 4000)
dbParam.Size = 4000;
command.Parameters.Add(dbParam);
listParams.Add(listParam);
enumeratorCount++;
}
}
commandText = Regex.Replace(commandText,
string.Format("@{0}", item.Key), string.Join(",", listParams.ToArray()),
RegexOptions.IgnoreCase);
}
else
{
var dbParam = command.CreateParameter();
dbParam.Value = item.Value;
dbParam.ParameterName = string.Format("@{0}", item.Key);
dbParam.DbType = MSS.SqlMetaData.InferFromValue(item.Value, dbParam.ParameterName).DbType;
if (item.Value is string && ((string)item.Value).Length <= 4000)
dbParam.Size = 4000;
command.Parameters.Add(dbParam);
}
}
command.CommandType = CommandType.Text;
command.CommandText = commandText;
return command;
}
private Func<DbDataReader, T> DynamicMappingInternal<T>(string[] fileName, IDictionary<string, string> customerMapping)
{
ParameterExpression r = Expression.Parameter(typeof(DbDataReader), "dr");
Expression<Func<DbDataReader, T>> lambda;
if (IsSimpleType<T>())//IsSimpleType
{
Expression body = Expression.Call(typeof(DbQueryExtensionMethods).GetMethod("Get").MakeGenericMethod(typeof(T)),
new Expression[] { r, Expression.Constant(fileName.First()) });
lambda = Expression.Lambda<Func<DbDataReader, T>>(body, r);
return lambda.Compile();
}
List<MemberBinding> bindings = new List<MemberBinding>();
var properties = typeof(T).GetProperties().Where(p => IsSimpleType(p.PropertyType) && p.GetSetMethod() != null);
foreach (PropertyInfo property in properties)
{
string mappedPropertyName;
customerMapping.TryGetValue(property.Name.ToLower(), out mappedPropertyName);
if (string.IsNullOrEmpty(mappedPropertyName))
{
mappedPropertyName = property.Name.ToLower();
}
if (fileName.Contains(mappedPropertyName))
{
MethodCallExpression propertyValue = Expression.Call(
typeof(DbQueryExtensionMethods).GetMethod("Get").MakeGenericMethod(property.PropertyType),
r, Expression.Constant(mappedPropertyName));
MemberBinding binding = Expression.Bind(property, propertyValue);
bindings.Add(binding);
}
}
Expression initializer = Expression.MemberInit(Expression.New(typeof(T)), bindings);
lambda = Expression.Lambda<Func<DbDataReader, T>>(initializer, r);
return lambda.Compile();
}
public Func<DbDataReader, T> DynamicMapping<T>(DbDataReader reader, object customerMapping)
{
var customerMappingDic = AnonymousObjectToDictionary(customerMapping).ToDictionary(c => c.Key.ToLower(), c => c.Value.ToString().ToLower());
string[] readerFiled = Enumerable.Range(0, reader.FieldCount).Select(f => reader.GetName(f).ToLower()).ToArray();
return ReaderMappingCache<T>.GetCacheInfo(new Identity(readerFiled, typeof(T), customerMappingDic),
() => DynamicMappingInternal<T>(readerFiled, customerMappingDic));
}
public int ExecuteCommand(string connectionString, string commandText)
{
return ExecuteCommand(connectionString, commandText, null);
}
public int ExecuteCommand(string connectionString, string commandText, object parameters)
{
using (var conn = GetDbConnection(connectionString))
using (var command = SetupCommand(conn, commandText, parameters))
{
return command.ExecuteNonQuery();
}
}
public T ExecuteScalar<T>(string connectionString, string commandText, object parameters)
{
using (var connection = GetDbConnection(connectionString))
using (var command = SetupCommand(connection, commandText, parameters))
{
object result = command.ExecuteScalar();
if (result == null || result == DBNull.Value)
return default(T);
return (T)result;
}
}
public IEnumerable<T> ExecuteQuery<T>(string connectionString, string commandText, ExecuteQueryOptional optional)
{
using (var reader = GetDbDataReader(connectionString, commandText, optional.Parameters))
{
Func<DbDataReader, T> readerFunc = null;
if (optional is ExecuteQueryOptional<T>)
{
readerFunc = ((ExecuteQueryOptional<T>)optional).ReaderFunc;
}
readerFunc = readerFunc ?? DynamicMapping<T>(reader, optional.CustomerMapping);
while (reader.Read())
{
yield return readerFunc(reader);
}
}
}
public DbDataReader GetDbDataReader(string connectionString, string commandText, object parameters = null)
{
var connection = GetDbConnection(connectionString);
var command = SetupCommand(connection, commandText, parameters);
return command.ExecuteReader(CommandBehavior.CloseConnection);
}
public abstract DbConnection GetDbConnection(string connectionString);
}
public sealed class SQlDbQuery : DbQuery
{
public static readonly string LastInsertIDSql = "SELECT CONVERT(Int,SCOPE_IDENTITY())";
public static DbQuery Current
{
get { return new SQlDbQuery(); }
}
public override DbConnection GetDbConnection(string connectionString)
{
return new SqlConnection(connectionString);
}
}
public static class DbQueryExtensionMethods
{
internal static T As<T>(this object value)
{
var resultType = typeof(T);
if (!TypeDescriptor.GetConverter(resultType).CanConvertFrom(typeof(string)))
throw new ArgumentException("only supprot simple type convert");
T result;
try
{
TypeConverter tc = TypeDescriptor.GetConverter(resultType);
result = (T)(tc.ConvertFrom(value.ToString()));
}
catch
{
result = default(T);
}
return result;
}
public static T Get<T>(this DbDataReader reader, string colName)
{
object value = reader[colName];
if (value != DBNull.Value)
return value.As<T>();
return default(T);
}
}
#region
internal class ReaderMappingCache<T>
{
private static readonly Dictionary<Identity, Func<DbDataReader, T>> _queryCache = new Dictionary<Identity, Func<DbDataReader, T>>();
private static void SetQueryCache(Identity key, Func<DbDataReader, T> value)
{
lock (_queryCache) { _queryCache[key] = value; }
}
private static bool TryGetQueryCache(Identity key, out Func<DbDataReader, T> value)
{
lock (_queryCache) { return _queryCache.TryGetValue(key, out value); }
}
public static Func<DbDataReader, T> GetCacheInfo(Identity identity, Func<Func<DbDataReader, T>> setValue)
{
Func<DbDataReader, T> value;
if (!TryGetQueryCache(identity, out value))
{
value = setValue();
SetQueryCache(identity, value);
}
return value;
}
}
internal class Identity : IEquatable<Identity>
{
public Identity(string[] readerFiled, Type type, IDictionary<string, string> customerMapping)
{
this.readerFiled = readerFiled;
this.type = type;
this.customerMapping = customerMapping;
unchecked
{
hashCode = 17;
if (readerFiled != null && readerFiled.Count() > 0)
{
foreach (var t in readerFiled)
{
hashCode = hashCode * 23 + (t == null ? 0 : t.GetHashCode());
}
}
if (customerMapping != null && customerMapping.Count > 0)
{
foreach (var item in customerMapping)
{
hashCode = hashCode * 23 + item.Key.GetHashCode() + item.Value.GetHashCode();
}
}
hashCode = hashCode * 23 + (type == null ? 0 : type.GetHashCode());
}
}
public override bool Equals(object obj)
{
return Equals(obj as Identity);
}
private readonly string[] readerFiled;
private readonly int hashCode;
private readonly IDictionary<string, string> customerMapping;
private readonly Type type;
public override int GetHashCode()
{
return hashCode;
}
public bool Equals(Identity other)
{
return type == other.type
&& readerFiled.SequenceEqual(other.readerFiled)
&& customerMapping.SequenceEqual(other.customerMapping);
}
}
#endregion
}