Added SQLite storage backend.
This commit is contained in:
parent
999ce86a27
commit
3f6007922c
21 changed files with 665 additions and 245 deletions
SharpChat.SQLite
56
SharpChat.SQLite/SQLiteConnection.cs
Normal file
56
SharpChat.SQLite/SQLiteConnection.cs
Normal file
|
@ -0,0 +1,56 @@
|
|||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Data.SQLite;
|
||||
using NativeSQLiteConnection = System.Data.SQLite.SQLiteConnection;
|
||||
|
||||
namespace SharpChat.SQLite;
|
||||
|
||||
public class SQLiteConnection(NativeSQLiteConnection conn) : IDisposable {
|
||||
public async Task<int> RunCommand(string command, params SQLiteParameter[] parameters) {
|
||||
using SQLiteCommand cmd = conn.CreateCommand();
|
||||
if(parameters?.Length > 0)
|
||||
cmd.Parameters.AddRange(parameters);
|
||||
cmd.CommandText = command;
|
||||
return await cmd.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
public async Task<DbDataReader?> RunQuery(string command, params SQLiteParameter[] parameters) {
|
||||
using SQLiteCommand cmd = conn.CreateCommand();
|
||||
if(parameters?.Length > 0)
|
||||
cmd.Parameters.AddRange(parameters);
|
||||
cmd.CommandText = command;
|
||||
return await cmd.ExecuteReaderAsync();
|
||||
}
|
||||
|
||||
public async Task<T> RunQueryValue<T>(string command, params SQLiteParameter[] parameters)
|
||||
where T : struct {
|
||||
using SQLiteCommand cmd = conn.CreateCommand();
|
||||
if(parameters?.Length > 0)
|
||||
cmd.Parameters.AddRange(parameters);
|
||||
cmd.CommandText = command;
|
||||
await cmd.PrepareAsync();
|
||||
|
||||
object? raw = await cmd.ExecuteScalarAsync();
|
||||
return raw is T value ? value : default;
|
||||
}
|
||||
|
||||
private bool disposed = false;
|
||||
|
||||
~SQLiteConnection() {
|
||||
DoDispose();
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
DoDispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
private void DoDispose() {
|
||||
if(disposed)
|
||||
return;
|
||||
disposed = true;
|
||||
|
||||
RunCommand("VACUUM").Wait();
|
||||
conn.Dispose();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue