Help us shape the future of Go! Take the 2026 Go Developer Survey

types

package
v0.4.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package types provides common SQL column types and model field embeds.

Basic time types (package sqltime)

  • sqltime.Datetime: calendar date-time stored as a SQL datetime type (MySQL DATETIME, PostgreSQL TIMESTAMP).
  • sqltime.Timestamp: integer unix timestamp at second precision.
  • sqltime.TimestampMilli: integer unix timestamp at millisecond precision.

Operation-time embeds over sqltime.Datetime

Combinations of CreatedAt / UpdatedAt / DeletedAt for record lifecycle. Soft-deletion variants (CreationModificationDeletion*) implement SoftDeletion. Aliases: OperationDatetime, OperationDatetimePrecise.

Second precision:

  • CreationDatetime
  • CreationModificationDatetime
  • CreationModificationDeletionDatetime
  • sqlops.CreationDatetime
  • sqlops.CreationModificationDatetime
  • sqlops.CreationModificationDeletionDatetime

Millisecond precision (Datetime with precision=3):

  • CreationDatetimeMilli
  • CreationModificationDatetimePrecise
  • CreationModificationDeletionDatetimePrecise
  • sqlops.CreationDatetimeMilli
  • sqlops.CreationModificationDatetimePrecise
  • sqlops.CreationModificationDeletionDatetimePrecise

Column names differ: types uses an `f_` prefix (e.g. f_created_at); sqlops uses unprefixed names (e.g. created_at).

Operation-time embeds over sqltime.Timestamp / TimestampMilli

Second precision (sqltime.Timestamp):

  • CreationTime
  • CreationModificationTime
  • CreationModificationDeletionTime
  • sqlops.CreationTime
  • sqlops.CreationModificationTime
  • sqlops.CreationModificationDeletionTime

Millisecond precision (sqltime.TimestampMilli):

  • CreationTimePrecise
  • CreationModificationTimePrecise
  • CreationModificationDeletionTimePrecise
  • sqlops.CreationTimePrecise
  • sqlops.CreationModificationTimePrecise
  • sqlops.CreationModificationDeletionTimePrecise

Soft-deletion variants implement SoftDeletion. Aliases: OperationTime, OperationTimePrecise. Same f_ vs unprefixed column naming as the Datetime embeds above.

Bool

types.Bool is a three-state boolean (TRUE / FALSE / UNKNOWN ) for avoiding ambiguity with Go's two-state bool. Use Boolean(b) to convert from bool.

Text

types.Text wraps SQL TEXT. An empty string is stored as NULL.

Blob

types.Blob wraps SQL BLOB (BYTEA on PostgreSQL). An empty slice is stored as NULL.

Decimal

types.Decimal is a high-precision decimal for monetary and exact numeric columns.

JSON

types.JSONArray and types.JSONObject marshal Go values to/from JSON columns.

Other

AutoIncID is an embed for an auto-increment primary key (f_id). ID is a generic unsigned identifier type.

Marker interfaces CreationMarker, ModificationMarker, DeletionMarker, and SoftDeletion describe create / update / soft-delete behavior used by model code generation. +genx:doc

Code generated by genx:doc@v0.3.9 DO NOT EDIT.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DriverJSONValue

func DriverJSONValue(v any) (driver.Value, error)

DriverJSONValue unmarshal input to driver.Value

func HasSoftDeletion

func HasSoftDeletion[M internal.Model]() bool

HasSoftDeletion reports whether M implements SoftDeletion.

func ScanJSONValue

func ScanJSONValue(src, dst any) error

ScanJSONValue scan database input to value

Types

type ArrayAsList added in v0.0.5

type ArrayAsList[T _E] []T

ArrayAsList stores a typed slice as a comma-separated TEXT column.

func ParseArrayAsList added in v0.3.6

func ParseArrayAsList[T _E](s string) (ArrayAsList[T], error)

ParseArrayAsList parses a comma-separated list into ArrayAsList[T]. An empty input string yields an empty list. Empty parts (after TrimSpace) between commas are rejected.

