Skip to content

Commit

Permalink
fix(plugin): fix get version (#887)
Browse files Browse the repository at this point in the history
  • Loading branch information
qianmoQ authored Dec 9, 2024
2 parents a2797d2 + 2692fb0 commit 9d11c52
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ expression
| expression OR expression #OrExpression
| expression NOT? BETWEEN expression AND expression #BetweenExpression
| expression NOT? IN ('(' expression (',' expression)* ')') #InExpression
| expression NOT? LIKE expression #LikeExpression // 添加这一行
| expression NOT? LIKE expression #LikeExpression
| expression IS NOT? NULL #IsNullExpression
| expression IS NOT? (TRUE | FALSE) #IsBooleanExpression
| NOT expression #NotExpression
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.edurt.datacap.sql.formatter;

import io.edurt.datacap.sql.node.Expression;

import static io.edurt.datacap.sql.formatter.SQLFormatter.INDENT;

public class ExpressionFormatter
{
public ExpressionFormatter()
{}

public static String formatExpression(Expression expr, int depth)
{
if (expr == null) {
return "null";
}

StringBuilder sb = new StringBuilder();
String currentIndent = INDENT.repeat(depth);

// Start expression with current depth indentation
sb.append(currentIndent).append("Expression {\n");

// Content should be indented one more level
String contentIndent = INDENT.repeat(depth + 1);
sb.append(contentIndent).append("type: ").append(expr.getType()).append(",\n");
sb.append(contentIndent).append("value: \"").append(expr.getValue()).append("\"");

if (expr.getChildren() != null && !expr.getChildren().isEmpty()) {
sb.append(",\n");
sb.append(contentIndent).append("children: [\n");

for (int i = 0; i < expr.getChildren().size(); i++) {
Expression child = expr.getChildren().get(i);
// Children should be indented one more level than the children array
sb.append(formatExpression(child, depth + 2));

if (i < expr.getChildren().size() - 1) {
sb.append(",");
}
sb.append("\n");
}

sb.append(contentIndent).append("]");
}

sb.append("\n").append(currentIndent).append("}");
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.edurt.datacap.sql.formatter;

import io.edurt.datacap.sql.statement.SQLStatement;
import io.edurt.datacap.sql.statement.SelectStatement;

public abstract class SQLFormatter
{
protected static final String INDENT = " ";

public String format(SQLStatement statement)
{
if (statement instanceof SelectStatement) {
return new SelectFormatter().format(statement);
}
else {
throw new UnsupportedOperationException("Unsupported statement: " + statement);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package io.edurt.datacap.sql.formatter;

import io.edurt.datacap.sql.node.Expression;
import io.edurt.datacap.sql.node.clause.LimitClause;
import io.edurt.datacap.sql.node.element.OrderByElement;
import io.edurt.datacap.sql.node.element.SelectElement;
import io.edurt.datacap.sql.node.element.TableElement;
import io.edurt.datacap.sql.statement.SQLStatement;
import io.edurt.datacap.sql.statement.SelectStatement;

import static io.edurt.datacap.sql.formatter.ExpressionFormatter.formatExpression;

public class SelectFormatter
extends SQLFormatter
{
@Override
public String format(SQLStatement statement)
{
if (statement == null) {
return "null";
}

SelectStatement selectStatement = (SelectStatement) statement;

StringBuilder sb = new StringBuilder();
String indent = INDENT;

sb.append("SelectStatement {\n");

// Format SELECT elements
if (selectStatement.getSelectElements() != null && !selectStatement.getSelectElements().isEmpty()) {
sb.append(indent).append("selectElements: [\n");
for (SelectElement element : selectStatement.getSelectElements()) {
sb.append(indent).append(INDENT).append("SelectElement {\n");
if (element.getColumn() != null) {
sb.append(indent).append(INDENT).append(INDENT).append("column: \"").append(element.getColumn()).append("\",\n");
}
if (element.getAlias() != null) {
sb.append(indent).append(INDENT).append(INDENT).append("alias: \"").append(element.getAlias()).append("\",\n");
}
if (element.getExpression() != null) {
sb.append(indent).append(INDENT).append(INDENT).append("expression: ");
// 移除前面的换行,并调整缩进级别
String expressionStr = formatExpression(element.getExpression(), 4);
sb.append(expressionStr.trim()).append(",\n");
}
sb.append(indent).append(INDENT).append("},\n");
}
sb.append(indent).append("],\n");
}

// Format FROM sources
if (selectStatement.getFromSources() != null && !selectStatement.getFromSources().isEmpty()) {
sb.append(indent).append("fromSources: [\n");
for (TableElement table : selectStatement.getFromSources()) {
sb.append(indent).append(INDENT).append("TableElement {\n");
if (table.getTableName() != null) {
sb.append(indent).append(INDENT).append(INDENT).append("tableName: \"").append(table.getTableName()).append("\",\n");
}
if (table.getAlias() != null) {
sb.append(indent).append(INDENT).append(INDENT).append("alias: \"").append(table.getAlias()).append("\",\n");
}
if (table.getSubquery() != null) {
sb.append(indent).append(INDENT).append(INDENT).append("subquery: {...},\n");
}
sb.append(indent).append(INDENT).append("},\n");
}
sb.append(indent).append("],\n");
}

// Format WHERE clause
if (selectStatement.getWhereClause() != null) {
sb.append(indent).append("whereClause: ");
String expressionStr = formatExpression(selectStatement.getWhereClause(), 1);
sb.append(expressionStr.trim()).append(",\n");
}

// Format GROUP BY elements
if (selectStatement.getGroupByElements() != null && !selectStatement.getGroupByElements().isEmpty()) {
sb.append(indent).append("groupByElements: [\n");
for (Expression expr : selectStatement.getGroupByElements()) {
sb.append(indent).append(INDENT);
String expressionStr = formatExpression(expr, 2);
sb.append(expressionStr.trim()).append(",\n");
}
sb.append(indent).append("],\n");
}

// Format HAVING clause
if (selectStatement.getHavingClause() != null) {
sb.append(indent).append("havingClause: ");
String expressionStr = formatExpression(selectStatement.getHavingClause(), 1);
sb.append(expressionStr.trim()).append(",\n");
}

// Format ORDER BY elements
if (selectStatement.getOrderByElements() != null && !selectStatement.getOrderByElements().isEmpty()) {
sb.append(indent).append("orderByElements: [\n");
for (OrderByElement element : selectStatement.getOrderByElements()) {
sb.append(indent).append(INDENT).append("OrderByElement {\n");
if (element.getExpression() != null) {
sb.append(indent).append(INDENT).append(INDENT).append("expression: ");
String expressionStr = formatExpression(element.getExpression(), 4);
sb.append(expressionStr.trim()).append(",\n");
}
sb.append(indent).append(INDENT).append(INDENT).append("ascending: ").append(element.isAscending()).append("\n");
sb.append(indent).append(INDENT).append("},\n");
}
sb.append(indent).append("],\n");
}

// Format LIMIT clause
if (selectStatement.getLimitClause() != null) {
LimitClause limit = selectStatement.getLimitClause();
sb.append(indent).append("limitClause: {\n");
sb.append(indent).append(INDENT).append("limit: ").append(limit.getLimit()).append(",\n");
sb.append(indent).append(INDENT).append("offset: ").append(limit.getOffset()).append("\n");
sb.append(indent).append("}\n");
}

sb.append("}");
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ private static boolean containsPluginClass(JarFile jarFile)
// Read all class files
List<JarEntry> classEntries = jarFile.stream()
.filter(entry -> !entry.isDirectory() && entry.getName().endsWith(".class"))
.filter(entry -> !entry.getName().endsWith("module-info.class"))
.collect(Collectors.toList());

for (JarEntry entry : classEntries) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Map;
import java.util.Optional;

/**
Expand Down Expand Up @@ -161,7 +162,12 @@ private void updateSourceVersion(SourceEntity source, Response response)
return ((ArrayList<?>) version).get(0).toString();
}
else if (version instanceof JsonNode) {
return ((JsonNode) version).get("version").asText();
String columnName = response.getHeaders().get(0);
return ((JsonNode) version).get(columnName).asText();
}
else if (version instanceof Map) {
String columnName = response.getHeaders().get(0);
return ((Map<?, ?>) version).get(columnName).toString();
}
return version.toString();
})
Expand Down
9 changes: 6 additions & 3 deletions core/datacap-ui/src/views/pages/admin/source/SourceInfo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,7 @@ export default defineComponent({
this.testInfo.connected = true
this.testInfo.successful = true
this.testInfo.message = null
this.formState.version = response.data?.columns[0]?.version
? response.data?.columns[0]?.version
: response.data?.columns[0]?.result
this.formState.version = this.extractVersion(response.data)
}
else {
this.testInfo.message = response.message
Expand Down Expand Up @@ -427,6 +425,11 @@ export default defineComponent({
.filter(group => group && typeof group === 'string')
this.configureTabs = [...new Set(validGroups)]
},
extractVersion(json: string)
{
const columnName = json['headers'][0]
return json['columns'][0][columnName]
}
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
public class DmService
implements PluginService
{
@Override
public String validator()
{
return "SELECT DB_VERSION AS version\n" +
"FROM V$INSTANCE";
}

@Override
public String driver()
{
Expand Down Expand Up @@ -41,7 +48,7 @@ public String url(Configure configure)
buffer.append(configure.getDatabase().get());
}
if (configure.getSsl().isPresent()) {
if (!configure.getDatabase().isPresent()) {
if (configure.getDatabase().isEmpty()) {
buffer.append(String.format("?ssl=%s", configure.getSsl().get()));
}
else {
Expand All @@ -54,7 +61,7 @@ public String url(Configure configure)
.stream()
.map(value -> String.format("%s=%s", value.getKey(), value.getValue()))
.collect(Collectors.toList());
if (!configure.getDatabase().isPresent()) {
if (configure.getDatabase().isEmpty()) {
buffer.append("?");
}
else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.edurt.datacap.test.formatter;

import io.edurt.datacap.sql.SQLParser;
import io.edurt.datacap.sql.formatter.SelectFormatter;
import io.edurt.datacap.sql.statement.SQLStatement;
import org.junit.Test;

import static org.junit.Assert.assertNotNull;

public class SelectStatementTest
{
@Test
public void test()
{
String sql = "SELECT id, name, COUNT(1) as count FROM users WHERE age > 18 and name = 12 GROUP BY id, name HAVING count > 1 ORDER BY id DESC LIMIT 10";
SQLStatement statement = SQLParser.parse(sql);
SelectFormatter formatter = new SelectFormatter();
assertNotNull(formatter.format(statement));
}
}

0 comments on commit 9d11c52

Please sign in to comment.