forked from pgpkg/pgpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres.go
More file actions
65 lines (54 loc) · 1.82 KB
/
postgres.go
File metadata and controls
65 lines (54 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package pgpkg
import (
"fmt"
"github.com/lib/pq"
"regexp"
"strings"
)
// The "volume level" is incremented or decremented to supress messages from the database.
// This helps to stop database generated messages that will make users think something is wrong
// when it isn't. These messages are great during debugging but should be silenced during
// operations that will generate them spuriously; sometimes PG itself emits them when we
// don't want them.
var logVolume = 0
func noticeHandler(err *pq.Error) {
// Don't allow warnings to be quiet.
if err.Severity == "WARNING" {
Stderr.Printf("[%s] %s\n", strings.ToUpper(err.Severity), err.Message)
} else {
if logVolume == 0 || Options.Verbose {
Stdout.Printf("[%s] %s\n", strings.ToLower(err.Severity), err.Message)
}
}
}
func LogQuieter() {
logVolume--
}
func LogLouder() {
logVolume++
}
var fnamePattern = regexp.MustCompile("function ([a-z_][a-z0-9_.]*[(].*[)])")
// Get the source of a function from the database itself, based on the "where" field
// of a pgsql error.
func getFunctionSource(tx *PkgTx, where string) (string, error) {
// The where string should contain a function name.
fnames := fnamePattern.FindStringSubmatch(where)
if len(fnames) != 2 {
return "", fmt.Errorf("can't identify function in error detail")
}
// Convert the function name into an OID
foidRow := tx.QueryRow("select $1::pg_catalog.regprocedure::pg_catalog.oid", fnames[1])
var foid int
err := foidRow.Scan(&foid)
if err != nil {
return "", fmt.Errorf("error looking up function name: %w", err)
}
// Look up the OID to get the source of the function.
var src string
srcRow := tx.QueryRow("select prosrc from pg_catalog.pg_proc where OID=$1", foid)
err = srcRow.Scan(&src)
if err != nil {
return "", fmt.Errorf("error looking up function source: %w", err)
}
return src, nil
}