2017-11-10 03:33:11 +00:00
|
|
|
package common
|
2017-11-06 07:44:08 +00:00
|
|
|
|
2019-05-17 08:40:41 +00:00
|
|
|
//import "log"
|
2020-01-02 21:52:41 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
qgen "github.com/Azareal/Gosora/query_gen"
|
|
|
|
)
|
2017-11-06 07:44:08 +00:00
|
|
|
|
2017-11-11 04:06:16 +00:00
|
|
|
var Rstore ReplyStore
|
2017-11-06 07:44:08 +00:00
|
|
|
|
|
|
|
type ReplyStore interface {
|
|
|
|
Get(id int) (*Reply, error)
|
2019-11-12 06:56:51 +00:00
|
|
|
Create(topic *Topic, content string, ip string, uid int) (id int, err error)
|
2019-06-01 12:31:48 +00:00
|
|
|
Count() (count int)
|
2019-05-17 08:40:41 +00:00
|
|
|
|
|
|
|
SetCache(cache ReplyCache)
|
|
|
|
GetCache() ReplyCache
|
2017-11-06 07:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SQLReplyStore struct {
|
2019-05-17 08:40:41 +00:00
|
|
|
cache ReplyCache
|
|
|
|
|
2017-11-06 07:44:08 +00:00
|
|
|
get *sql.Stmt
|
|
|
|
create *sql.Stmt
|
2020-01-02 21:52:41 +00:00
|
|
|
count *sql.Stmt
|
2017-11-06 07:44:08 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 08:40:41 +00:00
|
|
|
func NewSQLReplyStore(acc *qgen.Accumulator, cache ReplyCache) (*SQLReplyStore, error) {
|
|
|
|
if cache == nil {
|
|
|
|
cache = NewNullReplyCache()
|
|
|
|
}
|
2019-11-12 06:56:51 +00:00
|
|
|
re := "replies"
|
2017-11-06 07:44:08 +00:00
|
|
|
return &SQLReplyStore{
|
2019-05-17 08:40:41 +00:00
|
|
|
cache: cache,
|
2019-11-12 06:56:51 +00:00
|
|
|
get: acc.Select(re).Columns("tid, content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress, likeCount, attachCount, actionType").Where("rid = ?").Prepare(),
|
|
|
|
create: acc.Insert(re).Columns("tid, content, parsed_content, createdAt, lastUpdated, ipaddress, words, createdBy").Fields("?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),?,?,?").Prepare(),
|
2020-01-02 21:52:41 +00:00
|
|
|
count: acc.Count(re).Prepare(),
|
2017-11-06 16:24:45 +00:00
|
|
|
}, acc.FirstError()
|
2017-11-06 07:44:08 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 08:40:41 +00:00
|
|
|
func (s *SQLReplyStore) Get(id int) (*Reply, error) {
|
2019-06-01 12:31:48 +00:00
|
|
|
r, err := s.cache.Get(id)
|
2019-05-17 08:40:41 +00:00
|
|
|
if err == nil {
|
2019-06-01 12:31:48 +00:00
|
|
|
return r, nil
|
2019-05-17 08:40:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-01 12:31:48 +00:00
|
|
|
r = &Reply{ID: id}
|
2019-08-31 22:34:43 +00:00
|
|
|
err = s.get.QueryRow(id).Scan(&r.ParentID, &r.Content, &r.CreatedBy, &r.CreatedAt, &r.LastEdit, &r.LastEditBy, &r.IP, &r.LikeCount, &r.AttachCount, &r.ActionType)
|
2019-05-17 08:40:41 +00:00
|
|
|
if err == nil {
|
2019-06-01 12:31:48 +00:00
|
|
|
_ = s.cache.Set(r)
|
2019-05-17 08:40:41 +00:00
|
|
|
}
|
2019-06-01 12:31:48 +00:00
|
|
|
return r, err
|
2017-11-06 07:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Write a test for this
|
2019-08-31 22:34:43 +00:00
|
|
|
func (s *SQLReplyStore) Create(t *Topic, content string, ip string, uid int) (rid int, err error) {
|
2020-01-02 21:52:41 +00:00
|
|
|
if Config.DisablePostIP {
|
|
|
|
ip = "0"
|
|
|
|
}
|
2019-12-08 03:40:56 +00:00
|
|
|
res, err := s.create.Exec(t.ID, content, ParseMessage(content, t.ParentID, "forums", nil), ip, WordCount(content), uid)
|
2017-11-06 07:44:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-11-08 07:28:33 +00:00
|
|
|
|
2017-11-06 07:44:08 +00:00
|
|
|
lastID, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2019-08-31 22:34:43 +00:00
|
|
|
rid = int(lastID)
|
|
|
|
return rid, t.AddReply(rid, uid)
|
2017-11-06 07:44:08 +00:00
|
|
|
}
|
2019-05-17 08:40:41 +00:00
|
|
|
|
2019-06-01 12:31:48 +00:00
|
|
|
// TODO: Write a test for this
|
|
|
|
// Count returns the total number of topic replies on these forums
|
|
|
|
func (s *SQLReplyStore) Count() (count int) {
|
|
|
|
err := s.count.QueryRow().Scan(&count)
|
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2019-05-17 08:40:41 +00:00
|
|
|
func (s *SQLReplyStore) SetCache(cache ReplyCache) {
|
|
|
|
s.cache = cache
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQLReplyStore) GetCache() ReplyCache {
|
|
|
|
return s.cache
|
|
|
|
}
|