0
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Data;
|
|
4
|
|
5 using BLToolkit.Data;
|
|
6 using BLToolkit.Mapping;
|
|
7
|
|
8 namespace BLToolkit.DataAccess
|
|
9 {
|
|
10 public class SqlQueryInfo
|
|
11 {
|
|
12 //NOTE Added empty constructor
|
|
13 public SqlQueryInfo()
|
|
14 {
|
|
15 }
|
|
16
|
|
17 public SqlQueryInfo(ObjectMapper objectMapper)
|
|
18 {
|
|
19 ObjectMapper = objectMapper;
|
|
20 }
|
|
21
|
|
22 public string QueryText { get; set; }
|
|
23 public ObjectMapper ObjectMapper { get; private set; }
|
|
24
|
|
25 public Type ObjectType
|
|
26 {
|
|
27 get { return ObjectMapper.TypeAccessor.OriginalType; }
|
|
28 }
|
|
29
|
|
30 //NOTE Changed from private to protected
|
|
31 protected readonly List<SqlQueryParameterInfo> Parameters = new List<SqlQueryParameterInfo>();
|
|
32
|
|
33 //NOTE Changed to virtual
|
|
34 public virtual SqlQueryParameterInfo AddParameter(string parameterName, string fieldName)
|
|
35 {
|
|
36 var parameter = new SqlQueryParameterInfo { ParameterName = parameterName, FieldName = fieldName };
|
|
37
|
|
38 parameter.SetMemberMapper(ObjectMapper);
|
|
39
|
|
40 Parameters.Add(parameter);
|
|
41
|
|
42 return parameter;
|
|
43 }
|
|
44
|
|
45 public IDbDataParameter[] GetParameters(DbManager db, object[] key)
|
|
46 {
|
|
47 if (Parameters.Count != key.Length)
|
|
48 throw new DataAccessException("Parameter list does match key list.");
|
|
49
|
|
50 var parameters = new IDbDataParameter[Parameters.Count];
|
|
51
|
|
52 for (var i = 0; i < Parameters.Count; i++)
|
|
53 {
|
|
54 var info = Parameters[i];
|
|
55
|
|
56 parameters[i] = db.Parameter(info.ParameterName, key[i]);
|
|
57 }
|
|
58
|
|
59 return parameters;
|
|
60 }
|
|
61
|
|
62 public IDbDataParameter[] GetParameters(DbManager db, object obj)
|
|
63 {
|
|
64 var parameters = new IDbDataParameter[Parameters.Count];
|
|
65
|
|
66 for (var i = 0; i < Parameters.Count; i++)
|
|
67 {
|
|
68 var info = Parameters[i];
|
|
69
|
|
70 //parameters[i] = db.Parameter(info.ParameterName, info.MemberMapper.GetValue(obj));
|
|
71
|
|
72 var mmi = info.MemberMapper.MapMemberInfo;
|
|
73 var val = info.MemberMapper.GetValue(obj);
|
|
74
|
|
75 if (val == null && mmi.Nullable/* && mmi.NullValue == null*/)
|
|
76 {
|
|
77 //replace value with DbNull
|
|
78 val = DBNull.Value;
|
|
79 }
|
|
80
|
|
81 if (mmi.IsDbTypeSet)
|
|
82 {
|
|
83 parameters[i] = mmi.IsDbSizeSet
|
|
84 ? db.Parameter(info.ParameterName, val, info.MemberMapper.DbType, mmi.DbSize)
|
|
85 : db.Parameter(info.ParameterName, val, info.MemberMapper.DbType);
|
|
86 }
|
|
87 else
|
|
88 {
|
|
89 parameters[i] = val != DBNull.Value
|
|
90 ? db.Parameter(info.ParameterName, val)
|
|
91 : db.Parameter(info.ParameterName, val, info.MemberMapper.GetDbType());
|
|
92 }
|
|
93 }
|
|
94
|
|
95 return parameters;
|
|
96 }
|
|
97
|
|
98 public MemberMapper[] GetMemberMappers()
|
|
99 {
|
|
100 var members = new MemberMapper[Parameters.Count];
|
|
101
|
|
102 for (var i = 0; i < Parameters.Count; i++)
|
|
103 members[i] = Parameters[i].MemberMapper;
|
|
104
|
|
105 return members;
|
|
106 }
|
|
107 }
|
|
108 }
|