MCPcopy Create free account
hub / github.com/go-dev-frame/sponge / ConvertToSQLByMgoFields

Function ConvertToSQLByMgoFields

pkg/sql2code/parser/mongodb.go:238–276  ·  view source on GitHub ↗

ConvertToSQLByMgoFields convert to mysql table ddl

(tableName string, fields []*MgoField)

Source from the content-addressed store, hash-verified

236
237// ConvertToSQLByMgoFields convert to mysql table ddl
238func ConvertToSQLByMgoFields(tableName string, fields []*MgoField) (string, map[string]string) {
239 isHaveID := false
240 fieldStr := ""
241 srcMongoTypeMap := make(map[string]string) // name:type
242 objectStrs := []string{}
243 protoObjectStrs := []string{}
244
245 for _, field := range fields {
246 switch field.Type {
247 case goTypeInterface, goTypeSliceInterface:
248 srcMongoTypeMap[field.Name] = xstrings.ToCamelCase(field.Name)
249 default:
250 srcMongoTypeMap[field.Name] = field.Type
251 }
252 if field.Name == oidName {
253 isHaveID = true
254 srcMongoTypeMap["id"] = field.Type
255 continue
256 }
257
258 fieldStr += fmt.Sprintf(" `%s` %s,\n", field.Name, convertMongoToMysqlType(field.Type))
259 if field.ObjectStr != "" {
260 objectStrs = append(objectStrs, field.ObjectStr)
261 protoObjectStrs = append(protoObjectStrs, field.ProtoObjectStr)
262 }
263 }
264
265 fieldStr = strings.TrimSuffix(fieldStr, ",\n")
266 if isHaveID {
267 fieldStr = " `id` varchar(24),\n" + fieldStr + ",\n PRIMARY KEY (id)"
268 }
269
270 if len(objectStrs) > 0 {
271 srcMongoTypeMap[SubStructKey] = strings.Join(objectStrs, "\n") + "\n"
272 srcMongoTypeMap[ProtoSubStructKey] = strings.Join(protoObjectStrs, "\n") + "\n"
273 }
274
275 return fmt.Sprintf("CREATE TABLE `%s` (\n%s\n);", tableName, fieldStr), srcMongoTypeMap
276}
277
278// nolint
279func convertMongoToMysqlType(goType string) string {

Callers 3

getSQLFunction · 0.92
TestGetMongodbTableInfoFunction · 0.85

Calls 1

convertMongoToMysqlTypeFunction · 0.85

Tested by 2

TestGetMongodbTableInfoFunction · 0.68