func (*ArrayAsList[T]) Append added in v0.0.5

func (aa *ArrayAsList[T]) Append(values ...T)

Append appends values to the list.

func (ArrayAsList[T]) DBType added in v0.0.5

func (aa ArrayAsList[T]) DBType(driver string) string

DBType returns the SQL datatype for ArrayAsList.

func (*ArrayAsList[T]) DocOf added in v0.2.8

func (v *ArrayAsList[T]) DocOf(names ...string) ([]string, bool)

func (ArrayAsList[T]) Elements added in v0.0.5

func (aa ArrayAsList[T]) Elements() []string

Elements returns string forms of all elements.

func (*ArrayAsList[T]) MarshalJSON added in v0.0.5

func (aa *ArrayAsList[T]) MarshalJSON() ([]byte, error)

MarshalJSON encodes the list as a quoted comma-separated JSON string.

func (*ArrayAsList[T]) MarshalText added in v0.3.6

func (aa *ArrayAsList[T]) MarshalText() ([]byte, error)

MarshalText encodes the list as comma-separated text.

func (*ArrayAsList[T]) Scan added in v0.0.5

func (aa *ArrayAsList[T]) Scan(v any) error

Scan implements sql.Scanner. NULL scans to an empty list.

func (ArrayAsList[T]) String added in v0.0.5

func (aa ArrayAsList[T]) String() string

String returns the comma-separated representation.

func (*ArrayAsList[T]) UnmarshalJSON added in v0.0.5

