Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
Tags (8)
master
v1.4.1
v1.3.5
v1.3.2
v1.3.1
v1.3.0
v1.2.0
v1.1.1
v1.1.0
master
Branches (1)
Tags (8)
master
v1.4.1
v1.3.5
v1.3.2
v1.3.1
v1.3.0
v1.2.0
v1.1.1
v1.1.0
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
Tags (8)
master
v1.4.1
v1.3.5
v1.3.2
v1.3.1
v1.3.0
v1.2.0
v1.1.1
v1.1.0
pkg
/
sql2code
/
sql2code.go
pkg
/
sql2code
/
sql2code.go
sql2code.go 3.60 KB
Copy Edit Raw Blame History
zhufuyi authored 2022年11月15日 22:50 +08:00 . adjustment code
package sql2code
import (
"errors"
"fmt"
"os"
"github.com/zhufuyi/pkg/sql2code/parser"
)
// 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
}
func (a *Args) checkValid() error {
if a.SQL == "" && a.DDLFile == "" && (a.DBDsn == "" && a.DBTable == "") {
return errors.New("you must specify sql or ddl file")
}
return nil
}
func getSQL(args *Args) (string, error) {
if args.SQL != "" {
return args.SQL, nil
}
sql := ""
if args.DDLFile != "" {
b, err := os.ReadFile(args.DDLFile)
if err != nil {
return sql, fmt.Errorf("read %s failed, %s", args.DDLFile, err)
}
return string(b), nil
} else if args.DBDsn != "" {
if args.DBTable == "" {
return sql, errors.New("miss mysql table")
}
sqlStr, err := parser.GetTableInfo(args.DBDsn, args.DBTable)
if err != nil {
return sql, err
}
return sqlStr, nil
}
return sql, errors.New("no SQL input(-sql|-f|-db-dsn)")
}
func getOptions(args *Args) []parser.Option {
var opts []parser.Option
if args.Charset != "" {
opts = append(opts, parser.WithCharset(args.Charset))
}
if args.Collation != "" {
opts = append(opts, parser.WithCollation(args.Collation))
}
if args.JSONTag {
opts = append(opts, parser.WithJSONTag(args.JSONNamedType))
}
if args.TablePrefix != "" {
opts = append(opts, parser.WithTablePrefix(args.TablePrefix))
}
if args.ColumnPrefix != "" {
opts = append(opts, parser.WithColumnPrefix(args.ColumnPrefix))
}
if args.NoNullType {
opts = append(opts, parser.WithNoNullType())
}
if args.IsEmbed {
opts = append(opts, parser.WithEmbed())
}
if args.NullStyle != "" {
switch args.NullStyle {
case "sql":
opts = append(opts, parser.WithNullStyle(parser.NullInSql))
case "ptr":
opts = append(opts, parser.WithNullStyle(parser.NullInPointer))
default:
fmt.Printf("invalid null style: %s\n", args.NullStyle)
return nil
}
} else {
opts = append(opts, parser.WithNullStyle(parser.NullDisable))
}
if args.Package != "" {
opts = append(opts, parser.WithPackage(args.Package))
}
if args.GormType {
opts = append(opts, parser.WithGormType())
}
if args.ForceTableName {
opts = append(opts, parser.WithForceTableName())
}
return opts
}
// GenerateOne 根据sql生成gorm代码,sql可以从参数、文件、db三种方式获取,优先从高到低
func GenerateOne(args *Args) (string, error) {
codes, err := Generate(args)
if err != nil {
return "", err
}
if args.CodeType == "" {
args.CodeType = parser.CodeTypeModel // 默认为model code
}
out, ok := codes[args.CodeType]
if !ok {
return "", fmt.Errorf("unknown code type %s", args.CodeType)
}
return out, nil
}
// Generate 生成model, json, dao, handler不同用途代码
func Generate(args *Args) (map[string]string, error) {
if err := args.checkValid(); err != nil {
return nil, err
}
sql, err := getSQL(args)
if err != nil {
return nil, err
}
opt := getOptions(args)
return parser.ParseSQL(sql, opt...)
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

Cancel

Releases

No release

Contributors

All

Language(Optional)

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/633game/pkg.git
git@gitee.com:633game/pkg.git
633game
pkg
pkg
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /