-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathParameters.cs
189 lines (166 loc) · 6.14 KB
/
Parameters.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
/*
* Author : sfk
* Licence : Public
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace DbConnect
{
/// <summary>
/// Class that holds bag of parameters
/// </summary>
public class DynamicParameters : IDynamicParameters
{
private const int DefaultLength = 4000;
/// <summary>
/// Parameter Info Template
/// </summary>
public class ParameterInfo
{
public string Name { get; set; }
public object Value { get; set; }
public ParameterDirection ParameterDirection { get; set; }
public DbType? DbType { get; set; }
public int? Size { get; set; }
}
/// <summary>
/// Dictionary hold the parameter details
/// </summary>
private readonly Dictionary<string, ParameterInfo> parameters = new Dictionary<string, ParameterInfo>();
/// <summary>
/// Adds parameters necessary for the query execution
/// </summary>
/// <param name="name"> Parameter name</param>
/// <param name="value">Parameter value</param>
/// <param name="dbType">DbType</param>
/// <param name="direction">Parameter direction</param>
/// <param name="size">Parameter Size</param>
public void Add(string name, object value, DbType? dbType = null, ParameterDirection? direction = null, int? size = null)
{
parameters[Clean(name)] = new ParameterInfo()
{
Name = Clean(name),
Value = value,
ParameterDirection = direction ?? ParameterDirection.Input,
DbType = dbType,
Size = size
};
}
/// <summary>
/// Cleans the parameter name by removing these characters ['@', ':', '?']
/// </summary>
/// <param name="name">Parameter name</param>
/// <returns>name as cleaned</returns>
static string Clean(string name)
{
if (!string.IsNullOrEmpty(name))
{
switch (name[0])
{
case '@':
case ':':
case '?':
return name.Substring(1);
}
}
return name;
}
/// <summary>
/// Interface implemetaion for AddParameters
/// </summary>
/// <param name="cmd"></param>
void IDynamicParameters.AddParameters(IDbCommand cmd)
{
AddParameters(cmd);
}
/// <summary>
/// Interface implementation for CallBack
/// </summary>
/// <param name="cmd"></param>
void IDynamicParameters.CallBack(IDbCommand cmd)
{
Callback(cmd);
}
/// <summary>
/// Adds Params to the cmd before its executed
/// </summary>
/// <param name="cmd"></param>
private void AddParameters(IDbCommand cmd)
{
foreach (var paramToAdd in parameters.Values)
{
var p = cmd.CreateParameter();
p.ParameterName = paramToAdd.Name;
p.Direction = paramToAdd.ParameterDirection;
paramToAdd.Value = paramToAdd.Value ?? DBNull.Value;
p.Value = paramToAdd.Value;
var dbType = paramToAdd.DbType ?? DbConnect.LookDbType(paramToAdd.Value.GetType(), paramToAdd.Name, true);
if (dbType == DbType.Structured)
{
SetTablevaluedParam(p, paramToAdd);
cmd.Parameters.Add(p);
continue;
}
p.DbType = (System.Data.DbType)dbType;
var s = paramToAdd.Value as string;
if (s != null && s.Length <= DefaultLength)
{
p.Size = DefaultLength;
}
if (paramToAdd.Size != null)
{
p.Size = (int)paramToAdd.Size;
}
cmd.Parameters.Add(p);
}
}
/// <summary>
/// Sets the table valued parameter
/// </summary>
/// <param name="param">Parameter Object from the Command</param>
/// <param name="info">Parameter info user supplied to add</param>
private static void SetTablevaluedParam(IDbDataParameter param, ParameterInfo info)
{
var sqlParam = param as SqlParameter;
if (sqlParam == null) throw new Exception("Failed to add the parameter !!");
if (info.Value == null) throw new Exception("Table valued parameter cannot be null !!");
var typeName = ((DataTable)info.Value).TableName;
if (String.IsNullOrWhiteSpace(typeName)) throw new Exception("Expects TableValue Type Name as DataTable Name, didn't supplied in the param. Please provide that !!");
sqlParam.TypeName = ((DataTable)info.Value).TableName;
sqlParam.Value = (DataTable)info.Value;
sqlParam.SqlDbType = SqlDbType.Structured;
}
/// <summary>
/// Callback called after query execution to collect the out params
/// </summary>
/// <param name="cmd"></param>
private void Callback(IDbCommand cmd)
{
foreach (IDbDataParameter param in cmd.Parameters)
{
parameters[param.ParameterName].Value = param.Value;
}
}
/// <summary>
/// Gets the Out parameter with type T
/// </summary>
/// <typeparam name="T">Type T in which we need data</typeparam>
/// <param name="name">Parameter Name</param>
/// <returns>Value as T</returns>
public T Get<T>(string name)
{
var val = parameters[Clean(name)].Value;
if (val == DBNull.Value)
{
if (default(T) != null)
{
throw new ApplicationException("Attempting to cast a DBNull to a non nullable type!");
}
return default(T);
}
return (T)val;
}
}
}