mirror of
https://github.com/mattermost/focalboard.git
synced 2025-01-11 18:13:52 +02:00
Merge pull request #18 from mattermost/adding-squirrel-query-builder
Adding squirrel query builder
This commit is contained in:
commit
4ae88bee0b
@ -3,6 +3,7 @@ module github.com/mattermost/mattermost-octo-tasks/server
|
|||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/Masterminds/squirrel v1.4.0
|
||||||
github.com/go-ldap/ldap v3.0.3+incompatible // indirect
|
github.com/go-ldap/ldap v3.0.3+incompatible // indirect
|
||||||
github.com/golang-migrate/migrate v3.5.4+incompatible
|
github.com/golang-migrate/migrate v3.5.4+incompatible
|
||||||
github.com/golang-migrate/migrate/v4 v4.13.0
|
github.com/golang-migrate/migrate/v4 v4.13.0
|
||||||
|
@ -6,29 +6,23 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
sq "github.com/Masterminds/squirrel"
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
"github.com/mattermost/mattermost-octo-tasks/server/model"
|
"github.com/mattermost/mattermost-octo-tasks/server/model"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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})
|
||||||
|
}
|
||||||
|
|
||||||
func (s *SQLStore) GetBlocksWithParentAndType(parentID string, blockType string) ([]model.Block, error) {
|
func (s *SQLStore) GetBlocksWithParentAndType(parentID string, blockType string) ([]model.Block, error) {
|
||||||
query := `WITH latest AS
|
query := s.getQueryBuilder().Select("id", "parent_id", "schema", "type", "title", "COALESCE(\"fields\", '{}')", "create_at", "update_at", "delete_at").
|
||||||
(
|
FromSelect(s.latestsBlocksSubquery(), "latest").
|
||||||
SELECT * FROM
|
Where(sq.Eq{"delete_at": 0}).
|
||||||
(
|
Where(sq.Eq{"parent_id": parentID}).
|
||||||
SELECT
|
Where(sq.Eq{"type": blockType})
|
||||||
*,
|
rows, err := query.Query()
|
||||||
ROW_NUMBER() OVER (PARTITION BY id ORDER BY insert_at DESC) AS rn
|
|
||||||
FROM blocks
|
|
||||||
) a
|
|
||||||
WHERE rn = 1
|
|
||||||
)
|
|
||||||
|
|
||||||
SELECT id, parent_id, schema, type, title, COALESCE("fields", '{}'), create_at, update_at, delete_at
|
|
||||||
FROM latest
|
|
||||||
WHERE delete_at = 0 and parent_id = $1 and type = $2`
|
|
||||||
|
|
||||||
rows, err := s.db.Query(query, parentID, blockType)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf(`getBlocksWithParentAndType ERROR: %v`, err)
|
log.Printf(`getBlocksWithParentAndType ERROR: %v`, err)
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -38,23 +32,12 @@ func (s *SQLStore) GetBlocksWithParentAndType(parentID string, blockType string)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLStore) GetBlocksWithParent(parentID string) ([]model.Block, error) {
|
func (s *SQLStore) GetBlocksWithParent(parentID string) ([]model.Block, error) {
|
||||||
query := `WITH latest AS
|
query := s.getQueryBuilder().Select("id", "parent_id", "schema", "type", "title", "COALESCE(\"fields\", '{}')", "create_at", "update_at", "delete_at").
|
||||||
(
|
FromSelect(s.latestsBlocksSubquery(), "latest").
|
||||||
SELECT * FROM
|
Where(sq.Eq{"delete_at": 0}).
|
||||||
(
|
Where(sq.Eq{"parent_id": parentID})
|
||||||
SELECT
|
|
||||||
*,
|
|
||||||
ROW_NUMBER() OVER (PARTITION BY id ORDER BY insert_at DESC) AS rn
|
|
||||||
FROM blocks
|
|
||||||
) a
|
|
||||||
WHERE rn = 1
|
|
||||||
)
|
|
||||||
|
|
||||||
SELECT id, parent_id, schema, type, title, COALESCE("fields", '{}'), create_at, update_at, delete_at
|
rows, err := query.Query()
|
||||||
FROM latest
|
|
||||||
WHERE delete_at = 0 and parent_id = $1`
|
|
||||||
|
|
||||||
rows, err := s.db.Query(query, parentID)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf(`getBlocksWithParent ERROR: %v`, err)
|
log.Printf(`getBlocksWithParent ERROR: %v`, err)
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -64,23 +47,11 @@ func (s *SQLStore) GetBlocksWithParent(parentID string) ([]model.Block, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLStore) GetBlocksWithType(blockType string) ([]model.Block, error) {
|
func (s *SQLStore) GetBlocksWithType(blockType string) ([]model.Block, error) {
|
||||||
query := `WITH latest AS
|
query := s.getQueryBuilder().Select("id", "parent_id", "schema", "type", "title", "COALESCE(\"fields\", '{}')", "create_at", "update_at", "delete_at").
|
||||||
(
|
FromSelect(s.latestsBlocksSubquery(), "latest").
|
||||||
SELECT * FROM
|
Where(sq.Eq{"delete_at": 0}).
|
||||||
(
|
Where(sq.Eq{"type": blockType})
|
||||||
SELECT
|
rows, err := query.Query()
|
||||||
*,
|
|
||||||
ROW_NUMBER() OVER (PARTITION BY id ORDER BY insert_at DESC) AS rn
|
|
||||||
FROM blocks
|
|
||||||
) a
|
|
||||||
WHERE rn = 1
|
|
||||||
)
|
|
||||||
|
|
||||||
SELECT id, parent_id, schema, type, title, COALESCE("fields", '{}'), create_at, update_at, delete_at
|
|
||||||
FROM latest
|
|
||||||
WHERE delete_at = 0 and type = $1`
|
|
||||||
|
|
||||||
rows, err := s.db.Query(query, blockType)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf(`getBlocksWithParentAndType ERROR: %v`, err)
|
log.Printf(`getBlocksWithParentAndType ERROR: %v`, err)
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -90,25 +61,12 @@ func (s *SQLStore) GetBlocksWithType(blockType string) ([]model.Block, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLStore) GetSubTree(blockID string) ([]model.Block, error) {
|
func (s *SQLStore) GetSubTree(blockID string) ([]model.Block, error) {
|
||||||
query := `WITH latest AS
|
query := s.getQueryBuilder().Select("id", "parent_id", "schema", "type", "title", "COALESCE(\"fields\", '{}')", "create_at", "update_at", "delete_at").
|
||||||
(
|
FromSelect(s.latestsBlocksSubquery(), "latest").
|
||||||
SELECT * FROM
|
Where(sq.Eq{"delete_at": 0}).
|
||||||
(
|
Where(sq.Or{sq.Eq{"id": blockID}, sq.Eq{"parent_id": blockID}})
|
||||||
SELECT
|
|
||||||
*,
|
|
||||||
ROW_NUMBER() OVER (PARTITION BY id ORDER BY insert_at DESC) AS rn
|
|
||||||
FROM blocks
|
|
||||||
) a
|
|
||||||
WHERE rn = 1
|
|
||||||
)
|
|
||||||
|
|
||||||
SELECT id, parent_id, schema, type, title, COALESCE("fields", '{}'), create_at, update_at, delete_at
|
rows, err := query.Query()
|
||||||
FROM latest
|
|
||||||
WHERE delete_at = 0
|
|
||||||
AND (id = $1
|
|
||||||
OR parent_id = $1)`
|
|
||||||
|
|
||||||
rows, err := s.db.Query(query, blockID)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf(`getSubTree ERROR: %v`, err)
|
log.Printf(`getSubTree ERROR: %v`, err)
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -118,23 +76,11 @@ func (s *SQLStore) GetSubTree(blockID string) ([]model.Block, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLStore) GetAllBlocks() ([]model.Block, error) {
|
func (s *SQLStore) GetAllBlocks() ([]model.Block, error) {
|
||||||
query := `WITH latest AS
|
query := s.getQueryBuilder().Select("id", "parent_id", "schema", "type", "title", "COALESCE(\"fields\", '{}')", "create_at", "update_at", "delete_at").
|
||||||
(
|
FromSelect(s.latestsBlocksSubquery(), "latest").
|
||||||
SELECT * FROM
|
Where(sq.Eq{"delete_at": 0})
|
||||||
(
|
|
||||||
SELECT
|
|
||||||
*,
|
|
||||||
ROW_NUMBER() OVER (PARTITION BY id ORDER BY insert_at DESC) AS rn
|
|
||||||
FROM blocks
|
|
||||||
) a
|
|
||||||
WHERE rn = 1
|
|
||||||
)
|
|
||||||
|
|
||||||
SELECT id, parent_id, schema, type, title, COALESCE("fields", '{}'), create_at, update_at, delete_at
|
rows, err := query.Query()
|
||||||
FROM latest
|
|
||||||
WHERE delete_at = 0`
|
|
||||||
|
|
||||||
rows, err := s.db.Query(query)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf(`getAllBlocks ERROR: %v`, err)
|
log.Printf(`getAllBlocks ERROR: %v`, err)
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -181,25 +127,12 @@ func blocksFromRows(rows *sql.Rows) ([]model.Block, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLStore) GetParentID(blockID string) (string, error) {
|
func (s *SQLStore) GetParentID(blockID string) (string, error) {
|
||||||
statement :=
|
query := s.getQueryBuilder().Select("parent_id").
|
||||||
`WITH latest AS
|
FromSelect(s.latestsBlocksSubquery(), "latest").
|
||||||
(
|
Where(sq.Eq{"delete_at": 0}).
|
||||||
SELECT * FROM
|
Where(sq.Eq{"id": blockID})
|
||||||
(
|
|
||||||
SELECT
|
|
||||||
*,
|
|
||||||
ROW_NUMBER() OVER (PARTITION BY id ORDER BY insert_at DESC) AS rn
|
|
||||||
FROM blocks
|
|
||||||
) a
|
|
||||||
WHERE rn = 1
|
|
||||||
)
|
|
||||||
|
|
||||||
SELECT parent_id
|
row := query.QueryRow()
|
||||||
FROM latest
|
|
||||||
WHERE delete_at = 0
|
|
||||||
AND id = $1`
|
|
||||||
|
|
||||||
row := s.db.QueryRow(statement, blockID)
|
|
||||||
|
|
||||||
var parentID string
|
var parentID string
|
||||||
err := row.Scan(&parentID)
|
err := row.Scan(&parentID)
|
||||||
@ -216,29 +149,10 @@ func (s *SQLStore) InsertBlock(block model.Block) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
statement := `INSERT INTO blocks(
|
query := s.getQueryBuilder().Insert("blocks").
|
||||||
id,
|
Columns("id", "parent_id", "schema", "type", "title", "fields", "create_at", "update_at", "delete_at").
|
||||||
parent_id,
|
Values(block.ID, block.ParentID, block.Schema, block.Type, block.Title, fieldsJSON, block.CreateAt, block.UpdateAt, block.DeleteAt)
|
||||||
schema,
|
_, err = query.Exec()
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -247,8 +161,8 @@ func (s *SQLStore) InsertBlock(block model.Block) error {
|
|||||||
|
|
||||||
func (s *SQLStore) DeleteBlock(blockID string) error {
|
func (s *SQLStore) DeleteBlock(blockID string) error {
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
statement := `INSERT INTO blocks(id, update_at, delete_at) VALUES($1, $2, $3)`
|
query := s.getQueryBuilder().Insert("blocks").Columns("id", "update_at", "delete_at").Values(blockID, now, now)
|
||||||
_, err := s.db.Exec(statement, blockID, now, now)
|
_, err := query.Exec()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package sqlstore
|
package sqlstore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/golang-migrate/migrate/v4"
|
"github.com/golang-migrate/migrate/v4"
|
||||||
"github.com/golang-migrate/migrate/v4/database"
|
"github.com/golang-migrate/migrate/v4/database"
|
||||||
"github.com/golang-migrate/migrate/v4/database/postgres"
|
"github.com/golang-migrate/migrate/v4/database/postgres"
|
||||||
@ -15,7 +13,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (s *SQLStore) Migrate() error {
|
func (s *SQLStore) Migrate() error {
|
||||||
fmt.Println("HOLA")
|
|
||||||
var driver database.Driver
|
var driver database.Driver
|
||||||
var err error
|
var err error
|
||||||
var bresource *bindata.AssetSource
|
var bresource *bindata.AssetSource
|
||||||
|
@ -3,6 +3,8 @@ package sqlstore
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
sq "github.com/Masterminds/squirrel"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SQLStore is a SQL database
|
// SQLStore is a SQL database
|
||||||
@ -44,3 +46,8 @@ func New(dbType, connectionString string) (*SQLStore, error) {
|
|||||||
func (s *SQLStore) Shutdown() error {
|
func (s *SQLStore) Shutdown() error {
|
||||||
return s.db.Close()
|
return s.db.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SQLStore) getQueryBuilder() sq.StatementBuilderType {
|
||||||
|
builder := sq.StatementBuilder.PlaceholderFormat(sq.Dollar)
|
||||||
|
return builder.RunWith(s.db)
|
||||||
|
}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
package sqlstore
|
package sqlstore
|
||||||
|
|
||||||
func (s *SQLStore) GetSystemSettings() (map[string]string, error) {
|
func (s *SQLStore) GetSystemSettings() (map[string]string, error) {
|
||||||
query := `SELECT * FROM system_settings`
|
query := s.getQueryBuilder().Select("*").From("system_settings")
|
||||||
|
|
||||||
rows, err := s.db.Query(query)
|
rows, err := query.Query()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -25,9 +25,9 @@ func (s *SQLStore) GetSystemSettings() (map[string]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLStore) SetSystemSetting(id string, value string) error {
|
func (s *SQLStore) SetSystemSetting(id string, value string) error {
|
||||||
query := `INSERT INTO system_settings(id, value) VALUES ($1,$2) ON CONFLICT (id) DO UPDATE SET value=$2`
|
query := s.getQueryBuilder().Insert("system_settings").Columns("id", "value").Values(id, value)
|
||||||
|
|
||||||
_, err := s.db.Exec(query, id, value)
|
_, err := query.Exec()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user