-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathValuesObject.cs
More file actions
90 lines (76 loc) · 3.36 KB
/
ValuesObject.cs
File metadata and controls
90 lines (76 loc) · 3.36 KB
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
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
namespace Poco.Sql.NetCore
{
public class ValuesObject
{
private static readonly SqlDateTime _smallDateTimeMinValue = new SqlDateTime(new DateTime(1900, 01, 01, 00, 00, 00));
public static object Create(params object[] args)
{
dynamic obj = new ExpandoObject();
List<string> sqlParams = GetPramsFromSqlString(args);
int startPos = sqlParams == null ? 0 : 1; // if we have sqlParams that means that the first agrument is an sql string
// loop over all objects that were sent for merging
for (int i = startPos; i < args.Length; i++)
{
// get an object and make sure it's valid
object currentObj = args[i];
if (currentObj == null) continue;
IDictionary<string, object> objDic = (IDictionary<string, object>) obj;
// loop over all elements of current object
IEnumerable<PropertyInfo> propertyInfos = currentObj.GetType().GetRuntimeProperties();
foreach (
PropertyInfo propertyInfo in
propertyInfos.Where(
p =>
p.PropertyType.GetTypeInfo().IsEnum ||
(p.PropertyType.FullName.StartsWith("System") &&
!p.PropertyType.FullName.StartsWith("System.Collections"))))
// only loop on objects that are not custom class
{
if (objDic.ContainsKey(propertyInfo.Name))
continue; // same key can't be added twice (first key found will be used)
if (sqlParams == null || sqlParams.Contains(propertyInfo.Name))
{
var val = propertyInfo.GetValue(currentObj);
if (propertyInfo.PropertyType == typeof (DateTime) && (DateTime) val == DateTime.MinValue)
val = (DateTime) System.Data.SqlTypes.SqlDateTime.MinValue;
objDic.Add(propertyInfo.Name, val);
}
}
}
return (object)obj;
}
private static object getDefaultValue(Type t)
{
if (t == typeof(DateTime))
return DateTime.MinValue;
else if (t.GetTypeInfo().IsValueType)
return Activator.CreateInstance(t);
return null;
}
private static List<string> GetPramsFromSqlString(object[] args)
{
List<string> sqlParams = null;
if (args.Length > 1 && args[0] is String)
{
sqlParams = new List<string>();
string sql = args[0].ToString();
Regex regex = new Regex(@"(?<=@)([\w\-]+)");
var matches = regex.Matches(sql);
for (int i = 0; i < matches.Count; i++)
{
var val = matches[i].Value;
if (!sqlParams.Contains(val))
sqlParams.Add(matches[i].Value);
}
}
return sqlParams;
}
}
}