Пример #1
0
        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);
        }
Пример #2
0
        /// <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());
        }