-
Notifications
You must be signed in to change notification settings - Fork 0
/
gist.go
44 lines (34 loc) · 1.4 KB
/
gist.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
// Package sql
package sql
import (
"context"
"github.com/jmoiron/sqlx"
"gorm.io/gorm"
)
// Gist Возвращается настроенный и готовый к работе интерфейс подключения к базе данных.
func (db *Implementation) Gist() Interface { return db.getParent() }
// Gorm Возвращается настроенный и готовый к работе объект ORM gorm.io/gorm.
func (db *Implementation) Gorm(opts ...*Option) (ret *gorm.DB) {
var n int
ret = db.getParent().GormDB()
for n = range opts {
if opts[n] != nil && opts[n].ctx != nil {
ret = ret.Session(&gorm.Session{Context: opts[n].ctx})
}
}
return
}
// Sqlx Настроенный и готовый к работе объект обёртки над соединением с БД github.com/jmoiron/sqlx.
func (db *Implementation) Sqlx() *sqlx.DB { return db.getParent().SqlxDB() }
// Возвращает объект родителя, с запоминанием объекта.
func (db *Implementation) getParent() Interface {
if db.parent != nil {
return db.parent
}
db.parent = Get()
return db.parent
}
// OptionSilent Полное отключение логирования запросов к базе данных.
func (db *Implementation) OptionSilent() *Option {
return &Option{ctx: context.WithValue(context.Background(), keyContextLogLevel, keyLogSilent)}
}