XSql.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Threading;
  7. using UnityEngine.Networking;
  8. using UnityEngine.XR;
  9. using XRTool.Util;
  10. namespace PublicTools.XMLDataBase
  11. {
  12. public enum DataType
  13. {
  14. /// <summary>
  15. /// xml的方式存储
  16. /// </summary>
  17. Xml = 0,
  18. /// <summary>
  19. /// json文本的方式存储
  20. /// </summary>
  21. Json = 1,
  22. /// <summary>
  23. /// sqlite
  24. /// </summary>
  25. SqlLite = 2,
  26. /// <summary>
  27. /// 文本的方式存储
  28. /// </summary>
  29. Txt = 3,
  30. /// <summary>
  31. /// mysql读写
  32. /// </summary>
  33. MySql = 4,
  34. /// <summary>
  35. /// 预留扩展
  36. /// </summary>
  37. Other = 5
  38. }
  39. /// <summary>
  40. /// 数据库的管理类
  41. /// 注意同时只能操作一个数据库文件,当传入新的路径时,老的文件连接都将被关闭
  42. /// </summary>
  43. public class XSql : Singleton<XSql>
  44. {
  45. private Dictionary<string, string> sqlList = new Dictionary<string, string>();
  46. /// <summary>
  47. /// 缓存表格处理对象
  48. /// </summary>
  49. private Dictionary<string, TableInterface> tableDic = new Dictionary<string, TableInterface>();
  50. private string sqlPath;
  51. /// <summary>
  52. /// 当前正在处理的数据库文件路径
  53. /// </summary>
  54. public string SqlPath { get => sqlPath; set => sqlPath = value; }
  55. /// <summary>
  56. /// 打开数据库
  57. /// 即创建文件夹,开启对应的文件的读取流
  58. /// 同时仅能操作一个文件夹
  59. /// </summary>
  60. /// <param name="sqlPath">绝对路径</param>
  61. public bool Open(string sqlPath, bool isforceCreate = false)
  62. {
  63. if (!string.IsNullOrEmpty(sqlPath) && SqlPath == sqlPath)
  64. {
  65. return true;
  66. }
  67. try
  68. {
  69. if (isforceCreate && !Directory.Exists(sqlPath))
  70. {
  71. Directory.CreateDirectory(sqlPath);
  72. }
  73. if (Directory.Exists(sqlPath))
  74. {
  75. this.SqlPath = sqlPath;
  76. return true;
  77. }
  78. }
  79. catch (Exception ex)
  80. {
  81. UnityLog.LogException(ex);
  82. UnityLog.LogError(ex.ToString());
  83. }
  84. return false;
  85. }
  86. /// <summary>
  87. /// 关闭数据库
  88. /// 即关闭此文件夹的所有文件读取流
  89. /// </summary>
  90. /// <param name="sqlPath"></param>
  91. public void Close()
  92. {
  93. foreach (var item in tableDic)
  94. {
  95. item.Value.Close();
  96. }
  97. tableDic.Clear();
  98. }
  99. public TableInterface OpenTable(string tableName, DataType dataType, bool forceCreate = false)
  100. {
  101. string suffix = ".xml";
  102. if (dataType == DataType.Xml)
  103. {
  104. suffix = ".xml";
  105. }
  106. else if (dataType == DataType.Json)
  107. {
  108. suffix = ".json";
  109. }
  110. return OpenTable(tableName, suffix, forceCreate);
  111. }
  112. /// <summary>
  113. /// 打开本地表格
  114. /// 打开对应的文件流,文件流存在时可能为空
  115. /// 操作表格的情况:不存在文件,存在但是字节流为空,字节流不为空
  116. /// 字节流为空时如何处理?
  117. /// </summary>
  118. /// <param name="tableName"></param>
  119. /// <param name="forceCreate">强制创建,文件为空时创建我呢见</param>
  120. /// <param name="suffix">文件后缀名</param>
  121. public TableInterface OpenTable(string tableName, string suffix = ".xml", bool forceCreate = false)
  122. {
  123. return OpenTable(SqlPath, tableName, suffix, forceCreate);
  124. }
  125. /// <summary>
  126. /// 打开本地表格
  127. /// 打开对应的文件流,文件流存在时可能为空
  128. /// 操作表格的情况:不存在文件,存在但是字节流为空,字节流不为空
  129. /// 字节流为空时如何处理?
  130. /// 注意安卓下StreamPath下无法使用File类读取!会报告异常
  131. /// </summary>
  132. /// <param name="tableName"></param>
  133. /// <param name="forceCreate">强制创建,文件为空时创建我呢见</param>
  134. /// <param name="suffix">文件后缀名</param>
  135. public TableInterface OpenTable(string sqlPath, string tableName, string suffix = ".xml", bool forceCreate = false)
  136. {
  137. if (!Open(sqlPath, forceCreate))
  138. {
  139. return null;
  140. }
  141. string unExtenName = Path.GetFileNameWithoutExtension(tableName);
  142. if (tableDic.ContainsKey(unExtenName))
  143. {
  144. return tableDic[unExtenName];
  145. }
  146. ///文件后缀,如果table已有后缀不添加后缀,若无后缀名,则添加指定的后缀名
  147. if (string.IsNullOrEmpty(Path.GetExtension(tableName)))
  148. {
  149. tableName += suffix;
  150. }
  151. string path = Path.Combine(sqlPath, tableName);
  152. try
  153. {
  154. bool isCreateNewTable = false;
  155. ///目标文件不存在,强制创建
  156. if (forceCreate && !File.Exists(path))
  157. {
  158. File.Create(path).Dispose();
  159. isCreateNewTable = true;
  160. }
  161. ///文件存在,创建对应的对象
  162. if (File.Exists(path))
  163. {
  164. TableInterface fs = new XTable(path);
  165. tableDic.Add(unExtenName, fs);
  166. if (isCreateNewTable)
  167. {
  168. fs.Create(tableName);
  169. fs.Save();
  170. }
  171. return fs;
  172. }
  173. }
  174. catch (Exception ex)
  175. {
  176. ///IO异常处理
  177. UnityEngine.Debug.LogException(ex);
  178. UnityLog.LogError(ex.ToString());
  179. }
  180. return null;
  181. }
  182. /// <summary>
  183. /// 关闭本地表操作
  184. /// </summary>
  185. /// <param name="tableName"></param>
  186. public void CloseTable(string tableName)
  187. {
  188. string unExtenName = Path.GetFileNameWithoutExtension(tableName);
  189. if (tableDic.ContainsKey(unExtenName))
  190. {
  191. tableDic[unExtenName].Close();
  192. tableDic.Remove(unExtenName);
  193. }
  194. }
  195. /// <summary>
  196. /// 新建一个表格
  197. /// </summary>
  198. /// <param name="tableName"></param>
  199. public bool CreateTable(string tableName, DataType dataType)
  200. {
  201. string suffix = ".xml";
  202. if (dataType == DataType.Xml)
  203. {
  204. suffix = ".xml";
  205. }
  206. else if (dataType == DataType.Json)
  207. {
  208. suffix = ".json";
  209. }
  210. return CreateTable(tableName, suffix);
  211. }
  212. /// <summary>
  213. /// 新建一个表格
  214. /// </summary>
  215. /// <param name="tableName"></param>
  216. public bool CreateTable(string tableName, string suffix = ".xml")
  217. {
  218. if (string.IsNullOrEmpty(Path.GetExtension(tableName)))
  219. {
  220. tableName += suffix;
  221. }
  222. TableInterface table = new XTable();
  223. return table.Create(tableName);
  224. }
  225. /// <summary>
  226. /// 读取远程的表格数据信息
  227. /// 或者在只读文件夹下读取数据
  228. /// </summary>
  229. /// <typeparam name="T"></typeparam>
  230. /// <param name="data">数据</param>
  231. /// <param name="type">数据类型,可能是.xml或者json文本0代表xml文本</param>
  232. /// <returns></returns>
  233. public List<T> ReadServerData<T>(byte[] data, int type = 0)
  234. {
  235. TableInterface table = new XTable(type);
  236. List<T> list = table.FindAllData<T>(data);
  237. table.Close();
  238. table = null;
  239. return list;
  240. }
  241. /// <summary>
  242. /// 读取远程的表格数据信息
  243. /// 或者在只读文件夹下读取数据
  244. /// </summary>
  245. /// <typeparam name="T"></typeparam>
  246. /// <param name="data">数据</param>
  247. /// <param name="type">数据类型,可能是.xml或者json文本0代表xml文本</param>
  248. /// <returns></returns>
  249. public List<T> ReadServerData<T>(string data, int type = 0)
  250. {
  251. TableInterface table = new XTable(type);
  252. List<T> list = table.FindAllData<T>(data);
  253. table.Close();
  254. table = null;
  255. return list;
  256. }
  257. public IEnumerator ReadServerData<T>(string url, Action<List<T>> action, int type = 0)
  258. {
  259. yield return DataFileUtil.RequestDownData(url, (string error, UnityWebRequest handler) =>
  260. {
  261. if (!string.IsNullOrEmpty(error))
  262. {
  263. UnityLog.LogError(url + "Read Data Error" + error);
  264. action?.Invoke(null);
  265. }
  266. else
  267. {
  268. action?.Invoke(ReadServerData<T>(handler.downloadHandler.data, type));
  269. }
  270. });
  271. }
  272. }
  273. }