refactor(SQL): move to common
This commit is contained in:
30
EnvelopeGenerator.Application/Common/SQL/ParamsExtensions.cs
Normal file
30
EnvelopeGenerator.Application/Common/SQL/ParamsExtensions.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace EnvelopeGenerator.Application.Common.SQL;
|
||||
|
||||
/// <summary>
|
||||
/// Extension method for converting objects to SQL parameter strings.
|
||||
/// </summary>
|
||||
public static class ParamsExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a .NET object to its corresponding SQL-safe parameter string.
|
||||
/// </summary>
|
||||
/// <param name="obj">The object to convert.</param>
|
||||
/// <returns>A string representing the SQL parameter.</returns>
|
||||
public static string ToSqlParam(this object? obj)
|
||||
{
|
||||
if (obj is null)
|
||||
return "NULL";
|
||||
else if (obj is string strVal)
|
||||
return $"'{strVal}'";
|
||||
else if (obj is bool boolVal)
|
||||
return boolVal ? "1" : "0";
|
||||
else if (obj is double doubleVal)
|
||||
return $"'{doubleVal.ToString(CultureInfo.InvariantCulture)}'";
|
||||
else if (obj is int intVal)
|
||||
return intVal.ToString();
|
||||
else
|
||||
throw new NotSupportedException($"Type '{obj.GetType().FullName}' is not supported for SQL parameter conversion.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user