/// <summary> /// 动态加载数据库工厂 /// </summary> /// <param name="providerInvariantName"></param> /// <param name="dbType"></param> /// <returns></returns> public static DbProviderFactory GetFactory(ConfigModel config) { try { if (DbCache.Exists(CacheType.Web, config.ProviderName)) { return(DbCache.Get <object>(CacheType.Web, config.ProviderName) as DbProviderFactory); } else { var assembly = AppDomain.CurrentDomain.GetAssemblies().ToList().Find(a => a.FullName.Split(',')[0] == config.ProviderName); if (assembly == null) { assembly = Assembly.Load(config.ProviderName); } var type = assembly.GetType(config.FactoryClient, false); object instance = null; if (type != null) { instance = Activator.CreateInstance(type); } DbCache.Set <object>(CacheType.Web, config.ProviderName, instance); return(instance as DbProviderFactory); } } catch (Exception ex) { DbLog.LogException(config.IsOutError, config.DbType, ex, "GetFactory", ""); return(null); } }
/// <summary> /// foreach数据 /// </summary> /// <param name="name"></param> /// <param name="db"></param> /// <param name="key"></param> /// <returns></returns> public static List <Dictionary <string, object> > MapForEach(List <Dictionary <string, object> > data, string name, DataContext db, string key, ConfigModel config, int i = 1) { var result = new List <Dictionary <string, object> >(); var param = new List <DbParameter>(); var dicName = DbCache.Get(config.CacheType, string.Format("{0}.foreach.name.{1}", name.ToLower(), i)); var field = DbCache.Get(config.CacheType, string.Format("{0}.foreach.field.{1}", name.ToLower(), i)); var sql = DbCache.Get(config.CacheType, string.Format("{0}.foreach.sql.{1}", name.ToLower(), i)); data.ForEach(a => { param.Clear(); if (field.IndexOf(',') > 0) { foreach (var split in field.Split(',')) { var tempParam = DbProviderFactories.GetFactory(config).CreateParameter(); tempParam.ParameterName = split; tempParam.Value = a.GetValue(split); param.Add(tempParam); } } else { var tempParam = DbProviderFactories.GetFactory(config).CreateParameter(); tempParam.ParameterName = field; tempParam.Value = a.GetValue(field); param.Add(tempParam); } a.Add(dicName, FastRead.ExecuteSql(sql, param.ToArray(), db, key)); result.Add(a); }); return(result); }
public static bool DataType(string dbFile = "db.json") { var list = new List <ConfigModel>(); var cacheKey = "FastData.Core.Config"; if (DbCache.Exists(CacheType.Web, cacheKey)) { list = DbCache.Get <List <ConfigModel> >(CacheType.Web, cacheKey); } else { list = BaseConfig.GetListValue <ConfigModel>(AppSettingKey.Config, dbFile); DbCache.Set <List <ConfigModel> >(CacheType.Web, cacheKey, list); } var result = new List <bool>(); result.Add(list.Count(a => a.DbType.ToLower() == DataDbType.Oracle) > 0); result.Add(list.Count(a => a.DbType.ToLower() == DataDbType.DB2) > 0); result.Add(list.Count(a => a.DbType.ToLower() == DataDbType.MySql) > 0); result.Add(list.Count(a => a.DbType.ToLower() == DataDbType.PostgreSql) > 0); result.Add(list.Count(a => a.DbType.ToLower() == DataDbType.SQLite) > 0); result.Add(list.Count(a => a.DbType.ToLower() == DataDbType.SqlServer) > 0); return(result.Count(a => a == true) > 1); }
/// <summary> /// 读取xml map并缓存 /// </summary> public static List <string> ReadXml(string path, ConfigModel config, string fileName, string xml = null) { var map = DbCache.Get <Dictionary <string, object> >(DataConfig.Get().CacheType, "FastMap.Api") ?? new Dictionary <string, object>(); var key = new List <string>(); var sql = new List <string>(); var db = new Dictionary <string, object>(); var type = new Dictionary <string, object>(); var param = new Dictionary <string, object>(); var check = new Dictionary <string, object>(); var name = new Dictionary <string, object>(); var parameName = new Dictionary <string, object>(); GetXmlList(path, "sqlMap", ref key, ref sql, ref db, ref type, ref check, ref param, ref name, ref parameName, config, xml); for (var i = 0; i < key.Count; i++) { DbCache.Set(config.CacheType, key[i].ToLower(), sql[i]); } var apilist = new List <string>(); db.ToList().ForEach(a => { DbCache.Set(config.CacheType, string.Format("{0}.db", a.Key.ToLower()), a.Value.ToStr()); apilist.Add(a.Key.ToLower()); }); map.Remove(fileName); map.Add(fileName, apilist); DbCache.Set <Dictionary <string, object> >(config.CacheType, "FastMap.Api", map); type.ToList().ForEach(a => { DbCache.Set(config.CacheType, string.Format("{0}.type", a.Key.ToLower()), a.Value.ToStr()); key.Add(string.Format("{0}.type", a.Key.ToLower())); }); param.ToList().ForEach(a => { DbCache.Set <List <string> >(config.CacheType, string.Format("{0}.param", a.Key.ToLower()), a.Value as List <string>); key.Add(string.Format("{0}.param", a.Key.ToLower())); }); check.ToList().ForEach(a => { DbCache.Set(config.CacheType, a.Key, a.Value.ToStr()); key.Add(a.Key); }); name.ToList().ForEach(a => { DbCache.Set(config.CacheType, a.Key, a.Value.ToStr()); key.Add(a.Key); }); parameName.ToList().ForEach(a => { DbCache.Set(config.CacheType, a.Key, a.Value.ToStr()); key.Add(a.Key); }); return(key); }
/// <summary> /// 是否foreach /// </summary> /// <param name="name"></param> /// <returns></returns> public static bool MapIsForEach(string name, ConfigModel config, int i = 1) { var keyName = string.Format("{0}.foreach.name.{1}", name.ToLower(), i); var keyField = string.Format("{0}.foreach.field.{1}", name.ToLower(), i); var keySql = string.Format("{0}.foreach.sql.{1}", name.ToLower(), i); return(!string.IsNullOrEmpty(DbCache.Get(config.CacheType, keyName)) && !string.IsNullOrEmpty(DbCache.Get(config.CacheType, keyField)) && !string.IsNullOrEmpty(DbCache.Get(config.CacheType, keySql))); }
public static bool DataType(string key = null, string projectName = null, string dbFile = "db.json") { var cacheKey = "FastData.Core.Config"; if (!DbCache.Exists(CacheType.Web, cacheKey)) { DataConfig.Get(key, projectName, dbFile); } var list = DbCache.Get <List <ConfigModel> >(CacheType.Web, cacheKey); var result = new List <bool>(); result.Add(list.Count(a => a.DbType.ToLower() == DataDbType.Oracle) > 0); result.Add(list.Count(a => a.DbType.ToLower() == DataDbType.DB2) > 0); result.Add(list.Count(a => a.DbType.ToLower() == DataDbType.MySql) > 0); result.Add(list.Count(a => a.DbType.ToLower() == DataDbType.PostgreSql) > 0); result.Add(list.Count(a => a.DbType.ToLower() == DataDbType.SQLite) > 0); result.Add(list.Count(a => a.DbType.ToLower() == DataDbType.SqlServer) > 0); return(result.Count(a => a == true) > 1); }
/// <summary> /// 获取配置实体 /// </summary> /// <param name="key"></param> /// <returns></returns> public static ConfigModel Get(string key = null, string projectName = null, string dbFile = "db.json") { var list = new List <ConfigModel>(); var item = new ConfigModel(); var cacheKey = "FastData.Core.Config"; if (DbCache.Exists(CacheType.Web, cacheKey)) { list = DbCache.Get <List <ConfigModel> >(CacheType.Web, cacheKey); } else if (projectName != null) { var assembly = Assembly.Load(projectName); using (var resource = assembly.GetManifestResourceStream(string.Format("{0}.{1}", projectName, dbFile))) { if (resource != null) { using (var reader = new StreamReader(resource)) { var content = reader.ReadToEnd(); list = BaseJson.JsonToList <ConfigModel>(BaseJson.ModelToJson(BaseJson.JsonToDic(content).GetValue("DataConfig"))); list.ForEach(a => { a.IsUpdateCache = false; }); DbCache.Set <List <ConfigModel> >(CacheType.Web, cacheKey, list); } } else { list = BaseConfig.GetListValue <ConfigModel>(AppSettingKey.Config, dbFile); DbCache.Set <List <ConfigModel> >(CacheType.Web, cacheKey, list); } } } else { list = BaseConfig.GetListValue <ConfigModel>(AppSettingKey.Config, dbFile); DbCache.Set <List <ConfigModel> >(CacheType.Web, cacheKey, list); } if (string.IsNullOrEmpty(key)) { item = list[0]; } else { item = list.Find(a => a.Key.ToLower() == key.ToLower()); } if (item.DesignModel == "") { item.DesignModel = Config.DbFirst; } if (item.SqlErrorType == "") { item.SqlErrorType = SqlErrorType.File; } if (item.CacheType == "") { item.CacheType = CacheType.Web; } item.IsPropertyCache = true; item.DbType = item.DbType.ToLower(); if (projectName != null) { item.IsUpdateCache = false; } return(item); }
/// <summary> /// foreach数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <param name="name"></param> /// <param name="db"></param> /// <param name="config"></param> /// <returns></returns> public static List <T> MapForEach <T>(List <T> data, string name, DataContext db, ConfigModel config, int i = 1) where T : class, new() { var result = new List <T>(); var param = new List <DbParameter>(); var dicName = DbCache.Get(config.CacheType, string.Format("{0}.foreach.name.{1}", name.ToLower(), i)); var type = DbCache.Get(config.CacheType, string.Format("{0}.foreach.type.{1}", name.ToLower(), i)); var field = DbCache.Get(config.CacheType, string.Format("{0}.foreach.field.{1}", name.ToLower(), i)); var sql = DbCache.Get(config.CacheType, string.Format("{0}.foreach.sql.{1}", name.ToLower(), i)); Assembly assembly; if (type.IndexOf(',') > 0) { var key = string.Format("ForEach.{0}", type.Split(',')[1]); if (DbCache.Exists(CacheType.Web, key)) { assembly = DbCache.Get <object>(CacheType.Web, key) as Assembly; } else { assembly = AppDomain.CurrentDomain.GetAssemblies().ToList().Find(a => a.FullName.Split(',')[0] == type.Split(',')[1]); } if (assembly == null) { assembly = Assembly.Load(type.Split(',')[1]); } if (assembly == null) { return(data); } else { DbCache.Set <object>(CacheType.Web, key, assembly); if (assembly.GetType(type.Split(',')[0]) == null) { return(data); } foreach (var item in data) { var model = Activator.CreateInstance(assembly.GetType(type.Split(',')[0])); var list = Activator.CreateInstance(typeof(List <>).MakeGenericType(assembly.GetType(type.Split(',')[0]))); var infoResult = BaseDic.PropertyInfo <T>().Find(a => a.PropertyType.FullName == list.GetType().FullName); var dynSet = new DynamicSet(model); //param param.Clear(); if (field.IndexOf(',') > 0) { foreach (var split in field.Split(',')) { var infoField = BaseDic.PropertyInfo <T>().Find(a => a.Name.ToLower() == split); var tempParam = DbProviderFactories.GetFactory(config).CreateParameter(); tempParam.ParameterName = split; tempParam.Value = infoField.GetValue(item, null); param.Add(tempParam); } } else { var infoField = BaseDic.PropertyInfo <T>().Find(a => a.Name.ToLower() == field); var tempParam = DbProviderFactories.GetFactory(config).CreateParameter(); tempParam.ParameterName = field; tempParam.Value = infoField.GetValue(item, null); param.Add(tempParam); } var tempData = db.ExecuteSql(sql, param.ToArray(), false); foreach (var temp in tempData.DicList) { foreach (var info in model.GetType().GetProperties()) { if (temp.GetValue(info.Name).ToStr() == "" && info.PropertyType.Name == "Nullable`1") { continue; } if (info.PropertyType.Name == "Nullable`1" && info.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { dynSet.SetValue(model, info.Name, Convert.ChangeType(temp.GetValue(info.Name), Nullable.GetUnderlyingType(info.PropertyType)), config.IsPropertyCache); } else { dynSet.SetValue(model, info.Name, Convert.ChangeType(temp.GetValue(info.Name), info.PropertyType), config.IsPropertyCache); } } var method = list.GetType().GetMethod("Add", BindingFlags.Instance | BindingFlags.Public); method.Invoke(list, new object[] { model }); } infoResult.SetValue(item, list); result.Add(item); } return(result); } } else { return(data); } }
/// <summary> /// 获取map sql语句 /// </summary> /// <param name="name"></param> /// <param name="param"></param> /// <returns></returns> public static string GetMapSql(string name, ref DbParameter[] param, DataContext db, string key) { var tempParam = new List <DbParameter>(); var sql = new StringBuilder(); var flag = ""; var cacheType = ""; if (db != null) { flag = db.config.Flag; cacheType = db.config.CacheType; } else if (key != null) { flag = DataConfig.Get(key).Flag; cacheType = DataConfig.Get(key).CacheType; } for (var i = 0; i <= DbCache.Get(cacheType, name.ToLower()).ToInt(0); i++) { #region 文本 var txtKey = string.Format("{0}.{1}", name.ToLower(), i); if (DbCache.Exists(cacheType, txtKey)) { sql.Append(DbCache.Get(cacheType, txtKey)); } #endregion #region 动态 var dynKey = string.Format("{0}.format.{1}", name.ToLower(), i); if (DbCache.Exists(cacheType, dynKey)) { if (param != null) { var tempSql = new StringBuilder(); foreach (var item in DbCache.Get <List <string> >(DataConfig.Get().CacheType, string.Format("{0}.param", name.ToLower()))) { if (!param.ToList().Exists(a => a.ParameterName.ToLower() == item.ToLower())) { continue; } var temp = param.ToList().Find(a => a.ParameterName.ToLower() == item.ToLower()); if (!tempParam.ToList().Exists(a => a.ParameterName == temp.ParameterName)) { tempParam.Add(temp); } var paramKey = string.Format("{0}.{1}.{2}", name.ToLower(), temp.ParameterName.ToLower(), i); var conditionKey = string.Format("{0}.{1}.condition.{2}", name.ToLower(), temp.ParameterName.ToLower(), i); var conditionValueKey = string.Format("{0}.{1}.condition.value.{2}", name.ToLower(), temp.ParameterName.ToLower(), i); if (DbCache.Exists(cacheType, paramKey)) { var flagParam = string.Format("{0}{1}", flag, temp.ParameterName).ToLower(); var tempKey = string.Format("#{0}#", temp.ParameterName).ToLower(); var paramSql = DbCache.Get(cacheType, paramKey.ToLower()).ToLower(); var condition = DbCache.Get(cacheType, conditionKey).ToStr().ToLower(); var conditionValue = DbCache.Get(cacheType, conditionValueKey).ToStr().ToLower(); switch (condition) { case "isequal": { if (conditionValue == temp.Value.ToStr()) { if (paramSql.IndexOf(tempKey) >= 0) { tempParam.Remove(temp); tempSql.Append(paramSql.ToString().Replace(tempKey, temp.Value.ToStr())); } else if (paramSql.IndexOf(flagParam) < 0 && flag != "") { tempParam.Remove(temp); tempSql.Append(DbCache.Get(cacheType, paramKey)); } else { tempSql.Append(DbCache.Get(cacheType, paramKey)); } } else { tempParam.Remove(temp); } break; } case "isnotequal": { if (conditionValue != temp.Value.ToStr()) { if (paramSql.IndexOf(tempKey) >= 0) { tempParam.Remove(temp); tempSql.Append(paramSql.ToString().Replace(temp.ParameterName.ToLower(), temp.Value.ToStr())); } else if (paramSql.IndexOf(flagParam) < 0 && flag != "") { tempParam.Remove(temp); tempSql.Append(DbCache.Get(cacheType, paramKey)); } else { tempSql.Append(DbCache.Get(cacheType, paramKey)); } } else { tempParam.Remove(temp); } break; } case "isgreaterthan": { if (temp.Value.ToStr().ToDecimal(0) > conditionValue.ToDecimal(0)) { if (paramSql.IndexOf(tempKey) >= 0) { tempParam.Remove(temp); tempSql.Append(paramSql.ToString().Replace(tempKey, temp.Value.ToStr())); } else if (paramSql.IndexOf(flagParam) < 0 && flag != "") { tempParam.Remove(temp); tempSql.Append(DbCache.Get(cacheType, paramKey)); } else { tempSql.Append(DbCache.Get(cacheType, paramKey)); } } else { tempParam.Remove(temp); } break; } case "islessthan": { if (temp.Value.ToStr().ToDecimal(0) < conditionValue.ToDecimal(0)) { if (paramSql.IndexOf(tempKey) >= 0) { tempParam.Remove(temp); tempSql.Append(paramSql.ToString().Replace(tempKey, temp.Value.ToStr())); } else if (paramSql.IndexOf(flagParam) < 0 && flag != "") { tempParam.Remove(temp); tempSql.Append(DbCache.Get(cacheType, paramKey)); } else { tempSql.Append(DbCache.Get(cacheType, paramKey)); } } else { tempParam.Remove(temp); } break; } case "isnullorempty": { if (string.IsNullOrEmpty(temp.Value.ToStr())) { if (paramSql.IndexOf(tempKey) >= 0) { tempParam.Remove(temp); tempSql.Append(paramSql.ToString().Replace(tempKey, temp.Value.ToStr())); } else if (paramSql.IndexOf(flagParam) < 0 && flag != "") { tempParam.Remove(temp); tempSql.Append(DbCache.Get(cacheType, paramKey)); } else { tempSql.Append(DbCache.Get(cacheType, paramKey)); } } else { tempParam.Remove(temp); } break; } case "isnotnullorempty": { if (!string.IsNullOrEmpty(temp.Value.ToStr())) { if (paramSql.IndexOf(tempKey) >= 0) { tempParam.Remove(temp); tempSql.Append(paramSql.ToString().Replace(tempKey, temp.Value.ToStr())); } else if (paramSql.IndexOf(flagParam) < 0 && flag != "") { tempParam.Remove(temp); tempSql.Append(DbCache.Get(cacheType, paramKey)); } else { tempSql.Append(DbCache.Get(cacheType, paramKey)); } } else { tempParam.Remove(temp); } break; } case "if": { conditionValue = conditionValue.Replace(temp.ParameterName, temp.Value == null ? null : temp.Value.ToStr()); conditionValue = conditionValue.Replace("#", "\""); if (CSharpScript.EvaluateAsync <bool>(conditionValue).Result) { if (paramSql.IndexOf(tempKey) >= 0) { tempParam.Remove(temp); tempSql.Append(paramSql.ToString().Replace(tempKey, temp.Value.ToStr())); } else if (paramSql.IndexOf(flagParam) < 0 && flag != "") { tempParam.Remove(temp); tempSql.Append(DbCache.Get(cacheType, paramKey)); } else { tempSql.Append(DbCache.Get(cacheType, paramKey)); } } else { tempParam.Remove(temp); } break; } case "choose": { var isSuccess = false; for (int j = 0; j < DbCache.Get(cacheType, paramKey).ToStr().ToInt(0); j++) { conditionKey = string.Format("{0}.choose.{1}", paramKey, j); condition = DbCache.Get(cacheType, conditionKey).ToStr().ToLower(); conditionValueKey = string.Format("{0}.choose.condition.{1}", paramKey, j); conditionValue = DbCache.Get(cacheType, conditionValueKey).ToStr(); conditionValue = conditionValue.Replace(temp.ParameterName, temp.Value == null ? null : temp.Value.ToStr()); conditionValue = conditionValue.Replace("#", "\""); if (CSharpScript.EvaluateAsync <bool>(conditionValue).Result) { isSuccess = true; if (condition.IndexOf(tempKey) >= 0) { tempParam.Remove(temp); tempSql.Append(condition.Replace(tempKey, temp.Value.ToStr())); } else if (condition.IndexOf(flagParam) < 0 && flag != "") { tempParam.Remove(temp); tempSql.Append(condition.Replace(tempKey, temp.Value.ToStr())); } else { tempSql.Append(condition); } break; } } if (!isSuccess) { tempParam.Remove(temp); } break; } default: { //isPropertyAvailable if (paramSql.IndexOf(tempKey) >= 0) { tempParam.Remove(temp); tempSql.Append(paramSql.ToString().Replace(tempKey, temp.Value.ToStr())); } else if (paramSql.IndexOf(flagParam) < 0 && flag != "") { tempParam.Remove(temp); tempSql.Append(DbCache.Get(cacheType, paramKey)); } else { tempSql.Append(DbCache.Get(cacheType, paramKey)); } break; } } } } if (tempSql.ToString() != "") { sql.Append(DbCache.Get(cacheType, dynKey)); sql.Append(tempSql.ToString()); } } } #endregion } param = tempParam.ToArray(); return(sql.ToString()); }
/// <summary> /// froeach数量 /// </summary> /// <param name="name"></param> /// <param name="config"></param> /// <returns></returns> public static int MapForEachCount(string name, ConfigModel config) { return(DbCache.Get(config.CacheType, string.Format("{0}.foreach", name.ToLower())).ToInt(1)); }