-
Notifications
You must be signed in to change notification settings - Fork 597
/
PocoData.cs
410 lines (342 loc) · 17.7 KB
/
PocoData.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
using PetaPoco.Internal;
namespace PetaPoco.Core
{
public class PocoData
{
private static Cache<Type, PocoData> _pocoDatas = new Cache<Type, PocoData>();
private static List<Func<object, object>> _converters = new List<Func<object, object>>();
private static object _converterLock = new object();
private static MethodInfo fnGetValue = typeof(IDataRecord).GetMethod("GetValue", new Type[] { typeof(int) });
private static MethodInfo fnIsDBNull = typeof(IDataRecord).GetMethod("IsDBNull");
private static FieldInfo fldConverters = typeof(PocoData).GetField("_converters", BindingFlags.Static | BindingFlags.GetField | BindingFlags.NonPublic);
private static MethodInfo fnListGetItem = typeof(List<Func<object, object>>).GetProperty("Item").GetGetMethod();
private static MethodInfo fnInvoke = typeof(Func<object, object>).GetMethod("Invoke");
private Cache<Tuple<string, string, int, int>, Delegate> PocoFactories = new Cache<Tuple<string, string, int, int>, Delegate>();
public Type Type;
public string[] QueryColumns { get; private set; }
public string[] UpdateColumns
{
// No need to cache as it's not used by PetaPoco internally
get { return (from c in Columns where !c.Value.ResultColumn && c.Value.ColumnName != TableInfo.PrimaryKey select c.Key).ToArray(); }
}
public TableInfo TableInfo { get; private set; }
public Dictionary<string, PocoColumn> Columns { get; private set; }
public PocoData()
{
}
public PocoData(Type type, IMapper defaultMapper)
{
Type = type;
// Get the mapper for this type
var mapper = Mappers.GetMapper(type, defaultMapper);
// Get the table info
TableInfo = mapper.GetTableInfo(type);
// Work out bound properties
Columns = new Dictionary<string, PocoColumn>(StringComparer.OrdinalIgnoreCase);
foreach (var pi in type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
{
ColumnInfo ci = mapper.GetColumnInfo(pi);
if (ci == null)
continue;
var pc = new PocoColumn();
pc.PropertyInfo = pi;
pc.ColumnName = ci.ColumnName;
pc.ResultColumn = ci.ResultColumn;
pc.AutoSelectedResultColumn = ci.AutoSelectedResultColumn;
pc.ForceToUtc = ci.ForceToUtc;
pc.InsertTemplate = ci.InsertTemplate;
pc.UpdateTemplate = ci.UpdateTemplate;
// Store it
Columns.Add(pc.ColumnName, pc);
}
// Build column list for automatic select
QueryColumns = (from c in Columns where !c.Value.ResultColumn || c.Value.AutoSelectedResultColumn select c.Key).ToArray();
}
public static PocoData ForObject(object obj, string primaryKeyName, IMapper defaultMapper)
{
var t = obj.GetType();
if (t == typeof(System.Dynamic.ExpandoObject))
{
var pd = new PocoData();
pd.TableInfo = new TableInfo();
pd.Columns = new Dictionary<string, PocoColumn>(StringComparer.OrdinalIgnoreCase);
pd.Columns.Add(primaryKeyName, new ExpandoColumn() { ColumnName = primaryKeyName });
pd.TableInfo.PrimaryKey = primaryKeyName;
pd.TableInfo.AutoIncrement = true;
foreach (var col in (obj as IDictionary<string, object>).Keys)
{
if (col != primaryKeyName)
pd.Columns.Add(col, new ExpandoColumn() { ColumnName = col });
}
return pd;
}
return ForType(t, defaultMapper);
}
public static PocoData ForType(Type type, IMapper defaultMapper)
{
if (type == typeof(System.Dynamic.ExpandoObject))
throw new InvalidOperationException("Can't use dynamic types with this method");
return _pocoDatas.Get(type, () => new PocoData(type, defaultMapper));
}
private static bool IsIntegralType(Type type)
{
var tc = Type.GetTypeCode(type);
return tc >= TypeCode.SByte && tc <= TypeCode.UInt64;
}
// Create factory function that can convert a IDataReader record into a POCO
public Delegate GetFactory(string sql, string connectionString, int firstColumn, int countColumns, IDataReader reader, IMapper defaultMapper)
{
// Check cache
var key = Tuple.Create<string, string, int, int>(sql, connectionString, firstColumn, countColumns);
return PocoFactories.Get(key, () =>
{
// Create the method
var m = new DynamicMethod("petapoco_factory_" + PocoFactories.Count.ToString(), Type, new Type[] { typeof(IDataReader) }, true);
var il = m.GetILGenerator();
var mapper = Mappers.GetMapper(Type, defaultMapper);
if (Type == typeof(object))
{
// var poco=new T()
il.Emit(OpCodes.Newobj, typeof(System.Dynamic.ExpandoObject).GetConstructor(Type.EmptyTypes)); // obj
MethodInfo fnAdd = typeof(IDictionary<string, object>).GetMethod("Add");
// Enumerate all fields generating a set assignment for the column
for (int i = firstColumn; i < firstColumn + countColumns; i++)
{
var srcType = reader.GetFieldType(i);
il.Emit(OpCodes.Dup); // obj, obj
il.Emit(OpCodes.Ldstr, reader.GetName(i)); // obj, obj, fieldname
// Get the converter
Func<object, object> converter = mapper.GetFromDbConverter((PropertyInfo) null, srcType);
/*
if (ForceDateTimesToUtc && converter == null && srcType == typeof(DateTime))
converter = delegate(object src) { return new DateTime(((DateTime)src).Ticks, DateTimeKind.Utc); };
*/
// Setup stack for call to converter
AddConverterToStack(il, converter);
// r[i]
il.Emit(OpCodes.Ldarg_0); // obj, obj, fieldname, converter?, rdr
il.Emit(OpCodes.Ldc_I4, i); // obj, obj, fieldname, converter?, rdr,i
il.Emit(OpCodes.Callvirt, fnGetValue); // obj, obj, fieldname, converter?, value
// Convert DBNull to null
il.Emit(OpCodes.Dup); // obj, obj, fieldname, converter?, value, value
il.Emit(OpCodes.Isinst, typeof(DBNull)); // obj, obj, fieldname, converter?, value, (value or null)
var lblNotNull = il.DefineLabel();
il.Emit(OpCodes.Brfalse_S, lblNotNull); // obj, obj, fieldname, converter?, value
il.Emit(OpCodes.Pop); // obj, obj, fieldname, converter?
if (converter != null)
il.Emit(OpCodes.Pop); // obj, obj, fieldname,
il.Emit(OpCodes.Ldnull); // obj, obj, fieldname, null
if (converter != null)
{
var lblReady = il.DefineLabel();
il.Emit(OpCodes.Br_S, lblReady);
il.MarkLabel(lblNotNull);
il.Emit(OpCodes.Callvirt, fnInvoke);
il.MarkLabel(lblReady);
}
else
{
il.MarkLabel(lblNotNull);
}
il.Emit(OpCodes.Callvirt, fnAdd);
}
}
else if (Type.IsValueType || Type == typeof(string) || Type == typeof(byte[]))
{
// Do we need to install a converter?
var srcType = reader.GetFieldType(0);
var converter = GetConverter(mapper, null, srcType, Type);
// "if (!rdr.IsDBNull(i))"
il.Emit(OpCodes.Ldarg_0); // rdr
il.Emit(OpCodes.Ldc_I4_0); // rdr,0
il.Emit(OpCodes.Callvirt, fnIsDBNull); // bool
var lblCont = il.DefineLabel();
il.Emit(OpCodes.Brfalse_S, lblCont);
il.Emit(OpCodes.Ldnull); // null
var lblFin = il.DefineLabel();
il.Emit(OpCodes.Br_S, lblFin);
il.MarkLabel(lblCont);
// Setup stack for call to converter
AddConverterToStack(il, converter);
il.Emit(OpCodes.Ldarg_0); // rdr
il.Emit(OpCodes.Ldc_I4_0); // rdr,0
il.Emit(OpCodes.Callvirt, fnGetValue); // value
// Call the converter
if (converter != null)
il.Emit(OpCodes.Callvirt, fnInvoke);
il.MarkLabel(lblFin);
il.Emit(OpCodes.Unbox_Any, Type); // value converted
}
else
{
// var poco=new T()
var ctor = Type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[0], null);
if (ctor == null)
throw new InvalidOperationException("Type [" + Type.FullName + "] should have default public or non-public constructor");
il.Emit(OpCodes.Newobj, ctor);
// Enumerate all fields generating a set assignment for the column
for (int i = firstColumn; i < firstColumn + countColumns; i++)
{
// Get the PocoColumn for this db column, ignore if not known
PocoColumn pc;
if (!Columns.TryGetValue(reader.GetName(i), out pc))
continue;
// Get the source type for this column
var srcType = reader.GetFieldType(i);
var dstType = pc.PropertyInfo.PropertyType;
// "if (!rdr.IsDBNull(i))"
il.Emit(OpCodes.Ldarg_0); // poco,rdr
il.Emit(OpCodes.Ldc_I4, i); // poco,rdr,i
il.Emit(OpCodes.Callvirt, fnIsDBNull); // poco,bool
var lblNext = il.DefineLabel();
il.Emit(OpCodes.Brtrue_S, lblNext); // poco
il.Emit(OpCodes.Dup); // poco,poco
// Do we need to install a converter?
var converter = GetConverter(mapper, pc, srcType, dstType);
// Fast
bool Handled = false;
if (converter == null)
{
var valuegetter = typeof(IDataRecord).GetMethod("Get" + srcType.Name, new Type[] { typeof(int) });
if (valuegetter != null && valuegetter.ReturnType == srcType &&
(valuegetter.ReturnType == dstType || valuegetter.ReturnType == Nullable.GetUnderlyingType(dstType)))
{
il.Emit(OpCodes.Ldarg_0); // *,rdr
il.Emit(OpCodes.Ldc_I4, i); // *,rdr,i
il.Emit(OpCodes.Callvirt, valuegetter); // *,value
// Convert to Nullable
if (Nullable.GetUnderlyingType(dstType) != null)
{
il.Emit(OpCodes.Newobj, dstType.GetConstructor(new Type[] { Nullable.GetUnderlyingType(dstType) }));
}
il.Emit(OpCodes.Callvirt, pc.PropertyInfo.GetSetMethod(true)); // poco
Handled = true;
}
}
// Not so fast
if (!Handled)
{
// Setup stack for call to converter
AddConverterToStack(il, converter);
// "value = rdr.GetValue(i)"
il.Emit(OpCodes.Ldarg_0); // *,rdr
il.Emit(OpCodes.Ldc_I4, i); // *,rdr,i
il.Emit(OpCodes.Callvirt, fnGetValue); // *,value
// Call the converter
if (converter != null)
il.Emit(OpCodes.Callvirt, fnInvoke);
// Assign it
il.Emit(OpCodes.Unbox_Any, pc.PropertyInfo.PropertyType); // poco,poco,value
il.Emit(OpCodes.Callvirt, pc.PropertyInfo.GetSetMethod(true)); // poco
}
il.MarkLabel(lblNext);
}
var fnOnLoaded = RecurseInheritedTypes<MethodInfo>(Type,
(x) => x.GetMethod("OnLoaded", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[0], null));
if (fnOnLoaded != null)
{
il.Emit(OpCodes.Dup);
il.Emit(OpCodes.Callvirt, fnOnLoaded);
}
}
il.Emit(OpCodes.Ret);
// Cache it, return it
return m.CreateDelegate(Expression.GetFuncType(typeof(IDataReader), Type));
});
}
private static void AddConverterToStack(ILGenerator il, Func<object, object> converter)
{
if (converter != null)
{
// Add the converter
int converterIndex;
lock (_converterLock)
{
converterIndex = _converters.Count;
_converters.Add(converter);
}
// Generate IL to push the converter onto the stack
il.Emit(OpCodes.Ldsfld, fldConverters);
il.Emit(OpCodes.Ldc_I4, converterIndex);
il.Emit(OpCodes.Callvirt, fnListGetItem); // Converter
}
}
private static Func<object, object> GetConverter(IMapper mapper, PocoColumn pc, Type srcType, Type dstType)
{
Func<object, object> converter = null;
// Get converter from the mapper
if (pc != null)
{
converter = mapper.GetFromDbConverter(pc.PropertyInfo, srcType);
if (converter != null)
return converter;
}
// Standard DateTime->Utc mapper
if (pc != null && pc.ForceToUtc && srcType == typeof(DateTime) && (dstType == typeof(DateTime) || dstType == typeof(DateTime?)))
{
return delegate(object src) { return new DateTime(((DateTime) src).Ticks, DateTimeKind.Utc); };
}
// unwrap nullable types
Type underlyingDstType = Nullable.GetUnderlyingType(dstType);
if (underlyingDstType != null)
{
dstType = underlyingDstType;
}
// Forced type conversion including integral types -> enum
if (dstType.IsEnum && IsIntegralType(srcType))
{
var backingDstType = Enum.GetUnderlyingType(dstType);
if (underlyingDstType != null)
{
// if dstType is Nullable<Enum>, convert to enum value
return delegate(object src) { return Enum.ToObject(dstType, src); };
}
else if (srcType != backingDstType)
{
return delegate(object src) { return Convert.ChangeType(src, backingDstType, null); };
}
}
else if (!dstType.IsAssignableFrom(srcType))
{
if (dstType.IsEnum && srcType == typeof(string))
{
return delegate(object src) { return EnumMapper.EnumFromString(dstType, (string) src); };
}
if (dstType == typeof(Guid) && srcType == typeof(string))
{
return delegate(object src) { return Guid.Parse((string) src); };
}
if (dstType == typeof(string) && srcType == typeof(Guid))
{
return delegate(object src) { return Convert.ToString(src); };
}
return delegate(object src) { return Convert.ChangeType(src, dstType, null); };
}
return null;
}
private static T RecurseInheritedTypes<T>(Type t, Func<Type, T> cb)
{
while (t != null)
{
T info = cb(t);
if (info != null)
return info;
t = t.BaseType;
}
return default(T);
}
public static void FlushCaches()
=> _pocoDatas.Flush();
public string GetColumnName(string propertyName)
{
return Columns.Values.First(c => c.PropertyInfo.Name.Equals(propertyName)).ColumnName;
}
}
}