MCPcopy Index your code
hub / github.com/go-dev-frame/sponge / ParseSQL

Function ParseSQL

pkg/sql2code/parser/parser.go:80–149  ·  view source on GitHub ↗

ParseSQL generate different usage codes based on sql

(sql string, options ...Option)

Source from the content-addressed store, hash-verified

78
79// ParseSQL generate different usage codes based on sql
80func ParseSQL(sql string, options ...Option) (map[string]string, error) {
81 initTemplate()
82 initCommonTemplate()
83 opt := parseOption(options)
84
85 stmts, err := parser.New().Parse(sql, opt.Charset, opt.Collation)
86 if err != nil {
87 return nil, err
88 }
89 modelStructCodes := make([]string, 0, len(stmts))
90 updateFieldsCodes := make([]string, 0, len(stmts))
91 handlerStructCodes := make([]string, 0, len(stmts))
92 protoFileCodes := make([]string, 0, len(stmts))
93 serviceStructCodes := make([]string, 0, len(stmts))
94 modelJSONCodes := make([]string, 0, len(stmts))
95 importPath := make(map[string]struct{})
96 tableNames := make([]string, 0, len(stmts))
97 primaryKeysCodes := make([]string, 0, len(stmts))
98 tableInfoCodes := make([]string, 0, len(stmts))
99 for _, stmt := range stmts {
100 if ct, ok := stmt.(*ast.CreateTableStmt); ok {
101 code, err2 := makeCode(ct, opt)
102 if err2 != nil {
103 return nil, err2
104 }
105 modelStructCodes = append(modelStructCodes, code.modelStruct)
106 updateFieldsCodes = append(updateFieldsCodes, code.updateFields)
107 handlerStructCodes = append(handlerStructCodes, code.handlerStruct)
108 protoFileCodes = append(protoFileCodes, code.protoFile)
109 serviceStructCodes = append(serviceStructCodes, code.serviceStruct)
110 modelJSONCodes = append(modelJSONCodes, code.modelJSON)
111 tableNames = append(tableNames, toCamel(ct.Table.Name.String()))
112 primaryKeysCodes = append(primaryKeysCodes, code.crudInfo)
113 tableInfoCodes = append(tableInfoCodes, string(code.tableInfo))
114 for _, s := range code.importPaths {
115 importPath[s] = struct{}{}
116 }
117 }
118 }
119
120 importPathArr := make([]string, 0, len(importPath))
121 for s := range importPath {
122 importPathArr = append(importPathArr, s)
123 }
124 sort.Strings(importPathArr)
125
126 mc := modelCodes{
127 Package: opt.Package,
128 ImportPath: importPathArr,
129 StructCode: modelStructCodes,
130 }
131 modelCode, err := getModelCode(mc)
132 if err != nil {
133 return nil, err
134 }
135
136 var codesMap = map[string]string{
137 CodeTypeModel: modelCode,

Callers 6

GenerateFunction · 0.92
TestParseMysqlSQLFunction · 0.85
TestParseSQLFunction · 0.85
TestParseSQLsFunction · 0.85

Calls 7

initTemplateFunction · 0.85
initCommonTemplateFunction · 0.85
parseOptionFunction · 0.85
makeCodeFunction · 0.85
toCamelFunction · 0.85
getModelCodeFunction · 0.70
StringMethod · 0.65

Tested by 5

TestParseMysqlSQLFunction · 0.68
TestParseSQLFunction · 0.68
TestParseSQLsFunction · 0.68