mng_v2.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "time"
  7. "go-common/app/admin/main/search/model"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _typeDatabus = `databus`
  12. _typeDB = `db`
  13. _typeTable = `table`
  14. _businessAllV2SQL = `SELECT id,pid,name,description,state FROM gf_business`
  15. _businessInfoV2SQL = `SELECT id,pid,name,description,data_conf,index_conf,business_conf,state,mtime FROM gf_business WHERE name=?`
  16. _bussinessInsSQL = `INSERT INTO gf_business (pid,name,description) VALUES(?,?,?)`
  17. _bussinessUpdateSQL = `UPDATE gf_business SET %s=? WHERE name=?`
  18. _assetDBTablesV2SQL = `SELECT id,type,db,name,regex,fields,description,state FROM gf_asset WHERE type=? OR type=?`
  19. _assetDBInsSQL = `INSERT INTO gf_asset (type,name,description,dsn) VALUES(?,?,?,?)`
  20. _assetTableInsSQL = `INSERT INTO gf_asset (type,name,db,regex,fields,description) VALUES(?,?,?,?,?,?)`
  21. _assetTableUpdateSQL = `UPDATE gf_asset set fields=? WHERE name=?`
  22. _assetSQL = `SELECT id,type,name,dsn,db,regex,fields,description,state FROM gf_asset WHERE name=?`
  23. )
  24. // BusinessAllV2 .
  25. func (d *Dao) BusinessAllV2(c context.Context) (list []*model.GFBusiness, err error) {
  26. rows, err := d.db.Query(c, _businessAllV2SQL)
  27. if err != nil {
  28. return
  29. }
  30. defer rows.Close()
  31. for rows.Next() {
  32. t := new(model.GFBusiness)
  33. if err = rows.Scan(&t.ID, &t.PID, &t.Name, &t.Description, &t.State); err != nil {
  34. return
  35. }
  36. list = append(list, t)
  37. }
  38. err = rows.Err()
  39. return
  40. }
  41. // BusinessInfoV2 .
  42. func (d *Dao) BusinessInfoV2(c context.Context, name string) (b *model.GFBusiness, err error) {
  43. row := d.db.QueryRow(c, _businessInfoV2SQL, name)
  44. if err != nil {
  45. return
  46. }
  47. b = new(model.GFBusiness)
  48. if err = row.Scan(&b.ID, &b.PID, &b.Name, &b.Description, &b.DataConf, &b.IndexConf, &b.BusinessConf, &b.State, &b.Mtime); err != nil {
  49. if err == sql.ErrNoRows {
  50. err = nil
  51. b = nil
  52. return
  53. }
  54. }
  55. tm, _ := time.Parse(time.RFC3339, b.Mtime)
  56. b.Mtime = tm.Format("2006-01-02 15:04:05")
  57. return
  58. }
  59. // BusinessIns insert business.
  60. func (d *Dao) BusinessIns(c context.Context, pid int64, name, description string) (rows int64, err error) {
  61. res, err := d.db.Exec(c, _bussinessInsSQL, pid, name, description)
  62. if err != nil {
  63. log.Error("d.db.Exec error(%v)", err)
  64. return
  65. }
  66. return res.LastInsertId()
  67. }
  68. // BusinessUpdate update business.
  69. func (d *Dao) BusinessUpdate(c context.Context, name, field, value string) (rows int64, err error) {
  70. res, err := d.db.Exec(c, fmt.Sprintf(_bussinessUpdateSQL, field), value, name)
  71. if err != nil {
  72. log.Error("d.db.Exec error(%v)", err)
  73. return
  74. }
  75. return res.RowsAffected()
  76. }
  77. // AssetDBTables .
  78. func (d *Dao) AssetDBTables(c context.Context) (list []*model.GFAsset, err error) {
  79. rows, err := d.db.Query(c, _assetDBTablesV2SQL, _typeDB, _typeTable)
  80. if err != nil {
  81. return
  82. }
  83. defer rows.Close()
  84. for rows.Next() {
  85. t := new(model.GFAsset)
  86. if err = rows.Scan(&t.ID, &t.Type, &t.DB, &t.Name, &t.Regex, &t.Fields, &t.Description, &t.State); err != nil {
  87. return
  88. }
  89. list = append(list, t)
  90. }
  91. err = rows.Err()
  92. return
  93. }
  94. // AssetDBIns insert db asset.
  95. func (d *Dao) AssetDBIns(c context.Context, name, description, dsn string) (rows int64, err error) {
  96. res, err := d.db.Exec(c, _assetDBInsSQL, _typeDB, name, description, dsn)
  97. if err != nil {
  98. log.Error("d.db.Exec error(%v)", err)
  99. return
  100. }
  101. return res.LastInsertId()
  102. }
  103. // AssetTableIns insert table asset.
  104. func (d *Dao) AssetTableIns(c context.Context, name, db, regex, fields, description string) (rows int64, err error) {
  105. res, err := d.db.Exec(c, _assetTableInsSQL, _typeTable, name, db, regex, fields, description)
  106. if err != nil {
  107. log.Error("d.db.Exec error(%v)", err)
  108. return
  109. }
  110. return res.LastInsertId()
  111. }
  112. // UpdateAssetTable update table asset.
  113. func (d *Dao) UpdateAssetTable(c context.Context, name, fields string) (rows int64, err error) {
  114. res, err := d.db.Exec(c, _assetTableUpdateSQL, fields, name)
  115. if err != nil {
  116. log.Error("d.db.Exec error(%v)", err)
  117. return
  118. }
  119. return res.RowsAffected()
  120. }
  121. // Asset .
  122. func (d *Dao) Asset(c context.Context, name string) (r *model.GFAsset, err error) {
  123. row := d.db.QueryRow(c, _assetSQL, name)
  124. r = new(model.GFAsset)
  125. if err = row.Scan(&r.ID, &r.Type, &r.Name, &r.DSN, &r.DB, &r.Regex, &r.Fields, &r.Description, &r.State); err != nil {
  126. if err == sql.ErrNoRows {
  127. err = nil
  128. r = nil
  129. return
  130. }
  131. }
  132. return
  133. }