Skip to content

Commit cd11c65

Browse files
committed
feat: support sqlParse
1 parent 7831d54 commit cd11c65

37 files changed

+2498
-1
lines changed

arex-instrumentation-api/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
<groupId>com.fasterxml.jackson.core</groupId>
3333
<artifactId>jackson-databind</artifactId>
3434
<version>${jackson.version}</version>
35-
<scope>test</scope>
3635
</dependency>
3736
<dependency>
3837
<groupId>com.google.code.gson</groupId>

arex-third-party/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77
<groupId>io.arex</groupId>
88
<version>${revision}</version>
99
</parent>
10+
<dependencies>
11+
<dependency>
12+
<groupId>com.fasterxml.jackson.core</groupId>
13+
<artifactId>jackson-databind</artifactId>
14+
<version>${jackson.version}</version>
15+
</dependency>
16+
<dependency>
17+
<groupId>com.github.jsqlparser</groupId>
18+
<artifactId>jsqlparser</artifactId>
19+
<version>${jsqlparser.version}</version>
20+
</dependency>
21+
</dependencies>
1022
<modelVersion>4.0.0</modelVersion>
1123

1224
<artifactId>arex-third-party</artifactId>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.arex.agent.thirdparty.util;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.node.ArrayNode;
5+
import com.fasterxml.jackson.databind.node.ObjectNode;
6+
7+
/**
8+
* @author niyan
9+
* @date 2024/4/3
10+
* @since 1.0.0
11+
*/
12+
public class JacksonHelperUtil {
13+
14+
public static ObjectMapper objectMapper = new ObjectMapper();
15+
16+
public static ObjectNode getObjectNode() {
17+
return objectMapper.createObjectNode();
18+
}
19+
20+
public static ArrayNode getArrayNode() {
21+
return objectMapper.createArrayNode();
22+
}
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.arex.agent.thirdparty.util.parse.sqlparse;
2+
3+
4+
import com.fasterxml.jackson.databind.node.ObjectNode;
5+
6+
/**
7+
* @author niyan
8+
* @date 2024/4/3
9+
* @since 1.0.0
10+
*/
11+
public interface Parse<T> {
12+
ObjectNode parse(T parseObj);
13+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.arex.agent.thirdparty.util.parse.sqlparse;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import io.arex.agent.thirdparty.util.parse.sqlparse.action.ActionFactory;
5+
import net.sf.jsqlparser.JSQLParserException;
6+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
7+
import net.sf.jsqlparser.statement.Statement;
8+
9+
/**
10+
* sql parse
11+
*
12+
* @author niyan
13+
* @date 2024/4/3
14+
* @since 1.0.0
15+
*/
16+
public class SqlParse {
17+
public JsonNode parse(String sql) throws JSQLParserException {
18+
if (sql == null || sql.isEmpty()) {
19+
return null;
20+
}
21+
22+
Statement statement = CCJSqlParserUtil.parse(sql);
23+
Parse matchParse = ActionFactory.selectParse(statement);
24+
return matchParse.parse(statement);
25+
}
26+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.arex.agent.thirdparty.util.parse.sqlparse.action;
2+
3+
import io.arex.agent.thirdparty.util.parse.sqlparse.Parse;
4+
import net.sf.jsqlparser.statement.delete.Delete;
5+
import net.sf.jsqlparser.statement.execute.Execute;
6+
import net.sf.jsqlparser.statement.insert.Insert;
7+
import net.sf.jsqlparser.statement.replace.Replace;
8+
import net.sf.jsqlparser.statement.select.Select;
9+
import net.sf.jsqlparser.statement.update.Update;
10+
11+
import net.sf.jsqlparser.statement.Statement;
12+
13+
14+
/**
15+
* @author niyan
16+
* @date 2024/4/3
17+
* @since 1.0.0
18+
*/
19+
public class ActionFactory {
20+
public static Parse selectParse(Statement statement) {
21+
if (statement instanceof Select) {
22+
return new SelectParse();
23+
} else if (statement instanceof Execute) {
24+
return new ExecuteParse();
25+
} else if (statement instanceof Delete) {
26+
return new DeleteParse();
27+
} else if (statement instanceof Insert) {
28+
return new InsertParse();
29+
} else if (statement instanceof Replace) {
30+
return new ReplaceParse();
31+
} else if (statement instanceof Update) {
32+
return new UpdateParse();
33+
} else {
34+
throw new UnsupportedOperationException("not support");
35+
}
36+
}
37+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package io.arex.agent.thirdparty.util.parse.sqlparse.action;
2+
3+
import com.fasterxml.jackson.databind.node.ObjectNode;
4+
import io.arex.agent.thirdparty.util.JacksonHelperUtil;
5+
import io.arex.agent.thirdparty.util.parse.sqlparse.Parse;
6+
import io.arex.agent.thirdparty.util.parse.sqlparse.parse.CommonParse;
7+
import io.arex.agent.thirdparty.util.parse.sqlparse.parse.ExpressionParse;
8+
import io.arex.agent.thirdparty.util.parse.sqlparse.parse.JoinParse;
9+
import io.arex.agent.thirdparty.util.parse.sqlparse.parse.OrderByParse;
10+
import io.arex.agent.thirdparty.util.parse.sqlparse.parse.TableParse;
11+
import io.arex.agent.thirdparty.util.parse.sqlparse.constants.DbParseConstants;
12+
import net.sf.jsqlparser.statement.delete.Delete;
13+
14+
/**
15+
* @author niyan
16+
* @date 2024/4/3
17+
* @since 1.0.0
18+
*/
19+
public class DeleteParse implements Parse<Delete> {
20+
21+
private final ObjectNode sqlObjectNode;
22+
public DeleteParse() {
23+
sqlObjectNode = JacksonHelperUtil.getObjectNode();
24+
// action parse
25+
sqlObjectNode.put(DbParseConstants.ACTION, DbParseConstants.DELETE);
26+
}
27+
@Override
28+
public ObjectNode parse(Delete parseObj) {
29+
30+
// tables parse
31+
TableParse.parseDelTable(parseObj.getTables(), parseObj.getTable(), sqlObjectNode);
32+
33+
// join parse
34+
JoinParse.parse(parseObj.getJoins(), sqlObjectNode);
35+
36+
// where parse
37+
ExpressionParse.parseWhere(parseObj.getWhere(), sqlObjectNode);
38+
39+
// orderBy parse
40+
OrderByParse.parse(parseObj.getOrderByElements(), sqlObjectNode);
41+
42+
// limit parse
43+
CommonParse.parseLimit(parseObj.getLimit(), sqlObjectNode);
44+
45+
return sqlObjectNode;
46+
}
47+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.arex.agent.thirdparty.util.parse.sqlparse.action;
2+
3+
import com.fasterxml.jackson.databind.node.ObjectNode;
4+
import io.arex.agent.thirdparty.util.JacksonHelperUtil;
5+
import io.arex.agent.thirdparty.util.parse.sqlparse.Parse;
6+
import io.arex.agent.thirdparty.util.parse.sqlparse.parse.CommonParse;
7+
import io.arex.agent.thirdparty.util.parse.sqlparse.parse.ExpressionParse;
8+
import io.arex.agent.thirdparty.util.parse.sqlparse.constants.DbParseConstants;
9+
import net.sf.jsqlparser.statement.execute.Execute;
10+
11+
/**
12+
* @author niyan
13+
* @date 2024/4/3
14+
* @since 1.0.0
15+
*/
16+
public class ExecuteParse implements Parse<Execute> {
17+
18+
private final ObjectNode executeObj;
19+
public ExecuteParse() {
20+
executeObj = JacksonHelperUtil.getObjectNode();
21+
// action parse
22+
executeObj.put(DbParseConstants.ACTION, DbParseConstants.EXECUTE);
23+
}
24+
25+
@Override
26+
public ObjectNode parse(Execute parseObj) {
27+
28+
// execute name parse
29+
CommonParse.parseName(parseObj.getName(), executeObj);
30+
31+
// expressions parse
32+
ExpressionParse.parse(parseObj.getExprList(), executeObj);
33+
34+
return executeObj;
35+
}
36+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.arex.agent.thirdparty.util.parse.sqlparse.action;
2+
3+
import com.fasterxml.jackson.databind.node.ObjectNode;
4+
import io.arex.agent.thirdparty.util.JacksonHelperUtil;
5+
import io.arex.agent.thirdparty.util.parse.sqlparse.Parse;
6+
import io.arex.agent.thirdparty.util.parse.sqlparse.parse.ColumnParse;
7+
import io.arex.agent.thirdparty.util.parse.sqlparse.parse.TableParse;
8+
import io.arex.agent.thirdparty.util.parse.sqlparse.constants.DbParseConstants;
9+
import net.sf.jsqlparser.statement.insert.Insert;
10+
11+
/**
12+
* @author niyan
13+
* @date 2024/4/3
14+
* @since 1.0.0
15+
*/
16+
public class InsertParse implements Parse<Insert> {
17+
18+
private final ObjectNode insertObj;
19+
public InsertParse() {
20+
insertObj = JacksonHelperUtil.getObjectNode();
21+
// action parse
22+
insertObj.put(DbParseConstants.ACTION, DbParseConstants.INSERT);
23+
}
24+
@Override
25+
public ObjectNode parse(Insert parseObj) {
26+
// table parse
27+
TableParse.parse(parseObj.getTable(), insertObj);
28+
29+
// columns parse
30+
ColumnParse.parse(parseObj.getColumns(), parseObj.getItemsList(), insertObj);
31+
32+
// setColumns parse
33+
ColumnParse.parseSetColumns(parseObj.getSetColumns(), parseObj.getSetExpressionList(), insertObj);
34+
35+
return insertObj;
36+
}
37+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.arex.agent.thirdparty.util.parse.sqlparse.action;
2+
3+
import com.fasterxml.jackson.databind.node.ObjectNode;
4+
import io.arex.agent.thirdparty.util.JacksonHelperUtil;
5+
import io.arex.agent.thirdparty.util.parse.sqlparse.Parse;
6+
import io.arex.agent.thirdparty.util.parse.sqlparse.parse.ColumnParse;
7+
import io.arex.agent.thirdparty.util.parse.sqlparse.parse.ExpressionParse;
8+
import io.arex.agent.thirdparty.util.parse.sqlparse.parse.TableParse;
9+
import io.arex.agent.thirdparty.util.parse.sqlparse.constants.DbParseConstants;
10+
import net.sf.jsqlparser.statement.replace.Replace;
11+
12+
/**
13+
* @author niyan
14+
* @date 2024/4/3
15+
* @since 1.0.0
16+
*/
17+
public class ReplaceParse implements Parse<Replace> {
18+
19+
private final ObjectNode replaceObj;
20+
public ReplaceParse() {
21+
replaceObj = JacksonHelperUtil.getObjectNode();
22+
// action parse
23+
replaceObj.put(DbParseConstants.ACTION, DbParseConstants.REPLACE);
24+
}
25+
@Override
26+
public ObjectNode parse(Replace parseObj) {
27+
// table parse
28+
TableParse.parse(parseObj.getTable(), replaceObj);
29+
30+
// columns parse
31+
ColumnParse.parse(parseObj.getColumns(), parseObj.getItemsList(), replaceObj);
32+
33+
// expressions parse
34+
ExpressionParse.parse(parseObj.getExpressions(), parseObj.getColumns(), replaceObj);
35+
36+
return replaceObj;
37+
}
38+
}

0 commit comments

Comments
 (0)