-
Notifications
You must be signed in to change notification settings - Fork 2
/
imp_dic.go
118 lines (107 loc) · 2.63 KB
/
imp_dic.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
package dao
import (
"database/sql"
"fmt"
"github.com/xfyun/xns/models"
"github.com/xfyun/xns/tools/uid"
)
type idcImp struct {
*baseDao
}
func NewIdcImp(db *sql.DB, ) Idc {
idc := &idcImp{
baseDao: newBaseDao(db, &models.Idc{},ChannelIdc,TableIdc),
}
return idc
}
func (i *idcImp) GetByName(name string) (idc *models.Idc, err error) {
sqlString := fmt.Sprintf("select %s from %s where name = '%s'", i.queryFields, i.table, name)
rows, err := i.query(sqlString)
if err != nil {
return nil, err
}
idc = &models.Idc{}
err = unmarshalFromRows(rows, idc)
return
}
func (i *idcImp) GetByIdOrName(idOrName string) (idc *models.Idc, err error) {
if uid.IsUUID(idOrName) {
return i.GetById(idOrName)
}
return i.GetByName(idOrName)
}
func (i *idcImp) GetList() (idcs []*models.Idc, err error) {
sqlString := fmt.Sprintf("select %s from %s;", i.queryFields, i.table)
rows, err := i.query(sqlString)
if err != nil {
return nil, err
}
err = unmarshalFromRows(rows, &idcs)
return
}
func (i *idcImp) GetUpdates(from int) (idcs []*models.Idc, err error) {
sqlString := fmt.Sprintf("select %s from %s where update_at >= %d;", i.queryFields, i.table, from)
rows, err := i.query(sqlString)
if err != nil {
return nil, err
}
err = unmarshalFromRows(rows, &idcs)
return
}
func (i *idcImp) Create(idc *models.Idc) error {
tx, err := i.beginTx()
if err != nil {
return err
}
createBase(&idc.Base)
err = i.insert(tx, idc)
if err != nil {
return err
}
if err := addClusterEvent(tx, EventCreate, ChannelIdc, string(idc.Id)); err != nil {
return err
}
return tx.Commit()
}
func (i *idcImp) Update(id string, idc *models.Idc) error {
tx, err := i.beginTx()
if err != nil {
return err
}
updateBase(&idc.Base)
err = i.update(tx, fmt.Sprintf(" id = '%s'", id), idc)
if err != nil {
return err
}
err = addClusterEvent(tx, EventUpdate, ChannelIdc, string(id))
if err != nil {
return err
}
return tx.Commit()
}
// 逻辑删除
func (i *idcImp) Delete(id string) error {
tx, err := i.beginTx()
if err != nil {
return err
}
sqlString := fmt.Sprintf("delete from %s where id='%s'", i.table, id)
err = i.execTx(tx, sqlString)
if err != nil {
return fmt.Errorf("delete by id error,sql=%s,err=%w", sqlString, err)
}
if err = addClusterEvent(tx, EventDelete, ChannelIdc, string(id)); err != nil {
return err
}
return tx.Commit()
}
func (i *idcImp) GetById(id string) (idc *models.Idc, err error) {
sqlString := fmt.Sprintf("select %s from %s where id = '%s';", i.queryFields, i.table, id)
rows, err := i.query(sqlString)
if err != nil {
return nil, err
}
idc = &models.Idc{}
err = unmarshalFromRows(rows, idc)
return
}