2016-03-30 13:43:40 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 GitHub Inc.
|
2016-05-16 09:09:17 +00:00
|
|
|
See https://github.com/github/gh-ost/blob/master/LICENSE
|
2016-03-30 13:43:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package mysql
|
|
|
|
|
2016-04-04 13:29:02 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2016-08-05 23:50:09 +00:00
|
|
|
"net"
|
2016-04-04 13:29:02 +00:00
|
|
|
)
|
|
|
|
|
2016-03-30 13:43:40 +00:00
|
|
|
// ConnectionConfig is the minimal configuration required to connect to a MySQL server
|
|
|
|
type ConnectionConfig struct {
|
2016-06-19 15:55:37 +00:00
|
|
|
Key InstanceKey
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
ImpliedKey *InstanceKey
|
2016-03-30 13:43:40 +00:00
|
|
|
}
|
2016-04-04 13:29:02 +00:00
|
|
|
|
|
|
|
func NewConnectionConfig() *ConnectionConfig {
|
|
|
|
config := &ConnectionConfig{
|
|
|
|
Key: InstanceKey{},
|
|
|
|
}
|
2016-06-19 15:55:37 +00:00
|
|
|
config.ImpliedKey = &config.Key
|
2016-04-04 13:29:02 +00:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2016-10-11 14:42:19 +00:00
|
|
|
// DuplicateCredentials creates a new connection config with given key and with same credentials as this config
|
|
|
|
func (this *ConnectionConfig) DuplicateCredentials(key InstanceKey) *ConnectionConfig {
|
2016-04-04 13:29:02 +00:00
|
|
|
config := &ConnectionConfig{
|
2016-10-11 14:42:19 +00:00
|
|
|
Key: key,
|
2016-04-04 13:29:02 +00:00
|
|
|
User: this.User,
|
|
|
|
Password: this.Password,
|
|
|
|
}
|
2016-06-19 15:55:37 +00:00
|
|
|
config.ImpliedKey = &config.Key
|
2016-04-04 13:29:02 +00:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2016-10-11 14:42:19 +00:00
|
|
|
func (this *ConnectionConfig) Duplicate() *ConnectionConfig {
|
|
|
|
return this.DuplicateCredentials(this.Key)
|
|
|
|
}
|
|
|
|
|
2016-04-04 13:29:02 +00:00
|
|
|
func (this *ConnectionConfig) String() string {
|
|
|
|
return fmt.Sprintf("%s, user=%s", this.Key.DisplayString(), this.User)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ConnectionConfig) Equals(other *ConnectionConfig) bool {
|
2016-06-19 15:55:37 +00:00
|
|
|
return this.Key.Equals(&other.Key) || this.ImpliedKey.Equals(other.ImpliedKey)
|
2016-04-04 13:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ConnectionConfig) GetDBUri(databaseName string) string {
|
2016-09-07 12:24:11 +00:00
|
|
|
hostname := this.Key.Hostname
|
|
|
|
var ip = net.ParseIP(hostname)
|
2016-08-05 23:50:09 +00:00
|
|
|
if (ip != nil) && (ip.To4() == nil) {
|
|
|
|
// Wrap IPv6 literals in square brackets
|
2016-09-07 12:24:11 +00:00
|
|
|
hostname = fmt.Sprintf("[%s]", hostname)
|
2016-08-05 23:50:09 +00:00
|
|
|
}
|
2017-08-30 08:38:22 +00:00
|
|
|
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?interpolateParams=true&autocommit=true&charset=utf8mb4,utf8,latin1", this.User, this.Password, hostname, this.Key.Port, databaseName)
|
2016-04-04 13:29:02 +00:00
|
|
|
}
|