question_create.go•30.8 kB
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/Southclaws/storyden/internal/ent/account"
"github.com/Southclaws/storyden/internal/ent/question"
"github.com/rs/xid"
)
// QuestionCreate is the builder for creating a Question entity.
type QuestionCreate struct {
config
mutation *QuestionMutation
hooks []Hook
conflict []sql.ConflictOption
}
// SetCreatedAt sets the "created_at" field.
func (_c *QuestionCreate) SetCreatedAt(v time.Time) *QuestionCreate {
_c.mutation.SetCreatedAt(v)
return _c
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *QuestionCreate) SetNillableCreatedAt(v *time.Time) *QuestionCreate {
if v != nil {
_c.SetCreatedAt(*v)
}
return _c
}
// SetIndexedAt sets the "indexed_at" field.
func (_c *QuestionCreate) SetIndexedAt(v time.Time) *QuestionCreate {
_c.mutation.SetIndexedAt(v)
return _c
}
// SetNillableIndexedAt sets the "indexed_at" field if the given value is not nil.
func (_c *QuestionCreate) SetNillableIndexedAt(v *time.Time) *QuestionCreate {
if v != nil {
_c.SetIndexedAt(*v)
}
return _c
}
// SetSlug sets the "slug" field.
func (_c *QuestionCreate) SetSlug(v string) *QuestionCreate {
_c.mutation.SetSlug(v)
return _c
}
// SetQuery sets the "query" field.
func (_c *QuestionCreate) SetQuery(v string) *QuestionCreate {
_c.mutation.SetQuery(v)
return _c
}
// SetResult sets the "result" field.
func (_c *QuestionCreate) SetResult(v string) *QuestionCreate {
_c.mutation.SetResult(v)
return _c
}
// SetMetadata sets the "metadata" field.
func (_c *QuestionCreate) SetMetadata(v map[string]interface{}) *QuestionCreate {
_c.mutation.SetMetadata(v)
return _c
}
// SetAccountID sets the "account_id" field.
func (_c *QuestionCreate) SetAccountID(v xid.ID) *QuestionCreate {
_c.mutation.SetAccountID(v)
return _c
}
// SetNillableAccountID sets the "account_id" field if the given value is not nil.
func (_c *QuestionCreate) SetNillableAccountID(v *xid.ID) *QuestionCreate {
if v != nil {
_c.SetAccountID(*v)
}
return _c
}
// SetParentQuestionID sets the "parent_question_id" field.
func (_c *QuestionCreate) SetParentQuestionID(v xid.ID) *QuestionCreate {
_c.mutation.SetParentQuestionID(v)
return _c
}
// SetNillableParentQuestionID sets the "parent_question_id" field if the given value is not nil.
func (_c *QuestionCreate) SetNillableParentQuestionID(v *xid.ID) *QuestionCreate {
if v != nil {
_c.SetParentQuestionID(*v)
}
return _c
}
// SetID sets the "id" field.
func (_c *QuestionCreate) SetID(v xid.ID) *QuestionCreate {
_c.mutation.SetID(v)
return _c
}
// SetNillableID sets the "id" field if the given value is not nil.
func (_c *QuestionCreate) SetNillableID(v *xid.ID) *QuestionCreate {
if v != nil {
_c.SetID(*v)
}
return _c
}
// SetAuthorID sets the "author" edge to the Account entity by ID.
func (_c *QuestionCreate) SetAuthorID(id xid.ID) *QuestionCreate {
_c.mutation.SetAuthorID(id)
return _c
}
// SetNillableAuthorID sets the "author" edge to the Account entity by ID if the given value is not nil.
func (_c *QuestionCreate) SetNillableAuthorID(id *xid.ID) *QuestionCreate {
if id != nil {
_c = _c.SetAuthorID(*id)
}
return _c
}
// SetAuthor sets the "author" edge to the Account entity.
func (_c *QuestionCreate) SetAuthor(v *Account) *QuestionCreate {
return _c.SetAuthorID(v.ID)
}
// SetParentID sets the "parent" edge to the Question entity by ID.
func (_c *QuestionCreate) SetParentID(id xid.ID) *QuestionCreate {
_c.mutation.SetParentID(id)
return _c
}
// SetNillableParentID sets the "parent" edge to the Question entity by ID if the given value is not nil.
func (_c *QuestionCreate) SetNillableParentID(id *xid.ID) *QuestionCreate {
if id != nil {
_c = _c.SetParentID(*id)
}
return _c
}
// SetParent sets the "parent" edge to the Question entity.
func (_c *QuestionCreate) SetParent(v *Question) *QuestionCreate {
return _c.SetParentID(v.ID)
}
// AddParentQuestionIDs adds the "parent_question" edge to the Question entity by IDs.
func (_c *QuestionCreate) AddParentQuestionIDs(ids ...xid.ID) *QuestionCreate {
_c.mutation.AddParentQuestionIDs(ids...)
return _c
}
// AddParentQuestion adds the "parent_question" edges to the Question entity.
func (_c *QuestionCreate) AddParentQuestion(v ...*Question) *QuestionCreate {
ids := make([]xid.ID, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _c.AddParentQuestionIDs(ids...)
}
// Mutation returns the QuestionMutation object of the builder.
func (_c *QuestionCreate) Mutation() *QuestionMutation {
return _c.mutation
}
// Save creates the Question in the database.
func (_c *QuestionCreate) Save(ctx context.Context) (*Question, error) {
_c.defaults()
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *QuestionCreate) SaveX(ctx context.Context) *Question {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *QuestionCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *QuestionCreate) ExecX(ctx context.Context) {
if err := _c.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (_c *QuestionCreate) defaults() {
if _, ok := _c.mutation.CreatedAt(); !ok {
v := question.DefaultCreatedAt()
_c.mutation.SetCreatedAt(v)
}
if _, ok := _c.mutation.ID(); !ok {
v := question.DefaultID()
_c.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_c *QuestionCreate) check() error {
if _, ok := _c.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Question.created_at"`)}
}
if _, ok := _c.mutation.Slug(); !ok {
return &ValidationError{Name: "slug", err: errors.New(`ent: missing required field "Question.slug"`)}
}
if _, ok := _c.mutation.Query(); !ok {
return &ValidationError{Name: "query", err: errors.New(`ent: missing required field "Question.query"`)}
}
if _, ok := _c.mutation.Result(); !ok {
return &ValidationError{Name: "result", err: errors.New(`ent: missing required field "Question.result"`)}
}
if v, ok := _c.mutation.ID(); ok {
if err := question.IDValidator(v.String()); err != nil {
return &ValidationError{Name: "id", err: fmt.Errorf(`ent: validator failed for field "Question.id": %w`, err)}
}
}
return nil
}
func (_c *QuestionCreate) sqlSave(ctx context.Context) (*Question, error) {
if err := _c.check(); err != nil {
return nil, err
}
_node, _spec := _c.createSpec()
if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
if _spec.ID.Value != nil {
if id, ok := _spec.ID.Value.(*xid.ID); ok {
_node.ID = *id
} else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
return nil, err
}
}
_c.mutation.id = &_node.ID
_c.mutation.done = true
return _node, nil
}
func (_c *QuestionCreate) createSpec() (*Question, *sqlgraph.CreateSpec) {
var (
_node = &Question{config: _c.config}
_spec = sqlgraph.NewCreateSpec(question.Table, sqlgraph.NewFieldSpec(question.FieldID, field.TypeString))
)
_spec.OnConflict = _c.conflict
if id, ok := _c.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = &id
}
if value, ok := _c.mutation.CreatedAt(); ok {
_spec.SetField(question.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := _c.mutation.IndexedAt(); ok {
_spec.SetField(question.FieldIndexedAt, field.TypeTime, value)
_node.IndexedAt = &value
}
if value, ok := _c.mutation.Slug(); ok {
_spec.SetField(question.FieldSlug, field.TypeString, value)
_node.Slug = value
}
if value, ok := _c.mutation.Query(); ok {
_spec.SetField(question.FieldQuery, field.TypeString, value)
_node.Query = value
}
if value, ok := _c.mutation.Result(); ok {
_spec.SetField(question.FieldResult, field.TypeString, value)
_node.Result = value
}
if value, ok := _c.mutation.Metadata(); ok {
_spec.SetField(question.FieldMetadata, field.TypeJSON, value)
_node.Metadata = value
}
if nodes := _c.mutation.AuthorIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: question.AuthorTable,
Columns: []string{question.AuthorColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.AccountID = nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.ParentIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: question.ParentTable,
Columns: []string{question.ParentColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(question.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.ParentQuestionID = nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.ParentQuestionIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: question.ParentQuestionTable,
Columns: []string{question.ParentQuestionColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(question.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
// client.Question.Create().
// SetCreatedAt(v).
// OnConflict(
// // Update the row with the new values
// // the was proposed for insertion.
// sql.ResolveWithNewValues(),
// ).
// // Override some of the fields with custom
// // update values.
// Update(func(u *ent.QuestionUpsert) {
// SetCreatedAt(v+v).
// }).
// Exec(ctx)
func (_c *QuestionCreate) OnConflict(opts ...sql.ConflictOption) *QuestionUpsertOne {
_c.conflict = opts
return &QuestionUpsertOne{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.Question.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *QuestionCreate) OnConflictColumns(columns ...string) *QuestionUpsertOne {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &QuestionUpsertOne{
create: _c,
}
}
type (
// QuestionUpsertOne is the builder for "upsert"-ing
// one Question node.
QuestionUpsertOne struct {
create *QuestionCreate
}
// QuestionUpsert is the "OnConflict" setter.
QuestionUpsert struct {
*sql.UpdateSet
}
)
// SetIndexedAt sets the "indexed_at" field.
func (u *QuestionUpsert) SetIndexedAt(v time.Time) *QuestionUpsert {
u.Set(question.FieldIndexedAt, v)
return u
}
// UpdateIndexedAt sets the "indexed_at" field to the value that was provided on create.
func (u *QuestionUpsert) UpdateIndexedAt() *QuestionUpsert {
u.SetExcluded(question.FieldIndexedAt)
return u
}
// ClearIndexedAt clears the value of the "indexed_at" field.
func (u *QuestionUpsert) ClearIndexedAt() *QuestionUpsert {
u.SetNull(question.FieldIndexedAt)
return u
}
// SetSlug sets the "slug" field.
func (u *QuestionUpsert) SetSlug(v string) *QuestionUpsert {
u.Set(question.FieldSlug, v)
return u
}
// UpdateSlug sets the "slug" field to the value that was provided on create.
func (u *QuestionUpsert) UpdateSlug() *QuestionUpsert {
u.SetExcluded(question.FieldSlug)
return u
}
// SetQuery sets the "query" field.
func (u *QuestionUpsert) SetQuery(v string) *QuestionUpsert {
u.Set(question.FieldQuery, v)
return u
}
// UpdateQuery sets the "query" field to the value that was provided on create.
func (u *QuestionUpsert) UpdateQuery() *QuestionUpsert {
u.SetExcluded(question.FieldQuery)
return u
}
// SetResult sets the "result" field.
func (u *QuestionUpsert) SetResult(v string) *QuestionUpsert {
u.Set(question.FieldResult, v)
return u
}
// UpdateResult sets the "result" field to the value that was provided on create.
func (u *QuestionUpsert) UpdateResult() *QuestionUpsert {
u.SetExcluded(question.FieldResult)
return u
}
// SetMetadata sets the "metadata" field.
func (u *QuestionUpsert) SetMetadata(v map[string]interface{}) *QuestionUpsert {
u.Set(question.FieldMetadata, v)
return u
}
// UpdateMetadata sets the "metadata" field to the value that was provided on create.
func (u *QuestionUpsert) UpdateMetadata() *QuestionUpsert {
u.SetExcluded(question.FieldMetadata)
return u
}
// ClearMetadata clears the value of the "metadata" field.
func (u *QuestionUpsert) ClearMetadata() *QuestionUpsert {
u.SetNull(question.FieldMetadata)
return u
}
// SetAccountID sets the "account_id" field.
func (u *QuestionUpsert) SetAccountID(v xid.ID) *QuestionUpsert {
u.Set(question.FieldAccountID, v)
return u
}
// UpdateAccountID sets the "account_id" field to the value that was provided on create.
func (u *QuestionUpsert) UpdateAccountID() *QuestionUpsert {
u.SetExcluded(question.FieldAccountID)
return u
}
// ClearAccountID clears the value of the "account_id" field.
func (u *QuestionUpsert) ClearAccountID() *QuestionUpsert {
u.SetNull(question.FieldAccountID)
return u
}
// SetParentQuestionID sets the "parent_question_id" field.
func (u *QuestionUpsert) SetParentQuestionID(v xid.ID) *QuestionUpsert {
u.Set(question.FieldParentQuestionID, v)
return u
}
// UpdateParentQuestionID sets the "parent_question_id" field to the value that was provided on create.
func (u *QuestionUpsert) UpdateParentQuestionID() *QuestionUpsert {
u.SetExcluded(question.FieldParentQuestionID)
return u
}
// ClearParentQuestionID clears the value of the "parent_question_id" field.
func (u *QuestionUpsert) ClearParentQuestionID() *QuestionUpsert {
u.SetNull(question.FieldParentQuestionID)
return u
}
// UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
// Using this option is equivalent to using:
//
// client.Question.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(question.FieldID)
// }),
// ).
// Exec(ctx)
func (u *QuestionUpsertOne) UpdateNewValues() *QuestionUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
if _, exists := u.create.mutation.ID(); exists {
s.SetIgnore(question.FieldID)
}
if _, exists := u.create.mutation.CreatedAt(); exists {
s.SetIgnore(question.FieldCreatedAt)
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.Question.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *QuestionUpsertOne) Ignore() *QuestionUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
return u
}
// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *QuestionUpsertOne) DoNothing() *QuestionUpsertOne {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the QuestionCreate.OnConflict
// documentation for more info.
func (u *QuestionUpsertOne) Update(set func(*QuestionUpsert)) *QuestionUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&QuestionUpsert{UpdateSet: update})
}))
return u
}
// SetIndexedAt sets the "indexed_at" field.
func (u *QuestionUpsertOne) SetIndexedAt(v time.Time) *QuestionUpsertOne {
return u.Update(func(s *QuestionUpsert) {
s.SetIndexedAt(v)
})
}
// UpdateIndexedAt sets the "indexed_at" field to the value that was provided on create.
func (u *QuestionUpsertOne) UpdateIndexedAt() *QuestionUpsertOne {
return u.Update(func(s *QuestionUpsert) {
s.UpdateIndexedAt()
})
}
// ClearIndexedAt clears the value of the "indexed_at" field.
func (u *QuestionUpsertOne) ClearIndexedAt() *QuestionUpsertOne {
return u.Update(func(s *QuestionUpsert) {
s.ClearIndexedAt()
})
}
// SetSlug sets the "slug" field.
func (u *QuestionUpsertOne) SetSlug(v string) *QuestionUpsertOne {
return u.Update(func(s *QuestionUpsert) {
s.SetSlug(v)
})
}
// UpdateSlug sets the "slug" field to the value that was provided on create.
func (u *QuestionUpsertOne) UpdateSlug() *QuestionUpsertOne {
return u.Update(func(s *QuestionUpsert) {
s.UpdateSlug()
})
}
// SetQuery sets the "query" field.
func (u *QuestionUpsertOne) SetQuery(v string) *QuestionUpsertOne {
return u.Update(func(s *QuestionUpsert) {
s.SetQuery(v)
})
}
// UpdateQuery sets the "query" field to the value that was provided on create.
func (u *QuestionUpsertOne) UpdateQuery() *QuestionUpsertOne {
return u.Update(func(s *QuestionUpsert) {
s.UpdateQuery()
})
}
// SetResult sets the "result" field.
func (u *QuestionUpsertOne) SetResult(v string) *QuestionUpsertOne {
return u.Update(func(s *QuestionUpsert) {
s.SetResult(v)
})
}
// UpdateResult sets the "result" field to the value that was provided on create.
func (u *QuestionUpsertOne) UpdateResult() *QuestionUpsertOne {
return u.Update(func(s *QuestionUpsert) {
s.UpdateResult()
})
}
// SetMetadata sets the "metadata" field.
func (u *QuestionUpsertOne) SetMetadata(v map[string]interface{}) *QuestionUpsertOne {
return u.Update(func(s *QuestionUpsert) {
s.SetMetadata(v)
})
}
// UpdateMetadata sets the "metadata" field to the value that was provided on create.
func (u *QuestionUpsertOne) UpdateMetadata() *QuestionUpsertOne {
return u.Update(func(s *QuestionUpsert) {
s.UpdateMetadata()
})
}
// ClearMetadata clears the value of the "metadata" field.
func (u *QuestionUpsertOne) ClearMetadata() *QuestionUpsertOne {
return u.Update(func(s *QuestionUpsert) {
s.ClearMetadata()
})
}
// SetAccountID sets the "account_id" field.
func (u *QuestionUpsertOne) SetAccountID(v xid.ID) *QuestionUpsertOne {
return u.Update(func(s *QuestionUpsert) {
s.SetAccountID(v)
})
}
// UpdateAccountID sets the "account_id" field to the value that was provided on create.
func (u *QuestionUpsertOne) UpdateAccountID() *QuestionUpsertOne {
return u.Update(func(s *QuestionUpsert) {
s.UpdateAccountID()
})
}
// ClearAccountID clears the value of the "account_id" field.
func (u *QuestionUpsertOne) ClearAccountID() *QuestionUpsertOne {
return u.Update(func(s *QuestionUpsert) {
s.ClearAccountID()
})
}
// SetParentQuestionID sets the "parent_question_id" field.
func (u *QuestionUpsertOne) SetParentQuestionID(v xid.ID) *QuestionUpsertOne {
return u.Update(func(s *QuestionUpsert) {
s.SetParentQuestionID(v)
})
}
// UpdateParentQuestionID sets the "parent_question_id" field to the value that was provided on create.
func (u *QuestionUpsertOne) UpdateParentQuestionID() *QuestionUpsertOne {
return u.Update(func(s *QuestionUpsert) {
s.UpdateParentQuestionID()
})
}
// ClearParentQuestionID clears the value of the "parent_question_id" field.
func (u *QuestionUpsertOne) ClearParentQuestionID() *QuestionUpsertOne {
return u.Update(func(s *QuestionUpsert) {
s.ClearParentQuestionID()
})
}
// Exec executes the query.
func (u *QuestionUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for QuestionCreate.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *QuestionUpsertOne) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}
// Exec executes the UPSERT query and returns the inserted/updated ID.
func (u *QuestionUpsertOne) ID(ctx context.Context) (id xid.ID, err error) {
if u.create.driver.Dialect() == dialect.MySQL {
// In case of "ON CONFLICT", there is no way to get back non-numeric ID
// fields from the database since MySQL does not support the RETURNING clause.
return id, errors.New("ent: QuestionUpsertOne.ID is not supported by MySQL driver. Use QuestionUpsertOne.Exec instead")
}
node, err := u.create.Save(ctx)
if err != nil {
return id, err
}
return node.ID, nil
}
// IDX is like ID, but panics if an error occurs.
func (u *QuestionUpsertOne) IDX(ctx context.Context) xid.ID {
id, err := u.ID(ctx)
if err != nil {
panic(err)
}
return id
}
// QuestionCreateBulk is the builder for creating many Question entities in bulk.
type QuestionCreateBulk struct {
config
err error
builders []*QuestionCreate
conflict []sql.ConflictOption
}
// Save creates the Question entities in the database.
func (_c *QuestionCreateBulk) Save(ctx context.Context) ([]*Question, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*Question, len(_c.builders))
mutators := make([]Mutator, len(_c.builders))
for i := range _c.builders {
func(i int, root context.Context) {
builder := _c.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*QuestionMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
if err := builder.check(); err != nil {
return nil, err
}
builder.mutation = mutation
var err error
nodes[i], specs[i] = builder.createSpec()
if i < len(mutators)-1 {
_, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation)
} else {
spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
spec.OnConflict = _c.conflict
// Invoke the actual operation on the latest mutation in the chain.
if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
}
}
if err != nil {
return nil, err
}
mutation.id = &nodes[i].ID
mutation.done = true
return nodes[i], nil
})
for i := len(builder.hooks) - 1; i >= 0; i-- {
mut = builder.hooks[i](mut)
}
mutators[i] = mut
}(i, ctx)
}
if len(mutators) > 0 {
if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (_c *QuestionCreateBulk) SaveX(ctx context.Context) []*Question {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *QuestionCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *QuestionCreateBulk) ExecX(ctx context.Context) {
if err := _c.Exec(ctx); err != nil {
panic(err)
}
}
// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
// client.Question.CreateBulk(builders...).
// OnConflict(
// // Update the row with the new values
// // the was proposed for insertion.
// sql.ResolveWithNewValues(),
// ).
// // Override some of the fields with custom
// // update values.
// Update(func(u *ent.QuestionUpsert) {
// SetCreatedAt(v+v).
// }).
// Exec(ctx)
func (_c *QuestionCreateBulk) OnConflict(opts ...sql.ConflictOption) *QuestionUpsertBulk {
_c.conflict = opts
return &QuestionUpsertBulk{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.Question.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *QuestionCreateBulk) OnConflictColumns(columns ...string) *QuestionUpsertBulk {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &QuestionUpsertBulk{
create: _c,
}
}
// QuestionUpsertBulk is the builder for "upsert"-ing
// a bulk of Question nodes.
type QuestionUpsertBulk struct {
create *QuestionCreateBulk
}
// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
// client.Question.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(question.FieldID)
// }),
// ).
// Exec(ctx)
func (u *QuestionUpsertBulk) UpdateNewValues() *QuestionUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
for _, b := range u.create.builders {
if _, exists := b.mutation.ID(); exists {
s.SetIgnore(question.FieldID)
}
if _, exists := b.mutation.CreatedAt(); exists {
s.SetIgnore(question.FieldCreatedAt)
}
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.Question.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *QuestionUpsertBulk) Ignore() *QuestionUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
return u
}
// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *QuestionUpsertBulk) DoNothing() *QuestionUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the QuestionCreateBulk.OnConflict
// documentation for more info.
func (u *QuestionUpsertBulk) Update(set func(*QuestionUpsert)) *QuestionUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&QuestionUpsert{UpdateSet: update})
}))
return u
}
// SetIndexedAt sets the "indexed_at" field.
func (u *QuestionUpsertBulk) SetIndexedAt(v time.Time) *QuestionUpsertBulk {
return u.Update(func(s *QuestionUpsert) {
s.SetIndexedAt(v)
})
}
// UpdateIndexedAt sets the "indexed_at" field to the value that was provided on create.
func (u *QuestionUpsertBulk) UpdateIndexedAt() *QuestionUpsertBulk {
return u.Update(func(s *QuestionUpsert) {
s.UpdateIndexedAt()
})
}
// ClearIndexedAt clears the value of the "indexed_at" field.
func (u *QuestionUpsertBulk) ClearIndexedAt() *QuestionUpsertBulk {
return u.Update(func(s *QuestionUpsert) {
s.ClearIndexedAt()
})
}
// SetSlug sets the "slug" field.
func (u *QuestionUpsertBulk) SetSlug(v string) *QuestionUpsertBulk {
return u.Update(func(s *QuestionUpsert) {
s.SetSlug(v)
})
}
// UpdateSlug sets the "slug" field to the value that was provided on create.
func (u *QuestionUpsertBulk) UpdateSlug() *QuestionUpsertBulk {
return u.Update(func(s *QuestionUpsert) {
s.UpdateSlug()
})
}
// SetQuery sets the "query" field.
func (u *QuestionUpsertBulk) SetQuery(v string) *QuestionUpsertBulk {
return u.Update(func(s *QuestionUpsert) {
s.SetQuery(v)
})
}
// UpdateQuery sets the "query" field to the value that was provided on create.
func (u *QuestionUpsertBulk) UpdateQuery() *QuestionUpsertBulk {
return u.Update(func(s *QuestionUpsert) {
s.UpdateQuery()
})
}
// SetResult sets the "result" field.
func (u *QuestionUpsertBulk) SetResult(v string) *QuestionUpsertBulk {
return u.Update(func(s *QuestionUpsert) {
s.SetResult(v)
})
}
// UpdateResult sets the "result" field to the value that was provided on create.
func (u *QuestionUpsertBulk) UpdateResult() *QuestionUpsertBulk {
return u.Update(func(s *QuestionUpsert) {
s.UpdateResult()
})
}
// SetMetadata sets the "metadata" field.
func (u *QuestionUpsertBulk) SetMetadata(v map[string]interface{}) *QuestionUpsertBulk {
return u.Update(func(s *QuestionUpsert) {
s.SetMetadata(v)
})
}
// UpdateMetadata sets the "metadata" field to the value that was provided on create.
func (u *QuestionUpsertBulk) UpdateMetadata() *QuestionUpsertBulk {
return u.Update(func(s *QuestionUpsert) {
s.UpdateMetadata()
})
}
// ClearMetadata clears the value of the "metadata" field.
func (u *QuestionUpsertBulk) ClearMetadata() *QuestionUpsertBulk {
return u.Update(func(s *QuestionUpsert) {
s.ClearMetadata()
})
}
// SetAccountID sets the "account_id" field.
func (u *QuestionUpsertBulk) SetAccountID(v xid.ID) *QuestionUpsertBulk {
return u.Update(func(s *QuestionUpsert) {
s.SetAccountID(v)
})
}
// UpdateAccountID sets the "account_id" field to the value that was provided on create.
func (u *QuestionUpsertBulk) UpdateAccountID() *QuestionUpsertBulk {
return u.Update(func(s *QuestionUpsert) {
s.UpdateAccountID()
})
}
// ClearAccountID clears the value of the "account_id" field.
func (u *QuestionUpsertBulk) ClearAccountID() *QuestionUpsertBulk {
return u.Update(func(s *QuestionUpsert) {
s.ClearAccountID()
})
}
// SetParentQuestionID sets the "parent_question_id" field.
func (u *QuestionUpsertBulk) SetParentQuestionID(v xid.ID) *QuestionUpsertBulk {
return u.Update(func(s *QuestionUpsert) {
s.SetParentQuestionID(v)
})
}
// UpdateParentQuestionID sets the "parent_question_id" field to the value that was provided on create.
func (u *QuestionUpsertBulk) UpdateParentQuestionID() *QuestionUpsertBulk {
return u.Update(func(s *QuestionUpsert) {
s.UpdateParentQuestionID()
})
}
// ClearParentQuestionID clears the value of the "parent_question_id" field.
func (u *QuestionUpsertBulk) ClearParentQuestionID() *QuestionUpsertBulk {
return u.Update(func(s *QuestionUpsert) {
s.ClearParentQuestionID()
})
}
// Exec executes the query.
func (u *QuestionUpsertBulk) Exec(ctx context.Context) error {
if u.create.err != nil {
return u.create.err
}
for i, b := range u.create.builders {
if len(b.conflict) != 0 {
return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the QuestionCreateBulk instead", i)
}
}
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for QuestionCreateBulk.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *QuestionUpsertBulk) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}