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"
|
2017-11-06 07:44:08 +00:00
|
|
|
import "database/sql"
|
2018-10-27 03:21:02 +00:00
|
|
|
import "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)
|
2017-11-08 07:28:33 +00:00
|
|
|
Create(topic *Topic, content string, ipaddress string, uid int) (id int, err error)
|
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
|
|
|
|
}
|
|
|
|
|
2019-05-17 08:40:41 +00:00
|
|
|
func NewSQLReplyStore(acc *qgen.Accumulator, cache ReplyCache) (*SQLReplyStore, error) {
|
|
|
|
if cache == nil {
|
|
|
|
cache = NewNullReplyCache()
|
|
|
|
}
|
2017-11-06 07:44:08 +00:00
|
|
|
return &SQLReplyStore{
|
2019-05-17 08:40:41 +00:00
|
|
|
cache: cache,
|
|
|
|
get: acc.Select("replies").Columns("tid, content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress, likeCount, attachCount, actionType").Where("rid = ?").Prepare(),
|
2017-11-12 03:29:05 +00:00
|
|
|
create: acc.Insert("replies").Columns("tid, content, parsed_content, createdAt, lastUpdated, ipaddress, words, createdBy").Fields("?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),?,?,?").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) {
|
|
|
|
//log.Print("SQLReplyStore.Get")
|
|
|
|
reply, err := s.cache.Get(id)
|
|
|
|
if err == nil {
|
|
|
|
return reply, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
reply = &Reply{ID: id}
|
|
|
|
err = s.get.QueryRow(id).Scan(&reply.ParentID, &reply.Content, &reply.CreatedBy, &reply.CreatedAt, &reply.LastEdit, &reply.LastEditBy, &reply.IPAddress, &reply.LikeCount, &reply.AttachCount, &reply.ActionType)
|
|
|
|
if err == nil {
|
|
|
|
_ = s.cache.Set(reply)
|
|
|
|
}
|
|
|
|
return reply, err
|
2017-11-06 07:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Write a test for this
|
2019-05-17 08:40:41 +00:00
|
|
|
func (s *SQLReplyStore) Create(topic *Topic, content string, ipaddress string, uid int) (id int, err error) {
|
2017-11-11 04:06:16 +00:00
|
|
|
wcount := WordCount(content)
|
2019-05-17 08:40:41 +00:00
|
|
|
res, err := s.create.Exec(topic.ID, content, ParseMessage(content, topic.ParentID, "forums"), ipaddress, wcount, 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
|
|
|
|
}
|
2018-12-27 05:42:41 +00:00
|
|
|
return int(lastID), topic.AddReply(int(lastID), uid)
|
2017-11-06 07:44:08 +00:00
|
|
|
}
|
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
|
|
|
|
}
|