Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(parser): support CREATE/DROP FUNCTION statements (#3767)
rebelicedf300a0c2f31
diff --git a/plugin/parser/ast/create_function_stmt.go b/plugin/parser/ast/create_function_stmt.gonew file mode 100644index 0000000000..f07e5db724--- /dev/null+++ b/plugin/parser/ast/create_function_stmt.go@@ -0,0 +1,8 @@+package ast++// CreateFunctionStmt is the struct for create function statement.+type CreateFunctionStmt struct {+ ddl++ Function *FunctionDef+}diff --git a/plugin/parser/ast/drop_function_stmt.go b/plugin/parser/ast/drop_function_stmt.gonew file mode 100644index 0000000000..f1f172f209--- /dev/null+++ b/plugin/parser/ast/drop_function_stmt.go@@ -0,0 +1,15 @@+package ast++// DropFunctionStmt is the struct for drop function statement.+type DropFunctionStmt struct {+ ddl++ // Here use FunctionDef because the drop function statement needs:+ // 1. the schema name+ // 2. the function name+ // 3. the IN/INOUT/VARIADIC parameter type+ // hint: pg_query_go will skip the OUT parameters and only parse the parameter data type for DROP FUNCTION statements.+ FunctionList []*FunctionDef+ IfExists bool+ Behavior DropBehavior+}diff --git a/plugin/parser/ast/function_def.go b/plugin/parser/ast/function_def.gonew file mode 100644index 0000000000..88c231180e--- /dev/null+++ b/plugin/parser/ast/function_def.go@@ -0,0 +1,33 @@+package ast++// FunctionParameterMode is the definition for function parameter mode.+type FunctionParameterMode int++const (+ // FunctionParameterModeUndefined is the undefined parameter mode.+ FunctionParameterModeUndefined FunctionParameterMode = iota+ // FunctionParameterModeIn is mode for the IN parameters. It's the default value.+ FunctionParameterModeIn+ // FunctionParameterModeOut is mode for the OUT parameters.+ FunctionParameterModeOut+ // FunctionParameterModeInOut is mode for the OUT parameters.+ FunctionParameterModeInOut+ // FunctionParameterModeVariadic is mode for the VARIADIC parameters.+ FunctionParameterModeVariadic+)++// FunctionParameterDef is the struct for function parameter definition.+type FunctionParameterDef struct {+ Name string+ Type DataType+ Mode FunctionParameterMode+}++// FunctionDef is the struct for function definition.+type FunctionDef struct {+ node++ Schema string+ Name string+ ParameterList []*FunctionParameterDef+}diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.goindex 88cc7f465c..7ac37230b8 100644--- a/plugin/parser/engine/pg/convert.go+++ b/plugin/parser/engine/pg/convert.go@@ -348,6 +348,31 @@ func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, errdropExtension.NameList = append(dropExtension.NameList, extensionName.String_.Str)}return dropExtension, nil+ case pgquery.ObjectType_OBJECT_FUNCTION:+ dropFunctionStmt := &ast.DropFunctionStmt{+ IfExists: in.DropStmt.MissingOk,+ Behavior: convertDropBehavior(in.DropStmt.Behavior),+ }+ for _, function := range in.DropStmt.Objects {+ functionNode, ok := function.Node.(*pgquery.Node_ObjectWithArgs)+ if !ok {+ return nil, parser.NewConvertErrorf("expected ObjectWithArgs but found %t", function.Node)+ }+ functionDef := &ast.FunctionDef{}+ var err error+ functionDef.Schema, functionDef.Name, err = convertFunctionName(functionNode.ObjectWithArgs.Objname)+ if err != nil {+ return nil, err+ }+ functionDef.ParameterList, err = convertFunctionParameterList(functionNode.ObjectWithArgs.Objargs)+ if err != nil {+ return nil, err+ }++ dropFunctionStmt.FunctionList = append(dropFunctionStmt.FunctionList, functionDef)+ }++ return dropFunctionStmt, nil}case *pgquery.Node_DropdbStmt:return &ast.DropDatabaseStmt{@@ -589,6 +614,21 @@ func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, err}return createExtensionStmt, nil+ case *pgquery.Node_CreateFunctionStmt:+ var err error+ functionDef := &ast.FunctionDef{}++ functionDef.Schema, functionDef.Name, err = convertFunctionName(in.CreateFunctionStmt.Funcname)+ if err != nil {+ return nil, err+ }++ functionDef.ParameterList, err = convertFunctionParameterList(in.CreateFunctionStmt.Parameters)+ if err != nil {+ return nil, err+ }++ return &ast.CreateFunctionStmt{Function: functionDef}, nildefault:return &ast.UnconvertedStmt{}, nil}@@ -596,6 +636,81 @@ func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, errreturn nil, nil}+func convertFunctionParameterList(parameterList []*pgquery.Node) ([]*ast.FunctionParameterDef, error) {+ var result []*ast.FunctionParameterDef+ for _, node := range parameterList {+ var err error+ switch parameterNode := node.Node.(type) {+ case *pgquery.Node_FunctionParameter:+ parameterDef := &ast.FunctionParameterDef{+ Name: parameterNode.FunctionParameter.Name,+ Mode: convertParameterMode(parameterNode.FunctionParameter.Mode),+ }+ parameterDef.Type, err = convertDataType(parameterNode.FunctionParameter.ArgType)+ if err != nil {+ return nil, err+ }+ result = append(result, parameterDef)+ case *pgquery.Node_TypeName:+ parameterDef := &ast.FunctionParameterDef{}+ parameterDef.Type, err = convertDataType(parameterNode.TypeName)+ if err != nil {+ return nil, err+ }+ result = append(result, parameterDef)+ default:+ return nil, parser.NewConvertErrorf("expected FunctionParameter or TypeName but found %t", node.Node)+ }+ }+ return result, nil+}++func convertParameterMode(mode pgquery.FunctionParameterMode) ast.FunctionParameterMode {+ switch mode {+ case pgquery.FunctionParameterMode_FUNC_PARAM_IN:+ return ast.FunctionParameterModeIn+ case pgquery.FunctionParameterMode_FUNC_PARAM_OUT:+ return ast.FunctionParameterModeOut+ case pgquery.FunctionParameterMode_FUNC_PARAM_INOUT:+ return ast.FunctionParameterModeInOut+ case pgquery.FunctionParameterMode_FUNC_PARAM_VARIADIC:+ return ast.FunctionParameterModeVariadic+ default:+ return ast.FunctionParameterModeUndefined+ }+}++func convertFunctionName(list []*pgquery.Node) (string, string, error) {+ switch len(list) {+ case 2:+ schema, err := convertToString(list[0])+ if err != nil {+ return "", "", err+ }+ name, err := convertToString(list[1])+ if err != nil {+ return "", "", err+ }+ return schema, name, nil+ case 1:+ name, err := convertToString(list[0])+ if err != nil {+ return "", "", err+ }+ return "", name, nil+ default:+ return "", "", parser.NewConvertErrorf("expected 1 or 2 items but found %d", len(list))+ }+}++func convertToString(in *pgquery.Node) (string, error) {+ stringNode, ok := in.Node.(*pgquery.Node_String_)+ if !ok {+ return "", parser.NewConvertErrorf("expected String but found %t", in.Node)+ }+ return stringNode.String_.Str, nil+}+func convertAlterSequence(in *pgquery.AlterSeqStmt) (*ast.AlterSequenceStmt, error) {alterSequenceStmt := &ast.AlterSequenceStmt{IfExists: in.MissingOk,diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.goindex 608a01ae4a..ee1f280ea6 100644--- a/plugin/parser/engine/pg/convert_test.go+++ b/plugin/parser/engine/pg/convert_test.go@@ -2677,3 +2677,139 @@ func TestDropExtension(t *testing.T) {runTests(t, tests)}++func TestCreateFunction(t *testing.T) {+ tests := []testData{+ {+ stmt: `Create function get_car_Price("Price_from" int, Price_to int)+ returns int+ language plpgsql+ as+ $$+ Declare+ Car_count integer;+ Begin+ select count(*)+ into Car_count+ from Car+ where Car_price between "Price_from" and Price_to;+ return Car_count;+ End;+ $$;`,+ want: []ast.Node{+ &ast.CreateFunctionStmt{+ Function: &ast.FunctionDef{+ Schema: "",+ Name: "get_car_price",+ ParameterList: []*ast.FunctionParameterDef{+ {+ Name: "Price_from",+ Type: &ast.Integer{Size: 4},+ Mode: ast.FunctionParameterModeIn,+ },+ {+ Name: "price_to",+ Type: &ast.Integer{Size: 4},+ Mode: ast.FunctionParameterModeIn,+ },+ },+ },+ },+ },+ statementList: []parser.SingleSQL{+ {+ Text: `Create function get_car_Price("Price_from" int, Price_to int)+ returns int+ language plpgsql+ as+ $$+ Declare+ Car_count integer;+ Begin+ select count(*)+ into Car_count+ from Car+ where Car_price between "Price_from" and Price_to;+ return Car_count;+ End;+ $$;`,+ LastLine: 15,+ },+ },+ },+ {+ stmt: `CREATE FUNCTION public.trigger_update_updated_ts() RETURNS trigger+ LANGUAGE plpgsql+ AS $$+ BEGIN+ NEW.updated_ts = extract(epoch from now());+ RETURN NEW;+ END;+ $$;`,+ want: []ast.Node{+ &ast.CreateFunctionStmt{+ Function: &ast.FunctionDef{+ Schema: "public",+ Name: "trigger_update_updated_ts",+ },+ },+ },+ statementList: []parser.SingleSQL{+ {+ Text: `CREATE FUNCTION public.trigger_update_updated_ts() RETURNS trigger+ LANGUAGE plpgsql+ AS $$+ BEGIN+ NEW.updated_ts = extract(epoch from now());+ RETURN NEW;+ END;+ $$;`,+ LastLine: 8,+ },+ },+ },+ }++ runTests(t, tests)+}++func TestDropFunction(t *testing.T) {+ tests := []testData{+ {+ // pg_query_go will skip the OUT parameters and only parse the parameter data type.+ stmt: `DROP FUNCTION IF EXISTS public.func1(INOUT "Price_from" int, IN price_to int, OUT out_item int), func2()`,+ want: []ast.Node{+ &ast.DropFunctionStmt{+ IfExists: true,+ FunctionList: []*ast.FunctionDef{+ {+ Schema: "public",+ Name: "func1",+ ParameterList: []*ast.FunctionParameterDef{+ {+ Type: &ast.Integer{Size: 4},+ },+ {+ Type: &ast.Integer{Size: 4},+ },+ },+ },+ {+ Schema: "",+ Name: "func2",+ },+ },+ Behavior: ast.DropBehaviorRestrict,+ },+ },+ statementList: []parser.SingleSQL{+ {+ Text: `DROP FUNCTION IF EXISTS public.func1(INOUT "Price_from" int, IN price_to int, OUT out_item int), func2()`,+ LastLine: 1,+ },+ },+ },+ }++ runTests(t, tests)+}