nolint
(stmt *ast.CreateTableStmt, opt options)
| 435 | |
| 436 | // nolint |
| 437 | func 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) |
no test coverage detected