| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- package dao
- import (
- "database/sql"
- "embed"
- _ "embed"
- "fmt"
- "git.rtzhtech.cn/iss/public-lib/config"
- "github.com/astaxie/beego/logs"
- "gorm.io/driver/mysql"
- "gorm.io/gorm"
- "io/ioutil"
- "path"
- "runtime"
- "time"
- )
- func ConnectDb(conf *config.Config) (*gorm.DB, error) {
- dsn := fmt.Sprintf(`%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local`,
- conf.Username,
- conf.Password,
- conf.Server,
- conf.Port,
- conf.Database,
- )
- dbMysql, err := gorm.Open(mysql.New(mysql.Config{
- DSN: dsn, // DSN data source name
- DefaultStringSize: 256, // string 类型字段的默认长度
- DisableDatetimePrecision: true, // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持
- DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引
- DontSupportRenameColumn: true, // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列
- SkipInitializeWithVersion: false, // 根据当前 MySQL 版本自动配置
- }), &gorm.Config{})
- if err != nil {
- logs.Error("链接mysql异常 err:" + err.Error())
- }
- sqlDb, _ := dbMysql.DB()
- // 对于中小型 web 应用程序,我通常使用以下设置作为起点,然后根据负载测试结果和实际吞吐量级别进行优化。
- // SetMaxIdleConns: 设置空闲连接池中链接的最大数量,这个连接数应大于等于打开的最大连接,否则需要额外连接时会频繁进行打开关闭。
- // 最好与最大连接数保持相同,当大于最大连接数时,内部自动会减少到与最大连接数相同。
- sqlDb.SetMaxIdleConns(200)
- // SetMaxOpenConns: 设置打开数据库链接的最大数量
- sqlDb.SetMaxOpenConns(200)
- // SetConnMaxLifetime: 设置链接可复用的最大时间,以确保连接可以被驱动安全关闭。官方建议小于5分钟。
- sqlDb.SetConnMaxLifetime(5 * time.Minute)
- // SetConnMaxIdleTime: 设置闲置连接的最大存在时间, support>=go1.15
- sqlDb.SetConnMaxIdleTime(5 * time.Minute)
- if conf.Debug.Mysql {
- dbMysql = dbMysql.Debug()
- }
- return dbMysql, err
- }
- func CreatDatabase(conf *config.Config) {
- dsn := fmt.Sprintf(`%s:%s@tcp(%s:%s)/`,
- conf.Username,
- conf.Password,
- conf.Server,
- conf.Port,
- )
- sqldb, err := sql.Open("mysql", dsn)
- if err != nil {
- logs.Error(err)
- }
- err = sqldb.Ping()
- if err != nil {
- sqldb.Close()
- } else {
- fmt.Println("Database is ready......")
- }
- sql := "CREATE DATABASE IF NOT EXISTS iss;"
- _, err = sqldb.Exec(sql)
- if err != nil {
- fmt.Println("创建数据库失败" + err.Error())
- }
- }
- //go:embed sql/base.sql
- var f embed.FS
- func TableInfo(conf *config.Config) {
- CreatDatabase(conf)
- db := OpenDatabase(conf)
- sqlBytes, err := f.ReadFile("sql/base.sql")
- if err != nil {
- logs.Error(err)
- }
- _, err = db.Exec(string(sqlBytes))
- if err != nil {
- logs.Error(err)
- }
- //filepath.Walk(getUrl(),
- // func(path string, f os.FileInfo, err error) error {
- // if f == nil {
- // return err
- // }
- // if !f.IsDir() {
- //
- // }
- // return nil
- // })
- }
- func OpenDatabase(conf *config.Config) *sql.DB {
- dsn := fmt.Sprintf(`%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&multiStatements=true`,
- conf.Username,
- conf.Password,
- conf.Server,
- conf.Port,
- conf.Database,
- )
- sqldb, err := sql.Open("mysql", dsn)
- if err != nil {
- logs.Error(err)
- }
- err = sqldb.Ping()
- if err != nil {
- sqldb.Close()
- } else {
- fmt.Println("Database is ready......")
- }
- return sqldb
- }
- func ExecTable(db *sql.DB, path string) {
- sqlBytes, err := ioutil.ReadFile(path)
- if err != nil {
- logs.Error(err)
- }
- _, err = db.Exec(string(sqlBytes))
- if err != nil {
- logs.Error(err)
- }
- }
- func getUrl() string {
- _, file, _, _ := runtime.Caller(1)
- file = file
- pathNew := path.Dir(file)
- return pathNew + "/../deploy/sql"
- }
|