75f3dd141c
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
CI and Release / lint-frontend (push) Has been cancelled
CI and Release / dockerfile-scan (push) Has been cancelled
CI and Release / test-frontend (push) Has been cancelled
CI and Release / lint-verification-agent (push) Has been cancelled
CI and Release / test-verification-agent (push) Has been cancelled
CI and Release / e2e-verification-agent (push) Has been cancelled
CI and Release / test-backend (push) Has been cancelled
CI and Release / build-dev-image (push) Has been cancelled
CI and Release / push-dev-image (push) Has been cancelled
CI and Release / build-image (push) Has been cancelled
CI and Release / build-verification-image (push) Has been cancelled
CI and Release / determine-version (push) Has been cancelled
CI and Release / push-image (push) Has been cancelled
CI and Release / push-verification-image (12) (push) Has been cancelled
CI and Release / lint-backend (push) Has been cancelled
CI and Release / push-verification-image (17) (push) Has been cancelled
CI and Release / push-verification-image (18) (push) Has been cancelled
CI and Release / release (push) Has been cancelled
CI and Release / publish-helm-chart (push) Has been cancelled
CI and Release / push-verification-image (13) (push) Has been cancelled
CI and Release / push-verification-image (14) (push) Has been cancelled
CI and Release / push-verification-image (15) (push) Has been cancelled
CI and Release / push-verification-image (16) (push) Has been cancelled
68 lines
1.4 KiB
Go
68 lines
1.4 KiB
Go
package walmath
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"fmt"
|
|
)
|
|
|
|
type LSN uint64
|
|
|
|
func (lsn LSN) String() string {
|
|
return fmt.Sprintf("%X/%X", uint32(lsn>>32), uint32(lsn))
|
|
}
|
|
|
|
func ParseLSN(s string) (LSN, error) {
|
|
var hi, lo uint32
|
|
|
|
n, err := fmt.Sscanf(s, "%X/%X", &hi, &lo)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("walmath: invalid LSN %q: %w", s, err)
|
|
}
|
|
if n != 2 {
|
|
return 0, fmt.Errorf("walmath: invalid LSN %q: expected two hex parts, got %d", s, n)
|
|
}
|
|
|
|
return LSN(uint64(hi)<<32 | uint64(lo)), nil
|
|
}
|
|
|
|
// Value implements driver.Valuer so LSN round-trips through a PostgreSQL
|
|
// pg_lsn column. Returns the canonical "X/X" hex form; pgx forwards it as
|
|
// text and pg_lsn accepts that representation implicitly.
|
|
func (lsn LSN) Value() (driver.Value, error) {
|
|
return lsn.String(), nil
|
|
}
|
|
|
|
// Scan implements sql.Scanner for pg_lsn columns. Accepts the text form (the
|
|
// common case under pgx) and the binary uint64 form for safety.
|
|
func (lsn *LSN) Scan(value any) error {
|
|
if value == nil {
|
|
*lsn = 0
|
|
return nil
|
|
}
|
|
|
|
switch v := value.(type) {
|
|
case string:
|
|
parsed, err := ParseLSN(v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
*lsn = parsed
|
|
case []byte:
|
|
parsed, err := ParseLSN(string(v))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
*lsn = parsed
|
|
case uint64:
|
|
*lsn = LSN(v)
|
|
case int64:
|
|
*lsn = LSN(uint64(v))
|
|
default:
|
|
return fmt.Errorf("walmath: unsupported scan source for LSN: %T", value)
|
|
}
|
|
|
|
return nil
|
|
}
|