123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- 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;
- }
- }
- }
|