-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathsql2code.go
More file actions
231 lines (206 loc) · 6.89 KB
/
sql2code.go
File metadata and controls
231 lines (206 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// 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.
package sql2code
import (
"errors"
"fmt"
"os"
"strings"
"github.com/go-dev-frame/sponge/pkg/gofile"
"github.com/go-dev-frame/sponge/pkg/sql2code/parser"
"github.com/go-dev-frame/sponge/pkg/utils"
)
// Args generate code arguments
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
fieldTypes map[string]string // field name:type
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
}
func (a *Args) checkValid() error {
if a.SQL == "" && a.DDLFile == "" && (a.DBDsn == "" && a.DBTable == "") {
return errors.New("you must specify sql or ddl file")
}
if a.DBTable != "" {
tables := strings.Split(a.DBTable, ",")
for _, name := range tables {
if strings.HasSuffix(name, "_test") {
return fmt.Errorf(`the table name (%s) suffix "_test" is not supported for code generation, please delete suffix "_test" or change it to another name. `, name)
}
}
}
if a.DBDriver == "" {
a.DBDriver = parser.DBDriverMysql
} else if a.DBDriver == parser.DBDriverSqlite {
if !gofile.IsExists(a.DBDsn) {
return fmt.Errorf("sqlite db file %s not found in local host", a.DBDsn)
}
}
if a.fieldTypes == nil {
a.fieldTypes = make(map[string]string)
}
return nil
}
func getSQL(args *Args) (string, map[string]string, error) {
if args.SQL != "" {
return args.SQL, nil, nil
}
sql := ""
dbDriverName := strings.ToLower(args.DBDriver)
if args.DDLFile != "" {
if dbDriverName != parser.DBDriverMysql {
return sql, nil, fmt.Errorf("not support driver %s for parsing the sql file, only mysql is supported", args.DBDriver)
}
b, err := os.ReadFile(args.DDLFile)
if err != nil {
return sql, nil, fmt.Errorf("read %s failed, %s", args.DDLFile, err)
}
return string(b), nil, nil
} else if args.DBDsn != "" {
if args.DBTable == "" {
return sql, nil, errors.New("miss database table")
}
switch dbDriverName {
case parser.DBDriverMysql, parser.DBDriverTidb:
dsn := utils.AdaptiveMysqlDsn(args.DBDsn)
sqlStr, err := parser.GetMysqlTableInfo(dsn, args.DBTable)
return sqlStr, nil, err
case parser.DBDriverPostgresql:
dsn := utils.AdaptivePostgresqlDsn(args.DBDsn)
fields, err := parser.GetPostgresqlTableInfo(dsn, args.DBTable)
if err != nil {
return "", nil, err
}
sqlStr, pgTypeMap := parser.ConvertToSQLByPgFields(args.DBTable, fields)
return sqlStr, pgTypeMap, nil
case parser.DBDriverSqlite:
sqlStr, err := parser.GetSqliteTableInfo(args.DBDsn, args.DBTable)
return sqlStr, nil, err
case parser.DBDriverMongodb:
dsn := utils.AdaptiveMongodbDsn(args.DBDsn)
fields, err := parser.GetMongodbTableInfo(dsn, args.DBTable)
if err != nil {
return "", nil, err
}
sqlStr, mongoTypeMap := parser.ConvertToSQLByMgoFields(args.DBTable, fields)
return sqlStr, mongoTypeMap, nil
default:
return "", nil, errors.New("get sql error, unsupported database driver: " + dbDriverName)
}
}
return sql, nil, errors.New("no SQL input(-sql|-f|-db-dsn)")
}
func setOptions(args *Args) []parser.Option {
var opts []parser.Option
if args.DBDriver != "" {
opts = append(opts, parser.WithDBDriver(args.DBDriver))
}
if args.fieldTypes != nil {
opts = append(opts, parser.WithFieldTypes(args.fieldTypes))
}
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.IsWebProto {
opts = append(opts, parser.WithWebProto())
}
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())
}
if args.IsExtendedAPI {
opts = append(opts, parser.WithExtendedAPI())
}
if args.IsCustomTemplate {
opts = append(opts, parser.WithCustomTemplate())
}
return opts
}
// GenerateOne generate gorm code from sql, which can be obtained from parameters, files and db, with priority from highest to lowest
func GenerateOne(args *Args) (string, error) {
codes, err := Generate(args)
if err != nil {
return "", err
}
if args.CodeType == "" {
args.CodeType = parser.CodeTypeModel // default is 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, proto codes
func Generate(args *Args) (map[string]string, error) {
if err := args.checkValid(); err != nil {
return nil, err
}
sql, fieldTypes, err := getSQL(args)
if err != nil {
return nil, err
}
if fieldTypes != nil {
args.fieldTypes = fieldTypes
}
if sql == "" {
return nil, fmt.Errorf("get sql from %s error, maybe the table %s doesn't exist", args.DBDriver, args.DBTable)
}
opt := setOptions(args)
return parser.ParseSQL(sql, opt...)
}