Пример #1
0
        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);
        }
Пример #2
0
        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 static void AddRepository(this IServiceCollection services, string conStr)
        {
            var cmdConfig = new SmartSqlConfigOptions()
            {
                Database = new Database()
                {
                    DbProvider = new DbProvider()
                    {
                        Name            = nameof(MySqlClientFactory),
                        ParameterPrefix = "@",
                        Type            = typeof(MySqlClientFactory).AssemblyQualifiedName,
                    },
                    Write = new WriteDataSource()
                    {
                        Name             = "Write-DB",
                        ConnectionString = conStr
                    }
                },
                Settings = new Settings()
                {
                    ParameterPrefix     = "@",
                    IgnoreParameterCase = true,
                    IsWatchConfigFile   = true
                },
                SmartSqlMaps = new List <SmartSqlMapSource>()
                {
                    new SmartSqlMapSource()
                    {
                        Path = "SqlMaps", Type = SmartSqlMapSource.ResourceType.Directory
                    }
                },
                TypeHandlers = new List <TypeHandler>()
                {
                    new TypeHandler()
                    {
                        Handler = new JsonTypeHandler(), Name = "Json", Type = typeof(JsonTypeHandler).AssemblyQualifiedName
                    }
                }
            };

            services.AddSmartSql(sp => new SmartSqlOptions()
            {
                Alias         = "cinema_cmd",
                LoggerFactory = sp.GetService <ILoggerFactory>(),
                ConfigLoader  = new OptionConfigLoader(cmdConfig, sp.GetService <ILoggerFactory>())
            });

            services.AddRepositoryFactory(sqlIdNamingConvert: (type, method) => method.Name.StartsWith("Update") ? "Update" : method.Name);
            services.AddRepositoryFromAssembly(option =>
            {
                option.AssemblyString = "Wizard.Cinema.Domain";
                option.ScopeTemplate  = "I{Scope}Repository";
                option.GetSmartSql    = sp => sp.GetSmartSqlMapper("cinema_cmd");
                option.Filter         = type => type.IsInterface && type.Name.EndsWith("Repository");
            });
        }
Пример #4
0
        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);
        }
Пример #5
0
        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);
        }
        public static void AddQuery(this IServiceCollection services, string conStr)
        {
            var qryConfig = new SmartSqlConfigOptions()
            {
                Database = new Database()
                {
                    DbProvider = new DbProvider()
                    {
                        Name            = nameof(MySqlClientFactory),
                        ParameterPrefix = "@",
                        Type            = typeof(MySqlClientFactory).AssemblyQualifiedName,
                    },
                    Write = new WriteDataSource()
                    {
                        Name             = "Write-DB",
                        ConnectionString = conStr
                    }
                },
                Settings = new Settings()
                {
                    ParameterPrefix     = "@",
                    IgnoreParameterCase = true,
                    IsWatchConfigFile   = true
                },
                SmartSqlMaps = new List <SmartSqlMapSource>()
                {
                    new SmartSqlMapSource()
                    {
                        Path = "SqlMaps", Type = SmartSqlMapSource.ResourceType.Directory
                    }
                },
                TypeHandlers = new List <TypeHandler>()
                {
                    new TypeHandler()
                    {
                        Handler = new JsonTypeHandler(), Name = "Json", Type = typeof(JsonTypeHandler).AssemblyQualifiedName
                    }
                }
            };

            services.AddSmartSql(sp => new SmartSqlOptions()
            {
                Alias         = "cinema_qry",
                LoggerFactory = sp.GetService <ILoggerFactory>(),
                ConfigLoader  = new OptionConfigLoader(qryConfig, sp.GetService <ILoggerFactory>())
            });

            services.AddRepositoryFactory(sqlIdNamingConvert: (type, method) =>
            {
                int index = method.Name.IndexOf("By", StringComparison.Ordinal);
                if (index <= 0)
                {
                    index = method.Name.IndexOf("To", StringComparison.Ordinal);
                }

                return(index <= 0 ? method.Name : method.Name.Substring(0, index));
            });
            services.AddRepositoryFromAssembly(option =>
            {
                option.AssemblyString = "Wizard.Cinema.QueryServices";
                option.GetSmartSql    = sp => sp.GetSmartSqlMapper("cinema_qry");
                option.ScopeTemplate  = "I{Scope}QueryService";
                option.Filter         = type => type.IsInterface && type.Name.EndsWith("QueryService");
            });
        }