| 72 | } |
| 73 | |
| 74 | func newCrudInfo(data tmplData) *CrudInfo { |
| 75 | if len(data.Fields) == 0 { |
| 76 | return nil |
| 77 | } |
| 78 | |
| 79 | var info *CrudInfo |
| 80 | for _, field := range data.Fields { |
| 81 | if field.IsPrimaryKey { |
| 82 | info = setCrudInfo(field) |
| 83 | break |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // if not found primary key, find the first xxx_id column as primary key |
| 88 | if info == nil { |
| 89 | for _, field := range data.Fields { |
| 90 | if strings.HasSuffix(field.ColName, "_id") && isDesiredGoType(field.GoType) { // xxx_id |
| 91 | info = setCrudInfo(field) |
| 92 | break |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // if not found xxx_id field, use the first field of integer or string type |
| 98 | if info == nil { |
| 99 | for _, field := range data.Fields { |
| 100 | if isDesiredGoType(field.GoType) { |
| 101 | info = setCrudInfo(field) |
| 102 | break |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // use the first column as primary key |
| 108 | if info == nil { |
| 109 | info = setCrudInfo(data.Fields[0]) |
| 110 | } |
| 111 | |
| 112 | info.TableNameCamel = data.TableName |
| 113 | info.TableNameCamelFCL = data.TName |
| 114 | |
| 115 | pluralName := inflection.Plural(data.TableName) |
| 116 | info.TableNamePluralCamel = customEndOfLetterToLower(data.TableName, pluralName) |
| 117 | info.TableNamePluralCamelFCL = customFirstLetterToLower(customEndOfLetterToLower(data.TableName, pluralName)) |
| 118 | |
| 119 | return info |
| 120 | } |
| 121 | |
| 122 | func (info *CrudInfo) getCode() string { |
| 123 | if info == nil { |