document.go 987 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/kvo/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _upDocument = "INSERT INTO document(check_sum,doc,ctime,mtime) VALUES(?,?,?,?) ON DUPLICATE KEY UPDATE doc=?"
  11. _getDocument = "SELECT check_sum,doc FROM document WHERE check_sum=?"
  12. )
  13. // Document get docuemtn
  14. func (d *Dao) Document(ctx context.Context, checkSum int64) (doc *model.Document, err error) {
  15. row := d.getDocument.QueryRow(ctx, checkSum)
  16. doc = &model.Document{}
  17. err = row.Scan(&doc.CheckSum, &doc.Doc)
  18. if err != nil {
  19. if err == sql.ErrNoRows {
  20. doc = nil
  21. err = nil
  22. return
  23. }
  24. log.Error("row.scan err:%v", err)
  25. }
  26. return
  27. }
  28. // TxUpDocuement add a document
  29. func (d *Dao) TxUpDocuement(ctx context.Context, tx *sql.Tx, checkSum int64, data string, now time.Time) (err error) {
  30. _, err = tx.Exec(_upDocument, checkSum, data, now, now, data)
  31. if err != nil {
  32. log.Error("db.exec err:%v", err)
  33. }
  34. return
  35. }