31 lines
1.0 KiB
C#

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.");
}
}