Skip to content

Commit

Permalink
refactor(dialect): converted Dialect from an interface into an abstra…
Browse files Browse the repository at this point in the history
…ct class, pulled up common variables within each subclass to ensure that all subclasses have common functionality.
  • Loading branch information
John Birdwell committed Nov 26, 2024
1 parent 352e1a9 commit b5a872e
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
public class MariaDBDialect implements Dialect {

private final ObjectMapper objectMapper;

private String coverChar = "`";
public class MariaDBDialect extends Dialect {
public MariaDBDialect(ObjectMapper objectMapper) {
super(objectMapper, "`");
}

@Override
public boolean isSupportedDb(String productName, int majorVersion) {
Expand All @@ -37,7 +35,7 @@ public void processTypes(DbTable table, List<String> insertableColumns, Map<Stri

if (StringUtils.equalsAnyIgnoreCase(columnDataTypeName, "json")) {

data.put(columnName, objectMapper.writeValueAsString(value));
data.put(columnName, getObjectMapper().writeValueAsString(value));
}

}
Expand All @@ -49,7 +47,7 @@ public void processTypes(DbTable table, List<String> insertableColumns, Map<Stri
}

private String getQuotedName(String name) {
return coverChar + name + coverChar;
return getCoverChar() + name + getCoverChar();
}

@Override
Expand Down Expand Up @@ -77,6 +75,4 @@ public String getAliasedName(DbColumn dbColumn, boolean deleteOp) {

return dbColumn.tableAlias() + "."+ dbColumn.name();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
import com.homihq.db2rest.jdbc.config.model.Database;
import com.homihq.db2rest.jdbc.config.model.DbTable;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
public class MsSQLServerDialect implements Dialect {

private static final String COVER_CHAR = "\"";

private final ObjectMapper objectMapper;
@Slf4j
public class MsSQLServerDialect extends Dialect {
public MsSQLServerDialect(ObjectMapper objectMapper) {
super(objectMapper, "\"");
}

// https://github.com/Microsoft/mssql-jdbc/issues/245
@Override
Expand All @@ -37,7 +37,7 @@ public void processTypes(DbTable table, List<String> insertableColumns, Map<Stri
String columnDataTypeName = table.getColumnDataTypeName(columnName);

if (StringUtils.equalsAnyIgnoreCase(columnDataTypeName, "json")) {
data.put(columnName, objectMapper.writeValueAsString(value));
data.put(columnName, getObjectMapper().writeValueAsString(value));
}
}
} catch (Exception exception) {
Expand All @@ -59,7 +59,7 @@ public String renderTableNameWithoutAlias(DbTable table) {
}

private String getQuotedName(String name) {
return COVER_CHAR + name + COVER_CHAR;
return getCoverChar() + name + getCoverChar();
}

@Override
Expand All @@ -81,5 +81,4 @@ public String getReadSqlTemplate() {
public String getUpdateSqlTemplate() {
return "update-mssql";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
public class MySQLDialect implements Dialect {

private final ObjectMapper objectMapper;

private String coverChar = "`";

public class MySQLDialect extends Dialect {
public MySQLDialect(ObjectMapper objectMapper) {
super(objectMapper, "`");
}

@Override
public boolean isSupportedDb(String productName, int majorVersion) {
Expand All @@ -39,7 +36,7 @@ public void processTypes(DbTable table, List<String> insertableColumns, Map<Stri

if (StringUtils.equalsAnyIgnoreCase(columnDataTypeName, "json")) {

data.put(columnName, objectMapper.writeValueAsString(value));
data.put(columnName, getObjectMapper().writeValueAsString(value));
}

}
Expand All @@ -51,7 +48,7 @@ public void processTypes(DbTable table, List<String> insertableColumns, Map<Stri
}

private String getQuotedName(String name) {
return coverChar + name + coverChar;
return getCoverChar() + name + getCoverChar();
}

@Override
Expand All @@ -63,8 +60,4 @@ public String renderTableName(DbTable table, boolean containsWhere, boolean dele
public String renderTableNameWithoutAlias(DbTable table) {
return getQuotedName(table.schema()) + "." + getQuotedName(table.name());
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
public class OracleDialect implements Dialect {

private final ObjectMapper objectMapper;

private String coverChar = "\"";
public class OracleDialect extends Dialect {
public OracleDialect(ObjectMapper objectMapper) {
super(objectMapper, "\"");
}

@Override
public boolean isSupportedDb(String productName, int majorVersion) {
Expand Down Expand Up @@ -47,7 +45,7 @@ public void processTypes(DbTable table, List<String> insertableColumns, Map<Stri

if (StringUtils.equalsAnyIgnoreCase(columnDataTypeName, "json")) {

data.put(columnName, objectMapper.writeValueAsString(value));
data.put(columnName, getObjectMapper().writeValueAsString(value));
} else if (StringUtils.equalsAnyIgnoreCase(columnDataTypeName, "TIMESTAMP(6)")) {
LocalDateTime v = convertToLocalDateTime((String) value);
data.put(columnName, v);
Expand All @@ -69,7 +67,7 @@ private LocalDateTime convertToLocalDateTime(String value) {
}

private String getQuotedName(String name) {
return coverChar + name + coverChar;
return getCoverChar() + name + getCoverChar();
}

@Override
Expand All @@ -81,6 +79,4 @@ public String renderTableName(DbTable table, boolean containsWhere, boolean dele
public String renderTableNameWithoutAlias(DbTable table) {
return getQuotedName(table.schema()) + "." + getQuotedName(table.name());
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
public class OracleDialect implements Dialect {

private final ObjectMapper objectMapper;

private String coverChar = "\"";

public class OracleDialect extends Dialect {
public OracleDialect(ObjectMapper objectMapper) {
super(objectMapper, "\"");
}

@Override
public boolean isSupportedDb(String productName, int majorVersion) {
Expand Down Expand Up @@ -48,7 +45,7 @@ public void processTypes(DbTable table, List<String> insertableColumns, Map<Stri

if (StringUtils.equalsAnyIgnoreCase(columnDataTypeName, "json")) {

data.put(columnName, objectMapper.writeValueAsString(value));
data.put(columnName, getObjectMapper().writeValueAsString(value));
}

}
Expand All @@ -59,10 +56,8 @@ public void processTypes(DbTable table, List<String> insertableColumns, Map<Stri

}



private String getQuotedName(String name) {
return coverChar + name + coverChar;
return getCoverChar() + name + getCoverChar();
}

@Override
Expand All @@ -74,7 +69,4 @@ public String renderTableName(DbTable table, boolean containsWhere, boolean dele
public String renderTableNameWithoutAlias(DbTable table) {
return getQuotedName(table.schema()) + "." + getQuotedName(table.name());
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
import java.util.*;


@RequiredArgsConstructor
@Slf4j
public class PostGreSQLDialect implements Dialect {

private final ObjectMapper objectMapper;
private String coverChar = "\"";
public class PostGreSQLDialect extends Dialect {
public PostGreSQLDialect(ObjectMapper objectMapper) {
super(objectMapper, "\"");
}

//Use during insert, bulk-insert, update
@Override
Expand Down Expand Up @@ -100,7 +99,7 @@ private Object convertToJson(Object value, String columnDataTypeName) {
try {
PGobject pGobject = new PGobject();
pGobject.setType(columnDataTypeName);
pGobject.setValue(objectMapper.writeValueAsString(value));
pGobject.setValue(getObjectMapper().writeValueAsString(value));

return pGobject;
} catch (Exception e) {
Expand All @@ -118,7 +117,7 @@ public Object convertJsonToVO(Object object) {
String val = pGobject.getValue();

try {
return objectMapper.readValue(val, Object.class);
return getObjectMapper().readValue(val, Object.class);
} catch (JsonProcessingException e) {
throw new GenericDataAccessException("Error converting to JSON type - " + e.getLocalizedMessage());
}
Expand All @@ -130,7 +129,7 @@ public Object convertJsonToVO(Object object) {
}

private String getQuotedName(String name) {
return coverChar + name + coverChar;
return getCoverChar() + name + getCoverChar();
}

@Override
Expand Down Expand Up @@ -160,9 +159,8 @@ public List<String> convertToStringArray(Object object) {
} catch (Exception e) {
throw new GenericDataAccessException("Error converting to Array type - " + e.getLocalizedMessage());
}

}

return Dialect.super.convertToStringArray(object);
return convertToStringArray(object);
}
}
Loading

0 comments on commit b5a872e

Please sign in to comment.