Esempio n. 1
0
        public static DbContextOptionsBuilder <T> CreateDbOption <T>(DbInfo dbInfo)
            where T : DbContext
        {
            // 这里是public,没办法防止外面随意调用,所以为了防止初始化后被随意改变,这里搞成单例
            if (optionFactory == null)
            {
                lock (lockRoot)
                {
                    if (optionFactory == null)
                    {
                        switch (dbInfo.DbType)
                        {
                        default:
                        case DbType.MySql:
                            optionFactory = new MySqlDBContextOptionBuilder();
                            break;

                        case DbType.Oracle:
                            optionFactory = new OracleDBContextOptionBuilder();
                            break;

                        case DbType.SqlServer:
                            optionFactory = new SqlServerDBContextOptionBuilder();
                            break;

                        case DbType.InMemory:
                            optionFactory = new InMemoryDBContextOptionBuilder();
                            break;

                        case DbType.PostgreSQL:
                            optionFactory = new PostgresqlDBContextOptionBuilder();
                            break;
                        }
                    }
                }
            }
            DbContextOptionsBuilder <T> options = optionFactory.CreateOptionsBuilder <T>(optionFactory.GetConnectionString(dbInfo));

            return(options);
        }
Esempio n. 2
0
 /// <summary>
 /// 获取数据库连接字符串
 /// </summary>
 /// <param name="dbInfo">dbInfo</param>
 /// <returns>string</returns>
 public string GetConnectionString(DbInfo dbInfo)
 {
     return(MySqlDBContextOptionBuilder.GetDbConnectionString(dbInfo));
 }
Esempio n. 3
0
 /// <summary>
 /// 根据数据类型获取连接字符串
 /// </summary>
 /// <param name="dbInfo">dbInfo</param>
 /// <returns>string</returns>
 public static string GetDbConnectionString(DbInfo dbInfo)
 {
     return($"Data Source={dbInfo.Ip};port={(dbInfo.Port > 0 ? dbInfo.Port : 3306)};Initial Catalog={dbInfo.DbName};user id={dbInfo.UserName};password={dbInfo.Pwd};Character Set=utf8;pooling=true;MaximumPoolsize=1000;");
 }
 /// <summary>
 /// 根据数据类型获取连接字符串
 /// </summary>
 /// <param name="dbInfo">dbInfo</param>
 /// <returns>string</returns>
 public static string GetDbConnectionString(DbInfo dbInfo)
 {
     //return $"DATA SOURCE ={dbInfo.Ip}:{dbInfo.Port}/{dbInfo.DbName}; PASSWORD = {dbInfo.Pwd}; PERSIST SECURITY INFO = True; USER ID = {dbInfo.UserName}";
     return($"Data Source = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = {dbInfo.Ip})(PORT = {(dbInfo.Port>0?dbInfo.Port:1521)}))(CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = {dbInfo.DbName}))); User ID = {dbInfo.UserName}; Password = {dbInfo.Pwd}; Persist Security Info = True");
 }
Esempio n. 5
0
 public GrantEFDbContext(DbContext context, DbInfo dbInfo)
 {
     _dbContext = context;
     _dbInfo    = dbInfo;
 }
Esempio n. 6
0
 public void Dispose()
 {
     dbInfo = null;
 }
Esempio n. 7
0
 public GrantDapperDBContext(DbInfo info)
 {
     dbInfo = info;
     // 这里应该根据工厂生成 mysql或者其他数据库连接实例,
     // _dbConnection = ConnectionManager.CreateConection(connectionString, info.DbType);
 }
