DefaultForeignKeyTermFragmentBuilder implements ForeignKeyTermFragmentBuilder { @SuppressWarnings("all") public SqlFragments createFragments(String tableName, ForeignKeyMetadata key, List<Term> terms) { PrepareSqlFragments sqlFragments = PrepareSqlFragments.of() .addSql("exists("); key.getTarget() .findFeature(QuerySqlBuilder.ID) .ifPresent(builder -> { QueryOperatorParameter parameter = new QueryOperatorParameter(); parameter.getSelect().add(NativeSelectColumn.of("1")); parameter.setFrom(key.getTarget().getName()); parameter.setFromAlias(key.getAlias()); for (ForeignKeyMetadata middleForeignKey : key.getMiddleForeignKeys()) { Join join = new Join(); for (ForeignKeyColumn column : middleForeignKey.getColumns()) { PrepareSqlFragments condition = PrepareSqlFragments.of(); condition.addSql(column.getSourceColumn().getFullName(key.getAlias())); condition.addSql("=").addSql(column.getTargetColumn().getFullName()); join.getTerms().add(SqlTerm.of(condition.toRequest().getSql())); } if (middleForeignKey.getTerms() != null) { join.getTerms().addAll(middleForeignKey.getTerms()); } join.setAlias(middleForeignKey.getTarget().getName()); join.setTarget(middleForeignKey.getTarget().getFullName()); join.setType(middleForeignKey.getJoinType()); join.addAlias(middleForeignKey.getName(), middleForeignKey.getAlias(), middleForeignKey.getTarget().getAlias()); parameter.getJoins().add(join); } for (ForeignKeyColumn column : key.getColumns()) { PrepareSqlFragments condition = PrepareSqlFragments.of(); if (column.getSourceColumn().getOwner().getFullName().equals(key.getSource().getFullName())) { condition.addSql(column.getSourceColumn().getFullName(tableName)); } else { condition.addSql(column.getSourceColumn().getFullName()); } condition.addSql("=").addSql(column.getTargetColumn().getFullName(key.getAlias())); parameter.getWhere().add(SqlTerm.of(condition.toRequest().getSql())); } Term term = new Term(); if (key.getTerms() != null && !key.getTerms().isEmpty()) { term.setTerms(new ArrayList<>(key.getTerms())); term.nest().setTerms(terms); } else { term.setTerms(terms); } parameter.getWhere().add(term); SqlRequest request = builder.build(parameter); if (request.isNotEmpty()) { sqlFragments.addSql(request.getSql()).addParameter(request.getParameters()); } }); return sqlFragments .addSql(")"); }  @SuppressWarnings("all") SqlFragments createFragments(String tableName, ForeignKeyMetadata key, List<Term> terms); static final DefaultForeignKeyTermFragmentBuilder INSTANCE; }
@Test public void test() { SqlFragments fragments = builder .createFragments("test", foreignKeyMetadata, Query.of().where("detail.comment", 1) .getParam().getTerms()); SqlRequest request = fragments.toRequest(); System.out.println(fragments.toRequest()); Assert.assertEquals(request.getSql(), "exists( select 1 from PUBLIC.detail detail where test.\"ID\" = detail.\"ID\" and ( ( detail.\"ID\" = ? or detail.\"ID\" = ? ) and ( detail.\"COMMENT\" = ? ) ) )"); }