示例#1
0
        private string ToLambdaStringItem(string paramName = "p", bool isRoot = false)
        {
            string target = string.Empty;

            target = QuoteValue(Value1?.ToString(), Value1?.GetType().FullName, "\"");
            switch (Field.FieldType)
            {
            case FieldType.Date:
                target = $"DateTime.Parse({target})";
                break;
            }
            var sqlString = string.Empty;

            switch (ComparisonOperator)
            {
            case ComparisonOperator.Contains:
                target    = ".Contains(" + target + ")";
                sqlString = (Negate ? "!(" : "") + $"({paramName}.{Field.Name} != null && {paramName}.{Field.Name}{target})" + (Negate ? ")" : "");
                break;

            case ComparisonOperator.StartsWith:
                target    = ".StartsWith(" + target + ")";
                sqlString = (Negate ? "!(" : "") + $"({paramName}.{Field.Name} != null && {paramName}.{Field.Name}{target})" + (Negate ? ")" : "");
                break;

            case ComparisonOperator.EndsWith:
                target    = ".EndsWith(" + target + ")";
                sqlString = (Negate ? "!(" : "") + $"({paramName}.{Field.Name} != null && {paramName}.{Field.Name}{target})" + (Negate ? ")" : "");
                break;

            case ComparisonOperator.Between:
                var target2 = QuoteValue(Value2?.ToString(), Value2?.GetType().FullName, "\"");
                switch (Field.FieldType)
                {
                case FieldType.Date:
                    target2 = $"DateTime.Parse({target2})";
                    break;
                }
                var part1 = $"{paramName}.{Field.Name} >= {target}";
                var part2 = $"{paramName}.{Field.Name} <= {target2}";
                sqlString = (Negate ? "!(" : "") + $"{part1} && {part2}" + (Negate ? ")" : "");
                break;

            default:
                sqlString = (Negate ? "!(" : "") + $"{paramName}.{Field.Name} {GetComparisonOperatorCSharp(ComparisonOperator)} {target}" + (Negate ? ")" : "");
                break;
            }
            return(sqlString);
        }
示例#2
0
        /// <summary>
        ///     <para>Default call for atributtes by the Service Proxy before the methody body.</para>
        /// </summary>
        /// <param name="method">
        ///     <para>Method .</para>
        /// </param>
        /// <param name="il">
        ///     <para>Il from  assembly to override the method.</para>
        /// </param>
        /// <returns>
        ///     <para>true if can execute method</para>
        /// </returns>
        public override bool MtnBeforeExecution(MethodInfo method, ILHelper il)
        {
            const BindingFlags bindFlag =
                BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;

            var name = "MtnAfterPermission" + Guid.NewGuid();

            // for security reasons set to int.MinValue
            if (Value1 == null)
            {
                Value1 = int.MinValue;
            }
            if (Value2 == null)
            {
                Value2 = int.MinValue;
            }
            if (Value3 == null)
            {
                Value3 = int.MinValue;
            }
            if (Value4 == null)
            {
                Value4 = int.MinValue;
            }

            // create local variables
            il
            .CreateLocalVar("Mtn.IsPermitted", typeof(bool), false)
            .CreateLocalVar("Mtn.PermissionParameter1", Value1.GetType(), true)
            .CreateLocalVar("Mtn.PermissionParameter2", Value2.GetType(), true)
            .CreateLocalVar("Mtn.PermissionParameter3", Value3.GetType(), true)
            .CreateLocalVar("Mtn.PermissionParameter4", Value4.GetType(), true);

            // load Value1
            il
            .LoadVar("Mtn.PermissionParameter1")
            .LoadObject(Value1)
            .Box(Value1.GetType())
            .SetVar("Mtn.PermissionParameter1");

            // load Value2
            il
            .LoadObject(Value2)
            .Box(Value2.GetType())
            .SetVar("Mtn.PermissionParameter2");

            // load Value3
            il
            .LoadObject(Value3)
            .Box(Value3.GetType())
            .SetVar("Mtn.PermissionParameter3");

            // load Value4
            il
            .LoadObject(Value4)
            .Box(Value4.GetType())
            .SetVar("Mtn.PermissionParameter4");

            // put all variables in stack
            il
            .LoadVar("Mtn.PermissionParameter1")
            .UnboxAny(typeof(object))
            .LoadVar("Mtn.PermissionParameter2")
            .UnboxAny(typeof(object))
            .LoadVar("Mtn.PermissionParameter3")
            .UnboxAny(typeof(object))
            .LoadVar("Mtn.PermissionParameter4")
            .UnboxAny(typeof(object));

            // invoke method
            il
            .InvokeMethod(typeof(Permission), "HasPermission", bindFlag)
            .SetVar("Mtn.IsPermitted")
            .LoadVar("Mtn.IsPermitted")
            .GotoIfNotNullOrTrue(name);

            // if is invalid returns the default value
            il
            .InvokeMethod(typeof(Permission).GetProperty("UnallowedResult").GetGetMethod())
            .SetVar("Mtn.ReturnValue")
            .LoadVar("Mtn.ReturnValue")
            .GotoIfNotNullOrTrue("MtnAfterAll");

            il.MarkLabel(name);
            return(true);
        }
示例#3
0
        private string ToSqlStringItem(bool useParameter = false)
        {
            if (Field == null || string.IsNullOrEmpty(Field.Name))
            {
                return(string.Empty);
            }
            var    columnName = Field.Name;
            string target     = string.Empty;

            // use field name for parameters, to avoid column with same name, but different sources
            if (Value1 != null)
            {
                target = (useParameter ? "@" + Field.Name : QuoteValue(Value1.ToString(), Value1.GetType().FullName, "'"));
            }
            else
            {
                target = "@" + Field.Name;
            }

            switch (ComparisonOperator)
            {
            case ComparisonOperator.Contains:
                target = "'%' + " + target + " + '%'";
                break;

            case ComparisonOperator.StartsWith:
                target = target + " + '%'";
                break;

            case ComparisonOperator.EndsWith:
                target = "'%' + " + target;
                break;

            case ComparisonOperator.Between:
                if (Value2 != null)
                {
                    target = $"{target} AND " + (useParameter ? "@" + $"{Field.Name}2" : QuoteValue(Value2.ToString(), Value2.GetType().FullName, "'"));
                }
                else
                {
                    target = $"{target} AND @{Field.Name}2";
                }
                break;

            default:
                break;
            }
            var sqlString = (Negate ? "NOT(" : "") + $"[{columnName}]" + " " + GetComparisonOperatorSql(ComparisonOperator) + " " + target + (Negate ? ")" : "");

            return(sqlString);
        }