mysql.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package v1
  2. import (
  3. "bytes"
  4. "context"
  5. "go-common/app/infra/config/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _getToken = "SELECT token FROM service_name WHERE name=? AND environment=?"
  12. _getBuildVersion = "SELECT b.config_id FROM service_name s, build_version b WHERE b.version=? AND s.name=? And s.environment=? AND s.id=b.service_id AND b.state=2"
  13. _getVersions = "SELECT c.id,c.remark FROM service_config c, service_name s WHERE s.name=? AND s.environment=? AND s.id =c.service_id AND c.state=? ORDER BY c.id DESC"
  14. _getNamespace = "SELECT id,namespace FROM service_namespace WHERE config_id=?"
  15. _getValue = "SELECT config_id,namespace_id,name,config FROM service_config_value WHERE config_id=?"
  16. _getFile = "SELECT config FROM service_config_value WHERE config_id=? AND name =?"
  17. _getBuilds = "SELECT b.version FROM service_name s ,build_version b WHERE s.name=? AND s.environment=? AND s.id=b.service_id AND b.state=2 ORDER BY b.id DESC"
  18. _getServiceID = "SELECT id FROM service_name where name=? AND environment =?"
  19. _insertVersion = "INSERT INTO service_config(service_id,state,operator) VALUES (?,?,?)"
  20. _insertConfigs = "INSERT INTO service_config_value(config_id,name,config,operator) VALUES "
  21. _updateConfigs = "UPDATE service_config_value SET config=?,operator=? WHERE config_id = ? AND name = ?"
  22. _insertLog = "INSERT INTO log(username,business,info) VALUES (?,?,?)"
  23. )
  24. // Token return a Secret from mysql.
  25. func (d *Dao) Token(c context.Context, svr, env string) (token string, err error) {
  26. row := d.db.QueryRow(c, _getToken, svr, env)
  27. if err = row.Scan(&token); err != nil {
  28. log.Error("row.Scan error(%v) svrName(%v)", err, svr)
  29. if err == sql.ErrNoRows {
  30. err = nil
  31. return
  32. }
  33. }
  34. return
  35. }
  36. // BuildVersion return service build version from mysql.
  37. func (d *Dao) BuildVersion(c context.Context, svr, bver, env string) (version int64, err error) {
  38. row := d.db.QueryRow(c, _getBuildVersion, bver, svr, env)
  39. if err = row.Scan(&version); err != nil {
  40. if err == sql.ErrNoRows {
  41. version = model.UnknownVersion
  42. err = nil
  43. return
  44. }
  45. log.Error("row.Scan error(%v)", err)
  46. }
  47. return
  48. }
  49. // Values return values from mysql.
  50. func (d *Dao) Values(c context.Context, ver int64) (rs []*model.NSValue, err error) {
  51. rows, err := d.db.Query(c, _getValue, ver)
  52. if err != nil {
  53. log.Error("db.Query(%d) error(%v)", ver, err)
  54. return
  55. }
  56. defer rows.Close()
  57. for rows.Next() {
  58. var r model.NSValue
  59. if err = rows.Scan(&r.ConfigID, &r.NamespaceID, &r.Name, &r.Config); err != nil {
  60. log.Error("rows.Scan error(%v)", err)
  61. return
  62. }
  63. rs = append(rs, &r)
  64. }
  65. return
  66. }
  67. // Namespaces return namespaces from mysql
  68. func (d *Dao) Namespaces(c context.Context, ver int64) (rs map[int64]string, err error) {
  69. rows, err := d.db.Query(c, _getNamespace, ver)
  70. if err != nil {
  71. log.Error("db.Query(%d) error(%v)", err)
  72. return
  73. }
  74. rs = make(map[int64]string)
  75. defer rows.Close()
  76. for rows.Next() {
  77. var id int64
  78. var name string
  79. if err = rows.Scan(&id, &name); err != nil {
  80. log.Error("rows.Scan error(%v)", err)
  81. return
  82. }
  83. rs[id] = name
  84. }
  85. return
  86. }
  87. //Versions return versions from mysql
  88. func (d *Dao) Versions(c context.Context, svr, env string, state int8) (rs []*model.ReVer, err error) {
  89. rows, err := d.db.Query(c, _getVersions, svr, env, state)
  90. if err != nil {
  91. log.Error("db.Query(%s) error(%v)", svr, err)
  92. return
  93. }
  94. defer rows.Close()
  95. for rows.Next() {
  96. var r model.ReVer
  97. if err = rows.Scan(&r.Version, &r.Remark); err != nil {
  98. log.Error("rows.Scan error(%v)", err)
  99. return
  100. }
  101. rs = append(rs, &r)
  102. }
  103. return
  104. }
  105. // Value return values from mysql.
  106. func (d *Dao) Value(c context.Context, fname string, ver int64) (file string, err error) {
  107. row := d.db.QueryRow(c, _getFile, ver, fname)
  108. if err = row.Scan(&file); err != nil {
  109. log.Error("row.Scan error(%v)", err)
  110. if err == sql.ErrNoRows {
  111. err = ecode.NothingFound
  112. return
  113. }
  114. }
  115. return
  116. }
  117. // Builds get service builds.
  118. func (d *Dao) Builds(c context.Context, svr, env string) (rs []string, err error) {
  119. rows, err := d.db.Query(c, _getBuilds, svr, env)
  120. if err != nil {
  121. log.Error("db.Query(%s) error(%v)", svr, err)
  122. return
  123. }
  124. defer rows.Close()
  125. for rows.Next() {
  126. var r string
  127. if err = rows.Scan(&r); err != nil {
  128. log.Error("rows.Scan error(%v)", err)
  129. return
  130. }
  131. rs = append(rs, r)
  132. }
  133. return
  134. }
  135. // ServiceID get ServiceID.
  136. func (d *Dao) ServiceID(c context.Context, svr, env string) (ID int64, err error) {
  137. row := d.db.QueryRow(c, _getServiceID, svr, env)
  138. if err != nil {
  139. log.Error("db.Query(%s) error(%v)", svr, err)
  140. return
  141. }
  142. if err = row.Scan(&ID); err != nil {
  143. log.Error("row.Scan error(%v)", err)
  144. if err == sql.ErrNoRows {
  145. err = ecode.NothingFound
  146. return
  147. }
  148. }
  149. return
  150. }
  151. // TxInsertVer insert version.
  152. func (d *Dao) TxInsertVer(tx *sql.Tx, svrID int64, user string) (verID int64, err error) {
  153. row, err := tx.Exec(_insertVersion, svrID, model.ConfigIng, user)
  154. if err != nil {
  155. log.Error("db.insert(%d) error(%v)", svrID, err)
  156. return
  157. }
  158. return row.LastInsertId()
  159. }
  160. // TxInsertValues insert config values.
  161. func (d *Dao) TxInsertValues(c context.Context, tx *sql.Tx, verID int64, user string, data map[string]string) (err error) {
  162. var (
  163. buffer bytes.Buffer
  164. insertTp string
  165. stmt *sql.Stmt
  166. is []interface{}
  167. )
  168. buffer.WriteString(_insertConfigs)
  169. insertTp = "(?,?,?,?),"
  170. for key, val := range data {
  171. buffer.WriteString(insertTp)
  172. is = append(is, verID)
  173. is = append(is, key)
  174. is = append(is, val)
  175. is = append(is, user)
  176. }
  177. buffer.Truncate(buffer.Len() - 1)
  178. if stmt, err = tx.Prepare(buffer.String()); err != nil {
  179. log.Error("d.insert() error(%v)", err)
  180. return
  181. }
  182. _, err = stmt.Exec(c, is...)
  183. if err != nil {
  184. log.Error("d.insert() error(%v)", err)
  185. }
  186. return
  187. }
  188. // TxUpdateValues update config values.
  189. func (d *Dao) TxUpdateValues(tx *sql.Tx, verID int64, user string, data map[string]string) (err error) {
  190. for key, val := range data {
  191. if _, err = tx.Exec(_updateConfigs, val, user, verID, key); err != nil {
  192. log.Error("db.UpdateValues(%d) error(%v)", user, err)
  193. break
  194. }
  195. }
  196. if err != nil {
  197. log.Error("d.insert() error(%v)", err)
  198. }
  199. return
  200. }
  201. // InsertLog insert log.
  202. func (d *Dao) InsertLog(c context.Context, user, business, info string) (err error) {
  203. _, err = d.db.Exec(c, _insertLog, user, business, info)
  204. if err != nil {
  205. log.Error("db.InsertLog(%d) error(%v)", user, err)
  206. return
  207. }
  208. return
  209. }