/// <summary> /// Initializes a new instance of the <see cref="UmbracoDatabase"/> class. /// </summary> /// <remarks>Internal for unit tests only.</remarks> internal UmbracoDatabase( DbConnection connection, ISqlContext sqlContext, ILogger <UmbracoDatabase> logger, IBulkSqlInsertProvider bulkSqlInsertProvider) : base(connection, sqlContext.DatabaseType, sqlContext.SqlSyntax.DefaultIsolationLevel) { SqlContext = sqlContext; _logger = logger; _bulkSqlInsertProvider = bulkSqlInsertProvider; Init(); }
/// <summary> /// Initializes a new instance of the <see cref="UmbracoDatabase"/> class. /// </summary> /// <remarks> /// <para>Used by UmbracoDatabaseFactory to create databases.</para> /// <para>Also used by DatabaseBuilder for creating databases and installing/upgrading.</para> /// </remarks> public UmbracoDatabase( string connectionString, ISqlContext sqlContext, DbProviderFactory provider, ILogger <UmbracoDatabase> logger, IBulkSqlInsertProvider bulkSqlInsertProvider, DatabaseSchemaCreatorFactory databaseSchemaCreatorFactory, IEnumerable <IMapper> mapperCollection = null) : base(connectionString, sqlContext.DatabaseType, provider, sqlContext.SqlSyntax.DefaultIsolationLevel) { SqlContext = sqlContext; _logger = logger; _bulkSqlInsertProvider = bulkSqlInsertProvider; _databaseSchemaCreatorFactory = databaseSchemaCreatorFactory; _mapperCollection = mapperCollection; Init(); }
private SqlContext Initialize() { _logger.LogDebug("Initializing."); if (ConnectionString.IsNullOrWhiteSpace()) { throw new InvalidOperationException("The factory has not been configured with a proper connection string."); } if (ProviderName.IsNullOrWhiteSpace()) { throw new InvalidOperationException("The factory has not been configured with a proper provider name."); } if (DbProviderFactory == null) { throw new Exception($"Can't find a provider factory for provider name \"{ProviderName}\"."); } _databaseType = DatabaseType.Resolve(DbProviderFactory.GetType().Name, ProviderName); if (_databaseType == null) { throw new Exception($"Can't find an NPoco database type for provider name \"{ProviderName}\"."); } _sqlSyntax = _dbProviderFactoryCreator.GetSqlSyntaxProvider(ProviderName); if (_sqlSyntax == null) { throw new Exception($"Can't find a sql syntax provider for provider name \"{ProviderName}\"."); } _bulkSqlInsertProvider = _dbProviderFactoryCreator.CreateBulkSqlInsertProvider(ProviderName); _databaseType = _sqlSyntax.GetUpdatedDatabaseType(_databaseType, ConnectionString); // ensure we have only 1 set of mappers, and 1 PocoDataFactory, for all database // so that everything NPoco is properly cached for the lifetime of the application _pocoMappers = new NPoco.MapperCollection(); // add all registered mappers for NPoco _pocoMappers.AddRange(_npocoMappers); _pocoMappers.AddRange(_dbProviderFactoryCreator.ProviderSpecificMappers(ProviderName)); var factory = new FluentPocoDataFactory(GetPocoDataFactoryResolver, _pocoMappers); _pocoDataFactory = factory; var config = new FluentConfig(xmappers => factory); // create the database factory _npocoDatabaseFactory = DatabaseFactory.Config(cfg => { cfg.UsingDatabase(CreateDatabaseInstance) // creating UmbracoDatabase instances .WithFluentConfig(config); // with proper configuration foreach (IProviderSpecificInterceptor interceptor in _dbProviderFactoryCreator.GetProviderSpecificInterceptors(ProviderName)) { cfg.WithInterceptor(interceptor); } }); if (_npocoDatabaseFactory == null) { throw new NullReferenceException("The call to UmbracoDatabaseFactory.Config yielded a null UmbracoDatabaseFactory instance."); } _logger.LogDebug("Initialized."); return(new SqlContext(_sqlSyntax, _databaseType, _pocoDataFactory, _mappers)); }