示例#1
0
        private IList <SqlBuildModel> SqlBuilderModel(string scriptCode, IDictionary <string, object> parameters, object bodyJson)
        {
            Lua lua = this.GetLua();
            var res = LuaScriptRunner.ExecuteLuaScript(lua, scriptCode, parameters, bodyJson);
            IList <SqlBuildModel> result = new List <SqlBuildModel>();

            for (var i = 0; i < res.Length; i++)
            {
                var item = res[i];
                if (item is LuaTable)
                {
                    var            table         = (LuaTable)item;
                    var            extraParams   = LuaScriptRunner.LuaTableToCSharpData <IDictionary <string, object> >(table["ExtraParams"]);
                    var            sqlParameters = RequestDataHelper.MergeDictionary(parameters, extraParams);//合并附加参数到sql执行参数中
                    IList <object> relationKeys  = LuaScriptRunner.LuaTableToCSharpData <IList <object> >(table["RelationKeys"]);
                    long           codeKind      = table["CodeKind"] == null ? 0 : (long)table["CodeKind"];
                    string         key           = (string)table["Key"];
                    var            model         = new SqlBuildModel {
                        Sql = (string)table["Sql"], Parameters = sqlParameters, CodeKind = codeKind, RelationKeys = relationKeys, Key = key
                    };
                    result.Add(model);
                    this.ChildSqlBuilderModel(parameters, model, LuaScriptRunner.LuaTableToCSharpData <object>(table["Children"]));
                }
                else
                {
                    throw new CustomException(99, "构建SQL的脚本返回有误");
                }
            }
            return(result);
        }
示例#2
0
 public override object LuaExecuteSql(string sql, int codeKind, LuaTable table)
 {
     try
     {
         IDictionary <string, object> parameters = LuaScriptRunner.LuaTableToCSharpData <IDictionary <string, object> >(table);
         object result    = this.ExecuteSql(sql, codeKind, parameters);
         string luaScript = LuaScriptRunner.ToLuaScript(result);
         Lua    lua       = GetLua();
         var    returns   = lua.DoString("return " + luaScript);
         return(returns[0]);
     }
     catch
     {
         _logger.LogError("Lua执行SQL语句异常");
         throw;
     }
 }
示例#3
0
 //lua中调用此方法执行已配置的内部或者外部接口
 public override object LuaInvokeDynamicApi(string code, LuaTable table)
 {
     try
     {
         IDictionary <string, object> parameters = LuaScriptRunner.LuaTableToCSharpData <IDictionary <string, object> >(table);
         object result    = this.DynamicFetch(code, parameters);
         string luaScript = LuaScriptRunner.ToLuaScript(result);
         Lua    lua       = GetLua();
         var    returns   = lua.DoString("return " + luaScript);
         return(returns[0]);
     }
     catch
     {
         _logger.LogError("Lua调用接口异常");
         throw;
     }
 }
示例#4
0
 //0 = String,1 = Integer,2 = Long,3 = Double,4 = Float,5 = Decimal,6 = Boolean,7 = Date,8 = DateTime,9=Ulong,10 = Key/Value,11 = List,12 = File
 private void LuaScriptCheck(Lua lua, int paramType, string paramCode, string paramName, string paramCheckScript, IDictionary <string, object> parameters)
 {
     if (paramType != 10 && paramType != 11 && paramType != 12 && parameters.ContainsKey(paramCode))
     {
         IDictionary <string, object> param = new Dictionary <string, object>();
         param[paramCode] = parameters[paramCode];
         object[] result = LuaScriptRunner.ExecuteLuaScript(lua, paramCheckScript, param);//第一个返回值为验证是否通过(true|false),第二个参数为验证错误信息,为true时没有
         if (!(bool)result[0])
         {
             if (result.Length > 1)
             {
                 throw new CustomException(11, result[1].ToString());//通过自定义异常抛出验证失败信息
             }
             else
             {
                 throw new CustomException(11, paramName + "验证失败");
             }
         }
     }
 }
示例#5
0
        private object ExecuteScript(string scriptCode, int codeKind, IDictionary <string, object> parameters, object bodyJson)
        {
            object result = null;

            if (codeKind == 4)//脚本结果
            {
                Lua lua = GetLua();
                result = LuaScriptRunner.ExecuteLuaScript(lua, scriptCode, parameters, bodyJson);
            }
            else
            {
                IList <SqlBuildModel> sqlItems = this.SqlBuilderModel(scriptCode, parameters, bodyJson);
                if (sqlItems.Count == 0)
                {
                    throw new CustomException(11, "SQL语句配置错误");
                }

                switch (codeKind)
                {
                case 1:    //分页
                {
                    //分页,第一条语句为分页语句,后面如果有其他语句,以字典值的形式返回结果
                    result = new Dictionary <string, object>();
                    SqlBuildModel sqlItem = null;
                    for (var i = 0; i < sqlItems.Count; i++)
                    {
                        sqlItem = sqlItems[i];
                        var key = i.ToString();
                        if (!string.IsNullOrWhiteSpace(sqlItem.Key))
                        {
                            key = sqlItem.Key;
                        }
                        object res;
                        if (i == 0)
                        {
                            res = this.Page(sqlItem.Sql, parameters);
                            this.ForEachMaster(sqlItem, parameters, ((IDictionary <string, object>)res)["List"]);
                        }
                        else
                        {
                            res = this.MasterDetail(sqlItem, parameters);
                        }
                        ((IDictionary <string, object>)result)[key] = res;
                    }
                    if (((Dictionary <string, object>)result).Count() == 1)
                    {
                        result = ((Dictionary <string, object>)result).ElementAt(0).Value;
                    }
                }
                break;

                case 2:    //结果集 - 字典(支持主从)
                {
                    int index = 0;
                    result = new Dictionary <string, object>();
                    foreach (var item in sqlItems)
                    {
                        var key = index.ToString();
                        if (!string.IsNullOrWhiteSpace(item.Key))
                        {
                            key = item.Key;
                        }
                        object res = this.MasterDetail(item, parameters);
                        ((Dictionary <string, object>)result)[key] = res;
                        index++;
                    }
                    if (((Dictionary <string, object>)result).Count() == 1)
                    {
                        result = ((Dictionary <string, object>)result).ElementAt(0).Value;
                    }
                }
                break;

                case 3:    //结果集-列表(支持主从)
                {
                    int index = 0;
                    result = new List <object>();
                    foreach (var item in sqlItems)
                    {
                        var key = index.ToString();
                        if (!string.IsNullOrWhiteSpace(item.Key))
                        {
                            key = item.Key;
                        }
                        object res = this.MasterDetail(item, parameters);
                        ((List <object>)result).Add(res);
                        index++;
                    }
                    if (((List <object>)result).Count() == 1)
                    {
                        result = ((List <object>)result).ElementAt(0);
                    }
                }
                break;

                default:
                    throw new CustomException(11, "非有效执行结果类型");
                }
            }
            return(result);
        }
