-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathrule_instances.sql.go
More file actions
210 lines (189 loc) · 5.51 KB
/
Copy pathrule_instances.sql.go
File metadata and controls
210 lines (189 loc) · 5.51 KB
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
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: rule_instances.sql
package db
import (
"context"
"encoding/json"
"github.com/google/uuid"
"github.com/lib/pq"
)
const deleteNonUpdatedRules = `-- name: DeleteNonUpdatedRules :exec
DELETE FROM rule_instances
WHERE profile_id = $1
AND entity_type = $2
AND NOT id = ANY($3::UUID[])
`
type DeleteNonUpdatedRulesParams struct {
ProfileID uuid.UUID `json:"profile_id"`
EntityType Entities `json:"entity_type"`
UpdatedIds []uuid.UUID `json:"updated_ids"`
}
func (q *Queries) DeleteNonUpdatedRules(ctx context.Context, arg DeleteNonUpdatedRulesParams) error {
_, err := q.db.ExecContext(ctx, deleteNonUpdatedRules, arg.ProfileID, arg.EntityType, pq.Array(arg.UpdatedIds))
return err
}
const deleteRuleInstanceOfProfileInProject = `-- name: DeleteRuleInstanceOfProfileInProject :exec
DELETE FROM rule_instances WHERE project_id = $1 AND profile_id = $2 AND rule_type_id = $3
`
type DeleteRuleInstanceOfProfileInProjectParams struct {
ProjectID uuid.UUID `json:"project_id"`
ProfileID uuid.UUID `json:"profile_id"`
RuleTypeID uuid.UUID `json:"rule_type_id"`
}
func (q *Queries) DeleteRuleInstanceOfProfileInProject(ctx context.Context, arg DeleteRuleInstanceOfProfileInProjectParams) error {
_, err := q.db.ExecContext(ctx, deleteRuleInstanceOfProfileInProject, arg.ProjectID, arg.ProfileID, arg.RuleTypeID)
return err
}
const getRuleInstancesEntityInProjects = `-- name: GetRuleInstancesEntityInProjects :many
SELECT id, profile_id, rule_type_id, name, entity_type, def, params, created_at, updated_at, project_id FROM rule_instances
WHERE entity_type = $1
AND project_id = ANY($2::UUID[])
`
type GetRuleInstancesEntityInProjectsParams struct {
EntityType Entities `json:"entity_type"`
ProjectIds []uuid.UUID `json:"project_ids"`
}
func (q *Queries) GetRuleInstancesEntityInProjects(ctx context.Context, arg GetRuleInstancesEntityInProjectsParams) ([]RuleInstance, error) {
rows, err := q.db.QueryContext(ctx, getRuleInstancesEntityInProjects, arg.EntityType, pq.Array(arg.ProjectIds))
if err != nil {
return nil, err
}
defer rows.Close()
items := []RuleInstance{}
for rows.Next() {
var i RuleInstance
if err := rows.Scan(
&i.ID,
&i.ProfileID,
&i.RuleTypeID,
&i.Name,
&i.EntityType,
&i.Def,
&i.Params,
&i.CreatedAt,
&i.UpdatedAt,
&i.ProjectID,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getRuleInstancesForProfile = `-- name: GetRuleInstancesForProfile :many
SELECT id, profile_id, rule_type_id, name, entity_type, def, params, created_at, updated_at, project_id FROM rule_instances WHERE profile_id = $1
`
func (q *Queries) GetRuleInstancesForProfile(ctx context.Context, profileID uuid.UUID) ([]RuleInstance, error) {
rows, err := q.db.QueryContext(ctx, getRuleInstancesForProfile, profileID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []RuleInstance{}
for rows.Next() {
var i RuleInstance
if err := rows.Scan(
&i.ID,
&i.ProfileID,
&i.RuleTypeID,
&i.Name,
&i.EntityType,
&i.Def,
&i.Params,
&i.CreatedAt,
&i.UpdatedAt,
&i.ProjectID,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getRuleTypeIDByRuleNameEntityProfile = `-- name: GetRuleTypeIDByRuleNameEntityProfile :one
SELECT rule_type_id FROM rule_instances
WHERE name = $1
AND entity_type = $2
AND profile_id = $3
`
type GetRuleTypeIDByRuleNameEntityProfileParams struct {
Name string `json:"name"`
EntityType Entities `json:"entity_type"`
ProfileID uuid.UUID `json:"profile_id"`
}
// intended as a temporary transition query
// this will be removed once rule_instances is used consistently in the engine
func (q *Queries) GetRuleTypeIDByRuleNameEntityProfile(ctx context.Context, arg GetRuleTypeIDByRuleNameEntityProfileParams) (uuid.UUID, error) {
row := q.db.QueryRowContext(ctx, getRuleTypeIDByRuleNameEntityProfile, arg.Name, arg.EntityType, arg.ProfileID)
var rule_type_id uuid.UUID
err := row.Scan(&rule_type_id)
return rule_type_id, err
}
const upsertRuleInstance = `-- name: UpsertRuleInstance :one
INSERT INTO rule_instances (
profile_id,
rule_type_id,
name,
entity_type,
def,
params,
project_id,
created_at,
updated_at
) VALUES(
$1,
$2,
$3,
$4,
$5,
$6,
$7,
NOW(),
NOW()
)
ON CONFLICT (profile_id, entity_type, lower(name)) DO UPDATE SET
rule_type_id = $2,
def = $5,
params = $6,
updated_at = NOW()
RETURNING id
`
type UpsertRuleInstanceParams struct {
ProfileID uuid.UUID `json:"profile_id"`
RuleTypeID uuid.UUID `json:"rule_type_id"`
Name string `json:"name"`
EntityType Entities `json:"entity_type"`
Def json.RawMessage `json:"def"`
Params json.RawMessage `json:"params"`
ProjectID uuid.UUID `json:"project_id"`
}
// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors
// SPDX-License-Identifier: Apache-2.0
func (q *Queries) UpsertRuleInstance(ctx context.Context, arg UpsertRuleInstanceParams) (uuid.UUID, error) {
row := q.db.QueryRowContext(ctx, upsertRuleInstance,
arg.ProfileID,
arg.RuleTypeID,
arg.Name,
arg.EntityType,
arg.Def,
arg.Params,
arg.ProjectID,
)
var id uuid.UUID
err := row.Scan(&id)
return id, err
}