summaryrefslogtreecommitdiffstats
path: root/store/sql_post_store.go
diff options
context:
space:
mode:
Diffstat (limited to 'store/sql_post_store.go')
-rw-r--r--store/sql_post_store.go106
1 files changed, 69 insertions, 37 deletions
diff --git a/store/sql_post_store.go b/store/sql_post_store.go
index 5b9ebfdf2..56c174e4c 100644
--- a/store/sql_post_store.go
+++ b/store/sql_post_store.go
@@ -6,6 +6,7 @@ package store
import (
"fmt"
"github.com/mattermost/platform/model"
+ "github.com/mattermost/platform/utils"
"strconv"
"strings"
)
@@ -43,13 +44,13 @@ func (s SqlPostStore) UpgradeSchemaIfNeeded() {
}
func (s SqlPostStore) CreateIndexesIfNotExists() {
- s.CreateIndexIfNotExists("idx_update_at", "Posts", "UpdateAt")
- s.CreateIndexIfNotExists("idx_create_at", "Posts", "CreateAt")
- s.CreateIndexIfNotExists("idx_channel_id", "Posts", "ChannelId")
- s.CreateIndexIfNotExists("idx_root_id", "Posts", "RootId")
+ s.CreateIndexIfNotExists("idx_posts_update_at", "Posts", "UpdateAt")
+ s.CreateIndexIfNotExists("idx_posts_create_at", "Posts", "CreateAt")
+ s.CreateIndexIfNotExists("idx_posts_channel_id", "Posts", "ChannelId")
+ s.CreateIndexIfNotExists("idx_posts_root_id", "Posts", "RootId")
- s.CreateFullTextIndexIfNotExists("idx_message_txt", "Posts", "Message")
- s.CreateFullTextIndexIfNotExists("idx_hashtags_txt", "Posts", "Hashtags")
+ s.CreateFullTextIndexIfNotExists("idx_posts_message_txt", "Posts", "Message")
+ s.CreateFullTextIndexIfNotExists("idx_posts_hashtags_txt", "Posts", "Hashtags")
}
func (s SqlPostStore) Save(post *model.Post) StoreChannel {
@@ -152,7 +153,7 @@ func (s SqlPostStore) Get(id string) StoreChannel {
pl := &model.PostList{}
var post model.Post
- err := s.GetReplica().SelectOne(&post, "SELECT * FROM Posts WHERE Id = ? AND DeleteAt = 0", id)
+ err := s.GetReplica().SelectOne(&post, "SELECT * FROM Posts WHERE Id = :Id AND DeleteAt = 0", map[string]interface{}{"Id": id})
if err != nil {
result.Err = model.NewAppError("SqlPostStore.GetPost", "We couldn't get the post", "id="+id+err.Error())
}
@@ -175,7 +176,7 @@ func (s SqlPostStore) Get(id string) StoreChannel {
}
var posts []*model.Post
- _, err = s.GetReplica().Select(&posts, "SELECT * FROM Posts WHERE (Id = ? OR RootId = ?) AND DeleteAt = 0", rootId, rootId)
+ _, err = s.GetReplica().Select(&posts, "SELECT * FROM Posts WHERE (Id = :Id OR RootId = :RootId) AND DeleteAt = 0", map[string]interface{}{"Id": rootId, "RootId": rootId})
if err != nil {
result.Err = model.NewAppError("SqlPostStore.GetPost", "We couldn't get the post", "root_id="+rootId+err.Error())
} else {
@@ -205,7 +206,7 @@ func (s SqlPostStore) GetEtag(channelId string) StoreChannel {
result := StoreResult{}
var et etagPosts
- err := s.GetReplica().SelectOne(&et, "SELECT Id, UpdateAt FROM Posts WHERE ChannelId = ? ORDER BY UpdateAt DESC LIMIT 1", channelId)
+ err := s.GetReplica().SelectOne(&et, "SELECT Id, UpdateAt FROM Posts WHERE ChannelId = :ChannelId ORDER BY UpdateAt DESC LIMIT 1", map[string]interface{}{"ChannelId": channelId})
if err != nil {
result.Data = fmt.Sprintf("%v.0.%v", model.ETAG_ROOT_VERSION, model.GetMillis())
} else {
@@ -225,7 +226,7 @@ func (s SqlPostStore) Delete(postId string, time int64) StoreChannel {
go func() {
result := StoreResult{}
- _, err := s.GetMaster().Exec("Update Posts SET DeleteAt = ?, UpdateAt = ? WHERE Id = ? OR ParentId = ? OR RootId = ?", time, time, postId, postId, postId)
+ _, err := s.GetMaster().Exec("Update Posts SET DeleteAt = :DeleteAt, UpdateAt = :UpdateAt WHERE Id = :Id OR ParentId = :ParentId OR RootId = :RootId", map[string]interface{}{"DeleteAt": time, "UpdateAt": time, "Id": postId, "ParentId": postId, "RootId": postId})
if err != nil {
result.Err = model.NewAppError("SqlPostStore.Delete", "We couldn't delete the post", "id="+postId+", err="+err.Error())
}
@@ -305,7 +306,7 @@ func (s SqlPostStore) getRootPosts(channelId string, offset int, limit int) Stor
result := StoreResult{}
var posts []*model.Post
- _, err := s.GetReplica().Select(&posts, "SELECT * FROM Posts WHERE ChannelId = ? AND DeleteAt = 0 ORDER BY CreateAt DESC LIMIT ?,?", channelId, offset, limit)
+ _, err := s.GetReplica().Select(&posts, "SELECT * FROM Posts WHERE ChannelId = :ChannelId AND DeleteAt = 0 ORDER BY CreateAt DESC LIMIT :Limit OFFSET :Offset", map[string]interface{}{"ChannelId": channelId, "Offset": offset, "Limit": limit})
if err != nil {
result.Err = model.NewAppError("SqlPostStore.GetLinearPosts", "We couldn't get the posts for the channel", "channelId="+channelId+err.Error())
} else {
@@ -340,15 +341,15 @@ func (s SqlPostStore) getParentsPosts(channelId string, offset int, limit int) S
FROM
Posts
WHERE
- ChannelId = ?
+ ChannelId = :ChannelId1
AND DeleteAt = 0
ORDER BY CreateAt DESC
- LIMIT ?, ?) q3) q1 ON q1.RootId = q2.RootId
+ LIMIT :Limit OFFSET :Offset) q3) q1 ON q1.RootId = q2.RootId
WHERE
- ChannelId = ?
+ ChannelId = :ChannelId2
AND DeleteAt = 0
ORDER BY CreateAt`,
- channelId, offset, limit, channelId)
+ map[string]interface{}{"ChannelId1": channelId, "Offset": offset, "Limit": limit, "ChannelId2": channelId})
if err != nil {
result.Err = model.NewAppError("SqlPostStore.GetLinearPosts", "We couldn't get the parent post for the channel", "channelId="+channelId+err.Error())
} else {
@@ -382,7 +383,37 @@ func (s SqlPostStore) Search(teamId string, userId string, terms string, isHasht
// cannot escape it so we replace it.
terms = strings.Replace(terms, "@", " ", -1)
- searchQuery := fmt.Sprintf(`SELECT
+ var posts []*model.Post
+
+ if utils.Cfg.SqlSettings.DriverName == "postgres" {
+ searchQuery := fmt.Sprintf(`SELECT
+ *
+ FROM
+ Posts
+ WHERE
+ DeleteAt = 0
+ AND ChannelId IN (SELECT
+ Id
+ FROM
+ Channels,
+ ChannelMembers
+ WHERE
+ Id = ChannelId AND TeamId = $1
+ AND UserId = $2
+ AND DeleteAt = 0)
+ AND %s @@ plainto_tsquery($3)
+ ORDER BY CreateAt DESC
+ LIMIT 100`, searchType)
+
+ terms = strings.Join(strings.Fields(terms), " | ")
+
+ _, err := s.GetReplica().Select(&posts, searchQuery, teamId, userId, terms)
+ if err != nil {
+ result.Err = model.NewAppError("SqlPostStore.Search", "We encounted an error while searching for posts", "teamId="+teamId+", err="+err.Error())
+
+ }
+ } else if utils.Cfg.SqlSettings.DriverName == "mysql" {
+ searchQuery := fmt.Sprintf(`SELECT
*
FROM
Posts
@@ -401,34 +432,35 @@ func (s SqlPostStore) Search(teamId string, userId string, terms string, isHasht
ORDER BY CreateAt DESC
LIMIT 100`, searchType)
- var posts []*model.Post
- _, err := s.GetReplica().Select(&posts, searchQuery, teamId, userId, terms)
- if err != nil {
- result.Err = model.NewAppError("SqlPostStore.Search", "We encounted an error while searching for posts", "teamId="+teamId+", err="+err.Error())
- } else {
+ _, err := s.GetReplica().Select(&posts, searchQuery, teamId, userId, terms)
+ if err != nil {
+ result.Err = model.NewAppError("SqlPostStore.Search", "We encounted an error while searching for posts", "teamId="+teamId+", err="+err.Error())
- list := &model.PostList{Order: make([]string, 0, len(posts))}
+ }
+ }
- for _, p := range posts {
- if searchType == "Hashtags" {
- exactMatch := false
- for _, tag := range strings.Split(p.Hashtags, " ") {
- if termMap[tag] {
- exactMatch = true
- }
- }
- if !exactMatch {
- continue
+ list := &model.PostList{Order: make([]string, 0, len(posts))}
+
+ for _, p := range posts {
+ if searchType == "Hashtags" {
+ exactMatch := false
+ for _, tag := range strings.Split(p.Hashtags, " ") {
+ if termMap[tag] {
+ exactMatch = true
}
}
- list.AddPost(p)
- list.AddOrder(p.Id)
+ if !exactMatch {
+ continue
+ }
}
+ list.AddPost(p)
+ list.AddOrder(p.Id)
+ }
- list.MakeNonNil()
+ list.MakeNonNil()
+
+ result.Data = list
- result.Data = list
- }
storeChannel <- result
close(storeChannel)
}()