generator

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2018 License: Apache-2.0 Imports: 13 Imported by: 0

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

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

func SaveFile

func SaveFile(s string, path string) error

保存到文件

func StructAssignCode

func StructAssignCode(v interface{}) ([]byte, error)

生成结构赋值代码

func ThriftStruct

func ThriftStruct(v interface{}) ([]byte, error)

转换结构为Thrift的结构

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 DBCodeGenerator

func DBCodeGenerator() *Session

数据库代码生成器

func (*Session) Func

func (s *Session) Func(funcName string, f interface{})

添加函数

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 (s *Session) ParseTables(tbs []*orm.Table, err error) ([]*Table, error)

获取所有的表

func (*Session) ParseTemplate

func (s *Session) ParseTemplate(file string) (CodeTemplate, error)

转换成为模板

func (*Session) Resolve

func (s *Session) Resolve(t CodeTemplate) CodeTemplate

解析模板

func (*Session) TableToGoIRepo

func (s *Session) TableToGoIRepo(tb *Table,
	sign bool, ePrefix string) string

表生成仓库仓储接口

func (*Session) TableToGoRepo

func (s *Session) TableToGoRepo(tb *Table,
	sign bool, ePrefix string) string

表生成仓储结构,sign:函数后是否带签名,ePrefix:实体是否带前缀

func (*Session) TableToGoStruct

func (s *Session) TableToGoStruct(tb *Table) string

表生成结构

func (*Session) Var

func (s *Session) Var(key string, v interface{})

定义变量或修改变量

type Table

type Table struct {
	// 顺序
	Ordinal int
	// 表名
	Name string
	// 表前缀
	Prefix string
	// 表名单词首字大写
	Title string
	// 表注释
	Comment string
	// 数据库引擎
	Engine string
	// 数据库编码
	Charset string
	// 表
	Raw *orm.Table
	// 列
	Columns []*Column
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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