focalboard/server/services/store/sqlstore/sqlstore.go
Shivashis Padhi f00b5c9e61
[MM-43781] Feature: boards insights (#3005)
* Add boilerplate functions and handlers for boards insights

* Fix function signatures to add 'duration' parameter, fix where clauses in db queries

* Fix where clause to include boards of which userId in parameter is a member

* Modify queries to work with sqlite, postgres, mysql

* Integration tests, and results of make generate

* Lint Fixes

* Add icons to board insights

* Lint fixes

* Format insights queries without squirrel to fix parameterization issues

* Add tests for sqlstore utility functions

* Improve team insights tests by creating 2 boards

* Refactor endpoints/app to adhere to developments in 7.0 release

* Refactor queries to use squirrel

* Lint fixes

* Fix client, integration tests

* Remove old integration tests

* Add storetests, refactor functions to handle authorized board_ids

* Make queries compatible with mysql, sqlite

* Add app tests

* Fix lint errors

* Revert makefile changes, fix docstring in api

* Lint fixes and doc correction suggested by @wiggin77

* Fix mock store call count error

* adding client code

* Make the following changes

 - use serviceAPI to get user.Timezone
 - rename licenseAndGuestUserCheck to insightPermissionGate, and handle returned error better
 - validate page, perPage parameters aren't < 0

* Lint fix

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local>
2022-08-08 11:42:02 +05:30

159 lines
4.3 KiB
Go

package sqlstore
import (
"database/sql"
"fmt"
"net/url"
sq "github.com/Masterminds/squirrel"
"github.com/mattermost/focalboard/server/model"
"github.com/mattermost/focalboard/server/services/store"
"github.com/mattermost/mattermost-plugin-api/cluster"
mmModel "github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/shared/mlog"
)
// SQLStore is a SQL database.
type SQLStore struct {
db *sql.DB
dbType string
tablePrefix string
connectionString string
isPlugin bool
isSingleUser bool
logger mlog.LoggerIFace
NewMutexFn MutexFactory
servicesAPI servicesAPI
isBinaryParam bool
}
// MutexFactory is used by the store in plugin mode to generate
// a cluster mutex.
type MutexFactory func(name string) (*cluster.Mutex, error)
// New creates a new SQL implementation of the store.
func New(params Params) (*SQLStore, error) {
if err := params.CheckValid(); err != nil {
return nil, err
}
params.Logger.Info("connectDatabase", mlog.String("dbType", params.DBType))
store := &SQLStore{
// TODO: add replica DB support too.
db: params.DB,
dbType: params.DBType,
tablePrefix: params.TablePrefix,
connectionString: params.ConnectionString,
logger: params.Logger,
isPlugin: params.IsPlugin,
isSingleUser: params.IsSingleUser,
NewMutexFn: params.NewMutexFn,
servicesAPI: params.ServicesAPI,
}
var err error
store.isBinaryParam, err = store.computeBinaryParam()
if err != nil {
params.Logger.Error(`Cannot compute binary parameter`, mlog.Err(err))
return nil, err
}
err = store.Migrate()
if err != nil {
params.Logger.Error(`Table creation / migration failed`, mlog.Err(err))
return nil, err
}
return store, nil
}
// computeBinaryParam returns whether the data source uses binary_parameters
// when using Postgres.
func (s *SQLStore) computeBinaryParam() (bool, error) {
if s.dbType != model.PostgresDBType {
return false, nil
}
url, err := url.Parse(s.connectionString)
if err != nil {
return false, err
}
return url.Query().Get("binary_parameters") == "yes", nil
}
// Shutdown close the connection with the store.
func (s *SQLStore) Shutdown() error {
return s.db.Close()
}
// DBHandle returns the raw sql.DB handle.
// It is used by the mattermostauthlayer to run their own
// raw SQL queries.
func (s *SQLStore) DBHandle() *sql.DB {
return s.db
}
// DBType returns the DB driver used for the store.
func (s *SQLStore) DBType() string {
return s.dbType
}
func (s *SQLStore) getQueryBuilder(db sq.BaseRunner) sq.StatementBuilderType {
builder := sq.StatementBuilder
if s.dbType == model.PostgresDBType || s.dbType == model.SqliteDBType {
builder = builder.PlaceholderFormat(sq.Dollar)
}
return builder.RunWith(db)
}
func (s *SQLStore) escapeField(fieldName string) string {
if s.dbType == model.MysqlDBType {
return "`" + fieldName + "`"
}
if s.dbType == model.PostgresDBType || s.dbType == model.SqliteDBType {
return "\"" + fieldName + "\""
}
return fieldName
}
func (s *SQLStore) concatenationSelector(field string, delimiter string) string {
if s.dbType == model.SqliteDBType {
return fmt.Sprintf("group_concat(%s)", field)
}
if s.dbType == model.PostgresDBType {
return fmt.Sprintf("string_agg(%s, '%s')", field, delimiter)
}
if s.dbType == model.MysqlDBType {
return fmt.Sprintf("GROUP_CONCAT(%s SEPARATOR '%s')", field, delimiter)
}
return ""
}
func (s *SQLStore) elementInColumn(column string) string {
if s.dbType == model.SqliteDBType || s.dbType == model.MysqlDBType {
return fmt.Sprintf("instr(%s, ?) > 0", column)
}
if s.dbType == model.PostgresDBType {
return fmt.Sprintf("position(? in %s) > 0", column)
}
return ""
}
func (s *SQLStore) getLicense(db sq.BaseRunner) *mmModel.License {
return nil
}
func (s *SQLStore) getCloudLimits(db sq.BaseRunner) (*mmModel.ProductLimits, error) {
return nil, nil
}
func (s *SQLStore) searchUserChannels(db sq.BaseRunner, teamID, userID, query string) ([]*mmModel.Channel, error) {
return nil, store.NewNotSupportedError("search user channels not supported on standalone mode")
}
func (s *SQLStore) getChannel(db sq.BaseRunner, teamID, channel string) (*mmModel.Channel, error) {
return nil, store.NewNotSupportedError("get channel not supported on standalone mode")
}