client.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "fmt"
  6. "log"
  7. "code.osinet.fr/fgm/go__ent_demo/ent/migrate"
  8. "code.osinet.fr/fgm/go__ent_demo/ent/car"
  9. "code.osinet.fr/fgm/go__ent_demo/ent/group"
  10. "code.osinet.fr/fgm/go__ent_demo/ent/user"
  11. "entgo.io/ent/dialect"
  12. "entgo.io/ent/dialect/sql"
  13. "entgo.io/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 database/sql.DB specified by the driver name and
  42. // the 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. The provided context
  57. // is used until the transaction is committed or rolled back.
  58. func (c *Client) Tx(ctx context.Context) (*Tx, error) {
  59. if _, ok := c.driver.(*txDriver); ok {
  60. return nil, fmt.Errorf("ent: cannot start a transaction within a transaction")
  61. }
  62. tx, err := newTx(ctx, c.driver)
  63. if err != nil {
  64. return nil, fmt.Errorf("ent: starting a transaction: %w", err)
  65. }
  66. cfg := c.config
  67. cfg.driver = tx
  68. return &Tx{
  69. ctx: ctx,
  70. config: cfg,
  71. Car: NewCarClient(cfg),
  72. Group: NewGroupClient(cfg),
  73. User: NewUserClient(cfg),
  74. }, nil
  75. }
  76. // BeginTx returns a transactional client with specified options.
  77. func (c *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
  78. if _, ok := c.driver.(*txDriver); ok {
  79. return nil, fmt.Errorf("ent: cannot start a transaction within a transaction")
  80. }
  81. tx, err := c.driver.(interface {
  82. BeginTx(context.Context, *sql.TxOptions) (dialect.Tx, error)
  83. }).BeginTx(ctx, opts)
  84. if err != nil {
  85. return nil, fmt.Errorf("ent: starting a transaction: %w", err)
  86. }
  87. cfg := c.config
  88. cfg.driver = &txDriver{tx: tx, drv: c.driver}
  89. return &Tx{
  90. ctx: ctx,
  91. config: cfg,
  92. Car: NewCarClient(cfg),
  93. Group: NewGroupClient(cfg),
  94. User: NewUserClient(cfg),
  95. }, nil
  96. }
  97. // Debug returns a new debug-client. It's used to get verbose logging on specific operations.
  98. //
  99. // client.Debug().
  100. // Car.
  101. // Query().
  102. // Count(ctx)
  103. //
  104. func (c *Client) Debug() *Client {
  105. if c.debug {
  106. return c
  107. }
  108. cfg := c.config
  109. cfg.driver = dialect.Debug(c.driver, c.log)
  110. client := &Client{config: cfg}
  111. client.init()
  112. return client
  113. }
  114. // Close closes the database connection and prevents new queries from starting.
  115. func (c *Client) Close() error {
  116. return c.driver.Close()
  117. }
  118. // Use adds the mutation hooks to all the entity clients.
  119. // In order to add hooks to a specific client, call: `client.Node.Use(...)`.
  120. func (c *Client) Use(hooks ...Hook) {
  121. c.Car.Use(hooks...)
  122. c.Group.Use(hooks...)
  123. c.User.Use(hooks...)
  124. }
  125. // CarClient is a client for the Car schema.
  126. type CarClient struct {
  127. config
  128. }
  129. // NewCarClient returns a client for the Car from the given config.
  130. func NewCarClient(c config) *CarClient {
  131. return &CarClient{config: c}
  132. }
  133. // Use adds a list of mutation hooks to the hooks stack.
  134. // A call to `Use(f, g, h)` equals to `car.Hooks(f(g(h())))`.
  135. func (c *CarClient) Use(hooks ...Hook) {
  136. c.hooks.Car = append(c.hooks.Car, hooks...)
  137. }
  138. // Create returns a create builder for Car.
  139. func (c *CarClient) Create() *CarCreate {
  140. mutation := newCarMutation(c.config, OpCreate)
  141. return &CarCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
  142. }
  143. // CreateBulk returns a builder for creating a bulk of Car entities.
  144. func (c *CarClient) CreateBulk(builders ...*CarCreate) *CarCreateBulk {
  145. return &CarCreateBulk{config: c.config, builders: builders}
  146. }
  147. // Update returns an update builder for Car.
  148. func (c *CarClient) Update() *CarUpdate {
  149. mutation := newCarMutation(c.config, OpUpdate)
  150. return &CarUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
  151. }
  152. // UpdateOne returns an update builder for the given entity.
  153. func (c *CarClient) UpdateOne(ca *Car) *CarUpdateOne {
  154. mutation := newCarMutation(c.config, OpUpdateOne, withCar(ca))
  155. return &CarUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
  156. }
  157. // UpdateOneID returns an update builder for the given id.
  158. func (c *CarClient) UpdateOneID(id int) *CarUpdateOne {
  159. mutation := newCarMutation(c.config, OpUpdateOne, withCarID(id))
  160. return &CarUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
  161. }
  162. // Delete returns a delete builder for Car.
  163. func (c *CarClient) Delete() *CarDelete {
  164. mutation := newCarMutation(c.config, OpDelete)
  165. return &CarDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
  166. }
  167. // DeleteOne returns a delete builder for the given entity.
  168. func (c *CarClient) DeleteOne(ca *Car) *CarDeleteOne {
  169. return c.DeleteOneID(ca.ID)
  170. }
  171. // DeleteOneID returns a delete builder for the given id.
  172. func (c *CarClient) DeleteOneID(id int) *CarDeleteOne {
  173. builder := c.Delete().Where(car.ID(id))
  174. builder.mutation.id = &id
  175. builder.mutation.op = OpDeleteOne
  176. return &CarDeleteOne{builder}
  177. }
  178. // Query returns a query builder for Car.
  179. func (c *CarClient) Query() *CarQuery {
  180. return &CarQuery{
  181. config: c.config,
  182. }
  183. }
  184. // Get returns a Car entity by its id.
  185. func (c *CarClient) Get(ctx context.Context, id int) (*Car, error) {
  186. return c.Query().Where(car.ID(id)).Only(ctx)
  187. }
  188. // GetX is like Get, but panics if an error occurs.
  189. func (c *CarClient) GetX(ctx context.Context, id int) *Car {
  190. obj, err := c.Get(ctx, id)
  191. if err != nil {
  192. panic(err)
  193. }
  194. return obj
  195. }
  196. // QueryOwner queries the owner edge of a Car.
  197. func (c *CarClient) QueryOwner(ca *Car) *UserQuery {
  198. query := &UserQuery{config: c.config}
  199. query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) {
  200. id := ca.ID
  201. step := sqlgraph.NewStep(
  202. sqlgraph.From(car.Table, car.FieldID, id),
  203. sqlgraph.To(user.Table, user.FieldID),
  204. sqlgraph.Edge(sqlgraph.M2O, true, car.OwnerTable, car.OwnerColumn),
  205. )
  206. fromV = sqlgraph.Neighbors(ca.driver.Dialect(), step)
  207. return fromV, nil
  208. }
  209. return query
  210. }
  211. // Hooks returns the client hooks.
  212. func (c *CarClient) Hooks() []Hook {
  213. return c.hooks.Car
  214. }
  215. // GroupClient is a client for the Group schema.
  216. type GroupClient struct {
  217. config
  218. }
  219. // NewGroupClient returns a client for the Group from the given config.
  220. func NewGroupClient(c config) *GroupClient {
  221. return &GroupClient{config: c}
  222. }
  223. // Use adds a list of mutation hooks to the hooks stack.
  224. // A call to `Use(f, g, h)` equals to `group.Hooks(f(g(h())))`.
  225. func (c *GroupClient) Use(hooks ...Hook) {
  226. c.hooks.Group = append(c.hooks.Group, hooks...)
  227. }
  228. // Create returns a create builder for Group.
  229. func (c *GroupClient) Create() *GroupCreate {
  230. mutation := newGroupMutation(c.config, OpCreate)
  231. return &GroupCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
  232. }
  233. // CreateBulk returns a builder for creating a bulk of Group entities.
  234. func (c *GroupClient) CreateBulk(builders ...*GroupCreate) *GroupCreateBulk {
  235. return &GroupCreateBulk{config: c.config, builders: builders}
  236. }
  237. // Update returns an update builder for Group.
  238. func (c *GroupClient) Update() *GroupUpdate {
  239. mutation := newGroupMutation(c.config, OpUpdate)
  240. return &GroupUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
  241. }
  242. // UpdateOne returns an update builder for the given entity.
  243. func (c *GroupClient) UpdateOne(gr *Group) *GroupUpdateOne {
  244. mutation := newGroupMutation(c.config, OpUpdateOne, withGroup(gr))
  245. return &GroupUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
  246. }
  247. // UpdateOneID returns an update builder for the given id.
  248. func (c *GroupClient) UpdateOneID(id int) *GroupUpdateOne {
  249. mutation := newGroupMutation(c.config, OpUpdateOne, withGroupID(id))
  250. return &GroupUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
  251. }
  252. // Delete returns a delete builder for Group.
  253. func (c *GroupClient) Delete() *GroupDelete {
  254. mutation := newGroupMutation(c.config, OpDelete)
  255. return &GroupDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
  256. }
  257. // DeleteOne returns a delete builder for the given entity.
  258. func (c *GroupClient) DeleteOne(gr *Group) *GroupDeleteOne {
  259. return c.DeleteOneID(gr.ID)
  260. }
  261. // DeleteOneID returns a delete builder for the given id.
  262. func (c *GroupClient) DeleteOneID(id int) *GroupDeleteOne {
  263. builder := c.Delete().Where(group.ID(id))
  264. builder.mutation.id = &id
  265. builder.mutation.op = OpDeleteOne
  266. return &GroupDeleteOne{builder}
  267. }
  268. // Query returns a query builder for Group.
  269. func (c *GroupClient) Query() *GroupQuery {
  270. return &GroupQuery{
  271. config: c.config,
  272. }
  273. }
  274. // Get returns a Group entity by its id.
  275. func (c *GroupClient) Get(ctx context.Context, id int) (*Group, error) {
  276. return c.Query().Where(group.ID(id)).Only(ctx)
  277. }
  278. // GetX is like Get, but panics if an error occurs.
  279. func (c *GroupClient) GetX(ctx context.Context, id int) *Group {
  280. obj, err := c.Get(ctx, id)
  281. if err != nil {
  282. panic(err)
  283. }
  284. return obj
  285. }
  286. // QueryUsers queries the users edge of a Group.
  287. func (c *GroupClient) QueryUsers(gr *Group) *UserQuery {
  288. query := &UserQuery{config: c.config}
  289. query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) {
  290. id := gr.ID
  291. step := sqlgraph.NewStep(
  292. sqlgraph.From(group.Table, group.FieldID, id),
  293. sqlgraph.To(user.Table, user.FieldID),
  294. sqlgraph.Edge(sqlgraph.M2M, false, group.UsersTable, group.UsersPrimaryKey...),
  295. )
  296. fromV = sqlgraph.Neighbors(gr.driver.Dialect(), step)
  297. return fromV, nil
  298. }
  299. return query
  300. }
  301. // Hooks returns the client hooks.
  302. func (c *GroupClient) Hooks() []Hook {
  303. return c.hooks.Group
  304. }
  305. // UserClient is a client for the User schema.
  306. type UserClient struct {
  307. config
  308. }
  309. // NewUserClient returns a client for the User from the given config.
  310. func NewUserClient(c config) *UserClient {
  311. return &UserClient{config: c}
  312. }
  313. // Use adds a list of mutation hooks to the hooks stack.
  314. // A call to `Use(f, g, h)` equals to `user.Hooks(f(g(h())))`.
  315. func (c *UserClient) Use(hooks ...Hook) {
  316. c.hooks.User = append(c.hooks.User, hooks...)
  317. }
  318. // Create returns a create builder for User.
  319. func (c *UserClient) Create() *UserCreate {
  320. mutation := newUserMutation(c.config, OpCreate)
  321. return &UserCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
  322. }
  323. // CreateBulk returns a builder for creating a bulk of User entities.
  324. func (c *UserClient) CreateBulk(builders ...*UserCreate) *UserCreateBulk {
  325. return &UserCreateBulk{config: c.config, builders: builders}
  326. }
  327. // Update returns an update builder for User.
  328. func (c *UserClient) Update() *UserUpdate {
  329. mutation := newUserMutation(c.config, OpUpdate)
  330. return &UserUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
  331. }
  332. // UpdateOne returns an update builder for the given entity.
  333. func (c *UserClient) UpdateOne(u *User) *UserUpdateOne {
  334. mutation := newUserMutation(c.config, OpUpdateOne, withUser(u))
  335. return &UserUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
  336. }
  337. // UpdateOneID returns an update builder for the given id.
  338. func (c *UserClient) UpdateOneID(id int) *UserUpdateOne {
  339. mutation := newUserMutation(c.config, OpUpdateOne, withUserID(id))
  340. return &UserUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
  341. }
  342. // Delete returns a delete builder for User.
  343. func (c *UserClient) Delete() *UserDelete {
  344. mutation := newUserMutation(c.config, OpDelete)
  345. return &UserDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
  346. }
  347. // DeleteOne returns a delete builder for the given entity.
  348. func (c *UserClient) DeleteOne(u *User) *UserDeleteOne {
  349. return c.DeleteOneID(u.ID)
  350. }
  351. // DeleteOneID returns a delete builder for the given id.
  352. func (c *UserClient) DeleteOneID(id int) *UserDeleteOne {
  353. builder := c.Delete().Where(user.ID(id))
  354. builder.mutation.id = &id
  355. builder.mutation.op = OpDeleteOne
  356. return &UserDeleteOne{builder}
  357. }
  358. // Query returns a query builder for User.
  359. func (c *UserClient) Query() *UserQuery {
  360. return &UserQuery{
  361. config: c.config,
  362. }
  363. }
  364. // Get returns a User entity by its id.
  365. func (c *UserClient) Get(ctx context.Context, id int) (*User, error) {
  366. return c.Query().Where(user.ID(id)).Only(ctx)
  367. }
  368. // GetX is like Get, but panics if an error occurs.
  369. func (c *UserClient) GetX(ctx context.Context, id int) *User {
  370. obj, err := c.Get(ctx, id)
  371. if err != nil {
  372. panic(err)
  373. }
  374. return obj
  375. }
  376. // QueryCars queries the cars edge of a User.
  377. func (c *UserClient) QueryCars(u *User) *CarQuery {
  378. query := &CarQuery{config: c.config}
  379. query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) {
  380. id := u.ID
  381. step := sqlgraph.NewStep(
  382. sqlgraph.From(user.Table, user.FieldID, id),
  383. sqlgraph.To(car.Table, car.FieldID),
  384. sqlgraph.Edge(sqlgraph.O2M, false, user.CarsTable, user.CarsColumn),
  385. )
  386. fromV = sqlgraph.Neighbors(u.driver.Dialect(), step)
  387. return fromV, nil
  388. }
  389. return query
  390. }
  391. // QueryGroups queries the groups edge of a User.
  392. func (c *UserClient) QueryGroups(u *User) *GroupQuery {
  393. query := &GroupQuery{config: c.config}
  394. query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) {
  395. id := u.ID
  396. step := sqlgraph.NewStep(
  397. sqlgraph.From(user.Table, user.FieldID, id),
  398. sqlgraph.To(group.Table, group.FieldID),
  399. sqlgraph.Edge(sqlgraph.M2M, true, user.GroupsTable, user.GroupsPrimaryKey...),
  400. )
  401. fromV = sqlgraph.Neighbors(u.driver.Dialect(), step)
  402. return fromV, nil
  403. }
  404. return query
  405. }
  406. // QuerySpouse queries the spouse edge of a User.
  407. func (c *UserClient) QuerySpouse(u *User) *UserQuery {
  408. query := &UserQuery{config: c.config}
  409. query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) {
  410. id := u.ID
  411. step := sqlgraph.NewStep(
  412. sqlgraph.From(user.Table, user.FieldID, id),
  413. sqlgraph.To(user.Table, user.FieldID),
  414. sqlgraph.Edge(sqlgraph.O2O, false, user.SpouseTable, user.SpouseColumn),
  415. )
  416. fromV = sqlgraph.Neighbors(u.driver.Dialect(), step)
  417. return fromV, nil
  418. }
  419. return query
  420. }
  421. // QueryFollowers queries the followers edge of a User.
  422. func (c *UserClient) QueryFollowers(u *User) *UserQuery {
  423. query := &UserQuery{config: c.config}
  424. query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) {
  425. id := u.ID
  426. step := sqlgraph.NewStep(
  427. sqlgraph.From(user.Table, user.FieldID, id),
  428. sqlgraph.To(user.Table, user.FieldID),
  429. sqlgraph.Edge(sqlgraph.M2M, true, user.FollowersTable, user.FollowersPrimaryKey...),
  430. )
  431. fromV = sqlgraph.Neighbors(u.driver.Dialect(), step)
  432. return fromV, nil
  433. }
  434. return query
  435. }
  436. // QueryFollowing queries the following edge of a User.
  437. func (c *UserClient) QueryFollowing(u *User) *UserQuery {
  438. query := &UserQuery{config: c.config}
  439. query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) {
  440. id := u.ID
  441. step := sqlgraph.NewStep(
  442. sqlgraph.From(user.Table, user.FieldID, id),
  443. sqlgraph.To(user.Table, user.FieldID),
  444. sqlgraph.Edge(sqlgraph.M2M, false, user.FollowingTable, user.FollowingPrimaryKey...),
  445. )
  446. fromV = sqlgraph.Neighbors(u.driver.Dialect(), step)
  447. return fromV, nil
  448. }
  449. return query
  450. }
  451. // QueryFriends queries the friends edge of a User.
  452. func (c *UserClient) QueryFriends(u *User) *UserQuery {
  453. query := &UserQuery{config: c.config}
  454. query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) {
  455. id := u.ID
  456. step := sqlgraph.NewStep(
  457. sqlgraph.From(user.Table, user.FieldID, id),
  458. sqlgraph.To(user.Table, user.FieldID),
  459. sqlgraph.Edge(sqlgraph.M2M, false, user.FriendsTable, user.FriendsPrimaryKey...),
  460. )
  461. fromV = sqlgraph.Neighbors(u.driver.Dialect(), step)
  462. return fromV, nil
  463. }
  464. return query
  465. }
  466. // Hooks returns the client hooks.
  467. func (c *UserClient) Hooks() []Hook {
  468. return c.hooks.User
  469. }