| | | 1 | | using Microsoft.Data.Sqlite; |
| | | 2 | | |
| | | 3 | | namespace LOCKnet.Data.Repositories; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Abstrakte Basisklasse für alle SQLite-Repositories. |
| | | 7 | | /// Hält den Connection-String und öffnet bei Bedarf eine neue Verbindung. |
| | | 8 | | /// Jede Methode öffnet eine eigene Verbindung — kein Connection-Pooling. |
| | | 9 | | /// </summary> |
| | | 10 | | public abstract class RepositoryBase |
| | | 11 | | { |
| | | 12 | | private readonly ISqliteConnectionFactory _connectionFactory; |
| | | 13 | | |
| | | 14 | | /// <summary>SQLite-Connection-String fuer diese Repository-Instanz.</summary> |
| | | 15 | | protected readonly string _connectionString; |
| | | 16 | | protected readonly string? _databasePath; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Initialisiert eine neue Instanz von <see cref="RepositoryBase"/>. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="connectionString">Der vollständige SQLite-Connection-String.</param> |
| | | 22 | | protected RepositoryBase(string connectionString) |
| | 125 | 23 | | : this(PlainSqliteConnectionFactory.FromConnectionString(connectionString)) |
| | 125 | 24 | | { |
| | 125 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initialisiert eine neue Instanz von <see cref="RepositoryBase"/> mit einer zentralen Connection-Factory. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="connectionFactory">Factory fuer Storage-spezifische SQLite-Verbindungen.</param> |
| | 322 | 31 | | protected RepositoryBase(ISqliteConnectionFactory connectionFactory) |
| | 322 | 32 | | { |
| | 322 | 33 | | ArgumentNullException.ThrowIfNull(connectionFactory); |
| | | 34 | | |
| | 322 | 35 | | _connectionFactory = connectionFactory; |
| | 322 | 36 | | _connectionString = connectionFactory.Storage.ConnectionString; |
| | 322 | 37 | | _databasePath = connectionFactory.Storage.DatabasePath; |
| | 322 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Öffnet eine neue SQLite-Verbindung und gibt sie zurück. |
| | | 42 | | /// Der Caller ist verantwortlich für das Schließen (via <c>using</c>). |
| | | 43 | | /// </summary> |
| | | 44 | | /// <returns>Eine geöffnete <see cref="Microsoft.Data.Sqlite.SqliteConnection"/>.</returns> |
| | | 45 | | protected SqliteConnection GetConnection() |
| | 657 | 46 | | => _connectionFactory.OpenConnection(); |
| | | 47 | | |
| | | 48 | | internal static void ConfigureConnection(SqliteConnection conn) |
| | 851 | 49 | | { |
| | 851 | 50 | | using var cmd = conn.CreateCommand(); |
| | 851 | 51 | | cmd.CommandText = @" |
| | 851 | 52 | | PRAGMA foreign_keys = ON; |
| | 851 | 53 | | PRAGMA journal_mode = DELETE; |
| | 851 | 54 | | PRAGMA synchronous = FULL; |
| | 851 | 55 | | PRAGMA temp_store = MEMORY; |
| | 851 | 56 | | PRAGMA secure_delete = ON; |
| | 851 | 57 | | PRAGMA busy_timeout = 5000; |
| | 851 | 58 | | PRAGMA locking_mode = NORMAL;"; |
| | 851 | 59 | | cmd.ExecuteNonQuery(); |
| | 1700 | 60 | | } |
| | | 61 | | } |