public static ISmartSqlMapper Create(CreateSmartSqlMapperOptions options) { var smartSqlDbProvider = DbProviders.GetDbProvider(options.ProviderName); SmartSqlConfigOptions smartSqlConfigOptions = new SmartSqlConfigOptions { Settings = new SmartSql.Configuration.Settings { ParameterPrefix = "$", IgnoreParameterCase = true, IsWatchConfigFile = false, IsCacheEnabled = false, }, Database = new Database { DbProvider = smartSqlDbProvider, Write = options.DataSource, Read = new List <SmartSql.Configuration.ReadDataSource>() }, SmartSqlMaps = new List <SmartSql.Configuration.SmartSqlMapSource>(), TypeHandlers = new List <SmartSql.Configuration.TypeHandler> { new SmartSql.Configuration.TypeHandler { Name = "Json", Type = "SmartSql.TypeHandler.JsonTypeHandler,SmartSql.TypeHandler" }, new SmartSql.Configuration.TypeHandler { Name = "PGJson", Type = "SmartSql.TypeHandler.PostgreSql.JsonTypeHandler,SmartSql.TypeHandler.PostgreSql" }, new SmartSql.Configuration.TypeHandler { Name = "PGJsonb", Type = "SmartSql.TypeHandler.PostgreSql.JsonbTypeHandler,SmartSql.TypeHandler.PostgreSql" }, new SmartSql.Configuration.TypeHandler { Name = "OracleBoolean", Type = "SmartSql.TypeHandler.Oracle.BooleanTypeHandler,SmartSql.TypeHandler.Oracle" } } }; if (!String.IsNullOrEmpty(options.SqlMapPath)) { smartSqlConfigOptions.SmartSqlMaps.Add(new SmartSql.Configuration.SmartSqlMapSource { Path = options.SqlMapPath, Type = SmartSql.Configuration.SmartSqlMapSource.ResourceType.Directory }); } var _configLoader = new OptionConfigLoader(smartSqlConfigOptions, options.LoggerFactory); var smartsqlOptions = new SmartSqlOptions { Alias = options.Alias, ConfigPath = options.Alias, ConfigLoader = _configLoader, LoggerFactory = options.LoggerFactory }; return(MapperContainer.Instance.GetSqlMapper(smartsqlOptions)); }
private void InitSqlMapper() { if (!_dbProviders.TryGetValue(DbProviderName, out SmartSql.Configuration.DbProvider smartSqlDbProvider)) { var supportDbProviders = String.Join(",", _dbProviders.Select(m => m.Key)); var errMsg = $"Can not find DbProvider:{DbProviderName},SmartCode support DbProviders:{supportDbProviders}!"; _logger.LogError(errMsg); throw new SmartCodeException(errMsg); } SmartSqlConfigOptions smartSqlConfigOptions = new SmartSqlConfigOptions { Settings = new SmartSql.Configuration.Settings { ParameterPrefix = "$" }, Database = new Database { DbProvider = smartSqlDbProvider, Write = new SmartSql.Configuration.WriteDataSource { Name = DbName, ConnectionString = ConnectionString }, Read = new List <SmartSql.Configuration.ReadDataSource>() }, SmartSqlMaps = new List <SmartSql.Configuration.SmartSqlMapSource> { new SmartSql.Configuration.SmartSqlMapSource { Path = "Maps", Type = SmartSql.Configuration.SmartSqlMapSource.ResourceType.Directory } }, TypeHandlers = new List <SmartSql.Configuration.TypeHandler>() }; var _configLoader = new OptionConfigLoader(smartSqlConfigOptions, _loggerFactory); var smartsqlOptions = new SmartSqlOptions { ConfigPath = "SmartSql", ConfigLoader = _configLoader }; SqlMapper = MapperContainer.Instance.GetSqlMapper(smartsqlOptions); }
public static void AddSmartSqlOptionLoader(this IServiceCollection services) { services.AddSingleton <IConfigLoader>((sp) => { var loggerFactory = sp.GetService <ILoggerFactory>() ?? NoneLoggerFactory.Instance; var optionsMonitor = sp.GetService <IOptionsMonitor <SmartSqlConfigOptions> >(); var _configLoader = new OptionConfigLoader(optionsMonitor.CurrentValue, loggerFactory); SmartSqlOptions smartSqlOptions = new SmartSqlOptions { ConfigLoader = _configLoader, LoggerFactory = loggerFactory }; if (optionsMonitor.CurrentValue.Settings.IsWatchConfigFile) { optionsMonitor.OnChange((ops, name) => { _configLoader.TriggerChanged(ops); }); } return(_configLoader); }); }
private void InitSqlMapper() { _dbProviders.TryGetValue(DbProviderName, out SmartSql.Configuration.DbProvider smartSqlDbProvider); SmartSqlConfigOptions smartSqlConfigOptions = new SmartSqlConfigOptions { Settings = new SmartSql.Configuration.Settings { ParameterPrefix = "$" }, Database = new SmartSql.Options.Database { DbProvider = smartSqlDbProvider, Write = new SmartSql.Configuration.WriteDataSource { Name = DbName, ConnectionString = ConnectionString }, Read = new List <SmartSql.Configuration.ReadDataSource>() }, SmartSqlMaps = new List <SmartSql.Configuration.SmartSqlMapSource> { new SmartSql.Configuration.SmartSqlMapSource { Path = "Maps", Type = SmartSql.Configuration.SmartSqlMapSource.ResourceType.Directory } }, TypeHandlers = new List <SmartSql.Configuration.TypeHandler>() }; var _configLoader = new OptionConfigLoader(smartSqlConfigOptions, _loggerFactory); var smartsqlOptions = new SmartSqlOptions { ConfigPath = "SmartSql", ConfigLoader = _configLoader }; SqlMapper = MapperContainer.Instance.GetSqlMapper(smartsqlOptions); }
private static IConfigLoader BuildConfigLoader(IServiceProvider sp, string configName) { var loggerFactory = sp.GetService <ILoggerFactory>() ?? NoneLoggerFactory.Instance; var optionsMonitor = sp.GetService <IOptionsMonitor <SmartSqlConfigOptions> >(); var smartSqlOptions = String.IsNullOrEmpty(configName) ? optionsMonitor.CurrentValue : optionsMonitor.Get(configName); var _configLoader = new OptionConfigLoader(smartSqlOptions, loggerFactory); if (smartSqlOptions.Settings.IsWatchConfigFile) { optionsMonitor.OnChange((ops, name) => { if (name == configName) { return; } _configLoader.TriggerChanged(ops); }); } return(_configLoader); }