2016-11-03 16:16:01 -06:00
|
|
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
|
|
|
//
|
|
|
|
// Copyright 2016 The Go-MySQL-Driver Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-07-03 15:58:31 -06:00
|
|
|
"crypto/rsa"
|
2016-11-03 16:16:01 -06:00
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-07-11 15:07:52 -06:00
|
|
|
"math/big"
|
2016-11-03 16:16:01 -06:00
|
|
|
"net"
|
|
|
|
"net/url"
|
2018-07-03 15:58:31 -06:00
|
|
|
"sort"
|
2016-11-03 16:16:01 -06:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errInvalidDSNUnescaped = errors.New("invalid DSN: did you forget to escape a param value?")
|
|
|
|
errInvalidDSNAddr = errors.New("invalid DSN: network address not terminated (missing closing brace)")
|
|
|
|
errInvalidDSNNoSlash = errors.New("invalid DSN: missing the slash separating the database name")
|
|
|
|
errInvalidDSNUnsafeCollation = errors.New("invalid DSN: interpolateParams can not be used with unsafe collations")
|
|
|
|
)
|
|
|
|
|
2018-07-03 15:58:31 -06:00
|
|
|
// Config is a configuration parsed from a DSN string.
|
|
|
|
// If a new Config is created instead of being parsed from a DSN string,
|
|
|
|
// the NewConfig function should be used, which sets default values.
|
2016-11-03 16:16:01 -06:00
|
|
|
type Config struct {
|
|
|
|
User string // Username
|
|
|
|
Passwd string // Password (requires User)
|
|
|
|
Net string // Network type
|
|
|
|
Addr string // Network address (requires Net)
|
|
|
|
DBName string // Database name
|
|
|
|
Params map[string]string // Connection parameters
|
|
|
|
Collation string // Connection collation
|
|
|
|
Loc *time.Location // Location for time.Time values
|
|
|
|
MaxAllowedPacket int // Max packet size allowed
|
2018-07-03 15:58:31 -06:00
|
|
|
ServerPubKey string // Server public key name
|
|
|
|
pubKey *rsa.PublicKey // Server public key
|
2016-11-03 16:16:01 -06:00
|
|
|
TLSConfig string // TLS configuration name
|
|
|
|
tls *tls.Config // TLS configuration
|
|
|
|
Timeout time.Duration // Dial timeout
|
|
|
|
ReadTimeout time.Duration // I/O read timeout
|
|
|
|
WriteTimeout time.Duration // I/O write timeout
|
|
|
|
|
|
|
|
AllowAllFiles bool // Allow all files to be used with LOAD DATA LOCAL INFILE
|
|
|
|
AllowCleartextPasswords bool // Allows the cleartext client side plugin
|
|
|
|
AllowNativePasswords bool // Allows the native password authentication method
|
|
|
|
AllowOldPasswords bool // Allows the old insecure password method
|
2020-07-11 15:07:52 -06:00
|
|
|
CheckConnLiveness bool // Check connections for liveness before using them
|
2016-11-03 16:16:01 -06:00
|
|
|
ClientFoundRows bool // Return number of matching rows instead of rows changed
|
|
|
|
ColumnsWithAlias bool // Prepend table alias to column names
|
|
|
|
InterpolateParams bool // Interpolate placeholders into query string
|
|
|
|
MultiStatements bool // Allow multiple statements in one query
|
|
|
|
ParseTime bool // Parse time values to time.Time
|
2018-07-03 15:58:31 -06:00
|
|
|
RejectReadOnly bool // Reject read-only connections
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewConfig creates a new Config and sets default values.
|
|
|
|
func NewConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
Collation: defaultCollation,
|
|
|
|
Loc: time.UTC,
|
|
|
|
MaxAllowedPacket: defaultMaxAllowedPacket,
|
|
|
|
AllowNativePasswords: true,
|
2020-07-11 15:07:52 -06:00
|
|
|
CheckConnLiveness: true,
|
2018-07-03 15:58:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-11 15:07:52 -06:00
|
|
|
func (cfg *Config) Clone() *Config {
|
|
|
|
cp := *cfg
|
|
|
|
if cp.tls != nil {
|
|
|
|
cp.tls = cfg.tls.Clone()
|
|
|
|
}
|
|
|
|
if len(cp.Params) > 0 {
|
|
|
|
cp.Params = make(map[string]string, len(cfg.Params))
|
|
|
|
for k, v := range cfg.Params {
|
|
|
|
cp.Params[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cfg.pubKey != nil {
|
|
|
|
cp.pubKey = &rsa.PublicKey{
|
|
|
|
N: new(big.Int).Set(cfg.pubKey.N),
|
|
|
|
E: cfg.pubKey.E,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &cp
|
|
|
|
}
|
|
|
|
|
2018-07-03 15:58:31 -06:00
|
|
|
func (cfg *Config) normalize() error {
|
|
|
|
if cfg.InterpolateParams && unsafeCollations[cfg.Collation] {
|
|
|
|
return errInvalidDSNUnsafeCollation
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set default network if empty
|
|
|
|
if cfg.Net == "" {
|
|
|
|
cfg.Net = "tcp"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set default address if empty
|
|
|
|
if cfg.Addr == "" {
|
|
|
|
switch cfg.Net {
|
|
|
|
case "tcp":
|
|
|
|
cfg.Addr = "127.0.0.1:3306"
|
|
|
|
case "unix":
|
|
|
|
cfg.Addr = "/tmp/mysql.sock"
|
|
|
|
default:
|
|
|
|
return errors.New("default addr for network '" + cfg.Net + "' unknown")
|
|
|
|
}
|
|
|
|
} else if cfg.Net == "tcp" {
|
|
|
|
cfg.Addr = ensureHavePort(cfg.Addr)
|
|
|
|
}
|
|
|
|
|
2020-07-11 15:07:52 -06:00
|
|
|
switch cfg.TLSConfig {
|
|
|
|
case "false", "":
|
|
|
|
// don't set anything
|
|
|
|
case "true":
|
|
|
|
cfg.tls = &tls.Config{}
|
|
|
|
case "skip-verify", "preferred":
|
|
|
|
cfg.tls = &tls.Config{InsecureSkipVerify: true}
|
|
|
|
default:
|
|
|
|
cfg.tls = getTLSConfigClone(cfg.TLSConfig)
|
|
|
|
if cfg.tls == nil {
|
|
|
|
return errors.New("invalid value / unknown config name: " + cfg.TLSConfig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.tls != nil && cfg.tls.ServerName == "" && !cfg.tls.InsecureSkipVerify {
|
|
|
|
host, _, err := net.SplitHostPort(cfg.Addr)
|
|
|
|
if err == nil {
|
|
|
|
cfg.tls.ServerName = host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.ServerPubKey != "" {
|
|
|
|
cfg.pubKey = getServerPubKey(cfg.ServerPubKey)
|
|
|
|
if cfg.pubKey == nil {
|
|
|
|
return errors.New("invalid value / unknown server pub key name: " + cfg.ServerPubKey)
|
2018-07-03 15:58:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
2020-07-11 15:07:52 -06:00
|
|
|
func writeDSNParam(buf *bytes.Buffer, hasParam *bool, name, value string) {
|
|
|
|
buf.Grow(1 + len(name) + 1 + len(value))
|
|
|
|
if !*hasParam {
|
|
|
|
*hasParam = true
|
|
|
|
buf.WriteByte('?')
|
|
|
|
} else {
|
|
|
|
buf.WriteByte('&')
|
|
|
|
}
|
|
|
|
buf.WriteString(name)
|
|
|
|
buf.WriteByte('=')
|
|
|
|
buf.WriteString(value)
|
|
|
|
}
|
|
|
|
|
2016-11-03 16:16:01 -06:00
|
|
|
// FormatDSN formats the given Config into a DSN string which can be passed to
|
|
|
|
// the driver.
|
|
|
|
func (cfg *Config) FormatDSN() string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
// [username[:password]@]
|
|
|
|
if len(cfg.User) > 0 {
|
|
|
|
buf.WriteString(cfg.User)
|
|
|
|
if len(cfg.Passwd) > 0 {
|
|
|
|
buf.WriteByte(':')
|
|
|
|
buf.WriteString(cfg.Passwd)
|
|
|
|
}
|
|
|
|
buf.WriteByte('@')
|
|
|
|
}
|
|
|
|
|
|
|
|
// [protocol[(address)]]
|
|
|
|
if len(cfg.Net) > 0 {
|
|
|
|
buf.WriteString(cfg.Net)
|
|
|
|
if len(cfg.Addr) > 0 {
|
|
|
|
buf.WriteByte('(')
|
|
|
|
buf.WriteString(cfg.Addr)
|
|
|
|
buf.WriteByte(')')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// /dbname
|
|
|
|
buf.WriteByte('/')
|
|
|
|
buf.WriteString(cfg.DBName)
|
|
|
|
|
|
|
|
// [?param1=value1&...¶mN=valueN]
|
|
|
|
hasParam := false
|
|
|
|
|
|
|
|
if cfg.AllowAllFiles {
|
|
|
|
hasParam = true
|
|
|
|
buf.WriteString("?allowAllFiles=true")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.AllowCleartextPasswords {
|
2020-07-11 15:07:52 -06:00
|
|
|
writeDSNParam(&buf, &hasParam, "allowCleartextPasswords", "true")
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
2018-07-03 15:58:31 -06:00
|
|
|
if !cfg.AllowNativePasswords {
|
2020-07-11 15:07:52 -06:00
|
|
|
writeDSNParam(&buf, &hasParam, "allowNativePasswords", "false")
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.AllowOldPasswords {
|
2020-07-11 15:07:52 -06:00
|
|
|
writeDSNParam(&buf, &hasParam, "allowOldPasswords", "true")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cfg.CheckConnLiveness {
|
|
|
|
writeDSNParam(&buf, &hasParam, "checkConnLiveness", "false")
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.ClientFoundRows {
|
2020-07-11 15:07:52 -06:00
|
|
|
writeDSNParam(&buf, &hasParam, "clientFoundRows", "true")
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if col := cfg.Collation; col != defaultCollation && len(col) > 0 {
|
2020-07-11 15:07:52 -06:00
|
|
|
writeDSNParam(&buf, &hasParam, "collation", col)
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.ColumnsWithAlias {
|
2020-07-11 15:07:52 -06:00
|
|
|
writeDSNParam(&buf, &hasParam, "columnsWithAlias", "true")
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.InterpolateParams {
|
2020-07-11 15:07:52 -06:00
|
|
|
writeDSNParam(&buf, &hasParam, "interpolateParams", "true")
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Loc != time.UTC && cfg.Loc != nil {
|
2020-07-11 15:07:52 -06:00
|
|
|
writeDSNParam(&buf, &hasParam, "loc", url.QueryEscape(cfg.Loc.String()))
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.MultiStatements {
|
2020-07-11 15:07:52 -06:00
|
|
|
writeDSNParam(&buf, &hasParam, "multiStatements", "true")
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.ParseTime {
|
2020-07-11 15:07:52 -06:00
|
|
|
writeDSNParam(&buf, &hasParam, "parseTime", "true")
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.ReadTimeout > 0 {
|
2020-07-11 15:07:52 -06:00
|
|
|
writeDSNParam(&buf, &hasParam, "readTimeout", cfg.ReadTimeout.String())
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
2018-07-03 15:58:31 -06:00
|
|
|
if cfg.RejectReadOnly {
|
2020-07-11 15:07:52 -06:00
|
|
|
writeDSNParam(&buf, &hasParam, "rejectReadOnly", "true")
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
2018-07-03 15:58:31 -06:00
|
|
|
if len(cfg.ServerPubKey) > 0 {
|
2020-07-11 15:07:52 -06:00
|
|
|
writeDSNParam(&buf, &hasParam, "serverPubKey", url.QueryEscape(cfg.ServerPubKey))
|
2018-07-03 15:58:31 -06:00
|
|
|
}
|
|
|
|
|
2016-11-03 16:16:01 -06:00
|
|
|
if cfg.Timeout > 0 {
|
2020-07-11 15:07:52 -06:00
|
|
|
writeDSNParam(&buf, &hasParam, "timeout", cfg.Timeout.String())
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(cfg.TLSConfig) > 0 {
|
2020-07-11 15:07:52 -06:00
|
|
|
writeDSNParam(&buf, &hasParam, "tls", url.QueryEscape(cfg.TLSConfig))
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.WriteTimeout > 0 {
|
2020-07-11 15:07:52 -06:00
|
|
|
writeDSNParam(&buf, &hasParam, "writeTimeout", cfg.WriteTimeout.String())
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
2018-07-03 15:58:31 -06:00
|
|
|
if cfg.MaxAllowedPacket != defaultMaxAllowedPacket {
|
2020-07-11 15:07:52 -06:00
|
|
|
writeDSNParam(&buf, &hasParam, "maxAllowedPacket", strconv.Itoa(cfg.MaxAllowedPacket))
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// other params
|
|
|
|
if cfg.Params != nil {
|
2018-07-03 15:58:31 -06:00
|
|
|
var params []string
|
|
|
|
for param := range cfg.Params {
|
|
|
|
params = append(params, param)
|
|
|
|
}
|
|
|
|
sort.Strings(params)
|
|
|
|
for _, param := range params {
|
2020-07-11 15:07:52 -06:00
|
|
|
writeDSNParam(&buf, &hasParam, param, url.QueryEscape(cfg.Params[param]))
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseDSN parses the DSN string to a Config
|
|
|
|
func ParseDSN(dsn string) (cfg *Config, err error) {
|
|
|
|
// New config with some default values
|
2018-07-03 15:58:31 -06:00
|
|
|
cfg = NewConfig()
|
2016-11-03 16:16:01 -06:00
|
|
|
|
|
|
|
// [user[:password]@][net[(addr)]]/dbname[?param1=value1¶mN=valueN]
|
|
|
|
// Find the last '/' (since the password or the net addr might contain a '/')
|
|
|
|
foundSlash := false
|
|
|
|
for i := len(dsn) - 1; i >= 0; i-- {
|
|
|
|
if dsn[i] == '/' {
|
|
|
|
foundSlash = true
|
|
|
|
var j, k int
|
|
|
|
|
|
|
|
// left part is empty if i <= 0
|
|
|
|
if i > 0 {
|
|
|
|
// [username[:password]@][protocol[(address)]]
|
|
|
|
// Find the last '@' in dsn[:i]
|
|
|
|
for j = i; j >= 0; j-- {
|
|
|
|
if dsn[j] == '@' {
|
|
|
|
// username[:password]
|
|
|
|
// Find the first ':' in dsn[:j]
|
|
|
|
for k = 0; k < j; k++ {
|
|
|
|
if dsn[k] == ':' {
|
|
|
|
cfg.Passwd = dsn[k+1 : j]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cfg.User = dsn[:k]
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// [protocol[(address)]]
|
|
|
|
// Find the first '(' in dsn[j+1:i]
|
|
|
|
for k = j + 1; k < i; k++ {
|
|
|
|
if dsn[k] == '(' {
|
|
|
|
// dsn[i-1] must be == ')' if an address is specified
|
|
|
|
if dsn[i-1] != ')' {
|
|
|
|
if strings.ContainsRune(dsn[k+1:i], ')') {
|
|
|
|
return nil, errInvalidDSNUnescaped
|
|
|
|
}
|
|
|
|
return nil, errInvalidDSNAddr
|
|
|
|
}
|
|
|
|
cfg.Addr = dsn[k+1 : i-1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cfg.Net = dsn[j+1 : k]
|
|
|
|
}
|
|
|
|
|
|
|
|
// dbname[?param1=value1&...¶mN=valueN]
|
|
|
|
// Find the first '?' in dsn[i+1:]
|
|
|
|
for j = i + 1; j < len(dsn); j++ {
|
|
|
|
if dsn[j] == '?' {
|
|
|
|
if err = parseDSNParams(cfg, dsn[j+1:]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cfg.DBName = dsn[i+1 : j]
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !foundSlash && len(dsn) > 0 {
|
|
|
|
return nil, errInvalidDSNNoSlash
|
|
|
|
}
|
|
|
|
|
2018-07-03 15:58:31 -06:00
|
|
|
if err = cfg.normalize(); err != nil {
|
|
|
|
return nil, err
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseDSNParams parses the DSN "query string"
|
|
|
|
// Values must be url.QueryEscape'ed
|
|
|
|
func parseDSNParams(cfg *Config, params string) (err error) {
|
|
|
|
for _, v := range strings.Split(params, "&") {
|
|
|
|
param := strings.SplitN(v, "=", 2)
|
|
|
|
if len(param) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// cfg params
|
|
|
|
switch value := param[1]; param[0] {
|
|
|
|
// Disable INFILE whitelist / enable all files
|
|
|
|
case "allowAllFiles":
|
|
|
|
var isBool bool
|
|
|
|
cfg.AllowAllFiles, isBool = readBool(value)
|
|
|
|
if !isBool {
|
|
|
|
return errors.New("invalid bool value: " + value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use cleartext authentication mode (MySQL 5.5.10+)
|
|
|
|
case "allowCleartextPasswords":
|
|
|
|
var isBool bool
|
|
|
|
cfg.AllowCleartextPasswords, isBool = readBool(value)
|
|
|
|
if !isBool {
|
|
|
|
return errors.New("invalid bool value: " + value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use native password authentication
|
|
|
|
case "allowNativePasswords":
|
|
|
|
var isBool bool
|
|
|
|
cfg.AllowNativePasswords, isBool = readBool(value)
|
|
|
|
if !isBool {
|
|
|
|
return errors.New("invalid bool value: " + value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use old authentication mode (pre MySQL 4.1)
|
|
|
|
case "allowOldPasswords":
|
|
|
|
var isBool bool
|
|
|
|
cfg.AllowOldPasswords, isBool = readBool(value)
|
|
|
|
if !isBool {
|
|
|
|
return errors.New("invalid bool value: " + value)
|
|
|
|
}
|
|
|
|
|
2020-07-11 15:07:52 -06:00
|
|
|
// Check connections for Liveness before using them
|
|
|
|
case "checkConnLiveness":
|
|
|
|
var isBool bool
|
|
|
|
cfg.CheckConnLiveness, isBool = readBool(value)
|
|
|
|
if !isBool {
|
|
|
|
return errors.New("invalid bool value: " + value)
|
|
|
|
}
|
|
|
|
|
2016-11-03 16:16:01 -06:00
|
|
|
// Switch "rowsAffected" mode
|
|
|
|
case "clientFoundRows":
|
|
|
|
var isBool bool
|
|
|
|
cfg.ClientFoundRows, isBool = readBool(value)
|
|
|
|
if !isBool {
|
|
|
|
return errors.New("invalid bool value: " + value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collation
|
|
|
|
case "collation":
|
|
|
|
cfg.Collation = value
|
|
|
|
break
|
|
|
|
|
|
|
|
case "columnsWithAlias":
|
|
|
|
var isBool bool
|
|
|
|
cfg.ColumnsWithAlias, isBool = readBool(value)
|
|
|
|
if !isBool {
|
|
|
|
return errors.New("invalid bool value: " + value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compression
|
|
|
|
case "compress":
|
|
|
|
return errors.New("compression not implemented yet")
|
|
|
|
|
|
|
|
// Enable client side placeholder substitution
|
|
|
|
case "interpolateParams":
|
|
|
|
var isBool bool
|
|
|
|
cfg.InterpolateParams, isBool = readBool(value)
|
|
|
|
if !isBool {
|
|
|
|
return errors.New("invalid bool value: " + value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Time Location
|
|
|
|
case "loc":
|
|
|
|
if value, err = url.QueryUnescape(value); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cfg.Loc, err = time.LoadLocation(value)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// multiple statements in one query
|
|
|
|
case "multiStatements":
|
|
|
|
var isBool bool
|
|
|
|
cfg.MultiStatements, isBool = readBool(value)
|
|
|
|
if !isBool {
|
|
|
|
return errors.New("invalid bool value: " + value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// time.Time parsing
|
|
|
|
case "parseTime":
|
|
|
|
var isBool bool
|
|
|
|
cfg.ParseTime, isBool = readBool(value)
|
|
|
|
if !isBool {
|
|
|
|
return errors.New("invalid bool value: " + value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// I/O read Timeout
|
|
|
|
case "readTimeout":
|
|
|
|
cfg.ReadTimeout, err = time.ParseDuration(value)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-03 15:58:31 -06:00
|
|
|
// Reject read-only connections
|
|
|
|
case "rejectReadOnly":
|
2016-11-03 16:16:01 -06:00
|
|
|
var isBool bool
|
2018-07-03 15:58:31 -06:00
|
|
|
cfg.RejectReadOnly, isBool = readBool(value)
|
2016-11-03 16:16:01 -06:00
|
|
|
if !isBool {
|
|
|
|
return errors.New("invalid bool value: " + value)
|
|
|
|
}
|
|
|
|
|
2018-07-03 15:58:31 -06:00
|
|
|
// Server public key
|
|
|
|
case "serverPubKey":
|
|
|
|
name, err := url.QueryUnescape(value)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid value for server pub key name: %v", err)
|
|
|
|
}
|
2020-07-11 15:07:52 -06:00
|
|
|
cfg.ServerPubKey = name
|
2018-07-03 15:58:31 -06:00
|
|
|
|
|
|
|
// Strict mode
|
|
|
|
case "strict":
|
|
|
|
panic("strict mode has been removed. See https://github.com/go-sql-driver/mysql/wiki/strict-mode")
|
|
|
|
|
2016-11-03 16:16:01 -06:00
|
|
|
// Dial Timeout
|
|
|
|
case "timeout":
|
|
|
|
cfg.Timeout, err = time.ParseDuration(value)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TLS-Encryption
|
|
|
|
case "tls":
|
|
|
|
boolValue, isBool := readBool(value)
|
|
|
|
if isBool {
|
|
|
|
if boolValue {
|
|
|
|
cfg.TLSConfig = "true"
|
|
|
|
} else {
|
|
|
|
cfg.TLSConfig = "false"
|
|
|
|
}
|
2020-07-11 15:07:52 -06:00
|
|
|
} else if vl := strings.ToLower(value); vl == "skip-verify" || vl == "preferred" {
|
2016-11-03 16:16:01 -06:00
|
|
|
cfg.TLSConfig = vl
|
|
|
|
} else {
|
|
|
|
name, err := url.QueryUnescape(value)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid value for TLS config name: %v", err)
|
|
|
|
}
|
2020-07-11 15:07:52 -06:00
|
|
|
cfg.TLSConfig = name
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// I/O write Timeout
|
|
|
|
case "writeTimeout":
|
|
|
|
cfg.WriteTimeout, err = time.ParseDuration(value)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "maxAllowedPacket":
|
|
|
|
cfg.MaxAllowedPacket, err = strconv.Atoi(value)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// lazy init
|
|
|
|
if cfg.Params == nil {
|
|
|
|
cfg.Params = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Params[param[0]], err = url.QueryUnescape(value); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2018-07-03 15:58:31 -06:00
|
|
|
|
|
|
|
func ensureHavePort(addr string) string {
|
|
|
|
if _, _, err := net.SplitHostPort(addr); err != nil {
|
|
|
|
return net.JoinHostPort(addr, "3306")
|
|
|
|
}
|
|
|
|
return addr
|
|
|
|
}
|