/
database.go
93 lines (76 loc) · 1.43 KB
/
database.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
package database
import (
"gorm.io/gorm"
"github.com/deatil/lakego-doak/lakego/database/interfaces"
)
/**
* 使用
*/
func New(driver interfaces.Driver, conf ...ConfigMap) *Database {
d := &Database{
driver: driver,
}
if len(conf) > 0{
d.config = conf[0]
}
return d
}
type (
// 配置 map
ConfigMap = map[string]any
)
/**
* 数据库
*
* @create 2021-9-15
* @author deatil
*/
type Database struct {
// 配置
config ConfigMap
// 驱动
driver interfaces.Driver
}
// 设置配置
func (this *Database) WithConfig(config ConfigMap) interfaces.Database {
this.config = config
return this
}
// 获取配置
func (this *Database) GetConfig(name string) any {
if data, ok := this.config[name]; ok {
return data
}
return nil
}
/**
* 设置驱动
*/
func (this *Database) WithDriver(driver interfaces.Driver) interfaces.Database {
this.driver = driver
return this
}
/**
* 获取驱动
*/
func (this *Database) GetDriver() interfaces.Driver {
return this.driver
}
/**
* 获取数据库连接对象db
*/
func (this *Database) GetConnection() *gorm.DB {
return this.driver.GetConnection()
}
/**
* 获取数据库连接对象db,带debug
*/
func (this *Database) GetConnectionWithDebug() *gorm.DB {
return this.driver.GetConnectionWithDebug()
}
/**
* 关闭连接
*/
func (this *Database) Close() {
this.driver.Close()
}