123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- using Microsoft.Data.SqlClient;
- using Microsoft.Data.Sqlite;
- using System.Data.Common;
- using System.Diagnostics;
- namespace EVCB_OCPP.DBAPI.ConnectionFactory;
- public class SqliteConnectionFactory<T> : ISqliteConnectionConnectionFactory<T> where T : class
- {
- private readonly ILogger<SqliteConnectionFactory<T>> logger;
- public required string ConnectionString { get; init; }
- public SqliteConnectionFactory(ILogger<SqliteConnectionFactory<T>> logger)
- {
- this.logger = logger;
- }
- public SqliteConnection Create()
- {
- var sqlConnection = new SqliteConnection(ConnectionString);
- sqlConnection.Open();
- return sqlConnection;
- }
- public async Task<SqliteConnection> CreateAsync()
- {
- var timer = Stopwatch.StartNew();
- long t0, t1;
- var connectionStringBuilder = new SqliteConnectionStringBuilder(ConnectionString) {};
- var sqlConnection = new SqliteConnection(connectionStringBuilder.ToString());
- t0 = timer.ElapsedMilliseconds;
- await sqlConnection.OpenAsync();
- t1 = timer.ElapsedMilliseconds;
- timer.Stop();
- if (t1 > 500)
- {
- logger.LogWarning("{type} SqlConnection Open slow {create}/{open}", typeof(T), t0, t1);
- }
- return sqlConnection;
- }
- }
|