v2rayN/v2rayN/ServiceLib/Common/SqliteHelper.cs

91 lines
2.5 KiB
C#
Raw Normal View History

2023-04-14 12:49:36 +00:00
using SQLite;
2023-01-01 11:42:01 +00:00
using System.Collections;
2024-08-19 10:15:54 +00:00
namespace ServiceLib.Common
2023-01-01 11:42:01 +00:00
{
2024-02-19 09:43:36 +00:00
public sealed class SQLiteHelper
2023-01-01 11:42:01 +00:00
{
2024-02-19 09:43:36 +00:00
private static readonly Lazy<SQLiteHelper> _instance = new(() => new());
public static SQLiteHelper Instance => _instance.Value;
2023-01-01 11:42:01 +00:00
private string _connstr;
2023-03-04 00:43:44 +00:00
private SQLiteConnection _db;
private SQLiteAsyncConnection _dbAsync;
2024-10-14 02:57:40 +00:00
private readonly string _configDB = "guiNDB.db";
2023-01-01 11:42:01 +00:00
2024-02-19 09:43:36 +00:00
public SQLiteHelper()
2023-01-01 11:42:01 +00:00
{
2024-03-26 06:26:03 +00:00
_connstr = Utils.GetConfigPath(_configDB);
2023-01-01 11:42:01 +00:00
_db = new SQLiteConnection(_connstr, false);
_dbAsync = new SQLiteAsyncConnection(_connstr, false);
}
public CreateTableResult CreateTable<T>()
{
return _db.CreateTable<T>();
}
2024-10-20 03:51:05 +00:00
public async Task<int> InsertAllAsync(IEnumerable models)
2023-01-01 11:42:01 +00:00
{
2024-10-20 03:51:05 +00:00
return await _dbAsync.InsertAllAsync(models);
2023-03-07 12:24:26 +00:00
}
2023-04-14 12:49:36 +00:00
2023-03-07 12:24:26 +00:00
public async Task<int> InsertAsync(object model)
2023-01-01 11:42:01 +00:00
{
return await _dbAsync.InsertAsync(model);
}
2023-04-14 12:49:36 +00:00
2024-02-19 09:43:36 +00:00
public async Task<int> ReplaceAsync(object model)
2023-01-01 11:42:01 +00:00
{
return await _dbAsync.InsertOrReplaceAsync(model);
}
public async Task<int> UpdateAsync(object model)
{
return await _dbAsync.UpdateAsync(model);
}
2023-04-14 12:49:36 +00:00
2024-10-20 03:51:05 +00:00
public async Task<int> UpdateAllAsync(IEnumerable models)
2023-01-01 11:42:01 +00:00
{
2024-10-20 03:51:05 +00:00
return await _dbAsync.UpdateAllAsync(models);
2023-01-01 11:42:01 +00:00
}
2023-04-14 12:49:36 +00:00
2023-01-01 11:42:01 +00:00
public async Task<int> DeleteAsync(object model)
{
return await _dbAsync.DeleteAsync(model);
}
2023-04-14 12:49:36 +00:00
2024-10-20 03:51:05 +00:00
public async Task<int> DeleteAllAsync<T>()
2023-01-01 11:42:01 +00:00
{
2024-10-20 03:51:05 +00:00
return await _dbAsync.DeleteAllAsync<T>();
2023-01-01 11:42:01 +00:00
}
2023-04-14 12:49:36 +00:00
2024-10-20 03:51:05 +00:00
public async Task<int> ExecuteAsync(string sql)
2023-01-01 11:42:01 +00:00
{
2024-10-20 03:51:05 +00:00
return await _dbAsync.ExecuteAsync(sql);
2023-01-01 11:42:01 +00:00
}
2023-04-14 12:49:36 +00:00
2024-10-20 03:51:05 +00:00
public async Task<List<T>> QueryAsync<T>(string sql) where T : new()
2023-01-01 11:42:01 +00:00
{
2024-10-20 03:51:05 +00:00
return await _dbAsync.QueryAsync<T>(sql);
2023-01-01 11:42:01 +00:00
}
public AsyncTableQuery<T> TableAsync<T>() where T : new()
{
return _dbAsync.Table<T>();
}
public async Task DisposeDbConnectionAsync()
{
await Task.Factory.StartNew(() =>
{
_db?.Close();
_db?.Dispose();
_db = null;
_dbAsync?.GetConnection()?.Close();
_dbAsync?.GetConnection()?.Dispose();
_dbAsync = null;
});
}
2023-01-01 11:42:01 +00:00
}
}