Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: handle insert..select statement (#9355)

zll6006d0f43cfe6af
diff --git a/backend/plugin/advisor/mysql/advisor_insert_must_specify_column.go b/backend/plugin/advisor/mysql/advisor_insert_must_specify_column.go
index 31e3a35579..a5319d22c2 100644
--- a/backend/plugin/advisor/mysql/advisor_insert_must_specify_column.go
+++ b/backend/plugin/advisor/mysql/advisor_insert_must_specify_column.go
@@ -64,6 +64,7 @@ type insertMustSpecifyColumnChecker struct {
*mysql.BaseMySQLParserListener
baseLine int
+ hasSelect bool
adviceList []advisor.Advice
level advisor.Status
title string
@@ -76,6 +77,10 @@ func (checker *insertMustSpecifyColumnChecker) EnterQuery(ctx *mysql.QueryContex
// EnterInsertStatement is called when production insertStatement is entered.
func (checker *insertMustSpecifyColumnChecker) EnterInsertStatement(ctx *mysql.InsertStatementContext) {
+ if ctx.InsertQueryExpression() != nil {
+ checker.hasSelect = true
+ }
+
if ctx.InsertFromConstructor() == nil {
return
}
@@ -92,3 +97,15 @@ func (checker *insertMustSpecifyColumnChecker) EnterInsertStatement(ctx *mysql.I
Line: checker.baseLine + ctx.GetStart().GetLine(),
})
}
+
+func (checker *insertMustSpecifyColumnChecker) EnterSelectItemList(ctx *mysql.SelectItemListContext) {
+ if checker.hasSelect && ctx.MULT_OPERATOR() != nil {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.InsertNotSpecifyColumn,
+ Title: checker.title,
+ Content: fmt.Sprintf("The INSERT statement must specify columns but \"%s\" does not", checker.text),
+ Line: checker.baseLine + ctx.GetStart().GetLine(),
+ })
+ }
+}
diff --git a/backend/plugin/advisor/mysql/test/statement_insert_must_specify_column.yaml b/backend/plugin/advisor/mysql/test/statement_insert_must_specify_column.yaml
index 332cc23376..24a695f9d5 100644
--- a/backend/plugin/advisor/mysql/test/statement_insert_must_specify_column.yaml
+++ b/backend/plugin/advisor/mysql/test/statement_insert_must_specify_column.yaml
@@ -1,3 +1,12 @@
+- statement: INSERT INTO t_copy SELECT * FROM t;
+ want:
+ - status: WARN
+ code: 1107
+ title: statement.insert.must-specify-column
+ content: |-
+ The INSERT statement must specify columns but "INSERT INTO t_copy SELECT * FROM t;" does not
+ line: 1
+ details: ""
- statement: INSERT INTO tech_book(id, name) VALUES (1, '1');
want:
- status: SUCCESS