Skip to content

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

chore: partial support DELETE statement (#2075)

rebelice04c54d97b3d6
diff --git a/plugin/parser/ast/delete_stmt.go b/plugin/parser/ast/delete_stmt.go
new file mode 100644
index 0000000000..0319dd1f7d
--- /dev/null
+++ b/plugin/parser/ast/delete_stmt.go
@@ -0,0 +1,18 @@
+package ast
+
+// DeleteStmt is the struct for delete statement.
+type DeleteStmt struct {
+ node
+
+ Table *TableDef
+ WhereClause ExpressionNode
+
+ // TODO(rebelice): support all expression and remove them.
+ // We define them because we cannot convert all expression now.
+ // And we only need to check the following nodes currently.
+ //
+ // PatternLikeList is the list of the patternLike nodes.
+ PatternLikeList []*PatternLikeDef
+ // SubqueryList is the list of the subquery nodes.
+ SubqueryList []*SubqueryDef
+}
diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.go
index 25b23d6356..0892b283eb 100644
--- a/plugin/parser/engine/pg/convert.go
+++ b/plugin/parser/engine/pg/convert.go
@@ -252,6 +252,17 @@ func convert(node *pgquery.Node, text string) (res ast.Node, err error) {
}, nil
case *pgquery.Node_SelectStmt:
return convertSelectStmt(in.SelectStmt)
+ case *pgquery.Node_DeleteStmt:
+ delete := &ast.DeleteStmt{
+ Table: convertRangeVarToTableName(in.DeleteStmt.Relation, ast.TableTypeBaseTable),
+ }
+ if in.DeleteStmt.WhereClause != nil {
+ var err error
+ if delete.WhereClause, delete.PatternLikeList, delete.SubqueryList, err = convertExpressionNode(in.DeleteStmt.WhereClause); err != nil {
+ return nil, err
+ }
+ }
+ return delete, nil
case *pgquery.Node_AlterObjectSchemaStmt:
switch in.AlterObjectSchemaStmt.ObjectType {
case pgquery.ObjectType_OBJECT_TABLE:
@@ -452,7 +463,6 @@ func convertSelectStmt(in *pgquery.SelectStmt) (*ast.SelectStmt, error) {
}
// Convert WHERE clause
if in.WhereClause != nil {
- selectStmt.WhereClause = &ast.UnconvertedExpressionDef{}
var err error
if selectStmt.WhereClause, selectStmt.PatternLikeList, selectStmt.SubqueryList, err = convertExpressionNode(in.WhereClause); err != nil {
return nil, err
diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.go
index 687c9ebe69..181142b2ee 100644
--- a/plugin/parser/engine/pg/convert_test.go
+++ b/plugin/parser/engine/pg/convert_test.go
@@ -1027,6 +1027,42 @@ func TestPGDropDatabaseStmt(t *testing.T) {
runTests(t, tests)
}
+func TestDeleteStmt(t *testing.T) {
+ tests := []testData{
+ {
+ stmt: "DELETE FROM tech_book",
+ want: []ast.Node{
+ &ast.DeleteStmt{
+ Table: &ast.TableDef{
+ Type: ast.TableTypeBaseTable,
+ Name: "tech_book",
+ },
+ },
+ },
+ textList: []string{
+ "DELETE FROM tech_book",
+ },
+ },
+ {
+ stmt: "DELETE FROM tech_book WHERE a > 1",
+ want: []ast.Node{
+ &ast.DeleteStmt{
+ Table: &ast.TableDef{
+ Type: ast.TableTypeBaseTable,
+ Name: "tech_book",
+ },
+ WhereClause: &ast.UnconvertedExpressionDef{},
+ },
+ },
+ textList: []string{
+ "DELETE FROM tech_book WHERE a > 1",
+ },
+ },
+ }
+
+ runTests(t, tests)
+}
+
func TestSetSchemaStmt(t *testing.T) {
tests := []testData{
{