Skip to content

Commit 7ff1838

Browse files
committed
Module merging
1 parent e9bdcc1 commit 7ff1838

13 files changed

Lines changed: 846 additions & 860 deletions

Sources/Command/SortOrder.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

Sources/Command/SqlCommand.cs

Lines changed: 421 additions & 0 deletions
Large diffs are not rendered by default.

Sources/Command/SqlCommandBuilder.cs

Lines changed: 0 additions & 424 deletions
This file was deleted.

Sources/Command/SqlOrderHint.cs

Lines changed: 0 additions & 52 deletions
This file was deleted.

Sources/Command/SqlOrderHintCollection.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,71 @@ namespace Belin.Sql;
44
using System.Collections.Specialized;
55
using System.Globalization;
66

7+
/// <summary>
8+
/// Specifies how rows of data are sorted.
9+
/// </summary>
10+
public enum SortOrder {
11+
12+
/// <summary>
13+
/// The rows are sorted in ascending order.
14+
/// </summary>
15+
Ascending,
16+
17+
/// <summary>
18+
/// The rows are sorted in descending order.
19+
/// </summary>
20+
Descending
21+
}
22+
23+
/// <summary>
24+
/// Defines the sort order for a database column.
25+
/// </summary>
26+
/// <param name="column">The name of the column for which the hint is being provided.</param>
27+
/// <param name="sortOrder">The sort order of the column.</param>
28+
public sealed class SqlOrderHint(string column, SortOrder sortOrder = SortOrder.Ascending) {
29+
30+
/// <summary>
31+
/// The name of the column for which the hint is being provided.
32+
/// </summary>
33+
public string Column { get; } = column;
34+
35+
/// <summary>
36+
/// The sort order of the column.
37+
/// </summary>
38+
public SortOrder SortOrder { get; set; } = sortOrder;
39+
40+
/// <summary>
41+
/// Creates a new order hint from the specified column name.
42+
/// </summary>
43+
/// <param name="column">The column name.</param>
44+
/// <returns>The command corresponding to the specified column name.</returns>
45+
public static implicit operator SqlOrderHint(string column) => new(column);
46+
47+
/// <summary>
48+
/// Creates a new order hint from the specified tuple.
49+
/// </summary>
50+
/// <param name="orderHint">The tuple providing the column name and its sort order.</param>
51+
/// <returns>The order hint corresponding to the specified tuple.</returns>
52+
/// <exception cref="ArgumentException">The specified array does not contain a column name and a sort order.</param>
53+
public static implicit operator SqlOrderHint(object[] orderHint) => orderHint.Length == 2
54+
? new(Convert.ToString(orderHint[0], CultureInfo.InvariantCulture) ?? "", orderHint[1] is SortOrder sortOrder ? sortOrder : Enum.Parse<SortOrder>(Convert.ToString(orderHint[1], CultureInfo.InvariantCulture) ?? "", ignoreCase: true))
55+
: throw new ArgumentException("The specified array must contain a column name and a sort order.", nameof(orderHint));
56+
57+
/// <summary>
58+
/// Creates a new order hint from the specified tuple.
59+
/// </summary>
60+
/// <param name="orderHint">The tuple providing the column name and its sort order.</param>
61+
/// <returns>The order hint corresponding to the specified tuple.</returns>
62+
public static implicit operator SqlOrderHint((string Column, SortOrder SortOrder) orderHint) => new(orderHint.Column, orderHint.SortOrder);
63+
64+
/// <summary>
65+
/// Creates a new order hint from the specified key/value pair.
66+
/// </summary>
67+
/// <param name="orderHint">The key/value pair providing the column name and its sort order.</param>
68+
/// <returns>The order hint corresponding to the specified key/value pair.</returns>
69+
public static implicit operator SqlOrderHint(KeyValuePair<string, SortOrder> orderHint) => new(orderHint.Key, orderHint.Value);
70+
}
71+
772
/// <summary>
873
/// A collection of hints describing the sort order of columns.
974
/// </summary>

Sources/Command/SqlParameter.cs

Lines changed: 0 additions & 112 deletions
This file was deleted.

Sources/Command/SqlParameterCollection.cs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,117 @@
11
namespace Belin.Sql;
22

33
using System.Collections;
4+
using System.Data;
5+
using System.Diagnostics.CodeAnalysis;
46
using System.Globalization;
57

