-
Notifications
You must be signed in to change notification settings - Fork 434
/
core.go
134 lines (109 loc) · 3.47 KB
/
core.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
package repository
import (
"context"
"fmt"
"github.com/uptrace/bun"
)
type QueryType string
const (
RAW QueryType = "RAW"
SELECT QueryType = "SELECT"
INSERT QueryType = "INSERT"
DELETE QueryType = "DELETE"
UPDATE QueryType = "UPDATE"
)
type Object struct {
Query string `json:"query"`
Type QueryType `json:"type"`
Params []string `json:"params"`
}
const (
ListKey = "LIST"
InsertKey = "INSERT"
DeleteKey = "DELETE"
UpdateAccountKey = "UPDATE_ACCOUNT"
UpdateAlertKey = "UPDATE_ALERT"
UpdateViewKey = "UPDATE_VIEW"
UpdateViewExcludeKey = "UPDATE_VIEW_EXCLUDE"
ReScanAccountKey = "RE_SCAN_ACCOUNT"
ResourceCountKey = "RESOURCE_COUNT"
ResourceCostSumKey = "RESOURCE_COST_SUM"
AccountsResourceCountKey = "ACCOUNTS_RESOURCE_COUNT"
RegionResourceCountKey = "REGION_RESOURCE_COUNT"
FilterResourceCountKey = "FILTER_RESOURCE_COUNT"
LocationBreakdownStatKey = "LOCATION_BREAKDOWN_STAT"
UpdateTagsKey = "UPDATE_TAGS"
ListRegionsKey = "LISST_REGIONS"
ListProvidersKey = "LIST_PROVIDERS"
ListServicesKey = "LIST_SERVICES"
ListAccountsKey = "LIST_ACCOUNTS"
ListResourceWithFilter = "LIST_RESOURCE_WITH_FILTER"
ListRelationWithFilter = "LIST_RELATION_WITH_FILTER"
ListStatsWithFilter = "LIST_STATS_WITH_FILTER"
)
func ExecuteRaw(ctx context.Context, db *bun.DB, query string, schema interface{}, additionals [][3]string) error {
if len(additionals) > 0 {
query = fmt.Sprintf("%s where", query)
}
for _, triplet := range additionals {
key, op, value := triplet[0], triplet[1], triplet[2]
query = fmt.Sprintf("%s %s %s '%s' and", query, key, op, value)
}
if len(additionals) > 0 {
query = query[:len(query)-4]
}
err := db.NewRaw(query).Scan(ctx, schema)
if err != nil {
return err
}
return nil
}
func ExecuteSelect(ctx context.Context, db *bun.DB, schema interface{}, conditions [][3]string) error {
q := db.NewSelect().Model(schema)
q = addWhereClause(q.QueryBuilder(), conditions).Unwrap().(*bun.SelectQuery)
return q.Scan(ctx, schema)
}
func ExecuteInsert(ctx context.Context, db *bun.DB, schema interface{}) (id int64, err error) {
res, err := db.NewInsert().Model(schema).Returning("id").Exec(ctx, &id)
if err != nil {
_id, err := res.LastInsertId()
if err != nil {
id = _id
}
}
return
}
func ExecuteDelete(ctx context.Context, db *bun.DB, schema interface{}, conditions [][3]string) (int64, error) {
q := db.NewDelete().Model(schema)
q = addWhereClause(q.QueryBuilder(), conditions).Unwrap().(*bun.DeleteQuery)
resp, err := q.Exec(ctx)
if err != nil {
return 0, err
}
rowsAffected, err := resp.RowsAffected()
if err != nil {
return 0, err
}
return rowsAffected, nil
}
func ExecuteUpdate(ctx context.Context, db *bun.DB, schema interface{}, columns []string, conditions [][3]string) (int64, error) {
q := db.NewUpdate().Model(schema).Column(columns...)
q = addWhereClause(q.QueryBuilder(), conditions).Unwrap().(*bun.UpdateQuery)
q = q.Returning("*")
resp, err := q.Exec(ctx)
if err != nil {
return 0, err
}
rowsAffected, err := resp.RowsAffected()
if err != nil {
return 0, err
}
return rowsAffected, nil
}
func addWhereClause(query bun.QueryBuilder, conditions [][3]string) bun.QueryBuilder {
for _, triplet := range conditions {
key, op, value := triplet[0], triplet[1], triplet[2]
query = query.Where(fmt.Sprintf("%s %s ?", key, op), value)
}
return query
}