Debian Codegraph @

ent/timestamp_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/signature"
15 "github.com/in-toto/archivista/ent/timestamp"
16 )
17
18 // TimestampCreate is the builder for creating a Timestamp entity.
19 type TimestampCreate struct {
20 config
21 mutation *TimestampMutation
22 hooks []Hook
23 }
24
25 // SetType sets the "type" field.
26 func (_c *TimestampCreate) SetType(v string) *TimestampCreate {
27 _c.mutation.SetType(v)
28 return _c
29 }
30
31 // SetTimestamp sets the "timestamp" field.
32 func (_c *TimestampCreate) SetTimestamp(v time.Time) *TimestampCreate {
33 _c.mutation.SetTimestamp(v)
34 return _c
35 }
36
37 // SetID sets the "id" field.
38 func (_c *TimestampCreate) SetID(v uuid.UUID) *TimestampCreate {
39 _c.mutation.SetID(v)
40 return _c
41 }
42
43 // SetNillableID sets the "id" field if the given value is not nil.
44 func (_c *TimestampCreate) SetNillableID(v *uuid.UUID) *TimestampCreate {
45 if v != nil {
46 _c.SetID(*v)
47 }
48 return _c
49 }
50
51 // SetSignatureID sets the "signature" edge to the Signature entity by ID.
52 func (_c *TimestampCreate) SetSignatureID(id uuid.UUID) *TimestampCreate {
53 _c.mutation.SetSignatureID(id)
54 return _c
55 }
56
57 // SetNillableSignatureID sets the "signature" edge to the Signature entity by ID if the given value is not nil.
58 func (_c *TimestampCreate) SetNillableSignatureID(id *uuid.UUID) *TimestampCreate {
59 if id != nil {
60 _c = _c.SetSignatureID(*id)
61 }
62 return _c
63 }
64
65 // SetSignature sets the "signature" edge to the Signature entity.
66 func (_c *TimestampCreate) SetSignature(v *Signature) *TimestampCreate {
67 return _c.SetSignatureID(v.ID)
68 }
69
70 // Mutation returns the TimestampMutation object of the builder.
71 func (_c *TimestampCreate) Mutation() *TimestampMutation {
72 return _c.mutation
73 }
74
75 // Save creates the Timestamp in the database.
76 func (_c *TimestampCreate) Save(ctx context.Context) (*Timestamp, error) {
77 _c.defaults()
78 return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
79 }
80
81 // SaveX calls Save and panics if Save returns an error.
82 func (_c *TimestampCreate) SaveX(ctx context.Context) *Timestamp {
83 v, err := _c.Save(ctx)
84 if err != nil {
85 panic(err)
86 }
87 return v
88 }
89
90 // Exec executes the query.
91 func (_c *TimestampCreate) Exec(ctx context.Context) error {
92 _, err := _c.Save(ctx)
93 return err
94 }
95
96 // ExecX is like Exec, but panics if an error occurs.
97 func (_c *TimestampCreate) ExecX(ctx context.Context) {
98 if err := _c.Exec(ctx); err != nil {
99 panic(err)
100 }
101 }
102
103 // defaults sets the default values of the builder before save.
104 func (_c *TimestampCreate) defaults() {
105 if _, ok := _c.mutation.ID(); !ok {
106 v := timestamp.DefaultID()
107 _c.mutation.SetID(v)
108 }
109 }
110
111 // check runs all checks and user-defined validators on the builder.
112 func (_c *TimestampCreate) check() error {
113 if _, ok := _c.mutation.GetType(); !ok {
114 return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "Timestamp.type"`)}
115 }
116 if _, ok := _c.mutation.Timestamp(); !ok {
117 return &ValidationError{Name: "timestamp", err: errors.New(`ent: missing required field "Timestamp.timestamp"`)}
118 }
119 return nil
120 }
121
122 func (_c *TimestampCreate) sqlSave(ctx context.Context) (*Timestamp, error) {
123 if err := _c.check(); err != nil {
124 return nil, err
125 }
126 _node, _spec := _c.createSpec()
127 if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil {
128 if sqlgraph.IsConstraintError(err) {
129 err = &ConstraintError{msg: err.Error(), wrap: err}
130 }
131 return nil, err
132 }
133 if _spec.ID.Value != nil {
134 if id, ok := _spec.ID.Value.(*uuid.UUID); ok {
135 _node.ID = *id
136 } else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
137 return nil, err
138 }
139 }
140 _c.mutation.id = &_node.ID
141 _c.mutation.done = true
142 return _node, nil
143 }
144
145 func (_c *TimestampCreate) createSpec() (*Timestamp, *sqlgraph.CreateSpec) {
146 var (
147 _node = &Timestamp{config: _c.config}
148 _spec = sqlgraph.NewCreateSpec(timestamp.Table, sqlgraph.NewFieldSpec(timestamp.FieldID, field.TypeUUID))
149 )
150 if id, ok := _c.mutation.ID(); ok {
151 _node.ID = id
152 _spec.ID.Value = &id
153 }
154 if value, ok := _c.mutation.GetType(); ok {
155 _spec.SetField(timestamp.FieldType, field.TypeString, value)
156 _node.Type = value
157 }
158 if value, ok := _c.mutation.Timestamp(); ok {
159 _spec.SetField(timestamp.FieldTimestamp, field.TypeTime, value)
160 _node.Timestamp = value
161 }
162 if nodes := _c.mutation.SignatureIDs(); len(nodes) > 0 {
163 edge := &sqlgraph.EdgeSpec{
164 Rel: sqlgraph.M2O,
165 Inverse: true,
166 Table: timestamp.SignatureTable,
167 Columns: []string{timestamp.SignatureColumn},
168 Bidi: false,
169 Target: &sqlgraph.EdgeTarget{
170 IDSpec: sqlgraph.NewFieldSpec(signature.FieldID, field.TypeUUID),
171 },
172 }
173 for _, k := range nodes {
174 edge.Target.Nodes = append(edge.Target.Nodes, k)
175 }
176 _node.signature_timestamps = &nodes[0]
177 _spec.Edges = append(_spec.Edges, edge)
178 }
179 return _node, _spec
180 }
181
182 // TimestampCreateBulk is the builder for creating many Timestamp entities in bulk.
183 type TimestampCreateBulk struct {
184 config
185 err error
186 builders []*TimestampCreate
187 }
188
189 // Save creates the Timestamp entities in the database.
190 func (_c *TimestampCreateBulk) Save(ctx context.Context) ([]*Timestamp, error) {
191 if _c.err != nil {
192 return nil, _c.err
193 }
194 specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
195 nodes := make([]*Timestamp, len(_c.builders))
196 mutators := make([]Mutator, len(_c.builders))
197 for i := range _c.builders {
198 func(i int, root context.Context) {
199 builder := _c.builders[i]
200 builder.defaults()
201 var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
202 mutation, ok := m.(*TimestampMutation)
203 if !ok {
204 return nil, fmt.Errorf("unexpected mutation type %T", m)
205 }
206 if err := builder.check(); err != nil {
207 return nil, err
208 }
209 builder.mutation = mutation
210 var err error
211 nodes[i], specs[i] = builder.createSpec()
212 if i < len(mutators)-1 {
213 _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation)
214 } else {
215 spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
216 // Invoke the actual operation on the latest mutation in the chain.
217 if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil {
218 if sqlgraph.IsConstraintError(err) {
219 err = &ConstraintError{msg: err.Error(), wrap: err}
220 }
221 }
222 }
223 if err != nil {
224 return nil, err
225 }
226 mutation.id = &nodes[i].ID
227 mutation.done = true
228 return nodes[i], nil
229 })
230 for i := len(builder.hooks) - 1; i >= 0; i-- {
231 mut = builder.hooks[i](mut)
232 }
233 mutators[i] = mut
234 }(i, ctx)
235 }
236 if len(mutators) > 0 {
237 if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil {
238 return nil, err
239 }
240 }
241 return nodes, nil
242 }
243
244 // SaveX is like Save, but panics if an error occurs.
245 func (_c *TimestampCreateBulk) SaveX(ctx context.Context) []*Timestamp {
246 v, err := _c.Save(ctx)
247 if err != nil {
248 panic(err)
249 }
250 return v
251 }
252
253 // Exec executes the query.
254 func (_c *TimestampCreateBulk) Exec(ctx context.Context) error {
255 _, err := _c.Save(ctx)
256 return err
257 }
258
259 // ExecX is like Exec, but panics if an error occurs.
260 func (_c *TimestampCreateBulk) ExecX(ctx context.Context) {
261 if err := _c.Exec(ctx); err != nil {
262 panic(err)
263 }
264 }