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;
|
|
|
|
|
private static readonly object objLock = new();
|
2024-02-10 08:57:26 +00:00
|
|
|
|
public 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>();
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-07 12:24:26 +00:00
|
|
|
|
public int Insert(object model)
|
2023-01-01 11:42:01 +00:00
|
|
|
|
{
|
|
|
|
|
return _db.Insert(model);
|
|
|
|
|
}
|
2023-04-14 12:49:36 +00:00
|
|
|
|
|
2023-03-07 12:24:26 +00:00
|
|
|
|
public int InsertAll(IEnumerable models)
|
|
|
|
|
{
|
|
|
|
|
lock (objLock)
|
|
|
|
|
{
|
|
|
|
|
return _db.InsertAll(models);
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
|
2023-01-01 11:42:01 +00:00
|
|
|
|
public int Replace(object model)
|
|
|
|
|
{
|
2023-03-04 00:43:44 +00:00
|
|
|
|
lock (objLock)
|
|
|
|
|
{
|
|
|
|
|
return _db.InsertOrReplace(model);
|
|
|
|
|
}
|
2023-01-01 11:42:01 +00:00
|
|
|
|
}
|
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 int Update(object model)
|
|
|
|
|
{
|
2023-03-04 00:43:44 +00:00
|
|
|
|
lock (objLock)
|
|
|
|
|
{
|
|
|
|
|
return _db.Update(model);
|
|
|
|
|
}
|
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> UpdateAsync(object model)
|
|
|
|
|
{
|
|
|
|
|
return await _dbAsync.UpdateAsync(model);
|
|
|
|
|
}
|
2023-04-14 12:49:36 +00:00
|
|
|
|
|
2023-01-01 11:42:01 +00:00
|
|
|
|
public int UpdateAll(IEnumerable models)
|
|
|
|
|
{
|
2023-03-04 00:43:44 +00:00
|
|
|
|
lock (objLock)
|
|
|
|
|
{
|
|
|
|
|
return _db.UpdateAll(models);
|
|
|
|
|
}
|
2023-01-01 11:42:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Delete(object model)
|
|
|
|
|
{
|
2023-03-04 00:43:44 +00:00
|
|
|
|
lock (objLock)
|
|
|
|
|
{
|
|
|
|
|
return _db.Delete(model);
|
|
|
|
|
}
|
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
|
|
|
|
|
2023-01-01 11:42:01 +00:00
|
|
|
|
public List<T> Query<T>(string sql) where T : new()
|
|
|
|
|
{
|
|
|
|
|
return _db.Query<T>(sql);
|
|
|
|
|
}
|
2023-04-14 12:49:36 +00:00
|
|
|
|
|
2023-01-01 11:42:01 +00:00
|
|
|
|
public async Task<List<T>> QueryAsync<T>(string sql) where T : new()
|
|
|
|
|
{
|
|
|
|
|
return await _dbAsync.QueryAsync<T>(sql);
|
|
|
|
|
}
|
2023-04-14 12:49:36 +00:00
|
|
|
|
|
2023-01-01 11:42:01 +00:00
|
|
|
|
public int Execute(string sql)
|
|
|
|
|
{
|
|
|
|
|
return _db.Execute(sql);
|
|
|
|
|
}
|
2023-04-14 12:49:36 +00:00
|
|
|
|
|
2023-01-01 11:42:01 +00:00
|
|
|
|
public async Task<int> ExecuteAsync(string sql)
|
|
|
|
|
{
|
|
|
|
|
return await _dbAsync.ExecuteAsync(sql);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TableQuery<T> Table<T>() where T : new()
|
|
|
|
|
{
|
|
|
|
|
return _db.Table<T>();
|
|
|
|
|
}
|
2023-04-14 12:49:36 +00:00
|
|
|
|
|
2023-01-01 11:42:01 +00:00
|
|
|
|
public AsyncTableQuery<T> TableAsync<T>() where T : new()
|
|
|
|
|
{
|
|
|
|
|
return _dbAsync.Table<T>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|