Skip to content

Commit

Permalink
#224 - fixed now if select is not specified all columns of the root t…
Browse files Browse the repository at this point in the history
…able is returned.
  • Loading branch information
grabdoc committed Jan 31, 2024
1 parent 91800b7 commit e503621
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/main/java/com/homihq/db2rest/mybatis/MyBatisTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.mybatis.dynamic.sql.AliasableSqlTable;
import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.SqlTable;
import schemacrawler.schema.Column;
import schemacrawler.schema.Table;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.homihq.db2rest.exception.InvalidTableException;
import com.homihq.db2rest.mybatis.MyBatisTable;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.SqlColumn;
Expand All @@ -25,10 +26,12 @@
import static org.mybatis.dynamic.sql.select.SelectDSL.select;



@Builder
@AllArgsConstructor
@NoArgsConstructor
@Data
@Slf4j
public class ReadContext {

String schemaName;
Expand Down Expand Up @@ -97,10 +100,30 @@ else if(isCountQuery()) {
queryExpressionDSL = select(count()).from(from, from.getAlias());
}
else{
log.info("Setting Select projections with columns - {}", columns);
from = getRootTable();
queryExpressionDSL = select(columns).from(from, from.getAlias());

queryExpressionDSL = select(getSelectionColumns(getRootTable(), columns)).from(from, from.getAlias());
}

}

private List<BasicColumn> getSelectionColumns(MyBatisTable rootTable, List<BasicColumn> columns) {
if(columns.isEmpty()) {
return
rootTable.getTable().getColumns()
.stream()
.peek(column -> {
log.debug("Column Data type - {}", column.getColumnDataType());
log.debug("Column Data type - {}", column.getColumnDataType().getJavaSqlType());
log.debug("Data type - {}", column.getType());
log.debug("Data type - {}", column.getType().getJavaSqlType());
})
.map(column -> (BasicColumn)SqlColumn.of(column.getName(), rootTable))//TODO - user jdbctype
.toList();
}

return columns;
}

private MyBatisTable getRootTable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,11 @@ private List<MyBatisTable> prepareTables(ReadContext context) {
}



log.info("context.select - {}", context.select);

//split to get all fragments
String [] tabCols = context.select.split(";");



log.info("tabCols - {}", tabCols.length);

//process the fragments
Expand All @@ -76,8 +73,6 @@ private List<MyBatisTable> prepareTables(ReadContext context) {
table.setRoot(true);
}

//TODO - multiple root tables and no other table = union query, else unsupported exception

}
tables.addAll(myBatisTables);

Expand Down

0 comments on commit e503621

Please sign in to comment.