Skip to content

Commit

Permalink
Merge pull request #251 from AdaptiveScale/release-2.5.3
Browse files Browse the repository at this point in the history
Release 2.5.3
  • Loading branch information
nbesimi authored Aug 29, 2024
2 parents a5a3787 + be41342 commit cbe0f4d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repositories {

allprojects {
group = 'com.adaptivescale'
version = '2.5.2'
version = '2.5.3'
sourceCompatibility = 11
targetCompatibility = 11
}
Expand Down
2 changes: 1 addition & 1 deletion cli/src/main/java/com/adaptivescale/rosetta/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
@Slf4j
@CommandLine.Command(name = "cli",
mixinStandardHelpOptions = true,
version = "2.5.2",
version = "2.5.3",
description = "Declarative Database Management - DDL Transpiler"
)
class Cli implements Callable<Void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected void attachViewDDL(Collection<View> views, java.sql.Connection connect

List<String> schemaList = views.stream()
.map(Table::getSchema)
.map(schema -> "'" + schema + "'") // Wrap each string in single quotes
.map(schema -> "'" + schema + "'")
.collect(Collectors.toList());

// Fetch view info only (M is for Materialized, V is Logical view)
Expand All @@ -60,7 +60,7 @@ protected void attachViewDDL(Collection<View> views, java.sql.Connection connect
Optional<Map<String, Object>> record = records.stream().filter(rec -> view.getName().equals(rec.get("object_name")) && view.getSchema().equals(rec.get("schema_name"))).findAny();

if (record.isPresent()) {
view.setCode(record.get().get("definition").toString());
view.setCode(fetchDdl(connection, view.getSchema(), view.getName()));
if (record.get().get("obj_kind").equals("M")) {
view.setMaterialized(true);
}
Expand All @@ -73,11 +73,46 @@ protected void attachViewDDL(Collection<View> views, java.sql.Connection connect

// Skip view due to incomplete code, safety issue
views.removeIf(view -> {
if (view.getCode() == null || view.getCode().isEmpty() || view.getCode().length() == 256) {
if (view.getCode() == null || view.getCode().isEmpty()) {
log.warn("Skipping view due to incomplete code: {}.{}", view.getSchema(), view.getName());
return true;
}
return false;
});
}

private String fetchDdl(java.sql.Connection connection, String schemaName, String viewName) {
String queryTemplate = "SHOW %s.%s;";
try (Statement statement = connection.createStatement()) {
String query = String.format(queryTemplate, schemaName, viewName);
ResultSet resultSet = statement.executeQuery(query);
List<Map<String, Object>> records = QueryHelper.mapRecords(resultSet);

if (!records.isEmpty()) {
return extractDefinition(records.get(0).get("ddl").toString());
}

} catch (SQLException e) {
log.warn("Skipping processing views due to error: {}", e.getMessage());
}

return null;
}

private String extractDefinition(String sql) {
String upperSql = sql.toUpperCase();
int selectIndex = upperSql.indexOf("AS");
int endIndex = sql.indexOf(';');

// If a semicolon is not found, default to the end of the string
if (endIndex == -1) {
endIndex = sql.length();
}

if (selectIndex != -1) {
return sql.substring(selectIndex + "AS".length(), endIndex).trim();
}

return null;
}
}

0 comments on commit cbe0f4d

Please sign in to comment.