tx.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "github.com/facebookincubator/ent/dialect"
  6. )
  7. // Tx is a transactional client that is created by calling Client.Tx().
  8. type Tx struct {
  9. config
  10. // Car is the client for interacting with the Car builders.
  11. Car *CarClient
  12. // Group is the client for interacting with the Group builders.
  13. Group *GroupClient
  14. // User is the client for interacting with the User builders.
  15. User *UserClient
  16. }
  17. // Commit commits the transaction.
  18. func (tx *Tx) Commit() error {
  19. return tx.config.driver.(*txDriver).tx.Commit()
  20. }
  21. // Rollback rollbacks the transaction.
  22. func (tx *Tx) Rollback() error {
  23. return tx.config.driver.(*txDriver).tx.Rollback()
  24. }
  25. // Client returns a Client that binds to current transaction.
  26. func (tx *Tx) Client() *Client {
  27. client := &Client{config: tx.config}
  28. client.init()
  29. return client
  30. }
  31. func (tx *Tx) init() {
  32. tx.Car = NewCarClient(tx.config)
  33. tx.Group = NewGroupClient(tx.config)
  34. tx.User = NewUserClient(tx.config)
  35. }
  36. // txDriver wraps the given dialect.Tx with a nop dialect.Driver implementation.
  37. // The idea is to support transactions without adding any extra code to the builders.
  38. // When a builder calls to driver.Tx(), it gets the same dialect.Tx instance.
  39. // Commit and Rollback are nop for the internal builders and the user must call one
  40. // of them in order to commit or rollback the transaction.
  41. //
  42. // If a closed transaction is embedded in one of the generated entities, and the entity
  43. // applies a query, for example: Car.QueryXXX(), the query will be executed
  44. // through the driver which created this transaction.
  45. //
  46. // Note that txDriver is not goroutine safe.
  47. type txDriver struct {
  48. // the driver we started the transaction from.
  49. drv dialect.Driver
  50. // tx is the underlying transaction.
  51. tx dialect.Tx
  52. }
  53. // newTx creates a new transactional driver.
  54. func newTx(ctx context.Context, drv dialect.Driver) (*txDriver, error) {
  55. tx, err := drv.Tx(ctx)
  56. if err != nil {
  57. return nil, err
  58. }
  59. return &txDriver{tx: tx, drv: drv}, nil
  60. }
  61. // Tx returns the transaction wrapper (txDriver) to avoid Commit or Rollback calls
  62. // from the internal builders. Should be called only by the internal builders.
  63. func (tx *txDriver) Tx(context.Context) (dialect.Tx, error) { return tx, nil }
  64. // Dialect returns the dialect of the driver we started the transaction from.
  65. func (tx *txDriver) Dialect() string { return tx.drv.Dialect() }
  66. // Close is a nop close.
  67. func (*txDriver) Close() error { return nil }
  68. // Commit is a nop commit for the internal builders.
  69. // User must call `Tx.Commit` in order to commit the transaction.
  70. func (*txDriver) Commit() error { return nil }
  71. // Rollback is a nop rollback for the internal builders.
  72. // User must call `Tx.Rollback` in order to rollback the transaction.
  73. func (*txDriver) Rollback() error { return nil }
  74. // Exec calls tx.Exec.
  75. func (tx *txDriver) Exec(ctx context.Context, query string, args, v interface{}) error {
  76. return tx.tx.Exec(ctx, query, args, v)
  77. }
  78. // Query calls tx.Query.
  79. func (tx *txDriver) Query(ctx context.Context, query string, args, v interface{}) error {
  80. return tx.tx.Query(ctx, query, args, v)
  81. }
  82. var _ dialect.Driver = (*txDriver)(nil)