package sqlstore import ( "database/sql" "github.com/mattermost/mattermost-server/v6/plugin" sq "github.com/Masterminds/squirrel" "github.com/mattermost/focalboard/server/model" "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 logger *mlog.Logger NewMutexFn MutexFactory pluginAPI *plugin.API } // 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, NewMutexFn: params.NewMutexFn, pluginAPI: params.PluginAPI, } err := store.Migrate() if err != nil { params.Logger.Error(`Table creation / migration failed`, mlog.Err(err)) return nil, err } return store, 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 { //nolint:unparam if s.dbType == model.MysqlDBType { return "`" + fieldName + "`" } if s.dbType == model.PostgresDBType || s.dbType == model.SqliteDBType { return "\"" + fieldName + "\"" } return fieldName } func (s *SQLStore) getLicense(db sq.BaseRunner) *mmModel.License { return nil }