Skip to content

Commit

Permalink
fix implicit inner join
Browse files Browse the repository at this point in the history
  • Loading branch information
grabdoc committed Feb 2, 2024
1 parent f3f6ae1 commit 36465f4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/homihq/db2rest/mybatis/MyBatisTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

@Getter
@Setter
@ToString(of = {"schemaName", "tableName"})
public class MyBatisTable extends AliasableSqlTable<MyBatisTable> {

String alias;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,25 @@ private void createJoins(ReadContext context, List<MyBatisTable> tableList, int
MyBatisTable root = tableList.get(i);
MyBatisTable child = tableList.get(i + 1);

/*
List<ForeignKey> foreignKeys = schemaManager.getForeignKeysBetween(context.getSchemaName(),
root.getTableName(),
child.getTableName());
*/

List<ForeignKey> foreignKeys = schemaManager.getForeignKeysBetween(root, child);

log.debug("Foreign keys - {}", foreignKeys);

int fk = 0;
for(ForeignKey foreignKey : foreignKeys) {

log.info("ForeignKey reference - {}", foreignKey);
log.debug("ForeignKey reference - {}", foreignKey);

for(ColumnReference columnReference : foreignKey.getColumnReferences()) {

log.info("Column reference - {}", columnReference);
log.debug("Column reference - {}", columnReference);

if(fk == 0) {
context.queryExpressionDSL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ private List<MyBatisTable> createTables(String schemaName, String tableName, St
}
}

log.debug("sName - {}", sName);

if(StringUtils.isNotBlank(sName)) {
MyBatisTable table = schemaManager.findTable(sName, tName, counter);

Expand All @@ -109,6 +111,7 @@ private List<MyBatisTable> createTables(String schemaName, String tableName, St
return List.of(table);
}
else {
log.debug("no schema -> {}", sName);
List<MyBatisTable> tables = schemaManager.findTables(tName);

for(MyBatisTable table : tables) {
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/com/homihq/db2rest/schema/SchemaManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public void createSchemaCache() {

}
}


}

private String getSchemaName(Table table) {
Expand All @@ -80,6 +78,7 @@ private String getSchemaName(Table table) {

schemaName = table.getSchema().getName();
}
log.info("schemaName -> {}", schemaName);
return schemaName;
}

Expand All @@ -102,6 +101,22 @@ public MyBatisTable getOneTable(String schemaName, String tableName) {

}

public List<ForeignKey> getForeignKeysBetween(MyBatisTable rootTable, MyBatisTable childTable) {
//1. first locate table in the cache
Table table = getTable(rootTable.getSchemaName(), rootTable.getTableName())
.orElseThrow(() -> new InvalidTableException(rootTable.getTableName()));

log.debug("Table - {}", table);
log.debug("Table - {}", table.getImportedForeignKeys());

//2. if foreign keys = null, look for join table option
return table.getImportedForeignKeys().stream()
.filter(fk -> StringUtils.equalsIgnoreCase(
getSchemaName(fk.getParent()), rootTable.getSchemaName())
&& StringUtils.equalsIgnoreCase(fk.getReferencedTable().getName(), childTable.getTableName())).toList();
}

@Deprecated
public List<ForeignKey> getForeignKeysBetween(String schemaName, String rootTable, String childTable) {

//1. first locate table in the cache
Expand Down

0 comments on commit 36465f4

Please sign in to comment.