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());
//private readonly DbHelper dbHelper = new DbHelper(StorageManager.GetDownload(), key);
protected string tableName = "download";
//public DownloadDb()
//{
// CreateTable();
//}
///
/// 关闭数据库连接
///
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("DownloadingDb", e);
}
}
///
/// 查询所有数据
///
///
///
public Dictionary QueryAll(string sql)
{
Dictionary objects = new Dictionary();
dbHelper.ExecuteQuery(sql, reader =>
{
while (reader.Read())
{
objects.Add((string)reader["id"], reader["data"]);
}
});
return objects;
}
///
/// 如果表不存在则创建表
///
protected void CreateTable()
{
string sql = $"create table if not exists {tableName} (id varchar(255), data blob)";
dbHelper.ExecuteNonQuery(sql);
}
}
}