using System.Collections.Generic; namespace DownKyi.Core.Storage.Database { public class CoverDb { private const string key = "b5018ecc-09d1-4da2-aa49-4625e41e623e"; private readonly DbHelper dbHelper = new DbHelper(StorageManager.GetCoverIndex(), key); public CoverDb() { CreateTable(); } /// /// 关闭数据库连接 /// public void Close() { dbHelper.Close(); } /// /// 插入新的数据 /// /// public void Insert(Cover cover) { string sql = $"insert into cover values ({cover.Avid}, '{cover.Bvid}', {cover.Cid}, '{cover.Url}', '{cover.Md5}')"; dbHelper.ExecuteNonQuery(sql); } /// /// 更新数据,以url检索 /// /// public void Update(Cover cover) { string sql = $"update cover set avid={cover.Avid}, bvid='{cover.Bvid}', cid={cover.Cid}, md5='{cover.Md5}' where url glob '{cover.Url}'"; dbHelper.ExecuteNonQuery(sql); } /// /// 查询所有数据 /// /// public List QueryAll() { string sql = $"select * from cover"; return Query(sql); } /// /// 查询url对应的数据 /// /// /// public Cover QueryByUrl(string url) { string sql = $"select * from cover where url glob '{url}'"; var query = Query(sql); if (query.Count > 0) { return query[0]; } else { return null; } } /// /// 查询md5对应的数据 /// /// /// public Cover QueryByMd5(string md5) { string sql = $"select * from cover where md5 glob '{md5}'"; var query = Query(sql); if (query.Count > 0) { return query[0]; } else { return null; } } /// /// 查询数据 /// /// /// private List Query(string sql) { List covers = new List(); dbHelper.ExecuteQuery(sql, reader => { while (reader.Read()) { Cover cover = new Cover { Avid = (long)reader["avid"], Bvid = (string)reader["bvid"], Cid = (long)reader["cid"], Url = (string)reader["url"], Md5 = (string)reader["md5"] }; covers.Add(cover); } }); return covers; } /// /// 如果表不存在则创建表 /// private void CreateTable() { string sql = "create table if not exists cover (avid unsigned big int, bvid varchar(20), cid unsigned big int, url varchar(255) unique, md5 varchar(32) unique)"; dbHelper.ExecuteNonQuery(sql); } } }