Documentation ¶
Overview ¶
*
- Copyright 2015 @ at3.net.
- name : tool.go
- author : jarryliu
- date : 2016-11-11 12:19
- description :
- history :
*
- Copyright 2015 @ at3.net.
- name : struct
- author : jarryliu
- date : 2016-11-17 13:44
- description :
- history :
*
- Copyright 2015 @ at3.net.
- name : thrift.go
- author : jarryliu
- date : 2016-11-17 13:14
- description :
- history :
Index ¶
- Constants
- Variables
- func SaveFile(s string, path string) error
- func StructAssignCode(v interface{}) ([]byte, error)
- func ThriftStruct(v interface{}) ([]byte, error)
- type CodeTemplate
- type Column
- type Session
- func (s *Session) Func(funcName string, f interface{})
- func (s *Session) GenerateCode(tb *Table, tpl CodeTemplate, structSuffix string, sign bool, ePrefix string) string
- func (s *Session) GenerateTablesCode(tables []*Table, tpl CodeTemplate) string
- func (s *Session) ParseTables(tbs []*orm.Table, err error) ([]*Table, error)
- func (s *Session) ParseTemplate(file string) (CodeTemplate, error)
- func (s *Session) Resolve(t CodeTemplate) CodeTemplate
- func (s *Session) TableToGoIRepo(tb *Table, sign bool, ePrefix string) string
- func (s *Session) TableToGoRepo(tb *Table, sign bool, ePrefix string) string
- func (s *Session) TableToGoStruct(tb *Table) string
- func (s *Session) Var(key string, v interface{})
- type Table
Constants ¶
View Source
const ( //模型包名 VModelPkgName = "ModelPkgName" //仓储结构包名 VRepoPkgName = "RepoPkgName" //仓储接口包名 VIRepoPkgName = "IRepoPkgName" //仓储结构引用模型包路径 VModelPkg = "ModelPkg" //仓储接口引用模型包路径 VIRepoPkg = "IRepoPkg" // 仓储包路径 VRepoPkg = "RepoPkg" )
Variables ¶
View Source
var ( // <R> : 仓储类的名称 // <R2> : 函数后添加的仓储类名称 // <E> : 实体 // <E2> : 包含包名的实体 // <Ptr> : 仓库类对象引用 TPL_ENTITY_REP = CodeTemplate( `// auto generate by gof (http://github.com/jsix/gof) package {{.VAR.RepoPkgName}} import( "log" "{{.VAR.ModelPkg}}" "database/sql" "github.com/jsix/gof/db/orm" ) // Create new <R> func New<R>(o orm.Orm)*<R>{ return &<R>{ _orm:o, } } type <R> struct{ _orm orm.Orm } // Get <E> func (<Ptr> *<R>) Get<R2>(primary interface{})*<E2>{ e := <E2>{} err := <Ptr>._orm.Get(primary,&e) if err == nil{ return &e } if err != sql.ErrNoRows{ log.Println("[ Orm][ Error]:",err.Error(),"; Entity:<E>") } return nil } // GetBy <E> func (<Ptr> *<R>) Get<R2>By(where string,v ...interface{})*<E2>{ e := <E2>{} err := <Ptr>._orm.GetBy(&e,where,v...) if err == nil{ return &e } if err != sql.ErrNoRows{ log.Println("[ Orm][ Error]:",err.Error(),"; Entity:<E>") } return nil } // Select <E> func (<Ptr> *<R>) Select<R2>(where string,v ...interface{})[]*<E2> { list := make([]*<E2>,0) err := <Ptr>._orm.Select(&list,where,v...) if err != nil && err != sql.ErrNoRows{ log.Println("[ Orm][ Error]:",err.Error(),"; Entity:<E>") } return list } // Save <E> func (<Ptr> *<R>) Save<R2>(v *<E2>)(int,error){ id,err := orm.Save(<Ptr>._orm,v,int(v.<PK>)) if err != nil && err != sql.ErrNoRows{ log.Println("[ Orm][ Error]:",err.Error(),"; Entity:<E>") } return id,err } // Delete <E> func (<Ptr> *<R>) Delete<R2>(primary interface{}) error { err := <Ptr>._orm.DeleteByPk(<E2>{}, primary) if err != nil && err != sql.ErrNoRows{ log.Println("[ Orm][ Error]:",err.Error(),"; Entity:<E>") } return err } // Batch Delete <E> func (<Ptr> *<R>) BatchDelete<R2>(where string,v ...interface{})(int64,error) { r,err := <Ptr>._orm.Delete(<E2>{},where,v...) if err != nil && err != sql.ErrNoRows{ log.Println("[ Orm][ Error]:",err.Error(),"; Entity:<E>") } return r,err } `) )
View Source
var ( // 实体仓储接口模板 TPL_ENTITY_REP_INTERFACE = CodeTemplate( `// auto generate by gof (http://github.com/jsix/gof) package {{.VAR.IRepoPkgName}} import( "{{.VAR.IRepoPkg}}" ) type I<R> interface{ // auto generate by gof // Get <E> Get<R2>(primary interface{})*<E2> // GetBy <E> Get<R2>By(where string,v ...interface{})*<E2> // Select <E> Select<R2>(where string,v ...interface{})[]*<E2> // Save <E> Save<R2>(v *<E2>)(int,error) // Delete <E> Delete<R2>(primary interface{}) error // Batch Delete <E> BatchDelete<R2>(where string,v ...interface{})(int64,error) }`) )
View Source
var TPL_REPO_FACTORY = CodeTemplate(`
package repo
{{$var := .VAR}}
import(
"github.com/jsix/gof/db/orm"
"{{$var.ModelPkg}}"
"{{$var.RepoPkg}}"
"{{$var.IRepoPkg}}"
)
type repoFactory struct{
o orm.Orm
{{range $i,$tb := .Tables}}
_{{$tb.Name}}_repo {{$var.IRepoPkgName}}.I{{$tb.Title}}Repo{{end}}
}
func NewRepoFactory(o orm.Orm)*repoFactory{
r := &repoFactory{
o:o,
}
return r.init()
}
func (r *repoFactory) init()*repoFactory{
{{range $i,$tb := .Tables}}
r.o.Mapping({{$var.ModelPkgName}}.{{$tb.Title}}{},"{{$tb.Name}}"){{end}}
return r
}
{{range $i,$tb := .Tables}}
func (r *repoFactory) Get{{$tb.Title}}Repo(){{$var.IRepoPkgName}}.I{{$tb.Title}}Repo{
if r._{{$tb.Name}}_repo == nil{
r._{{$tb.Name}}_repo = {{$var.RepoPkgName}}.New{{$tb.Title}}Repo(r.o)
}
return r._{{$tb.Name}}_repo
}
{{end}}
`)
Functions ¶
Types ¶
type CodeTemplate ¶
type CodeTemplate string
func (CodeTemplate) Replace ¶
func (g CodeTemplate) Replace(s, r string, n int) CodeTemplate
func (CodeTemplate) String ¶
func (g CodeTemplate) String() string
type Column ¶
type Column struct { // 顺序 Ordinal int // 表名 Name string // 表名首字大写 Title string // 是否主键 Pk bool // 是否自动生成 Auto bool // 是否不能为空 NotNull bool // 类型 Type string // 注释 Comment string }
列
type Session ¶
type Session struct { IdUpper bool // contains filtered or unexported fields }
func (*Session) GenerateCode ¶
func (s *Session) GenerateCode(tb *Table, tpl CodeTemplate, structSuffix string, sign bool, ePrefix string) string
生成代码
func (*Session) GenerateTablesCode ¶
func (s *Session) GenerateTablesCode(tables []*Table, tpl CodeTemplate) string
func (*Session) ParseTables ¶
获取所有的表
func (*Session) ParseTemplate ¶
func (s *Session) ParseTemplate(file string) (CodeTemplate, error)
转换成为模板
func (*Session) TableToGoIRepo ¶
表生成仓库仓储接口
func (*Session) TableToGoRepo ¶
表生成仓储结构,sign:函数后是否带签名,ePrefix:实体是否带前缀
Source Files ¶
Click to show internal directories.
Click to hide internal directories.