mysql.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "sync"
  6. "go-common/app/job/main/dm2/model"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. "go-common/library/sync/errgroup"
  10. "go-common/library/xstr"
  11. )
  12. const (
  13. _subtitleSharding = 100
  14. _subjectSharding = 100
  15. _indexSharding = 1000
  16. _contentSharding = 1000
  17. _addSubjectSQL = "INSERT INTO dm_subject_%02d(type,oid,pid,mid,maxlimit,attr) VALUES(?,?,?,?,?,?)"
  18. _updateChildpoolSQL = "UPDATE dm_subject_%02d SET childpool=? WHERE type=? AND oid=?"
  19. _updateSubMidSQL = "UPDATE dm_subject_%02d SET mid=? WHERE type=? AND oid=?"
  20. _updateSubAttrSQL = "UPDATE dm_subject_%02d SET attr=? WHERE type=? AND oid=?"
  21. _incrSubMCountSQL = "UPDATE dm_subject_%02d SET mcount=mcount+1 WHERE type=? AND oid=?"
  22. _incrSubCountSQL = "UPDATE dm_subject_%02d SET acount=acount+?,count=count+?,childpool=? WHERE type=? AND oid=?"
  23. _getSubjectSQL = "SELECT id,type,oid,pid,mid,state,attr,acount,count,mcount,move_count,maxlimit,childpool,ctime,mtime FROM dm_subject_%02d WHERE type=? AND oid=?"
  24. _addIndexSQL = "INSERT INTO dm_index_%03d(id,type,oid,mid,progress,state,pool,attr,ctime) VALUES(?,?,?,?,?,?,?,?,?)"
  25. _getIndexSQL = "SELECT id,type,oid,mid,progress,state,pool,attr,ctime,mtime FROM dm_index_%03d WHERE type=? AND oid=? AND state IN(0,6)"
  26. _idxSegIDSQL = "SELECT id FROM dm_index_%03d WHERE type=? AND oid=? AND progress>=? AND progress<? AND state IN(0,6) AND pool = ? limit ?"
  27. _idxSegSQL = "SELECT id,type,oid,mid,progress,state,pool,attr,ctime,mtime FROM dm_index_%03d WHERE type=? AND oid=? AND state IN(0,6) AND progress>=? AND progress<? AND pool=? limit ?"
  28. _idxIDSQL = "SELECT id FROM dm_index_%03d WHERE type=? AND oid=? AND state IN(0,6) AND pool=?"
  29. _idxsByidSQL = "SELECT id,type,oid,mid,progress,state,pool,attr,ctime,mtime FROM dm_index_%03d WHERE id IN(%s)"
  30. _idxsByPoolSQL = "SELECT id,type,oid,mid,progress,state,pool,attr,ctime,mtime FROM dm_index_%03d WHERE type=? AND oid=? AND state IN(0,6) AND pool=?"
  31. _addContentSQL = "REPLACE INTO dm_content_%03d(dmid,fontsize,color,mode,ip,plat,msg,ctime) VALUES(?,?,?,?,?,?,?,?)"
  32. _getContentsSQL = "SELECT dmid,fontsize,color,mode,ip,plat,msg,ctime,mtime FROM dm_content_%03d WHERE dmid IN(%s)"
  33. _getContentSQL = "SELECT dmid,fontsize,color,mode,ip,plat,msg,ctime,mtime FROM dm_content_%03d WHERE dmid=?"
  34. _addContentSpeSQL = "REPLACE INTO dm_special_content(dmid,msg,ctime) VALUES(?,?,?)"
  35. _getContentSpeSQL = "SELECT dmid,msg,ctime,mtime FROM dm_special_content WHERE dmid=?"
  36. _getContentsSpeSQL = "SELECT dmid,msg,ctime,mtime FROM dm_special_content WHERE dmid IN(%s)"
  37. //delete dm hide state
  38. _delDMHideState = "UPDATE dm_index_%03d SET state=? WHERE oid=? AND id=? AND state=?"
  39. // update subtitle upmid
  40. _getSubtitle = "SELECT id,oid,type,lan,status,mid,up_mid,subtitle_url,pub_time,reject_comment from subtitle_%02d WHERE id=?"
  41. _getSubtitles = "SELECT id,oid,type,lan,status,mid,up_mid,subtitle_url,pub_time,reject_comment from subtitle_%02d WHERE oid=? AND type=?"
  42. _updateSubtitle = "UPDATE subtitle_%02d SET up_mid=?,status=?,pub_time=?,reject_comment=? WHERE id=?"
  43. _addSubtitlePub = "INSERT INTO subtitle_pub(oid,type,lan,subtitle_id,is_delete) VALUES(?,?,?,?,?) ON DUPLICATE KEY UPDATE subtitle_id=?,is_delete=?"
  44. // get mask mid
  45. _getMaskMids = "SELECT mid from dm_mask_up where state=1"
  46. )
  47. func (d *Dao) hitSubject(oid int64) int64 {
  48. return oid % _subjectSharding
  49. }
  50. func (d *Dao) hitIndex(oid int64) int64 {
  51. return oid % _indexSharding
  52. }
  53. func (d *Dao) hitContent(oid int64) int64 {
  54. return oid % _contentSharding
  55. }
  56. func (d *Dao) hitSubtile(oid int64) int64 {
  57. return oid % _subtitleSharding
  58. }
  59. // AddSubject insert subject.
  60. func (d *Dao) AddSubject(c context.Context, tp int32, oid, pid, mid, maxlimit int64, attr int32) (lastID int64, err error) {
  61. res, err := d.dmWriter.Exec(c, fmt.Sprintf(_addSubjectSQL, d.hitSubject(oid)), tp, oid, pid, mid, maxlimit, attr)
  62. if err != nil {
  63. log.Error("dmWriter.Exec(%d,%d,%d,%d,%d,%d) error(%v)", tp, oid, pid, mid, maxlimit, attr, err)
  64. return
  65. }
  66. lastID, err = res.LastInsertId()
  67. return
  68. }
  69. // UpdateSubAttr .
  70. func (d *Dao) UpdateSubAttr(c context.Context, tp int32, oid int64, attr int32) (affect int64, err error) {
  71. res, err := d.dmWriter.Exec(c, fmt.Sprintf(_updateSubAttrSQL, d.hitSubject(oid)), attr, tp, oid)
  72. if err != nil {
  73. log.Error("dmWriter.Exec(%s,%d,%d) error(%v)", _updateSubMidSQL, oid, attr, err)
  74. return
  75. }
  76. affect, err = res.RowsAffected()
  77. return
  78. }
  79. // UpdateSubMid update mid in dm_subject.
  80. func (d *Dao) UpdateSubMid(c context.Context, tp int32, oid, mid int64) (affect int64, err error) {
  81. res, err := d.dmWriter.Exec(c, fmt.Sprintf(_updateSubMidSQL, d.hitSubject(oid)), mid, tp, oid)
  82. if err != nil {
  83. log.Error("dmWriter.Exec(%s,%d,%d) error(%v)", _updateSubMidSQL, oid, mid, err)
  84. return
  85. }
  86. affect, err = res.RowsAffected()
  87. return
  88. }
  89. // Subject get subject info from db.
  90. func (d *Dao) Subject(c context.Context, tp int32, oid int64) (s *model.Subject, err error) {
  91. s = &model.Subject{}
  92. row := d.dmReader.QueryRow(c, fmt.Sprintf(_getSubjectSQL, d.hitSubject(oid)), tp, oid)
  93. if err = row.Scan(&s.ID, &s.Type, &s.Oid, &s.Pid, &s.Mid, &s.State, &s.Attr, &s.ACount, &s.Count, &s.MCount, &s.MoveCnt, &s.Maxlimit, &s.Childpool, &s.Ctime, &s.Mtime); err != nil {
  94. if err == sql.ErrNoRows {
  95. s = nil
  96. err = nil
  97. } else {
  98. log.Error("row.Scan() error(%v)", err)
  99. }
  100. }
  101. return
  102. }
  103. // UpdateChildpool update childpool.
  104. func (d *Dao) UpdateChildpool(c context.Context, tp int32, oid int64, childpool int32) (affect int64, err error) {
  105. res, err := d.dmWriter.Exec(c, fmt.Sprintf(_updateChildpoolSQL, d.hitSubject(oid)), childpool, tp, oid)
  106. if err != nil {
  107. log.Error("dmWriter.Exec(%s %d) error(%v)", _updateChildpoolSQL, oid, err)
  108. return
  109. }
  110. affect, err = res.RowsAffected()
  111. return
  112. }
  113. // TxIncrSubjectCount update acount,count,childpool of dm by transcation.
  114. func (d *Dao) TxIncrSubjectCount(tx *sql.Tx, tp int32, oid, acount, count int64, childpool int32) (affect int64, err error) {
  115. res, err := tx.Exec(fmt.Sprintf(_incrSubCountSQL, d.hitSubject(oid)), acount, count, childpool, tp, oid)
  116. if err != nil {
  117. log.Error("tx.Exec error(%v)", err)
  118. return
  119. }
  120. return res.RowsAffected()
  121. }
  122. // TxAddIndex add index of dm by transcation.
  123. func (d *Dao) TxAddIndex(tx *sql.Tx, m *model.DM) (id int64, err error) {
  124. res, err := tx.Exec(fmt.Sprintf(_addIndexSQL, d.hitIndex(m.Oid)), m.ID, m.Type, m.Oid, m.Mid, m.Progress, m.State, m.Pool, m.Attr, m.Ctime)
  125. if err != nil {
  126. log.Error("tx.Exec error(%v)", err)
  127. return
  128. }
  129. return res.LastInsertId()
  130. }
  131. // Indexs get dm index by type and oid.
  132. func (d *Dao) Indexs(c context.Context, tp int32, oid int64) (idxMap map[int64]*model.DM, dmids, special []int64, err error) {
  133. rows, err := d.dmReader.Query(c, fmt.Sprintf(_getIndexSQL, d.hitIndex(oid)), tp, oid)
  134. if err != nil {
  135. log.Error("dmReader.Query(%d,%d) error(%v)", tp, oid, err)
  136. return
  137. }
  138. defer rows.Close()
  139. idxMap = make(map[int64]*model.DM)
  140. for rows.Next() {
  141. idx := &model.DM{}
  142. if err = rows.Scan(&idx.ID, &idx.Type, &idx.Oid, &idx.Mid, &idx.Progress, &idx.State, &idx.Pool, &idx.Attr, &idx.Ctime, &idx.Mtime); err != nil {
  143. log.Error("row.Scan() error(%v)", err)
  144. return
  145. }
  146. idxMap[idx.ID] = idx
  147. dmids = append(dmids, idx.ID)
  148. if idx.Pool == model.PoolSpecial {
  149. special = append(special, idx.ID)
  150. }
  151. }
  152. err = rows.Err()
  153. return
  154. }
  155. // IndexsSeg get segment index info from db by ps and pe.
  156. func (d *Dao) IndexsSeg(c context.Context, tp int32, oid, ps, pe, limit int64, pool int32) (res []*model.DM, dmids []int64, err error) {
  157. rows, err := d.dmReader.Query(c, fmt.Sprintf(_idxSegSQL, d.hitIndex(oid)), tp, oid, ps, pe, pool, limit)
  158. if err != nil {
  159. log.Error("db.Query(%d %d %d %d) error(%v)", tp, oid, ps, pe, err)
  160. return
  161. }
  162. defer rows.Close()
  163. for rows.Next() {
  164. dm := &model.DM{}
  165. if err = rows.Scan(&dm.ID, &dm.Type, &dm.Oid, &dm.Mid, &dm.Progress, &dm.State, &dm.Pool, &dm.Attr, &dm.Ctime, &dm.Mtime); err != nil {
  166. log.Error("row.Scan() error(%v)", err)
  167. return
  168. }
  169. res = append(res, dm)
  170. dmids = append(dmids, dm.ID)
  171. }
  172. return
  173. }
  174. // IndexsSegID get segment dmids.
  175. func (d *Dao) IndexsSegID(c context.Context, tp int32, oid, ps, pe, limit int64, pool int32) (dmids []int64, err error) {
  176. rows, err := d.dmReader.Query(c, fmt.Sprintf(_idxSegIDSQL, d.hitIndex(oid)), tp, oid, ps, pe, pool, limit)
  177. if err != nil {
  178. log.Error("db.Query() error(%v)", err)
  179. return
  180. }
  181. defer rows.Close()
  182. var dmid int64
  183. for rows.Next() {
  184. if err = rows.Scan(&dmid); err != nil {
  185. log.Error("rows.Scan() error(%v)", err)
  186. return
  187. }
  188. dmids = append(dmids, dmid)
  189. }
  190. if err = rows.Err(); err != nil {
  191. log.Error("rows.Err() error(%v)", err)
  192. }
  193. return
  194. }
  195. // IndexsID get dmids.
  196. func (d *Dao) IndexsID(c context.Context, tp int32, oid int64, pool int32) (dmids []int64, err error) {
  197. rows, err := d.dmReader.Query(c, fmt.Sprintf(_idxIDSQL, d.hitIndex(oid)), tp, oid, pool)
  198. if err != nil {
  199. log.Error("db.Query() error(%v)", err)
  200. return
  201. }
  202. defer rows.Close()
  203. var dmid int64
  204. for rows.Next() {
  205. if err = rows.Scan(&dmid); err != nil {
  206. log.Error("rows.Scan() error(%v)", err)
  207. return
  208. }
  209. dmids = append(dmids, dmid)
  210. }
  211. if err = rows.Err(); err != nil {
  212. log.Error("rows.Err() error(%v)", err)
  213. }
  214. return
  215. }
  216. // IndexsByid get dm index by dmids.
  217. func (d *Dao) IndexsByid(c context.Context, tp int32, oid int64, dmids []int64) (idxMap map[int64]*model.DM, special []int64, err error) {
  218. query := fmt.Sprintf(_idxsByidSQL, d.hitIndex(oid), xstr.JoinInts(dmids))
  219. rows, err := d.dmReader.Query(c, query)
  220. if err != nil {
  221. log.Error("db.Query(%s) error(%v)", query, err)
  222. return
  223. }
  224. defer rows.Close()
  225. idxMap = make(map[int64]*model.DM)
  226. for rows.Next() {
  227. idx := new(model.DM)
  228. if err = rows.Scan(&idx.ID, &idx.Type, &idx.Oid, &idx.Mid, &idx.Progress, &idx.State, &idx.Pool, &idx.Attr, &idx.Ctime, &idx.Mtime); err != nil {
  229. log.Error("row.Scan() error(%v)", err)
  230. return
  231. }
  232. idxMap[idx.ID] = idx
  233. if idx.Pool == model.PoolSpecial {
  234. special = append(special, idx.ID)
  235. }
  236. }
  237. if err = rows.Err(); err != nil {
  238. log.Error("rows.Err() error(%v)", err)
  239. }
  240. return
  241. }
  242. // IndexsByPool get dm index by type,oid and pool.
  243. func (d *Dao) IndexsByPool(c context.Context, tp int32, oid int64, pool int32) (dms []*model.DM, dmids []int64, err error) {
  244. rows, err := d.dmReader.Query(c, fmt.Sprintf(_idxsByPoolSQL, d.hitIndex(oid)), tp, oid, pool)
  245. if err != nil {
  246. log.Error("dmReader.Query(tp:%v,oid:%v) error(%v)", tp, oid, err)
  247. return
  248. }
  249. defer rows.Close()
  250. dms = make([]*model.DM, 0, 100)
  251. for rows.Next() {
  252. dm := &model.DM{}
  253. if err = rows.Scan(&dm.ID, &dm.Type, &dm.Oid, &dm.Mid, &dm.Progress, &dm.State, &dm.Pool, &dm.Attr, &dm.Ctime, &dm.Mtime); err != nil {
  254. log.Error("row.Scan() error(%v)", err)
  255. return
  256. }
  257. dms = append(dms, dm)
  258. dmids = append(dmids, dm.ID)
  259. }
  260. if err = rows.Err(); err != nil {
  261. log.Error("rows.Err() error(%v)", err)
  262. }
  263. return
  264. }
  265. // TxAddContent add content of dm by transcation.
  266. func (d *Dao) TxAddContent(tx *sql.Tx, oid int64, m *model.Content) (id int64, err error) {
  267. res, err := tx.Exec(fmt.Sprintf(_addContentSQL, d.hitContent(oid)), m.ID, m.FontSize, m.Color, m.Mode, m.IP, m.Plat, m.Msg, m.Ctime)
  268. if err != nil {
  269. log.Error("tx.Exec error(%v)", err)
  270. return
  271. }
  272. return res.LastInsertId()
  273. }
  274. // TxAddContentSpecial add special dm by transcation.
  275. func (d *Dao) TxAddContentSpecial(tx *sql.Tx, m *model.ContentSpecial) (id int64, err error) {
  276. res, err := tx.Exec(_addContentSpeSQL, m.ID, m.Msg, m.Ctime)
  277. if err != nil {
  278. log.Error("tx.Exec error(%v)", err)
  279. return
  280. }
  281. return res.LastInsertId()
  282. }
  283. // Content dm content by dmid
  284. func (d *Dao) Content(c context.Context, oid, dmid int64) (ct *model.Content, err error) {
  285. ct = &model.Content{}
  286. row := d.dmReader.QueryRow(c, fmt.Sprintf(_getContentSQL, d.hitContent(oid)), dmid)
  287. if err = row.Scan(&ct.ID, &ct.FontSize, &ct.Color, &ct.Mode, &ct.IP, &ct.Plat, &ct.Msg, &ct.Ctime, &ct.Mtime); err != nil {
  288. ct = nil
  289. log.Error("row.Scan() error(%v)", err)
  290. }
  291. return
  292. }
  293. // Contents multi get dm content by dmids.
  294. func (d *Dao) Contents(c context.Context, oid int64, dmids []int64) (ctsMap map[int64]*model.Content, err error) {
  295. var (
  296. wg errgroup.Group
  297. lock sync.Mutex
  298. )
  299. ctsMap = make(map[int64]*model.Content)
  300. pageNum := len(dmids) / d.pageSize
  301. if len(dmids)%d.pageSize > 0 {
  302. pageNum = pageNum + 1
  303. }
  304. for i := 0; i < pageNum; i++ {
  305. start := i * d.pageSize
  306. end := (i + 1) * d.pageSize
  307. if end > len(dmids) {
  308. end = len(dmids)
  309. }
  310. wg.Go(func() (err error) {
  311. rows, err := d.dmReader.Query(c, fmt.Sprintf(_getContentsSQL, d.hitContent(oid), xstr.JoinInts(dmids[start:end])))
  312. if err != nil {
  313. log.Error("db.Query(%s) error(%v)", fmt.Sprintf(_getContentsSQL, d.hitContent(oid), xstr.JoinInts(dmids)), err)
  314. return
  315. }
  316. defer rows.Close()
  317. for rows.Next() {
  318. ct := &model.Content{}
  319. if err = rows.Scan(&ct.ID, &ct.FontSize, &ct.Color, &ct.Mode, &ct.IP, &ct.Plat, &ct.Msg, &ct.Ctime, &ct.Mtime); err != nil {
  320. log.Error("rows.Scan() error(%v)", err)
  321. return
  322. }
  323. lock.Lock()
  324. ctsMap[ct.ID] = ct
  325. lock.Unlock()
  326. }
  327. err = rows.Err()
  328. return
  329. })
  330. }
  331. if err = wg.Wait(); err != nil {
  332. log.Error("wg.Wait() error(%v)", err)
  333. }
  334. return
  335. }
  336. // ContentsSpecial multi get special dm content by dmids.
  337. func (d *Dao) ContentsSpecial(c context.Context, dmids []int64) (res map[int64]*model.ContentSpecial, err error) {
  338. res = make(map[int64]*model.ContentSpecial, len(dmids))
  339. rows, err := d.dmReader.Query(c, fmt.Sprintf(_getContentsSpeSQL, xstr.JoinInts(dmids)))
  340. if err != nil {
  341. log.Error("db.Query() error(%v)", err)
  342. return
  343. }
  344. defer rows.Close()
  345. for rows.Next() {
  346. content := &model.ContentSpecial{}
  347. if err = rows.Scan(&content.ID, &content.Msg, &content.Ctime, &content.Mtime); err != nil {
  348. log.Error("rows.Scan() error(%v)", err)
  349. return
  350. }
  351. res[content.ID] = content
  352. }
  353. return
  354. }
  355. // ContentSpecial get special dm content by dmids.
  356. func (d *Dao) ContentSpecial(c context.Context, dmid int64) (contentSpe *model.ContentSpecial, err error) {
  357. contentSpe = &model.ContentSpecial{}
  358. row := d.dmReader.QueryRow(c, _getContentSpeSQL, dmid)
  359. if err = row.Scan(&contentSpe.ID, &contentSpe.Msg, &contentSpe.Ctime, &contentSpe.Mtime); err != nil {
  360. log.Error("rows.Scan() error(%v)", err)
  361. }
  362. return
  363. }
  364. // DelDMHideState del dm hide state
  365. func (d *Dao) DelDMHideState(c context.Context, tp int32, oid int64, dmid int64) (affect int64, err error) {
  366. res, err := d.dmWriter.Exec(c, fmt.Sprintf(_delDMHideState, d.hitIndex(oid)), model.StateNormal, oid, dmid, model.StateHide)
  367. if err != nil {
  368. log.Error("dmWriter.Exec(%s %d dmid=%d) error(%v)", _delDMHideState, oid, dmid, err)
  369. return
  370. }
  371. return res.RowsAffected()
  372. }
  373. // TxIncrSubMCount update monitor dm count.
  374. func (d *Dao) TxIncrSubMCount(tx *sql.Tx, tp int32, oid int64) (affect int64, err error) {
  375. res, err := tx.Exec(fmt.Sprintf(_incrSubMCountSQL, d.hitSubject(oid)), tp, oid)
  376. if err != nil {
  377. log.Error("tx.Exec(%s,%d,%d) error(%v)", _incrSubMCountSQL, tp, oid, err)
  378. return
  379. }
  380. return res.RowsAffected()
  381. }
  382. // UpdateSubtitle update subtitle mid
  383. func (d *Dao) UpdateSubtitle(c context.Context, subtitle *model.Subtitle) (err error) {
  384. if _, err = d.biliDMWriter.Exec(c, fmt.Sprintf(_updateSubtitle, d.hitSubtile(subtitle.Oid)), subtitle.UpMid, subtitle.Status,
  385. subtitle.PubTime, subtitle.RejectComment, subtitle.ID); err != nil {
  386. log.Error("biliDMWriter.Exec(query:%v,subtitle:%+v) error(%v)", _updateSubtitle, subtitle, err)
  387. return
  388. }
  389. return
  390. }
  391. // GetSubtitles .
  392. func (d *Dao) GetSubtitles(c context.Context, tp int32, oid int64) (subtitles []*model.Subtitle, err error) {
  393. rows, err := d.biliDMWriter.Query(c, fmt.Sprintf(_getSubtitles, d.hitSubtile(oid)), oid, tp)
  394. if err != nil {
  395. log.Error("biliDMWriter.Query(%s,%d,%d) error(%v)", _getSubtitles, oid, tp, err)
  396. return
  397. }
  398. defer rows.Close()
  399. for rows.Next() {
  400. var subtitle = &model.Subtitle{}
  401. if err = rows.Scan(&subtitle.ID, &subtitle.Oid, &subtitle.Type, &subtitle.Lan, &subtitle.Status, &subtitle.Mid, &subtitle.UpMid,
  402. &subtitle.SubtitleURL, &subtitle.PubTime, &subtitle.RejectComment); err != nil {
  403. log.Error("biliDMWriter.Scan(%s,%d,%d) error(%v)", _getSubtitles, oid, tp, err)
  404. return
  405. }
  406. subtitles = append(subtitles, subtitle)
  407. }
  408. if err = rows.Err(); err != nil {
  409. log.Error("biliDMWriter.rows.Err()(%s,%d,%d) error(%v)", _getSubtitles, oid, tp, err)
  410. return
  411. }
  412. return
  413. }
  414. // GetSubtitle .
  415. func (d *Dao) GetSubtitle(c context.Context, oid int64, subtitleID int64) (subtitle *model.Subtitle, err error) {
  416. subtitle = &model.Subtitle{}
  417. row := d.biliDMWriter.QueryRow(c, fmt.Sprintf(_getSubtitle, d.hitSubtile(oid)), subtitleID)
  418. if err = row.Scan(&subtitle.ID, &subtitle.Oid, &subtitle.Type, &subtitle.Lan, &subtitle.Status, &subtitle.Mid, &subtitle.UpMid,
  419. &subtitle.SubtitleURL, &subtitle.PubTime, &subtitle.RejectComment); err != nil {
  420. if err == sql.ErrNoRows {
  421. subtitle = nil
  422. err = nil
  423. } else {
  424. log.Error("row.Scan() error(%v)", err)
  425. }
  426. }
  427. return
  428. }
  429. // TxUpdateSubtitle .
  430. func (d *Dao) TxUpdateSubtitle(tx *sql.Tx, subtitle *model.Subtitle) (err error) {
  431. if _, err = tx.Exec(fmt.Sprintf(_updateSubtitle, d.hitSubtile(subtitle.Oid)), subtitle.UpMid, subtitle.Status,
  432. subtitle.PubTime, subtitle.RejectComment, subtitle.ID); err != nil {
  433. log.Error("params(%+v),error(%v)", subtitle, err)
  434. return
  435. }
  436. return
  437. }
  438. // TxAddSubtitlePub .
  439. func (d *Dao) TxAddSubtitlePub(tx *sql.Tx, subtitlePub *model.SubtitlePub) (err error) {
  440. if _, err = tx.Exec(_addSubtitlePub, subtitlePub.Oid, subtitlePub.Type, subtitlePub.Lan, subtitlePub.SubtitleID, subtitlePub.IsDelete, subtitlePub.SubtitleID, subtitlePub.IsDelete); err != nil {
  441. log.Error("params(%+v),error(%v)", subtitlePub, err)
  442. return
  443. }
  444. return
  445. }
  446. // MaskMids get mask mids from db.
  447. func (d *Dao) MaskMids(c context.Context) (mids []int64, err error) {
  448. mids = make([]int64, 0, 100)
  449. rows, err := d.biliDMWriter.Query(c, _getMaskMids)
  450. if err != nil {
  451. log.Error("biliDMWriter.Query(%s) error(%v)", _getMaskMids, err)
  452. return
  453. }
  454. defer rows.Close()
  455. for rows.Next() {
  456. var mid int64
  457. if err = rows.Scan(&mid); err != nil {
  458. log.Error("biliDMWriter.Scan(%s) error(%v)", _getMaskMids, err)
  459. return
  460. }
  461. mids = append(mids, mid)
  462. }
  463. if err = rows.Err(); err != nil {
  464. log.Error("biliDMWriter.rows.Err() error(%v)", err)
  465. }
  466. return
  467. }