SQLHelper.cs 35 KB

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