Debian Codegraph @

ent/tx.go

raw
1 // Code generated by ent, DO NOT EDIT.
2
3 package ent
4
5 import (
6 "context"
7 "sync"
8
9 "entgo.io/ent/dialect"
10 )
11
12 // Tx is a transactional client that is created by calling Client.Tx().
13 type Tx struct {
14 config
15 // Attestation is the client for interacting with the Attestation builders.
16 Attestation *AttestationClient
17 // AttestationCollection is the client for interacting with the AttestationCollection builders.
18 AttestationCollection *AttestationCollectionClient
19 // AttestationPolicy is the client for interacting with the AttestationPolicy builders.
20 AttestationPolicy *AttestationPolicyClient
21 // Dsse is the client for interacting with the Dsse builders.
22 Dsse *DsseClient
23 // PayloadDigest is the client for interacting with the PayloadDigest builders.
24 PayloadDigest *PayloadDigestClient
25 // Signature is the client for interacting with the Signature builders.
26 Signature *SignatureClient
27 // Statement is the client for interacting with the Statement builders.
28 Statement *StatementClient
29 // Subject is the client for interacting with the Subject builders.
30 Subject *SubjectClient
31 // SubjectDigest is the client for interacting with the SubjectDigest builders.
32 SubjectDigest *SubjectDigestClient
33 // Timestamp is the client for interacting with the Timestamp builders.
34 Timestamp *TimestampClient
35
36 // lazily loaded.
37 client *Client
38 clientOnce sync.Once
39 // ctx lives for the life of the transaction. It is
40 // the same context used by the underlying connection.
41 ctx context.Context
42 }
43
44 type (
45 // Committer is the interface that wraps the Commit method.
46 Committer interface {
47 Commit(context.Context, *Tx) error
48 }
49
50 // The CommitFunc type is an adapter to allow the use of ordinary
51 // function as a Committer. If f is a function with the appropriate
52 // signature, CommitFunc(f) is a Committer that calls f.
53 CommitFunc func(context.Context, *Tx) error
54
55 // CommitHook defines the "commit middleware". A function that gets a Committer
56 // and returns a Committer. For example:
57 //
58 // hook := func(next ent.Committer) ent.Committer {
59 // return ent.CommitFunc(func(ctx context.Context, tx *ent.Tx) error {
60 // // Do some stuff before.
61 // if err := next.Commit(ctx, tx); err != nil {
62 // return err
63 // }
64 // // Do some stuff after.
65 // return nil
66 // })
67 // }
68 //
69 CommitHook func(Committer) Committer
70 )
71
72 // Commit calls f(ctx, m).
73 func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error {
74 return f(ctx, tx)
75 }
76
77 // Commit commits the transaction.
78 func (tx *Tx) Commit() error {
79 txDriver := tx.config.driver.(*txDriver)
80 var fn Committer = CommitFunc(func(context.Context, *Tx) error {
81 return txDriver.tx.Commit()
82 })
83 txDriver.mu.Lock()
84 hooks := append([]CommitHook(nil), txDriver.onCommit...)
85 txDriver.mu.Unlock()
86 for i := len(hooks) - 1; i >= 0; i-- {
87 fn = hooks[i](fn)
88 }
89 return fn.Commit(tx.ctx, tx)
90 }
91
92 // OnCommit adds a hook to call on commit.
93 func (tx *Tx) OnCommit(f CommitHook) {
94 txDriver := tx.config.driver.(*txDriver)
95 txDriver.mu.Lock()
96 txDriver.onCommit = append(txDriver.onCommit, f)
97 txDriver.mu.Unlock()
98 }
99
100 type (
101 // Rollbacker is the interface that wraps the Rollback method.
102 Rollbacker interface {
103 Rollback(context.Context, *Tx) error
104 }
105
106 // The RollbackFunc type is an adapter to allow the use of ordinary
107 // function as a Rollbacker. If f is a function with the appropriate
108 // signature, RollbackFunc(f) is a Rollbacker that calls f.
109 RollbackFunc func(context.Context, *Tx) error
110
111 // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker
112 // and returns a Rollbacker. For example:
113 //
114 // hook := func(next ent.Rollbacker) ent.Rollbacker {
115 // return ent.RollbackFunc(func(ctx context.Context, tx *ent.Tx) error {
116 // // Do some stuff before.
117 // if err := next.Rollback(ctx, tx); err != nil {
118 // return err
119 // }
120 // // Do some stuff after.
121 // return nil
122 // })
123 // }
124 //
125 RollbackHook func(Rollbacker) Rollbacker
126 )
127
128 // Rollback calls f(ctx, m).
129 func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error {
130 return f(ctx, tx)
131 }
132
133 // Rollback rollbacks the transaction.
134 func (tx *Tx) Rollback() error {
135 txDriver := tx.config.driver.(*txDriver)
136 var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error {
137 return txDriver.tx.Rollback()
138 })
139 txDriver.mu.Lock()
140 hooks := append([]RollbackHook(nil), txDriver.onRollback...)
141 txDriver.mu.Unlock()
142 for i := len(hooks) - 1; i >= 0; i-- {
143 fn = hooks[i](fn)
144 }
145 return fn.Rollback(tx.ctx, tx)
146 }
147
148 // OnRollback adds a hook to call on rollback.
149 func (tx *Tx) OnRollback(f RollbackHook) {
150 txDriver := tx.config.driver.(*txDriver)
151 txDriver.mu.Lock()
152 txDriver.onRollback = append(txDriver.onRollback, f)
153 txDriver.mu.Unlock()
154 }
155
156 // Client returns a Client that binds to current transaction.
157 func (tx *Tx) Client() *Client {
158 tx.clientOnce.Do(func() {
159 tx.client = &Client{config: tx.config}
160 tx.client.init()
161 })
162 return tx.client
163 }
164
165 func (tx *Tx) init() {
166 tx.Attestation = NewAttestationClient(tx.config)
167 tx.AttestationCollection = NewAttestationCollectionClient(tx.config)
168 tx.AttestationPolicy = NewAttestationPolicyClient(tx.config)
169 tx.Dsse = NewDsseClient(tx.config)
170 tx.PayloadDigest = NewPayloadDigestClient(tx.config)
171 tx.Signature = NewSignatureClient(tx.config)
172 tx.Statement = NewStatementClient(tx.config)
173 tx.Subject = NewSubjectClient(tx.config)
174 tx.SubjectDigest = NewSubjectDigestClient(tx.config)
175 tx.Timestamp = NewTimestampClient(tx.config)
176 }
177
178 // txDriver wraps the given dialect.Tx with a nop dialect.Driver implementation.
179 // The idea is to support transactions without adding any extra code to the builders.
180 // When a builder calls to driver.Tx(), it gets the same dialect.Tx instance.
181 // Commit and Rollback are nop for the internal builders and the user must call one
182 // of them in order to commit or rollback the transaction.
183 //
184 // If a closed transaction is embedded in one of the generated entities, and the entity
185 // applies a query, for example: Attestation.QueryXXX(), the query will be executed
186 // through the driver which created this transaction.
187 //
188 // Note that txDriver is not goroutine safe.
189 type txDriver struct {
190 // the driver we started the transaction from.
191 drv dialect.Driver
192 // tx is the underlying transaction.
193 tx dialect.Tx
194 // completion hooks.
195 mu sync.Mutex
196 onCommit []CommitHook
197 onRollback []RollbackHook
198 }
199
200 // newTx creates a new transactional driver.
201 func newTx(ctx context.Context, drv dialect.Driver) (*txDriver, error) {
202 tx, err := drv.Tx(ctx)
203 if err != nil {
204 return nil, err
205 }
206 return &txDriver{tx: tx, drv: drv}, nil
207 }
208
209 // Tx returns the transaction wrapper (txDriver) to avoid Commit or Rollback calls
210 // from the internal builders. Should be called only by the internal builders.
211 func (tx *txDriver) Tx(context.Context) (dialect.Tx, error) { return tx, nil }
212
213 // Dialect returns the dialect of the driver we started the transaction from.
214 func (tx *txDriver) Dialect() string { return tx.drv.Dialect() }
215
216 // Close is a nop close.
217 func (*txDriver) Close() error { return nil }
218
219 // Commit is a nop commit for the internal builders.
220 // User must call `Tx.Commit` in order to commit the transaction.
221 func (*txDriver) Commit() error { return nil }
222
223 // Rollback is a nop rollback for the internal builders.
224 // User must call `Tx.Rollback` in order to rollback the transaction.
225 func (*txDriver) Rollback() error { return nil }
226
227 // Exec calls tx.Exec.
228 func (tx *txDriver) Exec(ctx context.Context, query string, args, v any) error {
229 return tx.tx.Exec(ctx, query, args, v)
230 }
231
232 // Query calls tx.Query.
233 func (tx *txDriver) Query(ctx context.Context, query string, args, v any) error {
234 return tx.tx.Query(ctx, query, args, v)
235 }
236
237 var _ dialect.Driver = (*txDriver)(nil)