client.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "log"
  8. "reflect"
  9. "wechat-api/ent/migrate"
  10. "wechat-api/ent/contact"
  11. "wechat-api/ent/server"
  12. "wechat-api/ent/wx"
  13. "entgo.io/ent"
  14. "entgo.io/ent/dialect"
  15. "entgo.io/ent/dialect/sql"
  16. "entgo.io/ent/dialect/sql/sqlgraph"
  17. stdsql "database/sql"
  18. )
  19. // Client is the client that holds all ent builders.
  20. type Client struct {
  21. config
  22. // Schema is the client for creating, migrating and dropping schema.
  23. Schema *migrate.Schema
  24. // Contact is the client for interacting with the Contact builders.
  25. Contact *ContactClient
  26. // Server is the client for interacting with the Server builders.
  27. Server *ServerClient
  28. // Wx is the client for interacting with the Wx builders.
  29. Wx *WxClient
  30. }
  31. // NewClient creates a new client configured with the given options.
  32. func NewClient(opts ...Option) *Client {
  33. client := &Client{config: newConfig(opts...)}
  34. client.init()
  35. return client
  36. }
  37. func (c *Client) init() {
  38. c.Schema = migrate.NewSchema(c.driver)
  39. c.Contact = NewContactClient(c.config)
  40. c.Server = NewServerClient(c.config)
  41. c.Wx = NewWxClient(c.config)
  42. }
  43. type (
  44. // config is the configuration for the client and its builder.
  45. config struct {
  46. // driver used for executing database requests.
  47. driver dialect.Driver
  48. // debug enable a debug logging.
  49. debug bool
  50. // log used for logging on debug mode.
  51. log func(...any)
  52. // hooks to execute on mutations.
  53. hooks *hooks
  54. // interceptors to execute on queries.
  55. inters *inters
  56. }
  57. // Option function to configure the client.
  58. Option func(*config)
  59. )
  60. // newConfig creates a new config for the client.
  61. func newConfig(opts ...Option) config {
  62. cfg := config{log: log.Println, hooks: &hooks{}, inters: &inters{}}
  63. cfg.options(opts...)
  64. return cfg
  65. }
  66. // options applies the options on the config object.
  67. func (c *config) options(opts ...Option) {
  68. for _, opt := range opts {
  69. opt(c)
  70. }
  71. if c.debug {
  72. c.driver = dialect.Debug(c.driver, c.log)
  73. }
  74. }
  75. // Debug enables debug logging on the ent.Driver.
  76. func Debug() Option {
  77. return func(c *config) {
  78. c.debug = true
  79. }
  80. }
  81. // Log sets the logging function for debug mode.
  82. func Log(fn func(...any)) Option {
  83. return func(c *config) {
  84. c.log = fn
  85. }
  86. }
  87. // Driver configures the client driver.
  88. func Driver(driver dialect.Driver) Option {
  89. return func(c *config) {
  90. c.driver = driver
  91. }
  92. }
  93. // Open opens a database/sql.DB specified by the driver name and
  94. // the data source name, and returns a new client attached to it.
  95. // Optional parameters can be added for configuring the client.
  96. func Open(driverName, dataSourceName string, options ...Option) (*Client, error) {
  97. switch driverName {
  98. case dialect.MySQL, dialect.Postgres, dialect.SQLite:
  99. drv, err := sql.Open(driverName, dataSourceName)
  100. if err != nil {
  101. return nil, err
  102. }
  103. return NewClient(append(options, Driver(drv))...), nil
  104. default:
  105. return nil, fmt.Errorf("unsupported driver: %q", driverName)
  106. }
  107. }
  108. // ErrTxStarted is returned when trying to start a new transaction from a transactional client.
  109. var ErrTxStarted = errors.New("ent: cannot start a transaction within a transaction")
  110. // Tx returns a new transactional client. The provided context
  111. // is used until the transaction is committed or rolled back.
  112. func (c *Client) Tx(ctx context.Context) (*Tx, error) {
  113. if _, ok := c.driver.(*txDriver); ok {
  114. return nil, ErrTxStarted
  115. }
  116. tx, err := newTx(ctx, c.driver)
  117. if err != nil {
  118. return nil, fmt.Errorf("ent: starting a transaction: %w", err)
  119. }
  120. cfg := c.config
  121. cfg.driver = tx
  122. return &Tx{
  123. ctx: ctx,
  124. config: cfg,
  125. Contact: NewContactClient(cfg),
  126. Server: NewServerClient(cfg),
  127. Wx: NewWxClient(cfg),
  128. }, nil
  129. }
  130. // BeginTx returns a transactional client with specified options.
  131. func (c *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
  132. if _, ok := c.driver.(*txDriver); ok {
  133. return nil, errors.New("ent: cannot start a transaction within a transaction")
  134. }
  135. tx, err := c.driver.(interface {
  136. BeginTx(context.Context, *sql.TxOptions) (dialect.Tx, error)
  137. }).BeginTx(ctx, opts)
  138. if err != nil {
  139. return nil, fmt.Errorf("ent: starting a transaction: %w", err)
  140. }
  141. cfg := c.config
  142. cfg.driver = &txDriver{tx: tx, drv: c.driver}
  143. return &Tx{
  144. ctx: ctx,
  145. config: cfg,
  146. Contact: NewContactClient(cfg),
  147. Server: NewServerClient(cfg),
  148. Wx: NewWxClient(cfg),
  149. }, nil
  150. }
  151. // Debug returns a new debug-client. It's used to get verbose logging on specific operations.
  152. //
  153. // client.Debug().
  154. // Contact.
  155. // Query().
  156. // Count(ctx)
  157. func (c *Client) Debug() *Client {
  158. if c.debug {
  159. return c
  160. }
  161. cfg := c.config
  162. cfg.driver = dialect.Debug(c.driver, c.log)
  163. client := &Client{config: cfg}
  164. client.init()
  165. return client
  166. }
  167. // Close closes the database connection and prevents new queries from starting.
  168. func (c *Client) Close() error {
  169. return c.driver.Close()
  170. }
  171. // Use adds the mutation hooks to all the entity clients.
  172. // In order to add hooks to a specific client, call: `client.Node.Use(...)`.
  173. func (c *Client) Use(hooks ...Hook) {
  174. c.Contact.Use(hooks...)
  175. c.Server.Use(hooks...)
  176. c.Wx.Use(hooks...)
  177. }
  178. // Intercept adds the query interceptors to all the entity clients.
  179. // In order to add interceptors to a specific client, call: `client.Node.Intercept(...)`.
  180. func (c *Client) Intercept(interceptors ...Interceptor) {
  181. c.Contact.Intercept(interceptors...)
  182. c.Server.Intercept(interceptors...)
  183. c.Wx.Intercept(interceptors...)
  184. }
  185. // Mutate implements the ent.Mutator interface.
  186. func (c *Client) Mutate(ctx context.Context, m Mutation) (Value, error) {
  187. switch m := m.(type) {
  188. case *ContactMutation:
  189. return c.Contact.mutate(ctx, m)
  190. case *ServerMutation:
  191. return c.Server.mutate(ctx, m)
  192. case *WxMutation:
  193. return c.Wx.mutate(ctx, m)
  194. default:
  195. return nil, fmt.Errorf("ent: unknown mutation type %T", m)
  196. }
  197. }
  198. // ContactClient is a client for the Contact schema.
  199. type ContactClient struct {
  200. config
  201. }
  202. // NewContactClient returns a client for the Contact from the given config.
  203. func NewContactClient(c config) *ContactClient {
  204. return &ContactClient{config: c}
  205. }
  206. // Use adds a list of mutation hooks to the hooks stack.
  207. // A call to `Use(f, g, h)` equals to `contact.Hooks(f(g(h())))`.
  208. func (c *ContactClient) Use(hooks ...Hook) {
  209. c.hooks.Contact = append(c.hooks.Contact, hooks...)
  210. }
  211. // Intercept adds a list of query interceptors to the interceptors stack.
  212. // A call to `Intercept(f, g, h)` equals to `contact.Intercept(f(g(h())))`.
  213. func (c *ContactClient) Intercept(interceptors ...Interceptor) {
  214. c.inters.Contact = append(c.inters.Contact, interceptors...)
  215. }
  216. // Create returns a builder for creating a Contact entity.
  217. func (c *ContactClient) Create() *ContactCreate {
  218. mutation := newContactMutation(c.config, OpCreate)
  219. return &ContactCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
  220. }
  221. // CreateBulk returns a builder for creating a bulk of Contact entities.
  222. func (c *ContactClient) CreateBulk(builders ...*ContactCreate) *ContactCreateBulk {
  223. return &ContactCreateBulk{config: c.config, builders: builders}
  224. }
  225. // MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates
  226. // a builder and applies setFunc on it.
  227. func (c *ContactClient) MapCreateBulk(slice any, setFunc func(*ContactCreate, int)) *ContactCreateBulk {
  228. rv := reflect.ValueOf(slice)
  229. if rv.Kind() != reflect.Slice {
  230. return &ContactCreateBulk{err: fmt.Errorf("calling to ContactClient.MapCreateBulk with wrong type %T, need slice", slice)}
  231. }
  232. builders := make([]*ContactCreate, rv.Len())
  233. for i := 0; i < rv.Len(); i++ {
  234. builders[i] = c.Create()
  235. setFunc(builders[i], i)
  236. }
  237. return &ContactCreateBulk{config: c.config, builders: builders}
  238. }
  239. // Update returns an update builder for Contact.
  240. func (c *ContactClient) Update() *ContactUpdate {
  241. mutation := newContactMutation(c.config, OpUpdate)
  242. return &ContactUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
  243. }
  244. // UpdateOne returns an update builder for the given entity.
  245. func (c *ContactClient) UpdateOne(co *Contact) *ContactUpdateOne {
  246. mutation := newContactMutation(c.config, OpUpdateOne, withContact(co))
  247. return &ContactUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
  248. }
  249. // UpdateOneID returns an update builder for the given id.
  250. func (c *ContactClient) UpdateOneID(id uint64) *ContactUpdateOne {
  251. mutation := newContactMutation(c.config, OpUpdateOne, withContactID(id))
  252. return &ContactUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
  253. }
  254. // Delete returns a delete builder for Contact.
  255. func (c *ContactClient) Delete() *ContactDelete {
  256. mutation := newContactMutation(c.config, OpDelete)
  257. return &ContactDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
  258. }
  259. // DeleteOne returns a builder for deleting the given entity.
  260. func (c *ContactClient) DeleteOne(co *Contact) *ContactDeleteOne {
  261. return c.DeleteOneID(co.ID)
  262. }
  263. // DeleteOneID returns a builder for deleting the given entity by its id.
  264. func (c *ContactClient) DeleteOneID(id uint64) *ContactDeleteOne {
  265. builder := c.Delete().Where(contact.ID(id))
  266. builder.mutation.id = &id
  267. builder.mutation.op = OpDeleteOne
  268. return &ContactDeleteOne{builder}
  269. }
  270. // Query returns a query builder for Contact.
  271. func (c *ContactClient) Query() *ContactQuery {
  272. return &ContactQuery{
  273. config: c.config,
  274. ctx: &QueryContext{Type: TypeContact},
  275. inters: c.Interceptors(),
  276. }
  277. }
  278. // Get returns a Contact entity by its id.
  279. func (c *ContactClient) Get(ctx context.Context, id uint64) (*Contact, error) {
  280. return c.Query().Where(contact.ID(id)).Only(ctx)
  281. }
  282. // GetX is like Get, but panics if an error occurs.
  283. func (c *ContactClient) GetX(ctx context.Context, id uint64) *Contact {
  284. obj, err := c.Get(ctx, id)
  285. if err != nil {
  286. panic(err)
  287. }
  288. return obj
  289. }
  290. // Hooks returns the client hooks.
  291. func (c *ContactClient) Hooks() []Hook {
  292. hooks := c.hooks.Contact
  293. return append(hooks[:len(hooks):len(hooks)], contact.Hooks[:]...)
  294. }
  295. // Interceptors returns the client interceptors.
  296. func (c *ContactClient) Interceptors() []Interceptor {
  297. inters := c.inters.Contact
  298. return append(inters[:len(inters):len(inters)], contact.Interceptors[:]...)
  299. }
  300. func (c *ContactClient) mutate(ctx context.Context, m *ContactMutation) (Value, error) {
  301. switch m.Op() {
  302. case OpCreate:
  303. return (&ContactCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
  304. case OpUpdate:
  305. return (&ContactUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
  306. case OpUpdateOne:
  307. return (&ContactUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
  308. case OpDelete, OpDeleteOne:
  309. return (&ContactDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
  310. default:
  311. return nil, fmt.Errorf("ent: unknown Contact mutation op: %q", m.Op())
  312. }
  313. }
  314. // ServerClient is a client for the Server schema.
  315. type ServerClient struct {
  316. config
  317. }
  318. // NewServerClient returns a client for the Server from the given config.
  319. func NewServerClient(c config) *ServerClient {
  320. return &ServerClient{config: c}
  321. }
  322. // Use adds a list of mutation hooks to the hooks stack.
  323. // A call to `Use(f, g, h)` equals to `server.Hooks(f(g(h())))`.
  324. func (c *ServerClient) Use(hooks ...Hook) {
  325. c.hooks.Server = append(c.hooks.Server, hooks...)
  326. }
  327. // Intercept adds a list of query interceptors to the interceptors stack.
  328. // A call to `Intercept(f, g, h)` equals to `server.Intercept(f(g(h())))`.
  329. func (c *ServerClient) Intercept(interceptors ...Interceptor) {
  330. c.inters.Server = append(c.inters.Server, interceptors...)
  331. }
  332. // Create returns a builder for creating a Server entity.
  333. func (c *ServerClient) Create() *ServerCreate {
  334. mutation := newServerMutation(c.config, OpCreate)
  335. return &ServerCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
  336. }
  337. // CreateBulk returns a builder for creating a bulk of Server entities.
  338. func (c *ServerClient) CreateBulk(builders ...*ServerCreate) *ServerCreateBulk {
  339. return &ServerCreateBulk{config: c.config, builders: builders}
  340. }
  341. // MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates
  342. // a builder and applies setFunc on it.
  343. func (c *ServerClient) MapCreateBulk(slice any, setFunc func(*ServerCreate, int)) *ServerCreateBulk {
  344. rv := reflect.ValueOf(slice)
  345. if rv.Kind() != reflect.Slice {
  346. return &ServerCreateBulk{err: fmt.Errorf("calling to ServerClient.MapCreateBulk with wrong type %T, need slice", slice)}
  347. }
  348. builders := make([]*ServerCreate, rv.Len())
  349. for i := 0; i < rv.Len(); i++ {
  350. builders[i] = c.Create()
  351. setFunc(builders[i], i)
  352. }
  353. return &ServerCreateBulk{config: c.config, builders: builders}
  354. }
  355. // Update returns an update builder for Server.
  356. func (c *ServerClient) Update() *ServerUpdate {
  357. mutation := newServerMutation(c.config, OpUpdate)
  358. return &ServerUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
  359. }
  360. // UpdateOne returns an update builder for the given entity.
  361. func (c *ServerClient) UpdateOne(s *Server) *ServerUpdateOne {
  362. mutation := newServerMutation(c.config, OpUpdateOne, withServer(s))
  363. return &ServerUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
  364. }
  365. // UpdateOneID returns an update builder for the given id.
  366. func (c *ServerClient) UpdateOneID(id uint64) *ServerUpdateOne {
  367. mutation := newServerMutation(c.config, OpUpdateOne, withServerID(id))
  368. return &ServerUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
  369. }
  370. // Delete returns a delete builder for Server.
  371. func (c *ServerClient) Delete() *ServerDelete {
  372. mutation := newServerMutation(c.config, OpDelete)
  373. return &ServerDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
  374. }
  375. // DeleteOne returns a builder for deleting the given entity.
  376. func (c *ServerClient) DeleteOne(s *Server) *ServerDeleteOne {
  377. return c.DeleteOneID(s.ID)
  378. }
  379. // DeleteOneID returns a builder for deleting the given entity by its id.
  380. func (c *ServerClient) DeleteOneID(id uint64) *ServerDeleteOne {
  381. builder := c.Delete().Where(server.ID(id))
  382. builder.mutation.id = &id
  383. builder.mutation.op = OpDeleteOne
  384. return &ServerDeleteOne{builder}
  385. }
  386. // Query returns a query builder for Server.
  387. func (c *ServerClient) Query() *ServerQuery {
  388. return &ServerQuery{
  389. config: c.config,
  390. ctx: &QueryContext{Type: TypeServer},
  391. inters: c.Interceptors(),
  392. }
  393. }
  394. // Get returns a Server entity by its id.
  395. func (c *ServerClient) Get(ctx context.Context, id uint64) (*Server, error) {
  396. return c.Query().Where(server.ID(id)).Only(ctx)
  397. }
  398. // GetX is like Get, but panics if an error occurs.
  399. func (c *ServerClient) GetX(ctx context.Context, id uint64) *Server {
  400. obj, err := c.Get(ctx, id)
  401. if err != nil {
  402. panic(err)
  403. }
  404. return obj
  405. }
  406. // QueryWxs queries the wxs edge of a Server.
  407. func (c *ServerClient) QueryWxs(s *Server) *WxQuery {
  408. query := (&WxClient{config: c.config}).Query()
  409. query.path = func(context.Context) (fromV *sql.Selector, _ error) {
  410. id := s.ID
  411. step := sqlgraph.NewStep(
  412. sqlgraph.From(server.Table, server.FieldID, id),
  413. sqlgraph.To(wx.Table, wx.FieldID),
  414. sqlgraph.Edge(sqlgraph.O2M, false, server.WxsTable, server.WxsColumn),
  415. )
  416. fromV = sqlgraph.Neighbors(s.driver.Dialect(), step)
  417. return fromV, nil
  418. }
  419. return query
  420. }
  421. // Hooks returns the client hooks.
  422. func (c *ServerClient) Hooks() []Hook {
  423. hooks := c.hooks.Server
  424. return append(hooks[:len(hooks):len(hooks)], server.Hooks[:]...)
  425. }
  426. // Interceptors returns the client interceptors.
  427. func (c *ServerClient) Interceptors() []Interceptor {
  428. inters := c.inters.Server
  429. return append(inters[:len(inters):len(inters)], server.Interceptors[:]...)
  430. }
  431. func (c *ServerClient) mutate(ctx context.Context, m *ServerMutation) (Value, error) {
  432. switch m.Op() {
  433. case OpCreate:
  434. return (&ServerCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
  435. case OpUpdate:
  436. return (&ServerUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
  437. case OpUpdateOne:
  438. return (&ServerUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
  439. case OpDelete, OpDeleteOne:
  440. return (&ServerDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
  441. default:
  442. return nil, fmt.Errorf("ent: unknown Server mutation op: %q", m.Op())
  443. }
  444. }
  445. // WxClient is a client for the Wx schema.
  446. type WxClient struct {
  447. config
  448. }
  449. // NewWxClient returns a client for the Wx from the given config.
  450. func NewWxClient(c config) *WxClient {
  451. return &WxClient{config: c}
  452. }
  453. // Use adds a list of mutation hooks to the hooks stack.
  454. // A call to `Use(f, g, h)` equals to `wx.Hooks(f(g(h())))`.
  455. func (c *WxClient) Use(hooks ...Hook) {
  456. c.hooks.Wx = append(c.hooks.Wx, hooks...)
  457. }
  458. // Intercept adds a list of query interceptors to the interceptors stack.
  459. // A call to `Intercept(f, g, h)` equals to `wx.Intercept(f(g(h())))`.
  460. func (c *WxClient) Intercept(interceptors ...Interceptor) {
  461. c.inters.Wx = append(c.inters.Wx, interceptors...)
  462. }
  463. // Create returns a builder for creating a Wx entity.
  464. func (c *WxClient) Create() *WxCreate {
  465. mutation := newWxMutation(c.config, OpCreate)
  466. return &WxCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
  467. }
  468. // CreateBulk returns a builder for creating a bulk of Wx entities.
  469. func (c *WxClient) CreateBulk(builders ...*WxCreate) *WxCreateBulk {
  470. return &WxCreateBulk{config: c.config, builders: builders}
  471. }
  472. // MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates
  473. // a builder and applies setFunc on it.
  474. func (c *WxClient) MapCreateBulk(slice any, setFunc func(*WxCreate, int)) *WxCreateBulk {
  475. rv := reflect.ValueOf(slice)
  476. if rv.Kind() != reflect.Slice {
  477. return &WxCreateBulk{err: fmt.Errorf("calling to WxClient.MapCreateBulk with wrong type %T, need slice", slice)}
  478. }
  479. builders := make([]*WxCreate, rv.Len())
  480. for i := 0; i < rv.Len(); i++ {
  481. builders[i] = c.Create()
  482. setFunc(builders[i], i)
  483. }
  484. return &WxCreateBulk{config: c.config, builders: builders}
  485. }
  486. // Update returns an update builder for Wx.
  487. func (c *WxClient) Update() *WxUpdate {
  488. mutation := newWxMutation(c.config, OpUpdate)
  489. return &WxUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
  490. }
  491. // UpdateOne returns an update builder for the given entity.
  492. func (c *WxClient) UpdateOne(w *Wx) *WxUpdateOne {
  493. mutation := newWxMutation(c.config, OpUpdateOne, withWx(w))
  494. return &WxUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
  495. }
  496. // UpdateOneID returns an update builder for the given id.
  497. func (c *WxClient) UpdateOneID(id uint64) *WxUpdateOne {
  498. mutation := newWxMutation(c.config, OpUpdateOne, withWxID(id))
  499. return &WxUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
  500. }
  501. // Delete returns a delete builder for Wx.
  502. func (c *WxClient) Delete() *WxDelete {
  503. mutation := newWxMutation(c.config, OpDelete)
  504. return &WxDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
  505. }
  506. // DeleteOne returns a builder for deleting the given entity.
  507. func (c *WxClient) DeleteOne(w *Wx) *WxDeleteOne {
  508. return c.DeleteOneID(w.ID)
  509. }
  510. // DeleteOneID returns a builder for deleting the given entity by its id.
  511. func (c *WxClient) DeleteOneID(id uint64) *WxDeleteOne {
  512. builder := c.Delete().Where(wx.ID(id))
  513. builder.mutation.id = &id
  514. builder.mutation.op = OpDeleteOne
  515. return &WxDeleteOne{builder}
  516. }
  517. // Query returns a query builder for Wx.
  518. func (c *WxClient) Query() *WxQuery {
  519. return &WxQuery{
  520. config: c.config,
  521. ctx: &QueryContext{Type: TypeWx},
  522. inters: c.Interceptors(),
  523. }
  524. }
  525. // Get returns a Wx entity by its id.
  526. func (c *WxClient) Get(ctx context.Context, id uint64) (*Wx, error) {
  527. return c.Query().Where(wx.ID(id)).Only(ctx)
  528. }
  529. // GetX is like Get, but panics if an error occurs.
  530. func (c *WxClient) GetX(ctx context.Context, id uint64) *Wx {
  531. obj, err := c.Get(ctx, id)
  532. if err != nil {
  533. panic(err)
  534. }
  535. return obj
  536. }
  537. // QueryServer queries the server edge of a Wx.
  538. func (c *WxClient) QueryServer(w *Wx) *ServerQuery {
  539. query := (&ServerClient{config: c.config}).Query()
  540. query.path = func(context.Context) (fromV *sql.Selector, _ error) {
  541. id := w.ID
  542. step := sqlgraph.NewStep(
  543. sqlgraph.From(wx.Table, wx.FieldID, id),
  544. sqlgraph.To(server.Table, server.FieldID),
  545. sqlgraph.Edge(sqlgraph.M2O, true, wx.ServerTable, wx.ServerColumn),
  546. )
  547. fromV = sqlgraph.Neighbors(w.driver.Dialect(), step)
  548. return fromV, nil
  549. }
  550. return query
  551. }
  552. // Hooks returns the client hooks.
  553. func (c *WxClient) Hooks() []Hook {
  554. hooks := c.hooks.Wx
  555. return append(hooks[:len(hooks):len(hooks)], wx.Hooks[:]...)
  556. }
  557. // Interceptors returns the client interceptors.
  558. func (c *WxClient) Interceptors() []Interceptor {
  559. inters := c.inters.Wx
  560. return append(inters[:len(inters):len(inters)], wx.Interceptors[:]...)
  561. }
  562. func (c *WxClient) mutate(ctx context.Context, m *WxMutation) (Value, error) {
  563. switch m.Op() {
  564. case OpCreate:
  565. return (&WxCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
  566. case OpUpdate:
  567. return (&WxUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
  568. case OpUpdateOne:
  569. return (&WxUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
  570. case OpDelete, OpDeleteOne:
  571. return (&WxDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
  572. default:
  573. return nil, fmt.Errorf("ent: unknown Wx mutation op: %q", m.Op())
  574. }
  575. }
  576. // hooks and interceptors per client, for fast access.
  577. type (
  578. hooks struct {
  579. Contact, Server, Wx []ent.Hook
  580. }
  581. inters struct {
  582. Contact, Server, Wx []ent.Interceptor
  583. }
  584. )
  585. // ExecContext allows calling the underlying ExecContext method of the driver if it is supported by it.
  586. // See, database/sql#DB.ExecContext for more information.
  587. func (c *config) ExecContext(ctx context.Context, query string, args ...any) (stdsql.Result, error) {
  588. ex, ok := c.driver.(interface {
  589. ExecContext(context.Context, string, ...any) (stdsql.Result, error)
  590. })
  591. if !ok {
  592. return nil, fmt.Errorf("Driver.ExecContext is not supported")
  593. }
  594. return ex.ExecContext(ctx, query, args...)
  595. }
  596. // QueryContext allows calling the underlying QueryContext method of the driver if it is supported by it.
  597. // See, database/sql#DB.QueryContext for more information.
  598. func (c *config) QueryContext(ctx context.Context, query string, args ...any) (*stdsql.Rows, error) {
  599. q, ok := c.driver.(interface {
  600. QueryContext(context.Context, string, ...any) (*stdsql.Rows, error)
  601. })
  602. if !ok {
  603. return nil, fmt.Errorf("Driver.QueryContext is not supported")
  604. }
  605. return q.QueryContext(ctx, query, args...)
  606. }