Debian Codegraph @

ent/statement_create.go

raw
1 // Code generated by ent, DO NOT EDIT.
2
3 package ent
4
5 import (
6 "context"
7 "errors"
8 "fmt"
9
10 "entgo.io/ent/dialect/sql/sqlgraph"
11 "entgo.io/ent/schema/field"
12 "github.com/google/uuid"
13 "github.com/in-toto/archivista/ent/attestationcollection"
14 "github.com/in-toto/archivista/ent/attestationpolicy"
15 "github.com/in-toto/archivista/ent/dsse"
16 "github.com/in-toto/archivista/ent/statement"
17 "github.com/in-toto/archivista/ent/subject"
18 )
19
20 // StatementCreate is the builder for creating a Statement entity.
21 type StatementCreate struct {
22 config
23 mutation *StatementMutation
24 hooks []Hook
25 }
26
27 // SetPredicate sets the "predicate" field.
28 func (_c *StatementCreate) SetPredicate(v string) *StatementCreate {
29 _c.mutation.SetPredicate(v)
30 return _c
31 }
32
33 // SetID sets the "id" field.
34 func (_c *StatementCreate) SetID(v uuid.UUID) *StatementCreate {
35 _c.mutation.SetID(v)
36 return _c
37 }
38
39 // SetNillableID sets the "id" field if the given value is not nil.
40 func (_c *StatementCreate) SetNillableID(v *uuid.UUID) *StatementCreate {
41 if v != nil {
42 _c.SetID(*v)
43 }
44 return _c
45 }
46
47 // AddSubjectIDs adds the "subjects" edge to the Subject entity by IDs.
48 func (_c *StatementCreate) AddSubjectIDs(ids ...uuid.UUID) *StatementCreate {
49 _c.mutation.AddSubjectIDs(ids...)
50 return _c
51 }
52
53 // AddSubjects adds the "subjects" edges to the Subject entity.
54 func (_c *StatementCreate) AddSubjects(v ...*Subject) *StatementCreate {
55 ids := make([]uuid.UUID, len(v))
56 for i := range v {
57 ids[i] = v[i].ID
58 }
59 return _c.AddSubjectIDs(ids...)
60 }
61
62 // SetPolicyID sets the "policy" edge to the AttestationPolicy entity by ID.
63 func (_c *StatementCreate) SetPolicyID(id uuid.UUID) *StatementCreate {
64 _c.mutation.SetPolicyID(id)
65 return _c
66 }
67
68 // SetNillablePolicyID sets the "policy" edge to the AttestationPolicy entity by ID if the given value is not nil.
69 func (_c *StatementCreate) SetNillablePolicyID(id *uuid.UUID) *StatementCreate {
70 if id != nil {
71 _c = _c.SetPolicyID(*id)
72 }
73 return _c
74 }
75
76 // SetPolicy sets the "policy" edge to the AttestationPolicy entity.
77 func (_c *StatementCreate) SetPolicy(v *AttestationPolicy) *StatementCreate {
78 return _c.SetPolicyID(v.ID)
79 }
80
81 // SetAttestationCollectionsID sets the "attestation_collections" edge to the AttestationCollection entity by ID.
82 func (_c *StatementCreate) SetAttestationCollectionsID(id uuid.UUID) *StatementCreate {
83 _c.mutation.SetAttestationCollectionsID(id)
84 return _c
85 }
86
87 // SetNillableAttestationCollectionsID sets the "attestation_collections" edge to the AttestationCollection entity by ID if the given value is not nil.
88 func (_c *StatementCreate) SetNillableAttestationCollectionsID(id *uuid.UUID) *StatementCreate {
89 if id != nil {
90 _c = _c.SetAttestationCollectionsID(*id)
91 }
92 return _c
93 }
94
95 // SetAttestationCollections sets the "attestation_collections" edge to the AttestationCollection entity.
96 func (_c *StatementCreate) SetAttestationCollections(v *AttestationCollection) *StatementCreate {
97 return _c.SetAttestationCollectionsID(v.ID)
98 }
99
100 // AddDsseIDs adds the "dsse" edge to the Dsse entity by IDs.
101 func (_c *StatementCreate) AddDsseIDs(ids ...uuid.UUID) *StatementCreate {
102 _c.mutation.AddDsseIDs(ids...)
103 return _c
104 }
105
106 // AddDsse adds the "dsse" edges to the Dsse entity.
107 func (_c *StatementCreate) AddDsse(v ...*Dsse) *StatementCreate {
108 ids := make([]uuid.UUID, len(v))
109 for i := range v {
110 ids[i] = v[i].ID
111 }
112 return _c.AddDsseIDs(ids...)
113 }
114
115 // Mutation returns the StatementMutation object of the builder.
116 func (_c *StatementCreate) Mutation() *StatementMutation {
117 return _c.mutation
118 }
119
120 // Save creates the Statement in the database.
121 func (_c *StatementCreate) Save(ctx context.Context) (*Statement, error) {
122 _c.defaults()
123 return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
124 }
125
126 // SaveX calls Save and panics if Save returns an error.
127 func (_c *StatementCreate) SaveX(ctx context.Context) *Statement {
128 v, err := _c.Save(ctx)
129 if err != nil {
130 panic(err)
131 }
132 return v
133 }
134
135 // Exec executes the query.
136 func (_c *StatementCreate) Exec(ctx context.Context) error {
137 _, err := _c.Save(ctx)
138 return err
139 }
140
141 // ExecX is like Exec, but panics if an error occurs.
142 func (_c *StatementCreate) ExecX(ctx context.Context) {
143 if err := _c.Exec(ctx); err != nil {
144 panic(err)
145 }
146 }
147
148 // defaults sets the default values of the builder before save.
149 func (_c *StatementCreate) defaults() {
150 if _, ok := _c.mutation.ID(); !ok {
151 v := statement.DefaultID()
152 _c.mutation.SetID(v)
153 }
154 }
155
156 // check runs all checks and user-defined validators on the builder.
157 func (_c *StatementCreate) check() error {
158 if _, ok := _c.mutation.Predicate(); !ok {
159 return &ValidationError{Name: "predicate", err: errors.New(`ent: missing required field "Statement.predicate"`)}
160 }
161 if v, ok := _c.mutation.Predicate(); ok {
162 if err := statement.PredicateValidator(v); err != nil {
163 return &ValidationError{Name: "predicate", err: fmt.Errorf(`ent: validator failed for field "Statement.predicate": %w`, err)}
164 }
165 }
166 return nil
167 }
168
169 func (_c *StatementCreate) sqlSave(ctx context.Context) (*Statement, error) {
170 if err := _c.check(); err != nil {
171 return nil, err
172 }
173 _node, _spec := _c.createSpec()
174 if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil {
175 if sqlgraph.IsConstraintError(err) {
176 err = &ConstraintError{msg: err.Error(), wrap: err}
177 }
178 return nil, err
179 }
180 if _spec.ID.Value != nil {
181 if id, ok := _spec.ID.Value.(*uuid.UUID); ok {
182 _node.ID = *id
183 } else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
184 return nil, err
185 }
186 }
187 _c.mutation.id = &_node.ID
188 _c.mutation.done = true
189 return _node, nil
190 }
191
192 func (_c *StatementCreate) createSpec() (*Statement, *sqlgraph.CreateSpec) {
193 var (
194 _node = &Statement{config: _c.config}
195 _spec = sqlgraph.NewCreateSpec(statement.Table, sqlgraph.NewFieldSpec(statement.FieldID, field.TypeUUID))
196 )
197 if id, ok := _c.mutation.ID(); ok {
198 _node.ID = id
199 _spec.ID.Value = &id
200 }
201 if value, ok := _c.mutation.Predicate(); ok {
202 _spec.SetField(statement.FieldPredicate, field.TypeString, value)
203 _node.Predicate = value
204 }
205 if nodes := _c.mutation.SubjectsIDs(); len(nodes) > 0 {
206 edge := &sqlgraph.EdgeSpec{
207 Rel: sqlgraph.O2M,
208 Inverse: false,
209 Table: statement.SubjectsTable,
210 Columns: []string{statement.SubjectsColumn},
211 Bidi: false,
212 Target: &sqlgraph.EdgeTarget{
213 IDSpec: sqlgraph.NewFieldSpec(subject.FieldID, field.TypeUUID),
214 },
215 }
216 for _, k := range nodes {
217 edge.Target.Nodes = append(edge.Target.Nodes, k)
218 }
219 _spec.Edges = append(_spec.Edges, edge)
220 }
221 if nodes := _c.mutation.PolicyIDs(); len(nodes) > 0 {
222 edge := &sqlgraph.EdgeSpec{
223 Rel: sqlgraph.O2O,
224 Inverse: false,
225 Table: statement.PolicyTable,
226 Columns: []string{statement.PolicyColumn},
227 Bidi: false,
228 Target: &sqlgraph.EdgeTarget{
229 IDSpec: sqlgraph.NewFieldSpec(attestationpolicy.FieldID, field.TypeUUID),
230 },
231 }
232 for _, k := range nodes {
233 edge.Target.Nodes = append(edge.Target.Nodes, k)
234 }
235 _spec.Edges = append(_spec.Edges, edge)
236 }
237 if nodes := _c.mutation.AttestationCollectionsIDs(); len(nodes) > 0 {
238 edge := &sqlgraph.EdgeSpec{
239 Rel: sqlgraph.O2O,
240 Inverse: false,
241 Table: statement.AttestationCollectionsTable,
242 Columns: []string{statement.AttestationCollectionsColumn},
243 Bidi: false,
244 Target: &sqlgraph.EdgeTarget{
245 IDSpec: sqlgraph.NewFieldSpec(attestationcollection.FieldID, field.TypeUUID),
246 },
247 }
248 for _, k := range nodes {
249 edge.Target.Nodes = append(edge.Target.Nodes, k)
250 }
251 _spec.Edges = append(_spec.Edges, edge)
252 }
253 if nodes := _c.mutation.DsseIDs(); len(nodes) > 0 {
254 edge := &sqlgraph.EdgeSpec{
255 Rel: sqlgraph.O2M,
256 Inverse: true,
257 Table: statement.DsseTable,
258 Columns: []string{statement.DsseColumn},
259 Bidi: false,
260 Target: &sqlgraph.EdgeTarget{
261 IDSpec: sqlgraph.NewFieldSpec(dsse.FieldID, field.TypeUUID),
262 },
263 }
264 for _, k := range nodes {
265 edge.Target.Nodes = append(edge.Target.Nodes, k)
266 }
267 _spec.Edges = append(_spec.Edges, edge)
268 }
269 return _node, _spec
270 }
271
272 // StatementCreateBulk is the builder for creating many Statement entities in bulk.
273 type StatementCreateBulk struct {
274 config
275 err error
276 builders []*StatementCreate
277 }
278
279 // Save creates the Statement entities in the database.
280 func (_c *StatementCreateBulk) Save(ctx context.Context) ([]*Statement, error) {
281 if _c.err != nil {
282 return nil, _c.err
283 }
284 specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
285 nodes := make([]*Statement, len(_c.builders))
286 mutators := make([]Mutator, len(_c.builders))
287 for i := range _c.builders {
288 func(i int, root context.Context) {
289 builder := _c.builders[i]
290 builder.defaults()
291 var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
292 mutation, ok := m.(*StatementMutation)
293 if !ok {
294 return nil, fmt.Errorf("unexpected mutation type %T", m)
295 }
296 if err := builder.check(); err != nil {
297 return nil, err
298 }
299 builder.mutation = mutation
300 var err error
301 nodes[i], specs[i] = builder.createSpec()
302 if i < len(mutators)-1 {
303 _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation)
304 } else {
305 spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
306 // Invoke the actual operation on the latest mutation in the chain.
307 if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil {
308 if sqlgraph.IsConstraintError(err) {
309 err = &ConstraintError{msg: err.Error(), wrap: err}
310 }
311 }
312 }
313 if err != nil {
314 return nil, err
315 }
316 mutation.id = &nodes[i].ID
317 mutation.done = true
318 return nodes[i], nil
319 })
320 for i := len(builder.hooks) - 1; i >= 0; i-- {
321 mut = builder.hooks[i](mut)
322 }
323 mutators[i] = mut
324 }(i, ctx)
325 }
326 if len(mutators) > 0 {
327 if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil {
328 return nil, err
329 }
330 }
331 return nodes, nil
332 }
333
334 // SaveX is like Save, but panics if an error occurs.
335 func (_c *StatementCreateBulk) SaveX(ctx context.Context) []*Statement {
336 v, err := _c.Save(ctx)
337 if err != nil {
338 panic(err)
339 }
340 return v
341 }
342
343 // Exec executes the query.
344 func (_c *StatementCreateBulk) Exec(ctx context.Context) error {
345 _, err := _c.Save(ctx)
346 return err
347 }
348
349 // ExecX is like Exec, but panics if an error occurs.
350 func (_c *StatementCreateBulk) ExecX(ctx context.Context) {
351 if err := _c.Exec(ctx); err != nil {
352 panic(err)
353 }
354 }