sql2code

package
v1.16.2 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 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/Eric-Guo/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/Eric-Guo/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

Deletion behavior

sponge web http defaults to --soft-delete=true. With --embed=true, it uses sgorm.Model (or Model2 for snake case JSON tags), which requires deleted_at and keeps deleted records in the database.

Use --embed=true --soft-delete=false for tables without that column. The generated model uses sgorm.BaseModel (or BaseModel2), retains ID, CreatedAt, and UpdatedAt, and excludes deleted_at from the query column whitelist. Single, batch, and transactional deletes permanently remove records. This setting applies to every table in --db-table. No database migration is run.

Go callers can set sql2code.Args.DisableSoftDelete: true, or pass parser.WithSoftDelete(false) to the parser. The zero-value arguments retain the existing behavior.

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
	DisableSoftDelete bool   // omit deleted_at from SQL models; false preserves the existing soft-delete default
	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