using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace WebAPIBase.NetCore.BusinessCore { public class SqlStrategy { public static string CheckSqlParamNumber(string input) { if (!string.IsNullOrEmpty(input)) { try { double.Parse(input); return input; } catch { } } return ""; } public static string CheckSqlParamDate(string input) { if (!string.IsNullOrEmpty(input)) { try { DateTime.Parse(input); return input; } catch { } } return ""; } public static string CheckSqlParamStringRange(string input) { if (input.IndexOf(")") >= 0) { return ""; } return input; } public static string CheckSqlParamIn(string input) { string pattern = "^\\d+(\\,\\d+)*$"; if (Regex.IsMatch(input, pattern)) { return input; } return ""; } public static string DeleteSpace(string input) { return Regex.Replace(input, "\\s", ""); } public static bool CheckStrategy(Strategy strategy) { string text = ""; switch (strategy.Type) { case StrategyType.NumberIn: text = CheckSqlParamIn(strategy.GetParameter(0)); if (text != strategy.GetParameter(0)) { return false; } break; case StrategyType.IntegerEqual: text = CheckSqlParamNumber(strategy.GetParameter(0)); if (text != strategy.GetParameter(0)) { return false; } break; case StrategyType.IntegerRange: case StrategyType.FloatRange: text = CheckSqlParamNumber(strategy.GetParameter(0)); if (text != strategy.GetParameter(0)) { return false; } text = CheckSqlParamNumber(strategy.GetParameter(1)); if (text != strategy.GetParameter(1)) { return false; } break; case StrategyType.DateTimeEqual: case StrategyType.DateTimeEqualOnlyDate: text = CheckSqlParamDate(strategy.GetParameter(0)); if (text != strategy.GetParameter(0)) { return false; } break; case StrategyType.DateTimeRange: case StrategyType.DateTimeRangeOnlyDate: text = CheckSqlParamDate(strategy.GetParameter(0)); if (text != strategy.GetParameter(0)) { return false; } text = CheckSqlParamDate(strategy.GetParameter(1)); if (text != strategy.GetParameter(1)) { return false; } break; case StrategyType.StringIn: strategy.SetParameter(0, strategy.GetParameter(0).Replace("'", "''")); break; case StrategyType.StringRange: text = CheckSqlParamStringRange(strategy.GetParameter(0)); if (text != strategy.GetParameter(0)) { return false; } break; } return true; } public static string FormatStrategyParamInString(string input) { string text = input.Replace("'", ""); return "'" + text.Replace(",", "','") + "'"; } public static decimal ToDecimal(object val) { decimal result = default(decimal); if (val != null && val.ToString() != "") { try { try { result = Convert.ToDecimal(val); return result; } catch { result = decimal.Parse(val.ToString()); return result; } } catch { } } return result; } public static object ToDecimalObj(object val) { object result = DBNull.Value; if (val != null && val.ToString() != "") { try { result = decimal.Parse(val.ToString()); } catch { } } return result; } public static double ToDouble(object val) { double result = 0.0; if (val != null && val.ToString() != "") { try { result = double.Parse(val.ToString()); } catch { } } return result; } public static object ToDoubleObj(object val) { object result = DBNull.Value; if (val != null && val.ToString() != "") { try { result = double.Parse(val.ToString()); } catch { } } return result; } public static int ToInt(object val) { int result = 0; if (val != null && val.ToString() != "") { try { result = int.Parse(val.ToString().Replace(",", "")); } catch { } } return result; } public static int? ToIntNull(object val) { int? result = null; if (val != null && val.ToString() != "") { try { result = int.Parse(val.ToString().Replace(",", "")); return result; } catch { } } return result; } public static object ToIntObj(object val) { object result = DBNull.Value; if (val != null && val.ToString() != "") { try { result = int.Parse(val.ToString().Replace(",", "")); } catch { } } return result; } public static long ToLong(object val) { long result = 0L; if (val != null && val.ToString() != "") { try { result = long.Parse(val.ToString()); } catch { } } return result; } public static object ToLongObj(object val) { object result = DBNull.Value; if (val != null && val.ToString() != "") { try { result = long.Parse(val.ToString()); } catch { } } return result; } public static object ToDate(object val) { object result = DBNull.Value; if (val != null && val.ToString() != "") { try { result = DateTime.Parse(val.ToString()); } catch { } } return result; } public static DateTime? ToDateTime(object val) { DateTime? result = null; if (val != null && val.ToString() != "") { try { result = DateTime.Parse(val.ToString()); return result; } catch { } } return result; } public static string ToDateString(object val, string format) { string result = ""; if (val != null && val.ToString() != "") { try { if (DateTime.Parse(val.ToString()) != DateTime.MinValue) { result = DateTime.Parse(val.ToString()).ToString(format); } } catch { } } return result; } public static string ToDateString(object val, string format, string nullTimeFormat) { string result = ""; if (val != null && val.ToString() != "") { try { if (DateTime.Parse(val.ToString()) != DateTime.MinValue) { DateTime dateTime = DateTime.Parse(val.ToString()); result = ((dateTime.Hour != 0 || dateTime.Minute != 0 || dateTime.Second != 0) ? dateTime.ToString(format) : dateTime.ToString(nullTimeFormat)); } } catch { } } return result; } public static string ToDateString(object val) { return ToDateString(val, "yyyy-MM-dd"); } public static string ToString(object val) { string result = ""; try { if (val != null && val != DBNull.Value) { try { result = val.ToString(); } catch { } } } catch { } return result; } public static bool ToBool(object val) { bool result = false; try { if (val != null && val != DBNull.Value) { try { result = bool.Parse(val.ToString()); } catch { } } } catch { } return result; } } }