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@devel DO NOT EDIT.
Index ¶
- func DriverJSONValue(v any) (driver.Value, error)
- func HasSoftDeletion[M internal.Model]() bool
- func ScanJSONValue(src, dst any) error
- type ArrayAsList
- func (aa *ArrayAsList[T]) Append(values ...T)
- func (aa ArrayAsList[T]) DBType(driver string) string
- func (v *ArrayAsList[T]) DocOf(names ...string) ([]string, bool)
- func (aa ArrayAsList[T]) Elements() []string
- func (aa *ArrayAsList[T]) MarshalJSON() ([]byte, error)
- func (aa *ArrayAsList[T]) MarshalText() ([]byte, error)
- func (aa *ArrayAsList[T]) Scan(v any) error
- func (aa ArrayAsList[T]) String() string
- func (aa *ArrayAsList[T]) UnmarshalJSON(data []byte) error
- func (aa *ArrayAsList[T]) UnmarshalText(data []byte) error
- func (aa ArrayAsList[T]) Value() (driver.Value, error)
- type AutoIncIDdeprecated
- type Blob
- type Bool
- func (v Bool) Bool() bool
- func (v *Bool) DocOf(names ...string) ([]string, bool)
- func (v Bool) MarshalJSON() ([]byte, error)
- func (v Bool) MarshalText() ([]byte, error)
- func (Bool) OpenAPISchemaType() []string
- func (v *Bool) UnmarshalJSON(data []byte) (err error)
- func (v *Bool) UnmarshalText(data []byte) error
- type CreationDatetime
- type CreationDatetimeMilli
- type CreationMarker
- type CreationModificationDatetime
- type CreationModificationDatetimePrecise
- type CreationModificationDeletionDatetime
- type CreationModificationDeletionDatetimePrecise
- type CreationModificationDeletionTime
- type CreationModificationDeletionTimePrecise
- type CreationModificationTime
- type CreationModificationTimePrecise
- type CreationTime
- type CreationTimePrecise
- type DBTypeAdapter
- type DBValue
- type Decimal
- type DeletionMarker
- type ID
- type JSONArray
- func (v *JSONArray[T]) DocOf(names ...string) ([]string, bool)
- func (v *JSONArray[T]) Get() []T
- func (v JSONArray[T]) IsZero() bool
- func (v JSONArray[T]) MarshalJSON() ([]byte, error)
- func (v *JSONArray[T]) Scan(src any) error
- func (v *JSONArray[T]) Set(x []T)
- func (v *JSONArray[T]) UnmarshalJSON(data []byte) error
- func (v JSONArray[T]) Value() (driver.Value, error)
- type JSONDBType
- type JSONObject
- func (v *JSONObject[T]) DocOf(names ...string) ([]string, bool)
- func (v *JSONObject[T]) Get() *T
- func (v JSONObject[T]) IsZero() bool
- func (v JSONObject[T]) MarshalJSON() ([]byte, error)
- func (v *JSONObject[T]) Scan(src any) error
- func (v *JSONObject[T]) Set(x *T)
- func (v *JSONObject[T]) UnmarshalJSON(data []byte) error
- func (v JSONObject[T]) Value() (driver.Value, error)
- type ModificationMarker
- type OperationDatetime
- type OperationDatetimePrecise
- type OperationTime
- type OperationTimePrecise
- type Serial
- type SoftDeletion
- type TElement
- type Text
- type ULID
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DriverJSONValue ¶
DriverJSONValue unmarshal input to driver.Value
func HasSoftDeletion ¶
HasSoftDeletion reports whether M implements SoftDeletion.
func ScanJSONValue ¶
ScanJSONValue scan database input to value
Types ¶
type ArrayAsList ¶ added in v0.0.5
type ArrayAsList[T TElement] []T
ArrayAsList stores a typed slice as a comma-separated TEXT column.
func ParseArrayAsList ¶ added in v0.3.6
func ParseArrayAsList[T TElement](s string) (ArrayAsList[T], error)
ParseArrayAsList parses a comma-separated list into ArrayAsList[T]. Empty parts (after TrimSpace) 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.
type Bool ¶
type Bool int
Bool a ternary boolean state UNKNOWN, TRUE or FALSE
const ( TRUE Bool // true FALSE // false )
func (Bool) MarshalJSON ¶
func (Bool) MarshalText ¶
func (Bool) OpenAPISchemaType ¶ added in v0.2.8
func (*Bool) UnmarshalJSON ¶
func (*Bool) UnmarshalText ¶
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 ¶
func (cud CreationModificationDeletionDatetime) SoftDeletion() (string, []string, driver.Value)
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 (v *CreationModificationDeletionDatetimePrecise) DocOf(names ...string) ([]string, bool)
func (*CreationModificationDeletionDatetimePrecise) MarkDeletedAt ¶ added in v0.0.5
func (cud *CreationModificationDeletionDatetimePrecise) MarkDeletedAt()
func (CreationModificationDeletionDatetimePrecise) SoftDeletion ¶ added in v0.0.5
func (cud CreationModificationDeletionDatetimePrecise) SoftDeletion() (string, []string, driver.Value)
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 (v *CreationModificationDeletionTimePrecise) DocOf(names ...string) ([]string, bool)
func (*CreationModificationDeletionTimePrecise) MarkDeletedAt ¶ added in v0.0.5
func (cmd *CreationModificationDeletionTimePrecise) MarkDeletedAt()
func (CreationModificationDeletionTimePrecise) SoftDeletion ¶ added in v0.0.5
func (cmd CreationModificationDeletionTimePrecise) SoftDeletion() (string, []string, driver.Value)
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 Decimal ¶
Decimal decimal number
func (Decimal) OpenAPISchemaFormat ¶ added in v0.2.8
func (Decimal) OpenAPISchemaType ¶ added in v0.2.8
type DeletionMarker ¶
type DeletionMarker interface {
MarkDeletedAt()
}
DeletionMarker marks a record as deleted.
type JSONArray ¶
type JSONArray[T any] struct { JSONDBType // contains filtered or unexported fields }
JSONArray stores a typed slice as JSON TEXT.
func JSONArrayOf ¶
JSONArrayOf wraps a slice as a JSON array column value.
func (JSONArray[T]) MarshalJSON ¶
func (*JSONArray[T]) UnmarshalJSON ¶
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
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:
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.
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:
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 TElement ¶ added in v0.3.6
type TElement interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
~float32 | ~float64 |
~string
}
TElement is the element constraint for ArrayAsList. Empty string elements and commas inside string elements are not supported.