public static void AddCommonDbContext <TDbContext>(this IServiceCollection services, string connectionString, DatabaseType databaseType)
            where TDbContext : DbContext
        {
            services.AddDbContext <TDbContext>((provider, builder) =>
            {
                // 微软efcore支持的数据库提供程序
                // https://docs.microsoft.com/zh-cn/ef/core/providers/
                switch (databaseType)
                {
                case DatabaseType.MySql:
                    MySqlBuilder.UseMySql(builder, connectionString, mySqlDbContextOptionsBuilder => { });
                    break;

                case DatabaseType.PostgreSql:
                    PostgreSqlBuilder.UsePostgreSql(builder, connectionString, npgsqlDbContextOptionsBuilder => { });
                    break;

                case DatabaseType.SqlLite:
                    SqlLiteBuilder.UseSqlLite(builder, connectionString, sqliteDbContextOptionsBuilder => { });
                    break;

                case DatabaseType.SqlServer:
                    SqlServerBuilder.UseSqlServer(builder, connectionString, sqlServerDbContextOptionsBuilder => { });
                    break;
                }
            });
        }
        private ISqlStatements ConstructSqlStatements(EntityMapping entityMapping)
        {
            entityMapping.FreezeMapping();

            ISqlStatements       sqlStatements;
            IStatementSqlBuilder statementSqlBuilder;

            switch (entityMapping.Dialect)
            {
            case SqlDialect.MsSql:
                statementSqlBuilder = new MsSqlBuilder(this, entityMapping);
                break;

            case SqlDialect.MySql:
                statementSqlBuilder = new MySqlBuilder(this, entityMapping);
                break;

            case SqlDialect.PostgreSql:
                statementSqlBuilder = new PostgreSqlBuilder(this, entityMapping);
                break;

            case SqlDialect.SqLite:
                statementSqlBuilder = new SqLiteBuilder(this, entityMapping);
                break;

            default:
                throw new NotSupportedException($"Dialect {entityMapping.Dialect} is not supported");
            }

            sqlStatements = new GenericSqlStatements <TEntity>(statementSqlBuilder);
            return(sqlStatements);
        }
Esempio n. 3
0
        public string ToSql(IExpr expr)
        {
            var sqlExporter = new MySqlBuilder(this._builderOptions);

            if (expr.Accept(sqlExporter, null))
            {
                return(sqlExporter.ToString());
            }

            throw new SqExpressException("Could not build Sql");
        }
        public void Can_GetData_WithLimitAndOffset()
        {
            var builder = new MySqlBuilder();
            var query   = builder.SelectDmlPageBuilder($"Select * from ex8_db1.ATeam", "Id", 50000, 9);

            var target    = new MySqlReader();
            var dataTable = target.GetData(connectionString, query);

            dataTable.Tables.Should().NotBeEmpty();
            dataTable.Tables[0].Rows.Count.Should().Equals(50000);
            dataTable.Tables[0].Rows.Should().NotBeEmpty();
        }
