1
0
mirror of https://github.com/mattermost/focalboard.git synced 2024-12-18 09:35:25 +02:00
focalboard/server/services/store/sqlstore/blocks.go

204 lines
4.8 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"
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")
return sq.Select("*").FromSelect(internalQuery, "a").Where(sq.Eq{"rn": 1})
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", "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{"delete_at": 0}).
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", "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{"delete_at": 0}).
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", "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{"delete_at": 0}).
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)
}
func (s *SQLStore) GetSubTree(blockID string) ([]model.Block, error) {
query := s.getQueryBuilder().
Select("id", "parent_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{"delete_at": 0}).
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)
}
func (s *SQLStore) GetAllBlocks() ([]model.Block, error) {
query := s.getQueryBuilder().
Select("id", "parent_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{"delete_at": 0})
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.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{"delete_at": 0}).
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 {
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", "schema", "type", "title", "fields", "create_at", "update_at", "delete_at").
Values(block.ID, block.ParentID, 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
}