Добавил алиасы для Join

This commit is contained in:
Struchkov Mark 2022-02-17 19:29:48 +03:00
parent 15f8bc25f8
commit 9c360c2900
2 changed files with 39 additions and 8 deletions

View File

@ -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<Record> dlsJoinTableName = DSL.table(tableName);
Table<Record> 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);

View File

@ -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;
}
}