2022-08-30 18:04:50 +00:00
|
|
|
// Code generated by ent, DO NOT EDIT.
|
2022-08-30 02:30:36 +00:00
|
|
|
|
|
|
|
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
|
2024-03-02 18:02:41 +00:00
|
|
|
// ActionToken is the client for interacting with the ActionToken builders.
|
|
|
|
ActionToken *ActionTokenClient
|
2022-09-12 22:47:27 +00:00
|
|
|
// Attachment is the client for interacting with the Attachment builders.
|
|
|
|
Attachment *AttachmentClient
|
2022-12-03 19:55:00 +00:00
|
|
|
// AuthRoles is the client for interacting with the AuthRoles builders.
|
|
|
|
AuthRoles *AuthRolesClient
|
2022-08-30 02:30:36 +00:00
|
|
|
// AuthTokens is the client for interacting with the AuthTokens builders.
|
|
|
|
AuthTokens *AuthTokensClient
|
2022-09-12 22:47:27 +00:00
|
|
|
// Document is the client for interacting with the Document builders.
|
|
|
|
Document *DocumentClient
|
2022-08-30 18:04:50 +00:00
|
|
|
// Group is the client for interacting with the Group builders.
|
|
|
|
Group *GroupClient
|
2022-10-07 02:54:09 +00:00
|
|
|
// GroupInvitationToken is the client for interacting with the GroupInvitationToken builders.
|
|
|
|
GroupInvitationToken *GroupInvitationTokenClient
|
2022-08-30 18:04:50 +00:00
|
|
|
// Item is the client for interacting with the Item builders.
|
|
|
|
Item *ItemClient
|
|
|
|
// ItemField is the client for interacting with the ItemField builders.
|
|
|
|
ItemField *ItemFieldClient
|
|
|
|
// Label is the client for interacting with the Label builders.
|
|
|
|
Label *LabelClient
|
|
|
|
// Location is the client for interacting with the Location builders.
|
|
|
|
Location *LocationClient
|
2022-12-10 05:57:57 +00:00
|
|
|
// MaintenanceEntry is the client for interacting with the MaintenanceEntry builders.
|
|
|
|
MaintenanceEntry *MaintenanceEntryClient
|
2023-03-07 06:18:58 +00:00
|
|
|
// Notifier is the client for interacting with the Notifier builders.
|
|
|
|
Notifier *NotifierClient
|
2022-08-30 02:30:36 +00:00
|
|
|
// 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()
|
|
|
|
})
|
2022-12-02 01:45:28 +00:00
|
|
|
txDriver.mu.Lock()
|
|
|
|
hooks := append([]CommitHook(nil), txDriver.onCommit...)
|
|
|
|
txDriver.mu.Unlock()
|
2022-08-30 02:30:36 +00:00
|
|
|
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) {
|
2022-12-02 01:45:28 +00:00
|
|
|
txDriver := tx.config.driver.(*txDriver)
|
|
|
|
txDriver.mu.Lock()
|
|
|
|
txDriver.onCommit = append(txDriver.onCommit, f)
|
|
|
|
txDriver.mu.Unlock()
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
})
|
2022-12-02 01:45:28 +00:00
|
|
|
txDriver.mu.Lock()
|
|
|
|
hooks := append([]RollbackHook(nil), txDriver.onRollback...)
|
|
|
|
txDriver.mu.Unlock()
|
2022-08-30 02:30:36 +00:00
|
|
|
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) {
|
2022-12-02 01:45:28 +00:00
|
|
|
txDriver := tx.config.driver.(*txDriver)
|
|
|
|
txDriver.mu.Lock()
|
|
|
|
txDriver.onRollback = append(txDriver.onRollback, f)
|
|
|
|
txDriver.mu.Unlock()
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
2024-03-02 18:02:41 +00:00
|
|
|
tx.ActionToken = NewActionTokenClient(tx.config)
|
2022-09-12 22:47:27 +00:00
|
|
|
tx.Attachment = NewAttachmentClient(tx.config)
|
2022-12-03 19:55:00 +00:00
|
|
|
tx.AuthRoles = NewAuthRolesClient(tx.config)
|
2022-08-30 02:30:36 +00:00
|
|
|
tx.AuthTokens = NewAuthTokensClient(tx.config)
|
2022-09-12 22:47:27 +00:00
|
|
|
tx.Document = NewDocumentClient(tx.config)
|
2022-08-30 18:04:50 +00:00
|
|
|
tx.Group = NewGroupClient(tx.config)
|
2022-10-07 02:54:09 +00:00
|
|
|
tx.GroupInvitationToken = NewGroupInvitationTokenClient(tx.config)
|
2022-08-30 18:04:50 +00:00
|
|
|
tx.Item = NewItemClient(tx.config)
|
|
|
|
tx.ItemField = NewItemFieldClient(tx.config)
|
|
|
|
tx.Label = NewLabelClient(tx.config)
|
|
|
|
tx.Location = NewLocationClient(tx.config)
|
2022-12-10 05:57:57 +00:00
|
|
|
tx.MaintenanceEntry = NewMaintenanceEntryClient(tx.config)
|
2023-03-07 06:18:58 +00:00
|
|
|
tx.Notifier = NewNotifierClient(tx.config)
|
2022-08-30 02:30:36 +00:00
|
|
|
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
|
2024-03-02 18:02:41 +00:00
|
|
|
// applies a query, for example: ActionToken.QueryXXX(), the query will be executed
|
2022-08-30 02:30:36 +00:00
|
|
|
// 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
|
2022-12-02 01:45:28 +00:00
|
|
|
// completion hooks.
|
|
|
|
mu sync.Mutex
|
|
|
|
onCommit []CommitHook
|
|
|
|
onRollback []RollbackHook
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2022-09-29 05:42:33 +00:00
|
|
|
func (tx *txDriver) Exec(ctx context.Context, query string, args, v any) error {
|
2022-08-30 02:30:36 +00:00
|
|
|
return tx.tx.Exec(ctx, query, args, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query calls tx.Query.
|
2022-09-29 05:42:33 +00:00
|
|
|
func (tx *txDriver) Query(ctx context.Context, query string, args, v any) error {
|
2022-08-30 02:30:36 +00:00
|
|
|
return tx.tx.Query(ctx, query, args, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ dialect.Driver = (*txDriver)(nil)
|