示例#6
0
 /// <summary>
 /// 参数验证
 /// </summary>
 /// <param name="code">接口编码</param>
 private void ParamsCheck(string code, IDictionary <string, object> config, IDictionary <string, object> inputParameters)
 {
     using (Lua lua = new Lua())
     {
         lua.State.Encoding = Encoding.UTF8;
         //参数整体验证
         string checkScript = config["CheckScript"].ToString();
         IDictionary <string, object> parameters = RequestDataHelper.GetMixParams();
         object bodyJson = RequestDataHelper.GetBodyJsonParameters();
         IDictionary <string, IList <IFormFile> > fileDic = RequestDataHelper.GetAllFiles();
         if (!string.IsNullOrWhiteSpace(checkScript))
         {
             object[] result = LuaScriptRunner.ExecuteLuaScript(lua, checkScript, parameters, bodyJson);//第一个返回值为验证是否通过(true|false),第二个参数为验证错误信息,为true时没有
             if (!(bool)result[0])
             {
                 if (result.Length > 1)
                 {
                     throw new CustomException(11, result[1].ToString());//通过自定义异常抛出验证失败信息
                 }
                 else
                 {
                     throw new CustomException(11, "参数验证失败");
                 }
             }
         }
         IDictionary <string, object> paramData = new Dictionary <string, object>(parameters);
         //单个参数验证
         IList <IDictionary <string, object> > apiParams = _dal.GetApiParams(code);//配置参数信息
         if (apiParams.Count > 0)
         {
             foreach (IDictionary <string, object> dic in apiParams)
             {
                 int    paramType        = (int)dic["ParamType"]; //0 = String,1 = Integer,2 = Long,3 = Double,4 = Float,5 = Decimal,6 = Boolean,7 = Date,8 = DateTime,9=Ulong,10 = Key/Value,11= List,12 = File
                 string paramCode        = dic["ParamCode"].ToString();
                 string paramName        = dic["ParamName"].ToString();
                 short  isRequire        = (short)dic["IsRequire"];
                 short  paramsKind       = (short)dic["ParamsKind"];      //ParamsKind 0 = 普通参数;1 = 系统参数;2=Id值;
                 string checkRule        = dic["CheckRule"].ToString();   //验证使用的正则表达式
                 string ruleError        = dic["RuleError"].ToString();   //正则表达式验证不通过时候的错误提示信息
                 string paramCheckScript = dic["CheckScript"].ToString(); //验证单个参数的lua脚本
                 if (paramsKind == 1)
                 {
                     if (isRequire == 1 && !ParamsPlugin.ContainsKey(paramCode))
                     {
                         throw new CustomException(11, "系统参数" + paramName + "不能为空");
                     }
                     var sysParamValue = ParamsPlugin.Get(paramCode);
                     if (isRequire == 1 && sysParamValue == null)
                     {
                         throw new CustomException(11, "系统参数" + paramName + "不能为空");
                     }
                     inputParameters[paramCode] = sysParamValue;
                     paramData[paramCode]       = sysParamValue;
                 }
                 else if (paramsKind == 2)
                 {
                     var id = DbHelper.NewLongId();
                     inputParameters[paramCode] = id;
                     paramData[paramCode]       = id;
                 }
                 //检查必录项
                 if (isRequire == 1)
                 {
                     this.CheckRequire(paramType, paramCode, paramName, paramData, fileDic);
                 }
                 //正则检查
                 if (!string.IsNullOrWhiteSpace(checkRule))
                 {
                     this.CheckRegexRule(paramType, paramCode, paramName, checkRule, ruleError, paramData);
                 }
                 //脚本验证
                 if (!string.IsNullOrWhiteSpace(paramCheckScript))
                 {
                     this.LuaScriptCheck(lua, paramType, paramCode, paramName, paramCheckScript, paramData);
                 }
                 //转换参数类型
                 this.ConvertParamsType(paramType, paramCode);
             }
         }
     }
 }
示例#7
0
        public virtual string ObjectToLuaScriptString(object obj)
        {
            string luaScriptString = LuaScriptRunner.ToLuaScript(obj);

            return(luaScriptString);
        }