2020-10-16 22:26:47 +02:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-10-20 20:00:46 +02:00
|
|
|
|
|
|
|
sq "github.com/Masterminds/squirrel"
|
2021-08-25 22:08:01 +02:00
|
|
|
|
2021-11-11 18:01:43 +02:00
|
|
|
"github.com/mattermost/mattermost-plugin-api/cluster"
|
2022-02-02 20:25:06 +02:00
|
|
|
|
2021-08-25 22:08:01 +02:00
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
2020-10-16 22:26:47 +02:00
|
|
|
)
|
|
|
|
|
2021-04-22 22:53:01 +02:00
|
|
|
const (
|
|
|
|
mysqlDBType = "mysql"
|
|
|
|
sqliteDBType = "sqlite3"
|
|
|
|
postgresDBType = "postgres"
|
|
|
|
)
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// SQLStore is a SQL database.
|
2020-10-16 22:26:47 +02:00
|
|
|
type SQLStore struct {
|
2021-05-29 08:23:10 +02:00
|
|
|
db *sql.DB
|
|
|
|
dbType string
|
|
|
|
tablePrefix string
|
2021-05-24 19:06:11 +02:00
|
|
|
connectionString string
|
2021-09-08 06:52:03 +02:00
|
|
|
isPlugin bool
|
2021-05-29 08:23:10 +02:00
|
|
|
logger *mlog.Logger
|
2021-11-11 18:01:43 +02:00
|
|
|
NewMutexFn MutexFactory
|
2020-10-16 22:26:47 +02:00
|
|
|
}
|
|
|
|
|
2021-11-11 18:01:43 +02:00
|
|
|
// MutexFactory is used by the store in plugin mode to generate
|
|
|
|
// a cluster mutex.
|
|
|
|
type MutexFactory func(name string) (*cluster.Mutex, error)
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// New creates a new SQL implementation of the store.
|
2021-11-11 18:01:43 +02:00
|
|
|
func New(params Params) (*SQLStore, error) {
|
|
|
|
if err := params.CheckValid(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
params.Logger.Info("connectDatabase", mlog.String("dbType", params.DBType))
|
2020-10-16 22:26:47 +02:00
|
|
|
store := &SQLStore{
|
2021-06-25 16:49:06 +02:00
|
|
|
// TODO: add replica DB support too.
|
2021-11-11 18:01:43 +02:00
|
|
|
db: params.DB,
|
|
|
|
dbType: params.DBType,
|
|
|
|
tablePrefix: params.TablePrefix,
|
|
|
|
connectionString: params.ConnectionString,
|
|
|
|
logger: params.Logger,
|
|
|
|
isPlugin: params.IsPlugin,
|
|
|
|
NewMutexFn: params.NewMutexFn,
|
2020-10-16 22:26:47 +02:00
|
|
|
}
|
|
|
|
|
2021-06-25 16:49:06 +02:00
|
|
|
err := store.Migrate()
|
2020-10-16 22:26:47 +02:00
|
|
|
if err != nil {
|
2021-11-11 18:01:43 +02:00
|
|
|
params.Logger.Error(`Table creation / migration failed`, mlog.Err(err))
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-16 22:26:47 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return store, nil
|
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// Shutdown close the connection with the store.
|
2020-10-16 22:26:47 +02:00
|
|
|
func (s *SQLStore) Shutdown() error {
|
|
|
|
return s.db.Close()
|
|
|
|
}
|
2020-10-20 20:00:46 +02:00
|
|
|
|
2021-06-25 16:49:06 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-02-22 23:30:47 +02:00
|
|
|
// DBType returns the DB driver used for the store.
|
|
|
|
func (s *SQLStore) DBType() string {
|
|
|
|
return s.dbType
|
|
|
|
}
|
|
|
|
|
2021-10-22 12:48:53 +02:00
|
|
|
func (s *SQLStore) getQueryBuilder(db sq.BaseRunner) sq.StatementBuilderType {
|
2021-04-22 22:53:01 +02:00
|
|
|
builder := sq.StatementBuilder
|
|
|
|
if s.dbType == postgresDBType || s.dbType == sqliteDBType {
|
|
|
|
builder = builder.PlaceholderFormat(sq.Dollar)
|
|
|
|
}
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2021-10-22 12:48:53 +02:00
|
|
|
return builder.RunWith(db)
|
2020-10-20 20:00:46 +02:00
|
|
|
}
|
2021-04-22 22:53:01 +02:00
|
|
|
|
2021-07-09 03:09:02 +02:00
|
|
|
func (s *SQLStore) escapeField(fieldName string) string { //nolint:unparam
|
2021-04-22 22:53:01 +02:00
|
|
|
if s.dbType == mysqlDBType {
|
|
|
|
return "`" + fieldName + "`"
|
|
|
|
}
|
|
|
|
if s.dbType == postgresDBType || s.dbType == sqliteDBType {
|
|
|
|
return "\"" + fieldName + "\""
|
|
|
|
}
|
|
|
|
return fieldName
|
|
|
|
}
|