public static ISqlMapper Create(CreateSmartSqlMapperOptions options) { SmartSqlConfigOptions smartSqlConfigOptions = new SmartSqlConfigOptions { Settings = new SmartSql.Configuration.Settings { ParameterPrefix = "$", IgnoreParameterCase = true, IsCacheEnabled = false, }, Database = new Database { DbProvider = new SmartSql.DataSource.DbProvider { Name = options.ProviderName }, Write = options.DataSource, Reads = new List <DataSource>() }, SmartSqlMaps = new List <SqlMapSource>(), TypeHandlers = new List <TypeHandler> { new TypeHandler { Name = "Json", Type = "SmartSql.TypeHandler.JsonTypeHandler,SmartSql.TypeHandler" }, new TypeHandler { Name = "PGJson", Type = "SmartSql.TypeHandler.PostgreSql.JsonTypeHandler,SmartSql.TypeHandler.PostgreSql" }, new TypeHandler { Name = "PGJsonb", Type = "SmartSql.TypeHandler.PostgreSql.JsonTypeHandler,SmartSql.TypeHandler.PostgreSql", Properties = new Dictionary <string, object> { { "DataTypeName", "jsonb" } } } } }; if (!String.IsNullOrEmpty(options.SqlMapPath)) { smartSqlConfigOptions.SmartSqlMaps.Add(new SqlMapSource { Path = options.SqlMapPath, Type = ResourceType.Directory }); } var optionConfigBuilder = new OptionConfigBuilder(smartSqlConfigOptions, options.LoggerFactory); return(new SmartSqlBuilder() .UseLoggerFactory(options.LoggerFactory) .UseConfigBuilder(optionConfigBuilder) .UseAlias(options.Alias) .Build().SqlMapper); }
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)); }
public async Task InitData() { var dataSource = _project.DataSource; dataSource.Paramters.EnsureValue("DbProvider", out string dbProvider); dataSource.Paramters.EnsureValue("ConnectionString", out string connString); dataSource.Paramters.Value("PKColumn", out string pkColumn); dataSource.Paramters.Value("ModifyTime", out string modifyTime); dataSource.Paramters.EnsureValue("Query", out string queryCmd); #region CreateSqlMapper var smartSqlOptions = new CreateSmartSqlMapperOptions { LoggerFactory = _loggerFactory, ProviderName = dbProvider, Alias = Name, DataSource = new SmartSql.Configuration.WriteDataSource { Name = Name, ConnectionString = connString } }; var sqlMapper = Create(smartSqlOptions); #endregion var lastExtract = await _etlRepository.GetLastExtract(_project.GetETLCode()); _project.SetETLLastExtract(lastExtract); var queryParams = new Dictionary <string, object> { { "LastMaxId", lastExtract.MaxId }, { "LastQueryTime", lastExtract.QueryTime }, { "LastMaxModifyTime", lastExtract.MaxModifyTime }, }; var extractEntity = new ETLExtract { QueryTime = DateTime.Now, PKColumn = pkColumn, QueryCommand = new ETLDbCommand { Command = queryCmd, Paramters = queryParams }, QuerySize = -1, MaxId = -1 }; Stopwatch stopwatch = Stopwatch.StartNew(); DbSet = await sqlMapper.GetDbSetAsync(new RequestContext { RealSql = queryCmd, Request = queryParams }); TransformData = DbSet.Tables.First(); stopwatch.Stop(); extractEntity.QuerySize = TransformData.Rows.Count; extractEntity.QueryCommand.Taken = stopwatch.ElapsedMilliseconds; _logger.LogWarning($"InitData,Data.Size:{extractEntity.QuerySize},Taken:{extractEntity.QueryCommand.Taken}ms!"); dataSource.Paramters.Value("PkIsNumeric", out bool pkIsNumeric); dataSource.Paramters.Value("AutoIncrement", out bool autoIncrement); if (!String.IsNullOrEmpty(pkColumn) && (pkIsNumeric || autoIncrement) && extractEntity.QuerySize > 0) { var maxId = TransformData.Rows.Max(m => m.Cells[pkColumn].Value); extractEntity.MaxId = Convert.ToInt64(maxId); } else { extractEntity.MaxId = lastExtract.MaxId; } if (!String.IsNullOrEmpty(modifyTime) && extractEntity.QuerySize > 0) { var maxModifyTime = TransformData.Rows.Max(m => m.Cells[modifyTime].Value); extractEntity.MaxModifyTime = Convert.ToDateTime(maxModifyTime); } else { extractEntity.MaxModifyTime = lastExtract.MaxModifyTime; } await _etlRepository.Extract(_project.GetETKTaskId(), extractEntity); }