Пример #1
0
        private static string Write(TextWriter writer, Expression expression, AtkExpSqlType atkSql, string leftBracket,
                                    string rightBracket)
        {
            expression = AtkPartialEvaluator.Eval(expression);


            var expressionWriterSql = new AtkExpressionWriterSql <T>(writer)
            {
                _leftBracket  = leftBracket,
                _rightBracket = rightBracket,
                atkRead       = atkSql
            };

            expressionWriterSql.Visit(expression);

            switch (atkSql)
            {
            case AtkExpSqlType.AtkWhere:
                return(Regex.Replace(expressionWriterSql.atkWhereResult, "and\\s?$", ""));

            case AtkExpSqlType.AtkOrder:
                return(Regex.Replace(expressionWriterSql.atkOrderResult, ",\\s?$", ""));

            default:
                return(string.Empty);
            }
        }
        private static string Write(TextWriter writer, System.Linq.Expressions.Expression expression, AtkExpSqlType atkSql, string tableAlias)
        {
            expression = AtkPartialEvaluator.Eval(expression);
            var atkR = new AtkExpressionWriterSql(writer);

            if (!string.IsNullOrWhiteSpace(tableAlias))
            {
                atkR._tableAlias = "[" + tableAlias + "].";
            }
            atkR.atkRead = atkSql;
            atkR.Visit(expression);
            string result = string.Empty;

            switch (atkSql)
            {
            case AtkExpSqlType.atkOrder:
                result = Regex.Replace(atkR.atkOrdeRsult, @",\s?$", "");
                return(result);

            case AtkExpSqlType.atkWhere:
                result = Regex.Replace(atkR.atkWhereResult, @"and\s?$", "");
                return(result);;

            default: return(string.Empty);
            }
        }
Пример #3
0
        private static CustomSqlWriteExpressionResult?WriteWithParameter(TextWriter writer, Expression expression,
                                                                         AtkExpSqlType atkSql, string leftBracket,
                                                                         string rightBracket)
        {
            expression = AtkPartialEvaluator.Eval(expression);
            PartialSelectSelectParameterDefinition leftConstantParameterDefinition  = null;
            PartialSelectSelectParameterDefinition rightConstantParameterDefinition = null;
            var expressionOperator = string.Empty;

            var expressionWriterSql = new AtkExpressionWriterSql <T>(writer)
            {
                _leftBracket  = leftBracket,
                _rightBracket = rightBracket,
                atkRead       = atkSql
            };

            expressionWriterSql.Visit(expression);
            if (expression is LambdaExpression lambda)
            {
                var body = lambda.Body as BinaryExpression;
                expressionOperator = new AtkExpressionWriterSql <T>(writer).GetOperator(lambda.Body.NodeType);

                if (body != null && body.Left.NodeType == ExpressionType.Constant)
                {
                    if (body.Left is ConstantExpression b)
                    {
                        leftConstantParameterDefinition = new PartialSelectSelectParameterDefinition
                        {
                            DbType     = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(body.Left.Type),
                            Direction  = ParameterDirection.Input,
                            IsNullable = body.Left.Type.IsNullable(),
                            Value      = Convert.ChangeType(b.Value, body.Left.Type)
                        }
                    }
                    ;
                }

                if (body != null && body.Right.NodeType == ExpressionType.Constant)
                {
                    if (body.Right is ConstantExpression b)
                    {
                        rightConstantParameterDefinition = new PartialSelectSelectParameterDefinition
                        {
                            DbType     = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(body.Right.Type),
                            Direction  = ParameterDirection.Input,
                            IsNullable = body.Right.Type.IsNullable(),
                            Value      = Convert.ChangeType(b.Value,
                                                            Nullable.GetUnderlyingType(body.Right.Type) ?? body.Right.Type)
                        }
                    }
                }
                ;
            }

            switch (atkSql)
            {
            case AtkExpSqlType.AtkWhere:

                return(new CustomSqlWriteExpressionResult(
                           Regex.Replace(expressionWriterSql.atkWhereResult, "and\\s?$", ""),
                           expressionOperator,
                           leftConstantParameterDefinition,
                           rightConstantParameterDefinition
                           ));

            case AtkExpSqlType.AtkOrder:
                return(null);    //Regex.Replace(expressionWriterSql.atkOrderResult, ",\\s?$", "");

            default:
                return(null);
            }
        }