model_base.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package models
  2. import "fmt"
  3. // Action CRUD actions
  4. type Action int32
  5. var (
  6. // Create action when record is created
  7. Create = Action(0)
  8. // RetrieveOne action when a record is retrieved from db
  9. RetrieveOne = Action(1)
  10. // RetrieveMany action when record(s) are retrieved from db
  11. RetrieveMany = Action(2)
  12. // Update action when record is updated in db
  13. Update = Action(3)
  14. // Delete action when record is deleted in db
  15. Delete = Action(4)
  16. // FetchDDL action when fetching ddl info from db
  17. FetchDDL = Action(5)
  18. tables map[string]*TableInfo
  19. )
  20. func init() {
  21. tables = make(map[string]*TableInfo)
  22. tables["admin"] = adminTableInfo
  23. tables["admin_log"] = admin_logTableInfo
  24. tables["admin_permission"] = admin_permissionTableInfo
  25. tables["artist_profile"] = artist_profileTableInfo
  26. tables["bid_log"] = bid_logTableInfo
  27. tables["collection"] = collectionTableInfo
  28. tables["collection_profile"] = collection_profileTableInfo
  29. tables["currency_price"] = currency_priceTableInfo
  30. tables["deposit"] = depositTableInfo
  31. tables["log"] = logTableInfo
  32. tables["log_relation"] = log_relationTableInfo
  33. tables["sale"] = saleTableInfo
  34. tables["sale_auction_temp"] = sale_auction_tempTableInfo
  35. tables["setting"] = settingTableInfo
  36. tables["token"] = tokenTableInfo
  37. tables["traits"] = traitsTableInfo
  38. tables["user"] = userTableInfo
  39. tables["user_authentication"] = user_authenticationTableInfo
  40. tables["user_like"] = user_likeTableInfo
  41. tables["user_profile"] = user_profileTableInfo
  42. tables["user_wallet"] = user_walletTableInfo
  43. }
  44. // String describe the action
  45. func (i Action) String() string {
  46. switch i {
  47. case Create:
  48. return "Create"
  49. case RetrieveOne:
  50. return "RetrieveOne"
  51. case RetrieveMany:
  52. return "RetrieveMany"
  53. case Update:
  54. return "Update"
  55. case Delete:
  56. return "Delete"
  57. case FetchDDL:
  58. return "FetchDDL"
  59. default:
  60. return fmt.Sprintf("unknown action: %d", int(i))
  61. }
  62. }
  63. // Model interface methods for database structs generated
  64. type Model interface {
  65. TableName() string
  66. BeforeSave() error
  67. Prepare()
  68. Validate(action Action) error
  69. TableInfo() *TableInfo
  70. }
  71. // TableInfo describes a table in the database
  72. type TableInfo struct {
  73. Name string `json:"name"`
  74. Columns []*ColumnInfo `json:"columns"`
  75. }
  76. // ColumnInfo describes a column in the database table
  77. type ColumnInfo struct {
  78. Index int `json:"index"`
  79. GoFieldName string `json:"go_field_name"`
  80. GoFieldType string `json:"go_field_type"`
  81. JSONFieldName string `json:"json_field_name"`
  82. ProtobufFieldName string `json:"protobuf_field_name"`
  83. ProtobufType string `json:"protobuf_field_type"`
  84. ProtobufPos int `json:"protobuf_field_pos"`
  85. Comment string `json:"comment"`
  86. Notes string `json:"notes"`
  87. Name string `json:"name"`
  88. Nullable bool `json:"is_nullable"`
  89. DatabaseTypeName string `json:"database_type_name"`
  90. DatabaseTypePretty string `json:"database_type_pretty"`
  91. IsPrimaryKey bool `json:"is_primary_key"`
  92. IsAutoIncrement bool `json:"is_auto_increment"`
  93. IsArray bool `json:"is_array"`
  94. ColumnType string `json:"column_type"`
  95. ColumnLength int64 `json:"column_length"`
  96. DefaultValue string `json:"default_value"`
  97. }
  98. // GetTableInfo retrieve TableInfo for a table
  99. func GetTableInfo(name string) (*TableInfo, bool) {
  100. val, ok := tables[name]
  101. return val, ok
  102. }