Example #1
0
        /// <summary>
        /// Convierte la cláusula RETURNING a SQL
        /// </summary>
        static (string sql, IReadOnlyList <string> cols) ReturningToString(LambdaExpression returning, ParamMode paramMode, SqlParamDic paramDic, string tableName)
        {
            var pars   = new SqlExprParams(returning.Parameters[0], null, false, tableName, new SqlFromList.ExprStrRawSql[0], paramMode, paramDic);
            var select = SqlSelect.SelectBodyToStr(returning.Body, pars);
            var sql    = $"RETURNING {Environment.NewLine}{SqlSelect.TabStr(SqlSelect.SelectExprToStr(select.Values))}";
            var cols   = select.Values.Select(x => x.Column).ToList();

            return(sql, cols);
        }
Example #2
0
        /// <summary>
        /// La regla que se aplica a las llamadas Atom(Raw(x)), lo que hace es convertir las llamadas ToSql del Atom(Raw(x))
        /// </summary>
        public static IEnumerable <RewriteRule> AtomRawRule(SqlExprParams pars)
        {
            var deferredToSqlRule = RewriteRule.Create(
                "deferredToSql",
                (RewriteTypes.C1 x) => SqlFunctions.ToSql <RewriteTypes.C1>(x),
                null,
                null,
                (match, expr, visit) => Expression.Call(
                    typeof(SqlExpression),
                    nameof(SqlExpression.ExprToSql),
                    new Type[0],
                    Expression.Constant(match.Args[0]),
                    Expression.Constant(pars),
                    Expression.Constant(true)));

            var toSqlRule = RewriteRule.Create(
                "toSql",
                () => RewriteSpecial.Call <string>(typeof(SqlFunctions), nameof(ToSql)),
                null,
                null,
                (match, expr, visit) => Expression.Constant(SqlExpression.ExprToSql(((MethodCallExpression)expr).Arguments[0], pars, true)));

            var toSelectBodySqlRule = RewriteRule.Create(
                "toSelectBodySqlRule",
                () => RewriteSpecial.Call <string>(typeof(SqlFunctions), nameof(ToSelectBodySql)),
                null,
                null,
                (match, expr, visit) =>
                Expression.Constant(
                    SqlSelect.SelectExprToStr(SqlSelect.SelectBodyToStr(((MethodCallExpression)expr).Arguments[0], pars).Values)
                    )
                );
            var windowToSqlRule = RewriteRule.Create(
                "windowToSql",
                (ISqlWindow a) => WindowToSql(a),
                null,
                null,
                (match, expr, visit) => Expression.Constant(SqlCalls.WindowToSql(match.Args[0]))
                );
            var toSqlRules = new[]
            {
                toSelectBodySqlRule,
                toSqlRule,
                windowToSqlRule
            };
            Func <Expression, Expression> applySqlRule = (Expression ex) => new RewriteVisitor(toSqlRules, ExcludeFromRewrite).Visit(ex);

            var atomRawRule = RewriteRule.Create(
                "executeAtomRaw",
                (string x) => RewriteSpecial.Atom(Sql.Raw <RewriteTypes.C1>(RewriteSpecial.NotConstant(x))),
                null,
                null,
                (match, expr, visit) =>
            {
                var arg      = match.Args[0];
                var applySql = applySqlRule(arg);
                if (applySql == arg)
                {
                    return(expr);
                }

                var type  = match.Types[typeof(RewriteTypes.C1)];
                var value = ExprEval.EvalExpr <string>(applySql).Value;

                var ret = Expression.Call(typeof(RewriteSpecial), nameof(RewriteSpecial.Atom), new[] { type },
                                          Expression.Call(typeof(Sql), nameof(Sql.Raw), new[] { type }, Expression.Constant(value))
                                          );

                return(ret);
            });

            return(new[] { atomRawRule });
        }