accountserver/ent/client.go

518 lines
16 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"log"
"reflect"
"code.icod.de/auth/accountserver/ent/migrate"
"github.com/google/uuid"
"code.icod.de/auth/accountserver/ent/account"
"code.icod.de/auth/accountserver/ent/email"
"entgo.io/ent"
"entgo.io/ent/dialect"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
)
// 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
// Account is the client for interacting with the Account builders.
Account *AccountClient
// Email is the client for interacting with the Email builders.
Email *EmailClient
}
// 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.Account = NewAccountClient(c.config)
c.Email = NewEmailClient(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,
Account: NewAccountClient(cfg),
Email: NewEmailClient(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,
Account: NewAccountClient(cfg),
Email: NewEmailClient(cfg),
}, nil
}
// Debug returns a new debug-client. It's used to get verbose logging on specific operations.
//
// client.Debug().
// Account.
// 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.Account.Use(hooks...)
c.Email.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.Account.Intercept(interceptors...)
c.Email.Intercept(interceptors...)
}
// Mutate implements the ent.Mutator interface.
func (c *Client) Mutate(ctx context.Context, m Mutation) (Value, error) {
switch m := m.(type) {
case *AccountMutation:
return c.Account.mutate(ctx, m)
case *EmailMutation:
return c.Email.mutate(ctx, m)
default:
return nil, fmt.Errorf("ent: unknown mutation type %T", m)
}
}
// AccountClient is a client for the Account schema.
type AccountClient struct {
config
}
// NewAccountClient returns a client for the Account from the given config.
func NewAccountClient(c config) *AccountClient {
return &AccountClient{config: c}
}
// Use adds a list of mutation hooks to the hooks stack.
// A call to `Use(f, g, h)` equals to `account.Hooks(f(g(h())))`.
func (c *AccountClient) Use(hooks ...Hook) {
c.hooks.Account = append(c.hooks.Account, hooks...)
}
// Intercept adds a list of query interceptors to the interceptors stack.
// A call to `Intercept(f, g, h)` equals to `account.Intercept(f(g(h())))`.
func (c *AccountClient) Intercept(interceptors ...Interceptor) {
c.inters.Account = append(c.inters.Account, interceptors...)
}
// Create returns a builder for creating a Account entity.
func (c *AccountClient) Create() *AccountCreate {
mutation := newAccountMutation(c.config, OpCreate)
return &AccountCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// CreateBulk returns a builder for creating a bulk of Account entities.
func (c *AccountClient) CreateBulk(builders ...*AccountCreate) *AccountCreateBulk {
return &AccountCreateBulk{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 *AccountClient) MapCreateBulk(slice any, setFunc func(*AccountCreate, int)) *AccountCreateBulk {
rv := reflect.ValueOf(slice)
if rv.Kind() != reflect.Slice {
return &AccountCreateBulk{err: fmt.Errorf("calling to AccountClient.MapCreateBulk with wrong type %T, need slice", slice)}
}
builders := make([]*AccountCreate, rv.Len())
for i := 0; i < rv.Len(); i++ {
builders[i] = c.Create()
setFunc(builders[i], i)
}
return &AccountCreateBulk{config: c.config, builders: builders}
}
// Update returns an update builder for Account.
func (c *AccountClient) Update() *AccountUpdate {
mutation := newAccountMutation(c.config, OpUpdate)
return &AccountUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// UpdateOne returns an update builder for the given entity.
func (c *AccountClient) UpdateOne(a *Account) *AccountUpdateOne {
mutation := newAccountMutation(c.config, OpUpdateOne, withAccount(a))
return &AccountUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// UpdateOneID returns an update builder for the given id.
func (c *AccountClient) UpdateOneID(id uuid.UUID) *AccountUpdateOne {
mutation := newAccountMutation(c.config, OpUpdateOne, withAccountID(id))
return &AccountUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// Delete returns a delete builder for Account.
func (c *AccountClient) Delete() *AccountDelete {
mutation := newAccountMutation(c.config, OpDelete)
return &AccountDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// DeleteOne returns a builder for deleting the given entity.
func (c *AccountClient) DeleteOne(a *Account) *AccountDeleteOne {
return c.DeleteOneID(a.ID)
}
// DeleteOneID returns a builder for deleting the given entity by its id.
func (c *AccountClient) DeleteOneID(id uuid.UUID) *AccountDeleteOne {
builder := c.Delete().Where(account.ID(id))
builder.mutation.id = &id
builder.mutation.op = OpDeleteOne
return &AccountDeleteOne{builder}
}
// Query returns a query builder for Account.
func (c *AccountClient) Query() *AccountQuery {
return &AccountQuery{
config: c.config,
ctx: &QueryContext{Type: TypeAccount},
inters: c.Interceptors(),
}
}
// Get returns a Account entity by its id.
func (c *AccountClient) Get(ctx context.Context, id uuid.UUID) (*Account, error) {
return c.Query().Where(account.ID(id)).Only(ctx)
}
// GetX is like Get, but panics if an error occurs.
func (c *AccountClient) GetX(ctx context.Context, id uuid.UUID) *Account {
obj, err := c.Get(ctx, id)
if err != nil {
panic(err)
}
return obj
}
// QueryEmails queries the emails edge of a Account.
func (c *AccountClient) QueryEmails(a *Account) *EmailQuery {
query := (&EmailClient{config: c.config}).Query()
query.path = func(context.Context) (fromV *sql.Selector, _ error) {
id := a.ID
step := sqlgraph.NewStep(
sqlgraph.From(account.Table, account.FieldID, id),
sqlgraph.To(email.Table, email.FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, account.EmailsTable, account.EmailsColumn),
)
fromV = sqlgraph.Neighbors(a.driver.Dialect(), step)
return fromV, nil
}
return query
}
// Hooks returns the client hooks.
func (c *AccountClient) Hooks() []Hook {
return c.hooks.Account
}
// Interceptors returns the client interceptors.
func (c *AccountClient) Interceptors() []Interceptor {
return c.inters.Account
}
func (c *AccountClient) mutate(ctx context.Context, m *AccountMutation) (Value, error) {
switch m.Op() {
case OpCreate:
return (&AccountCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdate:
return (&AccountUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdateOne:
return (&AccountUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpDelete, OpDeleteOne:
return (&AccountDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
default:
return nil, fmt.Errorf("ent: unknown Account mutation op: %q", m.Op())
}
}
// EmailClient is a client for the Email schema.
type EmailClient struct {
config
}
// NewEmailClient returns a client for the Email from the given config.
func NewEmailClient(c config) *EmailClient {
return &EmailClient{config: c}
}
// Use adds a list of mutation hooks to the hooks stack.
// A call to `Use(f, g, h)` equals to `email.Hooks(f(g(h())))`.
func (c *EmailClient) Use(hooks ...Hook) {
c.hooks.Email = append(c.hooks.Email, hooks...)
}
// Intercept adds a list of query interceptors to the interceptors stack.
// A call to `Intercept(f, g, h)` equals to `email.Intercept(f(g(h())))`.
func (c *EmailClient) Intercept(interceptors ...Interceptor) {
c.inters.Email = append(c.inters.Email, interceptors...)
}
// Create returns a builder for creating a Email entity.
func (c *EmailClient) Create() *EmailCreate {
mutation := newEmailMutation(c.config, OpCreate)
return &EmailCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// CreateBulk returns a builder for creating a bulk of Email entities.
func (c *EmailClient) CreateBulk(builders ...*EmailCreate) *EmailCreateBulk {
return &EmailCreateBulk{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 *EmailClient) MapCreateBulk(slice any, setFunc func(*EmailCreate, int)) *EmailCreateBulk {
rv := reflect.ValueOf(slice)
if rv.Kind() != reflect.Slice {
return &EmailCreateBulk{err: fmt.Errorf("calling to EmailClient.MapCreateBulk with wrong type %T, need slice", slice)}
}
builders := make([]*EmailCreate, rv.Len())
for i := 0; i < rv.Len(); i++ {
builders[i] = c.Create()
setFunc(builders[i], i)
}
return &EmailCreateBulk{config: c.config, builders: builders}
}
// Update returns an update builder for Email.
func (c *EmailClient) Update() *EmailUpdate {
mutation := newEmailMutation(c.config, OpUpdate)
return &EmailUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// UpdateOne returns an update builder for the given entity.
func (c *EmailClient) UpdateOne(e *Email) *EmailUpdateOne {
mutation := newEmailMutation(c.config, OpUpdateOne, withEmail(e))
return &EmailUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// UpdateOneID returns an update builder for the given id.
func (c *EmailClient) UpdateOneID(id uuid.UUID) *EmailUpdateOne {
mutation := newEmailMutation(c.config, OpUpdateOne, withEmailID(id))
return &EmailUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// Delete returns a delete builder for Email.
func (c *EmailClient) Delete() *EmailDelete {
mutation := newEmailMutation(c.config, OpDelete)
return &EmailDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// DeleteOne returns a builder for deleting the given entity.
func (c *EmailClient) DeleteOne(e *Email) *EmailDeleteOne {
return c.DeleteOneID(e.ID)
}
// DeleteOneID returns a builder for deleting the given entity by its id.
func (c *EmailClient) DeleteOneID(id uuid.UUID) *EmailDeleteOne {
builder := c.Delete().Where(email.ID(id))
builder.mutation.id = &id
builder.mutation.op = OpDeleteOne
return &EmailDeleteOne{builder}
}
// Query returns a query builder for Email.
func (c *EmailClient) Query() *EmailQuery {
return &EmailQuery{
config: c.config,
ctx: &QueryContext{Type: TypeEmail},
inters: c.Interceptors(),
}
}
// Get returns a Email entity by its id.
func (c *EmailClient) Get(ctx context.Context, id uuid.UUID) (*Email, error) {
return c.Query().Where(email.ID(id)).Only(ctx)
}
// GetX is like Get, but panics if an error occurs.
func (c *EmailClient) GetX(ctx context.Context, id uuid.UUID) *Email {
obj, err := c.Get(ctx, id)
if err != nil {
panic(err)
}
return obj
}
// QueryAccount queries the account edge of a Email.
func (c *EmailClient) QueryAccount(e *Email) *AccountQuery {
query := (&AccountClient{config: c.config}).Query()
query.path = func(context.Context) (fromV *sql.Selector, _ error) {
id := e.ID
step := sqlgraph.NewStep(
sqlgraph.From(email.Table, email.FieldID, id),
sqlgraph.To(account.Table, account.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, email.AccountTable, email.AccountColumn),
)
fromV = sqlgraph.Neighbors(e.driver.Dialect(), step)
return fromV, nil
}
return query
}
// Hooks returns the client hooks.
func (c *EmailClient) Hooks() []Hook {
return c.hooks.Email
}
// Interceptors returns the client interceptors.
func (c *EmailClient) Interceptors() []Interceptor {
return c.inters.Email
}
func (c *EmailClient) mutate(ctx context.Context, m *EmailMutation) (Value, error) {
switch m.Op() {
case OpCreate:
return (&EmailCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdate:
return (&EmailUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdateOne:
return (&EmailUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpDelete, OpDeleteOne:
return (&EmailDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
default:
return nil, fmt.Errorf("ent: unknown Email mutation op: %q", m.Op())
}
}
// hooks and interceptors per client, for fast access.
type (
hooks struct {
Account, Email []ent.Hook
}
inters struct {
Account, Email []ent.Interceptor
}
)