-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDbSupport.cs
186 lines (169 loc) · 7.62 KB
/
DbSupport.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
using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
namespace Ceen.Database
{
public static class DbSupport
{
/// <summary>
/// Helper method for creating a command and initializing the parameters
/// </summary>
/// <returns>The command.</returns>
/// <param name="commandtext">The commandtext.</param>
public static IDbCommand SetupCommand(this IDbConnection connection, string commandtext, IDbTransaction transaction = null)
{
if (string.IsNullOrWhiteSpace(commandtext))
throw new ArgumentNullException(nameof(commandtext));
var cmd = connection.CreateCommand();
cmd.CommandText = commandtext;
if (transaction != null)
cmd.Transaction = transaction;
AddParameters(cmd, commandtext.Count(x => x == '?'));
return cmd;
}
/// <summary>
/// Adds a number of parameters to the command
/// </summary>
/// <param name="cmd">The command to add the parameters to.</param>
/// <param name="count">The number of parameters to add.</param>
public static void AddParameters(this IDbCommand cmd, int count)
{
if (cmd == null)
throw new ArgumentNullException(nameof(cmd));
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count));
if (cmd.Parameters.Count > count)
cmd.Parameters.Clear();
for (var i = cmd.Parameters.Count; i < count; i++)
cmd.Parameters.Add(cmd.CreateParameter());
}
/// <summary>
/// Sets the parameter values.
/// </summary>
/// <param name="cmd">The command to set parameter values on.</param>
/// <param name="values">The values to set.</param>
public static void SetParameterValues(this IDbCommand cmd, IEnumerable<object> values)
{
if (cmd == null)
throw new ArgumentNullException(nameof(cmd));
values = values ?? new object[0];
AddParameters(cmd, values.Count());
int i = 0;
foreach (var v in values)
((IDbDataParameter)cmd.Parameters[i++]).Value = v;
}
/// <summary>
/// Sets the parameter values.
/// </summary>
/// <param name="cmd">The command to set parameter values on.</param>
/// <param name="values">The values to set.</param>
public static void SetParameterValues(this IDbCommand cmd, params object[] values)
{
SetParameterValues(cmd, (values ?? new object[0]).AsEnumerable());
}
/// <summary>
/// Fixes a deficiency in the database mapping,
/// and returns string null values as null
/// </summary>
/// <returns>The string representation.</returns>
/// <param name="rd">The reader to use.</param>
/// <param name="index">The index to read the string from.</param>
public static string GetAsString(this IDataReader rd, int index)
{
return (string)rd.GetNormalizedValue(index);
}
/// <summary>
/// Creates a new command with the given command text
/// </summary>
/// <returns>The new command.</returns>
/// <param name="connection">The connection to create the command on.</param>
/// <param name="command">The command text to use.</param>
/// <param name="transaction">The transaction to use.</param>
public static IDbCommand CreateCommand(this IDbConnection connection, string command, IDbTransaction transaction = null)
{
var cmd = connection.CreateCommand();
if (transaction != null)
cmd.Transaction = transaction;
if (command != null)
cmd.CommandText = command;
return cmd;
}
/// <summary>
/// Creates a new command with the given command text
/// </summary>
/// <returns>The new command.</returns>
/// <param name="connection">The connection to create the command on.</param>
/// <param name="command">The command text to use.</param>
/// <param name="transaction">The transaction to use.</param>
public static IDbCommand CreateCommandWithParameters(this IDbConnection connection, string command, IDbTransaction transaction = null)
{
var cmd = connection.CreateCommand(command ?? throw new ArgumentNullException(nameof(command)), transaction);
AddParameters(cmd, command.Count(x => x == '?'));
return cmd;
}
/// <summary>
/// Executes the command and returns the reader
/// </summary>
/// <returns>The reader.</returns>
/// <param name="command">The command to execute.</param>
/// <param name="values">The values to set.</param>
public static IDataReader ExecuteReader(this IDbCommand command, IEnumerable<object> values)
{
SetParameterValues(command, values);
return command.ExecuteReader();
}
/// <summary>
/// Executes the command and returns the reader
/// </summary>
/// <returns>The reader.</returns>
/// <param name="command">The command to execute.</param>
/// <param name="values">The values to set.</param>
public static IDataReader ExecuteReader(this IDbCommand command, params object[] values)
{
return ExecuteReader(command, (values ?? new object[0]).AsEnumerable());
}
/// <summary>
/// Executes the command and returns the reader
/// </summary>
/// <returns>The reader.</returns>
/// <param name="command">The command to execute.</param>
/// <param name="values">The values to set.</param>
public static int ExecuteNonQuery(this IDbCommand command, IEnumerable<object> values)
{
SetParameterValues(command, values);
return command.ExecuteNonQuery();
}
/// <summary>
/// Executes the command and returns the reader
/// </summary>
/// <returns>The reader.</returns>
/// <param name="command">The command to execute.</param>
/// <param name="values">The values to set.</param>
public static int ExecuteNonQuery(this IDbCommand command, params object[] values)
{
return ExecuteNonQuery(command, (values ?? new object[0]).AsEnumerable());
}
/// <summary>
/// Executes the command and returns the reader
/// </summary>
/// <returns>The reader.</returns>
/// <param name="command">The command to execute.</param>
/// <param name="values">The values to set.</param>
public static object ExecuteScalar(this IDbCommand command, IEnumerable<object> values)
{
SetParameterValues(command, values);
return command.ExecuteScalar();
}
/// <summary>
/// Executes the command and returns the reader
/// </summary>
/// <returns>The reader.</returns>
/// <param name="command">The command to execute.</param>
/// <param name="values">The values to set.</param>
public static object ExecuteScalar(this IDbCommand command, params object[] values)
{
return ExecuteScalar(command, (values ?? new object[0]).AsEnumerable());
}
}
}