sql2code

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2022 License: MIT Imports: 4 Imported by: 0

README

sql2code

根据sql生成不同用途代码,支持生成json、gorm model、update参数、request 参数代码,sql可以从参数、文件、db三种方式获取,优先从高到低。


使用示例

主要设置参数

type Args struct {
	SQL string // DDL sql

	DDLFile string // 读取文件的DDL sql

	DBDsn   string // 从db获取表的DDL sql
	DBTable string

	Package        string // 生成字段的包名(只有model类型有效)
	GormType       bool   // gorm type
	JSONTag        bool   // 是否包括json tag
	JSONNamedType  int    // json命名类型,0:和列名一致,其他值表示驼峰
	IsEmbed        bool   // 是否嵌入gorm.Model
	CodeType       string // 指定生成代码用途,支持4中类型,分别是 model(默认), json, dao, handler
}

生成代码示例:

    // 生成gorm model 代码
    code, err := sql2code.GenerateOne(&sql2code.Args{
        SQL: sqlData,  // 来源于sql语句
        // DDLFile: "user.sql", // 来源于sql文件
        // DBDsn: "root:123456@(127.0.0.1:3306)/account"
        // DBTable "user"
        GormType: true,
        JSONTag: true,
        IsEmbed: true,
        CodeType: "model"
    })

      // 生成json、model、dao、handler代码
      codes, err := sql2code.Generate(&sql2code.Args{
          SQL: sqlData,  // 来源于sql语句
          // DDLFile: "user.sql", // 来源于sql文件
          // DBDsn: "root:123456@(127.0.0.1:3306)/account"
          // DBTable "user"
          GormType: true,
          JSONTag: true,
          IsEmbed: true,
          CodeType: "model"
      })

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(args *Args) (map[string]string, error)

Generate 生成model, json, dao, handler不同用途代码

func GenerateOne

func GenerateOne(args *Args) (string, error)

GenerateOne 根据sql生成gorm代码,sql可以从参数、文件、db三种方式获取,优先从高到低

Types

type Args

type Args struct {
	SQL string // DDL sql

	DDLFile string // 读取文件的DDL sql

	DBDsn   string // 从db获取表的DDL sql
	DBTable string

	Package        string // 生成字段的包名(只有model类型有效)
	GormType       bool   // 是否显示gorm type名称(只有model类型代码有效)
	JSONTag        bool   // 是否包括json tag
	JSONNamedType  int    // json命名类型,0:和列名一致,其他值表示驼峰
	IsEmbed        bool   // 是否嵌入gorm.Model
	CodeType       string // 指定生成代码用途,支持4中类型,分别是 model(默认), json, dao, handler
	ForceTableName bool
	Charset        string
	Collation      string
	TablePrefix    string
	ColumnPrefix   string
	NoNullType     bool
	NullStyle      string
}

Args 参数

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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