Skip to content

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

chore: add DML interface and partial support INSERT and COPY statement (#2172)

rebelice90b29c1b9486
diff --git a/plugin/parser/ast/copy_stmt.go b/plugin/parser/ast/copy_stmt.go
new file mode 100644
index 0000000000..18f72413e7
--- /dev/null
+++ b/plugin/parser/ast/copy_stmt.go
@@ -0,0 +1,9 @@
+package ast
+
+// CopyStmt is the struct for copy statement.
+type CopyStmt struct {
+ dml
+
+ Table *TableDef
+ FilePath string
+}
diff --git a/plugin/parser/ast/delete_stmt.go b/plugin/parser/ast/delete_stmt.go
index 0319dd1f7d..9911d2dd76 100644
--- a/plugin/parser/ast/delete_stmt.go
+++ b/plugin/parser/ast/delete_stmt.go
@@ -2,7 +2,7 @@ package ast
// DeleteStmt is the struct for delete statement.
type DeleteStmt struct {
- node
+ dml
Table *TableDef
WhereClause ExpressionNode
diff --git a/plugin/parser/ast/dml_node.go b/plugin/parser/ast/dml_node.go
new file mode 100644
index 0000000000..7c71fc0e9d
--- /dev/null
+++ b/plugin/parser/ast/dml_node.go
@@ -0,0 +1,18 @@
+package ast
+
+var (
+ _ DMLNode = (*dml)(nil)
+)
+
+// DMLNode is the interface for DML.
+type DMLNode interface {
+ Node
+
+ dmlNode()
+}
+
+type dml struct {
+ node
+}
+
+func (*dml) dmlNode() {}
diff --git a/plugin/parser/ast/insert_stmt.go b/plugin/parser/ast/insert_stmt.go
new file mode 100644
index 0000000000..a79bd4f3c4
--- /dev/null
+++ b/plugin/parser/ast/insert_stmt.go
@@ -0,0 +1,9 @@
+package ast
+
+// InsertStmt is the struct for insert statement.
+type InsertStmt struct {
+ dml
+
+ Table *TableDef
+ Select *SelectStmt
+}
diff --git a/plugin/parser/ast/update_stmt.go b/plugin/parser/ast/update_stmt.go
index 27602cf475..c3618434e6 100644
--- a/plugin/parser/ast/update_stmt.go
+++ b/plugin/parser/ast/update_stmt.go
@@ -2,7 +2,7 @@ package ast
// UpdateStmt is the struct for update statement.
type UpdateStmt struct {
- node
+ dml
Table *TableDef
WhereClause ExpressionNode
diff --git a/plugin/parser/ast/utils.go b/plugin/parser/ast/utils.go
index 8c0eb0b04c..942284de23 100644
--- a/plugin/parser/ast/utils.go
+++ b/plugin/parser/ast/utils.go
@@ -52,6 +52,10 @@ func Walk(v Visitor, node Node) {
if n.Foreign != nil {
Walk(v, n.Foreign)
}
+ case *CopyStmt:
+ if n.Table != nil {
+ Walk(v, n.Table)
+ }
case *CreateIndexStmt:
if n.Index != nil {
Walk(v, n.Index)
@@ -115,6 +119,13 @@ func Walk(v Visitor, node Node) {
}
case *IndexKeyDef:
// No members to walk through.
+ case *InsertStmt:
+ if n.Table != nil {
+ Walk(v, n.Table)
+ }
+ if n.Select != nil {
+ Walk(v, n.Select)
+ }
case *PatternLikeDef:
if n.Expression != nil {
Walk(v, n.Expression)
diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.go
index 7c42eebdc1..750342e67a 100644
--- a/plugin/parser/engine/pg/convert.go
+++ b/plugin/parser/engine/pg/convert.go
@@ -321,6 +321,28 @@ func convert(node *pgquery.Node, text string) (res ast.Node, err error) {
},
}, nil
}
+ case *pgquery.Node_InsertStmt:
+ insertStmt := &ast.InsertStmt{
+ Table: convertRangeVarToTableName(in.InsertStmt.Relation, ast.TableTypeBaseTable),
+ }
+
+ if in.InsertStmt.SelectStmt != nil {
+ if selectNode, ok := in.InsertStmt.SelectStmt.Node.(*pgquery.Node_SelectStmt); ok {
+ if insertStmt.Select, err = convertSelectStmt(selectNode.SelectStmt); err != nil {
+ return nil, err
+ }
+ } else {
+ return nil, parser.NewConvertErrorf("expected SelectStmt but found %t", in.InsertStmt.SelectStmt.Node)
+ }
+ }
+ return insertStmt, nil
+ case *pgquery.Node_CopyStmt:
+ copyStmt := ast.CopyStmt{
+ Table: convertRangeVarToTableName(in.CopyStmt.Relation, ast.TableTypeBaseTable),
+ FilePath: in.CopyStmt.Filename,
+ }
+
+ return &copyStmt, nil
}
return nil, nil
diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.go
index 8278c2b31f..3f2a7c5bcf 100644
--- a/plugin/parser/engine/pg/convert_test.go
+++ b/plugin/parser/engine/pg/convert_test.go
@@ -1249,3 +1249,70 @@ func TestAlterColumnType(t *testing.T) {
runTests(t, tests)
}
+
+func TestInsertStmt(t *testing.T) {
+ tests := []testData{
+ {
+ stmt: "INSERT INTO tech_book SELECT * FROM book WHERE type='tech'",
+ want: []ast.Node{
+ &ast.InsertStmt{
+ Table: &ast.TableDef{
+ Type: ast.TableTypeBaseTable,
+ Name: "tech_book",
+ },
+ Select: &ast.SelectStmt{
+ FieldList: []ast.ExpressionNode{
+ &ast.ColumnNameDef{
+ Table: &ast.TableDef{},
+ ColumnName: "*",
+ },
+ },
+ WhereClause: &ast.UnconvertedExpressionDef{},
+ },
+ },
+ },
+ textList: []string{
+ "INSERT INTO tech_book SELECT * FROM book WHERE type='tech'",
+ },
+ },
+ {
+ stmt: "INSERT INTO tech_book VALUES(1, 2, 3, 4, 5)",
+ want: []ast.Node{
+ &ast.InsertStmt{
+ Table: &ast.TableDef{
+ Type: ast.TableTypeBaseTable,
+ Name: "tech_book",
+ },
+ Select: &ast.SelectStmt{},
+ },
+ },
+ textList: []string{
+ "INSERT INTO tech_book VALUES(1, 2, 3, 4, 5)",
+ },
+ },
+ }
+
+ runTests(t, tests)
+}
+
+func TestCopyStmt(t *testing.T) {
+ tests := []testData{
+ {
+ stmt: "COPY tech_book FROM '/file/path/in/file/system'",
+ want: []ast.Node{
+ &ast.CopyStmt{
+ Table: &ast.TableDef{
+ Type: ast.TableTypeBaseTable,
+ Name: "tech_book",
+ },
+ FilePath: "/file/path/in/file/system",
+ },
+ },
+ textList: []string{
+ "COPY tech_book FROM '/file/path/in/file/system'",
+ },
+ },
+ }
+
+ runTests(t, tests)
+}