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

168 lines
4.8 KiB
Go
Raw Normal View History

2022-07-19 08:58:31 +03:00
package postgres
import (
2022-09-23 13:50:54 +03:00
"embed"
2022-10-05 19:37:42 +03:00
2022-07-19 08:58:31 +03:00
"github.com/wader/fq/format"
"github.com/wader/fq/format/postgres/common"
2022-10-05 13:16:39 +03:00
"github.com/wader/fq/format/postgres/flavours/pgpro10"
2022-08-26 14:55:06 +03:00
"github.com/wader/fq/format/postgres/flavours/pgpro11"
2022-08-26 12:12:22 +03:00
"github.com/wader/fq/format/postgres/flavours/pgpro12"
2022-08-26 09:41:34 +03:00
"github.com/wader/fq/format/postgres/flavours/pgpro13"
2022-08-05 09:05:55 +03:00
"github.com/wader/fq/format/postgres/flavours/pgpro14"
"github.com/wader/fq/format/postgres/flavours/pgproee10"
2022-08-02 17:20:50 +03:00
"github.com/wader/fq/format/postgres/flavours/pgproee11"
"github.com/wader/fq/format/postgres/flavours/pgproee12"
"github.com/wader/fq/format/postgres/flavours/pgproee13"
2022-07-20 09:29:46 +03:00
"github.com/wader/fq/format/postgres/flavours/pgproee14"
2022-10-05 13:16:39 +03:00
"github.com/wader/fq/format/postgres/flavours/postgres10"
2022-07-20 08:43:24 +03:00
"github.com/wader/fq/format/postgres/flavours/postgres11"
2022-08-03 13:40:31 +03:00
"github.com/wader/fq/format/postgres/flavours/postgres12"
"github.com/wader/fq/format/postgres/flavours/postgres13"
2022-07-20 08:43:24 +03:00
"github.com/wader/fq/format/postgres/flavours/postgres14"
2022-07-19 08:58:31 +03:00
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
)
2022-09-23 13:50:54 +03:00
//go:embed pg_control.md
var pgControlFS embed.FS
2022-07-19 08:58:31 +03:00
func init() {
interp.RegisterFormat(decode.Format{
Name: format.PG_CONTROL,
2022-07-19 12:00:00 +03:00
Description: "PostgreSQL control file",
2022-07-19 08:58:31 +03:00
DecodeFn: decodePgControl,
2022-07-20 08:43:24 +03:00
DecodeInArg: format.PostgresIn{
Flavour: "",
2022-07-20 08:43:24 +03:00
},
2022-07-19 08:58:31 +03:00
})
2022-09-23 13:50:54 +03:00
interp.RegisterFS(pgControlFS)
2022-07-19 08:58:31 +03:00
}
2022-09-06 12:55:15 +03:00
//nolint:revive
2022-07-19 08:58:31 +03:00
const (
PG_CONTROL_VERSION_10 = 1002
PG_CONTROL_VERSION_11 = 1100
PGPRO_CONTROL_VERSION_11 = 1200
PG_CONTROL_VERSION_12 = 1201
//PG_CONTROL_VERSION_13 = 1300
2022-07-19 08:58:31 +03:00
PG_CONTROL_VERSION_14 = 1300
)
2022-09-06 12:55:15 +03:00
//nolint:revive
2022-07-20 08:43:24 +03:00
const (
2022-10-05 13:16:39 +03:00
PG_FLAVOUR_POSTGRES10 = "postgres10"
2022-07-20 08:43:24 +03:00
PG_FLAVOUR_POSTGRES11 = "postgres11"
2022-08-03 13:40:31 +03:00
PG_FLAVOUR_POSTGRES12 = "postgres12"
PG_FLAVOUR_POSTGRES13 = "postgres13"
2022-07-20 08:43:24 +03:00
PG_FLAVOUR_POSTGRES14 = "postgres14"
2022-10-05 13:16:39 +03:00
PG_FLAVOUR_PGPRO10 = "pgpro10"
2022-08-26 14:55:06 +03:00
PG_FLAVOUR_PGPRO11 = "pgpro11"
2022-08-26 12:12:22 +03:00
PG_FLAVOUR_PGPRO12 = "pgpro12"
2022-08-26 09:41:34 +03:00
PG_FLAVOUR_PGPRO13 = "pgpro13"
2022-08-05 09:05:55 +03:00
PG_FLAVOUR_PGPRO14 = "pgpro14"
PG_FLAVOUR_PGPROEE10 = "pgproee10"
2022-08-02 16:40:12 +03:00
PG_FLAVOUR_PGPROEE11 = "pgproee11"
PG_FLAVOUR_PGPROEE12 = "pgproee12"
PG_FLAVOUR_PGPROEE13 = "pgproee13"
2022-07-20 09:29:46 +03:00
PG_FLAVOUR_PGPROEE14 = "pgproee14"
2022-07-20 08:43:24 +03:00
)
2022-07-19 08:58:31 +03:00
func decodePgControl(d *decode.D, in any) any {
d.Endian = decode.LittleEndian
2022-09-06 12:55:15 +03:00
pgIn, ok := in.(format.PostgresIn)
if !ok {
d.Fatalf("DecodeInArg must be PostgresIn!\n")
}
switch pgIn.Flavour {
2022-10-05 13:16:39 +03:00
case PG_FLAVOUR_POSTGRES10:
return postgres10.DecodePgControl(d, in)
2022-07-20 08:43:24 +03:00
case PG_FLAVOUR_POSTGRES11:
2022-07-20 09:29:46 +03:00
return postgres11.DecodePgControl(d, in)
2022-08-03 13:40:31 +03:00
case PG_FLAVOUR_POSTGRES12:
return postgres12.DecodePgControl(d, in)
case PG_FLAVOUR_POSTGRES13:
return postgres13.DecodePgControl(d, in)
2022-09-23 13:50:54 +03:00
case PG_FLAVOUR_POSTGRES14:
2022-07-20 09:29:46 +03:00
return postgres14.DecodePgControl(d, in)
2022-10-05 13:16:39 +03:00
case PG_FLAVOUR_PGPRO10:
return pgpro10.DecodePgControl(d, in)
2022-08-26 14:55:06 +03:00
case PG_FLAVOUR_PGPRO11:
return pgpro11.DecodePgControl(d, in)
2022-08-26 12:12:22 +03:00
case PG_FLAVOUR_PGPRO12:
return pgpro12.DecodePgControl(d, in)
2022-08-26 09:41:34 +03:00
case PG_FLAVOUR_PGPRO13:
return pgpro13.DecodePgControl(d, in)
2022-08-05 09:05:55 +03:00
case PG_FLAVOUR_PGPRO14:
return pgpro14.DecodePgControl(d, in)
case PG_FLAVOUR_PGPROEE10:
return pgproee10.DecodePgControl(d, in)
2022-08-02 17:20:50 +03:00
case PG_FLAVOUR_PGPROEE11:
return pgproee11.DecodePgControl(d, in)
case PG_FLAVOUR_PGPROEE12:
return pgproee12.DecodePgControl(d, in)
case PG_FLAVOUR_PGPROEE13:
return pgproee13.DecodePgControl(d, in)
2022-07-20 09:29:46 +03:00
case PG_FLAVOUR_PGPROEE14:
return pgproee14.DecodePgControl(d, in)
2022-07-20 08:43:24 +03:00
default:
break
}
return probeForDecode(d, in)
}
func probeForDecode(d *decode.D, in any) any {
2022-07-19 08:58:31 +03:00
/* 0 | 8 */ // uint64 system_identifier;
/* 8 | 4 */ // uint32 pg_control_version;
2022-07-19 12:00:00 +03:00
d.U64()
pgControlVersion := d.U32()
2022-09-06 09:27:04 +03:00
d.SeekAbs(0)
2022-07-19 08:58:31 +03:00
pgProVersion, oriVersion := common.ParsePgProVersion(uint32(pgControlVersion))
if pgProVersion == common.PG_ORIGINAL {
switch oriVersion {
2022-10-05 13:16:39 +03:00
case PG_CONTROL_VERSION_10:
return postgres10.DecodePgControl(d, in)
case PG_CONTROL_VERSION_11:
return postgres11.DecodePgControl(d, in)
case PG_CONTROL_VERSION_12:
return postgres12.DecodePgControl(d, in)
case PG_CONTROL_VERSION_14:
return postgres14.DecodePgControl(d, in)
}
2022-07-19 08:58:31 +03:00
}
if pgProVersion == common.PGPRO_STANDARD {
switch oriVersion {
2022-10-05 13:16:39 +03:00
case PG_CONTROL_VERSION_10:
return pgpro10.DecodePgControl(d, in)
case PG_CONTROL_VERSION_11:
return pgpro11.DecodePgControl(d, in)
case PG_CONTROL_VERSION_12:
return pgpro12.DecodePgControl(d, in)
case PG_CONTROL_VERSION_14:
return pgpro14.DecodePgControl(d, in)
}
}
if pgProVersion == common.PGPRO_ENTERPRISE {
switch oriVersion {
case PG_CONTROL_VERSION_10:
return pgproee10.DecodePgControl(d, in)
case PGPRO_CONTROL_VERSION_11:
return pgproee11.DecodePgControl(d, in)
case PG_CONTROL_VERSION_12:
return pgproee12.DecodePgControl(d, in)
case PG_CONTROL_VERSION_14:
return pgproee14.DecodePgControl(d, in)
}
}
d.Fatalf("unsupported PG_CONTROL_VERSION = %d\n", pgControlVersion)
2022-07-19 08:58:31 +03:00
return nil
}