SQLHelperParametric.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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. namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
  11. {
  12. public class SQLHelperParametric
  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(".group.", "['group'].");
  21. sql.Replace(".group ", "['group'] ");
  22. sql.Replace(".end.", "['end'].");
  23. sql.Replace(".end ", "['end'] ");
  24. sql.Replace(".having.", "['having'].");
  25. sql.Replace(".having ", "['having'] ");
  26. }
  27. public static CosmosDbQuery GetSQL(Dictionary<string, object> dict, StringBuilder sql)
  28. {
  29. if (dict != null)
  30. {
  31. Dictionary<string, object> parmeters = new Dictionary<string, object>();
  32. int offsetNum = 0;
  33. int limitNum = 0;
  34. bool pageBool = false;
  35. GetPageNum(dict, ref offsetNum, ref limitNum, ref pageBool);
  36. string OrderByValue = "";
  37. // 处理OrderBy
  38. if (dict.TryGetValue("@ASC", out object ASC))
  39. {
  40. OrderByValue = Regex.Replace(ASC.ToString(), "\\s{1,}", "");
  41. dict.Remove("@ASC");
  42. }
  43. if (dict.TryGetValue("@DESC", out object DESC))
  44. {
  45. OrderByValue = Regex.Replace(DESC.ToString(), "\\s{1,}", "");
  46. dict.Remove("@DESC");
  47. };
  48. //处理顺序
  49. Stack<KeyValuePair<string, object>> stack = new Stack<KeyValuePair<string, object>>();
  50. foreach (string item in dict.Keys)
  51. {
  52. if (item.EndsWith(".|"))
  53. {
  54. stack.Push(new KeyValuePair<string, object>(item, dict[item]));
  55. }
  56. }
  57. foreach (string item in dict.Keys)
  58. {
  59. if (!item.EndsWith(".|"))
  60. {
  61. stack.Push(new KeyValuePair<string, object>(item, dict[item]));
  62. }
  63. }
  64. string Join = " join ";
  65. string instring = " in ";
  66. Dictionary<string, string> keyValues = new Dictionary<string, string>();
  67. StringBuilder WhereString = new StringBuilder();
  68. int heada = 0;
  69. string[] sqlHead = new string[] { "A", "B", "C", "D", "E", "F" };
  70. int kslength = 0;
  71. int logicOperNum = 0;
  72. bool keyListValueList = true;
  73. int keyListValueListNum = 0;
  74. int stackCount = stack.Count;
  75. for (int k = 0; k < stackCount; k++)
  76. {
  77. KeyValuePair<string, object> item = stack.Pop();
  78. bool isLikeSQL = false;
  79. if (item.Key.StartsWith("$.") || item.Key.StartsWith("!$."))
  80. {
  81. isLikeSQL = true;
  82. }
  83. string key = item.Key;
  84. string[] keyHead = key.Split(".");
  85. int index = 0;
  86. int compareOper = 4;
  87. int logicOper = 0;
  88. if (key.EndsWith(".&"))
  89. {
  90. logicOper = (int)LogicOper.and;
  91. key = key.Replace(".&", "");
  92. }
  93. else if (key.EndsWith(".|"))
  94. {
  95. logicOper = (int)LogicOper.or;
  96. key = key.Replace(".|", "");
  97. }
  98. CompareOperSwitch(keyHead[0], ref key, ref compareOper);
  99. string[] keyBody = key.Split("[*]");
  100. if (keyBody.Length > 1)
  101. {
  102. key = key.Replace("[*]", "");
  103. key = key.Replace(".", "");
  104. kslength += keyBody.Length;
  105. if (kslength < (7 + heada))
  106. {
  107. StringBuilder sqlitem = new StringBuilder();
  108. for (int i = 0; i < keyBody.Length - 1; i++)
  109. {
  110. //Console.WriteLine(ks[i]);
  111. if (i == 0)
  112. {
  113. sqlitem.Append(Join);
  114. string a = sqlHead[heada] + index;
  115. sqlitem.Append(a + " ");
  116. //keyValues.Add(ks[i], a);
  117. keyValues[keyBody[i]] = a;
  118. sqlitem.Append(instring);
  119. sqlitem.Append("c.");
  120. sqlitem.Append(keyBody[i]);
  121. }
  122. else
  123. {
  124. sqlitem.Append(Join);
  125. string a = sqlHead[heada] + index;
  126. sqlitem.Append(a + " ");
  127. //keyValues.Add(ks[i], a);
  128. keyValues[keyBody[i]] = a;
  129. sqlitem.Append(instring);
  130. sqlitem.Append(keyValues[keyBody[i - 1]]);
  131. sqlitem.Append(keyBody[i]);
  132. }
  133. index += 1;
  134. }
  135. sql.Append(sqlitem);
  136. string s = "";
  137. if (isLikeSQL)
  138. {
  139. s = ValueIsLike(sqlHead[heada] + (keyBody.Length - 2) + keyBody[index] + "", key, item.Value, LogicOpers[logicOper], logicOperNum, compareOper, ref keyListValueList);
  140. }
  141. else
  142. {
  143. s = ValueNotLike(sqlHead[heada] + (keyBody.Length - 2) + keyBody[index] + "", key, item.Value, LogicOpers[logicOper], logicOperNum, compareOper, ref keyListValueList);
  144. }
  145. WhereString.Append(s);
  146. if (keyListValueList && keyListValueListNum == 0)
  147. {
  148. sql = sql.Replace("select ", "select distinct ");
  149. keyListValueList = false;
  150. keyListValueListNum++;
  151. }
  152. }
  153. else
  154. {
  155. throw new BizException("数组总共深度不能超过5层", ResponseCode.PARAMS_ERROR);
  156. }
  157. heada += 1;
  158. }
  159. else
  160. {
  161. string itemKey = key.Replace(".", "");
  162. WhereString.Append(KeyNotElement(dict[item.Key], item.Key, itemKey, LogicOpers[logicOper], logicOperNum, compareOper));
  163. }
  164. logicOperNum += 1;
  165. }
  166. sql.Append(" where 1=1 ").Append(WhereString);
  167. if (ASC != null)
  168. {
  169. sql.Append(" Order By c." + OrderByValue );
  170. //sql.Append(" Order By c." + "@OrderByValue");
  171. }
  172. else if (DESC != null) {
  173. sql.Append(" Order By c." + OrderByValue );
  174. // sql.Append(" Order By c." + "@OrderByValue"+ " DESC " );
  175. }
  176. if (pageBool && offsetNum != 0 && limitNum != 0)
  177. {
  178. //sql.Append(" OFFSET " + offsetNum + " LIMIT " + limitNum);
  179. sql.Append(" OFFSET " + " @offsetNum " + " LIMIT " + " @limitNum ");
  180. }
  181. ReplaceKeyWords(ref sql);
  182. parmeters = GetParmeter(dict, parmeters, offsetNum, limitNum);
  183. CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
  184. {
  185. QueryText = sql.ToString(),
  186. Parameters = parmeters
  187. };
  188. return cosmosDbQuery;
  189. }
  190. return null;
  191. }
  192. private static void GetPageNum(Dictionary<string, object> dict, ref int offsetNum, ref int limitNum, ref bool pageBool)
  193. {
  194. dict.TryGetValue("@OFFSET", out object offset);
  195. dict.Remove("@OFFSET");
  196. dict.TryGetValue("@LIMIT", out object limit);
  197. dict.Remove("@LIMIT");
  198. if (offset != null && limit != null)
  199. {
  200. pageBool = true;
  201. offsetNum = int.Parse(offset.ToString());
  202. limitNum = int.Parse(limit.ToString());
  203. }
  204. }
  205. private static void CompareOperSwitch(string keyHead, ref string key, ref int compareOper)
  206. {
  207. switch (keyHead)
  208. {
  209. case ">":
  210. compareOper = (int)CompareOper.moreThan;
  211. key = key.Replace(">.", "");
  212. break;
  213. case "<":
  214. compareOper = (int)CompareOper.lessThan;
  215. key = key.Replace("<.", "");
  216. break;
  217. case "<=":
  218. compareOper = (int)CompareOper.notMoreThan;
  219. key = key.Replace("<=.", "");
  220. break;
  221. case ">=":
  222. compareOper = (int)CompareOper.notLessThan;
  223. key = key.Replace(">=.", "");
  224. break;
  225. case "=":
  226. compareOper = (int)CompareOper.equal;
  227. key = key.Replace("=.", "");
  228. break;
  229. case "!=":
  230. compareOper = (int)CompareOper.notEqual;
  231. key = key.Replace("!=.", "");
  232. break;
  233. case "$":
  234. compareOper = (int)CompareOper.like;
  235. key = key.Replace("$.", "");
  236. break;
  237. case "!$":
  238. compareOper = (int)CompareOper.notLike;
  239. key = key.Replace("!$.", "");
  240. break;
  241. default:
  242. compareOper = 4;
  243. break;
  244. }
  245. }
  246. private static string ValueNotLike(string key, string key1, object value, string logicOperParams, int logicOperNum, int compareOperNum, ref bool keyListValueList)
  247. {
  248. string logicOper = " and ";
  249. string compareOper = " = ";
  250. if (compareOperNum != 4) compareOper = CompareOpers[compareOperNum];
  251. if (logicOperNum != 0) logicOper = logicOperParams;
  252. StringBuilder sql = new StringBuilder(logicOper + key + " in (");
  253. if (value is JArray array)
  254. {
  255. int aa = 0;
  256. foreach (JValue obja in array)
  257. {
  258. sql.Append(" @" + key1 + aa + " ,");
  259. aa++;
  260. }
  261. string sqls = sql.ToString().Substring(0, sql.Length - 1);
  262. sqls += " ) ";
  263. return sqls;
  264. }
  265. else if (value is IEnumerable enumerable && !(value is String))
  266. {
  267. int aa = 0;
  268. foreach (object obja in enumerable)
  269. {
  270. sql.Append(" @" + key1 + aa + " ,");
  271. aa++;
  272. }
  273. string sqls = sql.ToString().Substring(0, sql.Length - 1);
  274. sqls += ") ";
  275. return sqls;
  276. }
  277. else if (value is JsonElement jsonElement)
  278. {
  279. if (jsonElement.ValueKind is JsonValueKind.Array)
  280. {
  281. int aa = 0;
  282. foreach (JsonElement obja in jsonElement.EnumerateArray().ToArray())
  283. {
  284. sql.Append(" @" + key1 + aa + " ,");
  285. aa++;
  286. }
  287. }
  288. else
  289. {
  290. if (compareOperNum == 4) keyListValueList = false;
  291. return logicOper + key + compareOper + " @" + key1 + " ";
  292. }
  293. string sqls = sql.ToString().Substring(0, sql.Length - 1);
  294. sqls += " ) ";
  295. return sqls;
  296. }
  297. else
  298. {
  299. Type s = value.GetType();
  300. TypeCode typeCode = Type.GetTypeCode(s);
  301. if (compareOperNum == 4) keyListValueList = false;
  302. return logicOper + key + compareOper + " @" + key1 + " ";
  303. }
  304. }
  305. private static string ValueIsLike(string key, string key1, object value, string logicOperParams, int logicOperNum, int compareOperNum, ref bool keyListValueList)
  306. {
  307. string compareOperBool = " true ";
  308. compareOperBool = CompareBoolSwitch(compareOperNum);
  309. string logicOper = " and ";
  310. if (logicOperNum != 0) logicOper = logicOperParams;
  311. StringBuilder s = new StringBuilder(logicOper + " ( Contains( ");
  312. if (value is JArray array)
  313. {
  314. int aa = 0;
  315. foreach (JValue obja in array)
  316. {
  317. if (aa != 0) s.Append("or Contains(");
  318. s.Append(key + "," + " @" + key1 + aa + " ) = " + compareOperBool + " ");
  319. if (obja.Value is string)
  320. {
  321. s.Append(key + "," + " @" + key1 + aa + " ) = " + compareOperBool + " ");
  322. }
  323. else
  324. {
  325. s.Append("ToString( " + key + " )," + " \'@" + key1 + aa + "\' ) = " + compareOperBool + " ");
  326. }
  327. aa++;
  328. }
  329. }
  330. else if (value is IEnumerable enumerable && !(value is String))
  331. {
  332. int aa = 0;
  333. foreach (object obja in enumerable)
  334. {
  335. if (aa != 0) s.Append("or Contains(");
  336. if (obja is string)
  337. {
  338. s.Append(key + "," + " @" + key1 + aa + " ) = " + compareOperBool + " ");
  339. }
  340. else
  341. {
  342. s.Append("ToString( " + key + " )," + " \'@" + key1 + aa + "\' ) = " + compareOperBool + " ");
  343. }
  344. aa++;
  345. }
  346. }
  347. else if (value is JsonElement jsonElement && jsonElement.ValueKind is JsonValueKind.Array)
  348. {
  349. int aa = 0;
  350. foreach (JsonElement obja in jsonElement.EnumerateArray().ToArray())
  351. {
  352. if (aa != 0) s.Append("or Contains(");
  353. if (obja.ValueKind is JsonValueKind.String)
  354. {
  355. s.Append(key + "," + " @" + key1 + aa + " ) = " + compareOperBool + " ");
  356. }
  357. else
  358. {
  359. s.Append("ToString( " + key + " )," + " \'@" + key1 + aa + "\' ) = " + compareOperBool + " ");
  360. }
  361. aa++;
  362. }
  363. }
  364. else
  365. {
  366. Type stype = value.GetType();
  367. TypeCode typeCode = Type.GetTypeCode(stype);
  368. keyListValueList = false;
  369. string sql = "";
  370. if (value is string)
  371. {
  372. sql = logicOper + "Contains( " + key + " , @" + key1 + " ) = " + compareOperBool + " ";
  373. }
  374. else if (value is JsonElement jsonElement1 && jsonElement1.ValueKind is JsonValueKind.String)
  375. {
  376. sql = logicOper + "Contains( " + key + " , @" + key1 + " ) = " + compareOperBool + " ";
  377. }
  378. else
  379. {
  380. sql = logicOper + "Contains( ToString(" + key + " ), \'@" + key1 + "\' ) = " + compareOperBool + " ";
  381. }
  382. return sql;
  383. }
  384. s.Append(" )");
  385. return s.ToString();
  386. }
  387. private static string CompareBoolSwitch(int compareOperNum)
  388. {
  389. return compareOperNum switch
  390. {
  391. 6 => " true ",
  392. 7 => " false ",
  393. _ => " true ",
  394. };
  395. }
  396. private static string KeyNotElement(object value, string key, string key1, string logicOperParams, int logicOperNum, int compareOperNum)
  397. {
  398. string compareOperBool = " true ";
  399. compareOperBool = CompareBoolSwitch(compareOperNum);
  400. string logicOper = " and ";
  401. int compareOper = 4;
  402. if (logicOperNum != 0) logicOper = logicOperParams;
  403. if (key.EndsWith(".&"))
  404. {
  405. key = key.Replace(".&", "");
  406. }
  407. else if (key.EndsWith(".|"))
  408. {
  409. key = key.Replace(".|", "");
  410. }
  411. string[] keyHead = key.Split(".");
  412. CompareOperSwitch(keyHead[0], ref key, ref compareOper);
  413. if (compareOper == 6 || compareOper == 7)
  414. {
  415. StringBuilder sql = new StringBuilder(logicOper + " ( Contains( ");
  416. if (value is JArray jarray)
  417. {
  418. int aa = 0;
  419. foreach (JValue obja in jarray)
  420. {
  421. if (aa != 0) sql.Append("or Contains(");
  422. if (obja.Value is string)
  423. {
  424. sql.Append(" c." + key + "," + " @" + key1 + aa + " ) = " + compareOperBool + " ");
  425. }
  426. else
  427. {
  428. sql.Append("ToString( c." + key + " )," + " \'@" + key1 + aa + "\' ) = " + compareOperBool + " ");
  429. }
  430. sql.Append(" c." + key + ", @" + key1 + aa + " )= " + compareOperBool + " ");
  431. aa++;
  432. }
  433. }
  434. else if (value is IEnumerable enumerable && !(value is String))
  435. {
  436. int aa = 0;
  437. foreach (object obja in enumerable)
  438. {
  439. if (aa != 0) sql.Append("or Contains(");
  440. if (obja is string)
  441. {
  442. sql.Append(" c." + key + "," + " @" + key1 + aa + " ) = " + compareOperBool + " ");
  443. }
  444. else
  445. {
  446. sql.Append("ToString( c." + key + " )," + " \'@" + key1 + aa + "\' ) = " + compareOperBool + " ");
  447. }
  448. aa++;
  449. }
  450. }
  451. else if (value is JsonElement jsonElement && jsonElement.ValueKind! is JsonValueKind.Array)
  452. {
  453. int aa = 0;
  454. foreach (JsonElement obja in jsonElement.EnumerateArray().ToArray())
  455. {
  456. if (aa != 0) sql.Append("or Contains(");
  457. if (obja.ValueKind is JsonValueKind.String)
  458. {
  459. sql.Append(" c." + key + "," + " @" + key1 + aa + " ) = " + compareOperBool + " ");
  460. }
  461. else
  462. {
  463. sql.Append("ToString( c." + key + " )," + " \'@" + key1 + aa + "\' ) = " + compareOperBool + " ");
  464. }
  465. aa++;
  466. }
  467. }
  468. else
  469. {
  470. Type s = value.GetType();
  471. TypeCode typeCode = Type.GetTypeCode(s);
  472. string sql1 = "";
  473. if (value is string)
  474. {
  475. sql1 = logicOper + "Contains( c." + key + " , @" + key1 + " ) = " + compareOperBool + " ";
  476. }
  477. else
  478. {
  479. sql1 = logicOper + "Contains( ToString( c." + key + " ), \'@" + key1 + "\' ) = " + compareOperBool + " ";
  480. }
  481. return sql1;
  482. }
  483. sql.Append(")");
  484. return sql.ToString();
  485. }
  486. else
  487. {
  488. StringBuilder sql = new StringBuilder(logicOper + " c." + key + " in (");
  489. if (value is JArray array)
  490. {
  491. int aa = 0;
  492. foreach (JValue obja in array)
  493. {
  494. sql.Append(" @" + key1 + aa + " ,");
  495. aa++;
  496. }
  497. string sqls = sql.ToString().Substring(0, sql.Length - 1);
  498. sqls += " ) ";
  499. return sqls;
  500. }
  501. else if (value is IEnumerable enumerable && !(value is String))
  502. {
  503. int aa = 0;
  504. foreach (object obja in enumerable)
  505. {
  506. sql.Append(" @" + key1 + aa + " ,");
  507. aa++;
  508. }
  509. string sqls = sql.ToString().Substring(0, sql.Length - 1);
  510. sqls += " ) ";
  511. return sqls;
  512. }
  513. else if (value is JsonElement jsonElement)
  514. {
  515. if (jsonElement.ValueKind is JsonValueKind.Array)
  516. {
  517. int aa = 0;
  518. foreach (JsonElement obja in jsonElement.EnumerateArray().ToArray())
  519. {
  520. sql.Append(" @" + key1 + aa + " ,");
  521. aa++;
  522. }
  523. }
  524. else
  525. {
  526. return logicOper + " c." + key + CompareOpers[compareOperNum] + " @" + key1;
  527. }
  528. string sqls = sql.ToString().Substring(0, sql.Length - 1);
  529. sqls += " ) ";
  530. return sqls;
  531. }
  532. else
  533. {
  534. Type s = value.GetType();
  535. TypeCode typeCode = Type.GetTypeCode(s);
  536. return typeCode switch
  537. {
  538. TypeCode.String => logicOper + " c." + key + CompareOpers[compareOperNum] + " @" + key1,// + "\'" + value.ToString() + "\'",
  539. TypeCode.Char => logicOper + " c." + key + CompareOpers[compareOperNum] + " @" + key1,// + "\'" + value.ToString() + "\'",
  540. TypeCode.Int32 => logicOper + " c." + key + CompareOpers[compareOperNum] + " @" + key1,// + int.Parse(value.ToString()),
  541. TypeCode.Double => logicOper + " c." + key + CompareOpers[compareOperNum] + " @" + key1,// + double.Parse(value.ToString()),
  542. TypeCode.Boolean => logicOper + " c." + key + CompareOpers[compareOperNum] + " @" + key1,// + bool.Parse(value.ToString()),
  543. TypeCode.DateTime => logicOper + " c." + key + CompareOpers[compareOperNum] + " @" + key1,// + (DateTime)value,
  544. TypeCode.Int64 => logicOper + " c." + key + CompareOpers[compareOperNum] + " @" + key1,// + long.Parse(value.ToString()),
  545. _ => null,
  546. };
  547. }
  548. }
  549. }
  550. public enum LogicOper : int
  551. {
  552. and = 0, or = 1
  553. }
  554. public enum CompareOper : int
  555. {
  556. moreThan = 0, lessThan = 1, notMoreThan = 2, notLessThan = 3, equal = 4, notEqual = 5, like = 6, notLike = 7, IN = 8
  557. }
  558. private static Dictionary<string, object> GetParmeter(Dictionary<string, object> dict, Dictionary<string, object> parmeters,int offset = 0,int limit = 0)
  559. {
  560. foreach (KeyValuePair<string, object> keyValue in dict)
  561. {
  562. string key = "";
  563. string[] keyHead = keyValue.Key.Split(".");
  564. switch (keyHead[0])
  565. {
  566. case ">":
  567. key = keyValue.Key.Replace(">.", "");
  568. break;
  569. case "<":
  570. key = keyValue.Key.Replace("<.", "");
  571. break;
  572. case "<=":
  573. key = keyValue.Key.Replace("<=.", "");
  574. break;
  575. case ">=":
  576. key = keyValue.Key.Replace(">=.", "");
  577. break;
  578. case "=":
  579. key = keyValue.Key.Replace("=.", "");
  580. break;
  581. case "!=":
  582. key = keyValue.Key.Replace("!=.", "");
  583. break;
  584. case "$":
  585. key = keyValue.Key.Replace("$.", "");
  586. break;
  587. case "!$":
  588. key = keyValue.Key.Replace("!$.", "");
  589. break;
  590. default:
  591. key = keyValue.Key;
  592. break;
  593. }
  594. if (key.EndsWith(".&"))
  595. {
  596. key = key.Replace(".&", "");
  597. }
  598. else if (key.EndsWith(".|"))
  599. {
  600. key = key.Replace(".|", "");
  601. }
  602. key = key.Replace("[*]", "");
  603. key = key.Replace(".", "");
  604. if (keyValue.Value is JArray array)
  605. {
  606. int aa = 0;
  607. foreach (JValue obja in array)
  608. {
  609. parmeters.Add("@" + key + aa, obja);
  610. aa++;
  611. }
  612. }
  613. else if (keyValue.Value is IEnumerable enumerable && !(keyValue.Value is String))
  614. {
  615. int aa = 0;
  616. foreach (object obja in enumerable)
  617. {
  618. parmeters.Add("@" + key + aa, obja);
  619. aa++;
  620. }
  621. }
  622. else if (keyValue.Value is JsonElement jsonElement)
  623. {
  624. if (jsonElement.ValueKind is JsonValueKind.Array)
  625. {
  626. int aa = 0;
  627. foreach (JsonElement obja in jsonElement.EnumerateArray().ToArray())
  628. {
  629. if (obja.ValueKind is JsonValueKind.String)
  630. {
  631. parmeters.Add("@" + key + aa, obja.ToString());
  632. }
  633. if (obja.ValueKind is JsonValueKind.Number)
  634. {
  635. parmeters.Add("@" + key + aa, double.Parse(obja.ToString()));
  636. }
  637. if (obja.ValueKind is JsonValueKind.True)
  638. {
  639. parmeters.Add("@" + key + aa, bool.Parse(obja.ToString()));
  640. }
  641. if (obja.ValueKind is JsonValueKind.False)
  642. {
  643. parmeters.Add("@" + key + aa, bool.Parse(obja.ToString()));
  644. }
  645. aa++;
  646. }
  647. }
  648. else
  649. {
  650. if (jsonElement.ValueKind is JsonValueKind.String)
  651. {
  652. parmeters.Add("@" + key, keyValue.Value.ToString());
  653. }
  654. else if (jsonElement.ValueKind is JsonValueKind.Number)
  655. {
  656. parmeters.Add("@" + key, double.Parse(keyValue.Value.ToString()));
  657. }
  658. else if (jsonElement.ValueKind is JsonValueKind.True)
  659. {
  660. parmeters.Add("@" + key, bool.Parse(keyValue.Value.ToString()));
  661. }
  662. else if (jsonElement.ValueKind is JsonValueKind.False)
  663. {
  664. parmeters.Add("@" + key, bool.Parse(keyValue.Value.ToString()));
  665. }
  666. else
  667. {
  668. parmeters.Add("@" + key, keyValue.Value.ToString());
  669. }
  670. }
  671. }
  672. else
  673. {
  674. parmeters.Add("@" + key, keyValue.Value);
  675. }
  676. }
  677. if (offset != 0 && limit != 0)
  678. {
  679. parmeters.Add("@offsetNum", offset);
  680. parmeters.Add("@limitNum", limit);
  681. }
  682. return parmeters;
  683. }
  684. }
  685. }