Exemplo n.º 1
0
        protected virtual SqlResult CompileCteQuery(SqlResult ctx, Query query)
        {
            var cteFinder       = new CteFinder(query, EngineCode);
            var cteSearchResult = cteFinder.Find();

            var rawSql      = new StringBuilder("WITH ");
            var cteBindings = new List <object>();

            foreach (var cte in cteSearchResult)
            {
                var cteCtx = CompileCte(cte);

                cteBindings.AddRange(cteCtx.Bindings);
                rawSql.Append(cteCtx.RawSql.Trim());
                rawSql.Append(",\n");
            }

            rawSql.Length -= 2; // remove last comma
            rawSql.Append('\n');
            rawSql.Append(ctx.RawSql);

            ctx.Bindings.InsertRange(0, cteBindings);
            ctx.RawSql = rawSql.ToString();

            return(ctx);
        }
Exemplo n.º 2
0
        protected virtual SqlResult CompileRaw(Query query)
        {
            SqlResult ctx;

            if (query.Method == "insert")
            {
                ctx = CompileInsertQuery(query);
            }
            else if (query.Method == "update")
            {
                ctx = CompileUpdateQuery(query);
            }
            else if (query.Method == "delete")
            {
                ctx = CompileDeleteQuery(query);
            }
            else
            {
                if (query.Method == "aggregate")
                {
                    query.ClearComponent("limit")
                    .ClearComponent("order")
                    .ClearComponent("group");

                    query = TransformAggregateQuery(query);
                }

                ctx = CompileSelectQuery(query);
            }

            // handle CTEs
            if (query.HasComponent("cte", EngineCode))
            {
                var cteFinder       = new CteFinder(query, EngineCode);
                var cteSearchResult = cteFinder.Find();

                var rawSql      = new StringBuilder("WITH ");
                var cteBindings = new List <object>();

                foreach (var cte in cteSearchResult)
                {
                    var cteCtx = CompileCte(cte);

                    cteBindings.AddRange(cteCtx.Bindings);
                    rawSql.Append(cteCtx.RawSql.Trim());
                    rawSql.Append(",\n");
                }

                rawSql.Length -= 2; // remove last comma
                rawSql.Append('\n');
                rawSql.Append(ctx.RawSql);

                ctx.Bindings.InsertRange(0, cteBindings);
                ctx.RawSql = rawSql.ToString();
            }

            ctx.RawSql = Helper.ExpandParameters(ctx.RawSql, "?", ctx.Bindings.ToArray());

            return(ctx);
        }