Skip to content

Commit

Permalink
Merge pull request #600 from kdhrubo/feature/597_598_jsonb_enhancements
Browse files Browse the repository at this point in the history
Feature/597 598 jsonb enhancements
  • Loading branch information
kdhrubo committed May 23, 2024
2 parents 7718e50 + 061357e commit 1013e01
Show file tree
Hide file tree
Showing 25 changed files with 72 additions and 39 deletions.
2 changes: 1 addition & 1 deletion api-rest/pom-oracle9i.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github</groupId>
<artifactId>db2rest-parent</artifactId>
<version>1.0.4</version>
<version>1.0.5-RC1</version>
<relativePath>../pom.xml</relativePath>

<!-- lookup parent from repository -->
Expand Down
2 changes: 1 addition & 1 deletion api-rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github</groupId>
<artifactId>db2rest-parent</artifactId>
<version>1.0.4</version>
<version>1.0.5-RC1</version>
<relativePath>../pom.xml</relativePath>

<!-- lookup parent from repository -->
Expand Down
2 changes: 1 addition & 1 deletion auth/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github</groupId>
<artifactId>db2rest-parent</artifactId>
<version>1.0.4</version>
<version>1.0.5-RC1</version>
<relativePath>../pom.xml</relativePath>

<!-- lookup parent from repository -->
Expand Down
2 changes: 1 addition & 1 deletion db2rest-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github</groupId>
<artifactId>db2rest-parent</artifactId>
<version>1.0.4</version>
<version>1.0.5-RC1</version>
<relativePath>../pom.xml</relativePath>

<!-- lookup parent from repository -->
Expand Down
2 changes: 1 addition & 1 deletion mariadb-dialect/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github</groupId>
<artifactId>db2rest-parent</artifactId>
<version>1.0.4</version>
<version>1.0.5-RC1</version>
<relativePath>../pom.xml</relativePath>

<!-- lookup parent from repository -->
Expand Down
2 changes: 1 addition & 1 deletion mongo-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github</groupId>
<artifactId>db2rest-parent</artifactId>
<version>1.0.4</version>
<version>1.0.5-RC1</version>
<relativePath>../pom.xml</relativePath>

<!-- lookup parent from repository -->
Expand Down
2 changes: 1 addition & 1 deletion mysql-dialect/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github</groupId>
<artifactId>db2rest-parent</artifactId>
<version>1.0.4</version>
<version>1.0.5-RC1</version>
<relativePath>../pom.xml</relativePath>

<!-- lookup parent from repository -->
Expand Down
2 changes: 1 addition & 1 deletion oracle-dialect/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github</groupId>
<artifactId>db2rest-parent</artifactId>
<version>1.0.4</version>
<version>1.0.5-RC1</version>
<relativePath>../pom.xml</relativePath>

<!-- lookup parent from repository -->
Expand Down
2 changes: 1 addition & 1 deletion oracle9i-dialect/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github</groupId>
<artifactId>db2rest-parent</artifactId>
<version>1.0.4</version>
<version>1.0.5-RC1</version>
<relativePath>../pom.xml</relativePath>

<!-- lookup parent from repository -->
Expand Down
2 changes: 1 addition & 1 deletion pg-dialect/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github</groupId>
<artifactId>db2rest-parent</artifactId>
<version>1.0.4</version>
<version>1.0.5-RC1</version>
<relativePath>../pom.xml</relativePath>

<!-- lookup parent from repository -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private Object convertToJson(Object value, String columnDataTypeName) {
}

