rediscfg.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package model
  2. import (
  3. "database/sql"
  4. "errors"
  5. )
  6. type RedisCfg struct {
  7. Model
  8. }
  9. type RedisCfgRow struct {
  10. Id int64
  11. Address string
  12. Remark string
  13. Password string
  14. MaxConnectWait int64
  15. MaxStatusFailed int64
  16. MinMemoryFree int64
  17. MinMemoryFreePC int64
  18. MaxMemoryUsage int64
  19. MaxConnection int64
  20. MaxEviIncreased int64
  21. MaxQPS int64
  22. MailList string
  23. Disabled bool
  24. }
  25. func NewRedisCfg(db *sql.DB) *RedisCfg {
  26. return &RedisCfg{
  27. Model{
  28. db: db,
  29. },
  30. }
  31. }
  32. func (m *RedisCfg) Insert(row *RedisCfgRow) (int64, error) {
  33. if row.Id <= 0 {
  34. return 0, errors.New("ID必须是大于0的整数")
  35. }
  36. if row.Address == "" {
  37. return 0, errors.New("Address不能为空")
  38. }
  39. disabledInt := 0
  40. if row.Disabled {
  41. disabledInt = 1
  42. }
  43. ret, err := m.db.Exec(`insert into rediscfg(
  44. address,
  45. remark,
  46. password,
  47. max_connect_wait,
  48. max_status_failed,
  49. min_memory_free,
  50. min_memory_free_pc,
  51. max_memory_usage,
  52. max_connection,
  53. max_evi_increased,
  54. max_qps,
  55. mail_list,
  56. disabled
  57. ) values(?,?,?,?,?,?,?,?,?,?,?,?,?)`,
  58. row.Address,
  59. row.Remark,
  60. row.Password,
  61. row.MaxConnectWait,
  62. row.MaxStatusFailed,
  63. row.MinMemoryFree,
  64. row.MinMemoryFreePC,
  65. row.MaxMemoryUsage,
  66. row.MaxConnection,
  67. row.MaxEviIncreased,
  68. row.MaxQPS,
  69. row.MailList,
  70. disabledInt,
  71. )
  72. if err != nil {
  73. return 0, err
  74. }
  75. return ret.LastInsertId()
  76. }
  77. func (m *RedisCfg) Update(row *RedisCfgRow) (int64, error) {
  78. if row.Id <= 0 {
  79. return 0, errors.New("ID必须是大于0的整数")
  80. }
  81. if row.Address == "" {
  82. return 0, errors.New("Address不能为空")
  83. }
  84. disabledInt := 0
  85. if row.Disabled {
  86. disabledInt = 1
  87. }
  88. ret, err := m.db.Exec(`update rediscfg set
  89. address=?,
  90. remark=?,
  91. password=?,
  92. max_connect_wait=?,
  93. max_status_failed=?,
  94. min_memory_free=?,
  95. min_memory_free_pc=?,
  96. max_memory_usage=?,
  97. max_connection=?,
  98. max_evi_increased=?,
  99. max_qps=?,
  100. mail_list=?,
  101. disabled=?
  102. where id=?`,
  103. row.Address,
  104. row.Remark,
  105. row.Password,
  106. row.MaxConnectWait,
  107. row.MaxStatusFailed,
  108. row.MinMemoryFree,
  109. row.MinMemoryFreePC,
  110. row.MaxMemoryUsage,
  111. row.MaxConnection,
  112. row.MaxEviIncreased,
  113. row.MaxQPS,
  114. row.MailList,
  115. disabledInt,
  116. row.Id,
  117. )
  118. if err != nil {
  119. return 0, nil
  120. }
  121. return ret.RowsAffected()
  122. }
  123. func (m *RedisCfg) Delete(id int64) (int64, error) {
  124. if id <= 0 {
  125. return 0, errors.New("ID必须是大于0的整数")
  126. }
  127. ret, err := m.db.Exec("delete from rediscfg where id=?", id)
  128. if err != nil {
  129. return 0, err
  130. }
  131. return ret.RowsAffected()
  132. }
  133. func (m *RedisCfg) Get(id int64) (*RedisCfgRow, error) {
  134. if id <= 0 {
  135. return nil, errors.New("ID必须是大于0的整数")
  136. }
  137. result := m.db.QueryRow(`select id,
  138. address,
  139. remark,
  140. password,
  141. max_connect_wait,
  142. max_status_failed,
  143. min_memory_free,
  144. min_memory_free_pc,
  145. max_memory_usage,
  146. max_connection,
  147. max_evi_increased,
  148. max_qps,
  149. mail_list,
  150. disabled from rediscfg where id=?`,
  151. id,
  152. )
  153. row := RedisCfgRow{}
  154. err := result.Scan(
  155. &row.Id,
  156. &row.Address,
  157. &row.Remark,
  158. &row.Password,
  159. &row.MaxConnectWait,
  160. &row.MaxStatusFailed,
  161. &row.MinMemoryFree,
  162. &row.MinMemoryFreePC,
  163. &row.MaxMemoryUsage,
  164. &row.MaxConnection,
  165. &row.MaxEviIncreased,
  166. &row.MaxQPS,
  167. &row.MailList,
  168. &row.Disabled,
  169. )
  170. if err != nil {
  171. if err == sql.ErrNoRows {
  172. return nil, errors.New("记录不存在")
  173. } else {
  174. return nil, err
  175. }
  176. } else {
  177. return &row, nil
  178. }
  179. }
  180. func (m *RedisCfg) GetAll(disabledInt int) ([]*RedisCfgRow, error) {
  181. var sql string
  182. if disabledInt < 0 {
  183. sql = `select id,
  184. address,
  185. remark,
  186. password,
  187. max_connect_wait,
  188. max_status_failed,
  189. min_memory_free,
  190. min_memory_free_pc,
  191. max_memory_usage,
  192. max_connection,
  193. max_evi_increased,
  194. max_qps,
  195. mail_list,
  196. disabled
  197. from rediscfg where disabled>? order by id`
  198. } else {
  199. sql = `select id,
  200. address,
  201. remark,
  202. password,
  203. max_connect_wait,
  204. max_status_failed,
  205. min_memory_free,
  206. min_memory_free_pc,
  207. max_memory_usage,
  208. max_connection,
  209. max_evi_increased,
  210. max_qps,
  211. mail_list,
  212. disabled
  213. from rediscfg where disabled=? order by id`
  214. }
  215. result, err := m.db.Query(sql, disabledInt)
  216. if err != nil {
  217. return nil, err
  218. }
  219. rows := make([]*RedisCfgRow, 0)
  220. for result.Next() {
  221. row := RedisCfgRow{}
  222. err = result.Scan(
  223. &row.Id,
  224. &row.Address,
  225. &row.Remark,
  226. &row.Password,
  227. &row.MaxConnectWait,
  228. &row.MaxStatusFailed,
  229. &row.MinMemoryFree,
  230. &row.MinMemoryFreePC,
  231. &row.MaxMemoryUsage,
  232. &row.MaxConnection,
  233. &row.MaxEviIncreased,
  234. &row.MaxQPS,
  235. &row.MailList,
  236. &row.Disabled,
  237. )
  238. if err != nil {
  239. continue
  240. }
  241. rows = append(rows, &row)
  242. }
  243. return rows, nil
  244. }