From 6f82a8a095017a8fcb66dc2b2a0bc5eaef4a24b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Tue, 20 Oct 2020 20:00:46 +0200 Subject: [PATCH] Adding squirrel query builder --- server/go.mod | 1 + server/services/store/sqlstore/blocks.go | 168 +++++---------------- server/services/store/sqlstore/migrate.go | 3 - server/services/store/sqlstore/sqlstore.go | 7 + server/services/store/sqlstore/system.go | 8 +- 5 files changed, 53 insertions(+), 134 deletions(-) diff --git a/server/go.mod b/server/go.mod index 3f7439b43..577f4dda9 100644 --- a/server/go.mod +++ b/server/go.mod @@ -3,6 +3,7 @@ module github.com/mattermost/mattermost-octo-tasks/server go 1.15 require ( + github.com/Masterminds/squirrel v1.4.0 github.com/go-ldap/ldap v3.0.3+incompatible // indirect github.com/golang-migrate/migrate v3.5.4+incompatible github.com/golang-migrate/migrate/v4 v4.13.0 diff --git a/server/services/store/sqlstore/blocks.go b/server/services/store/sqlstore/blocks.go index 8ff22ca03..19962adb1 100644 --- a/server/services/store/sqlstore/blocks.go +++ b/server/services/store/sqlstore/blocks.go @@ -6,29 +6,23 @@ import ( "log" "time" + sq "github.com/Masterminds/squirrel" _ "github.com/lib/pq" "github.com/mattermost/mattermost-octo-tasks/server/model" _ "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) { - 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 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) + query := s.getQueryBuilder().Select("id", "parent_id", "schema", "type", "title", "COALESCE(\"fields\", '{}')", "create_at", "update_at", "delete_at"). + 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() if err != nil { log.Printf(`getBlocksWithParentAndType ERROR: %v`, 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) { - 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 - ) + query := s.getQueryBuilder().Select("id", "parent_id", "schema", "type", "title", "COALESCE(\"fields\", '{}')", "create_at", "update_at", "delete_at"). + FromSelect(s.latestsBlocksSubquery(), "latest"). + Where(sq.Eq{"delete_at": 0}). + Where(sq.Eq{"parent_id": parentID}) - 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` - - rows, err := s.db.Query(query, parentID) + rows, err := query.Query() if err != nil { log.Printf(`getBlocksWithParent ERROR: %v`, 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) { - 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 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) + query := s.getQueryBuilder().Select("id", "parent_id", "schema", "type", "title", "COALESCE(\"fields\", '{}')", "create_at", "update_at", "delete_at"). + FromSelect(s.latestsBlocksSubquery(), "latest"). + Where(sq.Eq{"delete_at": 0}). + Where(sq.Eq{"type": blockType}) + rows, err := query.Query() if err != nil { log.Printf(`getBlocksWithParentAndType ERROR: %v`, 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) { - 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 - ) + query := s.getQueryBuilder().Select("id", "parent_id", "schema", "type", "title", "COALESCE(\"fields\", '{}')", "create_at", "update_at", "delete_at"). + FromSelect(s.latestsBlocksSubquery(), "latest"). + Where(sq.Eq{"delete_at": 0}). + Where(sq.Or{sq.Eq{"id": blockID}, sq.Eq{"parent_id": blockID}}) - SELECT id, parent_id, schema, type, title, COALESCE("fields", '{}'), create_at, update_at, delete_at - FROM latest - WHERE delete_at = 0 - AND (id = $1 - OR parent_id = $1)` - - rows, err := s.db.Query(query, blockID) + rows, err := query.Query() if err != nil { log.Printf(`getSubTree ERROR: %v`, err) return nil, err @@ -118,23 +76,11 @@ func (s *SQLStore) GetSubTree(blockID string) ([]model.Block, error) { } func (s *SQLStore) GetAllBlocks() ([]model.Block, error) { - 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 - ) + query := s.getQueryBuilder().Select("id", "parent_id", "schema", "type", "title", "COALESCE(\"fields\", '{}')", "create_at", "update_at", "delete_at"). + FromSelect(s.latestsBlocksSubquery(), "latest"). + Where(sq.Eq{"delete_at": 0}) - SELECT id, parent_id, schema, type, title, COALESCE("fields", '{}'), create_at, update_at, delete_at - FROM latest - WHERE delete_at = 0` - - rows, err := s.db.Query(query) + rows, err := query.Query() if err != nil { log.Printf(`getAllBlocks ERROR: %v`, err) return nil, err @@ -181,25 +127,12 @@ func blocksFromRows(rows *sql.Rows) ([]model.Block, error) { } func (s *SQLStore) GetParentID(blockID string) (string, error) { - 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 - ) + query := s.getQueryBuilder().Select("parent_id"). + FromSelect(s.latestsBlocksSubquery(), "latest"). + Where(sq.Eq{"delete_at": 0}). + Where(sq.Eq{"id": blockID}) - SELECT parent_id - FROM latest - WHERE delete_at = 0 - AND id = $1` - - row := s.db.QueryRow(statement, blockID) + row := query.QueryRow() var parentID string err := row.Scan(&parentID) @@ -216,29 +149,10 @@ func (s *SQLStore) InsertBlock(block model.Block) error { return err } - 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) + 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() if err != nil { return err } @@ -247,8 +161,8 @@ func (s *SQLStore) InsertBlock(block model.Block) error { func (s *SQLStore) DeleteBlock(blockID string) error { now := time.Now().Unix() - statement := `INSERT INTO blocks(id, update_at, delete_at) VALUES($1, $2, $3)` - _, err := s.db.Exec(statement, blockID, now, now) + query := s.getQueryBuilder().Insert("blocks").Columns("id", "update_at", "delete_at").Values(blockID, now, now) + _, err := query.Exec() if err != nil { return err } diff --git a/server/services/store/sqlstore/migrate.go b/server/services/store/sqlstore/migrate.go index 2102b0b15..7dbec692b 100644 --- a/server/services/store/sqlstore/migrate.go +++ b/server/services/store/sqlstore/migrate.go @@ -1,8 +1,6 @@ package sqlstore import ( - "fmt" - "github.com/golang-migrate/migrate/v4" "github.com/golang-migrate/migrate/v4/database" "github.com/golang-migrate/migrate/v4/database/postgres" @@ -15,7 +13,6 @@ import ( ) func (s *SQLStore) Migrate() error { - fmt.Println("HOLA") var driver database.Driver var err error var bresource *bindata.AssetSource diff --git a/server/services/store/sqlstore/sqlstore.go b/server/services/store/sqlstore/sqlstore.go index 9c6715212..940bff952 100644 --- a/server/services/store/sqlstore/sqlstore.go +++ b/server/services/store/sqlstore/sqlstore.go @@ -3,6 +3,8 @@ package sqlstore import ( "database/sql" "log" + + sq "github.com/Masterminds/squirrel" ) // SQLStore is a SQL database @@ -44,3 +46,8 @@ func New(dbType, connectionString string) (*SQLStore, error) { func (s *SQLStore) Shutdown() error { return s.db.Close() } + +func (s *SQLStore) getQueryBuilder() sq.StatementBuilderType { + builder := sq.StatementBuilder.PlaceholderFormat(sq.Dollar) + return builder.RunWith(s.db) +} diff --git a/server/services/store/sqlstore/system.go b/server/services/store/sqlstore/system.go index 41c3cbfe5..b6d1825e1 100644 --- a/server/services/store/sqlstore/system.go +++ b/server/services/store/sqlstore/system.go @@ -1,9 +1,9 @@ package sqlstore 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 { return nil, err } @@ -25,9 +25,9 @@ func (s *SQLStore) GetSystemSettings() (map[string]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 { return err }