SQLHelperParametric.cs 33 KB

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