package models import "fmt" // Action CRUD actions type Action int32 var ( // Create action when record is created Create = Action(0) // RetrieveOne action when a record is retrieved from db RetrieveOne = Action(1) // RetrieveMany action when record(s) are retrieved from db RetrieveMany = Action(2) // Update action when record is updated in db Update = Action(3) // Delete action when record is deleted in db Delete = Action(4) // FetchDDL action when fetching ddl info from db FetchDDL = Action(5) tables map[string]*TableInfo ) func init() { tables = make(map[string]*TableInfo) tables["admin"] = adminTableInfo tables["admin_log"] = admin_logTableInfo tables["admin_permission"] = admin_permissionTableInfo tables["artist_profile"] = artist_profileTableInfo tables["bid_log"] = bid_logTableInfo tables["collection"] = collectionTableInfo tables["collection_profile"] = collection_profileTableInfo tables["currency_price"] = currency_priceTableInfo tables["deposit"] = depositTableInfo tables["log"] = logTableInfo tables["log_relation"] = log_relationTableInfo tables["sale"] = saleTableInfo tables["sale_auction_temp"] = sale_auction_tempTableInfo tables["setting"] = settingTableInfo tables["token"] = tokenTableInfo tables["traits"] = traitsTableInfo tables["user"] = userTableInfo tables["user_authentication"] = user_authenticationTableInfo tables["user_like"] = user_likeTableInfo tables["user_profile"] = user_profileTableInfo tables["user_wallet"] = user_walletTableInfo } // String describe the action func (i Action) String() string { switch i { case Create: return "Create" case RetrieveOne: return "RetrieveOne" case RetrieveMany: return "RetrieveMany" case Update: return "Update" case Delete: return "Delete" case FetchDDL: return "FetchDDL" default: return fmt.Sprintf("unknown action: %d", int(i)) } } // Model interface methods for database structs generated type Model interface { TableName() string BeforeSave() error Prepare() Validate(action Action) error TableInfo() *TableInfo } // TableInfo describes a table in the database type TableInfo struct { Name string `json:"name"` Columns []*ColumnInfo `json:"columns"` } // ColumnInfo describes a column in the database table type ColumnInfo struct { Index int `json:"index"` GoFieldName string `json:"go_field_name"` GoFieldType string `json:"go_field_type"` JSONFieldName string `json:"json_field_name"` ProtobufFieldName string `json:"protobuf_field_name"` ProtobufType string `json:"protobuf_field_type"` ProtobufPos int `json:"protobuf_field_pos"` Comment string `json:"comment"` Notes string `json:"notes"` Name string `json:"name"` Nullable bool `json:"is_nullable"` DatabaseTypeName string `json:"database_type_name"` DatabaseTypePretty string `json:"database_type_pretty"` IsPrimaryKey bool `json:"is_primary_key"` IsAutoIncrement bool `json:"is_auto_increment"` IsArray bool `json:"is_array"` ColumnType string `json:"column_type"` ColumnLength int64 `json:"column_length"` DefaultValue string `json:"default_value"` } // GetTableInfo retrieve TableInfo for a table func GetTableInfo(name string) (*TableInfo, bool) { val, ok := tables[name] return val, ok }