2020-10-08 18:21:27 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
// Block is the basic data unit
|
|
|
|
type Block struct {
|
2020-10-13 20:16:44 +02:00
|
|
|
ID string `json:"id"`
|
|
|
|
ParentID string `json:"parentId"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Properties map[string]interface{} `json:"properties"`
|
|
|
|
CreateAt int64 `json:"createAt"`
|
|
|
|
UpdateAt int64 `json:"updateAt"`
|
|
|
|
DeleteAt int64 `json:"deleteAt"`
|
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),
|
|
|
|
insert_at DATETIME NOT NULL DEFAULT current_timestamp,
|
|
|
|
parent_id VARCHAR(36),
|
|
|
|
type TEXT,
|
2020-10-13 20:16:44 +02:00
|
|
|
title TEXT,
|
|
|
|
properties 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),
|
|
|
|
type TEXT,
|
2020-10-13 20:16:44 +02:00
|
|
|
title TEXT,
|
|
|
|
properties 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-09 10:08:38 +02:00
|
|
|
func (s *SQLStore) getBlocksWithParentAndType(parentID string, blockType string) []string {
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
SELECT COALESCE("json", '{}')
|
|
|
|
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)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return blocksFromRows(rows)
|
|
|
|
}
|
|
|
|
|
2020-10-09 10:08:38 +02:00
|
|
|
func (s *SQLStore) getBlocksWithParent(parentID string) []string {
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
SELECT COALESCE("json", '{}')
|
|
|
|
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)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return blocksFromRows(rows)
|
|
|
|
}
|
|
|
|
|
2020-10-14 21:51:25 +02:00
|
|
|
func (s *SQLStore) getBlocksWithType(blockID string) []string {
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
SELECT COALESCE("json", '{}')
|
|
|
|
FROM latest
|
|
|
|
WHERE delete_at = 0 and type = $1`
|
|
|
|
|
|
|
|
rows, err := db.Query(query, blockType)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(`getBlocksWithParentAndType ERROR: %v`, err)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return blocksFromRows(rows)
|
|
|
|
}
|
|
|
|
|
2020-10-09 10:08:38 +02:00
|
|
|
func (s *SQLStore) getSubTree(blockID string) []string {
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
SELECT COALESCE("json", '{}')
|
|
|
|
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)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return blocksFromRows(rows)
|
|
|
|
}
|
|
|
|
|
2020-10-09 10:08:38 +02:00
|
|
|
func (s *SQLStore) getAllBlocks() []string {
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
SELECT COALESCE("json", '{}')
|
|
|
|
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)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return blocksFromRows(rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
func blocksFromRows(rows *sql.Rows) []string {
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var results []string
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var json string
|
|
|
|
err := rows.Scan(&json)
|
|
|
|
if err != nil {
|
|
|
|
// handle this error
|
|
|
|
log.Printf(`blocksFromRows ERROR: %v`, err)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
results = append(results, json)
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2020-10-09 10:08:38 +02:00
|
|
|
func (s *SQLStore) getParentID(blockID string) string {
|
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 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return parentID
|
|
|
|
}
|
|
|
|
|
2020-10-09 10:08:38 +02:00
|
|
|
func (s *SQLStore) insertBlock(block Block, json string) {
|
2020-10-08 18:21:27 +02:00
|
|
|
statement := `INSERT INTO blocks(id, parent_id, type, json, create_at, update_at, delete_at) VALUES($1, $2, $3, $4, $5, $6, $7)`
|
2020-10-09 10:08:38 +02:00
|
|
|
_, err := s.db.Exec(statement, block.ID, block.ParentID, block.Type, json, block.CreateAt, block.UpdateAt, block.DeleteAt)
|
2020-10-08 18:21:27 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 10:08:38 +02:00
|
|
|
func (s *SQLStore) deleteBlock(blockID string) {
|
2020-10-08 18:21:27 +02:00
|
|
|
now := time.Now().Unix()
|
|
|
|
json := fmt.Sprintf(`{"id":"%s","updateAt":%d,"deleteAt":%d}`, blockID, now, now)
|
|
|
|
statement := `INSERT INTO blocks(id, json, update_at, delete_at) VALUES($1, $2, $3, $4)`
|
2020-10-09 10:08:38 +02:00
|
|
|
_, err := s.db.Exec(statement, blockID, json, now, now)
|
2020-10-08 18:21:27 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|