|
1 | 1 | namespace Belin.Sql; |
2 | 2 |
|
3 | 3 | using System.Collections; |
| 4 | +using System.Data; |
| 5 | +using System.Diagnostics.CodeAnalysis; |
4 | 6 | using System.Globalization; |
5 | 7 |
|
| 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 | + |
6 | 115 | /// <summary> |
7 | 116 | /// Collects all parameters relevant to a parameterized SQL statement. |
8 | 117 | /// </summary> |
|
0 commit comments