using System.Collections.Generic;
namespace DownKyi.Core.Storage.Database
{
public class HeaderDb
{
private const string key = "7c1f1f40-7cdf-4d11-ad28-f0137a3c5308";
private readonly DbHelper dbHelper = new DbHelper(StorageManager.GetHeaderIndex(), key);
public HeaderDb()
{
CreateTable();
}
///
/// 关闭数据库连接
///
public void Close()
{
dbHelper.Close();
}
///
/// 插入新的数据
///
///
public void Insert(Header header)
{
string sql = $"insert into header values ({header.Mid}, '{header.Name}', '{header.Url}', '{header.Md5}')";
dbHelper.ExecuteNonQuery(sql);
}
///
/// 更新数据
///
///
public void Update(Header header)
{
string sql = $"update header set name='{header.Name}', url='{header.Url}', md5='{header.Md5}' where mid={header.Mid}";
dbHelper.ExecuteNonQuery(sql);
}
///
/// 查询所有数据
///
///
public List QueryAll()
{
string sql = $"select * from header";
return Query(sql);
}
///
/// 查询mid对应的数据
///
///
///
public Header QueryByMid(long mid)
{
string sql = $"select * from header where mid={mid}";
var query = Query(sql);
if (query.Count > 0)
{
return query[0];
}
else
{
return null;
}
}
///
/// 查询数据
///
///
///
private List Query(string sql)
{
List headers = new List();
dbHelper.ExecuteQuery(sql, reader =>
{
while (reader.Read())
{
Header header = new Header
{
Mid = (long)reader["mid"],
Name = (string)reader["name"],
Url = (string)reader["url"],
Md5=(string)reader["md5"]
};
headers.Add(header);
}
});
return headers;
}
///
/// 如果表不存在则创建表
///
private void CreateTable()
{
string sql = "create table if not exists header (mid unsigned big int unique, name varchar(255), url varchar(255), md5 varchar(32))";
dbHelper.ExecuteNonQuery(sql);
}
}
}