restructure

This commit is contained in:
yanghao05
2023-04-20 12:11:34 +08:00
parent 6757e00d73
commit 5b8eca5d87
120 changed files with 546 additions and 7303 deletions

View File

@@ -0,0 +1,50 @@
package mysql
import (
"fmt"
)
// MySQL database config
type Config struct {
Host string
Port uint
Database string
Username string
Password string
Prefix string // 表前缀
Singular bool // 是否开启全局禁用复数true表示开启
MaxIdleConns int // 空闲中的最大连接数
MaxOpenConns int // 打开到数据库的最大连接数
Engine string // 数据库引擎默认InnoDB
}
func (m *Config) CreateDatabaseSql() string {
return fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s` DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_general_ci;", m.Database)
}
func (m *Config) EmptyDsn() string {
dsnTpl := "%s@tcp(%s:%d)/"
authString := func() string {
if len(m.Password) > 0 {
return m.Username + ":" + m.Password
}
return m.Username
}
return fmt.Sprintf(dsnTpl, authString(), m.Host, m.Port)
}
// DSN connection dsn
func (m *Config) DSN() string {
dsnTpl := "%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local"
authString := func() string {
if len(m.Password) > 0 {
return m.Username + ":" + m.Password
}
return m.Username
}
return fmt.Sprintf(dsnTpl, authString(), m.Host, m.Port, m.Database)
}