SQLHelper.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.Json;
  8. using System.Text.RegularExpressions;
  9. using TEAMModelOS.SDK;
  10. namespace TEAMModelOS.SDK.DI
  11. {
  12. public class SQLHelper
  13. {
  14. static readonly string[] LogicOpers = new string[] { " and ", " or " };
  15. static readonly string[] CompareOpers = new string[] { " > ", " < ", " <= ", " >= ", " = ", " != ", " like ", " not like ", " in " };
  16. public static void ReplaceKeyWords(ref StringBuilder sql)
  17. {
  18. sql.Replace(".order.", "['order'].");
  19. sql.Replace(".order ", "['order'] ");
  20. sql.Replace(".Order.", "['Order'].");
  21. sql.Replace(".Order ", "['Order'] ");
  22. sql.Replace(".group.", "['group'].");
  23. sql.Replace(".group ", "['group'] ");
  24. sql.Replace(".Group.", "['Group'].");
  25. sql.Replace(".Group ", "['Group'] ");
  26. sql.Replace(".end.", "['end'].");
  27. sql.Replace(".end ", "['end'] ");
  28. sql.Replace(".End.", "['End'].");
  29. sql.Replace(".End ", "['End'] ");
  30. sql.Replace(".having.", "['having'].");
  31. sql.Replace(".having ", "['having'] ");
  32. sql.Replace(".Having.", "['Having'].");
  33. sql.Replace(".Having ", "['Having'] ");
  34. }
  35. public static StringBuilder GetSQLSelect(List<string> propertys)
  36. {
  37. StringBuilder sql;
  38. if (propertys.IsNotEmpty())
  39. {
  40. string tmpSQL = " select ";
  41. int i = 0;
  42. foreach (string item in propertys)
  43. {
  44. if (i == 0)
  45. {
  46. tmpSQL += " c." + item + " ";
  47. }
  48. else
  49. {
  50. tmpSQL += " ,c." + item + " ";
  51. }
  52. i++;
  53. }
  54. tmpSQL += "from c";
  55. sql = new StringBuilder(tmpSQL);
  56. }
  57. else
  58. {
  59. sql = new StringBuilder("select value(c) from c");
  60. }
  61. return sql;
  62. }
  63. public static bool DictIsNotNULL(Dictionary<string, object> dict)
  64. {
  65. Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
  66. foreach (KeyValuePair<string, object> keyValuePair in dict)
  67. {
  68. if (keyValuePair.Value is JArray array)
  69. {
  70. if (array == null || array.Count == 0) keyValuePairs.Add(keyValuePair.Key, keyValuePair.Value);
  71. }
  72. else if (keyValuePair.Value is IList enumerable && !(keyValuePair.Value is String))
  73. {
  74. if (enumerable == null || enumerable.Count == 0) keyValuePairs.Add(keyValuePair.Key, keyValuePair.Value);
  75. }
  76. else if (keyValuePair.Value is JsonElement jsonElement && jsonElement.ValueKind is JsonValueKind.Array)
  77. {
  78. if (jsonElement.EnumerateArray().Count() == 0)
  79. {
  80. keyValuePairs.Add(keyValuePair.Key, keyValuePair.Value);
  81. }
  82. }
  83. else if (keyValuePair.Value is null)
  84. {
  85. //keyValuePairs.Add(keyValuePair.Key, keyValuePair.Value);
  86. }
  87. }
  88. if (keyValuePairs.Count > 0)
  89. {
  90. foreach (KeyValuePair<string, object> keyValuePair in keyValuePairs)
  91. {
  92. dict.Remove(keyValuePair.Key);
  93. }
  94. }
  95. if (dict.Keys.Count > 0)
  96. {
  97. return true;
  98. }
  99. else
  100. {
  101. return false;
  102. }
  103. }
  104. public static AzureCosmosQuery GetSQL(JsonElement json, StringBuilder sql, string pk = null)
  105. {
  106. Dictionary<string, object> dict = new Dictionary<string, object>();
  107. var emobj = json.EnumerateObject();
  108. while (emobj.MoveNext())
  109. {
  110. dict[emobj.Current.Name] = emobj.Current.Value;
  111. }
  112. var query = GetSQL(dict, sql, pk);
  113. return query;
  114. }
  115. public static AzureCosmosQuery GetSQL(Dictionary<string, object> dict, StringBuilder sql, string pk = null)
  116. {
  117. AzureCosmosQuery cosmosDbQuery;
  118. if (dict != null && dict.Count > 0)
  119. {
  120. if (!DictIsNotNULL(dict))
  121. {
  122. return null;
  123. }
  124. Dictionary<string, object> parmeters = new Dictionary<string, object>();
  125. int offsetNum = -1;
  126. int limitNum = 0;
  127. bool pageBool = false;
  128. GetPageNum(dict, ref offsetNum, ref limitNum, ref pageBool);
  129. string OrderByValue = "";
  130. //处理code
  131. /*if (dict.TryGetValue("code", out object _))
  132. {
  133. dict.Remove("code");
  134. }*/
  135. // 处理OrderBy
  136. if (dict.TryGetValue("@ASC", out object ASC))
  137. {
  138. OrderByValue = Regex.Replace(ASC.ToString(), "\\s{1,}", "");
  139. dict.Remove("@ASC");
  140. }
  141. if (dict.TryGetValue("@DESC", out object DESC))
  142. {
  143. OrderByValue = Regex.Replace(DESC.ToString(), "\\s{1,}", "");
  144. dict.Remove("@DESC");
  145. };
  146. //处理顺序
  147. Stack<KeyValuePair<string, object>> stack = new Stack<KeyValuePair<string, object>>();
  148. Dictionary<string, object> forGetParmeter = new Dictionary<string, object>();
  149. foreach (string item in dict.Keys)
  150. {
  151. if (item.EndsWith(".|"))
  152. {
  153. stack.Push(new KeyValuePair<string, object>(item, dict[item]));
  154. }
  155. }
  156. foreach (string item in dict.Keys)
  157. {
  158. if (!item.EndsWith(".|"))
  159. {
  160. stack.Push(new KeyValuePair<string, object>(item, dict[item]));
  161. }
  162. }
  163. string Join = " join ";
  164. string instring = " in ";
  165. Dictionary<string, string> keyValues = new Dictionary<string, string>();
  166. StringBuilder WhereString = new StringBuilder();
  167. int heada = 0;
  168. string[] sqlHead = new string[] { "A", "B", "C", "D", "E", "F" };
  169. int kslength = 0;
  170. int logicOperNum = 0;
  171. bool keyListValueList = false;
  172. int keyListValueListNum = 0;
  173. int stackCount = stack.Count;
  174. for (int k = 0; k < stackCount; k++)
  175. {
  176. KeyValuePair<string, object> item = stack.Pop();
  177. forGetParmeter.Add(item.Key, item.Value);
  178. bool isLikeSQL = false;
  179. if (item.Key.StartsWith("$.") || item.Key.StartsWith("!$."))
  180. {
  181. isLikeSQL = true;
  182. }
  183. string key = item.Key;
  184. string[] keyHead = key.Split(".");
  185. int index = 0;
  186. int compareOper = 4;
  187. int logicOper = 0;
  188. if (key.EndsWith(".&"))
  189. {
  190. logicOper = (int)LogicOper.and;
  191. key = key.Replace(".&", "");
  192. }
  193. else if (key.EndsWith(".|"))
  194. {
  195. logicOper = (int)LogicOper.or;
  196. key = key.Replace(".|", "");
  197. }
  198. CompareOperSwitch(keyHead[0], ref key, ref compareOper);
  199. string[] keyBody = key.Split("[*]");
  200. if (keyBody.Length > 1)
  201. {
  202. key = key.Replace("[*]", "");
  203. key = key.Replace(".", "");
  204. key += k;
  205. kslength += keyBody.Length;
  206. if (kslength < (7 + heada))
  207. {
  208. StringBuilder sqlitem = new StringBuilder();
  209. for (int i = 0; i < keyBody.Length - 1; i++)
  210. {
  211. if (i == 0)
  212. {
  213. sqlitem.Append(Join);
  214. string a = sqlHead[heada] + index;
  215. sqlitem.Append(a + " ");
  216. //keyValues.Add(ks[i], a);
  217. keyValues[keyBody[i]] = a;
  218. sqlitem.Append(instring);
  219. sqlitem.Append("c.");
  220. sqlitem.Append(keyBody[i]);
  221. }
  222. else
  223. {
  224. sqlitem.Append(Join);
  225. string a = sqlHead[heada] + index;
  226. sqlitem.Append(a + " ");
  227. //keyValues.Add(ks[i], a);
  228. keyValues[keyBody[i]] = a;
  229. sqlitem.Append(instring);
  230. sqlitem.Append(keyValues[keyBody[i - 1]]);
  231. sqlitem.Append(keyBody[i]);
  232. }
  233. index += 1;
  234. }
  235. sql.Append(sqlitem);
  236. string s = "";
  237. if (isLikeSQL)
  238. {
  239. s = ValueIsLike(sqlHead[heada] + (keyBody.Length - 2) + keyBody[index] + "", key, item.Value, LogicOpers[logicOper], logicOperNum, compareOper, ref keyListValueList);
  240. }
  241. else
  242. {
  243. s = ValueNotLike(sqlHead[heada] + (keyBody.Length - 2) + keyBody[index] + "", key, item.Value, LogicOpers[logicOper], logicOperNum, compareOper, ref keyListValueList);
  244. }
  245. WhereString.Append(s);
  246. if (keyListValueList && keyListValueListNum == 0)
  247. {
  248. sql = sql.Replace("select ", "select distinct ");
  249. keyListValueList = false;
  250. keyListValueListNum++;
  251. }
  252. }
  253. else
  254. {
  255. // throw new BizException("数组总共深度不能超过5层", ResponseCode.PARAMS_ERROR);
  256. }
  257. heada += 1;
  258. }
  259. else
  260. {
  261. string itemKey = key.Replace(".", "");
  262. itemKey += k;
  263. WhereString.Append(KeyNotElement(dict[item.Key], item.Key, itemKey, LogicOpers[logicOper], logicOperNum, compareOper));
  264. }
  265. logicOperNum += 1;
  266. }
  267. if (pk == null)
  268. {
  269. sql.Append(" where 1=1 ").Append(WhereString);
  270. }
  271. else
  272. {
  273. sql.Append(" where c.pk='" + pk + "'").Append(WhereString);
  274. }
  275. if (ASC != null)
  276. {
  277. sql.Append(" Order By c." + OrderByValue);
  278. //sql.Append(" Order By c." + "@OrderByValue");
  279. }
  280. else if (DESC != null)
  281. {
  282. sql.Append(" Order By c." + OrderByValue + " DESC ");
  283. // sql.Append(" Order By c." + "@OrderByValue"+ " DESC " );
  284. }
  285. if (pageBool && offsetNum != -1 && limitNum != 0)
  286. {
  287. //sql.Append(" OFFSET " + offsetNum + " LIMIT " + limitNum);
  288. sql.Append(" OFFSET " + " @offsetNum " + " LIMIT " + " @limitNum ");
  289. }
  290. //替换关键字
  291. ReplaceKeyWords(ref sql);
  292. //参数化查询拼接 参数dict
  293. parmeters = GetParmeter(forGetParmeter, parmeters, offsetNum, limitNum);
  294. cosmosDbQuery = new AzureCosmosQuery
  295. {
  296. QueryText = sql.ToString(),
  297. Parameters = parmeters
  298. };
  299. if (cosmosDbQuery.QueryText.Contains("order", StringComparison.OrdinalIgnoreCase) && cosmosDbQuery.QueryText.Contains("by", StringComparison.OrdinalIgnoreCase) && cosmosDbQuery.QueryText.Contains("distinct", StringComparison.OrdinalIgnoreCase))
  300. {
  301. cosmosDbQuery.QueryText = cosmosDbQuery.QueryText.Replace("distinct", " ");
  302. }
  303. return cosmosDbQuery;
  304. }
  305. cosmosDbQuery = new AzureCosmosQuery
  306. {
  307. QueryText = sql.ToString()
  308. };
  309. if (cosmosDbQuery.QueryText.Contains("order", StringComparison.OrdinalIgnoreCase) && cosmosDbQuery.QueryText.Contains("by", StringComparison.OrdinalIgnoreCase) && cosmosDbQuery.QueryText.Contains("distinct", StringComparison.OrdinalIgnoreCase))
  310. {
  311. cosmosDbQuery.QueryText = cosmosDbQuery.QueryText.Replace("distinct", " ");
  312. }
  313. return cosmosDbQuery;
  314. }
  315. private static void GetPageNum(Dictionary<string, object> dict, ref int offsetNum, ref int limitNum, ref bool pageBool)
  316. {
  317. dict.TryGetValue("@CURRPAGE", out object page);
  318. dict.Remove("@CURRPAGE");
  319. dict.TryGetValue("@PAGESIZE", out object limit);
  320. dict.Remove("@PAGESIZE");
  321. if (page != null && limit != null)
  322. {
  323. pageBool = true;
  324. limitNum = int.Parse(limit.ToString());
  325. if (limitNum < 0)
  326. {
  327. //throw new BizException("PAGESIZE can't be less than 0 !");
  328. }
  329. offsetNum = (int.Parse(page.ToString()) - 1) * limitNum;
  330. if (offsetNum < 0)
  331. {
  332. // throw new BizException("CURRPAGE can't be less than 1 !");
  333. }
  334. }
  335. }
  336. private static void CompareOperSwitch(string keyHead, ref string key, ref int compareOper)
  337. {
  338. switch (keyHead)
  339. {
  340. case ">":
  341. compareOper = (int)CompareOper.moreThan;
  342. key = key.Replace(">.", "");
  343. break;
  344. case "<":
  345. compareOper = (int)CompareOper.lessThan;
  346. key = key.Replace("<.", "");
  347. break;
  348. case "<=":
  349. compareOper = (int)CompareOper.notMoreThan;
  350. key = key.Replace("<=.", "");
  351. break;
  352. case ">=":
  353. compareOper = (int)CompareOper.notLessThan;
  354. key = key.Replace(">=.", "");
  355. break;
  356. case "=":
  357. compareOper = (int)CompareOper.equal;
  358. key = key.Replace("=.", "");
  359. break;
  360. case "!=":
  361. compareOper = (int)CompareOper.notEqual;
  362. key = key.Replace("!=.", "");
  363. break;
  364. case "$":
  365. compareOper = (int)CompareOper.like;
  366. key = key.Replace("$.", "");
  367. break;
  368. case "!$":
  369. compareOper = (int)CompareOper.notLike;
  370. key = key.Replace("!$.", "");
  371. break;
  372. default:
  373. compareOper = 4;
  374. break;
  375. }
  376. }
  377. private static string ValueNotLike(string key, string key1, object value, string logicOperParams, int logicOperNum, int compareOperNum, ref bool keyListValueList)
  378. {
  379. string logicOper = " and ";
  380. string compareOper = " = ";
  381. if (compareOperNum != 4) compareOper = CompareOpers[compareOperNum];
  382. if (logicOperNum != 0) logicOper = logicOperParams;
  383. StringBuilder sql = new StringBuilder(logicOper + key + " in (");
  384. if (value is JArray array)
  385. {
  386. keyListValueList = true;
  387. int aa = 0;
  388. foreach (JValue obja in array)
  389. {
  390. sql.Append(" @" + key1 + aa + " ,");
  391. aa++;
  392. }
  393. string sqls = sql.ToString().Substring(0, sql.Length - 1);
  394. sqls += " ) ";
  395. return sqls;
  396. }
  397. else if (value is IEnumerable enumerable && !(value is String))
  398. {
  399. keyListValueList = true;
  400. int aa = 0;
  401. foreach (object obja in enumerable)
  402. {
  403. sql.Append(" @" + key1 + aa + " ,");
  404. aa++;
  405. }
  406. string sqls = sql.ToString().Substring(0, sql.Length - 1);
  407. sqls += ") ";
  408. return sqls;
  409. }
  410. else if (value is JsonElement jsonElement)
  411. {
  412. if (jsonElement.ValueKind is JsonValueKind.Array)
  413. {
  414. keyListValueList = true;
  415. int aa = 0;
  416. foreach (JsonElement obja in jsonElement.EnumerateArray().ToArray())
  417. {
  418. sql.Append(" @" + key1 + aa + " ,");
  419. aa++;
  420. }
  421. }
  422. else
  423. {
  424. if (compareOperNum != 4) keyListValueList = true;
  425. return logicOper + key + compareOper + " @" + key1 + " ";
  426. }
  427. string sqls = sql.ToString().Substring(0, sql.Length - 1);
  428. sqls += " ) ";
  429. return sqls;
  430. }
  431. else
  432. {
  433. Type s = value.GetType();
  434. TypeCode typeCode = Type.GetTypeCode(s);
  435. if (compareOperNum != 4) keyListValueList = true;
  436. return logicOper + key + compareOper + " @" + key1 + " ";
  437. }
  438. }
  439. private static string ValueIsLike(string key, string key1, object value, string logicOperParams, int logicOperNum, int compareOperNum, ref bool keyListValueList)
  440. {
  441. string compareOperBool = " true ";
  442. compareOperBool = CompareBoolSwitch(compareOperNum);
  443. string logicOper = " and ";
  444. if (logicOperNum != 0) logicOper = logicOperParams;
  445. StringBuilder s = new StringBuilder(logicOper + " ( Contains( ");
  446. if (value is JArray array)
  447. {
  448. keyListValueList = true;
  449. int aa = 0;
  450. foreach (JValue obja in array)
  451. {
  452. if (aa != 0) s.Append("or Contains(");
  453. s.Append(key + "," + " @" + key1 + aa + " ) = " + compareOperBool + " ");
  454. if (obja.Value is string)
  455. {
  456. s.Append(key + "," + " @" + key1 + aa + " ) = " + compareOperBool + " ");
  457. }
  458. else
  459. {
  460. s.Append("ToString( " + key + " )," + " \'@" + key1 + aa + "\' ) = " + compareOperBool + " ");
  461. }
  462. aa++;
  463. }
  464. }
  465. else if (value is IEnumerable enumerable && !(value is String))
  466. {
  467. keyListValueList = true;
  468. int aa = 0;
  469. foreach (object obja in enumerable)
  470. {
  471. if (aa != 0) s.Append("or Contains(");
  472. if (obja is string)
  473. {
  474. s.Append(key + "," + " @" + key1 + aa + " ) = " + compareOperBool + " ");
  475. }
  476. else
  477. {
  478. s.Append("ToString( " + key + " )," + " \'@" + key1 + aa + "\' ) = " + compareOperBool + " ");
  479. }
  480. aa++;
  481. }
  482. }
  483. else if (value is JsonElement jsonElement && jsonElement.ValueKind is JsonValueKind.Array)
  484. {
  485. keyListValueList = true;
  486. int aa = 0;
  487. foreach (JsonElement obja in jsonElement.EnumerateArray().ToArray())
  488. {
  489. if (aa != 0) s.Append("or Contains(");
  490. if (obja.ValueKind is JsonValueKind.String)
  491. {
  492. s.Append(key + "," + " @" + key1 + aa + " ) = " + compareOperBool + " ");
  493. }
  494. else
  495. {
  496. s.Append("ToString( " + key + " )," + " \'@" + key1 + aa + "\' ) = " + compareOperBool + " ");
  497. }
  498. aa++;
  499. }
  500. }
  501. else
  502. {
  503. Type stype = value.GetType();
  504. TypeCode typeCode = Type.GetTypeCode(stype);
  505. if (compareOperNum == 7) keyListValueList = true;
  506. string sql = "";
  507. if (value is string)
  508. {
  509. sql = logicOper + "Contains( " + key + " , @" + key1 + " ) = " + compareOperBool + " ";
  510. }
  511. else if (value is JsonElement jsonElement1 && jsonElement1.ValueKind is JsonValueKind.String)
  512. {
  513. sql = logicOper + "Contains( " + key + " , @" + key1 + " ) = " + compareOperBool + " ";
  514. }
  515. else
  516. {
  517. sql = logicOper + "Contains( ToString(" + key + " ), \'@" + key1 + "\' ) = " + compareOperBool + " ";
  518. }
  519. return sql;
  520. }
  521. s.Append(" )");
  522. return s.ToString();
  523. }
  524. private static string CompareBoolSwitch(int compareOperNum)
  525. {
  526. return compareOperNum switch
  527. {
  528. 6 => " true ",
  529. 7 => " false ",
  530. _ => " true ",
  531. };
  532. }
  533. private static string KeyNotElement(object value, string key, string key1, string logicOperParams, int logicOperNum, int compareOperNum)
  534. {
  535. string compareOperBool = " true ";
  536. compareOperBool = CompareBoolSwitch(compareOperNum);
  537. string logicOper = " and ";
  538. int compareOper = 4;
  539. if (logicOperNum != 0) logicOper = logicOperParams;
  540. if (key.EndsWith(".&"))
  541. {
  542. key = key.Replace(".&", "");
  543. }
  544. else if (key.EndsWith(".|"))
  545. {
  546. key = key.Replace(".|", "");
  547. }
  548. string[] keyHead = key.Split(".");
  549. CompareOperSwitch(keyHead[0], ref key, ref compareOper);
  550. if (compareOper == 6 || compareOper == 7)
  551. {
  552. StringBuilder sql = new StringBuilder(logicOper + " ( Contains( ");
  553. if (value is JArray jarray)
  554. {
  555. int aa = 0;
  556. foreach (JValue obja in jarray)
  557. {
  558. if (aa != 0) sql.Append("or Contains(");
  559. if (obja.Value is string)
  560. {
  561. sql.Append(" c." + key + "," + " @" + key1 + aa + " ) = " + compareOperBool + " ");
  562. }
  563. else
  564. {
  565. sql.Append("ToString( c." + key + " )," + " \'@" + key1 + aa + "\' ) = " + compareOperBool + " ");
  566. }
  567. sql.Append(" c." + key + ", @" + key1 + aa + " )= " + compareOperBool + " ");
  568. aa++;
  569. }
  570. }
  571. else if (value is IEnumerable enumerable && !(value is String))
  572. {
  573. int aa = 0;
  574. foreach (object obja in enumerable)
  575. {
  576. if (aa != 0) sql.Append("or Contains(");
  577. if (obja is string)
  578. {
  579. sql.Append(" c." + key + "," + " @" + key1 + aa + " ) = " + compareOperBool + " ");
  580. }
  581. else
  582. {
  583. sql.Append("ToString( c." + key + " )," + " \'@" + key1 + aa + "\' ) = " + compareOperBool + " ");
  584. }
  585. aa++;
  586. }
  587. }
  588. else if (value is JsonElement jsonElement && jsonElement.ValueKind is JsonValueKind.Array)
  589. {
  590. int aa = 0;
  591. foreach (JsonElement obja in jsonElement.EnumerateArray().ToArray())
  592. {
  593. if (aa != 0) sql.Append("or Contains(");
  594. if (obja.ValueKind is JsonValueKind.String)
  595. {
  596. sql.Append(" c." + key + "," + " @" + key1 + aa + " ) = " + compareOperBool + " ");
  597. }
  598. else
  599. {
  600. sql.Append("ToString( c." + key + " )," + " \'@" + key1 + aa + "\' ) = " + compareOperBool + " ");
  601. }
  602. aa++;
  603. }
  604. }
  605. else
  606. {
  607. Type s = value.GetType();
  608. TypeCode typeCode = Type.GetTypeCode(s);
  609. string sql1 = "";
  610. if (value is string)
  611. {
  612. sql1 = logicOper + "Contains( c." + key + " , @" + key1 + " ) = " + compareOperBool + " ";
  613. }
  614. if (value is JsonElement jsonElement1 && jsonElement1.ValueKind is JsonValueKind.String)
  615. {
  616. sql1 = logicOper + "Contains( c." + key + " , @" + key1 + " ) = " + compareOperBool + " ";
  617. }
  618. else
  619. {
  620. sql1 = logicOper + "Contains( ToString( c." + key + " ), \'@" + key1 + "\' ) = " + compareOperBool + " ";
  621. }
  622. return sql1;
  623. }
  624. sql.Append(")");
  625. return sql.ToString();
  626. }
  627. else
  628. {
  629. StringBuilder sql = new StringBuilder(logicOper + " c." + key + " in (");
  630. if (value is JArray array)
  631. {
  632. int aa = 0;
  633. foreach (JValue obja in array)
  634. {
  635. sql.Append(" @" + key1 + aa + " ,");
  636. aa++;
  637. }
  638. string sqls = sql.ToString().Substring(0, sql.Length - 1);
  639. sqls += " ) ";
  640. return sqls;
  641. }
  642. else if (value is IEnumerable enumerable && !(value is String))
  643. {
  644. int aa = 0;
  645. foreach (object obja in enumerable)
  646. {
  647. sql.Append(" @" + key1 + aa + " ,");
  648. aa++;
  649. }
  650. string sqls = sql.ToString().Substring(0, sql.Length - 1);
  651. sqls += " ) ";
  652. return sqls;
  653. }
  654. else if (value is JsonElement jsonElement)
  655. {
  656. if (jsonElement.ValueKind is JsonValueKind.Array)
  657. {
  658. int aa = 0;
  659. foreach (JsonElement obja in jsonElement.EnumerateArray().ToArray())
  660. {
  661. sql.Append(" @" + key1 + aa + " ,");
  662. aa++;
  663. }
  664. }
  665. else
  666. {
  667. return logicOper + " c." + key + CompareOpers[compareOperNum] + " @" + key1;
  668. }
  669. string sqls = sql.ToString().Substring(0, sql.Length - 1);
  670. sqls += " ) ";
  671. return sqls;
  672. }
  673. else
  674. {
  675. if (value != null)
  676. {
  677. Type s = value.GetType();
  678. TypeCode typeCode = Type.GetTypeCode(s);
  679. return typeCode switch
  680. {
  681. TypeCode.String => logicOper + " c." + key + CompareOpers[compareOperNum] + " @" + key1,// + "\'" + value.ToString() + "\'",
  682. TypeCode.Char => logicOper + " c." + key + CompareOpers[compareOperNum] + " @" + key1,// + "\'" + value.ToString() + "\'",
  683. TypeCode.Int32 => logicOper + " c." + key + CompareOpers[compareOperNum] + " @" + key1,// + int.Parse(value.ToString()),
  684. TypeCode.Double => logicOper + " c." + key + CompareOpers[compareOperNum] + " @" + key1,// + double.Parse(value.ToString()),
  685. TypeCode.Boolean => logicOper + " c." + key + CompareOpers[compareOperNum] + " @" + key1,// + bool.Parse(value.ToString()),
  686. TypeCode.DateTime => logicOper + " c." + key + CompareOpers[compareOperNum] + " @" + key1,// + (DateTime)value,
  687. TypeCode.Int64 => logicOper + " c." + key + CompareOpers[compareOperNum] + " @" + key1,// + long.Parse(value.ToString()),
  688. _ => null,
  689. };
  690. }
  691. else {
  692. return logicOper + " c." + key + CompareOpers[compareOperNum] + " @" + key1;
  693. }
  694. }
  695. }
  696. }
  697. public enum LogicOper : int
  698. {
  699. and = 0, or = 1
  700. }
  701. public enum CompareOper : int
  702. {
  703. moreThan = 0, lessThan = 1, notMoreThan = 2, notLessThan = 3, equal = 4, notEqual = 5, like = 6, notLike = 7, IN = 8
  704. }
  705. private static Dictionary<string, object> GetParmeter(Dictionary<string, object> dict, Dictionary<string, object> parmeters, int offset = 0, int limit = 0)
  706. {
  707. int i = 0;
  708. foreach (KeyValuePair<string, object> keyValue in dict)
  709. {
  710. string key = "";
  711. string[] keyHead = keyValue.Key.Split(".");
  712. switch (keyHead[0])
  713. {
  714. case ">":
  715. key = keyValue.Key.Replace(">.", "");
  716. break;
  717. case "<":
  718. key = keyValue.Key.Replace("<.", "");
  719. break;
  720. case "<=":
  721. key = keyValue.Key.Replace("<=.", "");
  722. break;
  723. case ">=":
  724. key = keyValue.Key.Replace(">=.", "");
  725. break;
  726. case "=":
  727. key = keyValue.Key.Replace("=.", "");
  728. break;
  729. case "!=":
  730. key = keyValue.Key.Replace("!=.", "");
  731. break;
  732. case "$":
  733. key = keyValue.Key.Replace("$.", "");
  734. break;
  735. case "!$":
  736. key = keyValue.Key.Replace("!$.", "");
  737. break;
  738. default:
  739. key = keyValue.Key;
  740. break;
  741. }
  742. if (key.EndsWith(".&"))
  743. {
  744. key = key.Replace(".&", "");
  745. }
  746. else if (key.EndsWith(".|"))
  747. {
  748. key = key.Replace(".|", "");
  749. }
  750. key = key.Replace("[*]", "");
  751. key = key.Replace(".", "");
  752. key += i;
  753. if (keyValue.Value is JArray array)
  754. {
  755. int aa = 0;
  756. foreach (JValue obja in array)
  757. {
  758. parmeters.Add("@" + key + aa, obja);
  759. aa++;
  760. }
  761. }
  762. else if (keyValue.Value is IEnumerable enumerable && !(keyValue.Value is String))
  763. {
  764. int aa = 0;
  765. foreach (object obja in enumerable)
  766. {
  767. parmeters.Add("@" + key + aa, obja);
  768. aa++;
  769. }
  770. }
  771. else if (keyValue.Value is JsonElement jsonElement)
  772. {
  773. if (jsonElement.ValueKind is JsonValueKind.Array)
  774. {
  775. int aa = 0;
  776. foreach (JsonElement obja in jsonElement.EnumerateArray().ToArray())
  777. {
  778. if (obja.ValueKind is JsonValueKind.String)
  779. {
  780. parmeters.Add("@" + key + aa, obja.ToString());
  781. }
  782. if (obja.ValueKind is JsonValueKind.Number)
  783. {
  784. parmeters.Add("@" + key + aa, double.Parse(obja.ToString()));
  785. }
  786. if (obja.ValueKind is JsonValueKind.True)
  787. {
  788. parmeters.Add("@" + key + aa, bool.Parse(obja.ToString()));
  789. }
  790. if (obja.ValueKind is JsonValueKind.False)
  791. {
  792. parmeters.Add("@" + key + aa, bool.Parse(obja.ToString()));
  793. }
  794. aa++;
  795. }
  796. }
  797. else
  798. {
  799. if (jsonElement.ValueKind is JsonValueKind.String)
  800. {
  801. parmeters.Add("@" + key, keyValue.Value.ToString());
  802. }
  803. else if (jsonElement.ValueKind is JsonValueKind.Number)
  804. {
  805. parmeters.Add("@" + key, double.Parse(keyValue.Value.ToString()));
  806. }
  807. else if (jsonElement.ValueKind is JsonValueKind.True)
  808. {
  809. parmeters.Add("@" + key, bool.Parse(keyValue.Value.ToString()));
  810. }
  811. else if (jsonElement.ValueKind is JsonValueKind.False)
  812. {
  813. parmeters.Add("@" + key, bool.Parse(keyValue.Value.ToString()));
  814. }
  815. else
  816. {
  817. parmeters.Add("@" + key, keyValue.Value.ToString());
  818. }
  819. }
  820. }
  821. else
  822. {
  823. parmeters.Add("@" + key, keyValue.Value);
  824. }
  825. i++;
  826. }
  827. if (offset != -1 && limit != 0)
  828. {
  829. parmeters.Add("@offsetNum", offset);
  830. parmeters.Add("@limitNum", limit);
  831. }
  832. return parmeters;
  833. }
  834. }
  835. }