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