@Override
public Map convertJsonToMap(Object object) {
public Object convertJsonToVO(Object object) {

if(Objects.nonNull(object)) {

Expand All @@ -106,7 +106,7 @@ public Map convertJsonToMap(Object object) {
String val = pGobject.getValue();

try {
return objectMapper.readValue(val, Map.class);
return objectMapper.readValue(val, Object.class);
} catch (JsonProcessingException e) {
throw new GenericDataAccessException("Error converting to JSON type - " + e.getLocalizedMessage());
}
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.github</groupId>
<artifactId>db2rest-parent</artifactId>
<version>1.0.4</version>
<version>1.0.5-RC1</version>
<packaging>pom</packaging>
<name>DB2Rest parent</name>
<description>An Open Source instant REST Data API for databases. This is the DB2Rest parent Maven project.</description>
Expand Down
2 changes: 1 addition & 1 deletion rdbms-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github</groupId>
<artifactId>db2rest-parent</artifactId>
<version>1.0.4</version>
<version>1.0.5-RC1</version>
<relativePath>../pom.xml</relativePath>

<!-- lookup parent from repository -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,5 @@ else if(java.sql.Timestamp.class == type) {

default List<String> convertToStringArray(Object object) {return List.of();}

default Map convertJsonToMap(Object object) {return new HashMap();}
default Object convertJsonToVO(Object object) {return null;}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.homihq.db2rest.jdbc.config.model;

public record DbAlias(String name, String alias) { }
public record DbAlias(String name, String alias, String jsonParts) { }
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package com.homihq.db2rest.jdbc.config.model;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public record DbColumn(String tableName, String name, String alias, String tableAlias,
boolean pk, String columnDataTypeName, boolean generated, boolean autoIncremented
,Class<?> typeMappedClass , String coverChar) {
,Class<?> typeMappedClass , String coverChar, String jsonParts) {

@Deprecated
private String getQuotedName() {
return coverChar + name + coverChar;
if(StringUtils.isBlank(jsonParts)) {
return coverChar + name + coverChar;
}
else {
return coverChar + name + coverChar + jsonParts;
}
}

@Deprecated
Expand All @@ -23,10 +30,15 @@ public String render() {

@Deprecated
public String renderWithAlias() {
if(StringUtils.isNotBlank(alias))
return tableAlias + "."+ getQuotedName() + " as " + getQuotedAlias();

return tableAlias + "."+ getQuotedName() + " " + alias;
String firstPart = tableAlias + "."+ getQuotedName();

if(StringUtils.isNotBlank(alias)) {

return firstPart + " as " + getQuotedAlias();
}

return firstPart;
}


Expand All @@ -53,13 +65,17 @@ public boolean isStringFamily() {
"VARCHAR","TEXT", "VARCHAR2");
}

public DbColumn copyWithAlias(String columnAlias) {
return new DbColumn(tableName, name, columnAlias, tableAlias,
pk, columnDataTypeName, generated, autoIncremented, typeMappedClass, coverChar);
public DbColumn copyWithAlias(DbAlias columnAlias) {

log.debug("columnDataTypeName - {}", columnDataTypeName);
log.debug("full Column name - {}", columnAlias.jsonParts());

return new DbColumn(tableName, name, columnAlias.alias(), tableAlias,
pk, columnDataTypeName, generated, autoIncremented, typeMappedClass, coverChar, columnAlias.jsonParts());
}

public DbColumn copyWithTableAlias(String tableAlias) {
return new DbColumn(tableName, name, alias, tableAlias,
pk, columnDataTypeName, generated, autoIncremented, typeMappedClass, coverChar);
pk, columnDataTypeName, generated, autoIncremented, typeMappedClass, coverChar, "");
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.homihq.db2rest.jdbc.config.model;

import com.homihq.db2rest.core.exception.InvalidColumnException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.util.List;


@Slf4j
public record DbTable(String schema, String name, String fullName, String alias,
List<DbColumn> dbColumns, String type, String coverChar) {

Expand All @@ -23,28 +24,44 @@ public DbTable copyWithAlias(String tableAlias) {

public DbColumn buildColumn(String columnName) {


DbAlias dbAlias = getAlias(columnName);

log.info("Db alias - {}", dbAlias);

return getDbColumn(dbAlias);
}

private DbColumn getDbColumn(DbAlias dbAlias) {
return
this.dbColumns.stream()
.filter(dbColumn -> StringUtils.equalsAnyIgnoreCase(dbAlias.name(), dbColumn.name()))
.map(dbColumn -> dbColumn.copyWithAlias(dbAlias.alias()))
.map(dbColumn -> dbColumn.copyWithAlias(dbAlias))
.findFirst().orElseThrow(() -> new InvalidColumnException(name,dbAlias.name()));
}

public DbAlias getAlias(String name) {
private DbAlias getAlias(String name) {
String [] aliasParts = name.split(":");

String columnName = aliasParts[0];
String colName = columnName;
String jsonParts = "";

if(StringUtils.contains(columnName, "->")) {
colName = columnName.substring(0, columnName.indexOf("->"));
jsonParts = columnName.substring(columnName.indexOf("->"));
}
else if(StringUtils.contains(columnName, "->>")) {
colName = columnName.substring(0, columnName.indexOf("->>"));
jsonParts = columnName.substring(columnName.indexOf("->>"));
}

log.info("Col name - {}", colName);

if(aliasParts.length == 2) {
return new DbAlias(aliasParts[0], aliasParts[1]);
return new DbAlias(colName, aliasParts[1], jsonParts);
}
else {
return new DbAlias(aliasParts[0], "");
return new DbAlias(colName, "", jsonParts);
}
}

Expand Down
2 changes: 1 addition & 1 deletion rdbms-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github</groupId>
<artifactId>db2rest-parent</artifactId>
<version>1.0.4</version>
<version>1.0.5-RC1</version>
<relativePath>../pom.xml</relativePath>

<!-- lookup parent from repository -->
Expand Down
2 changes: 1 addition & 1 deletion rdbms-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github</groupId>
<artifactId>db2rest-parent</artifactId>
<version>1.0.4</version>
<version>1.0.5-RC1</version>
<relativePath>../pom.xml</relativePath>

<!-- lookup parent from repository -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected Object getColumnValue(ResultSet rs, int index) throws SQLException {
return dialect.convertToStringArray(rs.getArray(index));
}
if(StringUtils.equalsAnyIgnoreCase(columnType, "json","jsonb")) { //handle pg jsonb, json
return dialect.convertJsonToMap(rs.getObject(index));
return dialect.convertJsonToVO(rs.getObject(index));
}

return super.getColumnValue(rs, index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private List<DbColumn> getAllColumns(DatabaseMetaData databaseMetaData, String c
typeName,
StringUtils.equalsAnyIgnoreCase(isGenerated,"YES"),
StringUtils.equalsAnyIgnoreCase(isAutoIncrement,"YES"),
javaType, "`"
javaType, "`", ""
);

dbColumns.add(dbColumn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private List<DbColumn> getAllColumns(DatabaseMetaData databaseMetaData, String c
typeName,
StringUtils.equalsAnyIgnoreCase(isGenerated,"YES"),
StringUtils.equalsAnyIgnoreCase(isAutoIncrement,"YES"),
javaType, "`"
javaType, "`", ""
);

dbColumns.add(dbColumn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private List<DbColumn> getAllColumns(DatabaseMetaData databaseMetaData, String c
typeName,
StringUtils.equalsAnyIgnoreCase(isGenerated,"YES"),
StringUtils.equalsAnyIgnoreCase(isAutoIncrement,"YES"),
javaType, "\""
javaType, "\"", ""

);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private List<DbColumn> getAllColumns(DatabaseMetaData databaseMetaData, String c
typeName,
StringUtils.equalsAnyIgnoreCase(isGenerated,"YES"),
StringUtils.equalsAnyIgnoreCase(isAutoIncrement,"YES"),
javaType, "\""
javaType, "\"" , ""
);

dbColumns.add(dbColumn);
Expand Down
2 changes: 1 addition & 1 deletion rest-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github</groupId>
<artifactId>db2rest-parent</artifactId>
<version>1.0.4</version>
<version>1.0.5-RC1</version>
<relativePath>../pom.xml</relativePath>

<!-- lookup parent from repository -->
Expand Down

0 comments on commit 1013e01

Please sign in to comment.