func (aa *ArrayAsList[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON parses a JSON string holding a comma-separated list. null and empty payloads become an empty list.

func (*ArrayAsList[T]) UnmarshalText added in v0.3.6

func (aa *ArrayAsList[T]) UnmarshalText(data []byte) error

UnmarshalText parses comma-separated text into the list.

func (ArrayAsList[T]) Value added in v0.0.5

func (aa ArrayAsList[T]) Value() (driver.Value, error)

Value implements driver.Valuer.

type AutoIncID deprecated

type AutoIncID struct {
	ID uint64 `db:"f_id,autoinc" json:"-"`
}

AutoIncID is an auto-increment primary key field.

Deprecated: use Serial instead. uint64 conflicts with database/sql's int64-centric Value model (see Serial).

func (*AutoIncID) DocOf added in v0.2.8

func (v *AutoIncID) DocOf(names ...string) ([]string, bool)

type Blob

type Blob []byte

Blob nullable binary

func (Blob) DBType

func (Blob) DBType(driver string) string

func (*Blob) DocOf added in v0.2.8

func (v *Blob) DocOf(names ...string) ([]string, bool)

func (*Blob) Scan

func (v *Blob) Scan(src any) error

func (*Blob) Set

func (v *Blob) Set(x []byte)

func (Blob) Value

func (v Blob) Value() (driver.Value, error)

type Bool

type Bool int

Bool a ternary boolean state UNKNOWN, TRUE or FALSE

const (
	TRUE  Bool // true
	FALSE      // false
)

func Boolean

func Boolean(b bool) Bool

Boolean converts a Go bool to Bool (TRUE/FALSE).

func (Bool) Bool

func (v Bool) Bool() bool

func (*Bool) DocOf added in v0.2.8

func (v *Bool) DocOf(names ...string) ([]string, bool)

func (Bool) MarshalJSON

func (v Bool) MarshalJSON() ([]byte, error)

func (Bool) MarshalText

func (v Bool) MarshalText() ([]byte, error)

func (Bool) OpenAPISchemaType added in v0.2.8

func (Bool) OpenAPISchemaType() []string

func (*Bool) UnmarshalJSON

func (v *Bool) UnmarshalJSON(data []byte) (err error)

func (*Bool) UnmarshalText

func (v *Bool) UnmarshalText(data []byte) error

type CreationDatetime

type CreationDatetime struct {
	// CreatedAt 创建日期时间(秒)
	CreatedAt sqltime.Datetime `db:"f_created_at,default=CURRENT_TIMESTAMP" json:"createdAt"`
}

CreationDatetime holds CreatedAt as a second-precision datetime.

func (*CreationDatetime) DocOf added in v0.2.8

func (v *CreationDatetime) DocOf(names ...string) ([]string, bool)

func (*CreationDatetime) MarkCreatedAt

func (c *CreationDatetime) MarkCreatedAt()

type CreationDatetimeMilli added in v0.0.5

type CreationDatetimeMilli struct {
	// CreatedAt 创建日期时间(毫秒)
	CreatedAt sqltime.Datetime `db:"f_created_at,precision=3,default=CURRENT_TIMESTAMP(3)" json:"createdAt"`
}

CreationDatetimeMilli holds CreatedAt as a millisecond-precision datetime.

func (*CreationDatetimeMilli) DocOf added in v0.2.8

func (v *CreationDatetimeMilli) DocOf(names ...string) ([]string, bool)

func (*CreationDatetimeMilli) MarkCreatedAt added in v0.0.5

func (c *CreationDatetimeMilli) MarkCreatedAt()

type CreationMarker

type CreationMarker interface {
	MarkCreatedAt()
}

CreationMarker marks a record as created.

type CreationModificationDatetime

type CreationModificationDatetime struct {
	CreationDatetime
	// UpdatedAt 更新日期时间(秒)
	UpdatedAt sqltime.Datetime `db:"f_updated_at,default=CURRENT_TIMESTAMP,onupdate=CURRENT_TIMESTAMP" json:"updatedAt"`
}

CreationModificationDatetime adds UpdatedAt to CreationDatetime.

func (*CreationModificationDatetime) DocOf added in v0.2.8

func (v *CreationModificationDatetime) DocOf(names ...string) ([]string, bool)

func (*CreationModificationDatetime) MarkCreatedAt

func (cu *CreationModificationDatetime) MarkCreatedAt()

func (*CreationModificationDatetime) MarkModifiedAt

func (cu *CreationModificationDatetime) MarkModifiedAt()

type CreationModificationDatetimePrecise added in v0.0.5

type CreationModificationDatetimePrecise struct {
	CreationDatetimeMilli
	// UpdatedAt 更新日期时间(毫秒)
	UpdatedAt sqltime.Datetime `db:"f_updated_at,precision=3,default=CURRENT_TIMESTAMP(3),onupdate=CURRENT_TIMESTAMP(3)" json:"updatedAt"`
}

CreationModificationDatetimePrecise adds UpdatedAt to CreationDatetimeMilli.

func (*CreationModificationDatetimePrecise) DocOf added in v0.2.8

func (v *CreationModificationDatetimePrecise) DocOf(names ...string) ([]string, bool)

func (*CreationModificationDatetimePrecise) MarkCreatedAt added in v0.0.5

func (cu *CreationModificationDatetimePrecise) MarkCreatedAt()

func (*CreationModificationDatetimePrecise) MarkModifiedAt added in v0.0.5

func (cu *CreationModificationDatetimePrecise) MarkModifiedAt()

type CreationModificationDeletionDatetime

type CreationModificationDeletionDatetime struct {
	CreationModificationDatetime
	// DeletedAt 删除日期时间(秒)
	DeletedAt sqltime.Datetime `db:"f_deleted_at,default='0001-01-01 00:00:00'" json:"deletedAt"`
}

CreationModificationDeletionDatetime adds soft-deletion DeletedAt.

func (*CreationModificationDeletionDatetime) DocOf added in v0.2.8

func (v *CreationModificationDeletionDatetime) DocOf(names ...string) ([]string, bool)

func (*CreationModificationDeletionDatetime) MarkDeletedAt

func (cud *CreationModificationDeletionDatetime) MarkDeletedAt()

func (CreationModificationDeletionDatetime) SoftDeletion

type CreationModificationDeletionDatetimePrecise added in v0.0.5

type CreationModificationDeletionDatetimePrecise struct {
	CreationModificationDatetimePrecise
	// DeletedAt 删除日期时间(毫秒)
	DeletedAt sqltime.Datetime `db:"f_deleted_at,precision=3,default='0001-01-01 00:00:00.000'" json:"deletedAt"`
}

CreationModificationDeletionDatetimePrecise adds soft-deletion DeletedAt.

func (*CreationModificationDeletionDatetimePrecise) DocOf added in v0.2.8

func (*CreationModificationDeletionDatetimePrecise) MarkDeletedAt added in v0.0.5

func (cud *CreationModificationDeletionDatetimePrecise) MarkDeletedAt()

func (CreationModificationDeletionDatetimePrecise) SoftDeletion added in v0.0.5

type CreationModificationDeletionTime

type CreationModificationDeletionTime struct {
	CreationModificationTime
	// DeletedAt 删除时间 秒时间戳
	DeletedAt sqltime.Timestamp `db:"f_deleted_at,default=0" json:"deletedAt,omitzero"`
}

CreationModificationDeletionTime adds soft-deletion DeletedAt.

func (*CreationModificationDeletionTime) DocOf added in v0.2.8

func (v *CreationModificationDeletionTime) DocOf(names ...string) ([]string, bool)

func (*CreationModificationDeletionTime) MarkDeletedAt

func (cmd *CreationModificationDeletionTime) MarkDeletedAt()

func (CreationModificationDeletionTime) SoftDeletion

func (cmd CreationModificationDeletionTime) SoftDeletion() (string, []string, driver.Value)

type CreationModificationDeletionTimePrecise added in v0.0.5

type CreationModificationDeletionTimePrecise struct {
	CreationModificationTimePrecise
	// DeletedAt 删除时间 毫秒时间戳
	DeletedAt sqltime.TimestampMilli `db:"f_deleted_at,default=0" json:"deletedAt,omitzero"`
}

CreationModificationDeletionTimePrecise adds soft-deletion DeletedAt.

func (*CreationModificationDeletionTimePrecise) DocOf added in v0.2.8

func (*CreationModificationDeletionTimePrecise) MarkDeletedAt added in v0.0.5

func (cmd *CreationModificationDeletionTimePrecise) MarkDeletedAt()

func (CreationModificationDeletionTimePrecise) SoftDeletion added in v0.0.5

type CreationModificationTime

type CreationModificationTime struct {
	CreationTime
	// UpdatedAt 更新时间 秒时间戳
	UpdatedAt sqltime.Timestamp `db:"f_updated_at,default=0" json:"updatedAt"`
}

CreationModificationTime adds UpdatedAt to CreationTime.

func (*CreationModificationTime) DocOf added in v0.2.8

func (v *CreationModificationTime) DocOf(names ...string) ([]string, bool)

func (*CreationModificationTime) MarkCreatedAt

func (cu *CreationModificationTime) MarkCreatedAt()

func (*CreationModificationTime) MarkModifiedAt

func (cu *CreationModificationTime) MarkModifiedAt()

type CreationModificationTimePrecise added in v0.0.5

type CreationModificationTimePrecise struct {
	CreationTimePrecise
	// UpdatedAt 更新时间 毫秒时间戳
	UpdatedAt sqltime.TimestampMilli `db:"f_updated_at,default=0" json:"updatedAt"`
}

CreationModificationTimePrecise adds UpdatedAt to CreationTimePrecise.

func (*CreationModificationTimePrecise) DocOf added in v0.2.8

func (v *CreationModificationTimePrecise) DocOf(names ...string) ([]string, bool)

func (*CreationModificationTimePrecise) MarkCreatedAt added in v0.0.5

func (cu *CreationModificationTimePrecise) MarkCreatedAt()

func (*CreationModificationTimePrecise) MarkModifiedAt added in v0.0.5

func (cu *CreationModificationTimePrecise) MarkModifiedAt()

type CreationTime

type CreationTime struct {
	// CreatedAt 创建时间 秒时间戳
	CreatedAt sqltime.Timestamp `db:"f_created_at,default=0" json:"createdAt"`
}

CreationTime holds CreatedAt as a second-precision unix timestamp.

func (*CreationTime) DocOf added in v0.2.8

func (v *CreationTime) DocOf(names ...string) ([]string, bool)

func (*CreationTime) MarkCreatedAt

func (c *CreationTime) MarkCreatedAt()

type CreationTimePrecise added in v0.0.5

type CreationTimePrecise struct {
	// CreatedAt 创建时间 毫秒时间戳
	CreatedAt sqltime.TimestampMilli `db:"f_created_at,default=0" json:"createdAt"`
}

CreationTimePrecise holds CreatedAt as a millisecond unix timestamp.

func (*CreationTimePrecise) DocOf added in v0.2.8

func (v *CreationTimePrecise) DocOf(names ...string) ([]string, bool)

func (*CreationTimePrecise) MarkCreatedAt added in v0.0.5

func (c *CreationTimePrecise) MarkCreatedAt()

type DBTypeAdapter

type DBTypeAdapter interface {
	WithDBType(driver string)
}

DBTypeAdapter allows overriding the SQL datatype string.

type DBValue

type DBValue interface {
	driver.Valuer
	sql.Scanner
	DBType(driver string) string
}

DBValue can convert between RDB and Go values and describe the RDB datatype.

type Decimal

type Decimal struct {
	decimal.Decimal
}

Decimal decimal number

func AsDecimal

func AsDecimal(v decimal.Decimal) Decimal

AsDecimal wraps a shopspring decimal.Decimal.

func (*Decimal) DBType

func (d *Decimal) DBType(driver string) string

func (*Decimal) DocOf added in v0.2.8

func (v *Decimal) DocOf(names ...string) ([]string, bool)

func (Decimal) OpenAPISchemaFormat added in v0.2.8

func (Decimal) OpenAPISchemaFormat() string

func (Decimal) OpenAPISchemaType added in v0.2.8

func (Decimal) OpenAPISchemaType() []string

type DeletionMarker

type DeletionMarker interface {
	MarkDeletedAt()
}

DeletionMarker marks a record as deleted.

type ID

type ID uint64

ID is a generic unsigned identifier.

func (*ID) DocOf added in v0.2.8

func (v *ID) DocOf(names ...string) ([]string, bool)

type JSONArray

type JSONArray[T any] struct {
	JSONDBType
	// contains filtered or unexported fields
}

JSONArray stores a typed slice as JSON TEXT.

func JSONArrayOf

func JSONArrayOf[T any](s []T) *JSONArray[T]

JSONArrayOf wraps a slice as a JSON array column value.

func (*JSONArray[T]) DocOf added in v0.2.8

func (v *JSONArray[T]) DocOf(names ...string) ([]string, bool)

func (*JSONArray[T]) Get

func (v *JSONArray[T]) Get() []T

func (JSONArray[T]) IsZero

func (v JSONArray[T]) IsZero() bool

func (JSONArray[T]) MarshalJSON

func (v JSONArray[T]) MarshalJSON() ([]byte, error)

func (*JSONArray[T]) Scan

func (v *JSONArray[T]) Scan(src any) error

func (*JSONArray[T]) Set

func (v *JSONArray[T]) Set(x []T)

func (*JSONArray[T]) UnmarshalJSON

func (v *JSONArray[T]) UnmarshalJSON(data []byte) error

func (JSONArray[T]) Value

func (v JSONArray[T]) Value() (driver.Value, error)

type JSONDBType

type JSONDBType struct {
	// contains filtered or unexported fields
}

JSONDBType holds an optional SQL datatype override for JSON columns.

func (JSONDBType) DBType

func (t JSONDBType) DBType(driver string) string

func (*JSONDBType) DocOf added in v0.2.8

func (v *JSONDBType) DocOf(names ...string) ([]string, bool)

func (*JSONDBType) WithDBType

func (t *JSONDBType) WithDBType(typ string)

type JSONObject

type JSONObject[T any] struct {
	JSONDBType
	// contains filtered or unexported fields
}

JSONObject stores a typed struct/map as JSON TEXT.

func JSONObjectOf

func JSONObjectOf[T any](v *T) JSONObject[T]

JSONObjectOf wraps a struct/map pointer as a JSON object column value.

func (*JSONObject[T]) DocOf added in v0.2.8

func (v *JSONObject[T]) DocOf(names ...string) ([]string, bool)

func (*JSONObject[T]) Get

func (v *JSONObject[T]) Get() *T

func (JSONObject[T]) IsZero

func (v JSONObject[T]) IsZero() bool

func (JSONObject[T]) MarshalJSON

func (v JSONObject[T]) MarshalJSON() ([]byte, error)

func (*JSONObject[T]) Scan

func (v *JSONObject[T]) Scan(src any) error

func (*JSONObject[T]) Set

func (v *JSONObject[T]) Set(x *T)

func (*JSONObject[T]) UnmarshalJSON

func (v *JSONObject[T]) UnmarshalJSON(data []byte) error

func (JSONObject[T]) Value

func (v JSONObject[T]) Value() (driver.Value, error)

type ModificationMarker

type ModificationMarker interface {
	MarkModifiedAt()
}

ModificationMarker marks a record as modified.

type OperationDatetime

type OperationDatetime = CreationModificationDeletionDatetime

OperationDatetime is an alias of CreationModificationDeletionDatetime.

type OperationDatetimePrecise added in v0.0.5

type OperationDatetimePrecise = CreationModificationDeletionDatetimePrecise

OperationDatetimePrecise is an alias of the full create/modify/delete datetime embed.

type OperationTime

type OperationTime = CreationModificationDeletionTime

OperationTime is an alias of CreationModificationDeletionTime.

type OperationTimePrecise added in v0.0.5

type OperationTimePrecise = CreationModificationDeletionTimePrecise

OperationTimePrecise is an alias of CreationModificationDeletionTimePrecise.

type Serial added in v0.3.7

type Serial struct {
	// ID 自增主键
	ID int64 `db:"f_id,autoinc" json:"-"`
}

Serial is an auto-increment primary key embed (column f_id).

ID is int64 rather than uint64 for two reasons:

  1. SQL integer types used for identity columns are signed in the SQL standard and in common dialects (INTEGER / BIGINT; PostgreSQL SERIAL / BIGSERIAL map to signed integers). Unsigned 64-bit integers are not a portable auto-increment type across databases.

  2. database/sql treats integer driver.Value as int64. Passing a uint64 with the high bit set (value >= 1<<63) fails with "uint64 values with high bit set are not supported". Result.LastInsertId also returns int64. Using int64 keeps insert args, scan targets, and last-insert-id in one representation without conversion edge cases.

ref:

func (*Serial) DocOf added in v0.3.7

func (v *Serial) DocOf(names ...string) ([]string, bool)

type SoftDeletion

type SoftDeletion interface {
	// SoftDeletion returns soft deletion field name, modification fields if any,
	// and the default value of the deletion field.
	SoftDeletion() (deletion string, modifications []string, v driver.Value)
}

SoftDeletion describes soft-deletion field metadata.

type Text

type Text string

Text nullable text

func (Text) DBType

func (Text) DBType(driver string) string

func (*Text) DocOf added in v0.2.8

func (v *Text) DocOf(names ...string) ([]string, bool)

func (*Text) Scan

func (v *Text) Scan(src any) error

func (*Text) Set

func (v *Text) Set(str string)

func (Text) Value

func (v Text) Value() (driver.Value, error)

type ULID added in v0.3.9

type ULID struct {
	ulid.ULID
}

func MakeULID added in v0.3.9

func MakeULID() ULID

func (ULID) BigInt added in v0.3.9

func (u ULID) BigInt() *big.Int

func (ULID) DBFixedWidth added in v0.3.9

func (u ULID) DBFixedWidth(driver string) *uint64

func (ULID) DBType added in v0.3.9

func (ULID) DBType(driver string) string

func (*ULID) DocOf added in v0.3.9

func (v *ULID) DocOf(names ...string) ([]string, bool)

Directories

Path Synopsis
Package sqlops provides operation-time embeds with unprefixed column names.
Package sqlops provides operation-time embeds with unprefixed column names.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL