From 9c360c2900689eaed54d4aac472a3bcca35fe148 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Thu, 17 Feb 2022 19:29:48 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B0=D0=BB=D0=B8=D0=B0=D1=81=D1=8B=20=D0=B4=D0=BB=D1=8F=20Joi?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../haiti/filter/jooq/CriteriaJooqFilter.java | 18 +++++++++--- .../haiti/filter/jooq/join/JoinTable.java | 29 ++++++++++++++++--- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/main/java/dev/struchkov/haiti/filter/jooq/CriteriaJooqFilter.java b/src/main/java/dev/struchkov/haiti/filter/jooq/CriteriaJooqFilter.java index a48005e..8737766 100644 --- a/src/main/java/dev/struchkov/haiti/filter/jooq/CriteriaJooqFilter.java +++ b/src/main/java/dev/struchkov/haiti/filter/jooq/CriteriaJooqFilter.java @@ -192,14 +192,24 @@ public class CriteriaJooqFilter { if (!joinTables.isEmpty()) { for (JoinTable joinTable : joinTables) { final String tableName = joinTable.getTableName(); + String fieldBase = joinTable.getFieldBase(); + String fieldReference = joinTable.getFieldReference(); - final Table dlsJoinTableName = DSL.table(tableName); + Table dlsJoinTableName = DSL.table(tableName); + + if (joinTable.getAlias() != null) { + dlsJoinTableName = dlsJoinTableName.as(joinTable.getAlias()); + fieldReference = joinTable.getAlias() + "." + fieldReference; + } else { + fieldReference = tableName + "." + fieldReference; + } final JoinTypeOperation joinType = joinTable.getJoinTypeOperation(); - final Field fieldReference = field(joinTable.getFieldReference()); - final Field fieldBase = field(joinTable.getFieldBase()); - final Condition on = fieldBase.eq(fieldReference); + final Field dslFieldBase = field(fieldBase); + final Field dslFieldReference = field(fieldReference); + + final Condition on = dslFieldBase.eq(dslFieldReference); switch (joinType) { case LEFT: from = from.leftJoin(dlsJoinTableName).on(on); diff --git a/src/main/java/dev/struchkov/haiti/filter/jooq/join/JoinTable.java b/src/main/java/dev/struchkov/haiti/filter/jooq/join/JoinTable.java index d7e3d2a..114e058 100644 --- a/src/main/java/dev/struchkov/haiti/filter/jooq/join/JoinTable.java +++ b/src/main/java/dev/struchkov/haiti/filter/jooq/join/JoinTable.java @@ -13,27 +13,44 @@ public class JoinTable { private final String fieldBase; private final String fieldReference; private final JoinTypeOperation joinTypeOperation; + private String alias; - private JoinTable(String tableName, String fieldBase, String fieldReference, JoinTypeOperation joinTypeOperation) { + private JoinTable(String tableName, String fieldBase, String fieldReference, JoinTypeOperation joinTypeOperation, String alias) { this.tableName = tableName; this.fieldBase = fieldBase; this.fieldReference = fieldReference; this.joinTypeOperation = joinTypeOperation; + this.alias = alias; } public static JoinTable ofLeft(String tableName, String fieldBase, String fieldReference) { Assert.isNotNull(tableName, fieldBase, fieldReference); - return new JoinTable(tableName, fieldBase, fieldReference, JoinTypeOperation.LEFT); + return new JoinTable(tableName, fieldBase, fieldReference, JoinTypeOperation.LEFT, null); + } + + public static JoinTable ofLeft(String tableName, String fieldBase, String fieldReference, String alias) { + Assert.isNotNull(tableName, fieldBase, fieldReference, alias); + return new JoinTable(tableName, fieldBase, fieldReference, JoinTypeOperation.LEFT, alias); } public static JoinTable onRight(String tableName, String fieldBase, String fieldReference) { Assert.isNotNull(tableName, fieldBase, fieldReference); - return new JoinTable(tableName, fieldBase, fieldReference, JoinTypeOperation.RIGHT); + return new JoinTable(tableName, fieldBase, fieldReference, JoinTypeOperation.RIGHT, null); + } + + public static JoinTable onRight(String tableName, String fieldBase, String fieldReference, String alias) { + Assert.isNotNull(tableName, fieldBase, fieldReference, alias); + return new JoinTable(tableName, fieldBase, fieldReference, JoinTypeOperation.RIGHT, alias); } public static JoinTable of(String tableName, String fieldBase, String fieldReference, JoinTypeOperation joinType) { Assert.isNotNull(tableName, fieldBase, fieldReference); - return new JoinTable(tableName, fieldBase, fieldReference, joinType); + return new JoinTable(tableName, fieldBase, fieldReference, joinType, null); + } + + public static JoinTable of(String tableName, String fieldBase, String fieldReference, JoinTypeOperation joinType, String alias) { + Assert.isNotNull(tableName, fieldBase, fieldReference, alias); + return new JoinTable(tableName, fieldBase, fieldReference, joinType, alias); } public String getTableName() { @@ -52,4 +69,8 @@ public class JoinTable { return joinTypeOperation; } + public String getAlias() { + return alias; + } + }