Skip to content

Commit 1388209

Browse files
committed
Code optimization
1 parent fa40dfd commit 1388209

4 files changed

Lines changed: 14 additions & 7 deletions

File tree

Sources/Command/SqlOrderHint.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespace Belin.Sql;
22

3+
using System.Globalization;
4+
35
/// <summary>
46
/// Defines the sort order for a database column.
57
/// </summary>
@@ -31,7 +33,7 @@ public sealed class SqlOrderHint(string column, SortOrder sortOrder = SortOrder.
3133
/// <returns>The order hint corresponding to the specified tuple.</returns>
3234
/// <exception cref="ArgumentException">The specified array does not contain a column name and a sort order.</param>
3335
public static implicit operator SqlOrderHint(object?[] orderHint) => orderHint.Length == 2
34-
? new(orderHint[0]?.ToString() ?? "", orderHint[1] is SortOrder sortOrder ? sortOrder : Enum.Parse<SortOrder>(orderHint[1]?.ToString() ?? "", ignoreCase: true))
36+
? new(Convert.ToString(orderHint[0], CultureInfo.InvariantCulture) ?? "", orderHint[1] is SortOrder sortOrder ? sortOrder : Enum.Parse<SortOrder>(Convert.ToString(orderHint[1], CultureInfo.InvariantCulture) ?? "", ignoreCase: true))
3537
: throw new ArgumentException("The specified array must contain a column name and a sort order.", nameof(orderHint));
3638

3739
/// <summary>

Sources/Command/SqlOrderHintCollection.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace Belin.Sql;
22

33
using System.Collections;
44
using System.Collections.Specialized;
5+
using System.Globalization;
56

67
/// <summary>
78
/// A collection of hints describing the sort order of columns.
@@ -24,7 +25,7 @@ public class SqlOrderHintCollection(params IEnumerable<SqlOrderHint> orderHints)
2425
/// <param name="columns">The array whose elements are copied to the order hint collection.</param>
2526
/// <returns>The order hint collection corresponding to the specified array of column names.</returns>
2627
public static implicit operator SqlOrderHintCollection(object?[] columns) =>
27-
[.. columns.Select(value => new SqlOrderHint(value?.ToString() ?? "", SortOrder.Ascending))];
28+
[.. columns.Select(value => new SqlOrderHint(Convert.ToString(value, CultureInfo.InvariantCulture) ?? "", SortOrder.Ascending))];
2829

2930
/// <summary>
3031
/// Creates a new order hint collection from the specified array of column names.
@@ -48,8 +49,8 @@ public static implicit operator SqlOrderHintCollection(List<string> columns) =>
4849
/// <param name="orderHints">The dictionary whose elements are copied to the order hint collection.</param>
4950
/// <returns>The order hint collection corresponding to the specified dictionary of column names and sort orders.</returns>
5051
public static implicit operator SqlOrderHintCollection(OrderedDictionary orderHints) => [.. orderHints.Cast<DictionaryEntry>().Select(entry => {
51-
var value = entry.Value is SortOrder sortOrder ? sortOrder : Enum.Parse<SortOrder>(entry.Value?.ToString() ?? "", ignoreCase: true);
52-
return new SqlOrderHint(entry.Key.ToString() ?? "", value);
52+
var value = entry.Value is SortOrder sortOrder ? sortOrder : Enum.Parse<SortOrder>(Convert.ToString(entry.Value, CultureInfo.InvariantCulture) ?? "", ignoreCase: true);
53+
return new SqlOrderHint(Convert.ToString(entry.Key, CultureInfo.InvariantCulture) ?? "", value);
5354
})];
5455

5556
/// <summary>

Sources/Command/SqlParameter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace Belin.Sql;
22

33
using System.Data;
44
using System.Diagnostics.CodeAnalysis;
5+
using System.Globalization;
56

67
/// <summary>
78
/// Represents a parameter of a parameterized SQL statement.
@@ -56,7 +57,7 @@ public sealed class SqlParameter(string name = "?", object? value = null) {
5657
/// <returns>The parameter corresponding to the specified tuple.</returns>
5758
/// <exception cref="ArgumentException">The specified array does not contain a parameter name and a value.</param>
5859
public static implicit operator SqlParameter(object?[] parameter) => parameter.Length == 2
59-
? new(parameter[0]?.ToString() ?? "", parameter[1])
60+
? new(Convert.ToString(parameter[0], CultureInfo.InvariantCulture) ?? "", parameter[1])
6061
: throw new ArgumentException("The specified array must contain a parameter name and a value.", nameof(parameter));
6162

6263
/// <summary>

Sources/Command/SqlParameterCollection.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace Belin.Sql;
22

33
using System.Collections;
4+
using System.Globalization;
45

56
/// <summary>
67
/// Collects all parameters relevant to a parameterized SQL statement.
@@ -53,8 +54,10 @@ public static implicit operator SqlParameterCollection(Dictionary<string, object
5354
/// </summary>
5455
/// <param name="parameters">The hash table whose elements are copied to the parameter collection.</param>
5556
/// <returns>The parameter collection corresponding to the specified hash table of named parameters.</returns>
56-
public static implicit operator SqlParameterCollection(Hashtable parameters) =>
57-
parameters.Cast<DictionaryEntry>().ToDictionary(entry => entry.Key.ToString() ?? "", entry => entry.Value);
57+
public static implicit operator SqlParameterCollection(Hashtable parameters) => parameters.Cast<DictionaryEntry>().ToDictionary(
58+
entry => Convert.ToString(entry.Key, CultureInfo.InvariantCulture) ?? "",
59+
entry => entry.Value
60+
);
5861

5962
/// <summary>
6063
/// Adds a new positional parameter to the end of this collection.

0 commit comments

Comments
 (0)