Files
upstream-ent/entc/integration/edgeschema/ent/tweet_create.go
upstream-mirror 4b3ad0b611
Some checks failed
Atlas CI Public / lint (push) Failing after 13s
Atlas CI Public / sync (push) Has been skipped
snapshot of https://github.com/ent/ent.git (master, 2026-09-15; blobs > 131072B omitted)
2026-09-15 22:12:36 +05:00

634 lines
17 KiB
Go

// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/entc/integration/edgeschema/ent/tag"
"entgo.io/ent/entc/integration/edgeschema/ent/tweet"
"entgo.io/ent/entc/integration/edgeschema/ent/tweettag"
"entgo.io/ent/entc/integration/edgeschema/ent/user"
"entgo.io/ent/entc/integration/edgeschema/ent/usertweet"
"entgo.io/ent/schema/field"
"github.com/google/uuid"
)
// TweetCreate is the builder for creating a Tweet entity.
type TweetCreate struct {
config
mutation *TweetMutation
hooks []Hook
conflict []sql.ConflictOption
}
// SetText sets the "text" field.
func (_c *TweetCreate) SetText(v string) *TweetCreate {
_c.mutation.SetText(v)
return _c
}
// AddLikedUserIDs adds the "liked_users" edge to the User entity by IDs.
func (_c *TweetCreate) AddLikedUserIDs(ids ...int) *TweetCreate {
_c.mutation.AddLikedUserIDs(ids...)
return _c
}
// AddLikedUsers adds the "liked_users" edges to the User entity.
func (_c *TweetCreate) AddLikedUsers(v ...*User) *TweetCreate {
ids := make([]int, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _c.AddLikedUserIDs(ids...)
}
// AddUserIDs adds the "user" edge to the User entity by IDs.
func (_c *TweetCreate) AddUserIDs(ids ...int) *TweetCreate {
_c.mutation.AddUserIDs(ids...)
return _c
}
// AddUser adds the "user" edges to the User entity.
func (_c *TweetCreate) AddUser(v ...*User) *TweetCreate {
ids := make([]int, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _c.AddUserIDs(ids...)
}
// AddTagIDs adds the "tags" edge to the Tag entity by IDs.
func (_c *TweetCreate) AddTagIDs(ids ...int) *TweetCreate {
_c.mutation.AddTagIDs(ids...)
return _c
}
// AddTags adds the "tags" edges to the Tag entity.
func (_c *TweetCreate) AddTags(v ...*Tag) *TweetCreate {
ids := make([]int, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _c.AddTagIDs(ids...)
}
// AddTweetUserIDs adds the "tweet_user" edge to the UserTweet entity by IDs.
func (_c *TweetCreate) AddTweetUserIDs(ids ...int) *TweetCreate {
_c.mutation.AddTweetUserIDs(ids...)
return _c
}
// AddTweetUser adds the "tweet_user" edges to the UserTweet entity.
func (_c *TweetCreate) AddTweetUser(v ...*UserTweet) *TweetCreate {
ids := make([]int, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _c.AddTweetUserIDs(ids...)
}
// AddTweetTagIDs adds the "tweet_tags" edge to the TweetTag entity by IDs.
func (_c *TweetCreate) AddTweetTagIDs(ids ...uuid.UUID) *TweetCreate {
_c.mutation.AddTweetTagIDs(ids...)
return _c
}
// AddTweetTags adds the "tweet_tags" edges to the TweetTag entity.
func (_c *TweetCreate) AddTweetTags(v ...*TweetTag) *TweetCreate {
ids := make([]uuid.UUID, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _c.AddTweetTagIDs(ids...)
}
// Mutation returns the TweetMutation object of the builder.
func (_c *TweetCreate) Mutation() *TweetMutation {
return _c.mutation
}
// Save creates the Tweet in the database.
func (_c *TweetCreate) Save(ctx context.Context) (*Tweet, error) {
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *TweetCreate) SaveX(ctx context.Context) *Tweet {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *TweetCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *TweetCreate) ExecX(ctx context.Context) {
if err := _c.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (_c *TweetCreate) check() error {
if _, ok := _c.mutation.Text(); !ok {
return &ValidationError{Name: "text", err: errors.New(`ent: missing required field "Tweet.text"`)}
}
return nil
}
func (_c *TweetCreate) sqlSave(ctx context.Context) (*Tweet, 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
}
id := _spec.ID.Value.(int64)
_node.ID = int(id)
_c.mutation.id = &_node.ID
_c.mutation.done = true
return _node, nil
}
func (_c *TweetCreate) createSpec() (*Tweet, *sqlgraph.CreateSpec) {
var (
_node = &Tweet{config: _c.config}
_spec = sqlgraph.NewCreateSpec(tweet.Table, sqlgraph.NewFieldSpec(tweet.FieldID, field.TypeInt))
)
_spec.OnConflict = _c.conflict
if value, ok := _c.mutation.Text(); ok {
_spec.SetField(tweet.FieldText, field.TypeString, value)
_node.Text = value
}
if nodes := _c.mutation.LikedUsersIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: true,
Table: tweet.LikedUsersTable,
Columns: tweet.LikedUsersPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
createE := &TweetLikeCreate{config: _c.config, mutation: newTweetLikeMutation(_c.config, OpCreate)}
_ = createE.defaults()
_, specE := createE.createSpec()
edge.Target.Fields = specE.Fields
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.UserIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: true,
Table: tweet.UserTable,
Columns: tweet.UserPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
createE := &UserTweetCreate{config: _c.config, mutation: newUserTweetMutation(_c.config, OpCreate)}
createE.defaults()
_, specE := createE.createSpec()
edge.Target.Fields = specE.Fields
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.TagsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: true,
Table: tweet.TagsTable,
Columns: tweet.TagsPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(tag.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
createE := &TweetTagCreate{config: _c.config, mutation: newTweetTagMutation(_c.config, OpCreate)}
createE.defaults()
_, specE := createE.createSpec()
edge.Target.Fields = specE.Fields
if specE.ID.Value != nil {
edge.Target.Fields = append(edge.Target.Fields, specE.ID)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.TweetUserIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: true,
Table: tweet.TweetUserTable,
Columns: []string{tweet.TweetUserColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(usertweet.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.TweetTagsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: true,
Table: tweet.TweetTagsTable,
Columns: []string{tweet.TweetTagsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(tweettag.FieldID, field.TypeUUID),
},
}
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.Tweet.Create().
// SetText(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.TweetUpsert) {
// SetText(v+v).
// }).
// Exec(ctx)
func (_c *TweetCreate) OnConflict(opts ...sql.ConflictOption) *TweetUpsertOne {
_c.conflict = opts
return &TweetUpsertOne{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.Tweet.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *TweetCreate) OnConflictColumns(columns ...string) *TweetUpsertOne {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &TweetUpsertOne{
create: _c,
}
}
type (
// TweetUpsertOne is the builder for "upsert"-ing
// one Tweet node.
TweetUpsertOne struct {
create *TweetCreate
}
// TweetUpsert is the "OnConflict" setter.
TweetUpsert struct {
*sql.UpdateSet
}
)
// SetText sets the "text" field.
func (u *TweetUpsert) SetText(v string) *TweetUpsert {
u.Set(tweet.FieldText, v)
return u
}
// UpdateText sets the "text" field to the value that was provided on create.
func (u *TweetUpsert) UpdateText() *TweetUpsert {
u.SetExcluded(tweet.FieldText)
return u
}
// UpdateNewValues updates the mutable fields using the new values that were set on create.
// Using this option is equivalent to using:
//
// client.Tweet.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// ).
// Exec(ctx)
func (u *TweetUpsertOne) UpdateNewValues() *TweetUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.Tweet.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *TweetUpsertOne) Ignore() *TweetUpsertOne {
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 *TweetUpsertOne) DoNothing() *TweetUpsertOne {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the TweetCreate.OnConflict
// documentation for more info.
func (u *TweetUpsertOne) Update(set func(*TweetUpsert)) *TweetUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&TweetUpsert{UpdateSet: update})
}))
return u
}
// SetText sets the "text" field.
func (u *TweetUpsertOne) SetText(v string) *TweetUpsertOne {
return u.Update(func(s *TweetUpsert) {
s.SetText(v)
})
}
// UpdateText sets the "text" field to the value that was provided on create.
func (u *TweetUpsertOne) UpdateText() *TweetUpsertOne {
return u.Update(func(s *TweetUpsert) {
s.UpdateText()
})
}
// Exec executes the query.
func (u *TweetUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for TweetCreate.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *TweetUpsertOne) 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 *TweetUpsertOne) ID(ctx context.Context) (id int, err error) {
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 *TweetUpsertOne) IDX(ctx context.Context) int {
id, err := u.ID(ctx)
if err != nil {
panic(err)
}
return id
}
// TweetCreateBulk is the builder for creating many Tweet entities in bulk.
type TweetCreateBulk struct {
config
err error
builders []*TweetCreate
conflict []sql.ConflictOption
}
// Save creates the Tweet entities in the database.
func (_c *TweetCreateBulk) Save(ctx context.Context) ([]*Tweet, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*Tweet, len(_c.builders))
mutators := make([]Mutator, len(_c.builders))
for i := range _c.builders {
func(i int, root context.Context) {
builder := _c.builders[i]
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*TweetMutation)
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
if specs[i].ID.Value != nil {
id := specs[i].ID.Value.(int64)
nodes[i].ID = int(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 *TweetCreateBulk) SaveX(ctx context.Context) []*Tweet {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *TweetCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *TweetCreateBulk) 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.Tweet.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.TweetUpsert) {
// SetText(v+v).
// }).
// Exec(ctx)
func (_c *TweetCreateBulk) OnConflict(opts ...sql.ConflictOption) *TweetUpsertBulk {
_c.conflict = opts
return &TweetUpsertBulk{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.Tweet.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *TweetCreateBulk) OnConflictColumns(columns ...string) *TweetUpsertBulk {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &TweetUpsertBulk{
create: _c,
}
}
// TweetUpsertBulk is the builder for "upsert"-ing
// a bulk of Tweet nodes.
type TweetUpsertBulk struct {
create *TweetCreateBulk
}
// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
// client.Tweet.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// ).
// Exec(ctx)
func (u *TweetUpsertBulk) UpdateNewValues() *TweetUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.Tweet.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *TweetUpsertBulk) Ignore() *TweetUpsertBulk {
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 *TweetUpsertBulk) DoNothing() *TweetUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the TweetCreateBulk.OnConflict
// documentation for more info.
func (u *TweetUpsertBulk) Update(set func(*TweetUpsert)) *TweetUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&TweetUpsert{UpdateSet: update})
}))
return u
}
// SetText sets the "text" field.
func (u *TweetUpsertBulk) SetText(v string) *TweetUpsertBulk {
return u.Update(func(s *TweetUpsert) {
s.SetText(v)
})
}
// UpdateText sets the "text" field to the value that was provided on create.
func (u *TweetUpsertBulk) UpdateText() *TweetUpsertBulk {
return u.Update(func(s *TweetUpsert) {
s.UpdateText()
})
}
// Exec executes the query.
func (u *TweetUpsertBulk) 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 TweetCreateBulk instead", i)
}
}
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for TweetCreateBulk.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *TweetUpsertBulk) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}