Added SQLite storage backend.

This commit is contained in:
flash 2025-04-27 22:31:35 +00:00
commit 3f6007922c
Signed by: flash
GPG key ID: 2C9C2C574D47FE3E
21 changed files with 665 additions and 245 deletions
SharpChat.SQLite

View 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();
}
}