Skip to content

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

feat(parser): support to parse create/drop extension statement (#3728)

rebeliced3df8e445c27
diff --git a/plugin/parser/ast/create_extension_stmt.go b/plugin/parser/ast/create_extension_stmt.go
new file mode 100644
index 0000000000..5e45b71680
--- /dev/null
+++ b/plugin/parser/ast/create_extension_stmt.go
@@ -0,0 +1,10 @@
+package ast
+
+// CreateExtensionStmt is the struct for create extension statement.
+type CreateExtensionStmt struct {
+ ddl
+
+ Name string
+ Schema string
+ IfNotExists bool
+}
diff --git a/plugin/parser/ast/drop_extension_stmt.go b/plugin/parser/ast/drop_extension_stmt.go
new file mode 100644
index 0000000000..cc228b2900
--- /dev/null
+++ b/plugin/parser/ast/drop_extension_stmt.go
@@ -0,0 +1,10 @@
+package ast
+
+// DropExtensionStmt is the struct for drop extension statement.
+type DropExtensionStmt struct {
+ ddl
+
+ NameList []string
+ IfExists bool
+ Behavior DropBehavior
+}
diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.go
index 46a662441b..88cc7f465c 100644
--- a/plugin/parser/engine/pg/convert.go
+++ b/plugin/parser/engine/pg/convert.go
@@ -335,6 +335,19 @@ func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, err
dropSequence.SequenceNameList = append(dropSequence.SequenceNameList, sequenceDef)
}
return dropSequence, nil
+ case pgquery.ObjectType_OBJECT_EXTENSION:
+ dropExtension := &ast.DropExtensionStmt{
+ IfExists: in.DropStmt.MissingOk,
+ Behavior: convertDropBehavior(in.DropStmt.Behavior),
+ }
+ for _, extension := range in.DropStmt.Objects {
+ extensionName, ok := extension.Node.(*pgquery.Node_String_)
+ if !ok {
+ return nil, parser.NewConvertErrorf("expected String but found %t", extension.Node)
+ }
+ dropExtension.NameList = append(dropExtension.NameList, extensionName.String_.Str)
+ }
+ return dropExtension, nil
}
case *pgquery.Node_DropdbStmt:
return &ast.DropDatabaseStmt{
@@ -557,6 +570,25 @@ func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, err
}
}
return &createSchemaStmt, nil
+ case *pgquery.Node_CreateExtensionStmt:
+ createExtensionStmt := &ast.CreateExtensionStmt{
+ Name: in.CreateExtensionStmt.Extname,
+ IfNotExists: in.CreateExtensionStmt.IfNotExists,
+ }
+
+ for _, option := range in.CreateExtensionStmt.Options {
+ if item, ok := option.Node.(*pgquery.Node_DefElem); ok {
+ if item.DefElem.Defname == "schema" {
+ schemaName, ok := item.DefElem.Arg.Node.(*pgquery.Node_String_)
+ if !ok {
+ return nil, parser.NewConvertErrorf("expected String but found %t", item.DefElem.Arg.Node)
+ }
+ createExtensionStmt.Schema = schemaName.String_.Str
+ }
+ }
+ }
+
+ return createExtensionStmt, nil
default:
return &ast.UnconvertedStmt{}, nil
}
diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.go
index ed7ecc6679..608a01ae4a 100644
--- a/plugin/parser/engine/pg/convert_test.go
+++ b/plugin/parser/engine/pg/convert_test.go
@@ -2628,3 +2628,52 @@ func TestAlterSequence(t *testing.T) {
runTests(t, tests)
}
+
+func TestCreateExtension(t *testing.T) {
+ tests := []testData{
+ {
+ stmt: `CREATE EXTENSION IF NOT EXISTS pg_trgm WITH SCHEMA public`,
+ want: []ast.Node{
+ &ast.CreateExtensionStmt{
+ Schema: "public",
+ Name: "pg_trgm",
+ IfNotExists: true,
+ },
+ },
+ statementList: []parser.SingleSQL{
+ {
+ Text: `CREATE EXTENSION IF NOT EXISTS pg_trgm WITH SCHEMA public`,
+ LastLine: 1,
+ },
+ },
+ },
+ }
+
+ runTests(t, tests)
+}
+
+func TestDropExtension(t *testing.T) {
+ tests := []testData{
+ {
+ stmt: `DROP EXTENSION IF EXISTS pg_trgm, hstore`,
+ want: []ast.Node{
+ &ast.DropExtensionStmt{
+ IfExists: true,
+ NameList: []string{
+ "pg_trgm",
+ "hstore",
+ },
+ Behavior: ast.DropBehaviorRestrict,
+ },
+ },
+ statementList: []parser.SingleSQL{
+ {
+ Text: `DROP EXTENSION IF EXISTS pg_trgm, hstore`,
+ LastLine: 1,
+ },
+ },
+ },
+ }
+
+ runTests(t, tests)
+}