Esempio n. 8
0
        private static DbInfo GetDbInfo(RpcContext rpcContext, string dbName)
        {
            try
            {
                // 先找用户私有化的配置
                var sysInfos = rpcContext.GetUserContext()?.GetSysInfo();

                if (sysInfos == null || !sysInfos.Any())
                {
                    if (!string.IsNullOrEmpty(rpcContext.Args.tk))
                    {
                        logger.LogWarning("GrantDBContext.GetDbInfo.GetUserCtx.sysInfos==null");
                    }
                }

                var       dbInfos = sysInfos?.Where(a => a.DbModelName?.ToLower() == dbName).ToList();
                ConstItem dbValue = null;
                if (dbInfos == null || !dbInfos.Any())
                {
                    // 从配置文件做了兼容转换,等数据库tenant_database的DBModelName字段启用后就直接能取到了
                    dbValue = ServerSetting.GetConstValue(dbName);
                    if (dbValue != null)
                    {
                        var sysId = dbValue.Value.ToLower();
                        dbInfos = sysInfos.Where(a => a.SysID?.ToLower() == sysId && !string.IsNullOrEmpty(a.DBIP)).ToList();
                    }
                }
                DbInfo info = null;
                if (dbInfos != null && dbInfos.Any())
                {
                    var    dbInfo = dbInfos.First();
                    var    dbType = dbInfo.DBType;
                    DbType dType  = DbTypeParser.Parser(dbType);
                    info = new DbInfo()
                    {
                        DbType        = dType,
                        DbName        = dbInfo.DBName,
                        Ip            = dbInfo.DBIP,
                        Port          = dbInfo.DbPort,
                        UserName      = dbInfo.DBUser,
                        Pwd           = dbInfo.DBPwd,
                        DbContextName = dbName,
                    };
                }
                else
                {
                    // 如果私有化的表没有,找全局的表
                    var dbModel = ServerSetting.GetDbModelContext(dbName);
                    if (dbModel == null)
                    {
                        throw new Exception($"DataModel Info :{dbName} Is Not Found");
                    }

                    DbType dType = DbTypeParser.Parser(dbModel.DbType);

                    // 需要根据接口配置的主从来选择主从,这里先暂时全部取主,有空了在完善接口主从配置
                    info = new DbInfo()
                    {
                        DbName        = dbModel.Database,
                        DbType        = dType,
                        Ip            = dbModel.Master.Ip,
                        Port          = dbModel.Master.Port,
                        UserName      = dbModel.UserName,
                        Pwd           = dbModel.PassWord,
                        DbContextName = dbName,
                    };
                }
                logger.LogDebug($"获取的数据库信息是:{info?.ToString()}");
                return(info);
            }
            catch (Exception e)
            {
                logger.LogCritical(e, $"GrantDBContext.GetDbInfo.Error,获取数据库{dbName}连接信息异常");
                throw new BusinessException(new StatusCode(StatusCode.ServerError.code, $"无法找到用户的数据库信息,请检查租户数据库,或者Config目录下的数据库配置信息({dbName})"));
            }
        }
Esempio n. 9
0
 /// <summary>
 ///  返回连接字符串
 /// </summary>
 /// <param name="dbInfo">dbInfo</param>
 /// <returns>string</returns>
 public string GetConnectionString(DbInfo dbInfo)
 {
     return(dbInfo.ToString());
 }
 /// <summary>
 /// 根据数据类型获取连接字符串
 /// </summary>
 /// <param name="dbInfo">dbInfo</param>
 /// <returns>string</returns>
 public static string GetDbConnectionString(DbInfo dbInfo)
 {
     return($"Host={dbInfo.Ip}; Port={(dbInfo.Port > 0 ? dbInfo.Port : 5432)}; Database={dbInfo.DbName};User ID={dbInfo.UserName}; Password ={dbInfo.Pwd};Pooling=true;");
 }
Esempio n. 11
0
        private static void GrantDBContext_OnQtDapperCommit(RpcContext rpcContext, GrantDbFactory.GrantDBConnection GrantDBConnection, DbInfo dbInfo, EFEx.GrantDbFactory.SqlPara sqlPara)
        {
            return;

            var user = rpcContext.GetUserContext();

            if (haveAudit(user))
            {
                throw new NotImplementedException();
            }
        }
Esempio n. 12
0
        private static void GrantDBContext_OnQtEFCommit(RpcContext rpcContext, Microsoft.EntityFrameworkCore.DbContext dbContext, DbInfo dbInfo, Z.EntityFramework.Plus.Audit audit)
        {
            var user = rpcContext.GetUserContext();

            if (haveAudit(user))
            {
                if (audit?.Entries == null)
                {
                    return;
                }

                var auditData = new List <AuditData>();
                try
                {
                    audit.Entries = audit.Entries.Where(x =>
                                                        x.State == AuditEntryState.EntityModified ||
                                                        x.State == AuditEntryState.EntityDeleted).ToList();

                    //修正空审计记录
                    if (audit.Entries.Count() == 0)
                    {
                        return;
                    }
                    auditData = AuditTool.AuditConvertToArgs(dbContext.Database, user, audit, dbInfo);
                    auditData?.ForEach(x =>
                    {
                        if (!string.IsNullOrEmpty(rpcContext.Args.rid))
                        {
                            x.TransactionId = rpcContext.Args.rid;
                        }
                    });
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "GrantDBContext_OnQtEFCommit error.");
                }
                AuditTool.SaveAuditData(dbContext.Database, auditData, dbInfo);
            }
        }
Esempio n. 13
0
 /// <summary>
 /// 根据数据类型获取连接字符串
 /// </summary>
 /// <param name="dbInfo">dbInfo</param>
 /// <returns>string</returns>
 public static string GetDbConnectionString(DbInfo dbInfo)
 {
     dbInfo.Port = dbInfo.Port > 0 ? dbInfo.Port : 1433;
     return($"Data Source = {dbInfo.Ip},{dbInfo.Port};Network Library = DBMSSOCN;Initial Catalog = {dbInfo.DbName};User  ID = {dbInfo.UserName};Password = {dbInfo.Pwd};");
 }