forked from JonasBordewick/honua-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrules.go
294 lines (234 loc) · 6.19 KB
/
rules.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package honuadb
import (
"database/sql"
"errors"
"fmt"
"log"
"github.com/JonasBordewick/honua-db/models"
)
func (hdb *HonuaDB) AddRule(rule *models.Rule, hasID bool) (int, error) {
// First add Condition
cID, err := hdb.AddCondition(rule.Condition, hasID)
if err != nil {
return -1, err
}
var periodicTrigger sql.NullInt32 = sql.NullInt32{
Valid: !rule.EventBasedEvaluation,
Int32: int32(rule.PeriodicTrigger),
}
log.Printf("[DB.ADDRULE]: %d\n", int32(rule.PeriodicTrigger))
const query = "INSERT INTO rules(id, identity, target_id, enabled, event_based_evaluation, periodic_trigger_type, condition_id) VALUES($1, $2, $3, $4, $5, $6, $7);"
var rID int
if hasID {
rID = rule.ID
} else {
rID, err = hdb.get_rule_id(rule.Identity)
if err != nil {
hdb.DeleteCondition(cID, rule.Identity)
return -1, err
}
}
_, err = hdb.psqlDB.Exec(query, rID, rule.Identity, rule.TargetID, rule.Enabled, rule.EventBasedEvaluation, periodicTrigger, cID)
if err != nil {
hdb.DeleteCondition(cID, rule.Identity)
return -1, err
}
for _, action := range rule.ThenActions {
err = hdb.AddAction(action, hasID, true, rID)
if err != nil {
return -1, err
}
}
for _, action := range rule.ElseActions {
err = hdb.AddAction(action, hasID, false, rID)
if err != nil {
return -1, err
}
}
return rID, nil
}
func (hdb *HonuaDB) GetRules(identity string) ([]*models.Rule, error) {
const query = "SELECT * FROM rules WHERE identity=$1;"
rows, err := hdb.psqlDB.Query(query, identity)
if err != nil {
return nil, err
}
var rules []*models.Rule = []*models.Rule{}
for rows.Next() {
rule, err := hdb.make_rule(rows)
if err != nil {
rows.Close()
return nil, err
}
rules = append(rules, rule)
}
rows.Close()
return rules, nil
}
func (hdb *HonuaDB) GetRule(ruleID int, identity string) (*models.Rule, error) {
const query = "SELECT * from rules WHERE id=$1 AND identity=$2;"
rows, err := hdb.psqlDB.Query(query, ruleID, identity)
if err != nil {
return nil, err
}
var rule *models.Rule
for rows.Next() {
rule, err = hdb.make_rule(rows)
if err != nil {
return nil, err
}
}
rows.Close()
if rule == nil {
return nil, errors.New("something went wrong")
}
return rule, nil
}
func (hdb *HonuaDB) GetRuleOfTarget(targetID int, identity string) (*models.Rule, error) {
const query = "SELECT * from rules WHERE target_id=$1 AND identity=$2;"
rows, err := hdb.psqlDB.Query(query, targetID, identity)
if err != nil {
return nil, err
}
var rule *models.Rule
for rows.Next() {
rule, err = hdb.make_rule(rows)
if err != nil {
rows.Close()
return nil, err
}
}
rows.Close()
if rule == nil {
return nil, errors.New("something went wrong")
}
return rule, nil
}
func (hdb *HonuaDB) DeleteRule(ruleID int, identity string) error {
const query = "DELETE FROM rules WHERE id=$1 AND identity=$2;"
_, err := hdb.psqlDB.Exec(query, ruleID, identity)
return err
}
func (hdb *HonuaDB) GetStateOfRule(targetID int, identity string) (bool, error) {
const query = "SELECT enabled FROM rules WHERE target_id=$1 AND identity=$2;"
rows, err := hdb.psqlDB.Query(query, targetID, identity)
if err != nil {
return false, err
}
var enabled bool = false
for rows.Next() {
err := rows.Scan(&enabled)
if err != nil {
rows.Close()
return false, err
}
}
rows.Close()
return enabled, nil
}
func (hdb *HonuaDB) HasRule(targetID int32, identity string) bool {
const query = "SELECT CASE WHEN EXISTS ( SELECT * FROM rules WHERE target_id=$1 AND identity=$2) THEN true ELSE false END;"
rows, err := hdb.psqlDB.Query(query, targetID, identity)
if err != nil {
fmt.Println("There was an Error: " + err.Error())
return false
}
var state bool = false
for rows.Next() {
err = rows.Scan(&state)
if err != nil {
rows.Close()
return false
}
}
rows.Close()
return state
}
func (hdb *HonuaDB) ToggleRule(ruleID int32, state bool, identity string) error {
const query = "UPDATE rules SET enabled=$1 WHERE id=$2 AND identity=$3;"
_, err := hdb.psqlDB.Exec(query, state, ruleID, identity)
return err
}
func (hdb *HonuaDB) get_rule_id(identity string) (int, error) {
query := "SELECT CASE WHEN EXISTS ( SELECT * FROM rules WHERE identity = $1) THEN true ELSE false END"
rows, err := hdb.psqlDB.Query(query, identity)
if err != nil {
return -1, err
}
var exist_identity bool = false
for rows.Next() {
err = rows.Scan(&exist_identity)
if err != nil {
rows.Close()
return -1, err
}
}
rows.Close()
if !exist_identity {
return 0, nil
}
query = "SELECT MAX(id) FROM rules WHERE identity = $1;"
rows, err = hdb.psqlDB.Query(query, identity)
if err != nil {
return -1, err
}
var id int = -1
for rows.Next() {
err = rows.Scan(&id)
if err != nil {
rows.Close()
return -1, err
}
}
rows.Close()
if id == -1 {
return -1, errors.New("something went wrong during getting id of entity")
}
id = id + 1
return id, nil
}
func (hdb *HonuaDB) make_rule(rows *sql.Rows) (*models.Rule, error) {
var id int
var identity string
var targetID int32
var enabled bool
var eventBasedEvaluation bool
var triggerType sql.NullInt32
var conditionID int
err := rows.Scan(&id, &identity, &targetID, &enabled, &eventBasedEvaluation, &triggerType, &conditionID)
if err != nil {
return nil, err
}
condition, err := hdb.GetCondition(conditionID, identity)
if err != nil {
return nil, err
}
thenActions, elseActions, err := hdb.GetActions(identity, id)
if err != nil {
return nil, err
}
if !eventBasedEvaluation && triggerType.Valid {
return &models.Rule{
ID: id,
Identity: identity,
TargetID: int(targetID),
Enabled: enabled,
EventBasedEvaluation: false,
PeriodicTrigger: models.PeriodicTriggerType(triggerType.Int32),
Condition: condition,
ThenActions: thenActions,
ElseActions: elseActions,
}, nil
} else {
return &models.Rule{
ID: id,
Identity: identity,
TargetID: int(targetID),
Enabled: enabled,
EventBasedEvaluation: true,
Condition: condition,
ThenActions: thenActions,
ElseActions: elseActions,
}, nil
}
}