Debian Codegraph @

ent/subject_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 "time"
10
11 "entgo.io/ent/dialect/sql/sqlgraph"
12 "entgo.io/ent/schema/field"
13 "github.com/google/uuid"
14 "github.com/in-toto/archivista/ent/statement"
15 "github.com/in-toto/archivista/ent/subject"
16 "github.com/in-toto/archivista/ent/subjectdigest"
17 )
18
19 // SubjectCreate is the builder for creating a Subject entity.
20 type SubjectCreate struct {
21 config
22 mutation *SubjectMutation
23 hooks []Hook
24 }
25
26 // SetCreatedAt sets the "created_at" field.
27 func (_c *SubjectCreate) SetCreatedAt(v time.Time) *SubjectCreate {
28 _c.mutation.SetCreatedAt(v)
29 return _c
30 }
31
32 // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
33 func (_c *SubjectCreate) SetNillableCreatedAt(v *time.Time) *SubjectCreate {
34 if v != nil {
35 _c.SetCreatedAt(*v)
36 }
37 return _c
38 }
39
40 // SetName sets the "name" field.
41 func (_c *SubjectCreate) SetName(v string) *SubjectCreate {
42 _c.mutation.SetName(v)
43 return _c
44 }
45
46 // SetID sets the "id" field.
47 func (_c *SubjectCreate) SetID(v uuid.UUID) *SubjectCreate {
48 _c.mutation.SetID(v)
49 return _c
50 }
51
52 // SetNillableID sets the "id" field if the given value is not nil.
53 func (_c *SubjectCreate) SetNillableID(v *uuid.UUID) *SubjectCreate {
54 if v != nil {
55 _c.SetID(*v)
56 }
57 return _c
58 }
59
60 // AddSubjectDigestIDs adds the "subject_digests" edge to the SubjectDigest entity by IDs.
61 func (_c *SubjectCreate) AddSubjectDigestIDs(ids ...uuid.UUID) *SubjectCreate {
62 _c.mutation.AddSubjectDigestIDs(ids...)
63 return _c
64 }
65
66 // AddSubjectDigests adds the "subject_digests" edges to the SubjectDigest entity.
67 func (_c *SubjectCreate) AddSubjectDigests(v ...*SubjectDigest) *SubjectCreate {
68 ids := make([]uuid.UUID, len(v))
69 for i := range v {
70 ids[i] = v[i].ID
71 }
72 return _c.AddSubjectDigestIDs(ids...)
73 }
74
75 // SetStatementID sets the "statement" edge to the Statement entity by ID.
76 func (_c *SubjectCreate) SetStatementID(id uuid.UUID) *SubjectCreate {
77 _c.mutation.SetStatementID(id)
78 return _c
79 }
80
81 // SetNillableStatementID sets the "statement" edge to the Statement entity by ID if the given value is not nil.
82 func (_c *SubjectCreate) SetNillableStatementID(id *uuid.UUID) *SubjectCreate {
83 if id != nil {
84 _c = _c.SetStatementID(*id)
85 }
86 return _c
87 }
88
89 // SetStatement sets the "statement" edge to the Statement entity.
90 func (_c *SubjectCreate) SetStatement(v *Statement) *SubjectCreate {
91 return _c.SetStatementID(v.ID)
92 }
93
94 // Mutation returns the SubjectMutation object of the builder.
95 func (_c *SubjectCreate) Mutation() *SubjectMutation {
96 return _c.mutation
97 }
98
99 // Save creates the Subject in the database.
100 func (_c *SubjectCreate) Save(ctx context.Context) (*Subject, error) {
101 _c.defaults()
102 return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
103 }
104
105 // SaveX calls Save and panics if Save returns an error.
106 func (_c *SubjectCreate) SaveX(ctx context.Context) *Subject {
107 v, err := _c.Save(ctx)
108 if err != nil {
109 panic(err)
110 }
111 return v
112 }
113
114 // Exec executes the query.
115 func (_c *SubjectCreate) Exec(ctx context.Context) error {
116 _, err := _c.Save(ctx)
117 return err
118 }
119
120 // ExecX is like Exec, but panics if an error occurs.
121 func (_c *SubjectCreate) ExecX(ctx context.Context) {
122 if err := _c.Exec(ctx); err != nil {
123 panic(err)
124 }
125 }
126
127 // defaults sets the default values of the builder before save.
128 func (_c *SubjectCreate) defaults() {
129 if _, ok := _c.mutation.CreatedAt(); !ok {
130 v := subject.DefaultCreatedAt()
131 _c.mutation.SetCreatedAt(v)
132 }
133 if _, ok := _c.mutation.ID(); !ok {
134 v := subject.DefaultID()
135 _c.mutation.SetID(v)
136 }
137 }
138
139 // check runs all checks and user-defined validators on the builder.
140 func (_c *SubjectCreate) check() error {
141 if _, ok := _c.mutation.Name(); !ok {
142 return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Subject.name"`)}
143 }
144 if v, ok := _c.mutation.Name(); ok {
145 if err := subject.NameValidator(v); err != nil {
146 return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Subject.name": %w`, err)}
147 }
148 }
149 return nil
150 }
151
152 func (_c *SubjectCreate) sqlSave(ctx context.Context) (*Subject, error) {
153 if err := _c.check(); err != nil {
154 return nil, err
155 }
156 _node, _spec := _c.createSpec()
157 if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil {
158 if sqlgraph.IsConstraintError(err) {
159 err = &ConstraintError{msg: err.Error(), wrap: err}
160 }
161 return nil, err
162 }
163 if _spec.ID.Value != nil {
164 if id, ok := _spec.ID.Value.(*uuid.UUID); ok {
165 _node.ID = *id
166 } else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
167 return nil, err
168 }
169 }
170 _c.mutation.id = &_node.ID
171 _c.mutation.done = true
172 return _node, nil
173 }
174
175 func (_c *SubjectCreate) createSpec() (*Subject, *sqlgraph.CreateSpec) {
176 var (
177 _node = &Subject{config: _c.config}
178 _spec = sqlgraph.NewCreateSpec(subject.Table, sqlgraph.NewFieldSpec(subject.FieldID, field.TypeUUID))
179 )
180 if id, ok := _c.mutation.ID(); ok {
181 _node.ID = id
182 _spec.ID.Value = &id
183 }
184 if value, ok := _c.mutation.CreatedAt(); ok {
185 _spec.SetField(subject.FieldCreatedAt, field.TypeTime, value)
186 _node.CreatedAt = &value
187 }
188 if value, ok := _c.mutation.Name(); ok {
189 _spec.SetField(subject.FieldName, field.TypeString, value)
190 _node.Name = value
191 }
192 if nodes := _c.mutation.SubjectDigestsIDs(); len(nodes) > 0 {
193 edge := &sqlgraph.EdgeSpec{
194 Rel: sqlgraph.O2M,
195 Inverse: false,
196 Table: subject.SubjectDigestsTable,
197 Columns: []string{subject.SubjectDigestsColumn},
198 Bidi: false,
199 Target: &sqlgraph.EdgeTarget{
200 IDSpec: sqlgraph.NewFieldSpec(subjectdigest.FieldID, field.TypeUUID),
201 },
202 }
203 for _, k := range nodes {
204 edge.Target.Nodes = append(edge.Target.Nodes, k)
205 }
206 _spec.Edges = append(_spec.Edges, edge)
207 }
208 if nodes := _c.mutation.StatementIDs(); len(nodes) > 0 {
209 edge := &sqlgraph.EdgeSpec{
210 Rel: sqlgraph.M2O,
211 Inverse: true,
212 Table: subject.StatementTable,
213 Columns: []string{subject.StatementColumn},
214 Bidi: false,
215 Target: &sqlgraph.EdgeTarget{
216 IDSpec: sqlgraph.NewFieldSpec(statement.FieldID, field.TypeUUID),
217 },
218 }
219 for _, k := range nodes {
220 edge.Target.Nodes = append(edge.Target.Nodes, k)
221 }
222 _node.statement_subjects = &nodes[0]
223 _spec.Edges = append(_spec.Edges, edge)
224 }
225 return _node, _spec
226 }
227
228 // SubjectCreateBulk is the builder for creating many Subject entities in bulk.
229 type SubjectCreateBulk struct {
230 config
231 err error
232 builders []*SubjectCreate
233 }
234
235 // Save creates the Subject entities in the database.
236 func (_c *SubjectCreateBulk) Save(ctx context.Context) ([]*Subject, error) {
237 if _c.err != nil {
238 return nil, _c.err
239 }
240 specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
241 nodes := make([]*Subject, len(_c.builders))
242 mutators := make([]Mutator, len(_c.builders))
243 for i := range _c.builders {
244 func(i int, root context.Context) {
245 builder := _c.builders[i]
246 builder.defaults()
247 var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
248 mutation, ok := m.(*SubjectMutation)
249 if !ok {
250 return nil, fmt.Errorf("unexpected mutation type %T", m)
251 }
252 if err := builder.check(); err != nil {
253 return nil, err
254 }
255 builder.mutation = mutation
256 var err error
257 nodes[i], specs[i] = builder.createSpec()
258 if i < len(mutators)-1 {
259 _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation)
260 } else {
261 spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
262 // Invoke the actual operation on the latest mutation in the chain.
263 if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil {
264 if sqlgraph.IsConstraintError(err) {
265 err = &ConstraintError{msg: err.Error(), wrap: err}
266 }
267 }
268 }
269 if err != nil {
270 return nil, err
271 }
272 mutation.id = &nodes[i].ID
273 mutation.done = true
274 return nodes[i], nil
275 })
276 for i := len(builder.hooks) - 1; i >= 0; i-- {
277 mut = builder.hooks[i](mut)
278 }
279 mutators[i] = mut
280 }(i, ctx)
281 }
282 if len(mutators) > 0 {
283 if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil {
284 return nil, err
285 }
286 }
287 return nodes, nil
288 }
289
290 // SaveX is like Save, but panics if an error occurs.
291 func (_c *SubjectCreateBulk) SaveX(ctx context.Context) []*Subject {
292 v, err := _c.Save(ctx)
293 if err != nil {
294 panic(err)
295 }
296 return v
297 }
298
299 // Exec executes the query.
300 func (_c *SubjectCreateBulk) Exec(ctx context.Context) error {
301 _, err := _c.Save(ctx)
302 return err
303 }
304
305 // ExecX is like Exec, but panics if an error occurs.
306 func (_c *SubjectCreateBulk) ExecX(ctx context.Context) {
307 if err := _c.Exec(ctx); err != nil {
308 panic(err)
309 }
310 }