8+
/// <summary>
9+
/// Represents a parameter of a parameterized SQL statement.
10+
/// </summary>
11+
public sealed class SqlParameter(string name = "?", object? value = null) {
12+
13+
/// <summary>
14+
/// The prefixes used for parameter placeholders.
15+
/// </summary>
16+
private static readonly char[] prefixes = ['?', '@', ':', '$'];
17+
18+
/// <summary>
19+
/// The database type of this parameter.
20+
/// </summary>
21+
public DbType? DbType { get; set; }
22+
23+
/// <summary>
24+
/// Value indicating whether this parameter is input-only, output-only, bidirectional, or a stored procedure return value parameter.
25+
/// </summary>
26+
public ParameterDirection? Direction { get; set; }
27+
28+
/// <summary>
29+
/// The parameter name.
30+
/// </summary>
31+
public string Name { get; set => field = NormalizeName(value); } = NormalizeName(name);
32+
33+
/// <summary>
34+
/// Indicates the precision of numeric parameters.
35+
/// </summary>
36+
public byte? Precision { get; set; }
37+
38+
/// <summary>
39+
/// Indicates the scale of numeric parameters.
40+
/// </summary>
41+
public byte? Scale { get; set; }
42+
43+
/// <summary>
44+
/// The maximum size of this parameter, in bytes.
45+
/// </summary>
46+
public int? Size { get; set; }
47+
48+
/// <summary>
49+
/// The parameter value.
50+
/// </summary>
51+
[NotNull]
52+
public object? Value { get; set => field = NormalizeValue(value); } = NormalizeValue(value);
53+
54+
/// <summary>
55+
/// Creates a new parameter from the specified tuple.
56+
/// </summary>
57+
/// <param name="parameter">The tuple providing the parameter name and value.</param>
58+
/// <returns>The parameter corresponding to the specified tuple.</returns>
59+
/// <exception cref="ArgumentException">The specified array does not contain a parameter name and a value.</param>
60+
public static implicit operator SqlParameter(object?[] parameter) => parameter.Length == 2
61+
? new(Convert.ToString(parameter[0], CultureInfo.InvariantCulture) ?? "", parameter[1])
62+
: throw new ArgumentException("The specified array must contain a parameter name and a value.", nameof(parameter));
63+
64+
/// <summary>
65+
/// Creates a new parameter from the specified tuple.
66+
/// </summary>
67+
/// <param name="parameter">The tuple providing the parameter name and value.</param>
68+
/// <returns>The parameter corresponding to the specified tuple.</returns>
69+
public static implicit operator SqlParameter((string Name, object? Value) parameter) => new(parameter.Name, parameter.Value);
70+
71+
/// <summary>
72+
/// Creates a new parameter from the specified key/value pair.
73+
/// </summary>
74+
/// <param name="parameter">The key/value pair providing the parameter name and value.</param>
75+
/// <returns>The parameter corresponding to the specified key/value pair.</returns>
76+
public static implicit operator SqlParameter(KeyValuePair<string, object?> parameter) => new(parameter.Key, parameter.Value);
77+
78+
/// <summary>
79+
/// Converts this parameter into an <see cref="IDbDataParameter"/> object.
80+
/// </summary>
81+
/// <param name="command">The command to associate with the created parameter.</param>
82+
/// <returns>The <see cref="IDbDataParameter"/> object corresponding to this parameter.</returns>
83+
public IDbDataParameter ToDbParameter(IDbCommand command) {
84+
var parameter = command.CreateParameter();
85+
parameter.ParameterName = Name;
86+
parameter.Value = Value;
87+
if (DbType is not null) parameter.DbType = DbType.Value;
88+
if (Direction is not null) parameter.Direction = Direction.Value;
89+
if (Precision is not null) parameter.Precision = Precision.Value;
90+
if (Scale is not null) parameter.Scale = Scale.Value;
91+
if (Size is not null) parameter.Size = Size.Value;
92+
return parameter;
93+
}
94+
95+
/// <summary>
96+
/// Normalizes the specified parameter name.
97+
/// </summary>
98+
/// <param name="name">The parameter name.</param>
99+
/// <returns>The normalized parameter name.</returns>
100+
internal static string NormalizeName(string name) =>
101+
name.Length == 0 ? "?" : (prefixes.Contains(name[0]) ? name : $"@{name}");
102+
103+
/// <summary>
104+
/// Normalizes the specified parameter value.
105+
/// </summary>
106+
/// <param name="value">The parameter value.</param>
107+
/// <returns>The normalized parameter value.</returns>
108+
internal static object NormalizeValue(object? value) {
109+
if (value is null) return DBNull.Value;
110+
if (PowerShell.PSObject is null || value.GetType() != PowerShell.PSObject) return value;
111+
return PowerShell.PSObject.GetProperty("BaseObject")!.GetValue(value) ?? DBNull.Value;
112+
}
113+
}
114+
6115
/// <summary>
7116
/// Collects all parameters relevant to a parameterized SQL statement.
8117
/// </summary>

0 commit comments

Comments
 (0)