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-05-29 08:23:10 +02:00
|
|
|
"github.com/mattermost/focalboard/server/services/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-05-29 08:23:10 +02:00
|
|
|
logger *mlog.Logger
|
2020-10-16 22:26:47 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// New creates a new SQL implementation of the store.
|
2021-05-29 08:23:10 +02:00
|
|
|
func New(dbType, connectionString string, tablePrefix string, logger *mlog.Logger) (*SQLStore, error) {
|
|
|
|
logger.Info("connectDatabase", mlog.String("dbType", dbType), mlog.String("connStr", connectionString))
|
2020-10-16 22:26:47 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
db, err := sql.Open(dbType, connectionString)
|
|
|
|
if err != nil {
|
2021-05-29 08:23:10 +02:00
|
|
|
logger.Error("connectDatabase failed", mlog.Err(err))
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-16 22:26:47 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Ping()
|
|
|
|
if err != nil {
|
2021-05-29 08:23:10 +02:00
|
|
|
logger.Error(`Database Ping failed`, mlog.Err(err))
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-16 22:26:47 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
store := &SQLStore{
|
2021-05-24 19:06:11 +02:00
|
|
|
db: db,
|
|
|
|
dbType: dbType,
|
|
|
|
tablePrefix: tablePrefix,
|
|
|
|
connectionString: connectionString,
|
2021-05-29 08:23:10 +02:00
|
|
|
logger: logger,
|
2020-10-16 22:26:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = store.Migrate()
|
|
|
|
if err != nil {
|
2021-05-29 08:23:10 +02:00
|
|
|
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
|
|
|
|
}
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-12-10 21:41:06 +01:00
|
|
|
err = store.InitializeTemplates()
|
|
|
|
if err != nil {
|
2021-05-29 08:23:10 +02:00
|
|
|
logger.Error(`InitializeTemplates failed`, mlog.Err(err))
|
2020-12-10 21:41:06 +01:00
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-16 22:26:47 +02:00
|
|
|
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
|
|
|
|
|
|
|
func (s *SQLStore) getQueryBuilder() 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
|
|
|
|
2020-10-20 20:00:46 +02:00
|
|
|
return builder.RunWith(s.db)
|
|
|
|
}
|
2021-04-22 22:53:01 +02:00
|
|
|
|
|
|
|
func (s *SQLStore) escapeField(fieldName string) string {
|
|
|
|
if s.dbType == mysqlDBType {
|
|
|
|
return "`" + fieldName + "`"
|
|
|
|
}
|
|
|
|
if s.dbType == postgresDBType || s.dbType == sqliteDBType {
|
|
|
|
return "\"" + fieldName + "\""
|
|
|
|
}
|
|
|
|
return fieldName
|
|
|
|
}
|