Esempio n. 5
0
        /// <summary>
        /// Returns the sql statements for an entity mapping, or the default one if the argument is null.
        /// </summary>
        public ISqlStatements GetSqlStatements(EntityMapping entityMapping = null)
        {
            entityMapping = entityMapping ?? DefaultEntityMapping;

            entityMapping.FreezeMapping();

            ISqlStatements       sqlStatements;
            IStatementSqlBuilder statementSqlBuilder;

            var originalRegisteredEntityMappings = _registeredEntityMappings;

            if (!originalRegisteredEntityMappings.TryGetValue(entityMapping, out sqlStatements))
            {
                switch (entityMapping.Dialect)
                {
                case SqlDialect.MsSql:
                    statementSqlBuilder = new MsSqlBuilder(this, entityMapping);
                    break;

                case SqlDialect.MySql:
                    statementSqlBuilder = new MySqlBuilder(this, entityMapping);
                    break;

                case SqlDialect.PostgreSql:
                    statementSqlBuilder = new PostgreSqlBuilder(this, entityMapping);
                    break;

                case SqlDialect.SqLite:
                    statementSqlBuilder = new SqLiteBuilder(this, entityMapping);
                    break;

                default:
                    throw new NotSupportedException($"Dialect {entityMapping.Dialect} is not supported");
                }

                sqlStatements = new GenericSqlStatements <TEntity>(statementSqlBuilder);

                // replace the original collection
                // rather than using a lock, we prefer the risk of missing a registration or two as they will get captured eventually
                _registeredEntityMappings = new Dictionary <EntityMapping, ISqlStatements>(originalRegisteredEntityMappings)
                {
                    [entityMapping] = sqlStatements
                };
            }
            return(sqlStatements);
        }
        public void Can_BuildTargetSql()
        {
            var input  = GetJsonFile <DatabaseJobManifest>(_inputRoot, "database.manifest.mysql.json");
            var tables = input.manifest.tables;
            var expectedResultFiles = new List <string> {
                "mysql.target.ateam.json"
            };

            var target = new MySqlBuilder();

            for (int i = 0; i < tables.Length; i++)
            {
                var result         = target.BuildTargetSql(tables[i]);
                var expectedResult = GetJsonFile <TargetSql>(_outputRoot, expectedResultFiles[i]);
                result.Should().BeEquivalentTo(expectedResult);
            }
        }
        /// <summary>
        /// 添加认证数据库上下文服务
        /// </summary>
        /// <typeparam name="T">用户和角色实体的主键类型</typeparam>
        /// <param name="services">服务集</param>
        /// <param name="connectionString">数据库连接串</param>
        /// <param name="databaseType">数据库类型</param>
        /// <param name="setupAction">用户认证选项</param>
        public static void AddIdentityDbContext <TDbContext, TUser, TRole, Tkey>(this IServiceCollection services, string connectionString, DatabaseType databaseType,
                                                                                 Action <IdentityOptions> setupAction = null)
            where TDbContext : IdentityDbContext <TUser, TRole, Tkey>
            where TUser : IdentityUser <Tkey>
            where TRole : IdentityRole <Tkey>
            where Tkey : IEquatable <Tkey>
        {
            services.AddDbContext <TDbContext>((provider, builder) =>
            {
                // 微软efcore支持的数据库提供程序
                // https://docs.microsoft.com/zh-cn/ef/core/providers/
                switch (databaseType)
                {
                case DatabaseType.MySql:
                    MySqlBuilder.UseMySql(builder, connectionString, mySqlDbContextOptionsBuilder => { });
                    break;

                case DatabaseType.PostgreSql:
                    PostgreSqlBuilder.UsePostgreSql(builder, connectionString, npgsqlDbContextOptionsBuilder => { });
                    break;

                case DatabaseType.SqlLite:
                    SqlLiteBuilder.UseSqlLite(builder, connectionString, sqliteDbContextOptionsBuilder => { });
                    break;

                case DatabaseType.SqlServer:
                    SqlServerBuilder.UseSqlServer(builder, connectionString, sqlServerDbContextOptionsBuilder => { });
                    break;
                }
            })
            .AddIdentity <TUser, TRole>(setupAction) // 使用user和role 进行认证
            .AddEntityFrameworkStores <TDbContext>() // 使用默认的EF的Store
            .AddDefaultTokenProviders();             // 添加默认token生成工具,用其生成的token用来进行密码重置。

            //services.AddTransient<IUserStore<BaseIdentityUser>, BaseIdentityUserStore>(); // 使用自定义userstore
            //services.AddTransient<IRoleStore<BaseIdentityRole>, BaseIdentityRoleStore>(); // 使用自定义rolestore

            services.AddApiAuthorization();                                         // 添加api认证Handler

            services.AddScoped <IPermissionCacheService, PermissionCacheService>(); // 权限缓存服务

            services.AddRepositories <TDbContext>();                                // 批量注入数据仓储

            services.AddScoped <BaseIdentityUnitOfWork>();
        }
Esempio n. 8
0
 public MySqlBuilderTest(ITestOutputHelper output)
 {
     _output  = output;
     _builder = new MySqlBuilder();
 }
 public MySqlStatementBuilder(SqlBuilderOptions?options) : base(options)
 {
     this._exprBuilder = new MySqlBuilder(this.Options, this.Builder);
 }
Esempio n. 10
0
 /// <summary>
 /// Gets all the related columns
 /// </summary>
 /// <returns>Array of things</returns>
 public override T[] All()
 {
     return(EnumerableFor(MySqlBuilder.BuildSelectRelated(RelationalColumn, _parentThing.Id)).ToArray());
 }
Esempio n. 11
0
 public MySqlStatementBuilder(SqlBuilderOptions?options, StringBuilder?externalBuilder) : base(options, externalBuilder)
 {
     this._exprBuilder = new MySqlBuilder(this.Options, this.Builder);
 }