Documentation
¶
Overview ¶
Package sqlx provides a generator for generating sqlx model related implementations.
How to Integrate ¶
import _ "github.com/xoctopus/sqlx/devpkg/sqlx/v1"
ctx := genx.NewContext(&genx.Args{
Entrypoint: []string{entry},
})
_ = ctx.Execute(context.Background(), genx.Get()...)
Code Definition Conventions ¶
- Add `// +genx:model` comment to your model type.
- Use `@model` annotations in its comment to configure the generator (e.g., `// @model TableName=t_product`, `// @model Register=Catalog`, `// @model pk=ID`, `// @model uidx=...`, `// @model idx=...`).
- Map columns with `db` struct tags (e.g., `db:"name,width=128"`, `db:"f_id,autoinc"`).
- Soft relations use `// @model rel=<ModelType>.<Field>` on the related field comment (does not create foreign keys).
Index fields are separated by `;`; options by `,` (e.g., `// @model idx=i_name,BTREE;Name,NULLS,FIRST;DeletedAt`).
Prefer composing reusable embeds such as `types.AutoIncID`, `Rel*`, `Meta`, `State`, and operation-time types from `pkg/types` / `pkg/types/sqlops`. See `github.com/xoctopus/sqlx/example/models`.
Example:
var Catalog = builder.NewCatalog()
// Product 商品
// +genx:model
// @model TableName=t_product
// @model Register=Catalog
// @model pk=ID
// @model uidx=ui_product_id;ProductID;DeletedAt
// @model idx=i_product_name;Name
type Product struct {
types.AutoIncID
RelProduct
ProductMeta
types.CreationModificationDeletionTime
}
type RelProduct struct {
// @model rel=Product.ProductID
ProductID ProductID `db:"product_id"`
}
Example ¶
package main
import (
"context"
"os"
"path/filepath"
_ "github.com/xoctopus/genx/devpkg/docx"
_ "github.com/xoctopus/genx/devpkg/enumx"
"github.com/xoctopus/genx/pkg/genx"
"github.com/xoctopus/x/misc/must"
_ "github.com/xoctopus/sqlx/devpkg/sqlx/v1"
)
func main() {
root := filepath.Join(must.NoErrorV(os.Getwd()), "..", "..", "..")
ctx := genx.NewContext(&genx.Args{
Entrypoint: []string{
filepath.Join(root, "testdata"),
filepath.Join(root, "testdata", "v2"),
filepath.Join(root, "example", "models"),
},
})
if err := ctx.Execute(context.Background(), genx.Get()...); err != nil {
panic(err)
}
}
Output:
Index ¶
- func Span() func() time.Duration
- type Attr
- type Model
- func (m *Model) ColumnRelList() s.Snippet
- func (m *Model) CommentOf(ref string) s.Snippet
- func (m *Model) CreationMarker() s.Snippet
- func (m *Model) DeletionMarker() s.Snippet
- func (m *Model) HasSoftDeletion() bool
- func (m *Model) Ident() s.Snippet
- func (m *Model) IndexList(unique bool) s.Snippet
- func (m *Model) ModeledCT(ctx context.Context, t typx.Type) s.Snippet
- func (m *Model) ModeledColDefList(ctx context.Context) s.Snippet
- func (m *Model) ModeledColInitList(ctx context.Context) s.Snippet
- func (m *Model) ModeledKeyDefList(ctx context.Context) s.Snippet
- func (m *Model) ModeledKeyInitList(_ context.Context) s.Snippet
- func (m *Model) ModeledM(ctx context.Context) s.Snippet
- func (m *Model) ModeledTCol(ctx context.Context, t typx.Type) s.Snippet
- func (m *Model) ModeledTable(ctx context.Context) s.Snippet
- func (m *Model) ModificationMarker() s.Snippet
- func (m *Model) PrimaryColList() s.Snippet
- func (m *Model) Register() s.Snippet
- func (m *Model) SoftDeletionCondition(ctx context.Context) s.Snippet
- func (m *Model) TableDesc() (snip s.Snippet)
- func (m *Model) TableName() s.Snippet
- func (m *Model) TagMapping() s.Snippet
- func (m *Model) UniqueConditions(ctx context.Context, names []string) s.Snippet
- func (m *Model) UniqueFields(names []string) s.Snippet
- func (m *Model) UniqueNames() [][]string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
func (*Model) ColumnRelList ¶ added in v0.3.1
func (*Model) CreationMarker ¶ added in v0.0.3
func (*Model) DeletionMarker ¶ added in v0.0.3
func (*Model) HasSoftDeletion ¶ added in v0.0.3
func (*Model) ModeledColInitList ¶
func (*Model) ModificationMarker ¶ added in v0.0.3
func (*Model) PrimaryColList ¶
func (*Model) SoftDeletionCondition ¶ added in v0.0.3
func (*Model) TagMapping ¶ added in v0.3.1
func (*Model) UniqueConditions ¶ added in v0.0.3
func (*Model) UniqueNames ¶ added in v0.0.3
Click to show internal directories.
Click to hide internal directories.