client.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "fmt"
  6. "log"
  7. "code.osinet.fr/fgm/entdemo/ent/migrate"
  8. "code.osinet.fr/fgm/entdemo/ent/car"
  9. "code.osinet.fr/fgm/entdemo/ent/group"
  10. "code.osinet.fr/fgm/entdemo/ent/user"
  11. "github.com/facebookincubator/ent/dialect"
  12. "github.com/facebookincubator/ent/dialect/sql"
  13. "github.com/facebookincubator/ent/dialect/sql/sqlgraph"
  14. )
  15. // Client is the client that holds all ent builders.
  16. type Client struct {
  17. config
  18. // Schema is the client for creating, migrating and dropping schema.
  19. Schema *migrate.Schema
  20. // Car is the client for interacting with the Car builders.
  21. Car *CarClient
  22. // Group is the client for interacting with the Group builders.
  23. Group *GroupClient
  24. // User is the client for interacting with the User builders.
  25. User *UserClient
  26. }
  27. // NewClient creates a new client configured with the given options.
  28. func NewClient(opts ...Option) *Client {
  29. cfg := config{log: log.Println, hooks: &hooks{}}
  30. cfg.options(opts...)
  31. client := &Client{config: cfg}
  32. client.init()
  33. return client
  34. }
  35. func (c *Client) init() {
  36. c.Schema = migrate.NewSchema(c.driver)
  37. c.Car = NewCarClient(c.config)
  38. c.Group = NewGroupClient(c.config)
  39. c.User = NewUserClient(c.config)
  40. }
  41. // Open opens a connection to the database specified by the driver name and a
  42. // driver-specific data source name, and returns a new client attached to it.
  43. // Optional parameters can be added for configuring the client.
  44. func Open(driverName, dataSourceName string, options ...Option) (*Client, error) {
  45. switch driverName {
  46. case dialect.MySQL, dialect.Postgres, dialect.SQLite:
  47. drv, err := sql.Open(driverName, dataSourceName)
  48. if err != nil {
  49. return nil, err
  50. }
  51. return NewClient(append(options, Driver(drv))...), nil
  52. default:
  53. return nil, fmt.Errorf("unsupported driver: %q", driverName)
  54. }
  55. }
  56. // Tx returns a new transactional client.
  57. func (c *Client) Tx(ctx context.Context) (*Tx, error) {
  58. if _, ok := c.driver.(*txDriver); ok {
  59. return nil, fmt.Errorf("ent: cannot start a transaction within a transaction")
  60. }
  61. tx, err := newTx(ctx, c.driver)
  62. if err != nil {
  63. return nil, fmt.Errorf("ent: starting a transaction: %v", err)
  64. }
  65. cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks}
  66. return &Tx{
  67. config: cfg,
  68. Car: NewCarClient(cfg),
  69. Group: NewGroupClient(cfg),
  70. User: NewUserClient(cfg),
  71. }, nil
  72. }
  73. // Debug returns a new debug-client. It's used to get verbose logging on specific operations.
  74. //
  75. // client.Debug().
  76. // Car.
  77. // Query().
  78. // Count(ctx)
  79. //
  80. func (c *Client) Debug() *Client {
  81. if c.debug {
  82. return c
  83. }
  84. cfg := config{driver: dialect.Debug(c.driver, c.log), log: c.log, debug: true, hooks: c.hooks}
  85. client := &Client{config: cfg}
  86. client.init()
  87. return client
  88. }
  89. // Close closes the database connection and prevents new queries from starting.
  90. func (c *Client) Close() error {
  91. return c.driver.Close()
  92. }
  93. // Use adds the mutation hooks to all the entity clients.
  94. // In order to add hooks to a specific client, call: `client.Node.Use(...)`.
  95. func (c *Client) Use(hooks ...Hook) {
  96. c.Car.Use(hooks...)
  97. c.Group.Use(hooks...)
  98. c.User.Use(hooks...)
  99. }
  100. // CarClient is a client for the Car schema.
  101. type CarClient struct {
  102. config
  103. }
  104. // NewCarClient returns a client for the Car from the given config.
  105. func NewCarClient(c config) *CarClient {
  106. return &CarClient{config: c}
  107. }
  108. // Use adds a list of mutation hooks to the hooks stack.
  109. // A call to `Use(f, g, h)` equals to `car.Hooks(f(g(h())))`.
  110. func (c *CarClient) Use(hooks ...Hook) {
  111. c.hooks.Car = append(c.hooks.Car, hooks...)
  112. }
  113. // Create returns a create builder for Car.
  114. func (c *CarClient) Create() *CarCreate {
  115. mutation := newCarMutation(c.config, OpCreate)
  116. return &CarCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
  117. }
  118. // Update returns an update builder for Car.
  119. func (c *CarClient) Update() *CarUpdate {
  120. mutation := newCarMutation(c.config, OpUpdate)
  121. return &CarUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
  122. }
  123. // UpdateOne returns an update builder for the given entity.
  124. func (c *CarClient) UpdateOne(ca *Car) *CarUpdateOne {
  125. return c.UpdateOneID(ca.ID)
  126. }
  127. // UpdateOneID returns an update builder for the given id.
  128. func (c *CarClient) UpdateOneID(id int) *CarUpdateOne {
  129. mutation := newCarMutation(c.config, OpUpdateOne)
  130. mutation.id = &id
  131. return &CarUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
  132. }
  133. // Delete returns a delete builder for Car.
  134. func (c *CarClient) Delete() *CarDelete {
  135. mutation := newCarMutation(c.config, OpDelete)
  136. return &CarDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
  137. }
  138. // DeleteOne returns a delete builder for the given entity.
  139. func (c *CarClient) DeleteOne(ca *Car) *CarDeleteOne {
  140. return c.DeleteOneID(ca.ID)
  141. }
  142. // DeleteOneID returns a delete builder for the given id.
  143. func (c *CarClient) DeleteOneID(id int) *CarDeleteOne {
  144. builder := c.Delete().Where(car.ID(id))
  145. builder.mutation.id = &id
  146. builder.mutation.op = OpDeleteOne
  147. return &CarDeleteOne{builder}
  148. }
  149. // Create returns a query builder for Car.
  150. func (c *CarClient) Query() *CarQuery {
  151. return &CarQuery{config: c.config}
  152. }
  153. // Get returns a Car entity by its id.
  154. func (c *CarClient) Get(ctx context.Context, id int) (*Car, error) {
  155. return c.Query().Where(car.ID(id)).Only(ctx)
  156. }
  157. // GetX is like Get, but panics if an error occurs.
  158. func (c *CarClient) GetX(ctx context.Context, id int) *Car {
  159. ca, err := c.Get(ctx, id)
  160. if err != nil {
  161. panic(err)
  162. }
  163. return ca
  164. }
  165. // QueryOwner queries the owner edge of a Car.
  166. func (c *CarClient) QueryOwner(ca *Car) *UserQuery {
  167. query := &UserQuery{config: c.config}
  168. id := ca.ID
  169. step := sqlgraph.NewStep(
  170. sqlgraph.From(car.Table, car.FieldID, id),
  171. sqlgraph.To(user.Table, user.FieldID),
  172. sqlgraph.Edge(sqlgraph.M2O, true, car.OwnerTable, car.OwnerColumn),
  173. )
  174. query.sql = sqlgraph.Neighbors(ca.driver.Dialect(), step)
  175. return query
  176. }
  177. // Hooks returns the client hooks.
  178. func (c *CarClient) Hooks() []Hook {
  179. return c.hooks.Car
  180. }
  181. // GroupClient is a client for the Group schema.
  182. type GroupClient struct {
  183. config
  184. }
  185. // NewGroupClient returns a client for the Group from the given config.
  186. func NewGroupClient(c config) *GroupClient {
  187. return &GroupClient{config: c}
  188. }
  189. // Use adds a list of mutation hooks to the hooks stack.
  190. // A call to `Use(f, g, h)` equals to `group.Hooks(f(g(h())))`.
  191. func (c *GroupClient) Use(hooks ...Hook) {
  192. c.hooks.Group = append(c.hooks.Group, hooks...)
  193. }
  194. // Create returns a create builder for Group.
  195. func (c *GroupClient) Create() *GroupCreate {
  196. mutation := newGroupMutation(c.config, OpCreate)
  197. return &GroupCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
  198. }
  199. // Update returns an update builder for Group.
  200. func (c *GroupClient) Update() *GroupUpdate {
  201. mutation := newGroupMutation(c.config, OpUpdate)
  202. return &GroupUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
  203. }
  204. // UpdateOne returns an update builder for the given entity.
  205. func (c *GroupClient) UpdateOne(gr *Group) *GroupUpdateOne {
  206. return c.UpdateOneID(gr.ID)
  207. }
  208. // UpdateOneID returns an update builder for the given id.
  209. func (c *GroupClient) UpdateOneID(id int) *GroupUpdateOne {
  210. mutation := newGroupMutation(c.config, OpUpdateOne)
  211. mutation.id = &id
  212. return &GroupUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
  213. }
  214. // Delete returns a delete builder for Group.
  215. func (c *GroupClient) Delete() *GroupDelete {
  216. mutation := newGroupMutation(c.config, OpDelete)
  217. return &GroupDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
  218. }
  219. // DeleteOne returns a delete builder for the given entity.
  220. func (c *GroupClient) DeleteOne(gr *Group) *GroupDeleteOne {
  221. return c.DeleteOneID(gr.ID)
  222. }
  223. // DeleteOneID returns a delete builder for the given id.
  224. func (c *GroupClient) DeleteOneID(id int) *GroupDeleteOne {
  225. builder := c.Delete().Where(group.ID(id))
  226. builder.mutation.id = &id
  227. builder.mutation.op = OpDeleteOne
  228. return &GroupDeleteOne{builder}
  229. }
  230. // Create returns a query builder for Group.
  231. func (c *GroupClient) Query() *GroupQuery {
  232. return &GroupQuery{config: c.config}
  233. }
  234. // Get returns a Group entity by its id.
  235. func (c *GroupClient) Get(ctx context.Context, id int) (*Group, error) {
  236. return c.Query().Where(group.ID(id)).Only(ctx)
  237. }
  238. // GetX is like Get, but panics if an error occurs.
  239. func (c *GroupClient) GetX(ctx context.Context, id int) *Group {
  240. gr, err := c.Get(ctx, id)
  241. if err != nil {
  242. panic(err)
  243. }
  244. return gr
  245. }
  246. // QueryUsers queries the users edge of a Group.
  247. func (c *GroupClient) QueryUsers(gr *Group) *UserQuery {
  248. query := &UserQuery{config: c.config}
  249. id := gr.ID
  250. step := sqlgraph.NewStep(
  251. sqlgraph.From(group.Table, group.FieldID, id),
  252. sqlgraph.To(user.Table, user.FieldID),
  253. sqlgraph.Edge(sqlgraph.M2M, false, group.UsersTable, group.UsersPrimaryKey...),
  254. )
  255. query.sql = sqlgraph.Neighbors(gr.driver.Dialect(), step)
  256. return query
  257. }
  258. // Hooks returns the client hooks.
  259. func (c *GroupClient) Hooks() []Hook {
  260. return c.hooks.Group
  261. }
  262. // UserClient is a client for the User schema.
  263. type UserClient struct {
  264. config
  265. }
  266. // NewUserClient returns a client for the User from the given config.
  267. func NewUserClient(c config) *UserClient {
  268. return &UserClient{config: c}
  269. }
  270. // Use adds a list of mutation hooks to the hooks stack.
  271. // A call to `Use(f, g, h)` equals to `user.Hooks(f(g(h())))`.
  272. func (c *UserClient) Use(hooks ...Hook) {
  273. c.hooks.User = append(c.hooks.User, hooks...)
  274. }
  275. // Create returns a create builder for User.
  276. func (c *UserClient) Create() *UserCreate {
  277. mutation := newUserMutation(c.config, OpCreate)
  278. return &UserCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
  279. }
  280. // Update returns an update builder for User.
  281. func (c *UserClient) Update() *UserUpdate {
  282. mutation := newUserMutation(c.config, OpUpdate)
  283. return &UserUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
  284. }
  285. // UpdateOne returns an update builder for the given entity.
  286. func (c *UserClient) UpdateOne(u *User) *UserUpdateOne {
  287. return c.UpdateOneID(u.ID)
  288. }
  289. // UpdateOneID returns an update builder for the given id.
  290. func (c *UserClient) UpdateOneID(id int) *UserUpdateOne {
  291. mutation := newUserMutation(c.config, OpUpdateOne)
  292. mutation.id = &id
  293. return &UserUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
  294. }
  295. // Delete returns a delete builder for User.
  296. func (c *UserClient) Delete() *UserDelete {
  297. mutation := newUserMutation(c.config, OpDelete)
  298. return &UserDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
  299. }
  300. // DeleteOne returns a delete builder for the given entity.
  301. func (c *UserClient) DeleteOne(u *User) *UserDeleteOne {
  302. return c.DeleteOneID(u.ID)
  303. }
  304. // DeleteOneID returns a delete builder for the given id.
  305. func (c *UserClient) DeleteOneID(id int) *UserDeleteOne {
  306. builder := c.Delete().Where(user.ID(id))
  307. builder.mutation.id = &id
  308. builder.mutation.op = OpDeleteOne
  309. return &UserDeleteOne{builder}
  310. }
  311. // Create returns a query builder for User.
  312. func (c *UserClient) Query() *UserQuery {
  313. return &UserQuery{config: c.config}
  314. }
  315. // Get returns a User entity by its id.
  316. func (c *UserClient) Get(ctx context.Context, id int) (*User, error) {
  317. return c.Query().Where(user.ID(id)).Only(ctx)
  318. }
  319. // GetX is like Get, but panics if an error occurs.
  320. func (c *UserClient) GetX(ctx context.Context, id int) *User {
  321. u, err := c.Get(ctx, id)
  322. if err != nil {
  323. panic(err)
  324. }
  325. return u
  326. }
  327. // QueryCars queries the cars edge of a User.
  328. func (c *UserClient) QueryCars(u *User) *CarQuery {
  329. query := &CarQuery{config: c.config}
  330. id := u.ID
  331. step := sqlgraph.NewStep(
  332. sqlgraph.From(user.Table, user.FieldID, id),
  333. sqlgraph.To(car.Table, car.FieldID),
  334. sqlgraph.Edge(sqlgraph.O2M, false, user.CarsTable, user.CarsColumn),
  335. )
  336. query.sql = sqlgraph.Neighbors(u.driver.Dialect(), step)
  337. return query
  338. }
  339. // QueryGroups queries the groups edge of a User.
  340. func (c *UserClient) QueryGroups(u *User) *GroupQuery {
  341. query := &GroupQuery{config: c.config}
  342. id := u.ID
  343. step := sqlgraph.NewStep(
  344. sqlgraph.From(user.Table, user.FieldID, id),
  345. sqlgraph.To(group.Table, group.FieldID),
  346. sqlgraph.Edge(sqlgraph.M2M, true, user.GroupsTable, user.GroupsPrimaryKey...),
  347. )
  348. query.sql = sqlgraph.Neighbors(u.driver.Dialect(), step)
  349. return query
  350. }
  351. // Hooks returns the client hooks.
  352. func (c *UserClient) Hooks() []Hook {
  353. return c.hooks.User
  354. }