propertyschema_create.go•15.4 kB
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"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/node"
"github.com/Southclaws/storyden/internal/ent/propertyschema"
"github.com/Southclaws/storyden/internal/ent/propertyschemafield"
"github.com/rs/xid"
)
// PropertySchemaCreate is the builder for creating a PropertySchema entity.
type PropertySchemaCreate struct {
config
mutation *PropertySchemaMutation
hooks []Hook
conflict []sql.ConflictOption
}
// SetID sets the "id" field.
func (_c *PropertySchemaCreate) SetID(v xid.ID) *PropertySchemaCreate {
_c.mutation.SetID(v)
return _c
}
// SetNillableID sets the "id" field if the given value is not nil.
func (_c *PropertySchemaCreate) SetNillableID(v *xid.ID) *PropertySchemaCreate {
if v != nil {
_c.SetID(*v)
}
return _c
}
// AddNodeIDs adds the "node" edge to the Node entity by IDs.
func (_c *PropertySchemaCreate) AddNodeIDs(ids ...xid.ID) *PropertySchemaCreate {
_c.mutation.AddNodeIDs(ids...)
return _c
}
// AddNode adds the "node" edges to the Node entity.
func (_c *PropertySchemaCreate) AddNode(v ...*Node) *PropertySchemaCreate {
ids := make([]xid.ID, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _c.AddNodeIDs(ids...)
}
// AddFieldIDs adds the "fields" edge to the PropertySchemaField entity by IDs.
func (_c *PropertySchemaCreate) AddFieldIDs(ids ...xid.ID) *PropertySchemaCreate {
_c.mutation.AddFieldIDs(ids...)
return _c
}
// AddFields adds the "fields" edges to the PropertySchemaField entity.
func (_c *PropertySchemaCreate) AddFields(v ...*PropertySchemaField) *PropertySchemaCreate {
ids := make([]xid.ID, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _c.AddFieldIDs(ids...)
}
// Mutation returns the PropertySchemaMutation object of the builder.
func (_c *PropertySchemaCreate) Mutation() *PropertySchemaMutation {
return _c.mutation
}
// Save creates the PropertySchema in the database.
func (_c *PropertySchemaCreate) Save(ctx context.Context) (*PropertySchema, error) {
_c.defaults()
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *PropertySchemaCreate) SaveX(ctx context.Context) *PropertySchema {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *PropertySchemaCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *PropertySchemaCreate) 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 *PropertySchemaCreate) defaults() {
if _, ok := _c.mutation.ID(); !ok {
v := propertyschema.DefaultID()
_c.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_c *PropertySchemaCreate) check() error {
if v, ok := _c.mutation.ID(); ok {
if err := propertyschema.IDValidator(v.String()); err != nil {
return &ValidationError{Name: "id", err: fmt.Errorf(`ent: validator failed for field "PropertySchema.id": %w`, err)}
}
}
return nil
}
func (_c *PropertySchemaCreate) sqlSave(ctx context.Context) (*PropertySchema, 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 *PropertySchemaCreate) createSpec() (*PropertySchema, *sqlgraph.CreateSpec) {
var (
_node = &PropertySchema{config: _c.config}
_spec = sqlgraph.NewCreateSpec(propertyschema.Table, sqlgraph.NewFieldSpec(propertyschema.FieldID, field.TypeString))
)
_spec.OnConflict = _c.conflict
if id, ok := _c.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = &id
}
if nodes := _c.mutation.NodeIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: propertyschema.NodeTable,
Columns: []string{propertyschema.NodeColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(node.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.FieldsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: propertyschema.FieldsTable,
Columns: []string{propertyschema.FieldsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(propertyschemafield.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.PropertySchema.Create().
// OnConflict(
// // Update the row with the new values
// // the was proposed for insertion.
// sql.ResolveWithNewValues(),
// ).
// Exec(ctx)
func (_c *PropertySchemaCreate) OnConflict(opts ...sql.ConflictOption) *PropertySchemaUpsertOne {
_c.conflict = opts
return &PropertySchemaUpsertOne{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.PropertySchema.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *PropertySchemaCreate) OnConflictColumns(columns ...string) *PropertySchemaUpsertOne {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &PropertySchemaUpsertOne{
create: _c,
}
}
type (
// PropertySchemaUpsertOne is the builder for "upsert"-ing
// one PropertySchema node.
PropertySchemaUpsertOne struct {
create *PropertySchemaCreate
}
// PropertySchemaUpsert is the "OnConflict" setter.
PropertySchemaUpsert struct {
*sql.UpdateSet
}
)
// 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.PropertySchema.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(propertyschema.FieldID)
// }),
// ).
// Exec(ctx)
func (u *PropertySchemaUpsertOne) UpdateNewValues() *PropertySchemaUpsertOne {
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(propertyschema.FieldID)
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.PropertySchema.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *PropertySchemaUpsertOne) Ignore() *PropertySchemaUpsertOne {
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 *PropertySchemaUpsertOne) DoNothing() *PropertySchemaUpsertOne {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the PropertySchemaCreate.OnConflict
// documentation for more info.
func (u *PropertySchemaUpsertOne) Update(set func(*PropertySchemaUpsert)) *PropertySchemaUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&PropertySchemaUpsert{UpdateSet: update})
}))
return u
}
// Exec executes the query.
func (u *PropertySchemaUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for PropertySchemaCreate.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *PropertySchemaUpsertOne) 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 *PropertySchemaUpsertOne) 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: PropertySchemaUpsertOne.ID is not supported by MySQL driver. Use PropertySchemaUpsertOne.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 *PropertySchemaUpsertOne) IDX(ctx context.Context) xid.ID {
id, err := u.ID(ctx)
if err != nil {
panic(err)
}
return id
}
// PropertySchemaCreateBulk is the builder for creating many PropertySchema entities in bulk.
type PropertySchemaCreateBulk struct {
config
err error
builders []*PropertySchemaCreate
conflict []sql.ConflictOption
}
// Save creates the PropertySchema entities in the database.
func (_c *PropertySchemaCreateBulk) Save(ctx context.Context) ([]*PropertySchema, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*PropertySchema, 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.(*PropertySchemaMutation)
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 *PropertySchemaCreateBulk) SaveX(ctx context.Context) []*PropertySchema {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *PropertySchemaCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *PropertySchemaCreateBulk) 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.PropertySchema.CreateBulk(builders...).
// OnConflict(
// // Update the row with the new values
// // the was proposed for insertion.
// sql.ResolveWithNewValues(),
// ).
// Exec(ctx)
func (_c *PropertySchemaCreateBulk) OnConflict(opts ...sql.ConflictOption) *PropertySchemaUpsertBulk {
_c.conflict = opts
return &PropertySchemaUpsertBulk{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.PropertySchema.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *PropertySchemaCreateBulk) OnConflictColumns(columns ...string) *PropertySchemaUpsertBulk {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &PropertySchemaUpsertBulk{
create: _c,
}
}
// PropertySchemaUpsertBulk is the builder for "upsert"-ing
// a bulk of PropertySchema nodes.
type PropertySchemaUpsertBulk struct {
create *PropertySchemaCreateBulk
}
// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
// client.PropertySchema.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(propertyschema.FieldID)
// }),
// ).
// Exec(ctx)
func (u *PropertySchemaUpsertBulk) UpdateNewValues() *PropertySchemaUpsertBulk {
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(propertyschema.FieldID)
}
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.PropertySchema.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *PropertySchemaUpsertBulk) Ignore() *PropertySchemaUpsertBulk {
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 *PropertySchemaUpsertBulk) DoNothing() *PropertySchemaUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the PropertySchemaCreateBulk.OnConflict
// documentation for more info.
func (u *PropertySchemaUpsertBulk) Update(set func(*PropertySchemaUpsert)) *PropertySchemaUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&PropertySchemaUpsert{UpdateSet: update})
}))
return u
}
// Exec executes the query.
func (u *PropertySchemaUpsertBulk) 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 PropertySchemaCreateBulk instead", i)
}
}
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for PropertySchemaCreateBulk.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *PropertySchemaUpsertBulk) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}