/// <summary> /// 获取根据数据库连接信息对应的数据访问对象。 /// </summary> /// <param name="dbConfiguration">连接信息对象。</param> /// <returns>数据访问对象接口。</returns> public static IGSPDatabase GetDatabase(GSPDbConfigData dbConfiguration) { //DataValidator.CheckForNullReference(dbConfiguration, "数据库配置对象(dbConfiguration)"); if (null == dbConfiguration) { throw new ArgumentNullException("数据库配置对象(dbConfiguration)"); } switch (dbConfiguration.DbType) { case GSPDbType.PostgreSQL: return(new PostgreSQLDatabase(dbConfiguration)); //case GSPDbType.MySQL: // return new MySqlDatabase(dbConfiguration); case GSPDbType.SQLServer: return(new SqlDatabase(dbConfiguration)); //case GSPDbType.Oracle: // //return new OracleDatabase(dbConfiguration); // return new OracleClientDatabase(dbConfiguration); default: return(null); } }
/// <summary> /// 获取环境变量中的数据连接配置 /// </summary> /// <returns></returns> /// <exception cref="">如果环境变量没有配置数据库连接信息,返回null</exception> private static GSPDbConfigData GetEnvDBConfig() { if (ebvDBConfigData != null) { return(ebvDBConfigData); } IConfiguration config = new ConfigurationBuilder() .AddEnvironmentVariables("DBConn_") .Build(); EvnDBConfig dbConfig = new ServiceCollection() .AddOptions() .Configure <EvnDBConfig>(config) .BuildServiceProvider() .GetService <IOptions <EvnDBConfig> >() .Value; //如果没有配置相关环境变量,dbConfig会有默认值 if (dbConfig.ConnectionString == null) { return(null); } switch (dbConfig.DbType) { case GSPDbType.PostgreSQL: ebvDBConfigData = new PostgreSQLConfigData() { DbType = dbConfig.DbType, ConnectionString = dbConfig.ConnectionString }; break; case GSPDbType.MySQL: break; case GSPDbType.SQLServer: break; case GSPDbType.Oracle: break; case GSPDbType.Unknown: break; default: ebvDBConfigData = new PostgreSQLConfigData() { DbType = dbConfig.DbType, ConnectionString = dbConfig.ConnectionString }; break; } ebvDBConfigData = DBConnectionStringBuilder.GetConfigData(ebvDBConfigData); return(ebvDBConfigData); }
/// <summary> /// 获取根据数据源配置对应的数据访问对象。 /// </summary> /// <param name="dataSrcCode"></param> /// <returns>数据访问对象接口。</returns> public static IGSPDatabase GetDatabase(string dataSrcCode) { GSPDbConfigData dbConfiguration = GetConfiguration(dataSrcCode); if (dbConfiguration == null) { throw new ArgumentOutOfRangeException("dataSrcCode", dataSrcCode, string.Format("无法找到编号为[{0}]的数据库配置项,请检查配置是否正确", dataSrcCode)); } return(GetDatabase(dbConfiguration)); }
/// <summary> /// 获取根据配置默认的数据访问对象。由配置文件中的DefaultCode决定。 /// </summary> /// <returns>数据访问对象接口。</returns> public static IGSPDatabase GetDatabase() { GSPDbConfigData defaultDbConfiguration = GetEnvDBConfig(); if (defaultDbConfiguration != null) { return(GetDatabase(defaultDbConfiguration)); } defaultDbConfiguration = GetDefaultConfiguration(); return(GetDatabase(defaultDbConfiguration)); }
/// <summary> /// 获取根据配置默认的数据访问对象。由配置文件中的DefaultCode决定。 /// </summary> /// <returns>数据访问对象接口。</returns> public static IGSPDatabase GetEnvDatabase() { GSPDbConfigData defaultDbConfiguration = GetEnvDBConfig(); return(GetDatabase(defaultDbConfiguration)); }
/// <summary> /// 获取所有已注册的数据库连接配置。 /// </summary> /// <returns>数据库连接配置对象数组。</returns> public static GSPDbConfigData[] GetAllConfigurations() { GSPDbConfigData[] result = new GSPDbConfigData[DbConfigurations.Connections.Count]; DbConfigurations.Connections.CopyTo(result, 0); return(result); }
/// <summary> /// 构造一个database实例对象 /// </summary> /// <param name="dbConfiguration">连接信息</param> public PostgreSQLDatabase(GSPDbConfigData dbConfiguration) : base(dbConfiguration) { }
/// <summary> /// 构造一个database实例对象 /// </summary> /// <param name="dbConfiguration">连接信息</param> public SqlDatabase(GSPDbConfigData dbConfiguration) : base(dbConfiguration) { }
/// <summary> /// 根据数据源信息获取IGSPDatabase接口 /// </summary> /// <param name="dataSource">数据源信息</param> /// <returns>IGSPDatabase接口 data/Source为null返回Null</returns> public static GSPDbConfigData GetConfigData(GSPDbConfigData dataSource) { if (dataSource == null) { return(null); } GSPDbConfigData configdata = null; switch (dataSource.DbType) { case GSPDbType.PostgreSQL: configdata = new PostgreSQLConfigData(); NpgsqlConnectionStringBuilder npgBiulder = new NpgsqlConnectionStringBuilder(dataSource.ConnectionString); //npgBiulder.Password = ProtectePassword.DecryptPasswd(npgBiulder.Password); configdata.ConnectionString = npgBiulder.ConnectionString; configdata.UserId = npgBiulder.Username; configdata.Password = npgBiulder.Password; configdata.Source = npgBiulder.Host; configdata.Catalog = npgBiulder.Database; break; case GSPDbType.MySQL: break; case GSPDbType.SQLServer: //configdata = new Genersoft.Platform.Core.DataAccess.Configuration.SqlDbConfigData(); //SqlConnectionStringBuilder sqlbuilder = new SqlConnectionStringBuilder(dataSource.ConnectionString); //sqlbuilder.Password = ProtectePassword.DecryptPasswd(sqlbuilder.Password); //dataSource.ConnectionString = sqlbuilder.ConnectionString; //configdata.UserId = sqlbuilder.UserID; //configdata.Password = sqlbuilder.Password; //configdata.Source = sqlbuilder.DataSource; //configdata.Catalog = sqlbuilder.InitialCatalog; break; case GSPDbType.Oracle: //configdata = new Genersoft.Platform.Core.DataAccess.Configuration.OracleDbConfigData(); //configdata.DbType = GSPDbType.Oracle; //OracleConnectionStringBuilder orabuilder = new OracleConnectionStringBuilder(dataSource.ConnectionString); //orabuilder.Password = ProtectePassword.DecryptPasswd(orabuilder.Password); //dataSource.ConnectionString = orabuilder.ConnectionString; //configdata.Source = orabuilder.DataSource; //configdata.UserId = orabuilder.UserID; //configdata.Password = orabuilder.Password; break; case GSPDbType.Unknown: break; default: break; } if (configdata != null) { configdata.ConnectionString = dataSource.ConnectionString; configdata.CommandTimeout = 600; } return(configdata); }