Skip to content

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

feat(parser): support CREATE DATABASE statement partitially (#2987)

rebelice2eb50b942c0b
diff --git a/plugin/parser/ast/create_database_stmt.go b/plugin/parser/ast/create_database_stmt.go
new file mode 100644
index 0000000000..0f947be610
--- /dev/null
+++ b/plugin/parser/ast/create_database_stmt.go
@@ -0,0 +1,9 @@
+package ast
+
+// CreateDatabaseStmt is the struct for create database stmt.
+type CreateDatabaseStmt struct {
+ ddl
+
+ Name string
+ OptionList []*DatabaseOptionDef
+}
diff --git a/plugin/parser/ast/database_option_def.go b/plugin/parser/ast/database_option_def.go
new file mode 100644
index 0000000000..2bd2f22b96
--- /dev/null
+++ b/plugin/parser/ast/database_option_def.go
@@ -0,0 +1,17 @@
+package ast
+
+// DatabaseOptionType is the type for database options.
+type DatabaseOptionType int
+
+const (
+ // DatabaseOptionEncoding is the type for the database encoding option.
+ DatabaseOptionEncoding = iota
+)
+
+// DatabaseOptionDef is the struct for database option.
+type DatabaseOptionDef struct {
+ node
+
+ Type DatabaseOptionType
+ Value string
+}
diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.go
index 290f748bde..7bc14f8f9d 100644
--- a/plugin/parser/engine/pg/convert.go
+++ b/plugin/parser/engine/pg/convert.go
@@ -388,6 +388,24 @@ func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, err
}
return &commentStmt, nil
+ case *pgquery.Node_CreatedbStmt:
+ createDatabaseStmt := ast.CreateDatabaseStmt{
+ Name: in.CreatedbStmt.Dbname,
+ }
+ for _, option := range in.CreatedbStmt.Options {
+ if item, ok := option.Node.(*pgquery.Node_DefElem); ok && item.DefElem.Defname == "encoding" {
+ value, ok := item.DefElem.Arg.Node.(*pgquery.Node_String_)
+ if !ok {
+ return nil, parser.NewConvertErrorf("expected String but found %t", item.DefElem.Arg.Node)
+ }
+ createDatabaseStmt.OptionList = append(createDatabaseStmt.OptionList, &ast.DatabaseOptionDef{
+ Type: ast.DatabaseOptionEncoding,
+ Value: value.String_.Str,
+ })
+ }
+ }
+
+ return &createDatabaseStmt, 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 7026baa004..2e309b157a 100644
--- a/plugin/parser/engine/pg/convert_test.go
+++ b/plugin/parser/engine/pg/convert_test.go
@@ -1792,3 +1792,28 @@ func TestCommentStmt(t *testing.T) {
runTests(t, tests)
}
+
+func TestCreateDatabase(t *testing.T) {
+ tests := []testData{
+ {
+ stmt: "CREATE DATABASE db1 ENCODING = 'UTF8'",
+ want: []ast.Node{&ast.CreateDatabaseStmt{
+ Name: "db1",
+ OptionList: []*ast.DatabaseOptionDef{
+ {
+ Type: ast.DatabaseOptionEncoding,
+ Value: "UTF8",
+ },
+ },
+ }},
+ statementList: []parser.SingleSQL{
+ {
+ Text: "CREATE DATABASE db1 ENCODING = 'UTF8'",
+ LastLine: 1,
+ },
+ },
+ },
+ }
+
+ runTests(t, tests)
+}