Documentation ¶
Index ¶
Constants ¶
View Source
const ( // TableQueryStruct table query struct TableQueryStruct = createMethod + ` type {{.QueryStructName}} struct { {{.QueryStructName}}Do ` + fields + ` } ` + tableMethod + asMethond + updateFieldMethod + getFieldMethod + fillFieldMapMethod + cloneMethod + replaceMethod + relationship + defineMethodStruct // TableQueryStructWithContext table query struct with context TableQueryStructWithContext = createMethod + ` type {{.QueryStructName}} struct { {{.QueryStructName}}Do {{.QueryStructName}}Do ` + fields + ` } ` + tableMethod + asMethond + updateFieldMethod + ` func ({{.S}} *{{.QueryStructName}}) WithContext(ctx context.Context) {{.ReturnObject}} { return {{.S}}.{{.QueryStructName}}Do.WithContext(ctx)} func ({{.S}} {{.QueryStructName}}) TableName() string { return {{.S}}.{{.QueryStructName}}Do.TableName() } func ({{.S}} {{.QueryStructName}}) Alias() string { return {{.S}}.{{.QueryStructName}}Do.Alias() } ` + getFieldMethod + fillFieldMapMethod + cloneMethod + replaceMethod + relationship + defineMethodStruct // TableQueryIface table query interface TableQueryIface = defineDoInterface )
View Source
const CRUDMethod = `` /* 8031-byte string literal not displayed */
CRUDMethod CRUD method
View Source
const CRUDMethodTest = `` /* 3974-byte string literal not displayed */
CRUDMethodTest CRUD method test
View Source
const DIYMethod = `` /* 863-byte string literal not displayed */
DIYMethod DIY method
View Source
const DIYMethodTest = `` /* 496-byte string literal not displayed */
DIYMethodTest DIY method test
View Source
const DIYMethodTestBasic = `` /* 138-byte string literal not displayed */
DIYMethodTestBasic DIY method test basic
View Source
const DefaultQuery = `` /* 286-byte string literal not displayed */
DefaultQuery default query
View Source
const Header = NotEditMark + `
package {{.Package}}
import(
{{range .ImportPkgPaths}}{{.}}` + "\n" + `{{end}}
)
`
View Source
const Model = NotEditMark + `
package {{.StructInfo.Package}}
import (
"encoding/json"
"time"
"gorm.io/datatypes"
"gorm.io/gorm"
"gorm.io/gorm/schema"
{{range .ImportPkgPaths}}{{.}} ` + "\n" + `{{end}}
)
{{if .TableName -}}const TableName{{.ModelStructName}} = "{{.TableName}}"{{- end}}
// {{.ModelStructName}} {{.StructComment}}
type {{.ModelStructName}} struct {
{{range .Fields}}
{{if .MultilineComment -}}
/*
{{.ColumnComment}}
*/
{{end -}}
{{.Name}} {{.Type}} ` + "`{{.Tags}}` " +
"{{if not .MultilineComment}}{{if .ColumnComment}}// {{.ColumnComment}}{{end}}{{end}}" +
`{{end}}
}
`
Model used as a variable because it cannot load template file after packed, params still can pass file
View Source
const ModelMethod = `` /* 145-byte string literal not displayed */
ModelMethod model struct DIY method
View Source
const NotEditMark = `` /* 205-byte string literal not displayed */
View Source
const QueryMethod = `` /* 1840-byte string literal not displayed */
QueryMethod query method template
View Source
const QueryMethodTest = `
const dbName = "gen_test.db"
var db *gorm.DB
var once sync.Once
func init() {
InitializeDB()
db.AutoMigrate(&_another{})
}
func InitializeDB() {
once.Do(func() {
var err error
db, err = gorm.Open(sqlite.Open(dbName), &gorm.Config{})
if err != nil {
panic(fmt.Errorf("open sqlite %q fail: %w", dbName, err))
}
})
}
func assert(t *testing.T, methodName string, res, exp interface{}) {
if !reflect.DeepEqual(res, exp) {
t.Errorf("%v() gotResult = %v, want %v", methodName, res, exp)
}
}
type _another struct {
ID uint64 ` + "`" + `gorm:"primaryKey"` + "`" + `
}
func (*_another) TableName() string { return "another_for_unit_test" }
func Test_Available(t *testing.T) {
if !Use(db).Available() {
t.Errorf("query.Available() == false")
}
}
func Test_WithContext(t *testing.T) {
query := Use(db)
if !query.Available() {
t.Errorf("query Use(db) fail: query.Available() == false")
}
type Content string
var key, value Content = "gen_tag", "unit_test"
qCtx := query.WithContext(context.WithValue(context.Background(), key, value))
for _, ctx := range []context.Context{
{{range $name,$d :=.Data -}}
qCtx.{{$d.ModelStructName}}.UnderlyingDB().Statement.Context,
{{end}}
} {
if v := ctx.Value(key); v != value {
t.Errorf("get value from context fail, expect %q, got %q", value, v)
}
}
}
func Test_Transaction(t *testing.T) {
query := Use(db)
if !query.Available() {
t.Errorf("query Use(db) fail: query.Available() == false")
}
err := query.Transaction(func(tx *Query) error { return nil })
if err != nil {
t.Errorf("query.Transaction execute fail: %s", err)
}
tx := query.Begin()
err = tx.SavePoint("point")
if err != nil {
t.Errorf("query tx SavePoint fail: %s", err)
}
err = tx.RollbackTo("point")
if err != nil {
t.Errorf("query tx RollbackTo fail: %s", err)
}
err = tx.Commit()
if err != nil {
t.Errorf("query tx Commit fail: %s", err)
}
err = query.Begin().Rollback()
if err != nil {
t.Errorf("query tx Rollback fail: %s", err)
}
}
`
QueryMethodTest query method test template
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.