LiteDBOperator.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. using TEAMModelOS.SDK.Module.LiteDB.Configuration;
  2. using TEAMModelOS.SDK.Module.LiteDB.Interfaces;
  3. using LiteDB;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using TEAMModelOS.SDK.Helper.Common.JsonHelper;
  8. using TEAMModelOS.SDK.Context.Exception;
  9. namespace TEAMModelOS.SDK.Module.LiteDB.Implements
  10. {
  11. public class LiteDBOperator : ILiteDBOperator
  12. {
  13. private readonly LiteDatabase database;
  14. public LiteDBOperator(LiteDBOptions options)
  15. {
  16. database = LiteDatabaseSingleton.GetInstance(options.ConnectionString).GetDatabase();
  17. }
  18. /// <summary>
  19. /// 保存单个对象
  20. /// </summary>
  21. /// <typeparam name="T"></typeparam>
  22. /// <param name="obj"></param>
  23. /// <returns></returns>
  24. public string Save<T>(T obj)
  25. where T : new()
  26. {
  27. //var db = LiteDBSingleton.getInstance().GetLiteDatabase();
  28. // Open data file (or create if not exits)
  29. Type t = typeof(T);
  30. // Get a collection (or create, if not exits)
  31. var col = database.GetCollection<T>(t.Name.ToString());
  32. // Insert new customer document
  33. var value = col.Insert(obj);
  34. return value.ToString();
  35. }
  36. /// <summary>
  37. /// 获取全部数据
  38. /// </summary>
  39. /// <typeparam name="T"></typeparam>
  40. /// <returns></returns>
  41. public IList<T> FindAll<T>()
  42. where T : new()
  43. {
  44. // Open data file (or create if not exits)
  45. // var db = LiteDBSingleton.getInstance().GetLiteDatabase();
  46. Type t = typeof(T);
  47. // Get a collection (or create, if not exits)
  48. var col = database.GetCollection<T>(t.Name.ToString());
  49. var docs = col.FindAll();
  50. return docs.ToList();
  51. }
  52. /// <summary>
  53. /// 保存单个对象
  54. /// </summary>
  55. /// <typeparam name="T"></typeparam>
  56. /// <param name="obj"></param>
  57. /// <returns></returns>
  58. public int SaveAll<T>(List<T> obj)
  59. where T : new()
  60. {
  61. // Open data file (or create if not exits)
  62. // var db = LiteDBSingleton.getInstance().GetLiteDatabase();
  63. Type t = typeof(T);
  64. // Get a collection (or create, if not exits)
  65. var col = database.GetCollection<T>(t.Name.ToString());
  66. // Insert new customer document
  67. var value = col.Insert(obj);
  68. return value;
  69. }
  70. /// <summary>
  71. /// 更新单个对象
  72. /// </summary>
  73. /// <typeparam name="T"></typeparam>
  74. /// <param name="obj"></param>
  75. /// <returns></returns>
  76. public bool Update<T>(T obj)
  77. where T : new()
  78. {
  79. // Open data file (or create if not exits)
  80. // var db = LiteDBSingleton.getInstance().GetLiteDatabase();
  81. Type t = typeof(T);
  82. // Get a collection (or create, if not exits)
  83. var col = database.GetCollection<T>(t.Name.ToString());
  84. // Update a document inside a collection
  85. try
  86. {
  87. var success = col.Update(obj);
  88. return success;
  89. }
  90. catch (Exception e) {
  91. var s = e.Message;
  92. }
  93. return false;
  94. }
  95. /// <summary>
  96. /// 更新多个对象
  97. /// </summary>
  98. /// <typeparam name="T"></typeparam>
  99. /// <param name="obj"></param>
  100. /// <returns></returns>
  101. public int UpdateAll<T>(List<T> obj)
  102. where T : new()
  103. {
  104. // Open data file (or create if not exits)
  105. // var db = LiteDBSingleton.getInstance().GetLiteDatabase();
  106. Type t = typeof(T);
  107. // Get a collection (or create, if not exits)
  108. var col = database.GetCollection<T>(t.Name.ToString());
  109. // Update a document inside a collection
  110. try
  111. {
  112. var success = col.Update(obj);
  113. return success;
  114. }
  115. catch (Exception e) {
  116. var s = e.Message;
  117. }
  118. return 0;
  119. }
  120. /// <summary>
  121. /// 删除对象
  122. /// </summary>
  123. /// <typeparam name="T"></typeparam>
  124. /// <param name="docId"></param>
  125. /// <returns></returns>
  126. public bool Delete<T>(dynamic docId)
  127. {
  128. // Open data file (or create if not exits)
  129. // var db = LiteDBSingleton.getInstance().GetLiteDatabase();
  130. Type t = typeof(T);
  131. // Get a collection (or create, if not exits)
  132. var col = database.GetCollection(t.Name.ToString());
  133. var success = col.Delete(docId);
  134. return success;
  135. }
  136. /// <summary>
  137. /// 根据ID获取
  138. /// </summary>
  139. /// <typeparam name="T"></typeparam>
  140. /// <param name="docId"></param>
  141. /// <returns></returns>
  142. public T FindById<T>(dynamic docId)
  143. where T : new()
  144. {
  145. // Open data file (or create if not exits)
  146. // var db = LiteDBSingleton.getInstance().GetLiteDatabase();
  147. Type t = typeof(T);
  148. // Get a collection (or create, if not exits)
  149. var col = database.GetCollection(t.Name.ToString());
  150. var doc = col.FindById(docId);
  151. // String inf = doc.AsDocument.ToString();
  152. return MessagePackHelper.JsonToObject<T>(doc.ToString());
  153. }
  154. /// <summary>
  155. /// 根据key-value方式获取 匹配查询
  156. /// </summary>
  157. /// <typeparam name="T"></typeparam>
  158. /// <param name="dict"></param>
  159. /// <returns></returns>
  160. public T FindOneByDict<T>(Dictionary<String, Object> dict)
  161. where T : new()
  162. {
  163. // Open data file (or create if not exits)
  164. // var db = LiteDBSingleton.getInstance().GetLiteDatabase();
  165. Type t = typeof(T);
  166. // Get a collection (or create, if not exits)
  167. var col = database.GetCollection(t.Name.ToString());
  168. // var doc = col.FindById(docId);
  169. // String inf = doc.AsDocument.ToString();
  170. IList<Query> queries = new List<Query>();
  171. if (dict != null && dict.Count > 0)
  172. {
  173. var keys = dict.Keys;
  174. foreach (String key in dict.Keys)
  175. {
  176. if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
  177. {
  178. queries.Add(Query.EQ(key, dict[key].ToString()));
  179. }
  180. }
  181. }
  182. var doc = new List<BsonDocument>();
  183. if (queries != null && queries.Count > 1)
  184. {
  185. doc = col.Find(Query.And(queries.ToArray())).ToList();
  186. }
  187. else if (queries != null && queries.Count() == 1)
  188. {
  189. doc = col.Find(queries.ToArray()[0]).ToList();
  190. }
  191. else
  192. {
  193. throw new BizException("请输入查询条件!");
  194. }
  195. if (doc != null && doc.Count > 0)
  196. {
  197. return MessagePackHelper.JsonToObject<T>(doc[0].ToString());
  198. }
  199. else
  200. {
  201. return new T();
  202. }
  203. }
  204. /// <summary>
  205. /// 根据key-value方式获取 匹配查询
  206. /// </summary>
  207. /// <typeparam name="T"></typeparam>
  208. /// <param name="key"></param>
  209. /// <param name="value"></param>
  210. /// <returns></returns>
  211. public T FindOneByKey<T>(String key, String value)
  212. where T : new()
  213. {
  214. // Open data file (or create if not exits)
  215. // var db = LiteDBSingleton.getInstance().GetLiteDatabase();
  216. Type t = typeof(T);
  217. // Get a collection (or create, if not exits)
  218. var col = database.GetCollection(t.Name.ToString());
  219. // var doc = col.FindById(docId);
  220. // String inf = doc.AsDocument.ToString();
  221. var doc = col.Find(Query.EQ(key, value)).ToList();
  222. if (doc != null && doc.Count > 0)
  223. {
  224. return MessagePackHelper.JsonToObject<T>(doc[0].ToString());
  225. }
  226. else
  227. {
  228. return new T();
  229. }
  230. }
  231. /// <summary>
  232. /// 根据key-value方式获取 匹配查询
  233. /// </summary>
  234. /// <typeparam name="T"></typeparam>
  235. /// <param name="dict"></param>
  236. /// <returns></returns>
  237. public IList<T> FindListByDict<T>(Dictionary<String, Object> dict)
  238. where T : new()
  239. {
  240. // Open data file (or create if not exits)
  241. // var db = LiteDBSingleton.getInstance().GetLiteDatabase();
  242. Type t = typeof(T);
  243. // Get a collection (or create, if not exits)
  244. var col = database.GetCollection(t.Name.ToString());
  245. // var doc = col.FindById(docId);
  246. // String inf = doc.AsDocument.ToString();
  247. IList<Query> queries = new List<Query>();
  248. if (dict != null && dict.Count > 0)
  249. {
  250. var keys = dict.Keys;
  251. foreach (String key in dict.Keys)
  252. {
  253. if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
  254. {
  255. queries.Add(Query.EQ(key, dict[key].ToString()));
  256. }
  257. }
  258. }
  259. var doc = new List<BsonDocument>();
  260. if (queries != null && queries.Count > 1)
  261. {
  262. doc = col.Find(Query.And(queries.ToArray())).ToList();
  263. }
  264. else if (queries != null && queries.Count() == 1)
  265. {
  266. doc = col.Find(queries.ToArray()[0]).ToList();
  267. }
  268. else
  269. {
  270. // throw new BizException("请输入查询条件!");
  271. doc = col.FindAll().ToList();
  272. }
  273. return MessagePackHelper.JsonToObject<List<T>>(doc.ToString());
  274. }
  275. /// <summary>
  276. /// 根据key-value方式获取列表 匹配查询
  277. /// </summary>
  278. /// <typeparam name="T"></typeparam>
  279. /// <param name="key"></param>
  280. /// <param name="value"></param>
  281. /// <returns></returns>
  282. public IList<T> FindListByKey<T>(String key, String value)
  283. where T : new()
  284. {
  285. // Open data file (or create if not exits)
  286. // var db = LiteDBSingleton.getInstance().GetLiteDatabase();
  287. Type t = typeof(T);
  288. // Get a collection (or create, if not exits)
  289. var col = database.GetCollection(t.Name.ToString());
  290. var doc = col.Find(Query.EQ(key, value)).ToList();
  291. return MessagePackHelper.JsonToObject<List<T>>(doc.ToString());
  292. }
  293. /// <summary>
  294. /// 根据key-value方式获取 匹配查询
  295. /// </summary>
  296. /// <typeparam name="T"></typeparam>
  297. /// <param name="dict"></param>
  298. /// <returns></returns>
  299. public IList<T> FindListByDictAndLike<T>(Dictionary<String, Object> dict, Dictionary<String, Object> likeDict)
  300. where T : new()
  301. {
  302. // Open data file (or create if not exits)
  303. // var db = LiteDBSingleton.getInstance().GetLiteDatabase();
  304. Type t = typeof(T);
  305. // Get a collection (or create, if not exits)
  306. var col = database.GetCollection(t.Name.ToString());
  307. // var doc = col.FindById(docId);
  308. // String inf = doc.AsDocument.ToString();
  309. IList<Query> queries = new List<Query>();
  310. if (null != dict && dict.Count > 0)
  311. {
  312. var keys = dict.Keys;
  313. foreach (String key in keys)
  314. {
  315. if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
  316. {
  317. queries.Add(Query.EQ(key, dict[key].ToString()));
  318. }
  319. }
  320. }
  321. if (null != likeDict && likeDict.Count > 0)
  322. {
  323. var keys = likeDict.Keys;
  324. foreach (String key in keys)
  325. {
  326. if (likeDict[key] != null && !string.IsNullOrEmpty(likeDict[key].ToString()))
  327. {
  328. queries.Add(Query.Contains(key, likeDict[key].ToString()));
  329. }
  330. }
  331. }
  332. var doc = new List<BsonDocument>();
  333. if (queries != null && queries.Count() > 1)
  334. {
  335. doc = col.Find(Query.And(queries.ToArray())).ToList();
  336. }
  337. else if (queries != null && queries.Count() == 1)
  338. {
  339. doc = col.Find(queries.ToArray()[0]).ToList();
  340. }
  341. else
  342. {
  343. //throw new BizException("请输入查询条件!");
  344. doc = col.FindAll().ToList();
  345. }
  346. return MessagePackHelper.JsonToObject<List<T>>(doc.ToString());
  347. }
  348. /// <summary>
  349. /// 根据key-value方式获取 匹配查询
  350. /// </summary>
  351. /// <typeparam name="T"></typeparam>
  352. /// <param name="dict"></param>
  353. /// <returns></returns>
  354. public IList<T> FindListByDictAndLikeAndStartWith<T>(
  355. Dictionary<String, Object> dict,
  356. Dictionary<String, Object> likeDict,
  357. Dictionary<String, Object> startDict)
  358. where T : new()
  359. {
  360. // Open data file (or create if not exits)
  361. // var db = LiteDBSingleton.getInstance().GetLiteDatabase();
  362. Type t = typeof(T);
  363. // Get a collection (or create, if not exits)
  364. var col = database.GetCollection(t.Name.ToString());
  365. // var doc = col.FindById(docId);
  366. // String inf = doc.AsDocument.ToString();
  367. IList<Query> queries = new List<Query>();
  368. if (null != dict && dict.Count > 0)
  369. {
  370. var keys = dict.Keys;
  371. foreach (String key in keys)
  372. {
  373. if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
  374. {
  375. queries.Add(Query.EQ(key, dict[key].ToString()));
  376. }
  377. }
  378. }
  379. if (null != likeDict && likeDict.Count > 0)
  380. {
  381. var keys = likeDict.Keys;
  382. foreach (String key in keys)
  383. {
  384. if (likeDict[key] != null && !string.IsNullOrEmpty(likeDict[key].ToString()))
  385. {
  386. queries.Add(Query.Contains(key, likeDict[key].ToString()));
  387. }
  388. }
  389. }
  390. if (null != startDict && startDict.Count > 0)
  391. {
  392. var keys = startDict.Keys;
  393. foreach (String key in keys)
  394. {
  395. if (startDict[key] != null && !string.IsNullOrEmpty(startDict[key].ToString()))
  396. {
  397. queries.Add(Query.StartsWith(key, startDict[key].ToString()));
  398. }
  399. }
  400. }
  401. var doc = new List<BsonDocument>();
  402. if (queries != null && queries.Count() > 1)
  403. {
  404. doc = col.Find(Query.And(queries.ToArray())).ToList();
  405. }
  406. else if (queries != null && queries.Count() == 1)
  407. {
  408. doc = col.Find(queries.ToArray()[0]).ToList();
  409. }
  410. else
  411. {
  412. //throw new BizException("请输入查询条件!");
  413. doc = col.FindAll().ToList();
  414. }
  415. return MessagePackHelper.JsonToObject<List<T>>(doc.ToString());
  416. }
  417. /// <summary>
  418. /// 根据key-value方式获取 匹配查询
  419. /// </summary>
  420. /// <typeparam name="T"></typeparam>
  421. /// <param name="dict"></param>
  422. /// <returns></returns>
  423. public IList<T> FindListByDictAndLikeAndNotEQ<T>(Dictionary<String, Object> dict,
  424. Dictionary<String, Object> likeDict,
  425. Dictionary<String, Object> notEQDict)
  426. where T : new()
  427. {
  428. // Open data file (or create if not exits)
  429. // var db = LiteDatabaseSingleton.getInstance().GetLiteDatabase();
  430. Type t = typeof(T);
  431. // Get a collection (or create, if not exits)
  432. var col = database.GetCollection(t.Name.ToString());
  433. // var doc = col.FindById(docId);
  434. // String inf = doc.AsDocument.ToString();
  435. IList<Query> queries = new List<Query>();
  436. if (null != dict && dict.Count > 0)
  437. {
  438. var keys = dict.Keys;
  439. foreach (String key in keys)
  440. {
  441. if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
  442. {
  443. queries.Add(Query.EQ(key, dict[key].ToString()));
  444. }
  445. }
  446. }
  447. if (null != likeDict && likeDict.Count > 0)
  448. {
  449. var keys = likeDict.Keys;
  450. foreach (String key in keys)
  451. {
  452. if (likeDict[key] != null && !string.IsNullOrEmpty(likeDict[key].ToString()))
  453. {
  454. queries.Add(Query.Contains(key, likeDict[key].ToString()));
  455. }
  456. }
  457. }
  458. if (null != notEQDict && notEQDict.Count > 0)
  459. {
  460. var keys = notEQDict.Keys;
  461. foreach (String key in keys)
  462. {
  463. if (notEQDict[key] != null && !string.IsNullOrEmpty(notEQDict[key].ToString()))
  464. {
  465. queries.Add(Query.Not(key, notEQDict[key].ToString()));
  466. }
  467. }
  468. }
  469. var doc = new List<BsonDocument>();
  470. if (queries != null && queries.Count() > 1)
  471. {
  472. doc = col.Find(Query.And(queries.ToArray())).ToList();
  473. }
  474. else if (queries != null && queries.Count() == 1)
  475. {
  476. doc = col.Find(queries.ToArray()[0]).ToList();
  477. }
  478. else
  479. {
  480. // throw new BizException("请输入查询条件!");
  481. doc = col.FindAll().ToList();
  482. }
  483. return MessagePackHelper.JsonToObject<List<T>>(doc.ToString());
  484. }
  485. }
  486. }