SqlStrategy.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. namespace WebAPIBase.NetCore.BusinessCore
  8. {
  9. public class SqlStrategy
  10. {
  11. public static string CheckSqlParamNumber(string input)
  12. {
  13. if (!string.IsNullOrEmpty(input))
  14. {
  15. try
  16. {
  17. double.Parse(input);
  18. return input;
  19. }
  20. catch
  21. {
  22. }
  23. }
  24. return "";
  25. }
  26. public static string CheckSqlParamDate(string input)
  27. {
  28. if (!string.IsNullOrEmpty(input))
  29. {
  30. try
  31. {
  32. DateTime.Parse(input);
  33. return input;
  34. }
  35. catch
  36. {
  37. }
  38. }
  39. return "";
  40. }
  41. public static string CheckSqlParamStringRange(string input)
  42. {
  43. if (input.IndexOf(")") >= 0)
  44. {
  45. return "";
  46. }
  47. return input;
  48. }
  49. public static string CheckSqlParamIn(string input)
  50. {
  51. string pattern = "^\\d+(\\,\\d+)*$";
  52. if (Regex.IsMatch(input, pattern))
  53. {
  54. return input;
  55. }
  56. return "";
  57. }
  58. public static string DeleteSpace(string input)
  59. {
  60. return Regex.Replace(input, "\\s", "");
  61. }
  62. public static bool CheckStrategy(Strategy strategy)
  63. {
  64. string text = "";
  65. switch (strategy.Type)
  66. {
  67. case StrategyType.NumberIn:
  68. text = CheckSqlParamIn(strategy.GetParameter(0));
  69. if (text != strategy.GetParameter(0))
  70. {
  71. return false;
  72. }
  73. break;
  74. case StrategyType.IntegerEqual:
  75. text = CheckSqlParamNumber(strategy.GetParameter(0));
  76. if (text != strategy.GetParameter(0))
  77. {
  78. return false;
  79. }
  80. break;
  81. case StrategyType.IntegerRange:
  82. case StrategyType.FloatRange:
  83. text = CheckSqlParamNumber(strategy.GetParameter(0));
  84. if (text != strategy.GetParameter(0))
  85. {
  86. return false;
  87. }
  88. text = CheckSqlParamNumber(strategy.GetParameter(1));
  89. if (text != strategy.GetParameter(1))
  90. {
  91. return false;
  92. }
  93. break;
  94. case StrategyType.DateTimeEqual:
  95. case StrategyType.DateTimeEqualOnlyDate:
  96. text = CheckSqlParamDate(strategy.GetParameter(0));
  97. if (text != strategy.GetParameter(0))
  98. {
  99. return false;
  100. }
  101. break;
  102. case StrategyType.DateTimeRange:
  103. case StrategyType.DateTimeRangeOnlyDate:
  104. text = CheckSqlParamDate(strategy.GetParameter(0));
  105. if (text != strategy.GetParameter(0))
  106. {
  107. return false;
  108. }
  109. text = CheckSqlParamDate(strategy.GetParameter(1));
  110. if (text != strategy.GetParameter(1))
  111. {
  112. return false;
  113. }
  114. break;
  115. case StrategyType.StringIn:
  116. strategy.SetParameter(0, strategy.GetParameter(0).Replace("'", "''"));
  117. break;
  118. case StrategyType.StringRange:
  119. text = CheckSqlParamStringRange(strategy.GetParameter(0));
  120. if (text != strategy.GetParameter(0))
  121. {
  122. return false;
  123. }
  124. break;
  125. }
  126. return true;
  127. }
  128. public static string FormatStrategyParamInString(string input)
  129. {
  130. string text = input.Replace("'", "");
  131. return "'" + text.Replace(",", "','") + "'";
  132. }
  133. public static decimal ToDecimal(object val)
  134. {
  135. decimal result = default(decimal);
  136. if (val != null && val.ToString() != "")
  137. {
  138. try
  139. {
  140. try
  141. {
  142. result = Convert.ToDecimal(val);
  143. return result;
  144. }
  145. catch
  146. {
  147. result = decimal.Parse(val.ToString());
  148. return result;
  149. }
  150. }
  151. catch
  152. {
  153. }
  154. }
  155. return result;
  156. }
  157. public static object ToDecimalObj(object val)
  158. {
  159. object result = DBNull.Value;
  160. if (val != null && val.ToString() != "")
  161. {
  162. try
  163. {
  164. result = decimal.Parse(val.ToString());
  165. }
  166. catch
  167. {
  168. }
  169. }
  170. return result;
  171. }
  172. public static double ToDouble(object val)
  173. {
  174. double result = 0.0;
  175. if (val != null && val.ToString() != "")
  176. {
  177. try
  178. {
  179. result = double.Parse(val.ToString());
  180. }
  181. catch
  182. {
  183. }
  184. }
  185. return result;
  186. }
  187. public static object ToDoubleObj(object val)
  188. {
  189. object result = DBNull.Value;
  190. if (val != null && val.ToString() != "")
  191. {
  192. try
  193. {
  194. result = double.Parse(val.ToString());
  195. }
  196. catch
  197. {
  198. }
  199. }
  200. return result;
  201. }
  202. public static int ToInt(object val)
  203. {
  204. int result = 0;
  205. if (val != null && val.ToString() != "")
  206. {
  207. try
  208. {
  209. result = int.Parse(val.ToString().Replace(",", ""));
  210. }
  211. catch
  212. {
  213. }
  214. }
  215. return result;
  216. }
  217. public static int? ToIntNull(object val)
  218. {
  219. int? result = null;
  220. if (val != null && val.ToString() != "")
  221. {
  222. try
  223. {
  224. result = int.Parse(val.ToString().Replace(",", ""));
  225. return result;
  226. }
  227. catch
  228. {
  229. }
  230. }
  231. return result;
  232. }
  233. public static object ToIntObj(object val)
  234. {
  235. object result = DBNull.Value;
  236. if (val != null && val.ToString() != "")
  237. {
  238. try
  239. {
  240. result = int.Parse(val.ToString().Replace(",", ""));
  241. }
  242. catch
  243. {
  244. }
  245. }
  246. return result;
  247. }
  248. public static long ToLong(object val)
  249. {
  250. long result = 0L;
  251. if (val != null && val.ToString() != "")
  252. {
  253. try
  254. {
  255. result = long.Parse(val.ToString());
  256. }
  257. catch
  258. {
  259. }
  260. }
  261. return result;
  262. }
  263. public static object ToLongObj(object val)
  264. {
  265. object result = DBNull.Value;
  266. if (val != null && val.ToString() != "")
  267. {
  268. try
  269. {
  270. result = long.Parse(val.ToString());
  271. }
  272. catch
  273. {
  274. }
  275. }
  276. return result;
  277. }
  278. public static object ToDate(object val)
  279. {
  280. object result = DBNull.Value;
  281. if (val != null && val.ToString() != "")
  282. {
  283. try
  284. {
  285. result = DateTime.Parse(val.ToString());
  286. }
  287. catch
  288. {
  289. }
  290. }
  291. return result;
  292. }
  293. public static DateTime? ToDateTime(object val)
  294. {
  295. DateTime? result = null;
  296. if (val != null && val.ToString() != "")
  297. {
  298. try
  299. {
  300. result = DateTime.Parse(val.ToString());
  301. return result;
  302. }
  303. catch
  304. {
  305. }
  306. }
  307. return result;
  308. }
  309. public static string ToDateString(object val, string format)
  310. {
  311. string result = "";
  312. if (val != null && val.ToString() != "")
  313. {
  314. try
  315. {
  316. if (DateTime.Parse(val.ToString()) != DateTime.MinValue)
  317. {
  318. result = DateTime.Parse(val.ToString()).ToString(format);
  319. }
  320. }
  321. catch
  322. {
  323. }
  324. }
  325. return result;
  326. }
  327. public static string ToDateString(object val, string format, string nullTimeFormat)
  328. {
  329. string result = "";
  330. if (val != null && val.ToString() != "")
  331. {
  332. try
  333. {
  334. if (DateTime.Parse(val.ToString()) != DateTime.MinValue)
  335. {
  336. DateTime dateTime = DateTime.Parse(val.ToString());
  337. result = ((dateTime.Hour != 0 || dateTime.Minute != 0 || dateTime.Second != 0) ? dateTime.ToString(format) : dateTime.ToString(nullTimeFormat));
  338. }
  339. }
  340. catch
  341. {
  342. }
  343. }
  344. return result;
  345. }
  346. public static string ToDateString(object val)
  347. {
  348. return ToDateString(val, "yyyy-MM-dd");
  349. }
  350. public static string ToString(object val)
  351. {
  352. string result = "";
  353. try
  354. {
  355. if (val != null && val != DBNull.Value)
  356. {
  357. try
  358. {
  359. result = val.ToString();
  360. }
  361. catch
  362. {
  363. }
  364. }
  365. }
  366. catch
  367. {
  368. }
  369. return result;
  370. }
  371. public static bool ToBool(object val)
  372. {
  373. bool result = false;
  374. try
  375. {
  376. if (val != null && val != DBNull.Value)
  377. {
  378. try
  379. {
  380. result = bool.Parse(val.ToString());
  381. }
  382. catch
  383. {
  384. }
  385. }
  386. }
  387. catch
  388. {
  389. }
  390. return result;
  391. }
  392. }
  393. }