-
Notifications
You must be signed in to change notification settings - Fork 6
/
Builder.cs
187 lines (164 loc) · 9.1 KB
/
Builder.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
using System.Data;
namespace Dapper.SimpleSqlBuilder;
/// <summary>
/// An abstract class that implements the <see cref="ISqlBuilder"/> and defines the builder type.
/// </summary>
public abstract class Builder : ISqlBuilder
{
/// <inheritdoc/>
public abstract string Sql { get; }
/// <inheritdoc/>
public abstract object Parameters { get; }
/// <inheritdoc/>
public abstract IEnumerable<string> ParameterNames { get; }
/// <summary>
/// An add operator for the builder that enables dynamic query concatenation.
/// </summary>
/// <param name="builder">The <see cref="Builder"/> instance to perform the operation on.</param>
/// <param name="formattable">The <see cref="FormattableString"/>.</param>
/// <returns>The <see cref="Builder"/> instance.</returns>
/// <exception cref="ArgumentNullException">Throws an <see cref="ArgumentNullException"/> when called on a <see langword="null"/> object.</exception>
public static Builder operator +(Builder builder, FormattableString formattable)
{
return builder is null
? throw new ArgumentNullException(nameof(builder))
#if NET6_0_OR_GREATER
: builder.AppendIntact($"{formattable}");
#else
: builder.AppendIntact(formattable);
#endif
}
#if NET6_0_OR_GREATER
/// <summary>
/// Appends a space and the interpolated string to the builder.
/// </summary>
/// <param name="handler">The handler for the interpolated string.</param>
/// <returns>The <see cref="Builder"/> instance.</returns>
public abstract Builder Append([InterpolatedStringHandlerArgument("")] ref AppendInterpolatedStringHandler handler);
/// <summary>
/// Appends a space and the interpolated string to the builder.
/// </summary>
/// <param name="condition">The value to determine whether the method should be executed.</param>
/// <param name="handler">The handler for the interpolated string.</param>
/// <returns>The <see cref="Builder"/> instance.</returns>
public abstract Builder Append(bool condition, [InterpolatedStringHandlerArgument("condition", "")] ref AppendInterpolatedStringHandler handler);
/// <summary>
/// Appends the interpolated string to the builder.
/// </summary>
/// <param name="handler">The handler for the interpolated string.</param>
/// <returns>The <see cref="Builder"/> instance.</returns>
public abstract Builder AppendIntact([InterpolatedStringHandlerArgument("")] ref AppendIntactInterpolatedStringHandler handler);
/// <summary>
/// Appends the interpolated string to the builder.
/// </summary>
/// <param name="condition">The value to determine whether the method should be executed.</param>
/// <param name="handler">The handler for the interpolated string.</param>
/// <returns>The <see cref="Builder"/> instance.</returns>
public abstract Builder AppendIntact(bool condition, [InterpolatedStringHandlerArgument("condition", "")] ref AppendIntactInterpolatedStringHandler handler);
/// <summary>
/// Appends a <see cref="Environment.NewLine"/> and the interpolated string to the builder.
/// </summary>
/// <param name="handler">The handler for the interpolated string.</param>
/// <returns>The <see cref="Builder"/> instance.</returns>
public abstract Builder AppendNewLine([InterpolatedStringHandlerArgument("")] ref AppendNewLineInterpolatedStringHandler handler);
/// <summary>
/// Appends a <see cref="Environment.NewLine"/> and the interpolated string to the builder.
/// </summary>
/// <param name="condition">The value to determine whether the method should be executed.</param>
/// <param name="handler">The handler for the interpolated string.</param>
/// <returns>The <see cref="Builder"/> instance.</returns>
public abstract Builder AppendNewLine(bool condition, [InterpolatedStringHandlerArgument("condition", "")] ref AppendNewLineInterpolatedStringHandler handler);
#else
/// <summary>
/// Appends a space and the interpolated string or <see cref="FormattableString"/> to the builder.
/// </summary>
/// <param name="formattable">The <see cref="FormattableString"/>.</param>
/// <returns>The <see cref="Builder"/> instance.</returns>
public abstract Builder Append(FormattableString formattable);
/// <summary>
/// Appends a space and the interpolated string or <see cref="FormattableString"/> to the builder.
/// </summary>
/// <param name="condition">The value to determine whether the method should be executed.</param>
/// <param name="formattable">The <see cref="FormattableString"/>.</param>
/// <returns>The <see cref="Builder"/> instance.</returns>
public abstract Builder Append(bool condition, FormattableString formattable);
/// <summary>
/// Appends the interpolated string or <see cref="FormattableString"/> to the builder.
/// </summary>
/// <param name="formattable">The <see cref="FormattableString"/>.</param>
/// <returns>The <see cref="Builder"/> instance.</returns>
public abstract Builder AppendIntact(FormattableString formattable);
/// <summary>
/// Appends the interpolated string or <see cref="FormattableString"/> to the builder.
/// </summary>
/// <param name="condition">The value to determine whether the method should be executed.</param>
/// <param name="formattable">The <see cref="FormattableString"/>.</param>
/// <returns>The <see cref="Builder"/> instance.</returns>
public abstract Builder AppendIntact(bool condition, FormattableString formattable);
/// <summary>
/// Appends a <see cref="Environment.NewLine"/> and the interpolated string or <see cref="FormattableString"/> to the builder.
/// </summary>
/// <param name="formattable">The <see cref="FormattableString"/>.</param>
/// <returns>The <see cref="Builder"/> instance.</returns>
public abstract Builder AppendNewLine(FormattableString formattable);
/// <summary>
/// Appends a <see cref="Environment.NewLine"/> and the interpolated string or <see cref="FormattableString"/> to the builder.
/// </summary>
/// <param name="condition">The value to determine whether the method should be executed.</param>
/// <param name="formattable">The <see cref="FormattableString"/>.</param>
/// <returns>The <see cref="Builder"/> instance.</returns>
public abstract Builder AppendNewLine(bool condition, FormattableString formattable);
#endif
/// <summary>
/// Appends a <see cref="Environment.NewLine"/> to the builder.
/// </summary>
/// <returns>The <see cref="Builder"/> instance.</returns>
public abstract Builder AppendNewLine();
/// <summary>
/// Adds a parameter to the dynamic <see cref="Parameters">parameters</see> list.
/// </summary>
/// <param name="name">The name of the parameter.</param>
/// <param name="value">The value of the parameter.</param>
/// <param name="dbType">The <see cref="DbType"/> of the parameter.</param>
/// <param name="direction">The in or out <see cref="ParameterDirection"/> of the parameter.</param>
/// <param name="size">The size of the parameter.</param>
/// <param name="precision">The precision of the parameter.</param>
/// <param name="scale">The scale of the parameter.</param>
/// <returns>The <see cref="Builder"/> instance.</returns>
public abstract Builder AddParameter(string name, object? value = null, DbType? dbType = null, ParameterDirection? direction = null, int? size = null, byte? precision = null, byte? scale = null);
/// <summary>
/// Appends an object containing parameters to the dynamic <see cref="Parameters">parameters</see> bag.
/// <example>
/// <para>Example 1:</para>
/// <code>
/// // Add property A and B to the builder.
/// AddDynamicParameters(new {A = 1, B = 2});
/// </code>
/// <para>Example 2:</para>
/// <code>
/// // Create a DynamicParameters object to hold the parameters.
/// var dynamicParameters = new DynamicParameters();
/// // Add parameters A and B to the dynamicParameters object.
/// dynamicParameters.Add("A", 1);
/// dynamicParameters.Add("B", 2);
/// // Add the dynamicParameters object to the builder.
/// AddDynamicParameters(dynamicParameters);
/// </code>
/// </example>
/// </summary>
/// <param name="parameter">The parameter.</param>
/// <returns>The <see cref="Builder"/> instance.</returns>
public abstract Builder AddDynamicParameters(object? parameter);
/// <summary>
/// Gets the value of a parameter.
/// </summary>
/// <typeparam name="T">The type to cast the value to.</typeparam>
/// <param name="parameterName">The name of the parameter.</param>
/// <returns>The value of the parameter. Note <see cref="DBNull.Value">DBNull.Value</see> is not returned, instead the value is returned as <see langword="null"/>.</returns>
public abstract T GetValue<T>(string parameterName);
/// <summary>
/// Resets the builder to its initial state.
/// <para>This resets the <see cref="Parameters">parameters</see> and the <see cref="Sql">SQL</see> string.</para>
/// </summary>
public abstract void Reset();
}