-
Notifications
You must be signed in to change notification settings - Fork 0
/
orm.go
119 lines (102 loc) · 3.61 KB
/
orm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package contract
import (
"context"
"net"
"strconv"
"time"
"github.com/go-sql-driver/mysql"
"github.com/y19941115mx/ygo/framework"
"gorm.io/gorm"
)
// ORMKey 代表 ORM的服务
const ORMKey = "ygo:orm"
type TableColumn struct {
Field string `gorm:"column:Field"`
Type string `gorm:"column:Type"`
Null string `gorm:"column:Null"`
Key string `gorm:"column:key"`
Default string `gorm:"column:Default"`
Extra string `gorm:"column:Extra"`
}
// ORMService 表示传入的参数
type ORMService interface {
GetDB(option ...DBOption) (*gorm.DB, error)
// CanConnect 是否可以连接
CanConnect(ctx context.Context, db *gorm.DB) (bool, error)
// Table 相关
GetTables(ctx context.Context, db *gorm.DB) ([]string, error)
HasTable(ctx context.Context, db *gorm.DB, table string) (bool, error)
GetTableColumns(ctx context.Context, db *gorm.DB, table string) ([]TableColumn, error)
}
// DBOption 代表初始化的时候的选项
type DBOption func(container framework.Container, config *DBConfig) error
// DBConfig 代表数据库连接的所有配置
type DBConfig struct {
// 以下配置关于dsn
WriteTimeout string `yaml:"write_timeout"` // 写超时时间
Loc string `yaml:"loc"` // 时区
Port int `yaml:"port"` // 端口
ReadTimeout string `yaml:"read_timeout"` // 读超时时间
Charset string `yaml:"charset"` // 字符集
ParseTime bool `yaml:"parse_time"` // 是否解析时间
Protocol string `yaml:"protocol"` // 传输协议
Dsn string `yaml:"dsn"` // 直接传递dsn,如果传递了,其他关于dsn的配置均无效
Database string `yaml:"database"` // 数据库
Collation string `yaml:"collation"` // 字符序
Timeout string `yaml:"timeout"` // 连接超时时间
Username string `yaml:"username"` // 用户名
Password string `yaml:"password"` // 密码
Driver string `yaml:"driver"` // 驱动
Host string `yaml:"host"` // 数据库地址
// 以下配置关于连接池
ConnMaxIdle int `yaml:"conn_max_idle"` // 最大空闲连接数
ConnMaxOpen int `yaml:"conn_max_open"` // 最大连接数
ConnMaxLifetime string `yaml:"conn_max_lifetime"` // 连接最大生命周期
ConnMaxIdletime string `yaml:"conn_max_idletime"` // 空闲最大生命周期
// 以下配置关于gorm
*gorm.Config // 集成gorm的配置
}
// FormatDsn 生成dsn
func (conf *DBConfig) FormatDsn() (string, error) {
var err error
var timeout, readTimeout, writeTimeout time.Duration
port := strconv.Itoa(conf.Port)
if conf.Timeout != "" {
timeout, err = time.ParseDuration(conf.Timeout)
if err != nil {
return "", err
}
}
if conf.ReadTimeout != "" {
readTimeout, err = time.ParseDuration(conf.ReadTimeout)
if err != nil {
return "", err
}
}
if conf.WriteTimeout != "" {
writeTimeout, err = time.ParseDuration(conf.WriteTimeout)
if err != nil {
return "", err
}
}
location, err := time.LoadLocation(conf.Loc)
if err != nil {
return "", err
}
driverConf := &mysql.Config{
User: conf.Username,
Passwd: conf.Password,
Net: conf.Protocol,
Addr: net.JoinHostPort(conf.Host, port),
DBName: conf.Database,
Collation: conf.Collation,
Loc: location,
Timeout: timeout,
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
ParseTime: conf.ParseTime,
AllowNativePasswords: true,
CheckConnLiveness: true,
}
return driverConf.FormatDSN(), nil
}