sql2code

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2023 License: MIT Imports: 4 Imported by: 0

README

sql2code

Generate code for different purposes according to sql, support generating json, gorm model, update parameter, request parameter code, sql can be obtained from parameter, file, db three ways, priority from high to low.


Example of use

Main setting parameters.

type Args struct {
	SQL string // DDL sql

	DDLFile string // DDL file

	DBDsn   string // connecting to mysql's dsn
	DBTable string // table name

	Package        string // specify the package name (only valid for model types)
	GormType       bool   // gorm type
	JSONTag        bool   // does it include a json tag
	JSONNamedType  int    // json naming type, 0: consistent with the column name, other values indicate a hump
	IsEmbed        bool   // is gorm.Model embedded
	CodeType       string // specify the different types of code to be generated, namely model (default), json, dao, handler, proto
}

Generated code example.

    import "gitee.com/jianguosun_admin/common_pkg/sql2code"

    // generate gorm model code
    code, err := sql2code.GenerateOne(&sql2code.Args{
        SQL: sqlData,  // source from sql text
        // DDLFile: "user.sql", // source from sql file
        // DBDsn: "root:123456@(127.0.0.1:3306)/account"
        // DBTable "user"
        GormType: true,
        JSONTag: true,
        IsEmbed: true,
        CodeType: "model"
    })

      // generate json, model, dao, handler code
      codes, err := sql2code.Generate(&sql2code.Args{
          SQL: sqlData,  // source from sql text
          // DDLFile: "user.sql", // source from sql file
          // DBDsn: "root:123456@(127.0.0.1:3306)/account"
          // DBTable "user"
          GormType: true,
          JSONTag: true,
          IsEmbed: true,
          CodeType: "dao"
      })

Documentation

Overview

Package sql2code provides for generating code for different purposes according to sql, support generating json, gorm model, update parameter, request parameter code, sql can be obtained from parameter, file, db three ways, priority from high to low.

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, proto codes

func GenerateOne

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

GenerateOne generate gorm code from sql, which can be obtained from parameters, files and db, with priority from highest to lowest

Types

type Args

type Args struct {
	SQL string // DDL sql

	DDLFile string // DDL file

	DBDsn   string // connecting to mysql's dsn
	DBTable string

	Package        string // specify the package name (only valid for model types)
	GormType       bool   // whether to display the gorm type name (only valid for model type codes)
	JSONTag        bool   // does it include a json tag
	JSONNamedType  int    // json field naming type, 0: snake case such as my_field_name, 1: camel sase, such as myFieldName
	IsEmbed        bool   // is gorm.Model embedded
	IsWebProto     bool   // proto file type, true: include router path and swagger info, false: normal proto file without router and swagger
	CodeType       string // specify the different types of code to be generated, namely model (default), json, dao, handler, proto
	ForceTableName bool
	Charset        string
	Collation      string
	TablePrefix    string
	ColumnPrefix   string
	NoNullType     bool
	NullStyle      string
}

Args generate code arguments

Directories

Path Synopsis
Package parser is a library that parses to go structures based on sql and generates the code needed based on the template.
Package parser is a library that parses to go structures based on sql and generates the code needed based on the template.

Jump to

Keyboard shortcuts

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