-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSQLHelper.cs
172 lines (155 loc) · 6.34 KB
/
SQLHelper.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
/*
Copyright 2016 James Craig
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using BigBook;
using Microsoft.Extensions.Configuration;
using SQLHelper.HelperClasses;
using SQLHelper.HelperClasses.Interfaces;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
namespace SQLHelper
{
/// <summary>
/// SQL helper class
/// </summary>
public class SQLHelper
{
/// <summary>
/// Initializes a new instance of the <see cref="SQLHelper"/> class.
/// </summary>
/// <param name="configuration">The configuration object.</param>
/// <param name="factory">The factory.</param>
/// <param name="database">The database.</param>
public SQLHelper(IConfiguration configuration, DbProviderFactory factory, string database = "Default")
{
DatabaseSource = new Source(configuration, factory, "", database);
Batch = new Batch(DatabaseSource);
}
/// <summary>
/// Gets the batch.
/// </summary>
/// <value>The batch.</value>
protected IBatch Batch { get; private set; }
/// <summary>
/// Gets or sets the source.
/// </summary>
/// <value>The source.</value>
protected ISource DatabaseSource { get; private set; }
/// <summary>
/// Adds a command.
/// </summary>
/// <param name="command">The command.</param>
/// <param name="commandType">Type of the command.</param>
/// <param name="parameters">The parameters.</param>
/// <returns>This</returns>
public SQLHelper AddQuery(string command, CommandType commandType, params IParameter[] parameters)
{
return AddQuery<object>((x, y, z) => { }, null, command, commandType, parameters);
}
/// <summary>
/// Adds a command.
/// </summary>
/// <param name="command">The command.</param>
/// <param name="commandType">Type of the command.</param>
/// <param name="parameters">The parameters.</param>
/// <returns>This</returns>
public SQLHelper AddQuery(CommandType commandType, string command, params object[] parameters)
{
return AddQuery<object>((x, y, z) => { }, null, commandType, command, parameters);
}
/// <summary>
/// Adds a command.
/// </summary>
/// <param name="callback">The callback.</param>
/// <param name="callbackObject">The callback object.</param>
/// <param name="command">The command.</param>
/// <param name="commandType">Type of the command.</param>
/// <param name="parameters">The parameters.</param>
/// <returns>This</returns>
public SQLHelper AddQuery<TCallbackData>(Action<ICommand, IList<dynamic>, TCallbackData> callback, TCallbackData callbackObject,
string command, CommandType commandType, params IParameter[] parameters)
{
Batch.AddQuery(callback, callbackObject, command, commandType, parameters);
return this;
}
/// <summary>
/// Adds a command which will call the callback function with the object specified when it
/// </summary>
/// <param name="callback">The callback.</param>
/// <param name="callbackObject">The callback object.</param>
/// <param name="command">The command.</param>
/// <param name="commandType">Type of the command.</param>
/// <param name="parameters">The parameters.</param>
/// <returns>This</returns>
public SQLHelper AddQuery<TCallbackData>(Action<ICommand, IList<dynamic>, TCallbackData> callback, TCallbackData callbackObject,
CommandType commandType, string command, params object[] parameters)
{
Batch.AddQuery(callback, callbackObject, command, commandType, parameters);
return this;
}
/// <summary>
/// Adds an SQLHelper's commands to this instance
/// </summary>
/// <param name="helper">The helper to copy the commands from</param>
/// <returns>This</returns>
public SQLHelper AddQuery(SQLHelper helper)
{
Batch.AddQuery(helper.Batch);
return this;
}
/// <summary>
/// Clears the system and creates a new batch.
/// </summary>
/// <returns>This</returns>
public SQLHelper CreateBatch()
{
Batch = new Batch(DatabaseSource);
return this;
}
/// <summary>
/// Executes this instance.
/// </summary>
/// <returns></returns>
public IList<IList<dynamic>> Execute()
{
return Batch.Execute();
}
/// <summary>
/// Executes the batched commands and returns the first value, ignoring the rest.
/// </summary>
/// <typeparam name="TData">The type of the data to return.</typeparam>
/// <returns>The first value of the batch</returns>
public TData ExecuteScalar<TData>()
{
return ((object)Batch.Execute()[0][0]).To<object, TData>();
}
/// <summary>
/// Removes duplicate queries from the batch.
/// </summary>
/// <returns>This</returns>
public SQLHelper RemoveDuplicateCommands()
{
Batch.RemoveDuplicateCommands();
return this;
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <returns>A <see cref="System.String"/> that represents this instance.</returns>
public override string ToString()
{
return Batch.ToString();
}
}
}