Documentation ¶
Index ¶
Constants ¶
View Source
const ( BaseStruct = createMethod + ` type {{.NewStructName}} struct { {{.NewStructName}}Do ` + members + ` } ` + asMethond + getFieldMethod + fillFieldMapMethod + cloneMethod + relationship + defineMethodStruct BaseStructWithContext = createMethod + ` type {{.NewStructName}} struct { {{.NewStructName}}Do {{.NewStructName}}Do ` + members + ` } ` + asMethond + ` func ({{.S}} *{{.NewStructName}}) WithContext(ctx context.Context) *{{.NewStructName}}Do { return {{.S}}.{{.NewStructName}}Do.WithContext(ctx)} func ({{.S}} {{.NewStructName}}) TableName() string { return {{.S}}.{{.NewStructName}}Do.TableName()} ` + getFieldMethod + fillFieldMapMethod + cloneMethod + relationship + defineMethodStruct )
View Source
const CRUDMethod = `` /* 6386-byte string literal not displayed */
View Source
const CRUDMethod_TEST = `` /* 3653-byte string literal not displayed */
View Source
const DIYMethod = `` /* 889-byte string literal not displayed */
View Source
const DefaultQuery = `` /* 239-byte string literal not displayed */
View Source
const Header = NotEditMark + `
package {{.}}
import(
"database/sql"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"gorm.io/gorm/clause"
"github.com/polpo-studio/gen"
"github.com/polpo-studio/gen/field"
"github.com/polpo-studio/gen/helper"
)
`
View Source
const Model = NotEditMark + `
package {{.StructInfo.Package}}
import "time"
const TableName{{.StructName}} = "{{.TableName}}"
// {{.StructName}} mapped from table <{{.TableName}}>
type {{.StructName}} struct {
{{range .Members}}
{{if .MultilineComment -}}
/*
{{.ColumnComment}}
*/
{{end -}}
{{.Name}} {{.Type}} ` + "`{{if .OverwriteTag}}{{.OverwriteTag}}{{else}}gorm:\"{{.GORMTag}}\" json:\"{{.JSONTag}}\"{{.NewTag}}{{end}}` " +
"{{if not .MultilineComment}}{{if .ColumnComment}}// {{.ColumnComment}}{{end}}{{end}}" +
`{{end}}
}
// TableName {{.StructName}}'s table name
func (*{{.StructName}}) TableName() string {
return TableName{{.StructName}}
}
`
Model used as a variable because it cannot load template file after packed, params still can pass file
View Source
const NotEditMark = `` /* 190-byte string literal not displayed */
View Source
const QueryMethod = `` /* 1415-byte string literal not displayed */
View Source
const QueryMethod_TEST = `
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))
}
})
}
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.StructName}}.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)
}
}
`
View Source
const UnitTestHeader = NotEditMark + `
package {{.}}
import(
"context"
"fmt"
"testing"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
`
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.