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"
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"
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-20 20:00:46 +02:00
func ( s * SQLStore ) latestsBlocksSubquery ( ) sq . SelectBuilder {
return sq . Select ( "*" ) . FromSelect ( sq . Select ( "*" , "ROW_NUMBER() OVER (PARTITION BY id ORDER BY insert_at DESC) AS rn" ) . From ( "blocks" ) , "a" ) . Where ( sq . Eq { "rn" : 1 } )
}
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 ) {
2020-10-22 13:34:42 +02:00
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 } )
rows , err := query . Query ( )
2020-10-08 18:21:27 +02:00
if err != nil {
log . Printf ( ` getBlocksWithParentAndType ERROR: %v ` , err )
2020-10-22 13:34:42 +02:00
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-22 13:34:42 +02:00
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 )
}
2020-10-16 19:12:53 +02:00
func ( s * SQLStore ) GetBlocksWithType ( blockType string ) ( [ ] model . Block , error ) {
2020-10-22 13:34:42 +02:00
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 } )
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-10-16 19:12:53 +02:00
func ( s * SQLStore ) GetSubTree ( blockID string ) ( [ ] model . Block , error ) {
2020-10-22 13:34:42 +02:00
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 )
}
2020-10-16 19:12:53 +02:00
func ( s * SQLStore ) GetAllBlocks ( ) ( [ ] model . Block , error ) {
2020-10-22 13:34:42 +02:00
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 )
}
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
2020-10-22 13:34:42 +02:00
2020-10-15 23:56:15 +02:00
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-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
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
}
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 )
_ , 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
}
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-20 20:00:46 +02:00
query := s . getQueryBuilder ( ) . Insert ( "blocks" ) . Columns ( "id" , "update_at" , "delete_at" ) . Values ( blockID , now , now )
_ , 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
}