2020-10-16 19:20:43 +02:00
|
|
|
package sqlstore
|
2020-10-08 18:21:27 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-10-15 23:56:15 +02:00
|
|
|
"encoding/json"
|
2020-10-08 18:21:27 +02:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
_ "github.com/lib/pq"
|
2020-10-16 19:12:53 +02:00
|
|
|
"github.com/mattermost/mattermost-octo-tasks/server/model"
|
2020-10-08 18:21:27 +02:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
2020-10-14 21:51:37 +02:00
|
|
|
// SQLStore is a SQL database
|
2020-10-09 10:08:38 +02:00
|
|
|
type SQLStore struct {
|
|
|
|
db *sql.DB
|
|
|
|
dbType string
|
|
|
|
}
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2020-10-14 21:51:37 +02:00
|
|
|
// NewSQLStore creates a new SQLStore
|
2020-10-09 10:08:38 +02:00
|
|
|
func NewSQLStore(dbType, connectionString string) (*SQLStore, error) {
|
2020-10-08 18:21:27 +02:00
|
|
|
log.Println("connectDatabase")
|
|
|
|
var err error
|
|
|
|
|
2020-10-09 10:08:38 +02:00
|
|
|
db, err := sql.Open(dbType, connectionString)
|
2020-10-08 18:21:27 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("connectDatabase: ", err)
|
2020-10-09 10:08:38 +02:00
|
|
|
return nil, err
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Ping()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(`Database Ping failed`)
|
2020-10-09 10:08:38 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
store := &SQLStore{
|
|
|
|
db: db,
|
|
|
|
dbType: dbType,
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 10:08:38 +02:00
|
|
|
err = store.createTablesIfNotExists()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(`Table creation failed`)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return store, nil
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 10:08:38 +02:00
|
|
|
func (s *SQLStore) createTablesIfNotExists() error {
|
2020-10-08 18:21:27 +02:00
|
|
|
// TODO: Add update_by with the user's ID
|
|
|
|
// TODO: Consolidate insert_at and update_at, decide if the server of DB should set it
|
|
|
|
var query string
|
2020-10-09 10:08:38 +02:00
|
|
|
if s.dbType == "sqlite3" {
|
2020-10-08 18:21:27 +02:00
|
|
|
query = `CREATE TABLE IF NOT EXISTS blocks (
|
|
|
|
id VARCHAR(36),
|
2020-10-15 21:10:04 +02:00
|
|
|
insert_at DATETIME NOT NULL DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')),
|
2020-10-08 18:21:27 +02:00
|
|
|
parent_id VARCHAR(36),
|
2020-10-15 23:56:15 +02:00
|
|
|
schema BIGINT,
|
2020-10-08 18:21:27 +02:00
|
|
|
type TEXT,
|
2020-10-15 23:56:15 +02:00
|
|
|
title TEXT,
|
|
|
|
fields TEXT,
|
2020-10-08 18:21:27 +02:00
|
|
|
create_at BIGINT,
|
|
|
|
update_at BIGINT,
|
|
|
|
delete_at BIGINT,
|
|
|
|
PRIMARY KEY (id, insert_at)
|
|
|
|
);`
|
|
|
|
} else {
|
|
|
|
query = `CREATE TABLE IF NOT EXISTS blocks (
|
|
|
|
id VARCHAR(36),
|
|
|
|
insert_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
|
|
parent_id VARCHAR(36),
|
2020-10-15 23:56:15 +02:00
|
|
|
schema BIGINT,
|
2020-10-08 18:21:27 +02:00
|
|
|
type TEXT,
|
2020-10-15 23:56:15 +02:00
|
|
|
title TEXT,
|
|
|
|
fields TEXT,
|
2020-10-08 18:21:27 +02:00
|
|
|
create_at BIGINT,
|
|
|
|
update_at BIGINT,
|
|
|
|
delete_at BIGINT,
|
|
|
|
PRIMARY KEY (id, insert_at)
|
|
|
|
);`
|
|
|
|
}
|
|
|
|
|
2020-10-09 10:08:38 +02:00
|
|
|
_, err := s.db.Exec(query)
|
2020-10-08 18:21:27 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("createTablesIfNotExists: ", err)
|
2020-10-09 10:08:38 +02:00
|
|
|
return err
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
2020-10-09 10:08:38 +02:00
|
|
|
log.Printf("createTablesIfNotExists(%s)", s.dbType)
|
|
|
|
return nil
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
|
|
|
|
2020-10-16 19:12:53 +02:00
|
|
|
func (s *SQLStore) GetBlocksWithParentAndType(parentID string, blockType string) ([]model.Block, error) {
|
2020-10-08 18:21:27 +02:00
|
|
|
query := `WITH latest AS
|
|
|
|
(
|
|
|
|
SELECT * FROM
|
|
|
|
(
|
|
|
|
SELECT
|
|
|
|
*,
|
|
|
|
ROW_NUMBER() OVER (PARTITION BY id ORDER BY insert_at DESC) AS rn
|
|
|
|
FROM blocks
|
|
|
|
) a
|
|
|
|
WHERE rn = 1
|
|
|
|
)
|
|
|
|
|
2020-10-15 23:56:15 +02:00
|
|
|
SELECT id, parent_id, schema, type, title, COALESCE("fields", '{}'), create_at, update_at, delete_at
|
2020-10-08 18:21:27 +02:00
|
|
|
FROM latest
|
|
|
|
WHERE delete_at = 0 and parent_id = $1 and type = $2`
|
|
|
|
|
2020-10-09 10:08:38 +02:00
|
|
|
rows, err := s.db.Query(query, parentID, blockType)
|
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)
|
|
|
|
}
|
|
|
|
|
2020-10-16 19:12:53 +02:00
|
|
|
func (s *SQLStore) GetBlocksWithParent(parentID string) ([]model.Block, error) {
|
2020-10-08 18:21:27 +02:00
|
|
|
query := `WITH latest AS
|
|
|
|
(
|
|
|
|
SELECT * FROM
|
|
|
|
(
|
|
|
|
SELECT
|
|
|
|
*,
|
|
|
|
ROW_NUMBER() OVER (PARTITION BY id ORDER BY insert_at DESC) AS rn
|
|
|
|
FROM blocks
|
|
|
|
) a
|
|
|
|
WHERE rn = 1
|
|
|
|
)
|
|
|
|
|
2020-10-15 23:56:15 +02:00
|
|
|
SELECT id, parent_id, schema, type, title, COALESCE("fields", '{}'), create_at, update_at, delete_at
|
2020-10-08 18:21:27 +02:00
|
|
|
FROM latest
|
|
|
|
WHERE delete_at = 0 and parent_id = $1`
|
|
|
|
|
2020-10-09 10:08:38 +02:00
|
|
|
rows, err := s.db.Query(query, parentID)
|
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)
|
|
|
|
}
|
|
|
|
|
2020-10-16 19:12:53 +02:00
|
|
|
func (s *SQLStore) GetBlocksWithType(blockType string) ([]model.Block, error) {
|
2020-10-12 20:02:07 +02:00
|
|
|
query := `WITH latest AS
|
|
|
|
(
|
|
|
|
SELECT * FROM
|
|
|
|
(
|
|
|
|
SELECT
|
|
|
|
*,
|
|
|
|
ROW_NUMBER() OVER (PARTITION BY id ORDER BY insert_at DESC) AS rn
|
|
|
|
FROM blocks
|
|
|
|
) a
|
|
|
|
WHERE rn = 1
|
|
|
|
)
|
|
|
|
|
2020-10-15 23:56:15 +02:00
|
|
|
SELECT id, parent_id, schema, type, title, COALESCE("fields", '{}'), create_at, update_at, delete_at
|
2020-10-12 20:02:07 +02:00
|
|
|
FROM latest
|
|
|
|
WHERE delete_at = 0 and type = $1`
|
|
|
|
|
2020-10-14 21:51:37 +02:00
|
|
|
rows, err := s.db.Query(query, blockType)
|
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-10-16 19:12:53 +02:00
|
|
|
func (s *SQLStore) GetSubTree(blockID string) ([]model.Block, error) {
|
2020-10-08 18:21:27 +02:00
|
|
|
query := `WITH latest AS
|
|
|
|
(
|
|
|
|
SELECT * FROM
|
|
|
|
(
|
|
|
|
SELECT
|
|
|
|
*,
|
|
|
|
ROW_NUMBER() OVER (PARTITION BY id ORDER BY insert_at DESC) AS rn
|
|
|
|
FROM blocks
|
|
|
|
) a
|
|
|
|
WHERE rn = 1
|
|
|
|
)
|
|
|
|
|
2020-10-15 23:56:15 +02:00
|
|
|
SELECT id, parent_id, schema, type, title, COALESCE("fields", '{}'), create_at, update_at, delete_at
|
2020-10-08 18:21:27 +02:00
|
|
|
FROM latest
|
|
|
|
WHERE delete_at = 0
|
|
|
|
AND (id = $1
|
|
|
|
OR parent_id = $1)`
|
|
|
|
|
2020-10-09 10:08:38 +02:00
|
|
|
rows, err := s.db.Query(query, blockID)
|
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-10-16 19:12:53 +02:00
|
|
|
func (s *SQLStore) GetAllBlocks() ([]model.Block, error) {
|
2020-10-08 18:21:27 +02:00
|
|
|
query := `WITH latest AS
|
|
|
|
(
|
|
|
|
SELECT * FROM
|
|
|
|
(
|
|
|
|
SELECT
|
|
|
|
*,
|
|
|
|
ROW_NUMBER() OVER (PARTITION BY id ORDER BY insert_at DESC) AS rn
|
|
|
|
FROM blocks
|
|
|
|
) a
|
|
|
|
WHERE rn = 1
|
|
|
|
)
|
|
|
|
|
2020-10-15 23:56:15 +02:00
|
|
|
SELECT id, parent_id, schema, type, title, COALESCE("fields", '{}'), create_at, update_at, delete_at
|
2020-10-08 18:21:27 +02:00
|
|
|
FROM latest
|
|
|
|
WHERE delete_at = 0`
|
|
|
|
|
2020-10-09 10:08:38 +02:00
|
|
|
rows, err := s.db.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)
|
|
|
|
}
|
|
|
|
|
2020-10-16 19:12:53 +02:00
|
|
|
func blocksFromRows(rows *sql.Rows) ([]model.Block, error) {
|
2020-10-08 18:21:27 +02:00
|
|
|
defer rows.Close()
|
|
|
|
|
2020-10-16 19:12:53 +02:00
|
|
|
var results []model.Block
|
2020-10-08 18:21:27 +02:00
|
|
|
|
|
|
|
for rows.Next() {
|
2020-10-16 19:12:53 +02:00
|
|
|
var block model.Block
|
2020-10-15 23:56:15 +02:00
|
|
|
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
|
2020-10-15 23:56:15 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-15 23:56:15 +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
|
2020-10-15 23:56:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-16 19:12:53 +02:00
|
|
|
func (s *SQLStore) GetParentID(blockID string) (string, error) {
|
2020-10-08 18:21:27 +02:00
|
|
|
statement :=
|
|
|
|
`WITH latest AS
|
|
|
|
(
|
|
|
|
SELECT * FROM
|
|
|
|
(
|
|
|
|
SELECT
|
|
|
|
*,
|
|
|
|
ROW_NUMBER() OVER (PARTITION BY id ORDER BY insert_at DESC) AS rn
|
|
|
|
FROM blocks
|
|
|
|
) a
|
|
|
|
WHERE rn = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
SELECT parent_id
|
|
|
|
FROM latest
|
|
|
|
WHERE delete_at = 0
|
|
|
|
AND id = $1`
|
|
|
|
|
2020-10-09 10:08:38 +02:00
|
|
|
row := s.db.QueryRow(statement, blockID)
|
2020-10-08 18:21:27 +02:00
|
|
|
|
|
|
|
var parentID string
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-16 19:12:53 +02:00
|
|
|
func (s *SQLStore) InsertBlock(block model.Block) error {
|
2020-10-15 23:56:15 +02:00
|
|
|
fieldsJSON, err := json.Marshal(block.Fields)
|
|
|
|
if err != nil {
|
2020-10-16 16:21:42 +02:00
|
|
|
return err
|
2020-10-15 23:56:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
statement := `INSERT INTO blocks(
|
|
|
|
id,
|
|
|
|
parent_id,
|
|
|
|
schema,
|
|
|
|
type,
|
|
|
|
title,
|
|
|
|
fields,
|
|
|
|
create_at,
|
|
|
|
update_at,
|
|
|
|
delete_at
|
|
|
|
)
|
|
|
|
VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9)`
|
|
|
|
_, err = s.db.Exec(
|
|
|
|
statement,
|
|
|
|
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 {
|
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
|
|
|
}
|
|
|
|
|
2020-10-16 19:12:53 +02:00
|
|
|
func (s *SQLStore) DeleteBlock(blockID string) error {
|
2020-10-08 18:21:27 +02:00
|
|
|
now := time.Now().Unix()
|
2020-10-16 04:21:44 +02:00
|
|
|
statement := `INSERT INTO blocks(id, update_at, delete_at) VALUES($1, $2, $3)`
|
|
|
|
_, err := s.db.Exec(statement, blockID, now, now)
|
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
|
|
|
}
|