AppDbContext: Add ExecuteEmbeddedScript()

This commit is contained in:
2024-01-05 13:55:50 +01:00
parent e75e2ddbda
commit 3f6a94e773
2 changed files with 9 additions and 5 deletions

View File

@ -10,6 +10,7 @@ using Microsoft.Data.Sqlite;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using Elwig.Models.Dtos;
using System.Reflection;
namespace Elwig.Helpers {
@ -106,6 +107,12 @@ namespace Elwig.Helpers {
await (await cmd.ExecuteReaderAsync()).CloseAsync();
}
public static async Task ExecuteEmbeddedScript(SqliteConnection cnx, Assembly asm, string name) {
using var stream = asm.GetManifestResourceStream(name) ?? throw new FileNotFoundException("Unable to load embedded resource");
using var reader = new StreamReader(stream);
await ExecuteBatch(cnx, await reader.ReadToEndAsync());
}
public static async Task<object?> ExecuteScalar(SqliteConnection cnx, string sql) {
using var cmd = cnx.CreateCommand();
cmd.CommandText = sql;