XTable.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //using BestHTTP.JSON;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. namespace PublicTools.XMLDataBase
  6. {
  7. /// <summary>
  8. /// 表格代理
  9. /// 一个表格的控制器,支持多表同时编辑,提高IO效率
  10. /// </summary>
  11. public class XTable : TableInterface
  12. {
  13. /// <summary>
  14. /// 绝对路径
  15. /// </summary>
  16. private TableInterface table;
  17. public XTable(int type)
  18. {
  19. if (type == 0)
  20. {
  21. table = new XMLDataEditor();
  22. }
  23. else if (type == 1)
  24. {
  25. //table = new JsonDataEditor();
  26. }
  27. }
  28. public XTable(string path)
  29. {
  30. //Open(path);
  31. string exten = Path.GetExtension(path);
  32. if (exten == ".xml")
  33. {
  34. table = new XMLDataEditor(path);
  35. }
  36. else if (exten == ".json")
  37. {
  38. //table = new JsonDataEditor(path);
  39. }
  40. if (table == null)
  41. {
  42. UnityEngine.Debug.LogError("Table is null!" + path);
  43. }
  44. }
  45. public XTable(DataType dataType)
  46. {
  47. //Open(path);
  48. if (dataType == DataType.Xml)
  49. {
  50. table = new XMLDataEditor();
  51. }
  52. else if (dataType == DataType.Json)
  53. {
  54. //table = new JsonDataEditor();
  55. }
  56. if (table == null)
  57. {
  58. UnityEngine.Debug.LogError("Table is null!" + dataType);
  59. }
  60. }
  61. public XTable()
  62. {
  63. }
  64. /// <summary>
  65. /// 打开表格
  66. /// 支持xml,json文件的处理
  67. /// 后续可能支持sqlite或者其他处理方式
  68. /// </summary>
  69. /// <param name="path"></param>
  70. public bool Open()
  71. {
  72. return table.Open();
  73. }
  74. /// <summary>
  75. /// 打开表格
  76. /// 支持xml,json文件的处理
  77. /// 后续可能支持sqlite或者其他处理方式
  78. /// </summary>
  79. /// <param name="path"></param>
  80. public bool Open(string path)
  81. {
  82. return table.Open(path);
  83. }
  84. /// <summary>
  85. /// 关闭操作
  86. /// </summary>
  87. public void Close()
  88. {
  89. table.Close();
  90. }
  91. /// <summary>
  92. /// 插入一个对象
  93. /// </summary>
  94. /// <typeparam name="T"></typeparam>
  95. /// <param name="data"></param>
  96. /// <param name="suffix"></param>
  97. /// <returns></returns>
  98. public bool InsertData<T>(T data)
  99. {
  100. if (data != null)
  101. {
  102. return table.InsertData(data);
  103. }
  104. return false;
  105. }
  106. /// <summary>
  107. /// 查找对象
  108. /// </summary>
  109. /// <typeparam name="T"></typeparam>
  110. /// <param name="key"></param>
  111. /// <returns></returns>
  112. public T FindData<T>(string key)
  113. {
  114. return table.FindData<T>(key);
  115. }
  116. /// <summary>
  117. /// 查找所有对象
  118. /// </summary>
  119. /// <typeparam name="T"></typeparam>
  120. /// <returns></returns>
  121. public List<T> FindAllData<T>()
  122. {
  123. return table.FindAllData<T>();
  124. }
  125. /// <summary>
  126. /// 查找所有对象
  127. /// </summary>
  128. /// <typeparam name="T"></typeparam>
  129. /// <returns></returns>
  130. public List<T> FindAllData<T>(string data)
  131. {
  132. return table.FindAllData<T>(data);
  133. }
  134. /// <summary>
  135. /// 更新数据
  136. /// </summary>
  137. /// <typeparam name="T"></typeparam>
  138. /// <param name="data"></param>
  139. /// <param name="suffix"></param>
  140. /// <returns></returns>
  141. public bool UpdateData<T>(T data)
  142. {
  143. if (data != null)
  144. {
  145. return table.UpdateData(data);
  146. }
  147. return false;
  148. }
  149. /// <summary>
  150. /// 删除元素
  151. /// </summary>
  152. /// <typeparam name="T"></typeparam>
  153. /// <param name="key"></param>
  154. /// <param name="suffix">默认使用xml,可以是其他文件如json</param>
  155. /// <returns></returns>
  156. public bool DeleteData<T>(string key)
  157. {
  158. if (!string.IsNullOrEmpty(key))
  159. {
  160. return table.DeleteData<T>(key);
  161. }
  162. return false;
  163. }
  164. /// <summary>
  165. /// 删除所有元素
  166. /// </summary>
  167. /// <typeparam name="T"></typeparam>
  168. /// <param name="key"></param>
  169. /// <param name="suffix"></param>
  170. /// <returns></returns>
  171. public bool DeleteAllData<T>()
  172. {
  173. return table.DeleteAllData<T>();
  174. }
  175. public bool Create(string tableName)
  176. {
  177. return table.Create(tableName);
  178. }
  179. public void Save()
  180. {
  181. table.Save();
  182. }
  183. public List<T> FindAllData<T>(byte[] data)
  184. {
  185. return table.FindAllData<T>(data);
  186. }
  187. public bool InsertData<T>(T data, string priKey)
  188. {
  189. return table.InsertData<T>(data,priKey);
  190. }
  191. public bool UpdateData<T>(T data, string key)
  192. {
  193. return table.InsertData<T>(data,key);
  194. }
  195. }
  196. }