2025-09-03 15:51:25 +02:00
|
|
|
using System;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using ServiceStack.Data;
|
|
|
|
|
using ServiceStack.OrmLite;
|
2026-04-07 14:23:29 +02:00
|
|
|
using Finch.Configuration;
|
|
|
|
|
using Finch.Models;
|
|
|
|
|
using Finch.Modules;
|
2025-09-03 15:51:25 +02:00
|
|
|
|
2026-04-07 14:23:29 +02:00
|
|
|
namespace Finch.Sqlite;
|
2025-09-03 15:51:25 +02:00
|
|
|
|
2026-04-07 14:23:29 +02:00
|
|
|
public static class FinchBuilderExtensions
|
2025-09-03 15:51:25 +02:00
|
|
|
{
|
2026-04-07 14:23:29 +02:00
|
|
|
public static FinchBuilder AddSqlite(this FinchBuilder builder)
|
2025-09-03 15:51:25 +02:00
|
|
|
{
|
2026-04-07 14:23:29 +02:00
|
|
|
builder.AddModule<FinchSqliteModule>();
|
2025-09-03 15:51:25 +02:00
|
|
|
return builder;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 14:23:29 +02:00
|
|
|
internal class FinchSqliteModule : FinchModule
|
2025-09-03 15:51:25 +02:00
|
|
|
{
|
|
|
|
|
public override void ConfigureServices(IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IDbConnectionFactory>(CreateDbConnectionFactory);
|
|
|
|
|
services.AddScoped<IDbConnection>(CreateDbConnection);
|
|
|
|
|
services.AddScoped<IDbOperations, DbOperations>();
|
|
|
|
|
services.AddScoped<StoreContext>();
|
2026-02-17 15:57:47 +01:00
|
|
|
services.AddScoped<IEntityModifiedHandler, EmptyEntityModifiedHandler>();
|
2025-09-03 15:51:25 +02:00
|
|
|
services.AddOptions<FlavorOptions>();
|
2026-04-07 14:23:29 +02:00
|
|
|
services.AddOptions<SqliteOptions>().Bind(configuration.GetSection("Finch:Sqlite"));
|
2025-09-03 15:51:25 +02:00
|
|
|
services.ConfigureOptions<ConfigureFlavorJsonOptions>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected IDbConnectionFactory CreateDbConnectionFactory(IServiceProvider services)
|
|
|
|
|
{
|
2026-04-07 14:23:29 +02:00
|
|
|
IFinchOptions options = services.GetService<IFinchOptions>();
|
2025-09-04 16:05:41 +02:00
|
|
|
SqliteOptions sqliteOptions = options.For<SqliteOptions>();
|
|
|
|
|
return new OrmLiteConnectionFactory(sqliteOptions.ConnectionString, SqliteDialect.Provider);
|
2025-09-03 15:51:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected IDbConnection CreateDbConnection(IServiceProvider services)
|
|
|
|
|
{
|
|
|
|
|
IDbConnectionFactory factory = services.GetService<IDbConnectionFactory>();
|
2026-04-07 14:23:29 +02:00
|
|
|
IFinchOptions options = services.GetService<IFinchOptions>();
|
2025-09-04 16:05:41 +02:00
|
|
|
SqliteOptions sqliteOptions = options.For<SqliteOptions>();
|
2025-09-03 15:51:25 +02:00
|
|
|
IDbConnection db = factory.CreateDbConnection();
|
|
|
|
|
db.Open();
|
2025-09-04 16:05:41 +02:00
|
|
|
sqliteOptions.OnConnectionCreate?.Invoke(db);
|
2025-09-03 15:51:25 +02:00
|
|
|
return db;
|
|
|
|
|
}
|
|
|
|
|
}
|