sql2code

package
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2025 License: MIT Imports: 7 Imported by: 0

README

sql2code

sql2code is a code generation engine that generates CRUD code for model, dao, handler, service, protobuf based on sql and supports database types mysql, mongodb, postgresql, sqlite3.


Example of use

Generate code based on database table.

    import "github.com/go-dev-frame/sponge/pkg/sql2code"

    // generate model, dao, handler, service and protobuf code, supports database type: mysql, mongodb, postgres, sqlite3
    codes, err := sql2code.Generate(&sql2code.Args{
      DBDriver: "mysql",
      DBDsn: "root:123456@(127.0.0.1:3306)/account"
      DBTable "user"
      GormType: true,
      JSONTag: true,
      IsEmbed: true,
      IsExtendedAPI: false
    })

    // write code to file

Generate table information based on database table, used for customized code generation.

    import "github.com/go-dev-frame/sponge/pkg/sql2code"

    // generate table information based on database table, supports database type: mysql, mongodb, postgres, sqlite3
    codes, err := sql2code.Generate(&sql2code.Args{
      DBDriver: "mysql",
      DBDsn: "root:123456@(127.0.0.1:3306)/account"
      DBTable "user"
      GormType: true,
      JSONTag: true,
      IsEmbed: true,
      IsExtendedAPI: true
    })

    // generate customized code to file

Documentation

Overview

Package sql2code is a code generation engine that generates CRUD code for model, dao, handler, service, protobuf based on sql and supports database types mysql, mongodb, postgresql, sqlite3.

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

	DBDriver string // db driver name, such as mysql, mongodb, postgresql, sqlite, default is mysql
	DBDsn    string // connecting to mysql's dsn, if DBDriver is sqlite, DBDsn is local db file
	DBTable  string // table name

	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
	IsExtendedAPI  bool // true: generate extended api (9 api), false: generate basic api (5 api)

	IsCustomTemplate bool // whether to use custom template, default is false
	// contains filtered or unexported fields
}

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