1
0
mirror of https://github.com/mattermost/focalboard.git synced 2024-12-21 13:38:56 +02:00
focalboard/server/services/store/sqlstore/blocks.go

240 lines
6.2 KiB
Go
Raw Normal View History

2020-10-16 19:20:43 +02:00
package sqlstore
2020-10-08 18:21:27 +02:00
import (
"database/sql"
"encoding/json"
"errors"
2020-10-08 18:21:27 +02:00
"log"
"time"
2020-10-20 20:00:46 +02:00
sq "github.com/Masterminds/squirrel"
2020-10-08 18:21:27 +02:00
_ "github.com/lib/pq"
"github.com/mattermost/mattermost-octo-tasks/server/model"
2020-10-08 18:21:27 +02:00
_ "github.com/mattn/go-sqlite3"
)
2020-10-20 20:00:46 +02:00
func (s *SQLStore) latestsBlocksSubquery() sq.SelectBuilder {
internalQuery := sq.Select("*", "ROW_NUMBER() OVER (PARTITION BY id ORDER BY insert_at DESC) AS rn").From("blocks")
2020-11-12 20:16:59 +02:00
return sq.Select("*").
FromSelect(internalQuery, "a").
Where(sq.Eq{"rn": 1}).
Where(sq.Eq{"delete_at": 0})
2020-10-20 20:00:46 +02:00
}
2020-10-08 18:21:27 +02:00
2020-10-20 20:00:46 +02:00
func (s *SQLStore) GetBlocksWithParentAndType(parentID string, blockType string) ([]model.Block, error) {
query := s.getQueryBuilder().
Select("id", "parent_id", "root_id", "schema", "type", "title",
"COALESCE(\"fields\", '{}')", "create_at", "update_at",
"delete_at").
2020-10-20 20:00:46 +02:00
FromSelect(s.latestsBlocksSubquery(), "latest").
Where(sq.Eq{"parent_id": parentID}).
Where(sq.Eq{"type": blockType})
2020-10-20 20:00:46 +02:00
rows, err := query.Query()
2020-10-08 18:21:27 +02:00
if err != nil {
log.Printf(`getBlocksWithParentAndType ERROR: %v`, err)
2020-10-16 16:21:42 +02:00
return nil, err
2020-10-08 18:21:27 +02:00
}
return blocksFromRows(rows)
}
func (s *SQLStore) GetBlocksWithParent(parentID string) ([]model.Block, error) {
query := s.getQueryBuilder().
Select("id", "parent_id", "root_id", "schema", "type", "title",
"COALESCE(\"fields\", '{}')", "create_at", "update_at",
"delete_at").
2020-10-20 20:00:46 +02:00
FromSelect(s.latestsBlocksSubquery(), "latest").
Where(sq.Eq{"parent_id": parentID})
2020-10-08 18:21:27 +02:00
2020-10-20 20:00:46 +02:00
rows, err := query.Query()
2020-10-08 18:21:27 +02:00
if err != nil {
log.Printf(`getBlocksWithParent ERROR: %v`, err)
2020-10-16 16:21:42 +02:00
return nil, err
2020-10-08 18:21:27 +02:00
}
return blocksFromRows(rows)
}
func (s *SQLStore) GetBlocksWithType(blockType string) ([]model.Block, error) {
query := s.getQueryBuilder().
Select("id", "parent_id", "root_id", "schema", "type", "title",
"COALESCE(\"fields\", '{}')", "create_at", "update_at",
"delete_at").
2020-10-20 20:00:46 +02:00
FromSelect(s.latestsBlocksSubquery(), "latest").
Where(sq.Eq{"type": blockType})
2020-10-20 20:00:46 +02:00
rows, err := query.Query()
2020-10-12 20:02:07 +02:00
if err != nil {
log.Printf(`getBlocksWithParentAndType ERROR: %v`, err)
2020-10-16 16:21:42 +02:00
return nil, err
2020-10-12 20:02:07 +02:00
}
return blocksFromRows(rows)
}
2020-11-12 20:16:59 +02:00
// GetSubTree2 returns blocks within 2 levels of the given blockID
func (s *SQLStore) GetSubTree2(blockID string) ([]model.Block, error) {
query := s.getQueryBuilder().
Select("id", "parent_id", "root_id", "schema", "type", "title",
"COALESCE(\"fields\", '{}')", "create_at", "update_at",
"delete_at").
2020-10-20 20:00:46 +02:00
FromSelect(s.latestsBlocksSubquery(), "latest").
Where(sq.Or{sq.Eq{"id": blockID}, sq.Eq{"parent_id": blockID}})
2020-10-08 18:21:27 +02:00
2020-10-20 20:00:46 +02:00
rows, err := query.Query()
2020-10-08 18:21:27 +02:00
if err != nil {
log.Printf(`getSubTree ERROR: %v`, err)
2020-10-16 16:21:42 +02:00
return nil, err
2020-10-08 18:21:27 +02:00
}
return blocksFromRows(rows)
}
2020-11-12 20:16:59 +02:00
// GetSubTree3 returns blocks within 3 levels of the given blockID
func (s *SQLStore) GetSubTree3(blockID string) ([]model.Block, error) {
// This first subquery returns repeated blocks
subquery1 := sq.Select("l3.id", "l3.parent_id", "l3.root_id", "l3.schema", "l3.type", "l3.title",
2020-11-12 20:16:59 +02:00
"l3.fields", "l3.create_at", "l3.update_at",
"l3.delete_at").
FromSelect(s.latestsBlocksSubquery(), "l1").
JoinClause(s.latestsBlocksSubquery().Prefix("JOIN (").Suffix(") l2 on l2.parent_id = l1.id or l2.id = l1.id")).
JoinClause(s.latestsBlocksSubquery().Prefix("JOIN (").Suffix(") l3 on l3.parent_id = l2.id or l3.id = l2.id")).
Where(sq.Eq{"l1.id": blockID})
// This second subquery is used to return distinct blocks
// We can't use DISTINCT because JSON columns in Postgres don't support it, and SQLite doesn't support DISTINCT ON
subquery2 := sq.Select("*", "ROW_NUMBER() OVER (PARTITION BY id) AS rn").
FromSelect(subquery1, "sub1")
query := s.getQueryBuilder().Select("id", "parent_id", "root_id", "schema", "type", "title",
2020-11-12 20:16:59 +02:00
"COALESCE(\"fields\", '{}')", "create_at", "update_at",
"delete_at").
FromSelect(subquery2, "sub2").
Where(sq.Eq{"rn": 1})
rows, err := query.Query()
if err != nil {
log.Printf(`getSubTree3 ERROR: %v`, err)
return nil, err
}
return blocksFromRows(rows)
}
func (s *SQLStore) GetAllBlocks() ([]model.Block, error) {
query := s.getQueryBuilder().
Select("id", "parent_id", "root_id", "schema", "type", "title",
"COALESCE(\"fields\", '{}')", "create_at", "update_at",
"delete_at").
2020-11-12 20:16:59 +02:00
FromSelect(s.latestsBlocksSubquery(), "latest")
2020-10-08 18:21:27 +02:00
2020-10-20 20:00:46 +02:00
rows, err := query.Query()
2020-10-08 18:21:27 +02:00
if err != nil {
log.Printf(`getAllBlocks ERROR: %v`, err)
2020-10-16 16:21:42 +02:00
return nil, err
2020-10-08 18:21:27 +02:00
}
return blocksFromRows(rows)
}
func blocksFromRows(rows *sql.Rows) ([]model.Block, error) {
2020-10-08 18:21:27 +02:00
defer rows.Close()
var results []model.Block
2020-10-08 18:21:27 +02:00
for rows.Next() {
var block model.Block
var fieldsJSON string
err := rows.Scan(
&block.ID,
&block.ParentID,
&block.RootID,
&block.Schema,
&block.Type,
&block.Title,
&fieldsJSON,
&block.CreateAt,
&block.UpdateAt,
&block.DeleteAt)
2020-10-08 18:21:27 +02:00
if err != nil {
// handle this error
log.Printf(`ERROR blocksFromRows: %v`, err)
2020-10-16 16:21:42 +02:00
return nil, err
2020-10-08 18:21:27 +02:00
}
err = json.Unmarshal([]byte(fieldsJSON), &block.Fields)
if err != nil {
// handle this error
log.Printf(`ERROR blocksFromRows fields: %v`, err)
2020-10-16 16:21:42 +02:00
return nil, err
}
results = append(results, block)
2020-10-08 18:21:27 +02:00
}
2020-10-16 16:21:42 +02:00
return results, nil
2020-10-08 18:21:27 +02:00
}
func (s *SQLStore) GetParentID(blockID string) (string, error) {
2020-10-20 20:00:46 +02:00
query := s.getQueryBuilder().Select("parent_id").
FromSelect(s.latestsBlocksSubquery(), "latest").
Where(sq.Eq{"id": blockID})
2020-10-08 18:21:27 +02:00
2020-10-20 20:00:46 +02:00
row := query.QueryRow()
2020-10-08 18:21:27 +02:00
var parentID string
2020-10-08 18:21:27 +02:00
err := row.Scan(&parentID)
if err != nil {
2020-10-16 16:21:42 +02:00
return "", err
2020-10-08 18:21:27 +02:00
}
2020-10-16 16:21:42 +02:00
return parentID, nil
2020-10-08 18:21:27 +02:00
}
func (s *SQLStore) InsertBlock(block model.Block) error {
if block.RootID == "" {
return errors.New("rootId is nil")
}
fieldsJSON, err := json.Marshal(block.Fields)
if err != nil {
2020-10-16 16:21:42 +02:00
return err
}
2020-10-20 20:00:46 +02:00
query := s.getQueryBuilder().Insert("blocks").
Columns("id", "parent_id", "root_id", "schema", "type", "title", "fields", "create_at", "update_at", "delete_at").
Values(block.ID, block.ParentID, block.RootID, block.Schema, block.Type, block.Title,
fieldsJSON, block.CreateAt, block.UpdateAt, block.DeleteAt)
2020-10-20 20:00:46 +02:00
_, err = query.Exec()
2020-10-08 18:21:27 +02:00
if err != nil {
2020-10-16 16:21:42 +02:00
return err
2020-10-08 18:21:27 +02:00
}
2020-10-16 16:21:42 +02:00
return nil
2020-10-08 18:21:27 +02:00
}
func (s *SQLStore) DeleteBlock(blockID string) error {
2020-10-08 18:21:27 +02:00
now := time.Now().Unix()
2020-10-20 20:00:46 +02:00
query := s.getQueryBuilder().Insert("blocks").Columns("id", "update_at", "delete_at").Values(blockID, now, now)
2020-10-20 20:00:46 +02:00
_, err := query.Exec()
2020-10-08 18:21:27 +02:00
if err != nil {
2020-10-16 16:21:42 +02:00
return err
2020-10-08 18:21:27 +02:00
}
2020-10-16 16:21:42 +02:00
return nil
2020-10-08 18:21:27 +02:00
}