v2rayN/v2rayN/ServiceLib/Helper/SqliteHelper.cs

91 lines
2.2 KiB
C#
Raw Normal View History

2023-01-01 11:42:01 +00:00
using System.Collections;
2025-01-30 09:10:05 +00:00
using SQLite;
2023-01-01 11:42:01 +00:00
2025-08-17 08:26:13 +00:00
namespace ServiceLib.Helper;
public sealed class SQLiteHelper
2023-01-01 11:42:01 +00:00
{
private static readonly Lazy<SQLiteHelper> _instance = new(() => new());
public static SQLiteHelper Instance => _instance.Value;
private readonly string _connstr;
private SQLiteConnection _db;
private SQLiteAsyncConnection _dbAsync;
private readonly string _configDB = "guiNDB.db";
public SQLiteHelper()
2023-01-01 11:42:01 +00:00
{
_connstr = Utils.GetConfigPath(_configDB);
_db = new SQLiteConnection(_connstr, false);
_dbAsync = new SQLiteAsyncConnection(_connstr, false);
}
2023-01-01 11:42:01 +00:00
public CreateTableResult CreateTable<T>()
{
return _db.CreateTable<T>();
}
2023-01-01 11:42:01 +00:00
public async Task<int> InsertAllAsync(IEnumerable models)
{
return await _dbAsync.InsertAllAsync(models);
}
2023-04-14 12:49:36 +00:00
public async Task<int> InsertAsync(object model)
{
return await _dbAsync.InsertAsync(model);
}
2023-04-14 12:49:36 +00:00
public async Task<int> ReplaceAsync(object model)
{
return await _dbAsync.InsertOrReplaceAsync(model);
}
2023-01-01 11:42:01 +00:00
public async Task<int> UpdateAsync(object model)
{
return await _dbAsync.UpdateAsync(model);
}
2023-04-14 12:49:36 +00:00
public async Task<int> UpdateAllAsync(IEnumerable models)
{
return await _dbAsync.UpdateAllAsync(models);
}
2023-04-14 12:49:36 +00:00
public async Task<int> DeleteAsync(object model)
{
return await _dbAsync.DeleteAsync(model);
}
2023-04-14 12:49:36 +00:00
public async Task<int> DeleteAllAsync<T>()
{
return await _dbAsync.DeleteAllAsync<T>();
}
2023-04-14 12:49:36 +00:00
public async Task<int> ExecuteAsync(string sql)
{
return await _dbAsync.ExecuteAsync(sql);
}
2023-04-14 12:49:36 +00:00
public async Task<List<T>> QueryAsync<T>(string sql) where T : new()
{
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
}
2025-01-30 09:10:05 +00:00
}