All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.odysz.transact.sql.parts.select.WithClause Maven / Gradle / Ivy

The newest version!
package io.odysz.transact.sql.parts.select;

import static io.odysz.common.LangExt.isNull;

import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.odysz.common.Utils;
import io.odysz.semantics.ISemantext;
import io.odysz.transact.sql.Query;
import io.odysz.transact.sql.parts.AbsPart;
import io.odysz.transact.sql.parts.Tabl;
import io.odysz.transact.sql.parts.select.SelectElem.ElemType;
import io.odysz.transact.x.TransException;

/**
 * https://github.com/antlr/grammars-v4/blob/master/tsql/TSqlParser.g4
 * 
with_expression
    : WITH (XMLNAMESPACES ',')? common_table_expression (',' common_table_expression)*
    | WITH BLOCKING_HIERARCHY ('(' full_column_name_list ')')? AS '(' select_statement ')'
    ;
* @since 1.4.36 * @author Ody * */ public class WithClause extends AbsPart { boolean recursive; /** * [[with clause' as-tabl-name, {@link Query}]] */ ArrayList withs; public WithClause(boolean recursive, ArrayList withs) { this.recursive = recursive; this.withs = withs; } public WithClause(boolean recursive) { this.recursive = recursive; this.withs = new ArrayList(); } public WithClause recursive(boolean recur) { this.recursive = recur; return this; } @Override public String sql(ISemantext sctx) throws TransException { return isNull(withs) ? "" : recursive ? withs.stream() .filter(e -> e != null) .map((q) -> { return Stream.of( "recursive ", ((Tabl)q[0]).sql(sctx), " as (", ((SelectElem) q[1]).sql(sctx), " union all ", ((Query) q[2]).sql(sctx), ")") .filter(e -> e != null) .collect(Collectors.joining()); }) .collect(Collectors.joining(", ", "with ", "")) : withs.stream() .filter(e -> e != null) .map((q) -> { return Stream.of( ((Query)q[0]).alias().sql(sctx), " as (", ((Query) q[0]).sql(sctx), ")") .filter(e -> e != null) .collect(Collectors.joining()); }) .collect(Collectors.joining(", ", "with ", "")); } public WithClause with(String recurTabl, String rootValue, Query subSelect) { if (this.withs == null) this.withs = new ArrayList(); this.withs.add(new AbsPart[] {new Tabl(recurTabl), new SelectElem(ElemType.constant, rootValue), subSelect}); return this; } public WithClause with(Query q0, Query[] qi) { if (this.withs == null) this.withs = new ArrayList(); if (!isNull(q0)) { if (isblank(q0.alias())) Utils.warn("Adding with-table without alias? %s", q0.sql(null));; this.withs.add(new AbsPart[] {q0}); } if (!isNull(qi)) for (Query q : qi) { if (isblank(q.alias())) Utils.warn("Adding with-table without alias? %s", q.sql(null));; this.withs.add(new AbsPart[] {q}); } return this; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy