client.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "fmt"
  6. "log"
  7. "todo/ent/migrate"
  8. "todo/ent/todo"
  9. "entgo.io/ent/dialect"
  10. "entgo.io/ent/dialect/sql"
  11. "entgo.io/ent/dialect/sql/sqlgraph"
  12. )
  13. // Client is the client that holds all ent builders.
  14. type Client struct {
  15. config
  16. // Schema is the client for creating, migrating and dropping schema.
  17. Schema *migrate.Schema
  18. // Todo is the client for interacting with the Todo builders.
  19. Todo *TodoClient
  20. }
  21. // NewClient creates a new client configured with the given options.
  22. func NewClient(opts ...Option) *Client {
  23. cfg := config{log: log.Println, hooks: &hooks{}}
  24. cfg.options(opts...)
  25. client := &Client{config: cfg}
  26. client.init()
  27. return client
  28. }
  29. func (c *Client) init() {
  30. c.Schema = migrate.NewSchema(c.driver)
  31. c.Todo = NewTodoClient(c.config)
  32. }
  33. // Open opens a database/sql.DB specified by the driver name and
  34. // the data source name, and returns a new client attached to it.
  35. // Optional parameters can be added for configuring the client.
  36. func Open(driverName, dataSourceName string, options ...Option) (*Client, error) {
  37. switch driverName {
  38. case dialect.MySQL, dialect.Postgres, dialect.SQLite:
  39. drv, err := sql.Open(driverName, dataSourceName)
  40. if err != nil {
  41. return nil, err
  42. }
  43. return NewClient(append(options, Driver(drv))...), nil
  44. default:
  45. return nil, fmt.Errorf("unsupported driver: %q", driverName)
  46. }
  47. }
  48. // Tx returns a new transactional client. The provided context
  49. // is used until the transaction is committed or rolled back.
  50. func (c *Client) Tx(ctx context.Context) (*Tx, error) {
  51. if _, ok := c.driver.(*txDriver); ok {
  52. return nil, fmt.Errorf("ent: cannot start a transaction within a transaction")
  53. }
  54. tx, err := newTx(ctx, c.driver)
  55. if err != nil {
  56. return nil, fmt.Errorf("ent: starting a transaction: %w", err)
  57. }
  58. cfg := c.config
  59. cfg.driver = tx
  60. return &Tx{
  61. ctx: ctx,
  62. config: cfg,
  63. Todo: NewTodoClient(cfg),
  64. }, nil
  65. }
  66. // BeginTx returns a transactional client with specified options.
  67. func (c *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
  68. if _, ok := c.driver.(*txDriver); ok {
  69. return nil, fmt.Errorf("ent: cannot start a transaction within a transaction")
  70. }
  71. tx, err := c.driver.(interface {
  72. BeginTx(context.Context, *sql.TxOptions) (dialect.Tx, error)
  73. }).BeginTx(ctx, opts)
  74. if err != nil {
  75. return nil, fmt.Errorf("ent: starting a transaction: %w", err)
  76. }
  77. cfg := c.config
  78. cfg.driver = &txDriver{tx: tx, drv: c.driver}
  79. return &Tx{
  80. ctx: ctx,
  81. config: cfg,
  82. Todo: NewTodoClient(cfg),
  83. }, nil
  84. }
  85. // Debug returns a new debug-client. It's used to get verbose logging on specific operations.
  86. //
  87. // client.Debug().
  88. // Todo.
  89. // Query().
  90. // Count(ctx)
  91. //
  92. func (c *Client) Debug() *Client {
  93. if c.debug {
  94. return c
  95. }
  96. cfg := c.config
  97. cfg.driver = dialect.Debug(c.driver, c.log)
  98. client := &Client{config: cfg}
  99. client.init()
  100. return client
  101. }
  102. // Close closes the database connection and prevents new queries from starting.
  103. func (c *Client) Close() error {
  104. return c.driver.Close()
  105. }
  106. // Use adds the mutation hooks to all the entity clients.
  107. // In order to add hooks to a specific client, call: `client.Node.Use(...)`.
  108. func (c *Client) Use(hooks ...Hook) {
  109. c.Todo.Use(hooks...)
  110. }
  111. // TodoClient is a client for the Todo schema.
  112. type TodoClient struct {
  113. config
  114. }
  115. // NewTodoClient returns a client for the Todo from the given config.
  116. func NewTodoClient(c config) *TodoClient {
  117. return &TodoClient{config: c}
  118. }
  119. // Use adds a list of mutation hooks to the hooks stack.
  120. // A call to `Use(f, g, h)` equals to `todo.Hooks(f(g(h())))`.
  121. func (c *TodoClient) Use(hooks ...Hook) {
  122. c.hooks.Todo = append(c.hooks.Todo, hooks...)
  123. }
  124. // Create returns a create builder for Todo.
  125. func (c *TodoClient) Create() *TodoCreate {
  126. mutation := newTodoMutation(c.config, OpCreate)
  127. return &TodoCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
  128. }
  129. // CreateBulk returns a builder for creating a bulk of Todo entities.
  130. func (c *TodoClient) CreateBulk(builders ...*TodoCreate) *TodoCreateBulk {
  131. return &TodoCreateBulk{config: c.config, builders: builders}
  132. }
  133. // Update returns an update builder for Todo.
  134. func (c *TodoClient) Update() *TodoUpdate {
  135. mutation := newTodoMutation(c.config, OpUpdate)
  136. return &TodoUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
  137. }
  138. // UpdateOne returns an update builder for the given entity.
  139. func (c *TodoClient) UpdateOne(t *Todo) *TodoUpdateOne {
  140. mutation := newTodoMutation(c.config, OpUpdateOne, withTodo(t))
  141. return &TodoUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
  142. }
  143. // UpdateOneID returns an update builder for the given id.
  144. func (c *TodoClient) UpdateOneID(id int) *TodoUpdateOne {
  145. mutation := newTodoMutation(c.config, OpUpdateOne, withTodoID(id))
  146. return &TodoUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
  147. }
  148. // Delete returns a delete builder for Todo.
  149. func (c *TodoClient) Delete() *TodoDelete {
  150. mutation := newTodoMutation(c.config, OpDelete)
  151. return &TodoDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
  152. }
  153. // DeleteOne returns a delete builder for the given entity.
  154. func (c *TodoClient) DeleteOne(t *Todo) *TodoDeleteOne {
  155. return c.DeleteOneID(t.ID)
  156. }
  157. // DeleteOneID returns a delete builder for the given id.
  158. func (c *TodoClient) DeleteOneID(id int) *TodoDeleteOne {
  159. builder := c.Delete().Where(todo.ID(id))
  160. builder.mutation.id = &id
  161. builder.mutation.op = OpDeleteOne
  162. return &TodoDeleteOne{builder}
  163. }
  164. // Query returns a query builder for Todo.
  165. func (c *TodoClient) Query() *TodoQuery {
  166. return &TodoQuery{
  167. config: c.config,
  168. }
  169. }
  170. // Get returns a Todo entity by its id.
  171. func (c *TodoClient) Get(ctx context.Context, id int) (*Todo, error) {
  172. return c.Query().Where(todo.ID(id)).Only(ctx)
  173. }
  174. // GetX is like Get, but panics if an error occurs.
  175. func (c *TodoClient) GetX(ctx context.Context, id int) *Todo {
  176. obj, err := c.Get(ctx, id)
  177. if err != nil {
  178. panic(err)
  179. }
  180. return obj
  181. }
  182. // QueryChildren queries the children edge of a Todo.
  183. func (c *TodoClient) QueryChildren(t *Todo) *TodoQuery {
  184. query := &TodoQuery{config: c.config}
  185. query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) {
  186. id := t.ID
  187. step := sqlgraph.NewStep(
  188. sqlgraph.From(todo.Table, todo.FieldID, id),
  189. sqlgraph.To(todo.Table, todo.FieldID),
  190. sqlgraph.Edge(sqlgraph.O2M, true, todo.ChildrenTable, todo.ChildrenColumn),
  191. )
  192. fromV = sqlgraph.Neighbors(t.driver.Dialect(), step)
  193. return fromV, nil
  194. }
  195. return query
  196. }
  197. // QueryParent queries the parent edge of a Todo.
  198. func (c *TodoClient) QueryParent(t *Todo) *TodoQuery {
  199. query := &TodoQuery{config: c.config}
  200. query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) {
  201. id := t.ID
  202. step := sqlgraph.NewStep(
  203. sqlgraph.From(todo.Table, todo.FieldID, id),
  204. sqlgraph.To(todo.Table, todo.FieldID),
  205. sqlgraph.Edge(sqlgraph.M2O, false, todo.ParentTable, todo.ParentColumn),
  206. )
  207. fromV = sqlgraph.Neighbors(t.driver.Dialect(), step)
  208. return fromV, nil
  209. }
  210. return query
  211. }
  212. // Hooks returns the client hooks.
  213. func (c *TodoClient) Hooks() []Hook {
  214. return c.hooks.Todo
  215. }