change DBConfig.Path to URI and add ConnectionString method
This commit is contained in:
19
config.go
19
config.go
@@ -1,14 +1,31 @@
|
|||||||
package dbx
|
package dbx
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
// DBConfig provides a template for database connection configuration compatible
|
// DBConfig provides a template for database connection configuration compatible
|
||||||
// with go-toolkit/config. If used as a field in a struct, remember to include
|
// with go-toolkit/config. If used as a field in a struct, remember to include
|
||||||
// the `validate:"dive"` tag to ensure that the validator checks sub-fields.
|
// the `validate:"dive"` tag to ensure that the validator checks sub-fields.
|
||||||
type DBConfig struct {
|
type DBConfig struct {
|
||||||
User string `validate:"required"`
|
User string `validate:"required"`
|
||||||
Password string `validate:"required"`
|
Password string `validate:"required"`
|
||||||
Path string `validate:"hostname_port,required"`
|
URI string `validate:"hostname_port,required"`
|
||||||
Name string `validate:"required"`
|
Name string `validate:"required"`
|
||||||
MaxConn int `validate:"min=1,max=1000"`
|
MaxConn int `validate:"min=1,max=1000"`
|
||||||
AutoMigrate bool
|
AutoMigrate bool
|
||||||
DebugLog bool
|
DebugLog bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ConnectionString returns a connection string compatible with the given
|
||||||
|
// SQL dialect.
|
||||||
|
func (cfg *DBConfig) ConnectionString(dialect Dialect) string {
|
||||||
|
switch dialect {
|
||||||
|
case DialectPostgres:
|
||||||
|
return fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable",
|
||||||
|
cfg.User, cfg.Password, cfg.URI, cfg.Name)
|
||||||
|
case DialectMySQL:
|
||||||
|
return fmt.Sprintf("%s:%s@tcp(%s)/%s?parseTime=true&loc=Local",
|
||||||
|
cfg.User, cfg.Password, cfg.URI, cfg.Name)
|
||||||
|
default:
|
||||||
|
panic("unsupported dialect: " + string(dialect))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user