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

Function makeCode

pkg/sql2code/parser/parser.go:437–678  ·  view source on GitHub ↗

nolint

(stmt *ast.CreateTableStmt, opt options)

Source from the content-addressed store, hash-verified

435
436// nolint
437func makeCode(stmt *ast.CreateTableStmt, opt options) (*codeText, error) {
438 importPath := make([]string, 0, 1)
439 data := tmplData{
440 TableNamePrefix: opt.TablePrefix,
441 RawTableName: stmt.Table.Name.String(),
442 DBDriver: opt.DBDriver,
443 }
444
445 tablePrefix := data.TableNamePrefix
446 if tablePrefix != "" && strings.HasPrefix(data.RawTableName, tablePrefix) {
447 data.NameFunc = true
448 data.TableName = toCamel(data.RawTableName[len(tablePrefix):])
449 } else {
450 data.TableName = toCamel(data.RawTableName)
451 }
452 data.TName = firstLetterToLower(data.TableName)
453
454 if opt.ForceTableName || data.RawTableName != inflection.Plural(data.RawTableName) {
455 data.NameFunc = true
456 }
457
458 switch opt.DBDriver {
459 case DBDriverMongodb:
460 if opt.JSONNamedType != 0 {
461 SetJSONTagCamelCase()
462 } else {
463 SetJSONTagSnakeCase()
464 }
465 }
466
467 // find table comment
468 for _, o := range stmt.Options {
469 if o.Tp == ast.TableOptionComment {
470 data.Comment = replaceCommentNewline(o.StrValue)
471 break
472 }
473 }
474
475 isPrimaryKey := make(map[string]bool)
476 for _, con := range stmt.Constraints {
477 if con.Tp == ast.ConstraintPrimaryKey {
478 isPrimaryKey[con.Keys[0].Column.String()] = true
479 }
480 if con.Tp == ast.ConstraintForeignKey {
481 // TODO: foreign key support
482 }
483 }
484
485 columnPrefix := opt.ColumnPrefix
486 for _, col := range stmt.Cols {
487 colName := col.Name.Name.String()
488 goFieldName := colName
489 if columnPrefix != "" && strings.HasPrefix(goFieldName, columnPrefix) {
490 goFieldName = goFieldName[len(columnPrefix):]
491 }
492 jsonName := colName
493 if opt.JSONNamedType == 0 { // snake case
494 jsonName = customToSnake(jsonName)

Callers 1

ParseSQLFunction · 0.85

Calls 15

WriteStringMethod · 0.95
isCommonStyleMethod · 0.95
toCamelFunction · 0.85
SetJSONTagCamelCaseFunction · 0.85
SetJSONTagSnakeCaseFunction · 0.85
replaceCommentNewlineFunction · 0.85
customToSnakeFunction · 0.85
customToCamelFunction · 0.85
getDefaultValueFunction · 0.85
makeTagStrFunction · 0.85
mysqlToGoTypeFunction · 0.85
newCrudInfoFunction · 0.85

Tested by

no test coverage detected