59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package db
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
// Conversions between pgtype and plain Go types.
|
|
// sqlc with sql_package: pgx/v5 maps a nullable timestamptz to
|
|
// pgtype.Timestamptz regardless of emit_pointers_for_null_types, and the
|
|
// db_type override for it is fiddly to get right across sqlc versions. Doing
|
|
// the conversion here is two lines, always works, and keeps pgtype out of
|
|
// internal/api and cmd/ entirely.
|
|
// This file is hand-written and is NOT regenerated by sqlc. It lives in the db
|
|
// package so the pgtype import stays in one place.
|
|
|
|
// Time converts a nullable timestamptz to *time.Time. NULL becomes nil.
|
|
func Time(t pgtype.Timestamptz) *time.Time {
|
|
if !t.Valid {
|
|
return nil
|
|
}
|
|
|
|
v := t.Time
|
|
|
|
return &v
|
|
}
|
|
|
|
// Timestamptz converts *time.Time back for a query parameter. nil becomes NULL.
|
|
func Timestamptz(t *time.Time) pgtype.Timestamptz {
|
|
if t == nil {
|
|
return pgtype.Timestamptz{}
|
|
}
|
|
|
|
return pgtype.Timestamptz{Time: *t, Valid: true}
|
|
}
|
|
|
|
// Str converts a nullable text column to *string.
|
|
func Str(t pgtype.Text) *string {
|
|
if !t.Valid {
|
|
return nil
|
|
}
|
|
|
|
v := t.String
|
|
|
|
return &v
|
|
}
|
|
|
|
// Text converts a string for a nullable text parameter. An empty string becomes
|
|
// NULL: an absent label or callsign is absence, not a value, and the
|
|
// core.callsign domain would reject "" anyway.
|
|
func Text(s string) pgtype.Text {
|
|
if s == "" {
|
|
return pgtype.Text{}
|
|
}
|
|
|
|
return pgtype.Text{String: s, Valid: true}
|
|
}
|