Documentation ¶
Index ¶
Constants ¶
View Source
const BaseRepoMethod = NotEditMark + Header + `
var _ {{.InterfaceName}}Repo = (*default{{.StructName}})(nil)
const TableName{{.StructName}} = "{{.TableName}}"
type (
{{.InterfaceName}}Repo interface {
Insert(ctx context.Context,data *{{.StructName}}) ({{.PrimaryKeyType}}, error)
BatchInsert(ctx context.Context,list []*{{.StructName}}) ([]{{.PrimaryKeyType}}, error)
FindOne(ctx context.Context, id {{.PrimaryKeyType}}) (*{{.StructName}}, error)
Update(ctx context.Context, newData *{{.StructName}}, column []string) (int64, error)
UpdateColumns(ctx context.Context, id {{.PrimaryKeyType}}, newData map[string]any) (int64, error)
SoftDelete(ctx context.Context,ids []{{.PrimaryKeyType}}) error
Delete(ctx context.Context,ids []{{.PrimaryKeyType}}) error
}
// {{.StructName}} {{.StructComment}}
{{.StructName}} struct {
{{range .Fields}}
{{.Name}} {{.Type}} ` + "`{{.Tags}}` {{.CommentTag}}" +
`{{end}}
}
default{{.StructName}} struct {
*Conn
repo *{{.StructName}}
}
)
func (*{{.StructName}}) TableName() string {
return TableName{{.StructName}}
}
func new{{.StructName}}Repo(c *Conn) *default{{.StructName}} {
return &default{{.StructName}}{
Conn: c,
repo: &{{.StructName}}{},
}
}
func (d *default{{.StructName}}) Insert(ctx context.Context,data *{{.StructName}}) ({{.PrimaryKeyType}}, error) {
data.Id = 0
err := d.WithContext(ctx).Create(data).Error
if err != nil {
return 0,err
}
return data.Id,nil
}
func (d *default{{.StructName}}) BatchInsert(ctx context.Context,list []*{{.StructName}}) ([]{{.PrimaryKeyType}}, error) {
err := d.WithContext(ctx).Create(list).Error
if err != nil {
return nil,err
}
ids := make([]{{.PrimaryKeyType}}, len(list))
for i, v := range list {
ids[i] = v.Id
}
return ids,nil
}
func (d *default{{.StructName}}) FindOne(ctx context.Context,id {{.PrimaryKeyType}}) (*{{.StructName}}, error) {
result:= &{{.StructName}}{}
err := d.WithContext(ctx).Where(" id = ? ", id).First(result).Error
if err != nil {
return nil, err
}
return result, nil
}
func (d *default{{.StructName}}) Update(ctx context.Context,newData *{{.StructName}}, column []string) (int64, error) {
engine := d.WithContext(ctx).Model(d.repo)
if len(column) > 0 {
engine = engine.Select(column)
}
result := engine.Omit("id").Where(" id = ? ", newData.Id).Updates(newData)
return result.RowsAffected, result.Error
}
func (d *default{{.StructName}}) UpdateColumns(ctx context.Context,id {{.PrimaryKeyType}}, newData map[string]any) (int64, error) {
result := d.WithContext(ctx).Model(d.repo).Where(" id = ? ", id).Updates(newData)
return result.RowsAffected, result.Error
}
func (d *default{{.StructName}}) SoftDelete(ctx context.Context,ids []{{.PrimaryKeyType}}) error {
err := d.WithContext(ctx).Where(" id IN (?) ", ids).Delete(d.repo).Error
return err
}
func (d *default{{.StructName}}) Delete(ctx context.Context, ids []{{.PrimaryKeyType}}) error {
err := d.WithContext(ctx).Where(" id IN (?) ", ids).Unscoped().Delete(d.repo).Error
return err
}
`
View Source
const Header = `
package {{.PackageName}}
import (
"context"
"time"
"gorm.io/gorm"
)
`
View Source
const InterfaceMethod = `` /* 592-byte string literal not displayed */
View Source
const NotEditMark = `
// Code generated by SqlGen. DO NOT EDIT.
`
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Column ¶
type Column struct { gorm.ColumnType TableName string `gorm:"column:TABLE_NAME"` Indexes []*ColumnIndex `gorm:"-"` UseScanType bool `gorm:"-"` }
type ColumnIndex ¶
Index table index info
type Config ¶
type Config struct { DbConn *gorm.DB // db connection OutPath string // query code path PackageName string // generated model code's package name // generate model global configuration FieldNullable bool // generate pointer when field is nullable FieldCoverable bool // generate pointer when field has default value, to fix problem zero value cannot be assign: https://gorm.io/docs/create.html#Default-Values FieldSignable bool // detect integer field's unsigned type, adjust generated data type FieldWithIndexTag bool // generate with gorm index tag FieldWithTypeTag bool // generate with gorm column type tag }
Config generator's basic configuration
type Field ¶
type Field struct { Name string Type string GORMTag string JSONTag string CommentTag string IsPrimaryKey bool }
Field user input structures
type Generator ¶
type Generator struct { Cfg Config // contains filtered or unexported fields }
Generator code generator
func (*Generator) GenerateModel ¶
GenerateModel catch table info from db, return a BaseStruct
type Pool ¶
type Pool interface { // Add 添加令牌 Add() // Done 归还令牌 Done() // Num 当前发放的令牌书 Num() int // Size 总令牌数 Size() int // WaitAll 同步等待令牌全部归还 WaitAll() // AsyncWaitAll 异步等待令牌全部归还 AsyncWaitAll() <-chan struct{} }
Pool goroutine pool
type StructMeta ¶
type StructMeta struct { DbConn *gorm.DB FileName string // generated file name InterfaceName string // interface name StructName string // origin/model struct name TableName string // table name in db server PackageName string PrimaryKeyType string // 主键key类型 Fields []*Field }
func (*StructMeta) StructComment ¶
func (b *StructMeta) StructComment() string
StructComment struct comment
Click to show internal directories.
Click to hide internal directories.