Пример #1
0
        public static Expression <Func <TEntity, bool> > GetFuzzyExpression(FuzzyModel model)
        {
            var func = FuzzyQueryHandlerMapping[model.FuzzyField];

            if (func != default)
            {
                return(func(model));
            }
            else if (PropertiesCache <TEntity> .PropMembers.Contains(model.FuzzyField) &&
                     !PropertiesCache <TEntity> .GetBlockWhereFields().Contains(model.FuzzyField))
            {
                var action = NDelegate
                             .DefaultDomain()
                             .Func <FuzzyModel, Expression <Func <TEntity, bool> > >($@"
Expression<Func<{typeof(TEntity).GetDevelopName()},bool>> exp = default;
if(arg.IgnoreCase) {{
    exp = obj => obj.{model.FuzzyField}.Contains(arg.FuzzyValue,StringComparison.CurrentCultureIgnoreCase);
}} else {{
    exp = obj => obj.{model.FuzzyField}.Contains(arg.FuzzyValue);
}}
return exp;
");
                _dict[model.FuzzyField]  = action;
                FuzzyQueryHandlerMapping = _dict.PrecisioTree();
                return(action(model));
            }
            return(default);
Пример #2
0
 public static string Call(string caller, string parameters)
 {
     if (_invokeFastCache.TryGetValue(caller, out var func))
     {
         return(func(parameters));
     }
     else
     {
         var realType = LibraTypeManagement.GetTypeFromMapper(caller);
         var index    = realType.LastIndexOf('.');
         var type     = realType.Substring(0, index);
         var method   = realType.Substring(index + 1, realType.Length - index - 1);
         try
         {
             var message = NDelegate
                           .RandomDomain(item => item.LogSyntaxError().LogCompilerError())
                           .Func <string>($"return LibraProtocalAnalysis.HandlerType(\"{caller}\",typeof({type}),\"{method}\");")();
             if (message != null)
             {
                 return(message);
             }
         }
         catch (Exception ex)
         {
             throw new Exception($"请核对您所访问的类: {type} 及方法 {method} 是否存在! 额外信息:{ex.Message}");
         }
         return(_invokeFastCache[caller](parameters));
     }
 }
Пример #3
0
        //HashCache<string,string>

        public HashTest()
        {
            model        = new TestModel();
            HashHandler1 = model.Model1.HashTree();
            HashHandler2 = model.Model2.HashTree();
            HashHandler3 = model.Model3.HashTree();
        }
Пример #4
0
        public BenchmarkTest()
        {
            NErrorLog.Enabled = true;
            NatashaInitializer.InitializeAndPreheating();
            model            = new TestModel();
            _imMap           = ImMap <string> .Empty;
            _imHashMap       = ImHashMap <string, string> .Empty;
            FuzzyHandler     = model.Model1.FuzzyTree();
            HashHandler      = model.Model1.HashTree();
            PrecisionHandler = model.Model1.PrecisioTree();
            DictHandler      = model.Model1;
            Dictionary <int, string> hashDict = new();

            foreach (var item in model.Model1)
            {
                _imHashMap = _imHashMap.AddOrUpdate(item.Key, item.Value);
                _imMap     = _imMap.AddOrUpdate(Int32.Parse(item.Key), item.Value);
                hashDict[Int32.Parse(item.Key)] = item.Value;
            }
            HashCodePrecisionHandler = hashDict.CustomerTree(item => item.ToString());
            ConDictHandler           = new ConcurrentDictionary <string, string>(model.Model1);
            ReadonlyDictHandler      = ImmutableDictionary.CreateRange(DictHandler);
        }
Пример #5
0
        public static IEnumerable <TReturn> ToList <TReturn>(ISelect <TEntity> select, Expression <Func <TEntity, TReturn> > expression)
        {
            //查询表达式树是否为之前处理过的
            if (JoinAction <TEntity, TReturn> .Action == null)
            {
                //给匿名类创建一个代理类
                StringBuilder fieldsScript = new StringBuilder();
                StringBuilder joinScript   = new StringBuilder();
                // 获取构造函数参数
                var arguments = ((NewExpression)expression.Body).Arguments;
                //获取匿名类成员
                var members = ((NewExpression)expression.Body).Members;


                for (int i = 0; i < arguments.Count; i++)
                {
                    if (arguments[i].NodeType == ExpressionType.MemberAccess)
                    {
                        var memberExpression = (MemberExpression)arguments[i];
                        var memberName       = memberExpression.Member.Name;

                        // 方法类型参数
                        if (memberExpression.Expression.NodeType == ExpressionType.Convert ||
                            memberExpression.Expression.NodeType == ExpressionType.Call)
                        {
                            var scriptKey = memberExpression.Expression.ToString();
                            var aliasName = "";
                            if (JoinExpressionMapping[scriptKey] == default)
                            {
                                string targetMemberName = default;
                                var    methodExp        = (MethodCallExpression)memberExpression.Expression;
                                if (methodExp.Arguments[1].NodeType == ExpressionType.Quote)
                                {
                                    var unaryExp = (UnaryExpression)(methodExp.Arguments[1]);
                                    if (unaryExp.NodeType == ExpressionType.Quote)
                                    {
                                        var lbdExp = (LambdaExpression)(unaryExp.Operand);
                                        if (lbdExp.Body.NodeType == ExpressionType.Convert)
                                        {
                                            var bodyExp = (UnaryExpression)lbdExp.Body;
                                            if (bodyExp.Operand.NodeType == ExpressionType.MemberAccess)
                                            {
                                                var memberExp  = (MemberExpression)(bodyExp.Operand);
                                                var tempValues = memberExp.ToString().Split('.');
                                                aliasName             = tempValues[0];
                                                targetMemberName      = tempValues[1];
                                                _joinDict[scriptKey]  = aliasName;
                                                JoinExpressionMapping = _joinDict.PrecisioTree();
                                            }
                                        }
                                    }
                                }


                                if (methodExp.NodeType == ExpressionType.Call)
                                {
                                    var    joinTableType = methodExp.Method.GetGenericArguments()[0].GetDevelopName();
                                    string condition     = default;
                                    if (methodExp.Arguments.Count > 2)
                                    {
                                        var exp = methodExp.Arguments[2];
                                        if (exp.NodeType == ExpressionType.Quote)
                                        {
                                            condition = "&&" + ((LambdaExpression)(((UnaryExpression)exp).Operand)).Body.ToString();
                                        }
                                    }
                                    var joinType = methodExp.Method.Name;

                                    MemberExpression memberExp = default;
                                    if (methodExp.Arguments[0].NodeType == ExpressionType.Convert)
                                    {
                                        memberExp = (MemberExpression)((UnaryExpression)(methodExp.Arguments[0])).Operand;
                                    }
                                    else if (methodExp.Arguments[0].NodeType == ExpressionType.MemberAccess)
                                    {
                                        memberExp = (MemberExpression)(methodExp.Arguments[0]);
                                    }
                                    if (memberExp != default)
                                    {
                                        var sourceMemberName = memberExp.Member.Name;
                                        if (joinType == "AriesInnerJoin")
                                        {
                                            joinScript.Append($"obj.InnerJoin<{joinTableType}>((a,{aliasName})=>a.{sourceMemberName} == {aliasName}.{targetMemberName}{condition});");
                                        }
                                        else if (joinType == "AriesLeftJoin")
                                        {
                                            joinScript.Append($"obj.LeftJoin<{joinTableType}>((a,{aliasName})=>a.{sourceMemberName} == {aliasName}.{targetMemberName}{condition});");
                                        }
                                        else if (joinType == "AriesRightJoin")
                                        {
                                            joinScript.Append($"obj.RightJoin<{joinTableType}>((a,{aliasName})=>a.{sourceMemberName} == {aliasName}.{targetMemberName}{condition});");
                                        }
                                    }
                                }
                            }
                            fieldsScript.Append($"{JoinExpressionMapping[scriptKey]}.\"{memberName}\" AS \"{members[i].Name}\",");
                        }
                        else
                        {
                            if (members[i].Name == memberName)
                            {
                                fieldsScript.Append($"a.\"{memberName}\",");
                            }
                            else
                            {
                                fieldsScript.Append($"a.\"{memberName}\" AS \"{members[i].Name}\",");
                            }
                        }
                    }
                }
                if (fieldsScript.Length > 1)
                {
                    fieldsScript.Length -= 1;
                    JoinAction <TEntity, TReturn> .FieldsScript = fieldsScript.ToString();
                    JoinAction <TEntity, TReturn> .Action       = NDelegate
                                                                  .DefaultDomain()
                                                                  .Action <ISelect <TEntity> >(joinScript.ToString());
                }
            }
            //调用 TReturn 的处理函数
            JoinAction <TEntity, TReturn> .Action(select);

            //返回执行结果
            return(select.ToList <TReturn>(JoinAction <TEntity, TReturn> .FieldsScript));
            //return ProxyCaller<TEntity, TReturn>.ToList(code, select);
        }
Пример #6
0
 static SimpleJoinOperator()
 {
     _joinDict             = new ConcurrentDictionary <string, string>();
     JoinExpressionMapping = _joinDict.PrecisioTree();
 }
Пример #7
0
        public static string HandlerType(string key, Type type, string method)
        {
            if (!LibraTypeManagement.HasMethod(type, method))
            {
                return("该类型不支持远程调用!");
            }

            var           methodInfo         = type.GetMethod(method);
            var           methodCallBuilder  = new StringBuilder();
            var           parameterInfos     = methodInfo.GetParameters();
            ParameterInfo firstParameterInfo = default;
            string        parameterName      = default;

            var classBuilder = new StringBuilder();

            if (parameterInfos.Length > 1)
            {
                parameterName = "parameters";
                var parameterBuilder = new StringBuilder();
                var className        = "N" + Guid.NewGuid().ToString("N");
                classBuilder.Append($"public class {className}{{");
                foreach (var item in parameterInfos.OrderBy(c => c.Position))
                {
                    parameterBuilder.Append($"parameters.{item.Name},");
                    classBuilder.Append($"public {item.ParameterType.GetDevelopName()} {item.Name} {{ get;set; }}");
                }
                classBuilder.Append('}');
                methodCallBuilder.AppendLine($"var {parameterName} = string.IsNullOrEmpty(arg) ? default : System.Text.Json.JsonSerializer.Deserialize<{className}>(arg,LibraProtocalAnalysis.JsonOption);");

                parameterBuilder.Length -= 1;
                parameterName            = parameterBuilder.ToString();
            }
            else if (parameterInfos.Length == 1)
            {
                parameterName      = "parameters";
                firstParameterInfo = parameterInfos[0];
                var pType = firstParameterInfo.ParameterType;
                if (pType.IsPrimitive || pType == typeof(string) || pType == typeof(DateTime))
                {
                    methodCallBuilder.AppendLine($"var {parameterName} = string.IsNullOrEmpty(arg) ? default : System.Text.Json.JsonSerializer.Deserialize<LibraSingleParameter<{firstParameterInfo.ParameterType.GetDevelopName()}>>(arg,LibraProtocalAnalysis.JsonOption);");
                    parameterName += ".Value";
                }
                else
                {
                    methodCallBuilder.AppendLine($"var {parameterName} = string.IsNullOrEmpty(arg) ? default : System.Text.Json.JsonSerializer.Deserialize<{firstParameterInfo.ParameterType.GetDevelopName()}>(arg,LibraProtocalAnalysis.JsonOption);");
                }
            }


            //获取调用者
            string caller = default;

            if (methodInfo.IsStatic)
            {
                caller = type.GetDevelopName();
            }
            else
            {
                caller = $"LibraProtocalAnalysis.Provider.GetService<{type.GetDevelopName()}>()";
            }


            //调用
            if (methodInfo.ReturnType != typeof(void))
            {
                methodCallBuilder.AppendLine($"var result = new LibraResult<{methodInfo.ReturnType.GetDevelopName()}>(){{ Value = {caller}.{methodInfo.Name}({parameterName}) }};");
                methodCallBuilder.AppendLine($"return System.Text.Json.JsonSerializer.Serialize(result);");
            }
            else
            {
                methodCallBuilder.AppendLine($"{caller}.{methodInfo.Name}({parameterName});");
                methodCallBuilder.AppendLine("return default;");
            }


            _invokerMapping[key] = NDelegate
                                   .RandomDomain(item =>
            {
                item
                .LogSyntaxError()
                .UseFileCompile();
            })

                                   .SetClass(item => item.AllowPrivate(type).Body(classBuilder.ToString()))
                                   .Func <string, string>(methodCallBuilder.ToString());
            _invokeFastCache = _invokerMapping.PrecisioTree();
            return(null);
        }
Пример #8
0
 static LibraProtocalAnalysis()
 {
     JsonOption       = new JsonSerializerOptions();
     _invokerMapping  = new ConcurrentDictionary <string, Func <string, string> >();
     _invokeFastCache = _invokerMapping.PrecisioTree();
 }
Пример #9
0
        public DynamicSwitchBuilder(IDictionary <TKey, TValue> pairs, Func <TKey, string> keyToCase = null)
        {
            int count  = 0;
            var nClass = NClass.RandomDomain()
                         .Public()
                         .Inheritance <DynamicDictionaryBase <TKey, TValue> >()
                         .Unsafe();

#if NET5_0
            nClass.SkipInit();
#endif



            //构建快查字典 给BTF使用
            var getValueMethodScript    = new Dictionary <TKey, string>();
            var tryGetValueMethodScript = new Dictionary <TKey, string>();
            var setValueMethodSciprt    = new Dictionary <TKey, string>();
            foreach (var item in pairs)
            {
                count += 1;
                string field = _prefix + count;
                nClass.PrivateReadonlyField <TValue>(field);

                getValueMethodScript[item.Key]    = $"return {field};";
                tryGetValueMethodScript[item.Key] = $"value = {field};return true;";
                setValueMethodSciprt[item.Key]    = $"{field.ReadonlyScript()} = value;return;";
            }

            //根据快查字典生成快查代码
            //value GetValue(key)
            nClass.Method(method =>
            {
                method
                .Param <TKey>("key")
                .Override()
                .Return <TValue>()
                .Name("GetValue")
                .Public()
                .BodyAppend(ScriptKeyAction(getValueMethodScript, "key", keyToCase))
                .BodyAppend("return default;");
            });

            //bool TryGetValue(key,out value)
            nClass.Method(method =>
            {
                method
                .Param <TKey>("key")
                .Param <TValue>("value", "out ")
                .Override()
                .Return <bool>()
                .Name("TryGetValue")
                .Public()
                .BodyAppend(ScriptKeyAction(tryGetValueMethodScript, "key", keyToCase))
                .BodyAppend("value=default; return false;");
            });

            //Change(key,value)
            nClass.Method(method =>
            {
                method
                .Param <TKey>("key")
                .Param <TValue>("value")
                .Override()
                .Name("Change")
                .Public()
                .BodyAppend(ScriptKeyAction(setValueMethodSciprt, "key", keyToCase))
                .BodyAppend("throw new Exception(\"Can't find key!\");");
            });


            var ProxyType = nClass.GetType();

            Instance = nClass
                       .DelegateHandler
                       .Func <DynamicDictionaryBase <TKey, TValue> >($"return new {ProxyType.GetDevelopName()}();")();


            foreach (var item in pairs)
            {
                Instance.Change(item.Key, item.Value);
            }
        }
Пример #10
0
 static FuzzyQueryOperator()
 {
     _dict = new ConcurrentDictionary <string, Func <FuzzyModel, Expression <Func <TEntity, bool> > > >();
     FuzzyQueryHandlerMapping = _dict.PrecisioTree();
 }
Пример #11
0
 public PrecisionTest()
 {
     model    = new TestModel();
     Handler1 = model.Model1.PrecisioTree();
 }
Пример #12
0
 public FuzzyTest()
 {
     model    = new TestModel();
     Handler1 = model.Model1.FuzzyTree();
 }