chat_configs_model.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package model
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "strings"
  6. "github.com/tal-tech/go-zero/core/stores/builder"
  7. "github.com/tal-tech/go-zero/core/stores/sqlc"
  8. "github.com/tal-tech/go-zero/core/stores/sqlx"
  9. "github.com/tal-tech/go-zero/core/stringx"
  10. )
  11. var (
  12. chatConfigsFieldNames = builder.RawFieldNames(&ChatConfigs{})
  13. chatConfigsRows = strings.Join(chatConfigsFieldNames, ",")
  14. chatConfigsRowsExpectAutoSet = strings.Join(stringx.Remove(chatConfigsFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
  15. chatConfigsRowsWithPlaceHolder = strings.Join(stringx.Remove(chatConfigsFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
  16. )
  17. type (
  18. ChatConfigsModel interface {
  19. Insert(data *ChatConfigs) (sql.Result, error)
  20. FindOne(id int64) (*ChatConfigs, error)
  21. FindOneByConfKey(confKey string) (*ChatConfigs, error)
  22. Update(data *ChatConfigs) error
  23. Delete(id int64) error
  24. FindAll() ([]ChatConfigs, error)
  25. }
  26. defaultChatConfigsModel struct {
  27. conn sqlx.SqlConn
  28. table string
  29. }
  30. ChatConfigs struct {
  31. Id int64 `db:"id"`
  32. ConfName string `db:"conf_name"`
  33. ConfKey string `db:"conf_key"`
  34. ConfValue string `db:"conf_value"`
  35. }
  36. )
  37. func NewChatConfigsModel(conn sqlx.SqlConn) ChatConfigsModel {
  38. return &defaultChatConfigsModel{
  39. conn: conn,
  40. table: "`chat_configs`",
  41. }
  42. }
  43. func (m *defaultChatConfigsModel) Insert(data *ChatConfigs) (sql.Result, error) {
  44. query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?)", m.table, chatConfigsRowsExpectAutoSet)
  45. ret, err := m.conn.Exec(query, data.ConfName, data.ConfKey, data.ConfValue)
  46. return ret, err
  47. }
  48. func (m *defaultChatConfigsModel) FindOne(id int64) (*ChatConfigs, error) {
  49. query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", chatConfigsRows, m.table)
  50. var resp ChatConfigs
  51. err := m.conn.QueryRow(&resp, query, id)
  52. switch err {
  53. case nil:
  54. return &resp, nil
  55. case sqlc.ErrNotFound:
  56. return nil, ErrNotFound
  57. default:
  58. return nil, err
  59. }
  60. }
  61. func (m *defaultChatConfigsModel) FindOneByConfKey(confKey string) (*ChatConfigs, error) {
  62. var resp ChatConfigs
  63. query := fmt.Sprintf("select %s from %s where `conf_key` = ? limit 1", chatConfigsRows, m.table)
  64. err := m.conn.QueryRow(&resp, query, confKey)
  65. switch err {
  66. case nil:
  67. return &resp, nil
  68. case sqlc.ErrNotFound:
  69. return nil, ErrNotFound
  70. default:
  71. return nil, err
  72. }
  73. }
  74. func (m *defaultChatConfigsModel) Update(data *ChatConfigs) error {
  75. query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, chatConfigsRowsWithPlaceHolder)
  76. _, err := m.conn.Exec(query, data.ConfName, data.ConfKey, data.ConfValue, data.Id)
  77. return err
  78. }
  79. func (m *defaultChatConfigsModel) Delete(id int64) error {
  80. query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
  81. _, err := m.conn.Exec(query, id)
  82. return err
  83. }
  84. func (m *defaultChatConfigsModel) FindAll() ([]ChatConfigs, error) {
  85. configs := make([]ChatConfigs, 0)
  86. querySql := fmt.Sprintf("select %s from %s", configsRows, m.table)
  87. err := m.conn.QueryRows(&configs, querySql)
  88. if err != nil {
  89. if err == sqlx.ErrNotFound {
  90. return nil, ErrNotFound
  91. }
  92. return nil, err
  93. }
  94. return configs, err
  95. }