1
1
mirror of https://github.com/wader/fq.git synced 2024-12-02 12:45:53 +03:00
fq/format/postgres/common/pg_control.go

144 lines
3.1 KiB
Go
Raw Normal View History

2022-07-20 19:25:07 +03:00
package common
2022-07-20 20:04:51 +03:00
import (
"fmt"
2022-09-06 12:55:15 +03:00
2022-07-20 20:04:51 +03:00
"github.com/wader/fq/pkg/scalar"
2022-09-06 12:55:15 +03:00
2022-07-21 08:50:17 +03:00
"time"
2022-07-20 20:04:51 +03:00
)
2022-07-20 19:25:07 +03:00
//typedef enum DBState
//{
// DB_STARTUP = 0,
// DB_SHUTDOWNED,
// DB_SHUTDOWNED_IN_RECOVERY,
// DB_SHUTDOWNING,
// DB_IN_CRASH_RECOVERY,
// DB_IN_ARCHIVE_RECOVERY,
// DB_IN_PRODUCTION
//} DBState;
var DBState = scalar.UToScalar{
0: {Sym: "DB_STARTUP"},
1: {Sym: "DB_SHUTDOWNED"},
2: {Sym: "DB_SHUTDOWNED_IN_RECOVERY"},
3: {Sym: "DB_SHUTDOWNING"},
4: {Sym: "DB_IN_CRASH_RECOVERY"},
5: {Sym: "DB_IN_ARCHIVE_RECOVERY"},
6: {Sym: "DB_IN_PRODUCTION"},
}
2022-07-20 19:49:47 +03:00
//typedef enum WalLevel
//{
// WAL_LEVEL_MINIMAL = 0,
// WAL_LEVEL_REPLICA,
// WAL_LEVEL_LOGICAL
//} WalLevel;
var WalLevel = scalar.SToScalar{
0: {Sym: "WAL_LEVEL_MINIMAL"},
1: {Sym: "WAL_LEVEL_REPLICA"},
2: {Sym: "WAL_LEVEL_LOGICAL"},
}
2022-07-20 20:04:51 +03:00
type icuVersionMapper struct{}
func (m icuVersionMapper) MapScalar(s scalar.S) (scalar.S, error) {
a := s.ActualU()
major := a & 0xff
minor := (a >> 8) & 0xff
v1 := (a >> 16) & 0xff
v2 := (a >> 24) & 0xff
s.Sym = fmt.Sprintf("%d.%d.%d.%d", major, minor, v1, v2)
return s, nil
}
var IcuVersionMapper = icuVersionMapper{}
2022-07-20 20:17:50 +03:00
type xLogRecPtrMapper struct{}
func (m xLogRecPtrMapper) MapScalar(s scalar.S) (scalar.S, error) {
2022-07-21 09:39:12 +03:00
v := s.ActualU()
s.Sym = fmt.Sprintf("%X/%X", v>>32, uint32(v))
2022-07-20 20:17:50 +03:00
return s, nil
}
var XLogRecPtrMapper = xLogRecPtrMapper{}
2022-07-21 09:39:12 +03:00
var LocPtrMapper = xLogRecPtrMapper{}
2022-07-21 08:50:17 +03:00
2022-08-03 13:40:31 +03:00
type nextFullXidMapper struct{}
func (m nextFullXidMapper) MapScalar(s scalar.S) (scalar.S, error) {
v := s.ActualU()
s.Sym = fmt.Sprintf("%d:%d", v>>32, uint32(v))
return s, nil
}
var NextFullXidMapper = nextFullXidMapper{}
2022-07-21 08:50:17 +03:00
type timeMapper struct{}
func (m timeMapper) MapScalar(s scalar.S) (scalar.S, error) {
ut := s.ActualS()
t := time.Unix(ut, 0)
s.Sym = t.UTC().Format(time.RFC1123)
return s, nil
}
var TimeMapper = timeMapper{}
2022-07-21 09:15:15 +03:00
// typedef enum
//{
// PG_UNKNOWN = 0xFFFF,
// PG_ORIGINAL = 0,
// PGPRO_STANDARD = ('P'<<8|'P'),
// PGPRO_ENTERPRISE = ('P'<<8|'E'),
//} PgEdition;
2022-09-06 12:55:15 +03:00
//nolint:revive
2022-07-21 09:15:15 +03:00
const (
PG_UNKNOWN = 0xFFFF
PG_ORIGINAL = 0
PGPRO_STANDARD = (uint32('P') << 8) | uint32('P')
PGPRO_ENTERPRISE = (uint32('P') << 8) | uint32('E')
PG_UNKNOWN_STR = "(unknown edition)"
PG_ORIGINAL_STR = "PostgreSQL"
PGPRO_STANDARD_STR = "Postgres Pro Standard"
PGPRO_ENTERPRISE_STR = "Postgres Pro Enterprise"
)
type versionMapper struct{}
func (m versionMapper) MapScalar(s scalar.S) (scalar.S, error) {
v := s.ActualU()
v1, v2 := ParsePgProVersion(uint32(v))
2022-07-21 09:15:15 +03:00
switch v1 {
case PG_UNKNOWN:
s.Sym = fmt.Sprintf("%s %d", PG_UNKNOWN_STR, v2)
case PG_ORIGINAL:
s.Sym = fmt.Sprintf("%s %d", PG_ORIGINAL_STR, v2)
case PGPRO_STANDARD:
s.Sym = fmt.Sprintf("%s %d", PGPRO_STANDARD_STR, v2)
case PGPRO_ENTERPRISE:
s.Sym = fmt.Sprintf("%s %d", PGPRO_ENTERPRISE_STR, v2)
}
return s, nil
}
var VersionMapper = versionMapper{}
func ParsePgProVersion(v uint32) (pgProVersion uint32, oriVer uint32) {
pgProVersion = v >> 16
oriVer = v & 0xffff
return
}
type hexMapper struct{}
func (m hexMapper) MapScalar(s scalar.S) (scalar.S, error) {
v := s.ActualU()
s.Sym = fmt.Sprintf("%X", v)
return s, nil
}
var HexMapper = hexMapper{}