forked from eaciit/orm
/
base.go
81 lines (72 loc) · 1.23 KB
/
base.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
package main
var baseGo string = `
package {0}
import (
"strings"
"github.com/eaciit/dbox"
"github.com/eaciit/orm"
"github.com/eaciit/toolkit"
)
var _dbs map[string]*orm.DataContext
func initDbs() {
if _dbs == nil {
_dbs = map[string]*orm.DataContext{}
}
}
func SetDb(conn dbox.IConnection, ids ...string) error {
initDbs()
CloseDb(ids...)
dbID := "default"
if len(ids) > 0 {
dbID = ids[0]
}
var (
_db *orm.DataContext
e bool
)
if _db, e = _dbs[dbID]; !e {
_db = orm.New(conn)
}
_dbs[dbID] = _db
return nil
}
func CloseDb(ids ...string) {
initDbs()
dbID := "default"
if len(ids) > 0 {
dbID = ids[0]
}
if _db, e := _dbs[dbID]; e {
if _db != nil {
_db.Close()
}
}
}
func DB(ids ...string) *orm.DataContext {
initDbs()
dbID := "default"
if len(ids) > 0 {
dbID = ids[0]
}
_db := _dbs[dbID]
return _db
}
func makeFindConfig(fields, orders string, skip, limit int) toolkit.M {
config := toolkit.M{}
if fields != "" {
fieldses := strings.Split(fields, ",")
config.Set("select", fieldses)
}
if orders != "" {
fieldses := strings.Split(orders, ",")
config.Set("order", fieldses)
}
if skip > 0 {
config.Set("skip", skip)
}
if limit > 0 {
config.Set("take", limit)
}
return config
}
`