2021-11-23 09:45:38 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2022-10-22 04:25:12 +00:00
|
|
|
"github.com/go-sql-driver/mysql"
|
2021-11-23 09:45:38 +00:00
|
|
|
_ "github.com/go-sql-driver/mysql" // Load the MySQL Driver used in the connection string.
|
|
|
|
|
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MySQLProvider is a MySQL provider.
|
|
|
|
type MySQLProvider struct {
|
|
|
|
SQLProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMySQLProvider a MySQL provider.
|
2021-12-01 12:11:29 +00:00
|
|
|
func NewMySQLProvider(config *schema.Configuration) (provider *MySQLProvider) {
|
2021-11-23 09:45:38 +00:00
|
|
|
provider = &MySQLProvider{
|
2021-12-01 12:11:29 +00:00
|
|
|
SQLProvider: NewSQLProvider(config, providerMySQL, providerMySQL, dataSourceNameMySQL(*config.Storage.MySQL)),
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// All providers have differing SELECT existing table statements.
|
|
|
|
provider.sqlSelectExistingTables = queryMySQLSelectExistingTables
|
|
|
|
|
|
|
|
// Specific alterations to this provider.
|
|
|
|
provider.sqlFmtRenameTable = queryFmtMySQLRenameTable
|
|
|
|
|
|
|
|
return provider
|
|
|
|
}
|
|
|
|
|
|
|
|
func dataSourceNameMySQL(config schema.MySQLStorageConfiguration) (dataSourceName string) {
|
2022-10-22 04:25:12 +00:00
|
|
|
dconfig := mysql.NewConfig()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case config.Port == 0:
|
|
|
|
dconfig.Net = sqlNetworkTypeTCP
|
|
|
|
dconfig.Addr = fmt.Sprintf("%s:%d", config.Host, 3306)
|
|
|
|
default:
|
|
|
|
dconfig.Net = sqlNetworkTypeTCP
|
|
|
|
dconfig.Addr = fmt.Sprintf("%s:%d", config.Host, config.Port)
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 04:25:12 +00:00
|
|
|
switch config.Port {
|
|
|
|
case 0:
|
|
|
|
dconfig.Addr = config.Host
|
|
|
|
default:
|
|
|
|
dconfig.Addr = fmt.Sprintf("%s:%d", config.Host, config.Port)
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 04:25:12 +00:00
|
|
|
dconfig.DBName = config.Database
|
|
|
|
dconfig.User = config.Username
|
|
|
|
dconfig.Passwd = config.Password
|
|
|
|
dconfig.Timeout = config.Timeout
|
|
|
|
dconfig.MultiStatements = true
|
|
|
|
dconfig.ParseTime = true
|
|
|
|
dconfig.Loc = time.Local
|
2021-11-23 09:45:38 +00:00
|
|
|
|
2022-10-22 04:25:12 +00:00
|
|
|
return dconfig.FormatDSN()
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|