Fix pinned post content truncation with SQLite

This extracts the LEFT/SUBSTR logic into its own datastore.clip() method
that also works correctly with SQLite.

Ref T529
This commit is contained in:
Matt Baer 2018-12-08 12:54:49 -05:00
parent daaa4564bb
commit 026604b3dd
1 changed files with 9 additions and 5 deletions

View File

@ -113,6 +113,13 @@ func (db *datastore) now() string {
return "NOW()"
}
func (db *datastore) clip(field string, l int) string {
if db.driverName == driverSQLite {
return fmt.Sprintf("SUBSTR(%s, 0, %d)", field, l)
}
return fmt.Sprintf("LEFT(%s, %d)", field, l)
}
func (db *datastore) CreateUser(u *User, collectionTitle string) error {
// New users get a `users` and `collections` row.
t, err := db.Begin()
@ -1474,11 +1481,8 @@ func (db *datastore) GetLastPinnedPostPos(collID int64) int64 {
}
func (db *datastore) GetPinnedPosts(coll *CollectionObj) (*[]PublicPost, error) {
clipFunction := "LEFT"
if db.driverName == "sqlite3" {
clipFunction = "SUBSTR"
}
rows, err := db.Query("SELECT id, slug, title, "+clipFunction+"(content, 80), pinned_position FROM posts WHERE collection_id = ? AND pinned_position IS NOT NULL ORDER BY pinned_position ASC", coll.ID)
// FIXME: sqlite-backed instances don't include ellipsis on truncated titles
rows, err := db.Query("SELECT id, slug, title, "+db.clip("content", 80)+", pinned_position FROM posts WHERE collection_id = ? AND pinned_position IS NOT NULL ORDER BY pinned_position ASC", coll.ID)
if err != nil {
log.Error("Failed selecting pinned posts: %v", err)
return nil, impart.HTTPError{http.StatusInternalServerError, "Couldn't retrieve pinned posts."}