8e72b9c528
* Add RPC API support to plugin We use the mattermost-plugin-api client to create the sql.DB object and pass it to the store. To keep a common point of entry for both the standalone server and plugin, we refactor the store creation part out of server.New and pass the DB as a dependency to the server constructor. This allow us to create different stores in plugin and standalone, so that the server code remains unaware of any differences. https://focalboard-community.octo.mattermost.com/workspace/zyoahc9uapdn3xdptac6jb69ic?id=285b80a3-257d-41f6-8cf4-ed80ca9d92e5&v=495cdb4d-c13a-4992-8eb9-80cfee2819a4&c=c7386db7-65fd-469b-8bcf-8dc8f8e61e4f * Support linux desktop app * refactor * fix typos * Change authlayer to use existing DB conn too
83 lines
1.9 KiB
Go
83 lines
1.9 KiB
Go
package sqlstore
|
|
|
|
import (
|
|
"database/sql"
|
|
|
|
sq "github.com/Masterminds/squirrel"
|
|
"github.com/mattermost/focalboard/server/services/mlog"
|
|
)
|
|
|
|
const (
|
|
mysqlDBType = "mysql"
|
|
sqliteDBType = "sqlite3"
|
|
postgresDBType = "postgres"
|
|
)
|
|
|
|
// SQLStore is a SQL database.
|
|
type SQLStore struct {
|
|
db *sql.DB
|
|
dbType string
|
|
tablePrefix string
|
|
connectionString string
|
|
logger *mlog.Logger
|
|
}
|
|
|
|
// New creates a new SQL implementation of the store.
|
|
func New(dbType, connectionString, tablePrefix string, logger *mlog.Logger, db *sql.DB) (*SQLStore, error) {
|
|
logger.Info("connectDatabase", mlog.String("dbType", dbType), mlog.String("connStr", connectionString))
|
|
store := &SQLStore{
|
|
// TODO: add replica DB support too.
|
|
db: db,
|
|
dbType: dbType,
|
|
tablePrefix: tablePrefix,
|
|
connectionString: connectionString,
|
|
logger: logger,
|
|
}
|
|
|
|
err := store.Migrate()
|
|
if err != nil {
|
|
logger.Error(`Table creation / migration failed`, mlog.Err(err))
|
|
|
|
return nil, err
|
|
}
|
|
|
|
err = store.InitializeTemplates()
|
|
if err != nil {
|
|
logger.Error(`InitializeTemplates 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
|
|
}
|
|
|
|
func (s *SQLStore) getQueryBuilder() sq.StatementBuilderType {
|
|
builder := sq.StatementBuilder
|
|
if s.dbType == postgresDBType || s.dbType == sqliteDBType {
|
|
builder = builder.PlaceholderFormat(sq.Dollar)
|
|
}
|
|
|
|
return builder.RunWith(s.db)
|
|
}
|
|
|
|
func (s *SQLStore) escapeField(fieldName string) string {
|
|
if s.dbType == mysqlDBType {
|
|
return "`" + fieldName + "`"
|
|
}
|
|
if s.dbType == postgresDBType || s.dbType == sqliteDBType {
|
|
return "\"" + fieldName + "\""
|
|
}
|
|
return fieldName
|
|
}
|