github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/common/activity_stream_matches.go (about)

     1  package common
     2  
     3  import (
     4  	"database/sql"
     5  
     6  	qgen "github.com/Azareal/Gosora/query_gen"
     7  )
     8  
     9  var ActivityMatches ActivityStreamMatches
    10  
    11  type ActivityStreamMatches interface {
    12  	Add(watcher, asid int) error
    13  	Delete(watcher, asid int) error
    14  	DeleteAndCountChanged(watcher, asid int) (int, error)
    15  	CountAsid(asid int) int
    16  }
    17  
    18  type DefaultActivityStreamMatches struct {
    19  	add       *sql.Stmt
    20  	delete    *sql.Stmt
    21  	countAsid *sql.Stmt
    22  }
    23  
    24  func NewDefaultActivityStreamMatches(acc *qgen.Accumulator) (*DefaultActivityStreamMatches, error) {
    25  	asm := "activity_stream_matches"
    26  	return &DefaultActivityStreamMatches{
    27  		add:       acc.Insert(asm).Columns("watcher,asid").Fields("?,?").Prepare(),
    28  		delete:    acc.Delete(asm).Where("watcher=? AND asid=?").Prepare(),
    29  		countAsid: acc.Count(asm).Where("asid=?").Prepare(),
    30  	}, acc.FirstError()
    31  }
    32  
    33  func (s *DefaultActivityStreamMatches) Add(watcher, asid int) error {
    34  	_, e := s.add.Exec(watcher, asid)
    35  	return e
    36  }
    37  
    38  func (s *DefaultActivityStreamMatches) Delete(watcher, asid int) error {
    39  	_, e := s.delete.Exec(watcher, asid)
    40  	return e
    41  }
    42  
    43  func (s *DefaultActivityStreamMatches) DeleteAndCountChanged(watcher, asid int) (int, error) {
    44  	res, e := s.delete.Exec(watcher, asid)
    45  	if e != nil {
    46  		return 0, e
    47  	}
    48  	c64, e := res.RowsAffected()
    49  	return int(c64), e
    50  }
    51  
    52  func (s *DefaultActivityStreamMatches) CountAsid(asid int) int {
    53  	return Countf(s.countAsid, asid)
    54  }