using DownKyi.Core.Logging; using System; using System.Collections.Generic; using System.Data; using System.Data.SQLite; using System.IO; using System.Runtime.Serialization.Formatters.Binary; namespace DownKyi.Core.Storage.Database.Download { public class DownloadDb { private const string key = "bdb8eb69-3698-4af9-b722-9312d0fba623"; private readonly DbHelper dbHelper = new DbHelper(StorageManager.GetDownload(), key); protected string tableName = "download"; /// /// 关闭数据库连接 /// public void Close() { dbHelper.Close(); } /// /// 插入新的数据 /// /// public void Insert(string uuid, object obj) { // 定义一个流 Stream stream = new MemoryStream(); // 定义一个格式化器 BinaryFormatter formatter = new BinaryFormatter(); // 序列化 formatter.Serialize(stream, obj); byte[] array = null; array = new byte[stream.Length]; //将二进制流写入数组 stream.Position = 0; stream.Read(array, 0, (int)stream.Length); //关闭流 stream.Close(); try { string sql = $"insert into {tableName}(id, data) values (@id, @data)"; dbHelper.ExecuteNonQuery(sql, new Action((para) => { para.Add("@id", DbType.String).Value = uuid; para.Add("@data", DbType.Binary).Value = array; })); } catch (Exception e) { Utils.Debugging.Console.PrintLine("Insert()发生异常: {0}", e); LogManager.Error($"{tableName}", e); } } /// /// 删除uuid对应的数据 /// /// public void Delete(string uuid) { try { string sql = $"delete from {tableName} where id glob '{uuid}'"; dbHelper.ExecuteNonQuery(sql); } catch (Exception e) { Utils.Debugging.Console.PrintLine("Delete()发生异常: {0}", e); LogManager.Error($"{tableName}", e); } } public void Update(string uuid, object obj) { // 定义一个流 Stream stream = new MemoryStream(); // 定义一个格式化器 BinaryFormatter formatter = new BinaryFormatter(); // 序列化 formatter.Serialize(stream, obj); byte[] array = null; array = new byte[stream.Length]; //将二进制流写入数组 stream.Position = 0; stream.Read(array, 0, (int)stream.Length); //关闭流 stream.Close(); try { string sql = $"update {tableName} set data=@data where id glob @id"; dbHelper.ExecuteNonQuery(sql, new Action((para) => { para.Add("@id", DbType.String).Value = uuid; para.Add("@data", DbType.Binary).Value = array; })); } catch (Exception e) { Utils.Debugging.Console.PrintLine("Insert()发生异常: {0}", e); LogManager.Error($"{tableName}", e); } } /// /// 查询所有数据 /// /// /// public Dictionary QueryAll() { string sql = $"select * from {tableName}"; return Query(sql); } /// /// 查询uuid对应的数据 /// /// /// public object QueryById(string uuid) { string sql = $"select * from {tableName} where id glob '{uuid}'"; Dictionary query = Query(sql); if (query.ContainsKey(uuid)) { query.TryGetValue(uuid, out object obj); return obj; } else { return null; } } /// /// 查询数据 /// /// /// private Dictionary Query(string sql) { Dictionary objects = new Dictionary(); dbHelper.ExecuteQuery(sql, reader => { while (reader.Read()) { // 读取字节数组 byte[] array = (byte[])reader["data"]; // 定义一个流 MemoryStream stream = new MemoryStream(array); //定义一个格式化器 BinaryFormatter formatter = new BinaryFormatter(); // 反序列化 object obj = formatter.Deserialize(stream); objects.Add((string)reader["id"], obj); } }); return objects; } /// /// 如果表不存在则创建表 /// protected void CreateTable() { string sql = $"create table if not exists {tableName} (id varchar(255) unique, data blob)"; dbHelper.ExecuteNonQuery(sql); } } }