model_struct.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. package gorm
  2. import (
  3. "database/sql"
  4. "errors"
  5. "go/ast"
  6. "reflect"
  7. "strings"
  8. "sync"
  9. "time"
  10. "github.com/jinzhu/inflection"
  11. )
  12. // DefaultTableNameHandler default table name handler
  13. var DefaultTableNameHandler = func(db *DB, defaultTableName string) string {
  14. return defaultTableName
  15. }
  16. type safeModelStructsMap struct {
  17. m map[reflect.Type]*ModelStruct
  18. l *sync.RWMutex
  19. }
  20. func (s *safeModelStructsMap) Set(key reflect.Type, value *ModelStruct) {
  21. s.l.Lock()
  22. defer s.l.Unlock()
  23. s.m[key] = value
  24. }
  25. func (s *safeModelStructsMap) Get(key reflect.Type) *ModelStruct {
  26. s.l.RLock()
  27. defer s.l.RUnlock()
  28. return s.m[key]
  29. }
  30. func newModelStructsMap() *safeModelStructsMap {
  31. return &safeModelStructsMap{l: new(sync.RWMutex), m: make(map[reflect.Type]*ModelStruct)}
  32. }
  33. var modelStructsMap = newModelStructsMap()
  34. // ModelStruct model definition
  35. type ModelStruct struct {
  36. PrimaryFields []*StructField
  37. StructFields []*StructField
  38. ModelType reflect.Type
  39. defaultTableName string
  40. }
  41. // TableName get model's table name
  42. func (s *ModelStruct) TableName(db *DB) string {
  43. return DefaultTableNameHandler(db, s.defaultTableName)
  44. }
  45. // StructField model field's struct definition
  46. type StructField struct {
  47. DBName string
  48. Name string
  49. Names []string
  50. IsPrimaryKey bool
  51. IsNormal bool
  52. IsIgnored bool
  53. IsScanner bool
  54. HasDefaultValue bool
  55. Tag reflect.StructTag
  56. TagSettings map[string]string
  57. Struct reflect.StructField
  58. IsForeignKey bool
  59. Relationship *Relationship
  60. }
  61. func (structField *StructField) clone() *StructField {
  62. clone := &StructField{
  63. DBName: structField.DBName,
  64. Name: structField.Name,
  65. Names: structField.Names,
  66. IsPrimaryKey: structField.IsPrimaryKey,
  67. IsNormal: structField.IsNormal,
  68. IsIgnored: structField.IsIgnored,
  69. IsScanner: structField.IsScanner,
  70. HasDefaultValue: structField.HasDefaultValue,
  71. Tag: structField.Tag,
  72. TagSettings: map[string]string{},
  73. Struct: structField.Struct,
  74. IsForeignKey: structField.IsForeignKey,
  75. Relationship: structField.Relationship,
  76. }
  77. for key, value := range structField.TagSettings {
  78. clone.TagSettings[key] = value
  79. }
  80. return clone
  81. }
  82. // Relationship described the relationship between models
  83. type Relationship struct {
  84. Kind string
  85. PolymorphicType string
  86. PolymorphicDBName string
  87. PolymorphicValue string
  88. ForeignFieldNames []string
  89. ForeignDBNames []string
  90. AssociationForeignFieldNames []string
  91. AssociationForeignDBNames []string
  92. JoinTableHandler JoinTableHandlerInterface
  93. }
  94. func getForeignField(column string, fields []*StructField) *StructField {
  95. for _, field := range fields {
  96. if field.Name == column || field.DBName == column || field.DBName == ToDBName(column) {
  97. return field
  98. }
  99. }
  100. return nil
  101. }
  102. // GetModelStruct get value's model struct, relationships based on struct and tag definition
  103. func (scope *Scope) GetModelStruct() *ModelStruct {
  104. var modelStruct ModelStruct
  105. // Scope value can't be nil
  106. if scope.Value == nil {
  107. return &modelStruct
  108. }
  109. reflectType := reflect.ValueOf(scope.Value).Type()
  110. for reflectType.Kind() == reflect.Slice || reflectType.Kind() == reflect.Ptr {
  111. reflectType = reflectType.Elem()
  112. }
  113. // Scope value need to be a struct
  114. if reflectType.Kind() != reflect.Struct {
  115. return &modelStruct
  116. }
  117. // Get Cached model struct
  118. if value := modelStructsMap.Get(reflectType); value != nil {
  119. return value
  120. }
  121. modelStruct.ModelType = reflectType
  122. // Set default table name
  123. if tabler, ok := reflect.New(reflectType).Interface().(tabler); ok {
  124. modelStruct.defaultTableName = tabler.TableName()
  125. } else {
  126. tableName := ToDBName(reflectType.Name())
  127. if scope.db == nil || !scope.db.parent.singularTable {
  128. tableName = inflection.Plural(tableName)
  129. }
  130. modelStruct.defaultTableName = tableName
  131. }
  132. // Get all fields
  133. for i := 0; i < reflectType.NumField(); i++ {
  134. if fieldStruct := reflectType.Field(i); ast.IsExported(fieldStruct.Name) {
  135. field := &StructField{
  136. Struct: fieldStruct,
  137. Name: fieldStruct.Name,
  138. Names: []string{fieldStruct.Name},
  139. Tag: fieldStruct.Tag,
  140. TagSettings: parseTagSetting(fieldStruct.Tag),
  141. }
  142. // is ignored field
  143. if _, ok := field.TagSettings["-"]; ok {
  144. field.IsIgnored = true
  145. } else {
  146. if _, ok := field.TagSettings["PRIMARY_KEY"]; ok {
  147. field.IsPrimaryKey = true
  148. modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field)
  149. }
  150. if _, ok := field.TagSettings["DEFAULT"]; ok {
  151. field.HasDefaultValue = true
  152. }
  153. if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok && !field.IsPrimaryKey {
  154. field.HasDefaultValue = true
  155. }
  156. indirectType := fieldStruct.Type
  157. for indirectType.Kind() == reflect.Ptr {
  158. indirectType = indirectType.Elem()
  159. }
  160. fieldValue := reflect.New(indirectType).Interface()
  161. if _, isScanner := fieldValue.(sql.Scanner); isScanner {
  162. // is scanner
  163. field.IsScanner, field.IsNormal = true, true
  164. if indirectType.Kind() == reflect.Struct {
  165. for i := 0; i < indirectType.NumField(); i++ {
  166. for key, value := range parseTagSetting(indirectType.Field(i).Tag) {
  167. field.TagSettings[key] = value
  168. }
  169. }
  170. }
  171. } else if _, isTime := fieldValue.(*time.Time); isTime {
  172. // is time
  173. field.IsNormal = true
  174. } else if _, ok := field.TagSettings["EMBEDDED"]; ok || fieldStruct.Anonymous {
  175. // is embedded struct
  176. for _, subField := range scope.New(fieldValue).GetModelStruct().StructFields {
  177. subField = subField.clone()
  178. subField.Names = append([]string{fieldStruct.Name}, subField.Names...)
  179. if prefix, ok := field.TagSettings["EMBEDDED_PREFIX"]; ok {
  180. subField.DBName = prefix + subField.DBName
  181. }
  182. if subField.IsPrimaryKey {
  183. if _, ok := subField.TagSettings["PRIMARY_KEY"]; ok {
  184. modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, subField)
  185. } else {
  186. subField.IsPrimaryKey = false
  187. }
  188. }
  189. modelStruct.StructFields = append(modelStruct.StructFields, subField)
  190. }
  191. continue
  192. } else {
  193. // build relationships
  194. switch indirectType.Kind() {
  195. case reflect.Slice:
  196. defer func(field *StructField) {
  197. var (
  198. relationship = &Relationship{}
  199. toScope = scope.New(reflect.New(field.Struct.Type).Interface())
  200. foreignKeys []string
  201. associationForeignKeys []string
  202. elemType = field.Struct.Type
  203. )
  204. if foreignKey := field.TagSettings["FOREIGNKEY"]; foreignKey != "" {
  205. foreignKeys = strings.Split(field.TagSettings["FOREIGNKEY"], ",")
  206. }
  207. if foreignKey := field.TagSettings["ASSOCIATIONFOREIGNKEY"]; foreignKey != "" {
  208. associationForeignKeys = strings.Split(field.TagSettings["ASSOCIATIONFOREIGNKEY"], ",")
  209. }
  210. for elemType.Kind() == reflect.Slice || elemType.Kind() == reflect.Ptr {
  211. elemType = elemType.Elem()
  212. }
  213. if elemType.Kind() == reflect.Struct {
  214. if many2many := field.TagSettings["MANY2MANY"]; many2many != "" {
  215. relationship.Kind = "many_to_many"
  216. // if no foreign keys defined with tag
  217. if len(foreignKeys) == 0 {
  218. for _, field := range modelStruct.PrimaryFields {
  219. foreignKeys = append(foreignKeys, field.DBName)
  220. }
  221. }
  222. for _, foreignKey := range foreignKeys {
  223. if foreignField := getForeignField(foreignKey, modelStruct.StructFields); foreignField != nil {
  224. // source foreign keys (db names)
  225. relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.DBName)
  226. // join table foreign keys for source
  227. joinTableDBName := ToDBName(reflectType.Name()) + "_" + foreignField.DBName
  228. relationship.ForeignDBNames = append(relationship.ForeignDBNames, joinTableDBName)
  229. }
  230. }
  231. // if no association foreign keys defined with tag
  232. if len(associationForeignKeys) == 0 {
  233. for _, field := range toScope.PrimaryFields() {
  234. associationForeignKeys = append(associationForeignKeys, field.DBName)
  235. }
  236. }
  237. for _, name := range associationForeignKeys {
  238. if field, ok := toScope.FieldByName(name); ok {
  239. // association foreign keys (db names)
  240. relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, field.DBName)
  241. // join table foreign keys for association
  242. joinTableDBName := ToDBName(elemType.Name()) + "_" + field.DBName
  243. relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, joinTableDBName)
  244. }
  245. }
  246. joinTableHandler := JoinTableHandler{}
  247. joinTableHandler.Setup(relationship, many2many, reflectType, elemType)
  248. relationship.JoinTableHandler = &joinTableHandler
  249. field.Relationship = relationship
  250. } else {
  251. // User has many comments, associationType is User, comment use UserID as foreign key
  252. var associationType = reflectType.Name()
  253. var toFields = toScope.GetStructFields()
  254. relationship.Kind = "has_many"
  255. if polymorphic := field.TagSettings["POLYMORPHIC"]; polymorphic != "" {
  256. // Dog has many toys, tag polymorphic is Owner, then associationType is Owner
  257. // Toy use OwnerID, OwnerType ('dogs') as foreign key
  258. if polymorphicType := getForeignField(polymorphic+"Type", toFields); polymorphicType != nil {
  259. associationType = polymorphic
  260. relationship.PolymorphicType = polymorphicType.Name
  261. relationship.PolymorphicDBName = polymorphicType.DBName
  262. // if Dog has multiple set of toys set name of the set (instead of default 'dogs')
  263. if value, ok := field.TagSettings["POLYMORPHIC_VALUE"]; ok {
  264. relationship.PolymorphicValue = value
  265. } else {
  266. relationship.PolymorphicValue = scope.TableName()
  267. }
  268. polymorphicType.IsForeignKey = true
  269. }
  270. }
  271. // if no foreign keys defined with tag
  272. if len(foreignKeys) == 0 {
  273. // if no association foreign keys defined with tag
  274. if len(associationForeignKeys) == 0 {
  275. for _, field := range modelStruct.PrimaryFields {
  276. foreignKeys = append(foreignKeys, associationType+field.Name)
  277. associationForeignKeys = append(associationForeignKeys, field.Name)
  278. }
  279. } else {
  280. // generate foreign keys from defined association foreign keys
  281. for _, scopeFieldName := range associationForeignKeys {
  282. if foreignField := getForeignField(scopeFieldName, modelStruct.StructFields); foreignField != nil {
  283. foreignKeys = append(foreignKeys, associationType+foreignField.Name)
  284. associationForeignKeys = append(associationForeignKeys, foreignField.Name)
  285. }
  286. }
  287. }
  288. } else {
  289. // generate association foreign keys from foreign keys
  290. if len(associationForeignKeys) == 0 {
  291. for _, foreignKey := range foreignKeys {
  292. if strings.HasPrefix(foreignKey, associationType) {
  293. associationForeignKey := strings.TrimPrefix(foreignKey, associationType)
  294. if foreignField := getForeignField(associationForeignKey, modelStruct.StructFields); foreignField != nil {
  295. associationForeignKeys = append(associationForeignKeys, associationForeignKey)
  296. }
  297. }
  298. }
  299. if len(associationForeignKeys) == 0 && len(foreignKeys) == 1 {
  300. associationForeignKeys = []string{scope.PrimaryKey()}
  301. }
  302. } else if len(foreignKeys) != len(associationForeignKeys) {
  303. scope.Err(errors.New("invalid foreign keys, should have same length"))
  304. return
  305. }
  306. }
  307. for idx, foreignKey := range foreignKeys {
  308. if foreignField := getForeignField(foreignKey, toFields); foreignField != nil {
  309. if associationField := getForeignField(associationForeignKeys[idx], modelStruct.StructFields); associationField != nil {
  310. // source foreign keys
  311. foreignField.IsForeignKey = true
  312. relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, associationField.Name)
  313. relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, associationField.DBName)
  314. // association foreign keys
  315. relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
  316. relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
  317. }
  318. }
  319. }
  320. if len(relationship.ForeignFieldNames) != 0 {
  321. field.Relationship = relationship
  322. }
  323. }
  324. } else {
  325. field.IsNormal = true
  326. }
  327. }(field)
  328. case reflect.Struct:
  329. defer func(field *StructField) {
  330. var (
  331. // user has one profile, associationType is User, profile use UserID as foreign key
  332. // user belongs to profile, associationType is Profile, user use ProfileID as foreign key
  333. associationType = reflectType.Name()
  334. relationship = &Relationship{}
  335. toScope = scope.New(reflect.New(field.Struct.Type).Interface())
  336. toFields = toScope.GetStructFields()
  337. tagForeignKeys []string
  338. tagAssociationForeignKeys []string
  339. )
  340. if foreignKey := field.TagSettings["FOREIGNKEY"]; foreignKey != "" {
  341. tagForeignKeys = strings.Split(field.TagSettings["FOREIGNKEY"], ",")
  342. }
  343. if foreignKey := field.TagSettings["ASSOCIATIONFOREIGNKEY"]; foreignKey != "" {
  344. tagAssociationForeignKeys = strings.Split(field.TagSettings["ASSOCIATIONFOREIGNKEY"], ",")
  345. }
  346. if polymorphic := field.TagSettings["POLYMORPHIC"]; polymorphic != "" {
  347. // Cat has one toy, tag polymorphic is Owner, then associationType is Owner
  348. // Toy use OwnerID, OwnerType ('cats') as foreign key
  349. if polymorphicType := getForeignField(polymorphic+"Type", toFields); polymorphicType != nil {
  350. associationType = polymorphic
  351. relationship.PolymorphicType = polymorphicType.Name
  352. relationship.PolymorphicDBName = polymorphicType.DBName
  353. // if Cat has several different types of toys set name for each (instead of default 'cats')
  354. if value, ok := field.TagSettings["POLYMORPHIC_VALUE"]; ok {
  355. relationship.PolymorphicValue = value
  356. } else {
  357. relationship.PolymorphicValue = scope.TableName()
  358. }
  359. polymorphicType.IsForeignKey = true
  360. }
  361. }
  362. // Has One
  363. {
  364. var foreignKeys = tagForeignKeys
  365. var associationForeignKeys = tagAssociationForeignKeys
  366. // if no foreign keys defined with tag
  367. if len(foreignKeys) == 0 {
  368. // if no association foreign keys defined with tag
  369. if len(associationForeignKeys) == 0 {
  370. for _, primaryField := range modelStruct.PrimaryFields {
  371. foreignKeys = append(foreignKeys, associationType+primaryField.Name)
  372. associationForeignKeys = append(associationForeignKeys, primaryField.Name)
  373. }
  374. } else {
  375. // generate foreign keys form association foreign keys
  376. for _, associationForeignKey := range tagAssociationForeignKeys {
  377. if foreignField := getForeignField(associationForeignKey, modelStruct.StructFields); foreignField != nil {
  378. foreignKeys = append(foreignKeys, associationType+foreignField.Name)
  379. associationForeignKeys = append(associationForeignKeys, foreignField.Name)
  380. }
  381. }
  382. }
  383. } else {
  384. // generate association foreign keys from foreign keys
  385. if len(associationForeignKeys) == 0 {
  386. for _, foreignKey := range foreignKeys {
  387. if strings.HasPrefix(foreignKey, associationType) {
  388. associationForeignKey := strings.TrimPrefix(foreignKey, associationType)
  389. if foreignField := getForeignField(associationForeignKey, modelStruct.StructFields); foreignField != nil {
  390. associationForeignKeys = append(associationForeignKeys, associationForeignKey)
  391. }
  392. }
  393. }
  394. if len(associationForeignKeys) == 0 && len(foreignKeys) == 1 {
  395. associationForeignKeys = []string{scope.PrimaryKey()}
  396. }
  397. } else if len(foreignKeys) != len(associationForeignKeys) {
  398. scope.Err(errors.New("invalid foreign keys, should have same length"))
  399. return
  400. }
  401. }
  402. for idx, foreignKey := range foreignKeys {
  403. if foreignField := getForeignField(foreignKey, toFields); foreignField != nil {
  404. if scopeField := getForeignField(associationForeignKeys[idx], modelStruct.StructFields); scopeField != nil {
  405. foreignField.IsForeignKey = true
  406. // source foreign keys
  407. relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, scopeField.Name)
  408. relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, scopeField.DBName)
  409. // association foreign keys
  410. relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
  411. relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
  412. }
  413. }
  414. }
  415. }
  416. if len(relationship.ForeignFieldNames) != 0 {
  417. relationship.Kind = "has_one"
  418. field.Relationship = relationship
  419. } else {
  420. var foreignKeys = tagForeignKeys
  421. var associationForeignKeys = tagAssociationForeignKeys
  422. if len(foreignKeys) == 0 {
  423. // generate foreign keys & association foreign keys
  424. if len(associationForeignKeys) == 0 {
  425. for _, primaryField := range toScope.PrimaryFields() {
  426. foreignKeys = append(foreignKeys, field.Name+primaryField.Name)
  427. associationForeignKeys = append(associationForeignKeys, primaryField.Name)
  428. }
  429. } else {
  430. // generate foreign keys with association foreign keys
  431. for _, associationForeignKey := range associationForeignKeys {
  432. if foreignField := getForeignField(associationForeignKey, toFields); foreignField != nil {
  433. foreignKeys = append(foreignKeys, field.Name+foreignField.Name)
  434. associationForeignKeys = append(associationForeignKeys, foreignField.Name)
  435. }
  436. }
  437. }
  438. } else {
  439. // generate foreign keys & association foreign keys
  440. if len(associationForeignKeys) == 0 {
  441. for _, foreignKey := range foreignKeys {
  442. if strings.HasPrefix(foreignKey, field.Name) {
  443. associationForeignKey := strings.TrimPrefix(foreignKey, field.Name)
  444. if foreignField := getForeignField(associationForeignKey, toFields); foreignField != nil {
  445. associationForeignKeys = append(associationForeignKeys, associationForeignKey)
  446. }
  447. }
  448. }
  449. if len(associationForeignKeys) == 0 && len(foreignKeys) == 1 {
  450. associationForeignKeys = []string{toScope.PrimaryKey()}
  451. }
  452. } else if len(foreignKeys) != len(associationForeignKeys) {
  453. scope.Err(errors.New("invalid foreign keys, should have same length"))
  454. return
  455. }
  456. }
  457. for idx, foreignKey := range foreignKeys {
  458. if foreignField := getForeignField(foreignKey, modelStruct.StructFields); foreignField != nil {
  459. if associationField := getForeignField(associationForeignKeys[idx], toFields); associationField != nil {
  460. foreignField.IsForeignKey = true
  461. // association foreign keys
  462. relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, associationField.Name)
  463. relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, associationField.DBName)
  464. // source foreign keys
  465. relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
  466. relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
  467. }
  468. }
  469. }
  470. if len(relationship.ForeignFieldNames) != 0 {
  471. relationship.Kind = "belongs_to"
  472. field.Relationship = relationship
  473. }
  474. }
  475. }(field)
  476. default:
  477. field.IsNormal = true
  478. }
  479. }
  480. }
  481. // Even it is ignored, also possible to decode db value into the field
  482. if value, ok := field.TagSettings["COLUMN"]; ok {
  483. field.DBName = value
  484. } else {
  485. field.DBName = ToDBName(fieldStruct.Name)
  486. }
  487. modelStruct.StructFields = append(modelStruct.StructFields, field)
  488. }
  489. }
  490. if len(modelStruct.PrimaryFields) == 0 {
  491. if field := getForeignField("id", modelStruct.StructFields); field != nil {
  492. field.IsPrimaryKey = true
  493. modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field)
  494. }
  495. }
  496. modelStructsMap.Set(reflectType, &modelStruct)
  497. return &modelStruct
  498. }
  499. // GetStructFields get model's field structs
  500. func (scope *Scope) GetStructFields() (fields []*StructField) {
  501. return scope.GetModelStruct().StructFields
  502. }
  503. func parseTagSetting(tags reflect.StructTag) map[string]string {
  504. setting := map[string]string{}
  505. for _, str := range []string{tags.Get("sql"), tags.Get("gorm")} {
  506. tags := strings.Split(str, ";")
  507. for _, value := range tags {
  508. v := strings.Split(value, ":")
  509. k := strings.TrimSpace(strings.ToUpper(v[0]))
  510. if len(v) >= 2 {
  511. setting[k] = strings.Join(v[1:], ":")
  512. } else {
  513. setting[k] = k
  514. }
  515. }
  516. }
  517. return setting
  518. }