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

def

package
v0.4.1-rc0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package def parses column and index definitions used across sqlx.

structs.FieldsFor attaches a ColumnDef to each model field; [builder] and dialects consume it for DDL. The genx model generator uses ParseKeyDef to turn `@model pk|idx|uidx=...` annotations into KeyDefine values.

ColumnDef

d := def.ParseColDef(t, `db:"name,width=128,default='',null"`)

ParseColDef reads the `db` struct tag options into ColumnDef:

  • null / autoinc / unsigned
  • width / precision
  • default (raw string, including quotes or SQL functions)
  • onupdate
  • deprecated (optional rename target for migrations)

DefineFromCatalog / DefineFromUser / DefineFromGoType record where a definition originated when filled later by catalog scan or dialect.

KeyDefine

ParseKeyDef parses `@model` index annotation values. See that function's comment and ExampleParseKeyDef for annotation → parse mappings.

pk=<field>[;<field>...]
idx=<name[,method]>;<field[,options...]>[;...]
uidx=<name[,method]>;<field[,options...]>[;...]

Fields are separated by `;`, options by `,`. KeyColumnOption holds a field (or column) name plus per-column options such as NULLS,FIRST. Helpers: ResolveIndexNameAndUsing, ResolveKeyColumnOptions, KeyColumnOptionByNames.

Index

Examples

Constants

View Source
const (
	// DefineFromCatalog scan from database
	DefineFromCatalog = 1
	// DefineFromUser type implements builder.ColDef interface
	DefineFromUser = 2
	// DefineFromGoType parse depends on Go type
	DefineFromGoType = 3
)

Variables

This section is empty.

Functions

func ResolveIndexNameAndUsing

func ResolveIndexNameAndUsing(s string) (name string, using string)

Types

type ColumnDef

type ColumnDef struct {
	Type       typx.Type
	Tag        reflect.StructTag
	DataType   string
	IsUnsigned *bool
	// Width describes the max storage space.
	// It's valid when not nil and greater than 0
	// e.g. DECIMAL(10) VARCHAR(128) DATETIME(3)
	Width *uint64
	// Precision describes the max storage precision space.
	// e.g. DECIMAL(10,2)
	Precision  *uint64
	Default    *string
	OnUpdate   *string
	Null       bool
	AutoInc    bool
	Comment    string
	Desc       []string
	Relation   []string
	Deprecated *DeprecatedActions
	DefineFrom int
}

ColumnDef describes source and database model

func ParseColDef

func ParseColDef(t typx.Type, tag reflect.StructTag) *ColumnDef

func (*ColumnDef) ParseDBTag

func (d *ColumnDef) ParseDBTag(flag *reflectx.Flag)

type ColumnDef2 added in v0.3.1

type ColumnDef2 struct {
	Type     typx.Type
	Tag      reflect.StructTag
	DataType string

	IsUnsigned       bool
	NumericPrecision *uint64
	NumericScale     *uint64

	DatetimePrecision *uint64

	TextLength *uint64

	AutoInc      bool
	Nullable     bool
	DefaultValue *string
	OnUpdate     *string

	Comment  string
	Desc     []string
	Relation []string

	Deprecated *DeprecatedActions
}

type DeprecatedActions

type DeprecatedActions struct {
	RenameTo string `name:"rename"`
}

type KeyColumnOption

type KeyColumnOption struct {
	Name    string // maybe column name or field name
	Options []string
}

func KeyColumnOptionByNames

func KeyColumnOptionByNames(names ...string) []KeyColumnOption

func ResolveKeyColumnOptions

func ResolveKeyColumnOptions(s string) KeyColumnOption

func ResolveKeyColumnOptionsFromStrings

func ResolveKeyColumnOptionsFromStrings(ss ...string) (options []KeyColumnOption)

func (*KeyColumnOption) String

func (o *KeyColumnOption) String() string

type KeyDefine

type KeyDefine struct {
	Kind    KeyKind
	Name    string
	Using   string
	Comment string
	Options []KeyColumnOption
}

func ParseKeyDef

func ParseKeyDef(k, def string) *KeyDefine

ParseKeyDef parses an `@model` primary-key or index annotation value.

k is the annotation key: "pk", "idx", or "uidx". def is the annotation value after `=`.

Separators: fields use `;`; name/method and field options use `,`.

Annotation → ParseKeyDef(k, def):

// @model pk=ID
ParseKeyDef("pk", "ID")
// Kind=PRIMARY Name=primary Options=[{ID}]

// @model idx=i_status;Status
ParseKeyDef("idx", "i_status;Status")
// Kind=INDEX Name=i_status Options=[{Status}]

// @model idx=ui_username,BTREE;Username
ParseKeyDef("idx", "ui_username,BTREE;Username")
// Kind=INDEX Name=ui_username Using=BTREE Options=[{Username}]

// @model idx=i_status;Status,NULLS,FIRST
ParseKeyDef("idx", "i_status;Status,NULLS,FIRST")
// Kind=INDEX Name=i_status Options=[{Status [NULLS FIRST]}]

// @model uidx=ui_product_id;ProductID;DeletedAt
ParseKeyDef("uidx", "ui_product_id;ProductID;DeletedAt")
// Kind=UNIQUE Name=ui_product_id Options=[{ProductID} {DeletedAt}]

// @model uidx=ui_name;f_org_id,NULLS,FIRST;MemberID
ParseKeyDef("uidx", "ui_name;f_org_id,NULLS,FIRST;MemberID")
// Kind=UNIQUE Name=ui_name Options=[{f_org_id [NULLS FIRST]} {MemberID}]

Tabular form of def:

| Kind | Name[,Using]       | Field[,Option]...              |
| :--- | :---               | :----                          |
| idx  | idx_name,BTREE     | Name                           |
| uidx | idx_name           | OrgID;f_member_id,NULLS,FIRST  |
| pk   | (forced "primary") | ID                             |
Example
// // @model pk=ID
kd := def.ParseKeyDef("pk", "ID")
fmt.Printf("pk name=%s fields=%v\n", kd.Name, kd.OptionsNames())

// // @model idx=ui_username,BTREE;Username
kd = def.ParseKeyDef("idx", "ui_username,BTREE;Username")
fmt.Printf("idx name=%s using=%s fields=%v\n", kd.Name, kd.Using, kd.OptionsNames())

// // @model idx=i_status;Status,NULLS,FIRST
kd = def.ParseKeyDef("idx", "i_status;Status,NULLS,FIRST")
fmt.Printf("idx fields=%v\n", kd.OptionsStrings())

// // @model uidx=ui_product_id;ProductID;DeletedAt
kd = def.ParseKeyDef("uidx", "ui_product_id;ProductID;DeletedAt")
fmt.Printf("uidx name=%s fields=%v\n", kd.Name, kd.OptionsNames())
Output:
pk name=primary fields=[ID]
idx name=ui_username using=BTREE fields=[Username]
idx fields=[Status,NULLS,FIRST]
uidx name=ui_product_id fields=[ProductID DeletedAt]

func (*KeyDefine) OptionsNames

func (d *KeyDefine) OptionsNames() []string

func (*KeyDefine) OptionsStrings

func (d *KeyDefine) OptionsStrings() []string

type KeyKind

type KeyKind int8
const (
	KEY_KIND__INDEX KeyKind = iota + 1
	KEY_KIND__UNIQUE_INDEX
	KEY_KIND__PRIMARY
)

Jump to

Keyboard shortcuts

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