MCPcopy
hub / github.com/sqldef/sqldef / functions

Method functions

database/postgres/database.go:601–653  ·  view source on GitHub ↗

functions fetches user-defined functions from the database

()

Source from the content-addressed store, hash-verified

599
600// functions fetches user-defined functions from the database
601func (d *PostgresDatabase) functions() ([]string, error) {
602 // Query to get user-defined functions (excluding system functions and extension functions)
603 // We use pg_get_functiondef to get the complete function definition
604 // pg_get_function_identity_arguments gives us the function signature for comments
605 rows, err := d.db.Query(`
606 SELECT n.nspname AS func_schema,
607 p.proname AS func_name,
608 pg_get_functiondef(p.oid) AS func_def,
609 pg_get_function_identity_arguments(p.oid) AS func_args,
610 obj_description(p.oid, 'pg_proc') AS func_comment
611 FROM pg_catalog.pg_proc p
612 INNER JOIN pg_catalog.pg_namespace n ON p.pronamespace = n.oid
613 WHERE n.nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast', 'sys')
614 AND p.prokind = 'f'
615 AND NOT EXISTS (SELECT 1 FROM pg_depend d WHERE d.objid = p.oid AND d.classid = (SELECT oid FROM pg_catalog.pg_class WHERE relname = 'pg_proc') AND d.deptype = 'e')
616 ORDER BY n.nspname, p.proname;
617 `)
618 if err != nil {
619 return nil, err
620 }
621 defer rows.Close()
622
623 var ddls []string
624 for rows.Next() {
625 var funcSchema, funcName, funcDef, funcArgs string
626 var funcComment sql.NullString
627 if err := rows.Scan(&funcSchema, &funcName, &funcDef, &funcArgs, &funcComment); err != nil {
628 return nil, err
629 }
630 if d.config.TargetSchema != nil && !slices.Contains(d.config.TargetSchema, funcSchema) {
631 continue
632 }
633 // pg_get_functiondef returns the complete CREATE FUNCTION statement
634 // We just need to ensure it ends with a semicolon
635 funcDef = strings.TrimSpace(funcDef)
636 if !strings.HasSuffix(funcDef, ";") {
637 funcDef += ";"
638 }
639 ddls = append(ddls, funcDef)
640
641 // Add comment if exists
642 if funcComment.Valid {
643 ddls = append(ddls, fmt.Sprintf(
644 "COMMENT ON FUNCTION %s.%s(%s) IS %s;",
645 d.quoteIdentifierIfNeeded(funcSchema), d.quoteIdentifierIfNeeded(funcName), funcArgs, schemaLib.StringConstant(funcComment.String),
646 ))
647 }
648 }
649 if err := rows.Err(); err != nil {
650 return nil, err
651 }
652 return ddls, nil
653}
654
655// triggers fetches user-defined triggers from the database
656func (d *PostgresDatabase) triggers() ([]string, error) {

Callers 1

ExportDDLsMethod · 0.95

Calls 5

QueryMethod · 0.80
NextMethod · 0.80
ScanMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected