snapshot of https://github.com/ent/ent.git (master, 2026-09-15; blobs > 131072B omitted)
This commit is contained in:
344
entc/integration/json/ent/client.go
Normal file
344
entc/integration/json/ent/client.go
Normal file
@@ -0,0 +1,344 @@
|
||||
// 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"
|
||||
"log"
|
||||
"reflect"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/entc/integration/json/ent/migrate"
|
||||
|
||||
"entgo.io/ent/dialect"
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/entc/integration/json/ent/user"
|
||||
)
|
||||
|
||||
// Client is the client that holds all ent builders.
|
||||
type Client struct {
|
||||
config
|
||||
// Schema is the client for creating, migrating and dropping schema.
|
||||
Schema *migrate.Schema
|
||||
// User is the client for interacting with the User builders.
|
||||
User *UserClient
|
||||
}
|
||||
|
||||
// NewClient creates a new client configured with the given options.
|
||||
func NewClient(opts ...Option) *Client {
|
||||
client := &Client{config: newConfig(opts...)}
|
||||
client.init()
|
||||
return client
|
||||
}
|
||||
|
||||
func (c *Client) init() {
|
||||
c.Schema = migrate.NewSchema(c.driver)
|
||||
c.User = NewUserClient(c.config)
|
||||
}
|
||||
|
||||
type (
|
||||
// config is the configuration for the client and its builder.
|
||||
config struct {
|
||||
// driver used for executing database requests.
|
||||
driver dialect.Driver
|
||||
// debug enable a debug logging.
|
||||
debug bool
|
||||
// log used for logging on debug mode.
|
||||
log func(...any)
|
||||
// hooks to execute on mutations.
|
||||
hooks *hooks
|
||||
// interceptors to execute on queries.
|
||||
inters *inters
|
||||
}
|
||||
// Option function to configure the client.
|
||||
Option func(*config)
|
||||
)
|
||||
|
||||
// newConfig creates a new config for the client.
|
||||
func newConfig(opts ...Option) config {
|
||||
cfg := config{log: log.Println, hooks: &hooks{}, inters: &inters{}}
|
||||
cfg.options(opts...)
|
||||
return cfg
|
||||
}
|
||||
|
||||
// options applies the options on the config object.
|
||||
func (c *config) options(opts ...Option) {
|
||||
for _, opt := range opts {
|
||||
opt(c)
|
||||
}
|
||||
if c.debug {
|
||||
c.driver = dialect.Debug(c.driver, c.log)
|
||||
}
|
||||
}
|
||||
|
||||
// Debug enables debug logging on the ent.Driver.
|
||||
func Debug() Option {
|
||||
return func(c *config) {
|
||||
c.debug = true
|
||||
}
|
||||
}
|
||||
|
||||
// Log sets the logging function for debug mode.
|
||||
func Log(fn func(...any)) Option {
|
||||
return func(c *config) {
|
||||
c.log = fn
|
||||
}
|
||||
}
|
||||
|
||||
// Driver configures the client driver.
|
||||
func Driver(driver dialect.Driver) Option {
|
||||
return func(c *config) {
|
||||
c.driver = driver
|
||||
}
|
||||
}
|
||||
|
||||
// Open opens a database/sql.DB specified by the driver name and
|
||||
// the data source name, and returns a new client attached to it.
|
||||
// Optional parameters can be added for configuring the client.
|
||||
func Open(driverName, dataSourceName string, options ...Option) (*Client, error) {
|
||||
switch driverName {
|
||||
case dialect.MySQL, dialect.Postgres, dialect.SQLite:
|
||||
drv, err := sql.Open(driverName, dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewClient(append(options, Driver(drv))...), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported driver: %q", driverName)
|
||||
}
|
||||
}
|
||||
|
||||
// ErrTxStarted is returned when trying to start a new transaction from a transactional client.
|
||||
var ErrTxStarted = errors.New("ent: cannot start a transaction within a transaction")
|
||||
|
||||
// Tx returns a new transactional client. The provided context
|
||||
// is used until the transaction is committed or rolled back.
|
||||
func (c *Client) Tx(ctx context.Context) (*Tx, error) {
|
||||
if _, ok := c.driver.(*txDriver); ok {
|
||||
return nil, ErrTxStarted
|
||||
}
|
||||
tx, err := newTx(ctx, c.driver)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ent: starting a transaction: %w", err)
|
||||
}
|
||||
cfg := c.config
|
||||
cfg.driver = tx
|
||||
return &Tx{
|
||||
ctx: ctx,
|
||||
config: cfg,
|
||||
User: NewUserClient(cfg),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// BeginTx returns a transactional client with specified options.
|
||||
func (c *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
|
||||
if _, ok := c.driver.(*txDriver); ok {
|
||||
return nil, errors.New("ent: cannot start a transaction within a transaction")
|
||||
}
|
||||
tx, err := c.driver.(interface {
|
||||
BeginTx(context.Context, *sql.TxOptions) (dialect.Tx, error)
|
||||
}).BeginTx(ctx, opts)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ent: starting a transaction: %w", err)
|
||||
}
|
||||
cfg := c.config
|
||||
cfg.driver = &txDriver{tx: tx, drv: c.driver}
|
||||
return &Tx{
|
||||
ctx: ctx,
|
||||
config: cfg,
|
||||
User: NewUserClient(cfg),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Debug returns a new debug-client. It's used to get verbose logging on specific operations.
|
||||
//
|
||||
// client.Debug().
|
||||
// User.
|
||||
// Query().
|
||||
// Count(ctx)
|
||||
func (c *Client) Debug() *Client {
|
||||
if c.debug {
|
||||
return c
|
||||
}
|
||||
cfg := c.config
|
||||
cfg.driver = dialect.Debug(c.driver, c.log)
|
||||
client := &Client{config: cfg}
|
||||
client.init()
|
||||
return client
|
||||
}
|
||||
|
||||
// Close closes the database connection and prevents new queries from starting.
|
||||
func (c *Client) Close() error {
|
||||
return c.driver.Close()
|
||||
}
|
||||
|
||||
// Use adds the mutation hooks to all the entity clients.
|
||||
// In order to add hooks to a specific client, call: `client.Node.Use(...)`.
|
||||
func (c *Client) Use(hooks ...Hook) {
|
||||
c.User.Use(hooks...)
|
||||
}
|
||||
|
||||
// Intercept adds the query interceptors to all the entity clients.
|
||||
// In order to add interceptors to a specific client, call: `client.Node.Intercept(...)`.
|
||||
func (c *Client) Intercept(interceptors ...Interceptor) {
|
||||
c.User.Intercept(interceptors...)
|
||||
}
|
||||
|
||||
// Mutate implements the ent.Mutator interface.
|
||||
func (c *Client) Mutate(ctx context.Context, m Mutation) (Value, error) {
|
||||
switch m := m.(type) {
|
||||
case *UserMutation:
|
||||
return c.User.mutate(ctx, m)
|
||||
default:
|
||||
return nil, fmt.Errorf("ent: unknown mutation type %T", m)
|
||||
}
|
||||
}
|
||||
|
||||
// UserClient is a client for the User schema.
|
||||
type UserClient struct {
|
||||
config
|
||||
}
|
||||
|
||||
// NewUserClient returns a client for the User from the given config.
|
||||
func NewUserClient(c config) *UserClient {
|
||||
return &UserClient{config: c}
|
||||
}
|
||||
|
||||
// Use adds a list of mutation hooks to the hooks stack.
|
||||
// A call to `Use(f, g, h)` equals to `user.Hooks(f(g(h())))`.
|
||||
func (c *UserClient) Use(hooks ...Hook) {
|
||||
c.hooks.User = append(c.hooks.User, hooks...)
|
||||
}
|
||||
|
||||
// Intercept adds a list of query interceptors to the interceptors stack.
|
||||
// A call to `Intercept(f, g, h)` equals to `user.Intercept(f(g(h())))`.
|
||||
func (c *UserClient) Intercept(interceptors ...Interceptor) {
|
||||
c.inters.User = append(c.inters.User, interceptors...)
|
||||
}
|
||||
|
||||
// Create returns a builder for creating a User entity.
|
||||
func (c *UserClient) Create() *UserCreate {
|
||||
mutation := newUserMutation(c.config, OpCreate)
|
||||
return &UserCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
||||
}
|
||||
|
||||
// CreateBulk returns a builder for creating a bulk of User entities.
|
||||
func (c *UserClient) CreateBulk(builders ...*UserCreate) *UserCreateBulk {
|
||||
return &UserCreateBulk{config: c.config, builders: builders}
|
||||
}
|
||||
|
||||
// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates
|
||||
// a builder and applies setFunc on it.
|
||||
func (c *UserClient) MapCreateBulk(slice any, setFunc func(*UserCreate, int)) *UserCreateBulk {
|
||||
rv := reflect.ValueOf(slice)
|
||||
if rv.Kind() != reflect.Slice {
|
||||
return &UserCreateBulk{err: fmt.Errorf("calling to UserClient.MapCreateBulk with wrong type %T, need slice", slice)}
|
||||
}
|
||||
builders := make([]*UserCreate, rv.Len())
|
||||
for i := 0; i < rv.Len(); i++ {
|
||||
builders[i] = c.Create()
|
||||
setFunc(builders[i], i)
|
||||
}
|
||||
return &UserCreateBulk{config: c.config, builders: builders}
|
||||
}
|
||||
|
||||
// Update returns an update builder for User.
|
||||
func (c *UserClient) Update() *UserUpdate {
|
||||
mutation := newUserMutation(c.config, OpUpdate)
|
||||
return &UserUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
||||
}
|
||||
|
||||
// UpdateOne returns an update builder for the given entity.
|
||||
func (c *UserClient) UpdateOne(_m *User) *UserUpdateOne {
|
||||
mutation := newUserMutation(c.config, OpUpdateOne, withUser(_m))
|
||||
return &UserUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
||||
}
|
||||
|
||||
// UpdateOneID returns an update builder for the given id.
|
||||
func (c *UserClient) UpdateOneID(id int) *UserUpdateOne {
|
||||
mutation := newUserMutation(c.config, OpUpdateOne, withUserID(id))
|
||||
return &UserUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
||||
}
|
||||
|
||||
// Delete returns a delete builder for User.
|
||||
func (c *UserClient) Delete() *UserDelete {
|
||||
mutation := newUserMutation(c.config, OpDelete)
|
||||
return &UserDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
||||
}
|
||||
|
||||
// DeleteOne returns a builder for deleting the given entity.
|
||||
func (c *UserClient) DeleteOne(_m *User) *UserDeleteOne {
|
||||
return c.DeleteOneID(_m.ID)
|
||||
}
|
||||
|
||||
// DeleteOneID returns a builder for deleting the given entity by its id.
|
||||
func (c *UserClient) DeleteOneID(id int) *UserDeleteOne {
|
||||
builder := c.Delete().Where(user.ID(id))
|
||||
builder.mutation.id = &id
|
||||
builder.mutation.SetOp(OpDeleteOne)
|
||||
return &UserDeleteOne{builder}
|
||||
}
|
||||
|
||||
// Query returns a query builder for User.
|
||||
func (c *UserClient) Query() *UserQuery {
|
||||
return &UserQuery{
|
||||
config: c.config,
|
||||
ctx: &QueryContext{Type: TypeUser},
|
||||
inters: c.Interceptors(),
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns a User entity by its id.
|
||||
func (c *UserClient) Get(ctx context.Context, id int) (*User, error) {
|
||||
return c.Query().Where(user.ID(id)).Only(ctx)
|
||||
}
|
||||
|
||||
// GetX is like Get, but panics if an error occurs.
|
||||
func (c *UserClient) GetX(ctx context.Context, id int) *User {
|
||||
obj, err := c.Get(ctx, id)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return obj
|
||||
}
|
||||
|
||||
// Hooks returns the client hooks.
|
||||
func (c *UserClient) Hooks() []Hook {
|
||||
return c.hooks.User
|
||||
}
|
||||
|
||||
// Interceptors returns the client interceptors.
|
||||
func (c *UserClient) Interceptors() []Interceptor {
|
||||
return c.inters.User
|
||||
}
|
||||
|
||||
func (c *UserClient) mutate(ctx context.Context, m *UserMutation) (Value, error) {
|
||||
switch m.Op() {
|
||||
case OpCreate:
|
||||
return (&UserCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
|
||||
case OpUpdate:
|
||||
return (&UserUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
|
||||
case OpUpdateOne:
|
||||
return (&UserUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
|
||||
case OpDelete, OpDeleteOne:
|
||||
return (&UserDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
|
||||
default:
|
||||
return nil, fmt.Errorf("ent: unknown User mutation op: %q", m.Op())
|
||||
}
|
||||
}
|
||||
|
||||
// hooks and interceptors per client, for fast access.
|
||||
type (
|
||||
hooks struct {
|
||||
User []ent.Hook
|
||||
}
|
||||
inters struct {
|
||||
User []ent.Interceptor
|
||||
}
|
||||
)
|
||||
612
entc/integration/json/ent/ent.go
Normal file
612
entc/integration/json/ent/ent.go
Normal file
@@ -0,0 +1,612 @@
|
||||
// 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"
|
||||
"reflect"
|
||||
"sync"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
"entgo.io/ent/entc/integration/json/ent/user"
|
||||
)
|
||||
|
||||
// ent aliases to avoid import conflicts in user's code.
|
||||
type (
|
||||
Op = ent.Op
|
||||
Hook = ent.Hook
|
||||
Value = ent.Value
|
||||
Query = ent.Query
|
||||
QueryContext = ent.QueryContext
|
||||
Querier = ent.Querier
|
||||
QuerierFunc = ent.QuerierFunc
|
||||
Interceptor = ent.Interceptor
|
||||
InterceptFunc = ent.InterceptFunc
|
||||
Traverser = ent.Traverser
|
||||
TraverseFunc = ent.TraverseFunc
|
||||
Policy = ent.Policy
|
||||
Mutator = ent.Mutator
|
||||
Mutation = ent.Mutation
|
||||
MutateFunc = ent.MutateFunc
|
||||
)
|
||||
|
||||
type clientCtxKey struct{}
|
||||
|
||||
// FromContext returns a Client stored inside a context, or nil if there isn't one.
|
||||
func FromContext(ctx context.Context) *Client {
|
||||
c, _ := ctx.Value(clientCtxKey{}).(*Client)
|
||||
return c
|
||||
}
|
||||
|
||||
// NewContext returns a new context with the given Client attached.
|
||||
func NewContext(parent context.Context, c *Client) context.Context {
|
||||
return context.WithValue(parent, clientCtxKey{}, c)
|
||||
}
|
||||
|
||||
type txCtxKey struct{}
|
||||
|
||||
// TxFromContext returns a Tx stored inside a context, or nil if there isn't one.
|
||||
func TxFromContext(ctx context.Context) *Tx {
|
||||
tx, _ := ctx.Value(txCtxKey{}).(*Tx)
|
||||
return tx
|
||||
}
|
||||
|
||||
// NewTxContext returns a new context with the given Tx attached.
|
||||
func NewTxContext(parent context.Context, tx *Tx) context.Context {
|
||||
return context.WithValue(parent, txCtxKey{}, tx)
|
||||
}
|
||||
|
||||
// OrderFunc applies an ordering on the sql selector.
|
||||
// Deprecated: Use Asc/Desc functions or the package builders instead.
|
||||
type OrderFunc func(*sql.Selector)
|
||||
|
||||
var (
|
||||
initCheck sync.Once
|
||||
columnCheck sql.ColumnCheck
|
||||
)
|
||||
|
||||
// checkColumn checks if the column exists in the given table.
|
||||
func checkColumn(t, c string) error {
|
||||
initCheck.Do(func() {
|
||||
columnCheck = sql.NewColumnCheck(map[string]func(string) bool{
|
||||
user.Table: user.ValidColumn,
|
||||
})
|
||||
})
|
||||
return columnCheck(t, c)
|
||||
}
|
||||
|
||||
// Asc applies the given fields in ASC order.
|
||||
func Asc(fields ...string) func(*sql.Selector) {
|
||||
return func(s *sql.Selector) {
|
||||
for _, f := range fields {
|
||||
if err := checkColumn(s.TableName(), f); err != nil {
|
||||
s.AddError(&ValidationError{Name: f, err: fmt.Errorf("ent: %w", err)})
|
||||
}
|
||||
s.OrderBy(sql.Asc(s.C(f)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Desc applies the given fields in DESC order.
|
||||
func Desc(fields ...string) func(*sql.Selector) {
|
||||
return func(s *sql.Selector) {
|
||||
for _, f := range fields {
|
||||
if err := checkColumn(s.TableName(), f); err != nil {
|
||||
s.AddError(&ValidationError{Name: f, err: fmt.Errorf("ent: %w", err)})
|
||||
}
|
||||
s.OrderBy(sql.Desc(s.C(f)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AggregateFunc applies an aggregation step on the group-by traversal/selector.
|
||||
type AggregateFunc func(*sql.Selector) string
|
||||
|
||||
// As is a pseudo aggregation function for renaming another other functions with custom names. For example:
|
||||
//
|
||||
// GroupBy(field1, field2).
|
||||
// Aggregate(ent.As(ent.Sum(field1), "sum_field1"), (ent.As(ent.Sum(field2), "sum_field2")).
|
||||
// Scan(ctx, &v)
|
||||
func As(fn AggregateFunc, end string) AggregateFunc {
|
||||
return func(s *sql.Selector) string {
|
||||
return sql.As(fn(s), end)
|
||||
}
|
||||
}
|
||||
|
||||
// Count applies the "count" aggregation function on each group.
|
||||
func Count() AggregateFunc {
|
||||
return func(s *sql.Selector) string {
|
||||
return sql.Count("*")
|
||||
}
|
||||
}
|
||||
|
||||
// Max applies the "max" aggregation function on the given field of each group.
|
||||
func Max(field string) AggregateFunc {
|
||||
return func(s *sql.Selector) string {
|
||||
if err := checkColumn(s.TableName(), field); err != nil {
|
||||
s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)})
|
||||
return ""
|
||||
}
|
||||
return sql.Max(s.C(field))
|
||||
}
|
||||
}
|
||||
|
||||
// Mean applies the "mean" aggregation function on the given field of each group.
|
||||
func Mean(field string) AggregateFunc {
|
||||
return func(s *sql.Selector) string {
|
||||
if err := checkColumn(s.TableName(), field); err != nil {
|
||||
s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)})
|
||||
return ""
|
||||
}
|
||||
return sql.Avg(s.C(field))
|
||||
}
|
||||
}
|
||||
|
||||
// Min applies the "min" aggregation function on the given field of each group.
|
||||
func Min(field string) AggregateFunc {
|
||||
return func(s *sql.Selector) string {
|
||||
if err := checkColumn(s.TableName(), field); err != nil {
|
||||
s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)})
|
||||
return ""
|
||||
}
|
||||
return sql.Min(s.C(field))
|
||||
}
|
||||
}
|
||||
|
||||
// Sum applies the "sum" aggregation function on the given field of each group.
|
||||
func Sum(field string) AggregateFunc {
|
||||
return func(s *sql.Selector) string {
|
||||
if err := checkColumn(s.TableName(), field); err != nil {
|
||||
s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)})
|
||||
return ""
|
||||
}
|
||||
return sql.Sum(s.C(field))
|
||||
}
|
||||
}
|
||||
|
||||
// ValidationError returns when validating a field or edge fails.
|
||||
type ValidationError struct {
|
||||
Name string // Field or edge name.
|
||||
err error
|
||||
}
|
||||
|
||||
// Error implements the error interface.
|
||||
func (e *ValidationError) Error() string {
|
||||
return e.err.Error()
|
||||
}
|
||||
|
||||
// Unwrap implements the errors.Wrapper interface.
|
||||
func (e *ValidationError) Unwrap() error {
|
||||
return e.err
|
||||
}
|
||||
|
||||
// IsValidationError returns a boolean indicating whether the error is a validation error.
|
||||
func IsValidationError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
var e *ValidationError
|
||||
return errors.As(err, &e)
|
||||
}
|
||||
|
||||
// NotFoundError returns when trying to fetch a specific entity and it was not found in the database.
|
||||
type NotFoundError struct {
|
||||
label string
|
||||
}
|
||||
|
||||
// Error implements the error interface.
|
||||
func (e *NotFoundError) Error() string {
|
||||
return "ent: " + e.label + " not found"
|
||||
}
|
||||
|
||||
// IsNotFound returns a boolean indicating whether the error is a not found error.
|
||||
func IsNotFound(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
var e *NotFoundError
|
||||
return errors.As(err, &e)
|
||||
}
|
||||
|
||||
// MaskNotFound masks not found error.
|
||||
func MaskNotFound(err error) error {
|
||||
if IsNotFound(err) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// NotSingularError returns when trying to fetch a singular entity and more then one was found in the database.
|
||||
type NotSingularError struct {
|
||||
label string
|
||||
}
|
||||
|
||||
// Error implements the error interface.
|
||||
func (e *NotSingularError) Error() string {
|
||||
return "ent: " + e.label + " not singular"
|
||||
}
|
||||
|
||||
// IsNotSingular returns a boolean indicating whether the error is a not singular error.
|
||||
func IsNotSingular(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
var e *NotSingularError
|
||||
return errors.As(err, &e)
|
||||
}
|
||||
|
||||
// NotLoadedError returns when trying to get a node that was not loaded by the query.
|
||||
type NotLoadedError struct {
|
||||
edge string
|
||||
}
|
||||
|
||||
// Error implements the error interface.
|
||||
func (e *NotLoadedError) Error() string {
|
||||
return "ent: " + e.edge + " edge was not loaded"
|
||||
}
|
||||
|
||||
// IsNotLoaded returns a boolean indicating whether the error is a not loaded error.
|
||||
func IsNotLoaded(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
var e *NotLoadedError
|
||||
return errors.As(err, &e)
|
||||
}
|
||||
|
||||
// ConstraintError returns when trying to create/update one or more entities and
|
||||
// one or more of their constraints failed. For example, violation of edge or
|
||||
// field uniqueness.
|
||||
type ConstraintError struct {
|
||||
msg string
|
||||
wrap error
|
||||
}
|
||||
|
||||
// Error implements the error interface.
|
||||
func (e ConstraintError) Error() string {
|
||||
return "ent: constraint failed: " + e.msg
|
||||
}
|
||||
|
||||
// Unwrap implements the errors.Wrapper interface.
|
||||
func (e *ConstraintError) Unwrap() error {
|
||||
return e.wrap
|
||||
}
|
||||
|
||||
// IsConstraintError returns a boolean indicating whether the error is a constraint failure.
|
||||
func IsConstraintError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
var e *ConstraintError
|
||||
return errors.As(err, &e)
|
||||
}
|
||||
|
||||
// selector embedded by the different Select/GroupBy builders.
|
||||
type selector struct {
|
||||
label string
|
||||
flds *[]string
|
||||
fns []AggregateFunc
|
||||
scan func(context.Context, any) error
|
||||
}
|
||||
|
||||
// ScanX is like Scan, but panics if an error occurs.
|
||||
func (s *selector) ScanX(ctx context.Context, v any) {
|
||||
if err := s.scan(ctx, v); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Strings returns list of strings from a selector. It is only allowed when selecting one field.
|
||||
func (s *selector) Strings(ctx context.Context) ([]string, error) {
|
||||
if len(*s.flds) > 1 {
|
||||
return nil, errors.New("ent: Strings is not achievable when selecting more than 1 field")
|
||||
}
|
||||
var v []string
|
||||
if err := s.scan(ctx, &v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// StringsX is like Strings, but panics if an error occurs.
|
||||
func (s *selector) StringsX(ctx context.Context) []string {
|
||||
v, err := s.Strings(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// String returns a single string from a selector. It is only allowed when selecting one field.
|
||||
func (s *selector) String(ctx context.Context) (_ string, err error) {
|
||||
var v []string
|
||||
if v, err = s.Strings(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
switch len(v) {
|
||||
case 1:
|
||||
return v[0], nil
|
||||
case 0:
|
||||
err = &NotFoundError{s.label}
|
||||
default:
|
||||
err = fmt.Errorf("ent: Strings returned %d results when one was expected", len(v))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// StringX is like String, but panics if an error occurs.
|
||||
func (s *selector) StringX(ctx context.Context) string {
|
||||
v, err := s.String(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Ints returns list of ints from a selector. It is only allowed when selecting one field.
|
||||
func (s *selector) Ints(ctx context.Context) ([]int, error) {
|
||||
if len(*s.flds) > 1 {
|
||||
return nil, errors.New("ent: Ints is not achievable when selecting more than 1 field")
|
||||
}
|
||||
var v []int
|
||||
if err := s.scan(ctx, &v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// IntsX is like Ints, but panics if an error occurs.
|
||||
func (s *selector) IntsX(ctx context.Context) []int {
|
||||
v, err := s.Ints(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Int returns a single int from a selector. It is only allowed when selecting one field.
|
||||
func (s *selector) Int(ctx context.Context) (_ int, err error) {
|
||||
var v []int
|
||||
if v, err = s.Ints(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
switch len(v) {
|
||||
case 1:
|
||||
return v[0], nil
|
||||
case 0:
|
||||
err = &NotFoundError{s.label}
|
||||
default:
|
||||
err = fmt.Errorf("ent: Ints returned %d results when one was expected", len(v))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// IntX is like Int, but panics if an error occurs.
|
||||
func (s *selector) IntX(ctx context.Context) int {
|
||||
v, err := s.Int(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Float64s returns list of float64s from a selector. It is only allowed when selecting one field.
|
||||
func (s *selector) Float64s(ctx context.Context) ([]float64, error) {
|
||||
if len(*s.flds) > 1 {
|
||||
return nil, errors.New("ent: Float64s is not achievable when selecting more than 1 field")
|
||||
}
|
||||
var v []float64
|
||||
if err := s.scan(ctx, &v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// Float64sX is like Float64s, but panics if an error occurs.
|
||||
func (s *selector) Float64sX(ctx context.Context) []float64 {
|
||||
v, err := s.Float64s(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Float64 returns a single float64 from a selector. It is only allowed when selecting one field.
|
||||
func (s *selector) Float64(ctx context.Context) (_ float64, err error) {
|
||||
var v []float64
|
||||
if v, err = s.Float64s(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
switch len(v) {
|
||||
case 1:
|
||||
return v[0], nil
|
||||
case 0:
|
||||
err = &NotFoundError{s.label}
|
||||
default:
|
||||
err = fmt.Errorf("ent: Float64s returned %d results when one was expected", len(v))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Float64X is like Float64, but panics if an error occurs.
|
||||
func (s *selector) Float64X(ctx context.Context) float64 {
|
||||
v, err := s.Float64(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Bools returns list of bools from a selector. It is only allowed when selecting one field.
|
||||
func (s *selector) Bools(ctx context.Context) ([]bool, error) {
|
||||
if len(*s.flds) > 1 {
|
||||
return nil, errors.New("ent: Bools is not achievable when selecting more than 1 field")
|
||||
}
|
||||
var v []bool
|
||||
if err := s.scan(ctx, &v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// BoolsX is like Bools, but panics if an error occurs.
|
||||
func (s *selector) BoolsX(ctx context.Context) []bool {
|
||||
v, err := s.Bools(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Bool returns a single bool from a selector. It is only allowed when selecting one field.
|
||||
func (s *selector) Bool(ctx context.Context) (_ bool, err error) {
|
||||
var v []bool
|
||||
if v, err = s.Bools(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
switch len(v) {
|
||||
case 1:
|
||||
return v[0], nil
|
||||
case 0:
|
||||
err = &NotFoundError{s.label}
|
||||
default:
|
||||
err = fmt.Errorf("ent: Bools returned %d results when one was expected", len(v))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// BoolX is like Bool, but panics if an error occurs.
|
||||
func (s *selector) BoolX(ctx context.Context) bool {
|
||||
v, err := s.Bool(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// withHooks invokes the builder operation with the given hooks, if any.
|
||||
func withHooks[V Value, M any, PM interface {
|
||||
*M
|
||||
Mutation
|
||||
}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) {
|
||||
if len(hooks) == 0 {
|
||||
return exec(ctx)
|
||||
}
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutationT, ok := any(m).(PM)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
// Set the mutation to the builder.
|
||||
*mutation = *mutationT
|
||||
return exec(ctx)
|
||||
})
|
||||
for i := len(hooks) - 1; i >= 0; i-- {
|
||||
if hooks[i] == nil {
|
||||
return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
||||
}
|
||||
mut = hooks[i](mut)
|
||||
}
|
||||
v, err := mut.Mutate(ctx, mutation)
|
||||
if err != nil {
|
||||
return value, err
|
||||
}
|
||||
nv, ok := v.(V)
|
||||
if !ok {
|
||||
return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation)
|
||||
}
|
||||
return nv, nil
|
||||
}
|
||||
|
||||
// setContextOp returns a new context with the given QueryContext attached (including its op) in case it does not exist.
|
||||
func setContextOp(ctx context.Context, qc *QueryContext, op string) context.Context {
|
||||
if ent.QueryFromContext(ctx) == nil {
|
||||
qc.Op = op
|
||||
ctx = ent.NewQueryContext(ctx, qc)
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
func querierAll[V Value, Q interface {
|
||||
sqlAll(context.Context, ...queryHook) (V, error)
|
||||
}]() Querier {
|
||||
return QuerierFunc(func(ctx context.Context, q Query) (Value, error) {
|
||||
query, ok := q.(Q)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected query type %T", q)
|
||||
}
|
||||
return query.sqlAll(ctx)
|
||||
})
|
||||
}
|
||||
|
||||
func querierCount[Q interface {
|
||||
sqlCount(context.Context) (int, error)
|
||||
}]() Querier {
|
||||
return QuerierFunc(func(ctx context.Context, q Query) (Value, error) {
|
||||
query, ok := q.(Q)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected query type %T", q)
|
||||
}
|
||||
return query.sqlCount(ctx)
|
||||
})
|
||||
}
|
||||
|
||||
func withInterceptors[V Value](ctx context.Context, q Query, qr Querier, inters []Interceptor) (v V, err error) {
|
||||
for i := len(inters) - 1; i >= 0; i-- {
|
||||
qr = inters[i].Intercept(qr)
|
||||
}
|
||||
rv, err := qr.Query(ctx, q)
|
||||
if err != nil {
|
||||
return v, err
|
||||
}
|
||||
vt, ok := rv.(V)
|
||||
if !ok {
|
||||
return v, fmt.Errorf("unexpected type %T returned from %T. expected type: %T", vt, q, v)
|
||||
}
|
||||
return vt, nil
|
||||
}
|
||||
|
||||
func scanWithInterceptors[Q1 ent.Query, Q2 interface {
|
||||
sqlScan(context.Context, Q1, any) error
|
||||
}](ctx context.Context, rootQuery Q1, selectOrGroup Q2, inters []Interceptor, v any) error {
|
||||
rv := reflect.ValueOf(v)
|
||||
var qr Querier = QuerierFunc(func(ctx context.Context, q Query) (Value, error) {
|
||||
query, ok := q.(Q1)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected query type %T", q)
|
||||
}
|
||||
if err := selectOrGroup.sqlScan(ctx, query, v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if k := rv.Kind(); k == reflect.Pointer && rv.Elem().CanInterface() {
|
||||
return rv.Elem().Interface(), nil
|
||||
}
|
||||
return v, nil
|
||||
})
|
||||
for i := len(inters) - 1; i >= 0; i-- {
|
||||
qr = inters[i].Intercept(qr)
|
||||
}
|
||||
vv, err := qr.Query(ctx, rootQuery)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch rv2 := reflect.ValueOf(vv); {
|
||||
case rv.IsNil(), rv2.IsNil(), rv.Kind() != reflect.Pointer:
|
||||
case rv.Type() == rv2.Type():
|
||||
rv.Elem().Set(rv2.Elem())
|
||||
case rv.Elem().Type() == rv2.Type():
|
||||
rv.Elem().Set(rv2)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// queryHook describes an internal hook for the different sqlAll methods.
|
||||
type queryHook func(context.Context, *sqlgraph.QuerySpec)
|
||||
88
entc/integration/json/ent/enttest/enttest.go
Normal file
88
entc/integration/json/ent/enttest/enttest.go
Normal file
@@ -0,0 +1,88 @@
|
||||
// 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 enttest
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"entgo.io/ent/entc/integration/json/ent"
|
||||
// required by schema hooks.
|
||||
_ "entgo.io/ent/entc/integration/json/ent/runtime"
|
||||
|
||||
"entgo.io/ent/dialect/sql/schema"
|
||||
"entgo.io/ent/entc/integration/json/ent/migrate"
|
||||
)
|
||||
|
||||
type (
|
||||
// TestingT is the interface that is shared between
|
||||
// testing.T and testing.B and used by enttest.
|
||||
TestingT interface {
|
||||
FailNow()
|
||||
Error(...any)
|
||||
}
|
||||
|
||||
// Option configures client creation.
|
||||
Option func(*options)
|
||||
|
||||
options struct {
|
||||
opts []ent.Option
|
||||
migrateOpts []schema.MigrateOption
|
||||
}
|
||||
)
|
||||
|
||||
// WithOptions forwards options to client creation.
|
||||
func WithOptions(opts ...ent.Option) Option {
|
||||
return func(o *options) {
|
||||
o.opts = append(o.opts, opts...)
|
||||
}
|
||||
}
|
||||
|
||||
// WithMigrateOptions forwards options to auto migration.
|
||||
func WithMigrateOptions(opts ...schema.MigrateOption) Option {
|
||||
return func(o *options) {
|
||||
o.migrateOpts = append(o.migrateOpts, opts...)
|
||||
}
|
||||
}
|
||||
|
||||
func newOptions(opts []Option) *options {
|
||||
o := &options{}
|
||||
for _, opt := range opts {
|
||||
opt(o)
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
// Open calls ent.Open and auto-run migration.
|
||||
func Open(t TestingT, driverName, dataSourceName string, opts ...Option) *ent.Client {
|
||||
o := newOptions(opts)
|
||||
c, err := ent.Open(driverName, dataSourceName, o.opts...)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.FailNow()
|
||||
}
|
||||
migrateSchema(t, c, o)
|
||||
return c
|
||||
}
|
||||
|
||||
// NewClient calls ent.NewClient and auto-run migration.
|
||||
func NewClient(t TestingT, opts ...Option) *ent.Client {
|
||||
o := newOptions(opts)
|
||||
c := ent.NewClient(o.opts...)
|
||||
migrateSchema(t, c, o)
|
||||
return c
|
||||
}
|
||||
func migrateSchema(t TestingT, c *ent.Client, o *options) {
|
||||
tables, err := schema.CopyTables(migrate.Tables)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.FailNow()
|
||||
}
|
||||
if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil {
|
||||
t.Error(err)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
7
entc/integration/json/ent/generate.go
Normal file
7
entc/integration/json/ent/generate.go
Normal file
@@ -0,0 +1,7 @@
|
||||
// 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.
|
||||
|
||||
package ent
|
||||
|
||||
//go:generate go run -mod=mod entgo.io/ent/cmd/ent generate --feature sql/modifier --header "// Copyright 2019-present Facebook Inc. All rights reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by ent, DO NOT EDIT." ./schema
|
||||
203
entc/integration/json/ent/hook/hook.go
Normal file
203
entc/integration/json/ent/hook/hook.go
Normal file
@@ -0,0 +1,203 @@
|
||||
// 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 hook
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"entgo.io/ent/entc/integration/json/ent"
|
||||
)
|
||||
|
||||
// The UserFunc type is an adapter to allow the use of ordinary
|
||||
// function as User mutator.
|
||||
type UserFunc func(context.Context, *ent.UserMutation) (ent.Value, error)
|
||||
|
||||
// Mutate calls f(ctx, m).
|
||||
func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
|
||||
if mv, ok := m.(*ent.UserMutation); ok {
|
||||
return f(ctx, mv)
|
||||
}
|
||||
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UserMutation", m)
|
||||
}
|
||||
|
||||
// Condition is a hook condition function.
|
||||
type Condition func(context.Context, ent.Mutation) bool
|
||||
|
||||
// And groups conditions with the AND operator.
|
||||
func And(first, second Condition, rest ...Condition) Condition {
|
||||
return func(ctx context.Context, m ent.Mutation) bool {
|
||||
if !first(ctx, m) || !second(ctx, m) {
|
||||
return false
|
||||
}
|
||||
for _, cond := range rest {
|
||||
if !cond(ctx, m) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// Or groups conditions with the OR operator.
|
||||
func Or(first, second Condition, rest ...Condition) Condition {
|
||||
return func(ctx context.Context, m ent.Mutation) bool {
|
||||
if first(ctx, m) || second(ctx, m) {
|
||||
return true
|
||||
}
|
||||
for _, cond := range rest {
|
||||
if cond(ctx, m) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Not negates a given condition.
|
||||
func Not(cond Condition) Condition {
|
||||
return func(ctx context.Context, m ent.Mutation) bool {
|
||||
return !cond(ctx, m)
|
||||
}
|
||||
}
|
||||
|
||||
// HasOp is a condition testing mutation operation.
|
||||
func HasOp(op ent.Op) Condition {
|
||||
return func(_ context.Context, m ent.Mutation) bool {
|
||||
return m.Op().Is(op)
|
||||
}
|
||||
}
|
||||
|
||||
// HasAddedFields is a condition validating `.AddedField` on fields.
|
||||
func HasAddedFields(field string, fields ...string) Condition {
|
||||
return func(_ context.Context, m ent.Mutation) bool {
|
||||
if _, exists := m.AddedField(field); !exists {
|
||||
return false
|
||||
}
|
||||
for _, field := range fields {
|
||||
if _, exists := m.AddedField(field); !exists {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// HasClearedFields is a condition validating `.FieldCleared` on fields.
|
||||
func HasClearedFields(field string, fields ...string) Condition {
|
||||
return func(_ context.Context, m ent.Mutation) bool {
|
||||
if exists := m.FieldCleared(field); !exists {
|
||||
return false
|
||||
}
|
||||
for _, field := range fields {
|
||||
if exists := m.FieldCleared(field); !exists {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// HasFields is a condition validating `.Field` on fields.
|
||||
func HasFields(field string, fields ...string) Condition {
|
||||
return func(_ context.Context, m ent.Mutation) bool {
|
||||
if _, exists := m.Field(field); !exists {
|
||||
return false
|
||||
}
|
||||
for _, field := range fields {
|
||||
if _, exists := m.Field(field); !exists {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// If executes the given hook under condition.
|
||||
//
|
||||
// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...)))
|
||||
func If(hk ent.Hook, cond Condition) ent.Hook {
|
||||
return func(next ent.Mutator) ent.Mutator {
|
||||
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
|
||||
if cond(ctx, m) {
|
||||
return hk(next).Mutate(ctx, m)
|
||||
}
|
||||
return next.Mutate(ctx, m)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// On executes the given hook only for the given operation.
|
||||
//
|
||||
// hook.On(Log, ent.Delete|ent.Create)
|
||||
func On(hk ent.Hook, op ent.Op) ent.Hook {
|
||||
return If(hk, HasOp(op))
|
||||
}
|
||||
|
||||
// Unless skips the given hook only for the given operation.
|
||||
//
|
||||
// hook.Unless(Log, ent.Update|ent.UpdateOne)
|
||||
func Unless(hk ent.Hook, op ent.Op) ent.Hook {
|
||||
return If(hk, Not(HasOp(op)))
|
||||
}
|
||||
|
||||
// FixedError is a hook returning a fixed error.
|
||||
func FixedError(err error) ent.Hook {
|
||||
return func(ent.Mutator) ent.Mutator {
|
||||
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
|
||||
return nil, err
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Reject returns a hook that rejects all operations that match op.
|
||||
//
|
||||
// func (T) Hooks() []ent.Hook {
|
||||
// return []ent.Hook{
|
||||
// Reject(ent.Delete|ent.Update),
|
||||
// }
|
||||
// }
|
||||
func Reject(op ent.Op) ent.Hook {
|
||||
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
|
||||
return On(hk, op)
|
||||
}
|
||||
|
||||
// Chain acts as a list of hooks and is effectively immutable.
|
||||
// Once created, it will always hold the same set of hooks in the same order.
|
||||
type Chain struct {
|
||||
hooks []ent.Hook
|
||||
}
|
||||
|
||||
// NewChain creates a new chain of hooks.
|
||||
func NewChain(hooks ...ent.Hook) Chain {
|
||||
return Chain{append([]ent.Hook(nil), hooks...)}
|
||||
}
|
||||
|
||||
// Hook chains the list of hooks and returns the final hook.
|
||||
func (c Chain) Hook() ent.Hook {
|
||||
return func(mutator ent.Mutator) ent.Mutator {
|
||||
for i := len(c.hooks) - 1; i >= 0; i-- {
|
||||
mutator = c.hooks[i](mutator)
|
||||
}
|
||||
return mutator
|
||||
}
|
||||
}
|
||||
|
||||
// Append extends a chain, adding the specified hook
|
||||
// as the last ones in the mutation flow.
|
||||
func (c Chain) Append(hooks ...ent.Hook) Chain {
|
||||
newHooks := make([]ent.Hook, 0, len(c.hooks)+len(hooks))
|
||||
newHooks = append(newHooks, c.hooks...)
|
||||
newHooks = append(newHooks, hooks...)
|
||||
return Chain{newHooks}
|
||||
}
|
||||
|
||||
// Extend extends a chain, adding the specified chain
|
||||
// as the last ones in the mutation flow.
|
||||
func (c Chain) Extend(chain Chain) Chain {
|
||||
return c.Append(chain.hooks...)
|
||||
}
|
||||
68
entc/integration/json/ent/migrate/migrate.go
Normal file
68
entc/integration/json/ent/migrate/migrate.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// 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 migrate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"entgo.io/ent/dialect"
|
||||
"entgo.io/ent/dialect/sql/schema"
|
||||
)
|
||||
|
||||
var (
|
||||
// WithGlobalUniqueID sets the universal ids options to the migration.
|
||||
// If this option is enabled, ent migration will allocate a 1<<32 range
|
||||
// for the ids of each entity (table).
|
||||
// Note that this option cannot be applied on tables that already exist.
|
||||
WithGlobalUniqueID = schema.WithGlobalUniqueID
|
||||
// WithDropColumn sets the drop column option to the migration.
|
||||
// If this option is enabled, ent migration will drop old columns
|
||||
// that were used for both fields and edges. This defaults to false.
|
||||
WithDropColumn = schema.WithDropColumn
|
||||
// WithDropIndex sets the drop index option to the migration.
|
||||
// If this option is enabled, ent migration will drop old indexes
|
||||
// that were defined in the schema. This defaults to false.
|
||||
// Note that unique constraints are defined using `UNIQUE INDEX`,
|
||||
// and therefore, it's recommended to enable this option to get more
|
||||
// flexibility in the schema changes.
|
||||
WithDropIndex = schema.WithDropIndex
|
||||
// WithForeignKeys enables creating foreign-key in schema DDL. This defaults to true.
|
||||
WithForeignKeys = schema.WithForeignKeys
|
||||
)
|
||||
|
||||
// Schema is the API for creating, migrating and dropping a schema.
|
||||
type Schema struct {
|
||||
drv dialect.Driver
|
||||
}
|
||||
|
||||
// NewSchema creates a new schema client.
|
||||
func NewSchema(drv dialect.Driver) *Schema { return &Schema{drv: drv} }
|
||||
|
||||
// Create creates all schema resources.
|
||||
func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error {
|
||||
return Create(ctx, s, Tables, opts...)
|
||||
}
|
||||
|
||||
// Create creates all table resources using the given schema driver.
|
||||
func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error {
|
||||
migrate, err := schema.NewMigrate(s.drv, opts...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ent/migrate: %w", err)
|
||||
}
|
||||
return migrate.Create(ctx, tables...)
|
||||
}
|
||||
|
||||
// WriteTo writes the schema changes to w instead of running them against the database.
|
||||
//
|
||||
// if err := client.Schema.WriteTo(context.Background(), os.Stdout); err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
func (s *Schema) WriteTo(ctx context.Context, w io.Writer, opts ...schema.MigrateOption) error {
|
||||
return Create(ctx, &Schema{drv: &schema.WriteDriver{Writer: w, Driver: s.drv}}, Tables, opts...)
|
||||
}
|
||||
45
entc/integration/json/ent/migrate/schema.go
Normal file
45
entc/integration/json/ent/migrate/schema.go
Normal file
@@ -0,0 +1,45 @@
|
||||
// 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 migrate
|
||||
|
||||
import (
|
||||
"entgo.io/ent/dialect/sql/schema"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
var (
|
||||
// UsersColumns holds the columns for the "users" table.
|
||||
UsersColumns = []*schema.Column{
|
||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||
{Name: "t", Type: field.TypeJSON, Nullable: true},
|
||||
{Name: "url", Type: field.TypeJSON, Nullable: true},
|
||||
{Name: "urls", Type: field.TypeJSON, Nullable: true},
|
||||
{Name: "raw", Type: field.TypeJSON, Nullable: true},
|
||||
{Name: "dirs", Type: field.TypeJSON},
|
||||
{Name: "ints", Type: field.TypeJSON, Nullable: true},
|
||||
{Name: "floats", Type: field.TypeJSON, Nullable: true},
|
||||
{Name: "strings", Type: field.TypeJSON, Nullable: true},
|
||||
{Name: "ints_validate", Type: field.TypeJSON, Nullable: true},
|
||||
{Name: "floats_validate", Type: field.TypeJSON, Nullable: true},
|
||||
{Name: "strings_validate", Type: field.TypeJSON, Nullable: true},
|
||||
{Name: "addr", Type: field.TypeJSON, Nullable: true},
|
||||
{Name: "unknown", Type: field.TypeJSON, Nullable: true},
|
||||
}
|
||||
// UsersTable holds the schema information for the "users" table.
|
||||
UsersTable = &schema.Table{
|
||||
Name: "users",
|
||||
Columns: UsersColumns,
|
||||
PrimaryKey: []*schema.Column{UsersColumns[0]},
|
||||
}
|
||||
// Tables holds all the tables in the schema.
|
||||
Tables = []*schema.Table{
|
||||
UsersTable,
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
}
|
||||
394
entc/integration/json/ent/mutation.go
Normal file
394
entc/integration/json/ent/mutation.go
Normal file
@@ -0,0 +1,394 @@
|
||||
// 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"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sync"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/entc/integration/json/ent/schema"
|
||||
"entgo.io/ent/entc/integration/json/ent/user"
|
||||
)
|
||||
|
||||
const (
|
||||
// Operation types.
|
||||
OpCreate = ent.OpCreate
|
||||
OpDelete = ent.OpDelete
|
||||
OpDeleteOne = ent.OpDeleteOne
|
||||
OpUpdate = ent.OpUpdate
|
||||
OpUpdateOne = ent.OpUpdateOne
|
||||
|
||||
// Node types.
|
||||
TypeUser = "User"
|
||||
)
|
||||
|
||||
// UserMutation represents an operation that mutates the User nodes in the graph.
|
||||
type UserMutation struct {
|
||||
user.Mutation
|
||||
config
|
||||
id *int
|
||||
done bool
|
||||
oldValue func(context.Context) (*User, error)
|
||||
}
|
||||
|
||||
var _ ent.Mutation = (*UserMutation)(nil)
|
||||
|
||||
// userOption allows management of the mutation configuration using functional options.
|
||||
type userOption func(*UserMutation)
|
||||
|
||||
// newUserMutation creates new mutation for the User entity.
|
||||
func newUserMutation(c config, op Op, opts ...userOption) *UserMutation {
|
||||
m := &UserMutation{
|
||||
Mutation: *user.NewMutation(op),
|
||||
config: c,
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(m)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// ID returns the ID value in the mutation. Note that the ID is only available
|
||||
// if it was provided to the builder or after it was returned from the database.
|
||||
func (m *UserMutation) ID() (id int, exists bool) {
|
||||
if m.id == nil {
|
||||
return
|
||||
}
|
||||
return *m.id, true
|
||||
}
|
||||
|
||||
// withUserID sets the ID field of the mutation.
|
||||
func withUserID(id int) userOption {
|
||||
return func(m *UserMutation) {
|
||||
var (
|
||||
err error
|
||||
once sync.Once
|
||||
value *User
|
||||
)
|
||||
m.oldValue = func(ctx context.Context) (*User, error) {
|
||||
once.Do(func() {
|
||||
if m.done {
|
||||
err = errors.New("querying old values post mutation is not allowed")
|
||||
} else {
|
||||
value, err = m.Client().User.Get(ctx, id)
|
||||
}
|
||||
})
|
||||
return value, err
|
||||
}
|
||||
m.id = &id
|
||||
}
|
||||
}
|
||||
|
||||
// withUser sets the old User of the mutation.
|
||||
func withUser(node *User) userOption {
|
||||
return func(m *UserMutation) {
|
||||
m.oldValue = func(context.Context) (*User, error) {
|
||||
return node, nil
|
||||
}
|
||||
m.id = &node.ID
|
||||
}
|
||||
}
|
||||
|
||||
// Client returns a new `ent.Client` from the mutation. If the mutation was
|
||||
// executed in a transaction (ent.Tx), a transactional client is returned.
|
||||
func (m UserMutation) Client() *Client {
|
||||
client := &Client{config: m.config}
|
||||
client.init()
|
||||
return client
|
||||
}
|
||||
|
||||
// Tx returns an `ent.Tx` for mutations that were executed in transactions;
|
||||
// it returns an error otherwise.
|
||||
func (m UserMutation) Tx() (*Tx, error) {
|
||||
if _, ok := m.driver.(*txDriver); !ok {
|
||||
return nil, errors.New("ent: mutation is not running in a transaction")
|
||||
}
|
||||
tx := &Tx{config: m.config}
|
||||
tx.init()
|
||||
return tx, nil
|
||||
}
|
||||
|
||||
// IDs queries the database and returns the entity ids that match the mutation's predicate.
|
||||
// That means, if the mutation is applied within a transaction with an isolation level such
|
||||
// as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated
|
||||
// or updated by the mutation.
|
||||
func (m *UserMutation) IDs(ctx context.Context) ([]int, error) {
|
||||
switch {
|
||||
case m.Op().Is(OpUpdateOne | OpDeleteOne):
|
||||
id, exists := m.ID()
|
||||
if exists {
|
||||
return []int{id}, nil
|
||||
}
|
||||
fallthrough
|
||||
case m.Op().Is(OpUpdate | OpDelete):
|
||||
return m.Client().User.Query().Where(m.Predicates()...).IDs(ctx)
|
||||
default:
|
||||
return nil, fmt.Errorf("IDs is not allowed on %s operations", m.Op())
|
||||
}
|
||||
}
|
||||
|
||||
// OldT returns the old "t" field's value of the User entity.
|
||||
// If the User object wasn't provided to the builder, the object is fetched from the database.
|
||||
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
||||
func (m *UserMutation) OldT(ctx context.Context) (v *schema.T, err error) {
|
||||
if !m.Op().Is(OpUpdateOne) {
|
||||
return v, errors.New("OldT is only allowed on UpdateOne operations")
|
||||
}
|
||||
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
||||
return v, errors.New("OldT requires an ID field in the mutation")
|
||||
}
|
||||
oldValue, err := m.oldValue(ctx)
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("querying old value for OldT: %w", err)
|
||||
}
|
||||
return oldValue.T, nil
|
||||
}
|
||||
|
||||
// OldURL returns the old "url" field's value of the User entity.
|
||||
// If the User object wasn't provided to the builder, the object is fetched from the database.
|
||||
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
||||
func (m *UserMutation) OldURL(ctx context.Context) (v *url.URL, err error) {
|
||||
if !m.Op().Is(OpUpdateOne) {
|
||||
return v, errors.New("OldURL is only allowed on UpdateOne operations")
|
||||
}
|
||||
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
||||
return v, errors.New("OldURL requires an ID field in the mutation")
|
||||
}
|
||||
oldValue, err := m.oldValue(ctx)
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("querying old value for OldURL: %w", err)
|
||||
}
|
||||
return oldValue.URL, nil
|
||||
}
|
||||
|
||||
// OldURLs returns the old "URLs" field's value of the User entity.
|
||||
// If the User object wasn't provided to the builder, the object is fetched from the database.
|
||||
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
||||
func (m *UserMutation) OldURLs(ctx context.Context) (v []*url.URL, err error) {
|
||||
if !m.Op().Is(OpUpdateOne) {
|
||||
return v, errors.New("OldURLs is only allowed on UpdateOne operations")
|
||||
}
|
||||
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
||||
return v, errors.New("OldURLs requires an ID field in the mutation")
|
||||
}
|
||||
oldValue, err := m.oldValue(ctx)
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("querying old value for OldURLs: %w", err)
|
||||
}
|
||||
return oldValue.URLs, nil
|
||||
}
|
||||
|
||||
// OldRaw returns the old "raw" field's value of the User entity.
|
||||
// If the User object wasn't provided to the builder, the object is fetched from the database.
|
||||
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
||||
func (m *UserMutation) OldRaw(ctx context.Context) (v json.RawMessage, err error) {
|
||||
if !m.Op().Is(OpUpdateOne) {
|
||||
return v, errors.New("OldRaw is only allowed on UpdateOne operations")
|
||||
}
|
||||
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
||||
return v, errors.New("OldRaw requires an ID field in the mutation")
|
||||
}
|
||||
oldValue, err := m.oldValue(ctx)
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("querying old value for OldRaw: %w", err)
|
||||
}
|
||||
return oldValue.Raw, nil
|
||||
}
|
||||
|
||||
// OldDirs returns the old "dirs" field's value of the User entity.
|
||||
// If the User object wasn't provided to the builder, the object is fetched from the database.
|
||||
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
||||
func (m *UserMutation) OldDirs(ctx context.Context) (v []http.Dir, err error) {
|
||||
if !m.Op().Is(OpUpdateOne) {
|
||||
return v, errors.New("OldDirs is only allowed on UpdateOne operations")
|
||||
}
|
||||
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
||||
return v, errors.New("OldDirs requires an ID field in the mutation")
|
||||
}
|
||||
oldValue, err := m.oldValue(ctx)
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("querying old value for OldDirs: %w", err)
|
||||
}
|
||||
return oldValue.Dirs, nil
|
||||
}
|
||||
|
||||
// OldInts returns the old "ints" field's value of the User entity.
|
||||
// If the User object wasn't provided to the builder, the object is fetched from the database.
|
||||
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
||||
func (m *UserMutation) OldInts(ctx context.Context) (v []int, err error) {
|
||||
if !m.Op().Is(OpUpdateOne) {
|
||||
return v, errors.New("OldInts is only allowed on UpdateOne operations")
|
||||
}
|
||||
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
||||
return v, errors.New("OldInts requires an ID field in the mutation")
|
||||
}
|
||||
oldValue, err := m.oldValue(ctx)
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("querying old value for OldInts: %w", err)
|
||||
}
|
||||
return oldValue.Ints, nil
|
||||
}
|
||||
|
||||
// OldFloats returns the old "floats" field's value of the User entity.
|
||||
// If the User object wasn't provided to the builder, the object is fetched from the database.
|
||||
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
||||
func (m *UserMutation) OldFloats(ctx context.Context) (v []float64, err error) {
|
||||
if !m.Op().Is(OpUpdateOne) {
|
||||
return v, errors.New("OldFloats is only allowed on UpdateOne operations")
|
||||
}
|
||||
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
||||
return v, errors.New("OldFloats requires an ID field in the mutation")
|
||||
}
|
||||
oldValue, err := m.oldValue(ctx)
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("querying old value for OldFloats: %w", err)
|
||||
}
|
||||
return oldValue.Floats, nil
|
||||
}
|
||||
|
||||
// OldStrings returns the old "strings" field's value of the User entity.
|
||||
// If the User object wasn't provided to the builder, the object is fetched from the database.
|
||||
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
||||
func (m *UserMutation) OldStrings(ctx context.Context) (v []string, err error) {
|
||||
if !m.Op().Is(OpUpdateOne) {
|
||||
return v, errors.New("OldStrings is only allowed on UpdateOne operations")
|
||||
}
|
||||
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
||||
return v, errors.New("OldStrings requires an ID field in the mutation")
|
||||
}
|
||||
oldValue, err := m.oldValue(ctx)
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("querying old value for OldStrings: %w", err)
|
||||
}
|
||||
return oldValue.Strings, nil
|
||||
}
|
||||
|
||||
// OldIntsValidate returns the old "ints_validate" field's value of the User entity.
|
||||
// If the User object wasn't provided to the builder, the object is fetched from the database.
|
||||
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
||||
func (m *UserMutation) OldIntsValidate(ctx context.Context) (v []int, err error) {
|
||||
if !m.Op().Is(OpUpdateOne) {
|
||||
return v, errors.New("OldIntsValidate is only allowed on UpdateOne operations")
|
||||
}
|
||||
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
||||
return v, errors.New("OldIntsValidate requires an ID field in the mutation")
|
||||
}
|
||||
oldValue, err := m.oldValue(ctx)
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("querying old value for OldIntsValidate: %w", err)
|
||||
}
|
||||
return oldValue.IntsValidate, nil
|
||||
}
|
||||
|
||||
// OldFloatsValidate returns the old "floats_validate" field's value of the User entity.
|
||||
// If the User object wasn't provided to the builder, the object is fetched from the database.
|
||||
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
||||
func (m *UserMutation) OldFloatsValidate(ctx context.Context) (v []float64, err error) {
|
||||
if !m.Op().Is(OpUpdateOne) {
|
||||
return v, errors.New("OldFloatsValidate is only allowed on UpdateOne operations")
|
||||
}
|
||||
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
||||
return v, errors.New("OldFloatsValidate requires an ID field in the mutation")
|
||||
}
|
||||
oldValue, err := m.oldValue(ctx)
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("querying old value for OldFloatsValidate: %w", err)
|
||||
}
|
||||
return oldValue.FloatsValidate, nil
|
||||
}
|
||||
|
||||
// OldStringsValidate returns the old "strings_validate" field's value of the User entity.
|
||||
// If the User object wasn't provided to the builder, the object is fetched from the database.
|
||||
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
||||
func (m *UserMutation) OldStringsValidate(ctx context.Context) (v []string, err error) {
|
||||
if !m.Op().Is(OpUpdateOne) {
|
||||
return v, errors.New("OldStringsValidate is only allowed on UpdateOne operations")
|
||||
}
|
||||
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
||||
return v, errors.New("OldStringsValidate requires an ID field in the mutation")
|
||||
}
|
||||
oldValue, err := m.oldValue(ctx)
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("querying old value for OldStringsValidate: %w", err)
|
||||
}
|
||||
return oldValue.StringsValidate, nil
|
||||
}
|
||||
|
||||
// OldAddr returns the old "addr" field's value of the User entity.
|
||||
// If the User object wasn't provided to the builder, the object is fetched from the database.
|
||||
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
||||
func (m *UserMutation) OldAddr(ctx context.Context) (v schema.Addr, err error) {
|
||||
if !m.Op().Is(OpUpdateOne) {
|
||||
return v, errors.New("OldAddr is only allowed on UpdateOne operations")
|
||||
}
|
||||
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
||||
return v, errors.New("OldAddr requires an ID field in the mutation")
|
||||
}
|
||||
oldValue, err := m.oldValue(ctx)
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("querying old value for OldAddr: %w", err)
|
||||
}
|
||||
return oldValue.Addr, nil
|
||||
}
|
||||
|
||||
// OldUnknown returns the old "unknown" field's value of the User entity.
|
||||
// If the User object wasn't provided to the builder, the object is fetched from the database.
|
||||
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
||||
func (m *UserMutation) OldUnknown(ctx context.Context) (v any, err error) {
|
||||
if !m.Op().Is(OpUpdateOne) {
|
||||
return v, errors.New("OldUnknown is only allowed on UpdateOne operations")
|
||||
}
|
||||
if _, exists := m.ID(); !exists || m.oldValue == nil {
|
||||
return v, errors.New("OldUnknown requires an ID field in the mutation")
|
||||
}
|
||||
oldValue, err := m.oldValue(ctx)
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("querying old value for OldUnknown: %w", err)
|
||||
}
|
||||
return oldValue.Unknown, nil
|
||||
}
|
||||
|
||||
// OldField returns the old value of the field from the database. An error is
|
||||
// returned if the mutation operation is not UpdateOne, or the query to the
|
||||
// database failed.
|
||||
func (m *UserMutation) OldField(ctx context.Context, name string) (ent.Value, error) {
|
||||
switch name {
|
||||
case user.FieldT:
|
||||
return m.OldT(ctx)
|
||||
case user.FieldURL:
|
||||
return m.OldURL(ctx)
|
||||
case user.FieldURLs:
|
||||
return m.OldURLs(ctx)
|
||||
case user.FieldRaw:
|
||||
return m.OldRaw(ctx)
|
||||
case user.FieldDirs:
|
||||
return m.OldDirs(ctx)
|
||||
case user.FieldInts:
|
||||
return m.OldInts(ctx)
|
||||
case user.FieldFloats:
|
||||
return m.OldFloats(ctx)
|
||||
case user.FieldStrings:
|
||||
return m.OldStrings(ctx)
|
||||
case user.FieldIntsValidate:
|
||||
return m.OldIntsValidate(ctx)
|
||||
case user.FieldFloatsValidate:
|
||||
return m.OldFloatsValidate(ctx)
|
||||
case user.FieldStringsValidate:
|
||||
return m.OldStringsValidate(ctx)
|
||||
case user.FieldAddr:
|
||||
return m.OldAddr(ctx)
|
||||
case user.FieldUnknown:
|
||||
return m.OldUnknown(ctx)
|
||||
}
|
||||
return nil, fmt.Errorf("unknown User field %s", name)
|
||||
}
|
||||
14
entc/integration/json/ent/predicate/predicate.go
Normal file
14
entc/integration/json/ent/predicate/predicate.go
Normal file
@@ -0,0 +1,14 @@
|
||||
// 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 predicate
|
||||
|
||||
import (
|
||||
"entgo.io/ent/dialect/sql"
|
||||
)
|
||||
|
||||
// User is the predicate function for user builders.
|
||||
type User func(*sql.Selector)
|
||||
42
entc/integration/json/ent/runtime.go
Normal file
42
entc/integration/json/ent/runtime.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// 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 (
|
||||
"net/http"
|
||||
|
||||
"entgo.io/ent/entc/integration/json/ent/schema"
|
||||
"entgo.io/ent/entc/integration/json/ent/user"
|
||||
)
|
||||
|
||||
// The init function reads all schema descriptors with runtime code
|
||||
// (default values, validators, hooks and policies) and stitches it
|
||||
// to their package variables.
|
||||
func init() {
|
||||
userFields := schema.User{}.Fields()
|
||||
_ = userFields
|
||||
// userDescDirs is the schema descriptor for dirs field.
|
||||
userDescDirs := userFields[4].Descriptor()
|
||||
// user.DefaultDirs holds the default value on creation for the dirs field.
|
||||
user.DefaultDirs = userDescDirs.Default.(func() []http.Dir)
|
||||
// userDescInts is the schema descriptor for ints field.
|
||||
userDescInts := userFields[5].Descriptor()
|
||||
// user.DefaultInts holds the default value on creation for the ints field.
|
||||
user.DefaultInts = userDescInts.Default.([]int)
|
||||
// userDescIntsValidate is the schema descriptor for ints_validate field.
|
||||
userDescIntsValidate := userFields[8].Descriptor()
|
||||
// user.IntsValidateValidator is a validator for the "ints_validate" field. It is called by the builders before save.
|
||||
user.IntsValidateValidator = userDescIntsValidate.Validators[0].(func([]int) error)
|
||||
// userDescFloatsValidate is the schema descriptor for floats_validate field.
|
||||
userDescFloatsValidate := userFields[9].Descriptor()
|
||||
// user.FloatsValidateValidator is a validator for the "floats_validate" field. It is called by the builders before save.
|
||||
user.FloatsValidateValidator = userDescFloatsValidate.Validators[0].(func([]float64) error)
|
||||
// userDescStringsValidate is the schema descriptor for strings_validate field.
|
||||
userDescStringsValidate := userFields[10].Descriptor()
|
||||
// user.StringsValidateValidator is a validator for the "strings_validate" field. It is called by the builders before save.
|
||||
user.StringsValidateValidator = userDescStringsValidate.Validators[0].(func([]string) error)
|
||||
}
|
||||
13
entc/integration/json/ent/runtime/runtime.go
Normal file
13
entc/integration/json/ent/runtime/runtime.go
Normal file
@@ -0,0 +1,13 @@
|
||||
// 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 runtime
|
||||
|
||||
// The schema-stitching logic is generated in entgo.io/ent/entc/integration/json/ent/runtime.go
|
||||
|
||||
const (
|
||||
Version = "v0.0.0-00010101000000-000000000000" // Version of ent codegen.
|
||||
)
|
||||
128
entc/integration/json/ent/schema/user.go
Normal file
128
entc/integration/json/ent/schema/user.go
Normal file
@@ -0,0 +1,128 @@
|
||||
// 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.
|
||||
|
||||
package schema
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
// User holds the schema definition for the User entity.
|
||||
type User struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the User.
|
||||
func (User) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.JSON("t", &T{}).
|
||||
Optional(),
|
||||
field.JSON("url", &url.URL{}).
|
||||
Optional(),
|
||||
field.JSON("URLs", []*url.URL{}).
|
||||
StorageKey("urls").
|
||||
StructTag(`json:"urls,omitempty"`).
|
||||
Optional(),
|
||||
field.JSON("raw", json.RawMessage{}).
|
||||
Optional(),
|
||||
field.JSON("dirs", []http.Dir{}).
|
||||
Default(func() []http.Dir {
|
||||
return []http.Dir{"/tmp"}
|
||||
}),
|
||||
field.Ints("ints").
|
||||
Optional().
|
||||
Default([]int{1, 2, 3}),
|
||||
field.Floats("floats").
|
||||
Optional(),
|
||||
field.Strings("strings").
|
||||
Optional(),
|
||||
field.Ints("ints_validate").
|
||||
Optional().
|
||||
Validate(validate[int]),
|
||||
field.Floats("floats_validate").
|
||||
Optional().
|
||||
Validate(validate[float64]),
|
||||
field.Strings("strings_validate").
|
||||
Optional().
|
||||
Validate(validate[string]),
|
||||
field.JSON("addr", Addr{}).
|
||||
Sensitive().
|
||||
Optional(),
|
||||
field.Any("unknown").
|
||||
Optional(),
|
||||
}
|
||||
}
|
||||
|
||||
type T struct {
|
||||
I int `json:"i,omitempty"`
|
||||
F float64 `json:"f,omitempty"`
|
||||
B bool `json:"b,omitempty"`
|
||||
S string `json:"s,omitempty"`
|
||||
T *T `json:"t,omitempty"`
|
||||
Li []int `json:"li"`
|
||||
Ls []string `json:"ls"`
|
||||
// Do not omit empty or null maps.
|
||||
M map[string]any `json:"m"`
|
||||
}
|
||||
|
||||
type Addr struct{ net.Addr }
|
||||
|
||||
func (a *Addr) UnmarshalJSON(data []byte) error {
|
||||
var types struct {
|
||||
TCP *net.TCPAddr `json:"tcp,omitempty"`
|
||||
UDP *net.UDPAddr `json:"udp,omitempty"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &types); err != nil {
|
||||
return err
|
||||
}
|
||||
switch {
|
||||
case types.TCP != nil && types.UDP != nil:
|
||||
return errors.New("TCP and UDP addresses are mutually exclusive")
|
||||
case types.TCP != nil:
|
||||
a.Addr = types.TCP
|
||||
case types.UDP != nil:
|
||||
a.Addr = types.UDP
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a Addr) MarshalJSON() ([]byte, error) {
|
||||
var types struct {
|
||||
TCP *net.TCPAddr `json:"tcp,omitempty"`
|
||||
UDP *net.UDPAddr `json:"udp,omitempty"`
|
||||
}
|
||||
switch a := a.Addr.(type) {
|
||||
case *net.TCPAddr:
|
||||
types.TCP = a
|
||||
case *net.UDPAddr:
|
||||
types.UDP = a
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported address type: %T", a)
|
||||
}
|
||||
return json.Marshal(types)
|
||||
}
|
||||
|
||||
func (a Addr) String() string {
|
||||
if a.Addr == nil {
|
||||
return ""
|
||||
}
|
||||
return a.Addr.String()
|
||||
}
|
||||
|
||||
var ErrValidate = errors.New("validation error")
|
||||
|
||||
func validate[T int | string | float64](xs []T) error {
|
||||
if xs != nil {
|
||||
return ErrValidate
|
||||
}
|
||||
return nil
|
||||
}
|
||||
214
entc/integration/json/ent/tx.go
Normal file
214
entc/integration/json/ent/tx.go
Normal file
@@ -0,0 +1,214 @@
|
||||
// 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"
|
||||
"sync"
|
||||
|
||||
"entgo.io/ent/dialect"
|
||||
)
|
||||
|
||||
// Tx is a transactional client that is created by calling Client.Tx().
|
||||
type Tx struct {
|
||||
config
|
||||
// User is the client for interacting with the User builders.
|
||||
User *UserClient
|
||||
|
||||
// lazily loaded.
|
||||
client *Client
|
||||
clientOnce sync.Once
|
||||
// ctx lives for the life of the transaction. It is
|
||||
// the same context used by the underlying connection.
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
type (
|
||||
// Committer is the interface that wraps the Commit method.
|
||||
Committer interface {
|
||||
Commit(context.Context, *Tx) error
|
||||
}
|
||||
|
||||
// The CommitFunc type is an adapter to allow the use of ordinary
|
||||
// function as a Committer. If f is a function with the appropriate
|
||||
// signature, CommitFunc(f) is a Committer that calls f.
|
||||
CommitFunc func(context.Context, *Tx) error
|
||||
|
||||
// CommitHook defines the "commit middleware". A function that gets a Committer
|
||||
// and returns a Committer. For example:
|
||||
//
|
||||
// hook := func(next ent.Committer) ent.Committer {
|
||||
// return ent.CommitFunc(func(ctx context.Context, tx *ent.Tx) error {
|
||||
// // Do some stuff before.
|
||||
// if err := next.Commit(ctx, tx); err != nil {
|
||||
// return err
|
||||
// }
|
||||
// // Do some stuff after.
|
||||
// return nil
|
||||
// })
|
||||
// }
|
||||
//
|
||||
CommitHook func(Committer) Committer
|
||||
)
|
||||
|
||||
// Commit calls f(ctx, m).
|
||||
func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error {
|
||||
return f(ctx, tx)
|
||||
}
|
||||
|
||||
// Commit commits the transaction.
|
||||
func (tx *Tx) Commit() error {
|
||||
txDriver := tx.config.driver.(*txDriver)
|
||||
var fn Committer = CommitFunc(func(context.Context, *Tx) error {
|
||||
return txDriver.tx.Commit()
|
||||
})
|
||||
txDriver.mu.Lock()
|
||||
hooks := append([]CommitHook(nil), txDriver.onCommit...)
|
||||
txDriver.mu.Unlock()
|
||||
for i := len(hooks) - 1; i >= 0; i-- {
|
||||
fn = hooks[i](fn)
|
||||
}
|
||||
return fn.Commit(tx.ctx, tx)
|
||||
}
|
||||
|
||||
// OnCommit adds a hook to call on commit.
|
||||
func (tx *Tx) OnCommit(f CommitHook) {
|
||||
txDriver := tx.config.driver.(*txDriver)
|
||||
txDriver.mu.Lock()
|
||||
txDriver.onCommit = append(txDriver.onCommit, f)
|
||||
txDriver.mu.Unlock()
|
||||
}
|
||||
|
||||
type (
|
||||
// Rollbacker is the interface that wraps the Rollback method.
|
||||
Rollbacker interface {
|
||||
Rollback(context.Context, *Tx) error
|
||||
}
|
||||
|
||||
// The RollbackFunc type is an adapter to allow the use of ordinary
|
||||
// function as a Rollbacker. If f is a function with the appropriate
|
||||
// signature, RollbackFunc(f) is a Rollbacker that calls f.
|
||||
RollbackFunc func(context.Context, *Tx) error
|
||||
|
||||
// RollbackHook defines the "rollback middleware". A function that gets a Rollbacker
|
||||
// and returns a Rollbacker. For example:
|
||||
//
|
||||
// hook := func(next ent.Rollbacker) ent.Rollbacker {
|
||||
// return ent.RollbackFunc(func(ctx context.Context, tx *ent.Tx) error {
|
||||
// // Do some stuff before.
|
||||
// if err := next.Rollback(ctx, tx); err != nil {
|
||||
// return err
|
||||
// }
|
||||
// // Do some stuff after.
|
||||
// return nil
|
||||
// })
|
||||
// }
|
||||
//
|
||||
RollbackHook func(Rollbacker) Rollbacker
|
||||
)
|
||||
|
||||
// Rollback calls f(ctx, m).
|
||||
func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error {
|
||||
return f(ctx, tx)
|
||||
}
|
||||
|
||||
// Rollback rollbacks the transaction.
|
||||
func (tx *Tx) Rollback() error {
|
||||
txDriver := tx.config.driver.(*txDriver)
|
||||
var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error {
|
||||
return txDriver.tx.Rollback()
|
||||
})
|
||||
txDriver.mu.Lock()
|
||||
hooks := append([]RollbackHook(nil), txDriver.onRollback...)
|
||||
txDriver.mu.Unlock()
|
||||
for i := len(hooks) - 1; i >= 0; i-- {
|
||||
fn = hooks[i](fn)
|
||||
}
|
||||
return fn.Rollback(tx.ctx, tx)
|
||||
}
|
||||
|
||||
// OnRollback adds a hook to call on rollback.
|
||||
func (tx *Tx) OnRollback(f RollbackHook) {
|
||||
txDriver := tx.config.driver.(*txDriver)
|
||||
txDriver.mu.Lock()
|
||||
txDriver.onRollback = append(txDriver.onRollback, f)
|
||||
txDriver.mu.Unlock()
|
||||
}
|
||||
|
||||
// Client returns a Client that binds to current transaction.
|
||||
func (tx *Tx) Client() *Client {
|
||||
tx.clientOnce.Do(func() {
|
||||
tx.client = &Client{config: tx.config}
|
||||
tx.client.init()
|
||||
})
|
||||
return tx.client
|
||||
}
|
||||
|
||||
func (tx *Tx) init() {
|
||||
tx.User = NewUserClient(tx.config)
|
||||
}
|
||||
|
||||
// txDriver wraps the given dialect.Tx with a nop dialect.Driver implementation.
|
||||
// The idea is to support transactions without adding any extra code to the builders.
|
||||
// When a builder calls to driver.Tx(), it gets the same dialect.Tx instance.
|
||||
// Commit and Rollback are nop for the internal builders and the user must call one
|
||||
// of them in order to commit or rollback the transaction.
|
||||
//
|
||||
// If a closed transaction is embedded in one of the generated entities, and the entity
|
||||
// applies a query, for example: User.QueryXXX(), the query will be executed
|
||||
// through the driver which created this transaction.
|
||||
//
|
||||
// Note that txDriver is not goroutine safe.
|
||||
type txDriver struct {
|
||||
// the driver we started the transaction from.
|
||||
drv dialect.Driver
|
||||
// tx is the underlying transaction.
|
||||
tx dialect.Tx
|
||||
// completion hooks.
|
||||
mu sync.Mutex
|
||||
onCommit []CommitHook
|
||||
onRollback []RollbackHook
|
||||
}
|
||||
|
||||
// newTx creates a new transactional driver.
|
||||
func newTx(ctx context.Context, drv dialect.Driver) (*txDriver, error) {
|
||||
tx, err := drv.Tx(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &txDriver{tx: tx, drv: drv}, nil
|
||||
}
|
||||
|
||||
// Tx returns the transaction wrapper (txDriver) to avoid Commit or Rollback calls
|
||||
// from the internal builders. Should be called only by the internal builders.
|
||||
func (tx *txDriver) Tx(context.Context) (dialect.Tx, error) { return tx, nil }
|
||||
|
||||
// Dialect returns the dialect of the driver we started the transaction from.
|
||||
func (tx *txDriver) Dialect() string { return tx.drv.Dialect() }
|
||||
|
||||
// Close is a nop close.
|
||||
func (*txDriver) Close() error { return nil }
|
||||
|
||||
// Commit is a nop commit for the internal builders.
|
||||
// User must call `Tx.Commit` in order to commit the transaction.
|
||||
func (*txDriver) Commit() error { return nil }
|
||||
|
||||
// Rollback is a nop rollback for the internal builders.
|
||||
// User must call `Tx.Rollback` in order to rollback the transaction.
|
||||
func (*txDriver) Rollback() error { return nil }
|
||||
|
||||
// Exec calls tx.Exec.
|
||||
func (tx *txDriver) Exec(ctx context.Context, query string, args, v any) error {
|
||||
return tx.tx.Exec(ctx, query, args, v)
|
||||
}
|
||||
|
||||
// Query calls tx.Query.
|
||||
func (tx *txDriver) Query(ctx context.Context, query string, args, v any) error {
|
||||
return tx.tx.Query(ctx, query, args, v)
|
||||
}
|
||||
|
||||
var _ dialect.Driver = (*txDriver)(nil)
|
||||
268
entc/integration/json/ent/user.go
Normal file
268
entc/integration/json/ent/user.go
Normal file
@@ -0,0 +1,268 @@
|
||||
// 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 (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/entc/integration/json/ent/schema"
|
||||
"entgo.io/ent/entc/integration/json/ent/user"
|
||||
)
|
||||
|
||||
// User is the model entity for the User schema.
|
||||
type User struct {
|
||||
config `json:"-"`
|
||||
// ID of the ent.
|
||||
ID int `json:"id,omitempty"`
|
||||
// T holds the value of the "t" field.
|
||||
T *schema.T `json:"t,omitempty"`
|
||||
// URL holds the value of the "url" field.
|
||||
URL *url.URL `json:"url,omitempty"`
|
||||
// URLs holds the value of the "URLs" field.
|
||||
URLs []*url.URL `json:"urls,omitempty"`
|
||||
// Raw holds the value of the "raw" field.
|
||||
Raw json.RawMessage `json:"raw,omitempty"`
|
||||
// Dirs holds the value of the "dirs" field.
|
||||
Dirs []http.Dir `json:"dirs,omitempty"`
|
||||
// Ints holds the value of the "ints" field.
|
||||
Ints []int `json:"ints,omitempty"`
|
||||
// Floats holds the value of the "floats" field.
|
||||
Floats []float64 `json:"floats,omitempty"`
|
||||
// Strings holds the value of the "strings" field.
|
||||
Strings []string `json:"strings,omitempty"`
|
||||
// IntsValidate holds the value of the "ints_validate" field.
|
||||
IntsValidate []int `json:"ints_validate,omitempty"`
|
||||
// FloatsValidate holds the value of the "floats_validate" field.
|
||||
FloatsValidate []float64 `json:"floats_validate,omitempty"`
|
||||
// StringsValidate holds the value of the "strings_validate" field.
|
||||
StringsValidate []string `json:"strings_validate,omitempty"`
|
||||
// Addr holds the value of the "addr" field.
|
||||
Addr schema.Addr `json:"-"`
|
||||
// Unknown holds the value of the "unknown" field.
|
||||
Unknown any `json:"unknown,omitempty"`
|
||||
selectValues sql.SelectValues
|
||||
}
|
||||
|
||||
// scanValues returns the types for scanning values from sql.Rows.
|
||||
func (*User) scanValues(columns []string) ([]any, error) {
|
||||
values := make([]any, len(columns))
|
||||
for i := range columns {
|
||||
switch columns[i] {
|
||||
case user.FieldT, user.FieldURL, user.FieldURLs, user.FieldRaw, user.FieldDirs, user.FieldInts, user.FieldFloats, user.FieldStrings, user.FieldIntsValidate, user.FieldFloatsValidate, user.FieldStringsValidate, user.FieldAddr, user.FieldUnknown:
|
||||
values[i] = new([]byte)
|
||||
case user.FieldID:
|
||||
values[i] = new(sql.NullInt64)
|
||||
default:
|
||||
values[i] = new(sql.UnknownType)
|
||||
}
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
// assignValues assigns the values that were returned from sql.Rows (after scanning)
|
||||
// to the User fields.
|
||||
func (_m *User) assignValues(columns []string, values []any) error {
|
||||
if m, n := len(values), len(columns); m < n {
|
||||
return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
|
||||
}
|
||||
for i := range columns {
|
||||
switch columns[i] {
|
||||
case user.FieldID:
|
||||
value, ok := values[i].(*sql.NullInt64)
|
||||
if !ok {
|
||||
return fmt.Errorf("unexpected type %T for field id", value)
|
||||
}
|
||||
_m.ID = int(value.Int64)
|
||||
case user.FieldT:
|
||||
if value, ok := values[i].(*[]byte); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field t", values[i])
|
||||
} else if value != nil && len(*value) > 0 {
|
||||
if err := json.Unmarshal(*value, &_m.T); err != nil {
|
||||
return fmt.Errorf("unmarshal field t: %w", err)
|
||||
}
|
||||
}
|
||||
case user.FieldURL:
|
||||
if value, ok := values[i].(*[]byte); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field url", values[i])
|
||||
} else if value != nil && len(*value) > 0 {
|
||||
if err := json.Unmarshal(*value, &_m.URL); err != nil {
|
||||
return fmt.Errorf("unmarshal field url: %w", err)
|
||||
}
|
||||
}
|
||||
case user.FieldURLs:
|
||||
if value, ok := values[i].(*[]byte); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field URLs", values[i])
|
||||
} else if value != nil && len(*value) > 0 {
|
||||
if err := json.Unmarshal(*value, &_m.URLs); err != nil {
|
||||
return fmt.Errorf("unmarshal field URLs: %w", err)
|
||||
}
|
||||
}
|
||||
case user.FieldRaw:
|
||||
if value, ok := values[i].(*[]byte); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field raw", values[i])
|
||||
} else if value != nil && len(*value) > 0 {
|
||||
if err := json.Unmarshal(*value, &_m.Raw); err != nil {
|
||||
return fmt.Errorf("unmarshal field raw: %w", err)
|
||||
}
|
||||
}
|
||||
case user.FieldDirs:
|
||||
if value, ok := values[i].(*[]byte); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field dirs", values[i])
|
||||
} else if value != nil && len(*value) > 0 {
|
||||
if err := json.Unmarshal(*value, &_m.Dirs); err != nil {
|
||||
return fmt.Errorf("unmarshal field dirs: %w", err)
|
||||
}
|
||||
}
|
||||
case user.FieldInts:
|
||||
if value, ok := values[i].(*[]byte); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field ints", values[i])
|
||||
} else if value != nil && len(*value) > 0 {
|
||||
if err := json.Unmarshal(*value, &_m.Ints); err != nil {
|
||||
return fmt.Errorf("unmarshal field ints: %w", err)
|
||||
}
|
||||
}
|
||||
case user.FieldFloats:
|
||||
if value, ok := values[i].(*[]byte); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field floats", values[i])
|
||||
} else if value != nil && len(*value) > 0 {
|
||||
if err := json.Unmarshal(*value, &_m.Floats); err != nil {
|
||||
return fmt.Errorf("unmarshal field floats: %w", err)
|
||||
}
|
||||
}
|
||||
case user.FieldStrings:
|
||||
if value, ok := values[i].(*[]byte); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field strings", values[i])
|
||||
} else if value != nil && len(*value) > 0 {
|
||||
if err := json.Unmarshal(*value, &_m.Strings); err != nil {
|
||||
return fmt.Errorf("unmarshal field strings: %w", err)
|
||||
}
|
||||
}
|
||||
case user.FieldIntsValidate:
|
||||
if value, ok := values[i].(*[]byte); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field ints_validate", values[i])
|
||||
} else if value != nil && len(*value) > 0 {
|
||||
if err := json.Unmarshal(*value, &_m.IntsValidate); err != nil {
|
||||
return fmt.Errorf("unmarshal field ints_validate: %w", err)
|
||||
}
|
||||
}
|
||||
case user.FieldFloatsValidate:
|
||||
if value, ok := values[i].(*[]byte); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field floats_validate", values[i])
|
||||
} else if value != nil && len(*value) > 0 {
|
||||
if err := json.Unmarshal(*value, &_m.FloatsValidate); err != nil {
|
||||
return fmt.Errorf("unmarshal field floats_validate: %w", err)
|
||||
}
|
||||
}
|
||||
case user.FieldStringsValidate:
|
||||
if value, ok := values[i].(*[]byte); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field strings_validate", values[i])
|
||||
} else if value != nil && len(*value) > 0 {
|
||||
if err := json.Unmarshal(*value, &_m.StringsValidate); err != nil {
|
||||
return fmt.Errorf("unmarshal field strings_validate: %w", err)
|
||||
}
|
||||
}
|
||||
case user.FieldAddr:
|
||||
if value, ok := values[i].(*[]byte); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field addr", values[i])
|
||||
} else if value != nil && len(*value) > 0 {
|
||||
if err := json.Unmarshal(*value, &_m.Addr); err != nil {
|
||||
return fmt.Errorf("unmarshal field addr: %w", err)
|
||||
}
|
||||
}
|
||||
case user.FieldUnknown:
|
||||
if value, ok := values[i].(*[]byte); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field unknown", values[i])
|
||||
} else if value != nil && len(*value) > 0 {
|
||||
if err := json.Unmarshal(*value, &_m.Unknown); err != nil {
|
||||
return fmt.Errorf("unmarshal field unknown: %w", err)
|
||||
}
|
||||
}
|
||||
default:
|
||||
_m.selectValues.Set(columns[i], values[i])
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value returns the ent.Value that was dynamically selected and assigned to the User.
|
||||
// This includes values selected through modifiers, order, etc.
|
||||
func (_m *User) Value(name string) (ent.Value, error) {
|
||||
return _m.selectValues.Get(name)
|
||||
}
|
||||
|
||||
// Update returns a builder for updating this User.
|
||||
// Note that you need to call User.Unwrap() before calling this method if this User
|
||||
// was returned from a transaction, and the transaction was committed or rolled back.
|
||||
func (_m *User) Update() *UserUpdateOne {
|
||||
return NewUserClient(_m.config).UpdateOne(_m)
|
||||
}
|
||||
|
||||
// Unwrap unwraps the User entity that was returned from a transaction after it was closed,
|
||||
// so that all future queries will be executed through the driver which created the transaction.
|
||||
func (_m *User) Unwrap() *User {
|
||||
_tx, ok := _m.config.driver.(*txDriver)
|
||||
if !ok {
|
||||
panic("ent: User is not a transactional entity")
|
||||
}
|
||||
_m.config.driver = _tx.drv
|
||||
return _m
|
||||
}
|
||||
|
||||
// String implements the fmt.Stringer.
|
||||
func (_m *User) String() string {
|
||||
var builder strings.Builder
|
||||
builder.WriteString("User(")
|
||||
builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
|
||||
builder.WriteString("t=")
|
||||
builder.WriteString(fmt.Sprintf("%v", _m.T))
|
||||
builder.WriteString(", ")
|
||||
builder.WriteString("url=")
|
||||
builder.WriteString(fmt.Sprintf("%v", _m.URL))
|
||||
builder.WriteString(", ")
|
||||
builder.WriteString("URLs=")
|
||||
builder.WriteString(fmt.Sprintf("%v", _m.URLs))
|
||||
builder.WriteString(", ")
|
||||
builder.WriteString("raw=")
|
||||
builder.WriteString(fmt.Sprintf("%v", _m.Raw))
|
||||
builder.WriteString(", ")
|
||||
builder.WriteString("dirs=")
|
||||
builder.WriteString(fmt.Sprintf("%v", _m.Dirs))
|
||||
builder.WriteString(", ")
|
||||
builder.WriteString("ints=")
|
||||
builder.WriteString(fmt.Sprintf("%v", _m.Ints))
|
||||
builder.WriteString(", ")
|
||||
builder.WriteString("floats=")
|
||||
builder.WriteString(fmt.Sprintf("%v", _m.Floats))
|
||||
builder.WriteString(", ")
|
||||
builder.WriteString("strings=")
|
||||
builder.WriteString(fmt.Sprintf("%v", _m.Strings))
|
||||
builder.WriteString(", ")
|
||||
builder.WriteString("ints_validate=")
|
||||
builder.WriteString(fmt.Sprintf("%v", _m.IntsValidate))
|
||||
builder.WriteString(", ")
|
||||
builder.WriteString("floats_validate=")
|
||||
builder.WriteString(fmt.Sprintf("%v", _m.FloatsValidate))
|
||||
builder.WriteString(", ")
|
||||
builder.WriteString("strings_validate=")
|
||||
builder.WriteString(fmt.Sprintf("%v", _m.StringsValidate))
|
||||
builder.WriteString(", ")
|
||||
builder.WriteString("addr=<sensitive>")
|
||||
builder.WriteString(", ")
|
||||
builder.WriteString("unknown=")
|
||||
builder.WriteString(fmt.Sprintf("%v", _m.Unknown))
|
||||
builder.WriteByte(')')
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
// Users is a parsable slice of User.
|
||||
type Users []*User
|
||||
1040
entc/integration/json/ent/user/mutation.go
Normal file
1040
entc/integration/json/ent/user/mutation.go
Normal file
File diff suppressed because it is too large
Load Diff
97
entc/integration/json/ent/user/user.go
Normal file
97
entc/integration/json/ent/user/user.go
Normal file
@@ -0,0 +1,97 @@
|
||||
// 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 user
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
)
|
||||
|
||||
const (
|
||||
// Label holds the string label denoting the user type in the database.
|
||||
Label = "user"
|
||||
// FieldID holds the string denoting the id field in the database.
|
||||
FieldID = "id"
|
||||
// FieldT holds the string denoting the t field in the database.
|
||||
FieldT = "t"
|
||||
// FieldURL holds the string denoting the url field in the database.
|
||||
FieldURL = "url"
|
||||
// FieldURLs holds the string denoting the urls field in the database.
|
||||
FieldURLs = "urls"
|
||||
// FieldRaw holds the string denoting the raw field in the database.
|
||||
FieldRaw = "raw"
|
||||
// FieldDirs holds the string denoting the dirs field in the database.
|
||||
FieldDirs = "dirs"
|
||||
// FieldInts holds the string denoting the ints field in the database.
|
||||
FieldInts = "ints"
|
||||
// FieldFloats holds the string denoting the floats field in the database.
|
||||
FieldFloats = "floats"
|
||||
// FieldStrings holds the string denoting the strings field in the database.
|
||||
FieldStrings = "strings"
|
||||
// FieldIntsValidate holds the string denoting the ints_validate field in the database.
|
||||
FieldIntsValidate = "ints_validate"
|
||||
// FieldFloatsValidate holds the string denoting the floats_validate field in the database.
|
||||
FieldFloatsValidate = "floats_validate"
|
||||
// FieldStringsValidate holds the string denoting the strings_validate field in the database.
|
||||
FieldStringsValidate = "strings_validate"
|
||||
// FieldAddr holds the string denoting the addr field in the database.
|
||||
FieldAddr = "addr"
|
||||
// FieldUnknown holds the string denoting the unknown field in the database.
|
||||
FieldUnknown = "unknown"
|
||||
// Table holds the table name of the user in the database.
|
||||
Table = "users"
|
||||
)
|
||||
|
||||
// Columns holds all SQL columns for user fields.
|
||||
var Columns = []string{
|
||||
FieldID,
|
||||
FieldT,
|
||||
FieldURL,
|
||||
FieldURLs,
|
||||
FieldRaw,
|
||||
FieldDirs,
|
||||
FieldInts,
|
||||
FieldFloats,
|
||||
FieldStrings,
|
||||
FieldIntsValidate,
|
||||
FieldFloatsValidate,
|
||||
FieldStringsValidate,
|
||||
FieldAddr,
|
||||
FieldUnknown,
|
||||
}
|
||||
|
||||
// ValidColumn reports if the column name is valid (part of the table columns).
|
||||
func ValidColumn(column string) bool {
|
||||
for i := range Columns {
|
||||
if column == Columns[i] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
var (
|
||||
// DefaultDirs holds the default value on creation for the "dirs" field.
|
||||
DefaultDirs func() []http.Dir
|
||||
// DefaultInts holds the default value on creation for the "ints" field.
|
||||
DefaultInts []int
|
||||
// IntsValidateValidator is a validator for the "ints_validate" field. It is called by the builders before save.
|
||||
IntsValidateValidator func([]int) error
|
||||
// FloatsValidateValidator is a validator for the "floats_validate" field. It is called by the builders before save.
|
||||
FloatsValidateValidator func([]float64) error
|
||||
// StringsValidateValidator is a validator for the "strings_validate" field. It is called by the builders before save.
|
||||
StringsValidateValidator func([]string) error
|
||||
)
|
||||
|
||||
// OrderOption defines the ordering options for the User queries.
|
||||
type OrderOption func(*sql.Selector)
|
||||
|
||||
// ByID orders the results by the id field.
|
||||
func ByID(opts ...sql.OrderTermOption) OrderOption {
|
||||
return sql.OrderByField(FieldID, opts...).ToFunc()
|
||||
}
|
||||
192
entc/integration/json/ent/user/where.go
Normal file
192
entc/integration/json/ent/user/where.go
Normal file
@@ -0,0 +1,192 @@
|
||||
// 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 user
|
||||
|
||||
import (
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/entc/integration/json/ent/predicate"
|
||||
)
|
||||
|
||||
// ID filters vertices based on their ID field.
|
||||
func ID(id int) predicate.User {
|
||||
return predicate.User(sql.FieldEQ(FieldID, id))
|
||||
}
|
||||
|
||||
// IDEQ applies the EQ predicate on the ID field.
|
||||
func IDEQ(id int) predicate.User {
|
||||
return predicate.User(sql.FieldEQ(FieldID, id))
|
||||
}
|
||||
|
||||
// IDNEQ applies the NEQ predicate on the ID field.
|
||||
func IDNEQ(id int) predicate.User {
|
||||
return predicate.User(sql.FieldNEQ(FieldID, id))
|
||||
}
|
||||
|
||||
// IDIn applies the In predicate on the ID field.
|
||||
func IDIn(ids ...int) predicate.User {
|
||||
return predicate.User(sql.FieldIn(FieldID, ids...))
|
||||
}
|
||||
|
||||
// IDNotIn applies the NotIn predicate on the ID field.
|
||||
func IDNotIn(ids ...int) predicate.User {
|
||||
return predicate.User(sql.FieldNotIn(FieldID, ids...))
|
||||
}
|
||||
|
||||
// IDGT applies the GT predicate on the ID field.
|
||||
func IDGT(id int) predicate.User {
|
||||
return predicate.User(sql.FieldGT(FieldID, id))
|
||||
}
|
||||
|
||||
// IDGTE applies the GTE predicate on the ID field.
|
||||
func IDGTE(id int) predicate.User {
|
||||
return predicate.User(sql.FieldGTE(FieldID, id))
|
||||
}
|
||||
|
||||
// IDLT applies the LT predicate on the ID field.
|
||||
func IDLT(id int) predicate.User {
|
||||
return predicate.User(sql.FieldLT(FieldID, id))
|
||||
}
|
||||
|
||||
// IDLTE applies the LTE predicate on the ID field.
|
||||
func IDLTE(id int) predicate.User {
|
||||
return predicate.User(sql.FieldLTE(FieldID, id))
|
||||
}
|
||||
|
||||
// TIsNil applies the IsNil predicate on the "t" field.
|
||||
func TIsNil() predicate.User {
|
||||
return predicate.User(sql.FieldIsNull(FieldT))
|
||||
}
|
||||
|
||||
// TNotNil applies the NotNil predicate on the "t" field.
|
||||
func TNotNil() predicate.User {
|
||||
return predicate.User(sql.FieldNotNull(FieldT))
|
||||
}
|
||||
|
||||
// URLIsNil applies the IsNil predicate on the "url" field.
|
||||
func URLIsNil() predicate.User {
|
||||
return predicate.User(sql.FieldIsNull(FieldURL))
|
||||
}
|
||||
|
||||
// URLNotNil applies the NotNil predicate on the "url" field.
|
||||
func URLNotNil() predicate.User {
|
||||
return predicate.User(sql.FieldNotNull(FieldURL))
|
||||
}
|
||||
|
||||
// URLsIsNil applies the IsNil predicate on the "URLs" field.
|
||||
func URLsIsNil() predicate.User {
|
||||
return predicate.User(sql.FieldIsNull(FieldURLs))
|
||||
}
|
||||
|
||||
// URLsNotNil applies the NotNil predicate on the "URLs" field.
|
||||
func URLsNotNil() predicate.User {
|
||||
return predicate.User(sql.FieldNotNull(FieldURLs))
|
||||
}
|
||||
|
||||
// RawIsNil applies the IsNil predicate on the "raw" field.
|
||||
func RawIsNil() predicate.User {
|
||||
return predicate.User(sql.FieldIsNull(FieldRaw))
|
||||
}
|
||||
|
||||
// RawNotNil applies the NotNil predicate on the "raw" field.
|
||||
func RawNotNil() predicate.User {
|
||||
return predicate.User(sql.FieldNotNull(FieldRaw))
|
||||
}
|
||||
|
||||
// IntsIsNil applies the IsNil predicate on the "ints" field.
|
||||
func IntsIsNil() predicate.User {
|
||||
return predicate.User(sql.FieldIsNull(FieldInts))
|
||||
}
|
||||
|
||||
// IntsNotNil applies the NotNil predicate on the "ints" field.
|
||||
func IntsNotNil() predicate.User {
|
||||
return predicate.User(sql.FieldNotNull(FieldInts))
|
||||
}
|
||||
|
||||
// FloatsIsNil applies the IsNil predicate on the "floats" field.
|
||||
func FloatsIsNil() predicate.User {
|
||||
return predicate.User(sql.FieldIsNull(FieldFloats))
|
||||
}
|
||||
|
||||
// FloatsNotNil applies the NotNil predicate on the "floats" field.
|
||||
func FloatsNotNil() predicate.User {
|
||||
return predicate.User(sql.FieldNotNull(FieldFloats))
|
||||
}
|
||||
|
||||
// StringsIsNil applies the IsNil predicate on the "strings" field.
|
||||
func StringsIsNil() predicate.User {
|
||||
return predicate.User(sql.FieldIsNull(FieldStrings))
|
||||
}
|
||||
|
||||
// StringsNotNil applies the NotNil predicate on the "strings" field.
|
||||
func StringsNotNil() predicate.User {
|
||||
return predicate.User(sql.FieldNotNull(FieldStrings))
|
||||
}
|
||||
|
||||
// IntsValidateIsNil applies the IsNil predicate on the "ints_validate" field.
|
||||
func IntsValidateIsNil() predicate.User {
|
||||
return predicate.User(sql.FieldIsNull(FieldIntsValidate))
|
||||
}
|
||||
|
||||
// IntsValidateNotNil applies the NotNil predicate on the "ints_validate" field.
|
||||
func IntsValidateNotNil() predicate.User {
|
||||
return predicate.User(sql.FieldNotNull(FieldIntsValidate))
|
||||
}
|
||||
|
||||
// FloatsValidateIsNil applies the IsNil predicate on the "floats_validate" field.
|
||||
func FloatsValidateIsNil() predicate.User {
|
||||
return predicate.User(sql.FieldIsNull(FieldFloatsValidate))
|
||||
}
|
||||
|
||||
// FloatsValidateNotNil applies the NotNil predicate on the "floats_validate" field.
|
||||
func FloatsValidateNotNil() predicate.User {
|
||||
return predicate.User(sql.FieldNotNull(FieldFloatsValidate))
|
||||
}
|
||||
|
||||
// StringsValidateIsNil applies the IsNil predicate on the "strings_validate" field.
|
||||
func StringsValidateIsNil() predicate.User {
|
||||
return predicate.User(sql.FieldIsNull(FieldStringsValidate))
|
||||
}
|
||||
|
||||
// StringsValidateNotNil applies the NotNil predicate on the "strings_validate" field.
|
||||
func StringsValidateNotNil() predicate.User {
|
||||
return predicate.User(sql.FieldNotNull(FieldStringsValidate))
|
||||
}
|
||||
|
||||
// AddrIsNil applies the IsNil predicate on the "addr" field.
|
||||
func AddrIsNil() predicate.User {
|
||||
return predicate.User(sql.FieldIsNull(FieldAddr))
|
||||
}
|
||||
|
||||
// AddrNotNil applies the NotNil predicate on the "addr" field.
|
||||
func AddrNotNil() predicate.User {
|
||||
return predicate.User(sql.FieldNotNull(FieldAddr))
|
||||
}
|
||||
|
||||
// UnknownIsNil applies the IsNil predicate on the "unknown" field.
|
||||
func UnknownIsNil() predicate.User {
|
||||
return predicate.User(sql.FieldIsNull(FieldUnknown))
|
||||
}
|
||||
|
||||
// UnknownNotNil applies the NotNil predicate on the "unknown" field.
|
||||
func UnknownNotNil() predicate.User {
|
||||
return predicate.User(sql.FieldNotNull(FieldUnknown))
|
||||
}
|
||||
|
||||
// And groups predicates with the AND operator between them.
|
||||
func And(predicates ...predicate.User) predicate.User {
|
||||
return predicate.User(sql.AndPredicates(predicates...))
|
||||
}
|
||||
|
||||
// Or groups predicates with the OR operator between them.
|
||||
func Or(predicates ...predicate.User) predicate.User {
|
||||
return predicate.User(sql.OrPredicates(predicates...))
|
||||
}
|
||||
|
||||
// Not applies the not operator on the given predicate.
|
||||
func Not(p predicate.User) predicate.User {
|
||||
return predicate.User(sql.NotPredicates(p))
|
||||
}
|
||||
348
entc/integration/json/ent/user_create.go
Normal file
348
entc/integration/json/ent/user_create.go
Normal file
@@ -0,0 +1,348 @@
|
||||
// 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"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
"entgo.io/ent/entc/integration/json/ent/schema"
|
||||
"entgo.io/ent/entc/integration/json/ent/user"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
// UserCreate is the builder for creating a User entity.
|
||||
type UserCreate struct {
|
||||
config
|
||||
mutation *UserMutation
|
||||
hooks []Hook
|
||||
}
|
||||
|
||||
// SetT sets the "t" field.
|
||||
func (_c *UserCreate) SetT(v *schema.T) *UserCreate {
|
||||
_c.mutation.SetT(v)
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetURL sets the "url" field.
|
||||
func (_c *UserCreate) SetURL(v *url.URL) *UserCreate {
|
||||
_c.mutation.SetURL(v)
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetURLs sets the "URLs" field.
|
||||
func (_c *UserCreate) SetURLs(v []*url.URL) *UserCreate {
|
||||
_c.mutation.SetURLs(v)
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetRaw sets the "raw" field.
|
||||
func (_c *UserCreate) SetRaw(v json.RawMessage) *UserCreate {
|
||||
_c.mutation.SetRaw(v)
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetDirs sets the "dirs" field.
|
||||
func (_c *UserCreate) SetDirs(v []http.Dir) *UserCreate {
|
||||
_c.mutation.SetDirs(v)
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetInts sets the "ints" field.
|
||||
func (_c *UserCreate) SetInts(v []int) *UserCreate {
|
||||
_c.mutation.SetInts(v)
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetFloats sets the "floats" field.
|
||||
func (_c *UserCreate) SetFloats(v []float64) *UserCreate {
|
||||
_c.mutation.SetFloats(v)
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetStrings sets the "strings" field.
|
||||
func (_c *UserCreate) SetStrings(v []string) *UserCreate {
|
||||
_c.mutation.SetStrings(v)
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetIntsValidate sets the "ints_validate" field.
|
||||
func (_c *UserCreate) SetIntsValidate(v []int) *UserCreate {
|
||||
_c.mutation.SetIntsValidate(v)
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetFloatsValidate sets the "floats_validate" field.
|
||||
func (_c *UserCreate) SetFloatsValidate(v []float64) *UserCreate {
|
||||
_c.mutation.SetFloatsValidate(v)
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetStringsValidate sets the "strings_validate" field.
|
||||
func (_c *UserCreate) SetStringsValidate(v []string) *UserCreate {
|
||||
_c.mutation.SetStringsValidate(v)
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetAddr sets the "addr" field.
|
||||
func (_c *UserCreate) SetAddr(v schema.Addr) *UserCreate {
|
||||
_c.mutation.SetAddr(v)
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetNillableAddr sets the "addr" field if the given value is not nil.
|
||||
func (_c *UserCreate) SetNillableAddr(v *schema.Addr) *UserCreate {
|
||||
if v != nil {
|
||||
_c.SetAddr(*v)
|
||||
}
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetUnknown sets the "unknown" field.
|
||||
func (_c *UserCreate) SetUnknown(v any) *UserCreate {
|
||||
_c.mutation.SetUnknown(v)
|
||||
return _c
|
||||
}
|
||||
|
||||
// Mutation returns the UserMutation object of the builder.
|
||||
func (_c *UserCreate) Mutation() *UserMutation {
|
||||
return _c.mutation
|
||||
}
|
||||
|
||||
// Save creates the User in the database.
|
||||
func (_c *UserCreate) Save(ctx context.Context) (*User, error) {
|
||||
_c.defaults()
|
||||
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
|
||||
}
|
||||
|
||||
// SaveX calls Save and panics if Save returns an error.
|
||||
func (_c *UserCreate) SaveX(ctx context.Context) *User {
|
||||
v, err := _c.Save(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Exec executes the query.
|
||||
func (_c *UserCreate) Exec(ctx context.Context) error {
|
||||
_, err := _c.Save(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
// ExecX is like Exec, but panics if an error occurs.
|
||||
func (_c *UserCreate) 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 *UserCreate) defaults() {
|
||||
if _, ok := _c.mutation.Dirs(); !ok {
|
||||
v := user.DefaultDirs()
|
||||
_c.mutation.SetDirs(v)
|
||||
}
|
||||
if _, ok := _c.mutation.Ints(); !ok {
|
||||
v := user.DefaultInts
|
||||
_c.mutation.SetInts(v)
|
||||
}
|
||||
}
|
||||
|
||||
// check runs all checks and user-defined validators on the builder.
|
||||
func (_c *UserCreate) check() error {
|
||||
if _, ok := _c.mutation.Dirs(); !ok {
|
||||
return &ValidationError{Name: "dirs", err: errors.New(`ent: missing required field "User.dirs"`)}
|
||||
}
|
||||
if v, ok := _c.mutation.IntsValidate(); ok {
|
||||
if err := user.IntsValidateValidator(v); err != nil {
|
||||
return &ValidationError{Name: "ints_validate", err: fmt.Errorf(`ent: validator failed for field "User.ints_validate": %w`, err)}
|
||||
}
|
||||
}
|
||||
if v, ok := _c.mutation.FloatsValidate(); ok {
|
||||
if err := user.FloatsValidateValidator(v); err != nil {
|
||||
return &ValidationError{Name: "floats_validate", err: fmt.Errorf(`ent: validator failed for field "User.floats_validate": %w`, err)}
|
||||
}
|
||||
}
|
||||
if v, ok := _c.mutation.StringsValidate(); ok {
|
||||
if err := user.StringsValidateValidator(v); err != nil {
|
||||
return &ValidationError{Name: "strings_validate", err: fmt.Errorf(`ent: validator failed for field "User.strings_validate": %w`, err)}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (_c *UserCreate) sqlSave(ctx context.Context) (*User, 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 *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) {
|
||||
var (
|
||||
_node = &User{config: _c.config}
|
||||
_spec = sqlgraph.NewCreateSpec(user.Table, sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt))
|
||||
)
|
||||
if value, ok := _c.mutation.T(); ok {
|
||||
_spec.SetField(user.FieldT, field.TypeJSON, value)
|
||||
_node.T = value
|
||||
}
|
||||
if value, ok := _c.mutation.URL(); ok {
|
||||
_spec.SetField(user.FieldURL, field.TypeJSON, value)
|
||||
_node.URL = value
|
||||
}
|
||||
if value, ok := _c.mutation.URLs(); ok {
|
||||
_spec.SetField(user.FieldURLs, field.TypeJSON, value)
|
||||
_node.URLs = value
|
||||
}
|
||||
if value, ok := _c.mutation.Raw(); ok {
|
||||
_spec.SetField(user.FieldRaw, field.TypeJSON, value)
|
||||
_node.Raw = value
|
||||
}
|
||||
if value, ok := _c.mutation.Dirs(); ok {
|
||||
_spec.SetField(user.FieldDirs, field.TypeJSON, value)
|
||||
_node.Dirs = value
|
||||
}
|
||||
if value, ok := _c.mutation.Ints(); ok {
|
||||
_spec.SetField(user.FieldInts, field.TypeJSON, value)
|
||||
_node.Ints = value
|
||||
}
|
||||
if value, ok := _c.mutation.Floats(); ok {
|
||||
_spec.SetField(user.FieldFloats, field.TypeJSON, value)
|
||||
_node.Floats = value
|
||||
}
|
||||
if value, ok := _c.mutation.Strings(); ok {
|
||||
_spec.SetField(user.FieldStrings, field.TypeJSON, value)
|
||||
_node.Strings = value
|
||||
}
|
||||
if value, ok := _c.mutation.IntsValidate(); ok {
|
||||
_spec.SetField(user.FieldIntsValidate, field.TypeJSON, value)
|
||||
_node.IntsValidate = value
|
||||
}
|
||||
if value, ok := _c.mutation.FloatsValidate(); ok {
|
||||
_spec.SetField(user.FieldFloatsValidate, field.TypeJSON, value)
|
||||
_node.FloatsValidate = value
|
||||
}
|
||||
if value, ok := _c.mutation.StringsValidate(); ok {
|
||||
_spec.SetField(user.FieldStringsValidate, field.TypeJSON, value)
|
||||
_node.StringsValidate = value
|
||||
}
|
||||
if value, ok := _c.mutation.Addr(); ok {
|
||||
_spec.SetField(user.FieldAddr, field.TypeJSON, value)
|
||||
_node.Addr = value
|
||||
}
|
||||
if value, ok := _c.mutation.Unknown(); ok {
|
||||
_spec.SetField(user.FieldUnknown, field.TypeJSON, value)
|
||||
_node.Unknown = value
|
||||
}
|
||||
return _node, _spec
|
||||
}
|
||||
|
||||
// UserCreateBulk is the builder for creating many User entities in bulk.
|
||||
type UserCreateBulk struct {
|
||||
config
|
||||
err error
|
||||
builders []*UserCreate
|
||||
}
|
||||
|
||||
// Save creates the User entities in the database.
|
||||
func (_c *UserCreateBulk) Save(ctx context.Context) ([]*User, error) {
|
||||
if _c.err != nil {
|
||||
return nil, _c.err
|
||||
}
|
||||
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
|
||||
nodes := make([]*User, 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.(*UserMutation)
|
||||
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}
|
||||
// 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 *UserCreateBulk) SaveX(ctx context.Context) []*User {
|
||||
v, err := _c.Save(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Exec executes the query.
|
||||
func (_c *UserCreateBulk) Exec(ctx context.Context) error {
|
||||
_, err := _c.Save(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
// ExecX is like Exec, but panics if an error occurs.
|
||||
func (_c *UserCreateBulk) ExecX(ctx context.Context) {
|
||||
if err := _c.Exec(ctx); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
92
entc/integration/json/ent/user_delete.go
Normal file
92
entc/integration/json/ent/user_delete.go
Normal file
@@ -0,0 +1,92 @@
|
||||
// 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"
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
"entgo.io/ent/entc/integration/json/ent/predicate"
|
||||
"entgo.io/ent/entc/integration/json/ent/user"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
// UserDelete is the builder for deleting a User entity.
|
||||
type UserDelete struct {
|
||||
config
|
||||
hooks []Hook
|
||||
mutation *UserMutation
|
||||
}
|
||||
|
||||
// Where appends a list predicates to the UserDelete builder.
|
||||
func (_d *UserDelete) Where(ps ...predicate.User) *UserDelete {
|
||||
_d.mutation.Where(ps...)
|
||||
return _d
|
||||
}
|
||||
|
||||
// Exec executes the deletion query and returns how many vertices were deleted.
|
||||
func (_d *UserDelete) Exec(ctx context.Context) (int, error) {
|
||||
return withHooks(ctx, _d.sqlExec, _d.mutation, _d.hooks)
|
||||
}
|
||||
|
||||
// ExecX is like Exec, but panics if an error occurs.
|
||||
func (_d *UserDelete) ExecX(ctx context.Context) int {
|
||||
n, err := _d.Exec(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (_d *UserDelete) sqlExec(ctx context.Context) (int, error) {
|
||||
_spec := sqlgraph.NewDeleteSpec(user.Table, sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt))
|
||||
if ps := _d.mutation.Predicates(); len(ps) > 0 {
|
||||
_spec.Predicate = func(selector *sql.Selector) {
|
||||
for i := range ps {
|
||||
ps[i](selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
affected, err := sqlgraph.DeleteNodes(ctx, _d.driver, _spec)
|
||||
if err != nil && sqlgraph.IsConstraintError(err) {
|
||||
err = &ConstraintError{msg: err.Error(), wrap: err}
|
||||
}
|
||||
_d.mutation.done = true
|
||||
return affected, err
|
||||
}
|
||||
|
||||
// UserDeleteOne is the builder for deleting a single User entity.
|
||||
type UserDeleteOne struct {
|
||||
_d *UserDelete
|
||||
}
|
||||
|
||||
// Where appends a list predicates to the UserDelete builder.
|
||||
func (_d *UserDeleteOne) Where(ps ...predicate.User) *UserDeleteOne {
|
||||
_d._d.mutation.Where(ps...)
|
||||
return _d
|
||||
}
|
||||
|
||||
// Exec executes the deletion query.
|
||||
func (_d *UserDeleteOne) Exec(ctx context.Context) error {
|
||||
n, err := _d._d.Exec(ctx)
|
||||
switch {
|
||||
case err != nil:
|
||||
return err
|
||||
case n == 0:
|
||||
return &NotFoundError{user.Label}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// ExecX is like Exec, but panics if an error occurs.
|
||||
func (_d *UserDeleteOne) ExecX(ctx context.Context) {
|
||||
if err := _d.Exec(ctx); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
554
entc/integration/json/ent/user_query.go
Normal file
554
entc/integration/json/ent/user_query.go
Normal file
@@ -0,0 +1,554 @@
|
||||
// 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"
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
"entgo.io/ent/entc/integration/json/ent/predicate"
|
||||
"entgo.io/ent/entc/integration/json/ent/user"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
// UserQuery is the builder for querying User entities.
|
||||
type UserQuery struct {
|
||||
config
|
||||
ctx *QueryContext
|
||||
order []user.OrderOption
|
||||
inters []Interceptor
|
||||
predicates []predicate.User
|
||||
modifiers []func(*sql.Selector)
|
||||
// intermediate query (i.e. traversal path).
|
||||
sql *sql.Selector
|
||||
path func(context.Context) (*sql.Selector, error)
|
||||
}
|
||||
|
||||
// Where adds a new predicate for the UserQuery builder.
|
||||
func (_q *UserQuery) Where(ps ...predicate.User) *UserQuery {
|
||||
_q.predicates = append(_q.predicates, ps...)
|
||||
return _q
|
||||
}
|
||||
|
||||
// Limit the number of records to be returned by this query.
|
||||
func (_q *UserQuery) Limit(limit int) *UserQuery {
|
||||
_q.ctx.Limit = &limit
|
||||
return _q
|
||||
}
|
||||
|
||||
// Offset to start from.
|
||||
func (_q *UserQuery) Offset(offset int) *UserQuery {
|
||||
_q.ctx.Offset = &offset
|
||||
return _q
|
||||
}
|
||||
|
||||
// Unique configures the query builder to filter duplicate records on query.
|
||||
// By default, unique is set to true, and can be disabled using this method.
|
||||
func (_q *UserQuery) Unique(unique bool) *UserQuery {
|
||||
_q.ctx.Unique = &unique
|
||||
return _q
|
||||
}
|
||||
|
||||
// Order specifies how the records should be ordered.
|
||||
func (_q *UserQuery) Order(o ...user.OrderOption) *UserQuery {
|
||||
_q.order = append(_q.order, o...)
|
||||
return _q
|
||||
}
|
||||
|
||||
// First returns the first User entity from the query.
|
||||
// Returns a *NotFoundError when no User was found.
|
||||
func (_q *UserQuery) First(ctx context.Context) (*User, error) {
|
||||
nodes, err := _q.Limit(1).All(setContextOp(ctx, _q.ctx, ent.OpQueryFirst))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(nodes) == 0 {
|
||||
return nil, &NotFoundError{user.Label}
|
||||
}
|
||||
return nodes[0], nil
|
||||
}
|
||||
|
||||
// FirstX is like First, but panics if an error occurs.
|
||||
func (_q *UserQuery) FirstX(ctx context.Context) *User {
|
||||
node, err := _q.First(ctx)
|
||||
if err != nil && !IsNotFound(err) {
|
||||
panic(err)
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
// FirstID returns the first User ID from the query.
|
||||
// Returns a *NotFoundError when no User ID was found.
|
||||
func (_q *UserQuery) FirstID(ctx context.Context) (id int, err error) {
|
||||
var ids []int
|
||||
if ids, err = _q.Limit(1).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryFirstID)); err != nil {
|
||||
return
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
err = &NotFoundError{user.Label}
|
||||
return
|
||||
}
|
||||
return ids[0], nil
|
||||
}
|
||||
|
||||
// FirstIDX is like FirstID, but panics if an error occurs.
|
||||
func (_q *UserQuery) FirstIDX(ctx context.Context) int {
|
||||
id, err := _q.FirstID(ctx)
|
||||
if err != nil && !IsNotFound(err) {
|
||||
panic(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// Only returns a single User entity found by the query, ensuring it only returns one.
|
||||
// Returns a *NotSingularError when more than one User entity is found.
|
||||
// Returns a *NotFoundError when no User entities are found.
|
||||
func (_q *UserQuery) Only(ctx context.Context) (*User, error) {
|
||||
nodes, err := _q.Limit(2).All(setContextOp(ctx, _q.ctx, ent.OpQueryOnly))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch len(nodes) {
|
||||
case 1:
|
||||
return nodes[0], nil
|
||||
case 0:
|
||||
return nil, &NotFoundError{user.Label}
|
||||
default:
|
||||
return nil, &NotSingularError{user.Label}
|
||||
}
|
||||
}
|
||||
|
||||
// OnlyX is like Only, but panics if an error occurs.
|
||||
func (_q *UserQuery) OnlyX(ctx context.Context) *User {
|
||||
node, err := _q.Only(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
// OnlyID is like Only, but returns the only User ID in the query.
|
||||
// Returns a *NotSingularError when more than one User ID is found.
|
||||
// Returns a *NotFoundError when no entities are found.
|
||||
func (_q *UserQuery) OnlyID(ctx context.Context) (id int, err error) {
|
||||
var ids []int
|
||||
if ids, err = _q.Limit(2).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryOnlyID)); err != nil {
|
||||
return
|
||||
}
|
||||
switch len(ids) {
|
||||
case 1:
|
||||
id = ids[0]
|
||||
case 0:
|
||||
err = &NotFoundError{user.Label}
|
||||
default:
|
||||
err = &NotSingularError{user.Label}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// OnlyIDX is like OnlyID, but panics if an error occurs.
|
||||
func (_q *UserQuery) OnlyIDX(ctx context.Context) int {
|
||||
id, err := _q.OnlyID(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// All executes the query and returns a list of Users.
|
||||
func (_q *UserQuery) All(ctx context.Context) ([]*User, error) {
|
||||
ctx = setContextOp(ctx, _q.ctx, ent.OpQueryAll)
|
||||
if err := _q.prepareQuery(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
qr := querierAll[[]*User, *UserQuery]()
|
||||
return withInterceptors[[]*User](ctx, _q, qr, _q.inters)
|
||||
}
|
||||
|
||||
// AllX is like All, but panics if an error occurs.
|
||||
func (_q *UserQuery) AllX(ctx context.Context) []*User {
|
||||
nodes, err := _q.All(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return nodes
|
||||
}
|
||||
|
||||
// IDs executes the query and returns a list of User IDs.
|
||||
func (_q *UserQuery) IDs(ctx context.Context) (ids []int, err error) {
|
||||
if _q.ctx.Unique == nil && _q.path != nil {
|
||||
_q.Unique(true)
|
||||
}
|
||||
ctx = setContextOp(ctx, _q.ctx, ent.OpQueryIDs)
|
||||
if err = _q.Select(user.FieldID).Scan(ctx, &ids); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
// IDsX is like IDs, but panics if an error occurs.
|
||||
func (_q *UserQuery) IDsX(ctx context.Context) []int {
|
||||
ids, err := _q.IDs(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
// Count returns the count of the given query.
|
||||
func (_q *UserQuery) Count(ctx context.Context) (int, error) {
|
||||
ctx = setContextOp(ctx, _q.ctx, ent.OpQueryCount)
|
||||
if err := _q.prepareQuery(ctx); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return withInterceptors[int](ctx, _q, querierCount[*UserQuery](), _q.inters)
|
||||
}
|
||||
|
||||
// CountX is like Count, but panics if an error occurs.
|
||||
func (_q *UserQuery) CountX(ctx context.Context) int {
|
||||
count, err := _q.Count(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// Exist returns true if the query has elements in the graph.
|
||||
func (_q *UserQuery) Exist(ctx context.Context) (bool, error) {
|
||||
ctx = setContextOp(ctx, _q.ctx, ent.OpQueryExist)
|
||||
switch _, err := _q.FirstID(ctx); {
|
||||
case IsNotFound(err):
|
||||
return false, nil
|
||||
case err != nil:
|
||||
return false, fmt.Errorf("ent: check existence: %w", err)
|
||||
default:
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
// ExistX is like Exist, but panics if an error occurs.
|
||||
func (_q *UserQuery) ExistX(ctx context.Context) bool {
|
||||
exist, err := _q.Exist(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return exist
|
||||
}
|
||||
|
||||
// Clone returns a duplicate of the UserQuery builder, including all associated steps. It can be
|
||||
// used to prepare common query builders and use them differently after the clone is made.
|
||||
func (_q *UserQuery) Clone() *UserQuery {
|
||||
if _q == nil {
|
||||
return nil
|
||||
}
|
||||
return &UserQuery{
|
||||
config: _q.config,
|
||||
ctx: _q.ctx.Clone(),
|
||||
order: append([]user.OrderOption{}, _q.order...),
|
||||
inters: append([]Interceptor{}, _q.inters...),
|
||||
predicates: append([]predicate.User{}, _q.predicates...),
|
||||
// clone intermediate query.
|
||||
sql: _q.sql.Clone(),
|
||||
path: _q.path,
|
||||
modifiers: append([]func(*sql.Selector){}, _q.modifiers...),
|
||||
}
|
||||
}
|
||||
|
||||
// GroupBy is used to group vertices by one or more fields/columns.
|
||||
// It is often used with aggregate functions, like: count, max, mean, min, sum.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// var v []struct {
|
||||
// T *schema.T `json:"t,omitempty"`
|
||||
// Count int `json:"count,omitempty"`
|
||||
// }
|
||||
//
|
||||
// client.User.Query().
|
||||
// GroupBy(user.FieldT).
|
||||
// Aggregate(ent.Count()).
|
||||
// Scan(ctx, &v)
|
||||
func (_q *UserQuery) GroupBy(field string, fields ...string) *UserGroupBy {
|
||||
_q.ctx.Fields = append([]string{field}, fields...)
|
||||
grbuild := &UserGroupBy{build: _q}
|
||||
grbuild.flds = &_q.ctx.Fields
|
||||
grbuild.label = user.Label
|
||||
grbuild.scan = grbuild.Scan
|
||||
return grbuild
|
||||
}
|
||||
|
||||
// Select allows the selection one or more fields/columns for the given query,
|
||||
// instead of selecting all fields in the entity.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// var v []struct {
|
||||
// T *schema.T `json:"t,omitempty"`
|
||||
// }
|
||||
//
|
||||
// client.User.Query().
|
||||
// Select(user.FieldT).
|
||||
// Scan(ctx, &v)
|
||||
func (_q *UserQuery) Select(fields ...string) *UserSelect {
|
||||
_q.ctx.Fields = append(_q.ctx.Fields, fields...)
|
||||
sbuild := &UserSelect{UserQuery: _q}
|
||||
sbuild.label = user.Label
|
||||
sbuild.flds, sbuild.scan = &_q.ctx.Fields, sbuild.Scan
|
||||
return sbuild
|
||||
}
|
||||
|
||||
// Aggregate returns a UserSelect configured with the given aggregations.
|
||||
func (_q *UserQuery) Aggregate(fns ...AggregateFunc) *UserSelect {
|
||||
return _q.Select().Aggregate(fns...)
|
||||
}
|
||||
|
||||
func (_q *UserQuery) prepareQuery(ctx context.Context) error {
|
||||
for _, inter := range _q.inters {
|
||||
if inter == nil {
|
||||
return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
|
||||
}
|
||||
if trv, ok := inter.(Traverser); ok {
|
||||
if err := trv.Traverse(ctx, _q); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, f := range _q.ctx.Fields {
|
||||
if !user.ValidColumn(f) {
|
||||
return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
|
||||
}
|
||||
}
|
||||
if _q.path != nil {
|
||||
prev, err := _q.path(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_q.sql = prev
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (_q *UserQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*User, error) {
|
||||
var (
|
||||
nodes = []*User{}
|
||||
_spec = _q.querySpec()
|
||||
)
|
||||
_spec.ScanValues = func(columns []string) ([]any, error) {
|
||||
return (*User).scanValues(nil, columns)
|
||||
}
|
||||
_spec.Assign = func(columns []string, values []any) error {
|
||||
node := &User{config: _q.config}
|
||||
nodes = append(nodes, node)
|
||||
return node.assignValues(columns, values)
|
||||
}
|
||||
if len(_q.modifiers) > 0 {
|
||||
_spec.Modifiers = _q.modifiers
|
||||
}
|
||||
for i := range hooks {
|
||||
hooks[i](ctx, _spec)
|
||||
}
|
||||
if err := sqlgraph.QueryNodes(ctx, _q.driver, _spec); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(nodes) == 0 {
|
||||
return nodes, nil
|
||||
}
|
||||
return nodes, nil
|
||||
}
|
||||
|
||||
func (_q *UserQuery) sqlCount(ctx context.Context) (int, error) {
|
||||
_spec := _q.querySpec()
|
||||
if len(_q.modifiers) > 0 {
|
||||
_spec.Modifiers = _q.modifiers
|
||||
}
|
||||
_spec.Node.Columns = _q.ctx.Fields
|
||||
if len(_q.ctx.Fields) > 0 {
|
||||
_spec.Unique = _q.ctx.Unique != nil && *_q.ctx.Unique
|
||||
}
|
||||
return sqlgraph.CountNodes(ctx, _q.driver, _spec)
|
||||
}
|
||||
|
||||
func (_q *UserQuery) querySpec() *sqlgraph.QuerySpec {
|
||||
_spec := sqlgraph.NewQuerySpec(user.Table, user.Columns, sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt))
|
||||
_spec.From = _q.sql
|
||||
if unique := _q.ctx.Unique; unique != nil {
|
||||
_spec.Unique = *unique
|
||||
} else if _q.path != nil {
|
||||
_spec.Unique = true
|
||||
}
|
||||
if fields := _q.ctx.Fields; len(fields) > 0 {
|
||||
_spec.Node.Columns = make([]string, 0, len(fields))
|
||||
_spec.Node.Columns = append(_spec.Node.Columns, user.FieldID)
|
||||
for i := range fields {
|
||||
if fields[i] != user.FieldID {
|
||||
_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
if ps := _q.predicates; len(ps) > 0 {
|
||||
_spec.Predicate = func(selector *sql.Selector) {
|
||||
for i := range ps {
|
||||
ps[i](selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
if limit := _q.ctx.Limit; limit != nil {
|
||||
_spec.Limit = *limit
|
||||
}
|
||||
if offset := _q.ctx.Offset; offset != nil {
|
||||
_spec.Offset = *offset
|
||||
}
|
||||
if ps := _q.order; len(ps) > 0 {
|
||||
_spec.Order = func(selector *sql.Selector) {
|
||||
for i := range ps {
|
||||
ps[i](selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
return _spec
|
||||
}
|
||||
|
||||
func (_q *UserQuery) sqlQuery(ctx context.Context) *sql.Selector {
|
||||
builder := sql.Dialect(_q.driver.Dialect())
|
||||
t1 := builder.Table(user.Table)
|
||||
columns := _q.ctx.Fields
|
||||
if len(columns) == 0 {
|
||||
columns = user.Columns
|
||||
}
|
||||
selector := builder.Select(t1.Columns(columns...)...).From(t1)
|
||||
if _q.sql != nil {
|
||||
selector = _q.sql
|
||||
selector.Select(selector.Columns(columns...)...)
|
||||
}
|
||||
if _q.ctx.Unique != nil && *_q.ctx.Unique {
|
||||
selector.Distinct()
|
||||
}
|
||||
for _, m := range _q.modifiers {
|
||||
m(selector)
|
||||
}
|
||||
for _, p := range _q.predicates {
|
||||
p(selector)
|
||||
}
|
||||
for _, p := range _q.order {
|
||||
p(selector)
|
||||
}
|
||||
if offset := _q.ctx.Offset; offset != nil {
|
||||
// limit is mandatory for offset clause. We start
|
||||
// with default value, and override it below if needed.
|
||||
selector.Offset(*offset).Limit(math.MaxInt32)
|
||||
}
|
||||
if limit := _q.ctx.Limit; limit != nil {
|
||||
selector.Limit(*limit)
|
||||
}
|
||||
return selector
|
||||
}
|
||||
|
||||
// Modify adds a query modifier for attaching custom logic to queries.
|
||||
func (_q *UserQuery) Modify(modifiers ...func(s *sql.Selector)) *UserSelect {
|
||||
_q.modifiers = append(_q.modifiers, modifiers...)
|
||||
return _q.Select()
|
||||
}
|
||||
|
||||
// UserGroupBy is the group-by builder for User entities.
|
||||
type UserGroupBy struct {
|
||||
selector
|
||||
build *UserQuery
|
||||
}
|
||||
|
||||
// Aggregate adds the given aggregation functions to the group-by query.
|
||||
func (_g *UserGroupBy) Aggregate(fns ...AggregateFunc) *UserGroupBy {
|
||||
_g.fns = append(_g.fns, fns...)
|
||||
return _g
|
||||
}
|
||||
|
||||
// Scan applies the selector query and scans the result into the given value.
|
||||
func (_g *UserGroupBy) Scan(ctx context.Context, v any) error {
|
||||
ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy)
|
||||
if err := _g.build.prepareQuery(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return scanWithInterceptors[*UserQuery, *UserGroupBy](ctx, _g.build, _g, _g.build.inters, v)
|
||||
}
|
||||
|
||||
func (_g *UserGroupBy) sqlScan(ctx context.Context, root *UserQuery, v any) error {
|
||||
selector := root.sqlQuery(ctx).Select()
|
||||
aggregation := make([]string, 0, len(_g.fns))
|
||||
for _, fn := range _g.fns {
|
||||
aggregation = append(aggregation, fn(selector))
|
||||
}
|
||||
if len(selector.SelectedColumns()) == 0 {
|
||||
columns := make([]string, 0, len(*_g.flds)+len(_g.fns))
|
||||
for _, f := range *_g.flds {
|
||||
columns = append(columns, selector.C(f))
|
||||
}
|
||||
columns = append(columns, aggregation...)
|
||||
selector.Select(columns...)
|
||||
}
|
||||
selector.GroupBy(selector.Columns(*_g.flds...)...)
|
||||
if err := selector.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
rows := &sql.Rows{}
|
||||
query, args := selector.Query()
|
||||
if err := _g.build.driver.Query(ctx, query, args, rows); err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
return sql.ScanSlice(rows, v)
|
||||
}
|
||||
|
||||
// UserSelect is the builder for selecting fields of User entities.
|
||||
type UserSelect struct {
|
||||
*UserQuery
|
||||
selector
|
||||
}
|
||||
|
||||
// Aggregate adds the given aggregation functions to the selector query.
|
||||
func (_s *UserSelect) Aggregate(fns ...AggregateFunc) *UserSelect {
|
||||
_s.fns = append(_s.fns, fns...)
|
||||
return _s
|
||||
}
|
||||
|
||||
// Scan applies the selector query and scans the result into the given value.
|
||||
func (_s *UserSelect) Scan(ctx context.Context, v any) error {
|
||||
ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect)
|
||||
if err := _s.prepareQuery(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return scanWithInterceptors[*UserQuery, *UserSelect](ctx, _s.UserQuery, _s, _s.inters, v)
|
||||
}
|
||||
|
||||
func (_s *UserSelect) sqlScan(ctx context.Context, root *UserQuery, v any) error {
|
||||
selector := root.sqlQuery(ctx)
|
||||
aggregation := make([]string, 0, len(_s.fns))
|
||||
for _, fn := range _s.fns {
|
||||
aggregation = append(aggregation, fn(selector))
|
||||
}
|
||||
switch n := len(*_s.selector.flds); {
|
||||
case n == 0 && len(aggregation) > 0:
|
||||
selector.Select(aggregation...)
|
||||
case n != 0 && len(aggregation) > 0:
|
||||
selector.AppendSelect(aggregation...)
|
||||
}
|
||||
rows := &sql.Rows{}
|
||||
query, args := selector.Query()
|
||||
if err := _s.driver.Query(ctx, query, args, rows); err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
return sql.ScanSlice(rows, v)
|
||||
}
|
||||
|
||||
// Modify adds a query modifier for attaching custom logic to queries.
|
||||
func (_s *UserSelect) Modify(modifiers ...func(s *sql.Selector)) *UserSelect {
|
||||
_s.modifiers = append(_s.modifiers, modifiers...)
|
||||
return _s
|
||||
}
|
||||
910
entc/integration/json/ent/user_update.go
Normal file
910
entc/integration/json/ent/user_update.go
Normal file
@@ -0,0 +1,910 @@
|
||||
// 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"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
"entgo.io/ent/dialect/sql/sqljson"
|
||||
"entgo.io/ent/entc/integration/json/ent/predicate"
|
||||
"entgo.io/ent/entc/integration/json/ent/schema"
|
||||
"entgo.io/ent/entc/integration/json/ent/user"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
// UserUpdate is the builder for updating User entities.
|
||||
type UserUpdate struct {
|
||||
config
|
||||
hooks []Hook
|
||||
mutation *UserMutation
|
||||
modifiers []func(*sql.UpdateBuilder)
|
||||
}
|
||||
|
||||
// Where appends a list predicates to the UserUpdate builder.
|
||||
func (_u *UserUpdate) Where(ps ...predicate.User) *UserUpdate {
|
||||
_u.mutation.Where(ps...)
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetT sets the "t" field.
|
||||
func (_u *UserUpdate) SetT(v *schema.T) *UserUpdate {
|
||||
_u.mutation.SetT(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearT clears the value of the "t" field.
|
||||
func (_u *UserUpdate) ClearT() *UserUpdate {
|
||||
_u.mutation.ClearT()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetURL sets the "url" field.
|
||||
func (_u *UserUpdate) SetURL(v *url.URL) *UserUpdate {
|
||||
_u.mutation.SetURL(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearURL clears the value of the "url" field.
|
||||
func (_u *UserUpdate) ClearURL() *UserUpdate {
|
||||
_u.mutation.ClearURL()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetURLs sets the "URLs" field.
|
||||
func (_u *UserUpdate) SetURLs(v []*url.URL) *UserUpdate {
|
||||
_u.mutation.SetURLs(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// AppendURLs appends value to the "URLs" field.
|
||||
func (_u *UserUpdate) AppendURLs(v []*url.URL) *UserUpdate {
|
||||
_u.mutation.AppendURLs(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearURLs clears the value of the "URLs" field.
|
||||
func (_u *UserUpdate) ClearURLs() *UserUpdate {
|
||||
_u.mutation.ClearURLs()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetRaw sets the "raw" field.
|
||||
func (_u *UserUpdate) SetRaw(v json.RawMessage) *UserUpdate {
|
||||
_u.mutation.SetRaw(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// AppendRaw appends value to the "raw" field.
|
||||
func (_u *UserUpdate) AppendRaw(v json.RawMessage) *UserUpdate {
|
||||
_u.mutation.AppendRaw(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearRaw clears the value of the "raw" field.
|
||||
func (_u *UserUpdate) ClearRaw() *UserUpdate {
|
||||
_u.mutation.ClearRaw()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetDirs sets the "dirs" field.
|
||||
func (_u *UserUpdate) SetDirs(v []http.Dir) *UserUpdate {
|
||||
_u.mutation.SetDirs(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// AppendDirs appends value to the "dirs" field.
|
||||
func (_u *UserUpdate) AppendDirs(v []http.Dir) *UserUpdate {
|
||||
_u.mutation.AppendDirs(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetInts sets the "ints" field.
|
||||
func (_u *UserUpdate) SetInts(v []int) *UserUpdate {
|
||||
_u.mutation.SetInts(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// AppendInts appends value to the "ints" field.
|
||||
func (_u *UserUpdate) AppendInts(v []int) *UserUpdate {
|
||||
_u.mutation.AppendInts(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearInts clears the value of the "ints" field.
|
||||
func (_u *UserUpdate) ClearInts() *UserUpdate {
|
||||
_u.mutation.ClearInts()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetFloats sets the "floats" field.
|
||||
func (_u *UserUpdate) SetFloats(v []float64) *UserUpdate {
|
||||
_u.mutation.SetFloats(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// AppendFloats appends value to the "floats" field.
|
||||
func (_u *UserUpdate) AppendFloats(v []float64) *UserUpdate {
|
||||
_u.mutation.AppendFloats(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearFloats clears the value of the "floats" field.
|
||||
func (_u *UserUpdate) ClearFloats() *UserUpdate {
|
||||
_u.mutation.ClearFloats()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetStrings sets the "strings" field.
|
||||
func (_u *UserUpdate) SetStrings(v []string) *UserUpdate {
|
||||
_u.mutation.SetStrings(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// AppendStrings appends value to the "strings" field.
|
||||
func (_u *UserUpdate) AppendStrings(v []string) *UserUpdate {
|
||||
_u.mutation.AppendStrings(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearStrings clears the value of the "strings" field.
|
||||
func (_u *UserUpdate) ClearStrings() *UserUpdate {
|
||||
_u.mutation.ClearStrings()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetIntsValidate sets the "ints_validate" field.
|
||||
func (_u *UserUpdate) SetIntsValidate(v []int) *UserUpdate {
|
||||
_u.mutation.SetIntsValidate(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// AppendIntsValidate appends value to the "ints_validate" field.
|
||||
func (_u *UserUpdate) AppendIntsValidate(v []int) *UserUpdate {
|
||||
_u.mutation.AppendIntsValidate(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearIntsValidate clears the value of the "ints_validate" field.
|
||||
func (_u *UserUpdate) ClearIntsValidate() *UserUpdate {
|
||||
_u.mutation.ClearIntsValidate()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetFloatsValidate sets the "floats_validate" field.
|
||||
func (_u *UserUpdate) SetFloatsValidate(v []float64) *UserUpdate {
|
||||
_u.mutation.SetFloatsValidate(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// AppendFloatsValidate appends value to the "floats_validate" field.
|
||||
func (_u *UserUpdate) AppendFloatsValidate(v []float64) *UserUpdate {
|
||||
_u.mutation.AppendFloatsValidate(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearFloatsValidate clears the value of the "floats_validate" field.
|
||||
func (_u *UserUpdate) ClearFloatsValidate() *UserUpdate {
|
||||
_u.mutation.ClearFloatsValidate()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetStringsValidate sets the "strings_validate" field.
|
||||
func (_u *UserUpdate) SetStringsValidate(v []string) *UserUpdate {
|
||||
_u.mutation.SetStringsValidate(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// AppendStringsValidate appends value to the "strings_validate" field.
|
||||
func (_u *UserUpdate) AppendStringsValidate(v []string) *UserUpdate {
|
||||
_u.mutation.AppendStringsValidate(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearStringsValidate clears the value of the "strings_validate" field.
|
||||
func (_u *UserUpdate) ClearStringsValidate() *UserUpdate {
|
||||
_u.mutation.ClearStringsValidate()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetAddr sets the "addr" field.
|
||||
func (_u *UserUpdate) SetAddr(v schema.Addr) *UserUpdate {
|
||||
_u.mutation.SetAddr(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetNillableAddr sets the "addr" field if the given value is not nil.
|
||||
func (_u *UserUpdate) SetNillableAddr(v *schema.Addr) *UserUpdate {
|
||||
if v != nil {
|
||||
_u.SetAddr(*v)
|
||||
}
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearAddr clears the value of the "addr" field.
|
||||
func (_u *UserUpdate) ClearAddr() *UserUpdate {
|
||||
_u.mutation.ClearAddr()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetUnknown sets the "unknown" field.
|
||||
func (_u *UserUpdate) SetUnknown(v any) *UserUpdate {
|
||||
_u.mutation.SetUnknown(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearUnknown clears the value of the "unknown" field.
|
||||
func (_u *UserUpdate) ClearUnknown() *UserUpdate {
|
||||
_u.mutation.ClearUnknown()
|
||||
return _u
|
||||
}
|
||||
|
||||
// Mutation returns the UserMutation object of the builder.
|
||||
func (_u *UserUpdate) Mutation() *UserMutation {
|
||||
return _u.mutation
|
||||
}
|
||||
|
||||
// Save executes the query and returns the number of nodes affected by the update operation.
|
||||
func (_u *UserUpdate) Save(ctx context.Context) (int, error) {
|
||||
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
|
||||
}
|
||||
|
||||
// SaveX is like Save, but panics if an error occurs.
|
||||
func (_u *UserUpdate) SaveX(ctx context.Context) int {
|
||||
affected, err := _u.Save(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return affected
|
||||
}
|
||||
|
||||
// Exec executes the query.
|
||||
func (_u *UserUpdate) Exec(ctx context.Context) error {
|
||||
_, err := _u.Save(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
// ExecX is like Exec, but panics if an error occurs.
|
||||
func (_u *UserUpdate) ExecX(ctx context.Context) {
|
||||
if err := _u.Exec(ctx); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// check runs all checks and user-defined validators on the builder.
|
||||
func (_u *UserUpdate) check() error {
|
||||
if v, ok := _u.mutation.IntsValidate(); ok {
|
||||
if err := user.IntsValidateValidator(v); err != nil {
|
||||
return &ValidationError{Name: "ints_validate", err: fmt.Errorf(`ent: validator failed for field "User.ints_validate": %w`, err)}
|
||||
}
|
||||
}
|
||||
if v, ok := _u.mutation.FloatsValidate(); ok {
|
||||
if err := user.FloatsValidateValidator(v); err != nil {
|
||||
return &ValidationError{Name: "floats_validate", err: fmt.Errorf(`ent: validator failed for field "User.floats_validate": %w`, err)}
|
||||
}
|
||||
}
|
||||
if v, ok := _u.mutation.StringsValidate(); ok {
|
||||
if err := user.StringsValidateValidator(v); err != nil {
|
||||
return &ValidationError{Name: "strings_validate", err: fmt.Errorf(`ent: validator failed for field "User.strings_validate": %w`, err)}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
|
||||
func (_u *UserUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *UserUpdate {
|
||||
_u.modifiers = append(_u.modifiers, modifiers...)
|
||||
return _u
|
||||
}
|
||||
|
||||
func (_u *UserUpdate) sqlSave(ctx context.Context) (_node int, err error) {
|
||||
if err := _u.check(); err != nil {
|
||||
return _node, err
|
||||
}
|
||||
_spec := sqlgraph.NewUpdateSpec(user.Table, user.Columns, sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt))
|
||||
if ps := _u.mutation.Predicates(); len(ps) > 0 {
|
||||
_spec.Predicate = func(selector *sql.Selector) {
|
||||
for i := range ps {
|
||||
ps[i](selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
if value, ok := _u.mutation.T(); ok {
|
||||
_spec.SetField(user.FieldT, field.TypeJSON, value)
|
||||
}
|
||||
if _u.mutation.TCleared() {
|
||||
_spec.ClearField(user.FieldT, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.URL(); ok {
|
||||
_spec.SetField(user.FieldURL, field.TypeJSON, value)
|
||||
}
|
||||
if _u.mutation.URLCleared() {
|
||||
_spec.ClearField(user.FieldURL, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.URLs(); ok {
|
||||
_spec.SetField(user.FieldURLs, field.TypeJSON, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AppendedURLs(); ok {
|
||||
_spec.AddModifier(func(u *sql.UpdateBuilder) {
|
||||
sqljson.Append(u, user.FieldURLs, value)
|
||||
})
|
||||
}
|
||||
if _u.mutation.URLsCleared() {
|
||||
_spec.ClearField(user.FieldURLs, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.Raw(); ok {
|
||||
_spec.SetField(user.FieldRaw, field.TypeJSON, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AppendedRaw(); ok {
|
||||
_spec.AddModifier(func(u *sql.UpdateBuilder) {
|
||||
sqljson.Append(u, user.FieldRaw, value)
|
||||
})
|
||||
}
|
||||
if _u.mutation.RawCleared() {
|
||||
_spec.ClearField(user.FieldRaw, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.Dirs(); ok {
|
||||
_spec.SetField(user.FieldDirs, field.TypeJSON, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AppendedDirs(); ok {
|
||||
_spec.AddModifier(func(u *sql.UpdateBuilder) {
|
||||
sqljson.Append(u, user.FieldDirs, value)
|
||||
})
|
||||
}
|
||||
if value, ok := _u.mutation.Ints(); ok {
|
||||
_spec.SetField(user.FieldInts, field.TypeJSON, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AppendedInts(); ok {
|
||||
_spec.AddModifier(func(u *sql.UpdateBuilder) {
|
||||
sqljson.Append(u, user.FieldInts, value)
|
||||
})
|
||||
}
|
||||
if _u.mutation.IntsCleared() {
|
||||
_spec.ClearField(user.FieldInts, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.Floats(); ok {
|
||||
_spec.SetField(user.FieldFloats, field.TypeJSON, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AppendedFloats(); ok {
|
||||
_spec.AddModifier(func(u *sql.UpdateBuilder) {
|
||||
sqljson.Append(u, user.FieldFloats, value)
|
||||
})
|
||||
}
|
||||
if _u.mutation.FloatsCleared() {
|
||||
_spec.ClearField(user.FieldFloats, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.Strings(); ok {
|
||||
_spec.SetField(user.FieldStrings, field.TypeJSON, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AppendedStrings(); ok {
|
||||
_spec.AddModifier(func(u *sql.UpdateBuilder) {
|
||||
sqljson.Append(u, user.FieldStrings, value)
|
||||
})
|
||||
}
|
||||
if _u.mutation.StringsCleared() {
|
||||
_spec.ClearField(user.FieldStrings, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.IntsValidate(); ok {
|
||||
_spec.SetField(user.FieldIntsValidate, field.TypeJSON, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AppendedIntsValidate(); ok {
|
||||
_spec.AddModifier(func(u *sql.UpdateBuilder) {
|
||||
sqljson.Append(u, user.FieldIntsValidate, value)
|
||||
})
|
||||
}
|
||||
if _u.mutation.IntsValidateCleared() {
|
||||
_spec.ClearField(user.FieldIntsValidate, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.FloatsValidate(); ok {
|
||||
_spec.SetField(user.FieldFloatsValidate, field.TypeJSON, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AppendedFloatsValidate(); ok {
|
||||
_spec.AddModifier(func(u *sql.UpdateBuilder) {
|
||||
sqljson.Append(u, user.FieldFloatsValidate, value)
|
||||
})
|
||||
}
|
||||
if _u.mutation.FloatsValidateCleared() {
|
||||
_spec.ClearField(user.FieldFloatsValidate, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.StringsValidate(); ok {
|
||||
_spec.SetField(user.FieldStringsValidate, field.TypeJSON, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AppendedStringsValidate(); ok {
|
||||
_spec.AddModifier(func(u *sql.UpdateBuilder) {
|
||||
sqljson.Append(u, user.FieldStringsValidate, value)
|
||||
})
|
||||
}
|
||||
if _u.mutation.StringsValidateCleared() {
|
||||
_spec.ClearField(user.FieldStringsValidate, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.Addr(); ok {
|
||||
_spec.SetField(user.FieldAddr, field.TypeJSON, value)
|
||||
}
|
||||
if _u.mutation.AddrCleared() {
|
||||
_spec.ClearField(user.FieldAddr, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.Unknown(); ok {
|
||||
_spec.SetField(user.FieldUnknown, field.TypeJSON, value)
|
||||
}
|
||||
if _u.mutation.UnknownCleared() {
|
||||
_spec.ClearField(user.FieldUnknown, field.TypeJSON)
|
||||
}
|
||||
_spec.AddModifiers(_u.modifiers...)
|
||||
if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
|
||||
if _, ok := err.(*sqlgraph.NotFoundError); ok {
|
||||
err = &NotFoundError{user.Label}
|
||||
} else if sqlgraph.IsConstraintError(err) {
|
||||
err = &ConstraintError{msg: err.Error(), wrap: err}
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
_u.mutation.done = true
|
||||
return _node, nil
|
||||
}
|
||||
|
||||
// UserUpdateOne is the builder for updating a single User entity.
|
||||
type UserUpdateOne struct {
|
||||
config
|
||||
fields []string
|
||||
hooks []Hook
|
||||
mutation *UserMutation
|
||||
modifiers []func(*sql.UpdateBuilder)
|
||||
}
|
||||
|
||||
// SetT sets the "t" field.
|
||||
func (_u *UserUpdateOne) SetT(v *schema.T) *UserUpdateOne {
|
||||
_u.mutation.SetT(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearT clears the value of the "t" field.
|
||||
func (_u *UserUpdateOne) ClearT() *UserUpdateOne {
|
||||
_u.mutation.ClearT()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetURL sets the "url" field.
|
||||
func (_u *UserUpdateOne) SetURL(v *url.URL) *UserUpdateOne {
|
||||
_u.mutation.SetURL(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearURL clears the value of the "url" field.
|
||||
func (_u *UserUpdateOne) ClearURL() *UserUpdateOne {
|
||||
_u.mutation.ClearURL()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetURLs sets the "URLs" field.
|
||||
func (_u *UserUpdateOne) SetURLs(v []*url.URL) *UserUpdateOne {
|
||||
_u.mutation.SetURLs(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// AppendURLs appends value to the "URLs" field.
|
||||
func (_u *UserUpdateOne) AppendURLs(v []*url.URL) *UserUpdateOne {
|
||||
_u.mutation.AppendURLs(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearURLs clears the value of the "URLs" field.
|
||||
func (_u *UserUpdateOne) ClearURLs() *UserUpdateOne {
|
||||
_u.mutation.ClearURLs()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetRaw sets the "raw" field.
|
||||
func (_u *UserUpdateOne) SetRaw(v json.RawMessage) *UserUpdateOne {
|
||||
_u.mutation.SetRaw(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// AppendRaw appends value to the "raw" field.
|
||||
func (_u *UserUpdateOne) AppendRaw(v json.RawMessage) *UserUpdateOne {
|
||||
_u.mutation.AppendRaw(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearRaw clears the value of the "raw" field.
|
||||
func (_u *UserUpdateOne) ClearRaw() *UserUpdateOne {
|
||||
_u.mutation.ClearRaw()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetDirs sets the "dirs" field.
|
||||
func (_u *UserUpdateOne) SetDirs(v []http.Dir) *UserUpdateOne {
|
||||
_u.mutation.SetDirs(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// AppendDirs appends value to the "dirs" field.
|
||||
func (_u *UserUpdateOne) AppendDirs(v []http.Dir) *UserUpdateOne {
|
||||
_u.mutation.AppendDirs(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetInts sets the "ints" field.
|
||||
func (_u *UserUpdateOne) SetInts(v []int) *UserUpdateOne {
|
||||
_u.mutation.SetInts(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// AppendInts appends value to the "ints" field.
|
||||
func (_u *UserUpdateOne) AppendInts(v []int) *UserUpdateOne {
|
||||
_u.mutation.AppendInts(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearInts clears the value of the "ints" field.
|
||||
func (_u *UserUpdateOne) ClearInts() *UserUpdateOne {
|
||||
_u.mutation.ClearInts()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetFloats sets the "floats" field.
|
||||
func (_u *UserUpdateOne) SetFloats(v []float64) *UserUpdateOne {
|
||||
_u.mutation.SetFloats(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// AppendFloats appends value to the "floats" field.
|
||||
func (_u *UserUpdateOne) AppendFloats(v []float64) *UserUpdateOne {
|
||||
_u.mutation.AppendFloats(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearFloats clears the value of the "floats" field.
|
||||
func (_u *UserUpdateOne) ClearFloats() *UserUpdateOne {
|
||||
_u.mutation.ClearFloats()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetStrings sets the "strings" field.
|
||||
func (_u *UserUpdateOne) SetStrings(v []string) *UserUpdateOne {
|
||||
_u.mutation.SetStrings(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// AppendStrings appends value to the "strings" field.
|
||||
func (_u *UserUpdateOne) AppendStrings(v []string) *UserUpdateOne {
|
||||
_u.mutation.AppendStrings(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearStrings clears the value of the "strings" field.
|
||||
func (_u *UserUpdateOne) ClearStrings() *UserUpdateOne {
|
||||
_u.mutation.ClearStrings()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetIntsValidate sets the "ints_validate" field.
|
||||
func (_u *UserUpdateOne) SetIntsValidate(v []int) *UserUpdateOne {
|
||||
_u.mutation.SetIntsValidate(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// AppendIntsValidate appends value to the "ints_validate" field.
|
||||
func (_u *UserUpdateOne) AppendIntsValidate(v []int) *UserUpdateOne {
|
||||
_u.mutation.AppendIntsValidate(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearIntsValidate clears the value of the "ints_validate" field.
|
||||
func (_u *UserUpdateOne) ClearIntsValidate() *UserUpdateOne {
|
||||
_u.mutation.ClearIntsValidate()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetFloatsValidate sets the "floats_validate" field.
|
||||
func (_u *UserUpdateOne) SetFloatsValidate(v []float64) *UserUpdateOne {
|
||||
_u.mutation.SetFloatsValidate(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// AppendFloatsValidate appends value to the "floats_validate" field.
|
||||
func (_u *UserUpdateOne) AppendFloatsValidate(v []float64) *UserUpdateOne {
|
||||
_u.mutation.AppendFloatsValidate(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearFloatsValidate clears the value of the "floats_validate" field.
|
||||
func (_u *UserUpdateOne) ClearFloatsValidate() *UserUpdateOne {
|
||||
_u.mutation.ClearFloatsValidate()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetStringsValidate sets the "strings_validate" field.
|
||||
func (_u *UserUpdateOne) SetStringsValidate(v []string) *UserUpdateOne {
|
||||
_u.mutation.SetStringsValidate(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// AppendStringsValidate appends value to the "strings_validate" field.
|
||||
func (_u *UserUpdateOne) AppendStringsValidate(v []string) *UserUpdateOne {
|
||||
_u.mutation.AppendStringsValidate(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearStringsValidate clears the value of the "strings_validate" field.
|
||||
func (_u *UserUpdateOne) ClearStringsValidate() *UserUpdateOne {
|
||||
_u.mutation.ClearStringsValidate()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetAddr sets the "addr" field.
|
||||
func (_u *UserUpdateOne) SetAddr(v schema.Addr) *UserUpdateOne {
|
||||
_u.mutation.SetAddr(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetNillableAddr sets the "addr" field if the given value is not nil.
|
||||
func (_u *UserUpdateOne) SetNillableAddr(v *schema.Addr) *UserUpdateOne {
|
||||
if v != nil {
|
||||
_u.SetAddr(*v)
|
||||
}
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearAddr clears the value of the "addr" field.
|
||||
func (_u *UserUpdateOne) ClearAddr() *UserUpdateOne {
|
||||
_u.mutation.ClearAddr()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetUnknown sets the "unknown" field.
|
||||
func (_u *UserUpdateOne) SetUnknown(v any) *UserUpdateOne {
|
||||
_u.mutation.SetUnknown(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearUnknown clears the value of the "unknown" field.
|
||||
func (_u *UserUpdateOne) ClearUnknown() *UserUpdateOne {
|
||||
_u.mutation.ClearUnknown()
|
||||
return _u
|
||||
}
|
||||
|
||||
// Mutation returns the UserMutation object of the builder.
|
||||
func (_u *UserUpdateOne) Mutation() *UserMutation {
|
||||
return _u.mutation
|
||||
}
|
||||
|
||||
// Where appends a list predicates to the UserUpdate builder.
|
||||
func (_u *UserUpdateOne) Where(ps ...predicate.User) *UserUpdateOne {
|
||||
_u.mutation.Where(ps...)
|
||||
return _u
|
||||
}
|
||||
|
||||
// Select allows selecting one or more fields (columns) of the returned entity.
|
||||
// The default is selecting all fields defined in the entity schema.
|
||||
func (_u *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne {
|
||||
_u.fields = append([]string{field}, fields...)
|
||||
return _u
|
||||
}
|
||||
|
||||
// Save executes the query and returns the updated User entity.
|
||||
func (_u *UserUpdateOne) Save(ctx context.Context) (*User, error) {
|
||||
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
|
||||
}
|
||||
|
||||
// SaveX is like Save, but panics if an error occurs.
|
||||
func (_u *UserUpdateOne) SaveX(ctx context.Context) *User {
|
||||
node, err := _u.Save(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
// Exec executes the query on the entity.
|
||||
func (_u *UserUpdateOne) Exec(ctx context.Context) error {
|
||||
_, err := _u.Save(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
// ExecX is like Exec, but panics if an error occurs.
|
||||
func (_u *UserUpdateOne) ExecX(ctx context.Context) {
|
||||
if err := _u.Exec(ctx); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// check runs all checks and user-defined validators on the builder.
|
||||
func (_u *UserUpdateOne) check() error {
|
||||
if v, ok := _u.mutation.IntsValidate(); ok {
|
||||
if err := user.IntsValidateValidator(v); err != nil {
|
||||
return &ValidationError{Name: "ints_validate", err: fmt.Errorf(`ent: validator failed for field "User.ints_validate": %w`, err)}
|
||||
}
|
||||
}
|
||||
if v, ok := _u.mutation.FloatsValidate(); ok {
|
||||
if err := user.FloatsValidateValidator(v); err != nil {
|
||||
return &ValidationError{Name: "floats_validate", err: fmt.Errorf(`ent: validator failed for field "User.floats_validate": %w`, err)}
|
||||
}
|
||||
}
|
||||
if v, ok := _u.mutation.StringsValidate(); ok {
|
||||
if err := user.StringsValidateValidator(v); err != nil {
|
||||
return &ValidationError{Name: "strings_validate", err: fmt.Errorf(`ent: validator failed for field "User.strings_validate": %w`, err)}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
|
||||
func (_u *UserUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *UserUpdateOne {
|
||||
_u.modifiers = append(_u.modifiers, modifiers...)
|
||||
return _u
|
||||
}
|
||||
|
||||
func (_u *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) {
|
||||
if err := _u.check(); err != nil {
|
||||
return _node, err
|
||||
}
|
||||
_spec := sqlgraph.NewUpdateSpec(user.Table, user.Columns, sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt))
|
||||
id, ok := _u.mutation.ID()
|
||||
if !ok {
|
||||
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "User.id" for update`)}
|
||||
}
|
||||
_spec.Node.ID.Value = id
|
||||
if fields := _u.fields; len(fields) > 0 {
|
||||
_spec.Node.Columns = make([]string, 0, len(fields))
|
||||
_spec.Node.Columns = append(_spec.Node.Columns, user.FieldID)
|
||||
for _, f := range fields {
|
||||
if !user.ValidColumn(f) {
|
||||
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
|
||||
}
|
||||
if f != user.FieldID {
|
||||
_spec.Node.Columns = append(_spec.Node.Columns, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
if ps := _u.mutation.Predicates(); len(ps) > 0 {
|
||||
_spec.Predicate = func(selector *sql.Selector) {
|
||||
for i := range ps {
|
||||
ps[i](selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
if value, ok := _u.mutation.T(); ok {
|
||||
_spec.SetField(user.FieldT, field.TypeJSON, value)
|
||||
}
|
||||
if _u.mutation.TCleared() {
|
||||
_spec.ClearField(user.FieldT, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.URL(); ok {
|
||||
_spec.SetField(user.FieldURL, field.TypeJSON, value)
|
||||
}
|
||||
if _u.mutation.URLCleared() {
|
||||
_spec.ClearField(user.FieldURL, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.URLs(); ok {
|
||||
_spec.SetField(user.FieldURLs, field.TypeJSON, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AppendedURLs(); ok {
|
||||
_spec.AddModifier(func(u *sql.UpdateBuilder) {
|
||||
sqljson.Append(u, user.FieldURLs, value)
|
||||
})
|
||||
}
|
||||
if _u.mutation.URLsCleared() {
|
||||
_spec.ClearField(user.FieldURLs, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.Raw(); ok {
|
||||
_spec.SetField(user.FieldRaw, field.TypeJSON, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AppendedRaw(); ok {
|
||||
_spec.AddModifier(func(u *sql.UpdateBuilder) {
|
||||
sqljson.Append(u, user.FieldRaw, value)
|
||||
})
|
||||
}
|
||||
if _u.mutation.RawCleared() {
|
||||
_spec.ClearField(user.FieldRaw, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.Dirs(); ok {
|
||||
_spec.SetField(user.FieldDirs, field.TypeJSON, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AppendedDirs(); ok {
|
||||
_spec.AddModifier(func(u *sql.UpdateBuilder) {
|
||||
sqljson.Append(u, user.FieldDirs, value)
|
||||
})
|
||||
}
|
||||
if value, ok := _u.mutation.Ints(); ok {
|
||||
_spec.SetField(user.FieldInts, field.TypeJSON, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AppendedInts(); ok {
|
||||
_spec.AddModifier(func(u *sql.UpdateBuilder) {
|
||||
sqljson.Append(u, user.FieldInts, value)
|
||||
})
|
||||
}
|
||||
if _u.mutation.IntsCleared() {
|
||||
_spec.ClearField(user.FieldInts, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.Floats(); ok {
|
||||
_spec.SetField(user.FieldFloats, field.TypeJSON, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AppendedFloats(); ok {
|
||||
_spec.AddModifier(func(u *sql.UpdateBuilder) {
|
||||
sqljson.Append(u, user.FieldFloats, value)
|
||||
})
|
||||
}
|
||||
if _u.mutation.FloatsCleared() {
|
||||
_spec.ClearField(user.FieldFloats, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.Strings(); ok {
|
||||
_spec.SetField(user.FieldStrings, field.TypeJSON, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AppendedStrings(); ok {
|
||||
_spec.AddModifier(func(u *sql.UpdateBuilder) {
|
||||
sqljson.Append(u, user.FieldStrings, value)
|
||||
})
|
||||
}
|
||||
if _u.mutation.StringsCleared() {
|
||||
_spec.ClearField(user.FieldStrings, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.IntsValidate(); ok {
|
||||
_spec.SetField(user.FieldIntsValidate, field.TypeJSON, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AppendedIntsValidate(); ok {
|
||||
_spec.AddModifier(func(u *sql.UpdateBuilder) {
|
||||
sqljson.Append(u, user.FieldIntsValidate, value)
|
||||
})
|
||||
}
|
||||
if _u.mutation.IntsValidateCleared() {
|
||||
_spec.ClearField(user.FieldIntsValidate, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.FloatsValidate(); ok {
|
||||
_spec.SetField(user.FieldFloatsValidate, field.TypeJSON, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AppendedFloatsValidate(); ok {
|
||||
_spec.AddModifier(func(u *sql.UpdateBuilder) {
|
||||
sqljson.Append(u, user.FieldFloatsValidate, value)
|
||||
})
|
||||
}
|
||||
if _u.mutation.FloatsValidateCleared() {
|
||||
_spec.ClearField(user.FieldFloatsValidate, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.StringsValidate(); ok {
|
||||
_spec.SetField(user.FieldStringsValidate, field.TypeJSON, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AppendedStringsValidate(); ok {
|
||||
_spec.AddModifier(func(u *sql.UpdateBuilder) {
|
||||
sqljson.Append(u, user.FieldStringsValidate, value)
|
||||
})
|
||||
}
|
||||
if _u.mutation.StringsValidateCleared() {
|
||||
_spec.ClearField(user.FieldStringsValidate, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.Addr(); ok {
|
||||
_spec.SetField(user.FieldAddr, field.TypeJSON, value)
|
||||
}
|
||||
if _u.mutation.AddrCleared() {
|
||||
_spec.ClearField(user.FieldAddr, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.Unknown(); ok {
|
||||
_spec.SetField(user.FieldUnknown, field.TypeJSON, value)
|
||||
}
|
||||
if _u.mutation.UnknownCleared() {
|
||||
_spec.ClearField(user.FieldUnknown, field.TypeJSON)
|
||||
}
|
||||
_spec.AddModifiers(_u.modifiers...)
|
||||
_node = &User{config: _u.config}
|
||||
_spec.Assign = _node.assignValues
|
||||
_spec.ScanValues = _node.scanValues
|
||||
if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
|
||||
if _, ok := err.(*sqlgraph.NotFoundError); ok {
|
||||
err = &NotFoundError{user.Label}
|
||||
} else if sqlgraph.IsConstraintError(err) {
|
||||
err = &ConstraintError{msg: err.Error(), wrap: err}
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
_u.mutation.done = true
|
||||
return _node, nil
|
||||
}
|
||||
Reference in New Issue
Block a user