Skip to content

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

chore: partial support UPDATE statement (#2076)

rebelice8b631cd7bc05
diff --git a/plugin/parser/ast/update_stmt.go b/plugin/parser/ast/update_stmt.go
new file mode 100644
index 0000000000..27602cf475
--- /dev/null
+++ b/plugin/parser/ast/update_stmt.go
@@ -0,0 +1,18 @@
+package ast
+
+// UpdateStmt is the struct for update statement.
+type UpdateStmt 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 0892b283eb..ff1680d9fa 100644
--- a/plugin/parser/engine/pg/convert.go
+++ b/plugin/parser/engine/pg/convert.go
@@ -252,6 +252,18 @@ func convert(node *pgquery.Node, text string) (res ast.Node, err error) {
}, nil
case *pgquery.Node_SelectStmt:
return convertSelectStmt(in.SelectStmt)
+ case *pgquery.Node_UpdateStmt:
+ update := &ast.UpdateStmt{
+ Table: convertRangeVarToTableName(in.UpdateStmt.Relation, ast.TableTypeBaseTable),
+ }
+ // Convert WHERE clause
+ if in.UpdateStmt.WhereClause != nil {
+ var err error
+ if update.WhereClause, update.PatternLikeList, update.SubqueryList, err = convertExpressionNode(in.UpdateStmt.WhereClause); err != nil {
+ return nil, err
+ }
+ }
+ return update, nil
case *pgquery.Node_DeleteStmt:
delete := &ast.DeleteStmt{
Table: convertRangeVarToTableName(in.DeleteStmt.Relation, ast.TableTypeBaseTable),
diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.go
index 181142b2ee..8aa1652869 100644
--- a/plugin/parser/engine/pg/convert_test.go
+++ b/plugin/parser/engine/pg/convert_test.go
@@ -1027,6 +1027,41 @@ func TestPGDropDatabaseStmt(t *testing.T) {
runTests(t, tests)
}
+func TestUpdateStmt(t *testing.T) {
+ tests := []testData{
+ {
+ stmt: "UPDATE tech_book SET a = 1",
+ want: []ast.Node{
+ &ast.UpdateStmt{
+ Table: &ast.TableDef{
+ Type: ast.TableTypeBaseTable,
+ Name: "tech_book",
+ },
+ },
+ },
+ textList: []string{
+ "UPDATE tech_book SET a = 1",
+ },
+ },
+ {
+ stmt: "UPDATE tech_book SET a = 1 WHERE a > 1",
+ want: []ast.Node{
+ &ast.UpdateStmt{
+ Table: &ast.TableDef{
+ Type: ast.TableTypeBaseTable,
+ Name: "tech_book",
+ },
+ WhereClause: &ast.UnconvertedExpressionDef{},
+ },
+ },
+ textList: []string{
+ "UPDATE tech_book SET a = 1 WHERE a > 1",
+ },
+ },
+ }
+
+ runTests(t, tests)
+}
func TestDeleteStmt(t *testing.T) {
tests := []testData{
{