construct

package
v0.6.12 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2022 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ModelClasses = map[string]ClassSummary{
	"base.TimeModel": {
		Name:   "base.TimeModel",
		Import: "github.com/azhai/gozzo-db/construct",
		Alias:  "base",
		Features: []string{
			"CreatedAt time.Time",
			"UpdatedAt time.Time",
			"DeletedAt *time.Time",
		},
		FieldLines: []string{
			"CreatedAt time.Time  `json:\"-\" gorm:\"comment:'创建时间'\"`       // 创建时间",
			"UpdatedAt time.Time  `json:\"-\" gorm:\"comment:'更新时间'\"`       // 更新时间",
			"DeletedAt *time.Time `json:\"-\" gorm:\"index;comment:'删除时间'\"` // 删除时间",
		},
	},
	"*base.NestedModel": {
		Name:   "*base.NestedModel",
		Import: "github.com/azhai/gozzo-db/construct",
		Alias:  "base",
		Features: []string{
			"Lft uint",
			"Rgt uint",
			"Depth uint8",
		},
		FieldLines: []string{
			"Lft   uint  `json:\"-\" gorm:\"not null;default:0;comment:'左边界'\"`          // 左边界",
			"Rgt   uint  `json:\"-\" gorm:\"not null;index;default:0;comment:'右边界'\"`    // 右边界",
			"Depth uint8 `json:\"depth\" gorm:\"not null;index;default:1;comment:'高度'\"` // 高度",
		},
	},
}

Functions

func GuessTypeName

func GuessTypeName(ci *schema.ColumnInfo) string

根据数据库字段类型猜测Go类型,适用于mysql或pgsql

func IgnoreNotFoundError added in v0.6.0

func IgnoreNotFoundError(err error) error

忽略表中无数据的错误

func MoveEdge added in v0.6.0

func MoveEdge(query *gorm.DB, base uint, offset string) (err error)

左右边界整体移动

func Paginate

func Paginate(query *gorm.DB, out interface{}, pageno, pagesize int) (total int, err error)

*

  • 翻页查询,out参数需要传引用
  • 使用方法 total, err := Paginate(query, &rows, pageno, pagesize)

func ReplaceModelFields added in v0.6.3

func ReplaceModelFields(cp *rewrite.CodeParser, node *rewrite.DeclNode, summary ClassSummary)

func ScanModelDir added in v0.6.3

func ScanModelDir(dir string)

Types

type ClassSummary added in v0.6.3

type ClassSummary struct {
	Name          string
	Import, Alias string
	Features      []string

	FieldLines []string
	IsChanged  bool
	// contains filtered or unexported fields
}

func NewClassSummary added in v0.6.3

func NewClassSummary(name string) ClassSummary

func ReplaceSummary added in v0.6.3

func ReplaceSummary(summary, sub ClassSummary) ClassSummary

func (ClassSummary) GetInnerCode added in v0.6.3

func (s ClassSummary) GetInnerCode() string

func (ClassSummary) GetSortedFeatures added in v0.6.3

func (s ClassSummary) GetSortedFeatures() []string

func (*ClassSummary) ParseFields added in v0.6.3

func (s *ClassSummary) ParseFields(cp *rewrite.CodeParser, node *rewrite.DeclNode) int

type FilterFunc

type FilterFunc = func(query *gorm.DB) *gorm.DB

*

  • 过滤查询
  • 使用方法 query = query.Scopes(filters ...FilterFunc)

type ITableComment added in v0.5.5

type ITableComment interface {
	TableComment() string
}

*

  • 数据表注释

type ITableName added in v0.5.6

type ITableName interface {
	TableName() string
}

*

  • 数据表名

type Model

type Model struct {
	ID uint `json:"id" toml:"-" gorm:"primary_key;not null;auto_increment"`
}

*

  • 带自增主键的基础Model

func (Model) TableComment added in v0.5.5

func (Model) TableComment() string

type NestedModel added in v0.6.3

type NestedModel struct {
	Lft   uint  `json:"-" gorm:"not null;default:0;comment:'左边界'"`          // 左边界
	Rgt   uint  `json:"-" gorm:"not null;index;default:0;comment:'右边界'"`    // 右边界
	Depth uint8 `json:"depth" gorm:"not null;index;default:1;comment:'高度'"` // 高度
}

嵌套集合树

func (*NestedModel) AddToParent added in v0.6.3

func (n *NestedModel) AddToParent(parent *NestedModel, tbQuery *gorm.DB) error

添加到父节点最末,tbQuery一定要使用db.Table(...)

func (NestedModel) AncestorsFilter added in v0.6.3

func (n NestedModel) AncestorsFilter(Backward bool) FilterFunc

找出所有直系祖先节点

func (NestedModel) ChildrenFilter added in v0.6.3

func (n NestedModel) ChildrenFilter(rank uint8) FilterFunc

找出所有子孙节点

func (NestedModel) CountChildren added in v0.6.3

func (n NestedModel) CountChildren() int

有多少个子孙节点

func (NestedModel) IsLeaf added in v0.6.3

func (n NestedModel) IsLeaf() bool

是否叶子节点

type SqlTag

type SqlTag struct {
	reflect.StructTag
	// contains filtered or unexported fields
}

StructTag named sql or gorm

func GuessStructTags

func GuessStructTags(ci *schema.ColumnInfo) *SqlTag

根据数据库字段属性设置Struct tags

func NewSqlTag

func NewSqlTag() *SqlTag

func (*SqlTag) Delete

func (st *SqlTag) Delete(key string)

Deletes a tag

func (*SqlTag) Get

func (st *SqlTag) Get(key string) (string, bool)

Returns a tag from the tag data

func (*SqlTag) ParseTag

func (st *SqlTag) ParseTag(tag reflect.StructTag)

func (*SqlTag) Set

func (st *SqlTag) Set(key, val string)

Sets a tag in the tag data map

func (*SqlTag) String

func (st *SqlTag) String(head string) string

转为字符串格式,头通常使用sql或gorm

type TimeModel added in v0.6.3

type TimeModel struct {
	CreatedAt time.Time  `json:"-" gorm:"comment:'创建时间'"`       // 创建时间
	UpdatedAt time.Time  `json:"-" gorm:"comment:'更新时间'"`       // 更新时间
	DeletedAt *time.Time `json:"-" gorm:"index;comment:'删除时间'"` // 删除时间
}

*

  • 时间相关的三个典型字段

Jump to

Keyboard shortcuts

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