/
base.go
130 lines (105 loc) · 3.25 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
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
120
121
122
123
124
125
126
127
128
129
package daos
import (
"github.com/go-ozzo/ozzo-dbx"
"database/sql"
"strings"
"strconv"
"errors"
"encoding/json"
"github.com/nvwa-io/nvwa-io/nvwa-server/libs"
)
const (
ENABLED = 1
DELETED = 2
)
type BaseDao struct {
Self interface{}
}
func (t *BaseDao) insert(table string, p dbx.Params) (sql.Result, error) {
if _, ok := p["enabled"]; !ok {
p["enabled"] = ENABLED
}
p["ctime"] = libs.GetNow()
p["utime"] = p["ctime"]
return GetDb().Insert(table, p).Execute()
}
// v 是简单可以构成 key:value Entity
func (t *BaseDao) insertEntity(table string, v interface{}) (sql.Result, error) {
byteV, err := json.Marshal(v)
if err != nil {
return nil, err
}
p := dbx.Params{}
err = json.Unmarshal(byteV, &p)
if err != nil {
return nil, err
}
p["enabled"] = ENABLED
p["ctime"] = libs.GetNow()
p["utime"] = p["ctime"]
return t.insert(table, p)
}
func (t *BaseDao) updateById(table string, id int64, p dbx.Params) (sql.Result, error) {
return GetDb().Update(table, p, dbx.HashExp{"id": id}).Execute()
}
func (t *BaseDao) updateByHashExp(table string, p dbx.Params, c dbx.HashExp) (sql.Result, error) {
return GetDb().Update(table, p, c).Execute()
}
func (t *BaseDao) logicDelById(table string, id int64) (sql.Result, error) {
return t.updateById(table, id, dbx.Params{"enabled": DELETED})
}
func (t *BaseDao) getById(table string, id int64, o interface{}, col ...string) error {
// select
columns := make([]string, 0)
if len(col) > 0 {
columns = t._parseSelectStr(col[0])
}
return GetDb().Select(columns...).From(table).Where(dbx.HashExp{"id": id, "enabled": ENABLED}).One(o)
}
func (t *BaseDao) getOneByHashExp(table string, p dbx.HashExp, o interface{}, col ...string) error {
p["enabled"] = ENABLED
// select
columns := make([]string, 0)
if len(col) > 0 {
columns = t._parseSelectStr(col[0])
}
return GetDb().Select(columns...).From(table).Where(p).One(o)
}
func (t *BaseDao) getAllByHashExp(table string, p dbx.HashExp, s interface{}) error {
p["enabled"] = ENABLED
return GetDb().Select("*").From(table).Where(p).All(s)
}
func (t *BaseDao) getAllByIdsInt64(table string, ids []int64, o interface{}, col ...string) error {
// coz: not support HashExp []int64
c := make([]interface{}, 0)
columns := make([]string, 0)
if len(col) > 0 {
columns = t._parseSelectStr(col[0])
}
for _, v := range ids {
c = append(c, v)
}
return GetDb().Select(columns...).From(table).Where(dbx.HashExp{"id": c}).All(o)
}
// 获取总数
func (t *BaseDao) count(table string, exp dbx.HashExp) (int, error) {
if _, ok := exp["enabled"]; !ok {
exp["enabled"] = ENABLED
}
var ret dbx.NullStringMap
err := GetDb().Select("count(*) as total").From(table).Where(exp).One(&ret)
if err != nil {
return 0, err
}
if !ret["total"].Valid {
return 0, errors.New("查询总数失败")
}
return strconv.Atoi(ret["total"].String)
}
func (t *BaseDao) _parseSelectStr(str string) []string {
columns := strings.Split(str, ",")
for i, v := range columns {
columns[i] = strings.Trim(v, " ")
}
return columns
}