-
-
Notifications
You must be signed in to change notification settings - Fork 275
/
client.go
1870 lines (1641 loc) · 65.3 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"log"
"reflect"
uuid "github.com/gofrs/uuid/v5"
"github.com/suyuan32/simple-admin-core/rpc/ent/migrate"
"entgo.io/ent"
"entgo.io/ent/dialect"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"github.com/suyuan32/simple-admin-core/rpc/ent/api"
"github.com/suyuan32/simple-admin-core/rpc/ent/department"
"github.com/suyuan32/simple-admin-core/rpc/ent/dictionary"
"github.com/suyuan32/simple-admin-core/rpc/ent/dictionarydetail"
"github.com/suyuan32/simple-admin-core/rpc/ent/menu"
"github.com/suyuan32/simple-admin-core/rpc/ent/oauthprovider"
"github.com/suyuan32/simple-admin-core/rpc/ent/position"
"github.com/suyuan32/simple-admin-core/rpc/ent/role"
"github.com/suyuan32/simple-admin-core/rpc/ent/token"
"github.com/suyuan32/simple-admin-core/rpc/ent/user"
stdsql "database/sql"
)
// Client is the client that holds all ent builders.
type Client struct {
config
// Schema is the client for creating, migrating and dropping schema.
Schema *migrate.Schema
// API is the client for interacting with the API builders.
API *APIClient
// Department is the client for interacting with the Department builders.
Department *DepartmentClient
// Dictionary is the client for interacting with the Dictionary builders.
Dictionary *DictionaryClient
// DictionaryDetail is the client for interacting with the DictionaryDetail builders.
DictionaryDetail *DictionaryDetailClient
// Menu is the client for interacting with the Menu builders.
Menu *MenuClient
// OauthProvider is the client for interacting with the OauthProvider builders.
OauthProvider *OauthProviderClient
// Position is the client for interacting with the Position builders.
Position *PositionClient
// Role is the client for interacting with the Role builders.
Role *RoleClient
// Token is the client for interacting with the Token builders.
Token *TokenClient
// User is the client for interacting with the User builders.
User *UserClient
}
// NewClient creates a new client configured with the given options.
func NewClient(opts ...Option) *Client {
cfg := config{log: log.Println, hooks: &hooks{}, inters: &inters{}}
cfg.options(opts...)
client := &Client{config: cfg}
client.init()
return client
}
func (c *Client) init() {
c.Schema = migrate.NewSchema(c.driver)
c.API = NewAPIClient(c.config)
c.Department = NewDepartmentClient(c.config)
c.Dictionary = NewDictionaryClient(c.config)
c.DictionaryDetail = NewDictionaryDetailClient(c.config)
c.Menu = NewMenuClient(c.config)
c.OauthProvider = NewOauthProviderClient(c.config)
c.Position = NewPositionClient(c.config)
c.Role = NewRoleClient(c.config)
c.Token = NewTokenClient(c.config)
c.User = NewUserClient(c.config)
}
type (
// config is the configuration for the client and its builder.
config struct {
// driver used for executing database requests.
driver dialect.Driver
// debug enable a debug logging.
debug bool
// log used for logging on debug mode.
log func(...any)
// hooks to execute on mutations.
hooks *hooks
// interceptors to execute on queries.
inters *inters
}
// Option function to configure the client.
Option func(*config)
)
// options applies the options on the config object.
func (c *config) options(opts ...Option) {
for _, opt := range opts {
opt(c)
}
if c.debug {
c.driver = dialect.Debug(c.driver, c.log)
}
}
// Debug enables debug logging on the ent.Driver.
func Debug() Option {
return func(c *config) {
c.debug = true
}
}
// Log sets the logging function for debug mode.
func Log(fn func(...any)) Option {
return func(c *config) {
c.log = fn
}
}
// Driver configures the client driver.
func Driver(driver dialect.Driver) Option {
return func(c *config) {
c.driver = driver
}
}
// Open opens a database/sql.DB specified by the driver name and
// the data source name, and returns a new client attached to it.
// Optional parameters can be added for configuring the client.
func Open(driverName, dataSourceName string, options ...Option) (*Client, error) {
switch driverName {
case dialect.MySQL, dialect.Postgres, dialect.SQLite:
drv, err := sql.Open(driverName, dataSourceName)
if err != nil {
return nil, err
}
return NewClient(append(options, Driver(drv))...), nil
default:
return nil, fmt.Errorf("unsupported driver: %q", driverName)
}
}
// ErrTxStarted is returned when trying to start a new transaction from a transactional client.
var ErrTxStarted = errors.New("ent: cannot start a transaction within a transaction")
// Tx returns a new transactional client. The provided context
// is used until the transaction is committed or rolled back.
func (c *Client) Tx(ctx context.Context) (*Tx, error) {
if _, ok := c.driver.(*txDriver); ok {
return nil, ErrTxStarted
}
tx, err := newTx(ctx, c.driver)
if err != nil {
return nil, fmt.Errorf("ent: starting a transaction: %w", err)
}
cfg := c.config
cfg.driver = tx
return &Tx{
ctx: ctx,
config: cfg,
API: NewAPIClient(cfg),
Department: NewDepartmentClient(cfg),
Dictionary: NewDictionaryClient(cfg),
DictionaryDetail: NewDictionaryDetailClient(cfg),
Menu: NewMenuClient(cfg),
OauthProvider: NewOauthProviderClient(cfg),
Position: NewPositionClient(cfg),
Role: NewRoleClient(cfg),
Token: NewTokenClient(cfg),
User: NewUserClient(cfg),
}, nil
}
// BeginTx returns a transactional client with specified options.
func (c *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
if _, ok := c.driver.(*txDriver); ok {
return nil, errors.New("ent: cannot start a transaction within a transaction")
}
tx, err := c.driver.(interface {
BeginTx(context.Context, *sql.TxOptions) (dialect.Tx, error)
}).BeginTx(ctx, opts)
if err != nil {
return nil, fmt.Errorf("ent: starting a transaction: %w", err)
}
cfg := c.config
cfg.driver = &txDriver{tx: tx, drv: c.driver}
return &Tx{
ctx: ctx,
config: cfg,
API: NewAPIClient(cfg),
Department: NewDepartmentClient(cfg),
Dictionary: NewDictionaryClient(cfg),
DictionaryDetail: NewDictionaryDetailClient(cfg),
Menu: NewMenuClient(cfg),
OauthProvider: NewOauthProviderClient(cfg),
Position: NewPositionClient(cfg),
Role: NewRoleClient(cfg),
Token: NewTokenClient(cfg),
User: NewUserClient(cfg),
}, nil
}
// Debug returns a new debug-client. It's used to get verbose logging on specific operations.
//
// client.Debug().
// API.
// Query().
// Count(ctx)
func (c *Client) Debug() *Client {
if c.debug {
return c
}
cfg := c.config
cfg.driver = dialect.Debug(c.driver, c.log)
client := &Client{config: cfg}
client.init()
return client
}
// Close closes the database connection and prevents new queries from starting.
func (c *Client) Close() error {
return c.driver.Close()
}
// Use adds the mutation hooks to all the entity clients.
// In order to add hooks to a specific client, call: `client.Node.Use(...)`.
func (c *Client) Use(hooks ...Hook) {
for _, n := range []interface{ Use(...Hook) }{
c.API, c.Department, c.Dictionary, c.DictionaryDetail, c.Menu, c.OauthProvider,
c.Position, c.Role, c.Token, c.User,
} {
n.Use(hooks...)
}
}
// Intercept adds the query interceptors to all the entity clients.
// In order to add interceptors to a specific client, call: `client.Node.Intercept(...)`.
func (c *Client) Intercept(interceptors ...Interceptor) {
for _, n := range []interface{ Intercept(...Interceptor) }{
c.API, c.Department, c.Dictionary, c.DictionaryDetail, c.Menu, c.OauthProvider,
c.Position, c.Role, c.Token, c.User,
} {
n.Intercept(interceptors...)
}
}
// Mutate implements the ent.Mutator interface.
func (c *Client) Mutate(ctx context.Context, m Mutation) (Value, error) {
switch m := m.(type) {
case *APIMutation:
return c.API.mutate(ctx, m)
case *DepartmentMutation:
return c.Department.mutate(ctx, m)
case *DictionaryMutation:
return c.Dictionary.mutate(ctx, m)
case *DictionaryDetailMutation:
return c.DictionaryDetail.mutate(ctx, m)
case *MenuMutation:
return c.Menu.mutate(ctx, m)
case *OauthProviderMutation:
return c.OauthProvider.mutate(ctx, m)
case *PositionMutation:
return c.Position.mutate(ctx, m)
case *RoleMutation:
return c.Role.mutate(ctx, m)
case *TokenMutation:
return c.Token.mutate(ctx, m)
case *UserMutation:
return c.User.mutate(ctx, m)
default:
return nil, fmt.Errorf("ent: unknown mutation type %T", m)
}
}
// APIClient is a client for the API schema.
type APIClient struct {
config
}
// NewAPIClient returns a client for the API from the given config.
func NewAPIClient(c config) *APIClient {
return &APIClient{config: c}
}
// Use adds a list of mutation hooks to the hooks stack.
// A call to `Use(f, g, h)` equals to `api.Hooks(f(g(h())))`.
func (c *APIClient) Use(hooks ...Hook) {
c.hooks.API = append(c.hooks.API, hooks...)
}
// Intercept adds a list of query interceptors to the interceptors stack.
// A call to `Intercept(f, g, h)` equals to `api.Intercept(f(g(h())))`.
func (c *APIClient) Intercept(interceptors ...Interceptor) {
c.inters.API = append(c.inters.API, interceptors...)
}
// Create returns a builder for creating a API entity.
func (c *APIClient) Create() *APICreate {
mutation := newAPIMutation(c.config, OpCreate)
return &APICreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// CreateBulk returns a builder for creating a bulk of API entities.
func (c *APIClient) CreateBulk(builders ...*APICreate) *APICreateBulk {
return &APICreateBulk{config: c.config, builders: builders}
}
// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates
// a builder and applies setFunc on it.
func (c *APIClient) MapCreateBulk(slice any, setFunc func(*APICreate, int)) *APICreateBulk {
rv := reflect.ValueOf(slice)
if rv.Kind() != reflect.Slice {
return &APICreateBulk{err: fmt.Errorf("calling to APIClient.MapCreateBulk with wrong type %T, need slice", slice)}
}
builders := make([]*APICreate, rv.Len())
for i := 0; i < rv.Len(); i++ {
builders[i] = c.Create()
setFunc(builders[i], i)
}
return &APICreateBulk{config: c.config, builders: builders}
}
// Update returns an update builder for API.
func (c *APIClient) Update() *APIUpdate {
mutation := newAPIMutation(c.config, OpUpdate)
return &APIUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// UpdateOne returns an update builder for the given entity.
func (c *APIClient) UpdateOne(a *API) *APIUpdateOne {
mutation := newAPIMutation(c.config, OpUpdateOne, withAPI(a))
return &APIUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// UpdateOneID returns an update builder for the given id.
func (c *APIClient) UpdateOneID(id uint64) *APIUpdateOne {
mutation := newAPIMutation(c.config, OpUpdateOne, withAPIID(id))
return &APIUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// Delete returns a delete builder for API.
func (c *APIClient) Delete() *APIDelete {
mutation := newAPIMutation(c.config, OpDelete)
return &APIDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// DeleteOne returns a builder for deleting the given entity.
func (c *APIClient) DeleteOne(a *API) *APIDeleteOne {
return c.DeleteOneID(a.ID)
}
// DeleteOneID returns a builder for deleting the given entity by its id.
func (c *APIClient) DeleteOneID(id uint64) *APIDeleteOne {
builder := c.Delete().Where(api.ID(id))
builder.mutation.id = &id
builder.mutation.op = OpDeleteOne
return &APIDeleteOne{builder}
}
// Query returns a query builder for API.
func (c *APIClient) Query() *APIQuery {
return &APIQuery{
config: c.config,
ctx: &QueryContext{Type: TypeAPI},
inters: c.Interceptors(),
}
}
// Get returns a API entity by its id.
func (c *APIClient) Get(ctx context.Context, id uint64) (*API, error) {
return c.Query().Where(api.ID(id)).Only(ctx)
}
// GetX is like Get, but panics if an error occurs.
func (c *APIClient) GetX(ctx context.Context, id uint64) *API {
obj, err := c.Get(ctx, id)
if err != nil {
panic(err)
}
return obj
}
// Hooks returns the client hooks.
func (c *APIClient) Hooks() []Hook {
return c.hooks.API
}
// Interceptors returns the client interceptors.
func (c *APIClient) Interceptors() []Interceptor {
return c.inters.API
}
func (c *APIClient) mutate(ctx context.Context, m *APIMutation) (Value, error) {
switch m.Op() {
case OpCreate:
return (&APICreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdate:
return (&APIUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdateOne:
return (&APIUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpDelete, OpDeleteOne:
return (&APIDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
default:
return nil, fmt.Errorf("ent: unknown API mutation op: %q", m.Op())
}
}
// DepartmentClient is a client for the Department schema.
type DepartmentClient struct {
config
}
// NewDepartmentClient returns a client for the Department from the given config.
func NewDepartmentClient(c config) *DepartmentClient {
return &DepartmentClient{config: c}
}
// Use adds a list of mutation hooks to the hooks stack.
// A call to `Use(f, g, h)` equals to `department.Hooks(f(g(h())))`.
func (c *DepartmentClient) Use(hooks ...Hook) {
c.hooks.Department = append(c.hooks.Department, hooks...)
}
// Intercept adds a list of query interceptors to the interceptors stack.
// A call to `Intercept(f, g, h)` equals to `department.Intercept(f(g(h())))`.
func (c *DepartmentClient) Intercept(interceptors ...Interceptor) {
c.inters.Department = append(c.inters.Department, interceptors...)
}
// Create returns a builder for creating a Department entity.
func (c *DepartmentClient) Create() *DepartmentCreate {
mutation := newDepartmentMutation(c.config, OpCreate)
return &DepartmentCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// CreateBulk returns a builder for creating a bulk of Department entities.
func (c *DepartmentClient) CreateBulk(builders ...*DepartmentCreate) *DepartmentCreateBulk {
return &DepartmentCreateBulk{config: c.config, builders: builders}
}
// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates
// a builder and applies setFunc on it.
func (c *DepartmentClient) MapCreateBulk(slice any, setFunc func(*DepartmentCreate, int)) *DepartmentCreateBulk {
rv := reflect.ValueOf(slice)
if rv.Kind() != reflect.Slice {
return &DepartmentCreateBulk{err: fmt.Errorf("calling to DepartmentClient.MapCreateBulk with wrong type %T, need slice", slice)}
}
builders := make([]*DepartmentCreate, rv.Len())
for i := 0; i < rv.Len(); i++ {
builders[i] = c.Create()
setFunc(builders[i], i)
}
return &DepartmentCreateBulk{config: c.config, builders: builders}
}
// Update returns an update builder for Department.
func (c *DepartmentClient) Update() *DepartmentUpdate {
mutation := newDepartmentMutation(c.config, OpUpdate)
return &DepartmentUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// UpdateOne returns an update builder for the given entity.
func (c *DepartmentClient) UpdateOne(d *Department) *DepartmentUpdateOne {
mutation := newDepartmentMutation(c.config, OpUpdateOne, withDepartment(d))
return &DepartmentUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// UpdateOneID returns an update builder for the given id.
func (c *DepartmentClient) UpdateOneID(id uint64) *DepartmentUpdateOne {
mutation := newDepartmentMutation(c.config, OpUpdateOne, withDepartmentID(id))
return &DepartmentUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// Delete returns a delete builder for Department.
func (c *DepartmentClient) Delete() *DepartmentDelete {
mutation := newDepartmentMutation(c.config, OpDelete)
return &DepartmentDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// DeleteOne returns a builder for deleting the given entity.
func (c *DepartmentClient) DeleteOne(d *Department) *DepartmentDeleteOne {
return c.DeleteOneID(d.ID)
}
// DeleteOneID returns a builder for deleting the given entity by its id.
func (c *DepartmentClient) DeleteOneID(id uint64) *DepartmentDeleteOne {
builder := c.Delete().Where(department.ID(id))
builder.mutation.id = &id
builder.mutation.op = OpDeleteOne
return &DepartmentDeleteOne{builder}
}
// Query returns a query builder for Department.
func (c *DepartmentClient) Query() *DepartmentQuery {
return &DepartmentQuery{
config: c.config,
ctx: &QueryContext{Type: TypeDepartment},
inters: c.Interceptors(),
}
}
// Get returns a Department entity by its id.
func (c *DepartmentClient) Get(ctx context.Context, id uint64) (*Department, error) {
return c.Query().Where(department.ID(id)).Only(ctx)
}
// GetX is like Get, but panics if an error occurs.
func (c *DepartmentClient) GetX(ctx context.Context, id uint64) *Department {
obj, err := c.Get(ctx, id)
if err != nil {
panic(err)
}
return obj
}
// QueryParent queries the parent edge of a Department.
func (c *DepartmentClient) QueryParent(d *Department) *DepartmentQuery {
query := (&DepartmentClient{config: c.config}).Query()
query.path = func(context.Context) (fromV *sql.Selector, _ error) {
id := d.ID
step := sqlgraph.NewStep(
sqlgraph.From(department.Table, department.FieldID, id),
sqlgraph.To(department.Table, department.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, department.ParentTable, department.ParentColumn),
)
fromV = sqlgraph.Neighbors(d.driver.Dialect(), step)
return fromV, nil
}
return query
}
// QueryChildren queries the children edge of a Department.
func (c *DepartmentClient) QueryChildren(d *Department) *DepartmentQuery {
query := (&DepartmentClient{config: c.config}).Query()
query.path = func(context.Context) (fromV *sql.Selector, _ error) {
id := d.ID
step := sqlgraph.NewStep(
sqlgraph.From(department.Table, department.FieldID, id),
sqlgraph.To(department.Table, department.FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, department.ChildrenTable, department.ChildrenColumn),
)
fromV = sqlgraph.Neighbors(d.driver.Dialect(), step)
return fromV, nil
}
return query
}
// QueryUsers queries the users edge of a Department.
func (c *DepartmentClient) QueryUsers(d *Department) *UserQuery {
query := (&UserClient{config: c.config}).Query()
query.path = func(context.Context) (fromV *sql.Selector, _ error) {
id := d.ID
step := sqlgraph.NewStep(
sqlgraph.From(department.Table, department.FieldID, id),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.O2M, true, department.UsersTable, department.UsersColumn),
)
fromV = sqlgraph.Neighbors(d.driver.Dialect(), step)
return fromV, nil
}
return query
}
// Hooks returns the client hooks.
func (c *DepartmentClient) Hooks() []Hook {
return c.hooks.Department
}
// Interceptors returns the client interceptors.
func (c *DepartmentClient) Interceptors() []Interceptor {
return c.inters.Department
}
func (c *DepartmentClient) mutate(ctx context.Context, m *DepartmentMutation) (Value, error) {
switch m.Op() {
case OpCreate:
return (&DepartmentCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdate:
return (&DepartmentUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdateOne:
return (&DepartmentUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpDelete, OpDeleteOne:
return (&DepartmentDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
default:
return nil, fmt.Errorf("ent: unknown Department mutation op: %q", m.Op())
}
}
// DictionaryClient is a client for the Dictionary schema.
type DictionaryClient struct {
config
}
// NewDictionaryClient returns a client for the Dictionary from the given config.
func NewDictionaryClient(c config) *DictionaryClient {
return &DictionaryClient{config: c}
}
// Use adds a list of mutation hooks to the hooks stack.
// A call to `Use(f, g, h)` equals to `dictionary.Hooks(f(g(h())))`.
func (c *DictionaryClient) Use(hooks ...Hook) {
c.hooks.Dictionary = append(c.hooks.Dictionary, hooks...)
}
// Intercept adds a list of query interceptors to the interceptors stack.
// A call to `Intercept(f, g, h)` equals to `dictionary.Intercept(f(g(h())))`.
func (c *DictionaryClient) Intercept(interceptors ...Interceptor) {
c.inters.Dictionary = append(c.inters.Dictionary, interceptors...)
}
// Create returns a builder for creating a Dictionary entity.
func (c *DictionaryClient) Create() *DictionaryCreate {
mutation := newDictionaryMutation(c.config, OpCreate)
return &DictionaryCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// CreateBulk returns a builder for creating a bulk of Dictionary entities.
func (c *DictionaryClient) CreateBulk(builders ...*DictionaryCreate) *DictionaryCreateBulk {
return &DictionaryCreateBulk{config: c.config, builders: builders}
}
// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates
// a builder and applies setFunc on it.
func (c *DictionaryClient) MapCreateBulk(slice any, setFunc func(*DictionaryCreate, int)) *DictionaryCreateBulk {
rv := reflect.ValueOf(slice)
if rv.Kind() != reflect.Slice {
return &DictionaryCreateBulk{err: fmt.Errorf("calling to DictionaryClient.MapCreateBulk with wrong type %T, need slice", slice)}
}
builders := make([]*DictionaryCreate, rv.Len())
for i := 0; i < rv.Len(); i++ {
builders[i] = c.Create()
setFunc(builders[i], i)
}
return &DictionaryCreateBulk{config: c.config, builders: builders}
}
// Update returns an update builder for Dictionary.
func (c *DictionaryClient) Update() *DictionaryUpdate {
mutation := newDictionaryMutation(c.config, OpUpdate)
return &DictionaryUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// UpdateOne returns an update builder for the given entity.
func (c *DictionaryClient) UpdateOne(d *Dictionary) *DictionaryUpdateOne {
mutation := newDictionaryMutation(c.config, OpUpdateOne, withDictionary(d))
return &DictionaryUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// UpdateOneID returns an update builder for the given id.
func (c *DictionaryClient) UpdateOneID(id uint64) *DictionaryUpdateOne {
mutation := newDictionaryMutation(c.config, OpUpdateOne, withDictionaryID(id))
return &DictionaryUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// Delete returns a delete builder for Dictionary.
func (c *DictionaryClient) Delete() *DictionaryDelete {
mutation := newDictionaryMutation(c.config, OpDelete)
return &DictionaryDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// DeleteOne returns a builder for deleting the given entity.
func (c *DictionaryClient) DeleteOne(d *Dictionary) *DictionaryDeleteOne {
return c.DeleteOneID(d.ID)
}
// DeleteOneID returns a builder for deleting the given entity by its id.
func (c *DictionaryClient) DeleteOneID(id uint64) *DictionaryDeleteOne {
builder := c.Delete().Where(dictionary.ID(id))
builder.mutation.id = &id
builder.mutation.op = OpDeleteOne
return &DictionaryDeleteOne{builder}
}
// Query returns a query builder for Dictionary.
func (c *DictionaryClient) Query() *DictionaryQuery {
return &DictionaryQuery{
config: c.config,
ctx: &QueryContext{Type: TypeDictionary},
inters: c.Interceptors(),
}
}
// Get returns a Dictionary entity by its id.
func (c *DictionaryClient) Get(ctx context.Context, id uint64) (*Dictionary, error) {
return c.Query().Where(dictionary.ID(id)).Only(ctx)
}
// GetX is like Get, but panics if an error occurs.
func (c *DictionaryClient) GetX(ctx context.Context, id uint64) *Dictionary {
obj, err := c.Get(ctx, id)
if err != nil {
panic(err)
}
return obj
}
// QueryDictionaryDetails queries the dictionary_details edge of a Dictionary.
func (c *DictionaryClient) QueryDictionaryDetails(d *Dictionary) *DictionaryDetailQuery {
query := (&DictionaryDetailClient{config: c.config}).Query()
query.path = func(context.Context) (fromV *sql.Selector, _ error) {
id := d.ID
step := sqlgraph.NewStep(
sqlgraph.From(dictionary.Table, dictionary.FieldID, id),
sqlgraph.To(dictionarydetail.Table, dictionarydetail.FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, dictionary.DictionaryDetailsTable, dictionary.DictionaryDetailsColumn),
)
fromV = sqlgraph.Neighbors(d.driver.Dialect(), step)
return fromV, nil
}
return query
}
// Hooks returns the client hooks.
func (c *DictionaryClient) Hooks() []Hook {
return c.hooks.Dictionary
}
// Interceptors returns the client interceptors.
func (c *DictionaryClient) Interceptors() []Interceptor {
return c.inters.Dictionary
}
func (c *DictionaryClient) mutate(ctx context.Context, m *DictionaryMutation) (Value, error) {
switch m.Op() {
case OpCreate:
return (&DictionaryCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdate:
return (&DictionaryUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdateOne:
return (&DictionaryUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpDelete, OpDeleteOne:
return (&DictionaryDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
default:
return nil, fmt.Errorf("ent: unknown Dictionary mutation op: %q", m.Op())
}
}
// DictionaryDetailClient is a client for the DictionaryDetail schema.
type DictionaryDetailClient struct {
config
}
// NewDictionaryDetailClient returns a client for the DictionaryDetail from the given config.
func NewDictionaryDetailClient(c config) *DictionaryDetailClient {
return &DictionaryDetailClient{config: c}
}
// Use adds a list of mutation hooks to the hooks stack.
// A call to `Use(f, g, h)` equals to `dictionarydetail.Hooks(f(g(h())))`.
func (c *DictionaryDetailClient) Use(hooks ...Hook) {
c.hooks.DictionaryDetail = append(c.hooks.DictionaryDetail, hooks...)
}
// Intercept adds a list of query interceptors to the interceptors stack.
// A call to `Intercept(f, g, h)` equals to `dictionarydetail.Intercept(f(g(h())))`.
func (c *DictionaryDetailClient) Intercept(interceptors ...Interceptor) {
c.inters.DictionaryDetail = append(c.inters.DictionaryDetail, interceptors...)
}
// Create returns a builder for creating a DictionaryDetail entity.
func (c *DictionaryDetailClient) Create() *DictionaryDetailCreate {
mutation := newDictionaryDetailMutation(c.config, OpCreate)
return &DictionaryDetailCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// CreateBulk returns a builder for creating a bulk of DictionaryDetail entities.
func (c *DictionaryDetailClient) CreateBulk(builders ...*DictionaryDetailCreate) *DictionaryDetailCreateBulk {
return &DictionaryDetailCreateBulk{config: c.config, builders: builders}
}
// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates
// a builder and applies setFunc on it.
func (c *DictionaryDetailClient) MapCreateBulk(slice any, setFunc func(*DictionaryDetailCreate, int)) *DictionaryDetailCreateBulk {
rv := reflect.ValueOf(slice)
if rv.Kind() != reflect.Slice {
return &DictionaryDetailCreateBulk{err: fmt.Errorf("calling to DictionaryDetailClient.MapCreateBulk with wrong type %T, need slice", slice)}
}
builders := make([]*DictionaryDetailCreate, rv.Len())
for i := 0; i < rv.Len(); i++ {
builders[i] = c.Create()
setFunc(builders[i], i)
}
return &DictionaryDetailCreateBulk{config: c.config, builders: builders}
}
// Update returns an update builder for DictionaryDetail.
func (c *DictionaryDetailClient) Update() *DictionaryDetailUpdate {
mutation := newDictionaryDetailMutation(c.config, OpUpdate)
return &DictionaryDetailUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// UpdateOne returns an update builder for the given entity.
func (c *DictionaryDetailClient) UpdateOne(dd *DictionaryDetail) *DictionaryDetailUpdateOne {
mutation := newDictionaryDetailMutation(c.config, OpUpdateOne, withDictionaryDetail(dd))
return &DictionaryDetailUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// UpdateOneID returns an update builder for the given id.
func (c *DictionaryDetailClient) UpdateOneID(id uint64) *DictionaryDetailUpdateOne {
mutation := newDictionaryDetailMutation(c.config, OpUpdateOne, withDictionaryDetailID(id))
return &DictionaryDetailUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// Delete returns a delete builder for DictionaryDetail.
func (c *DictionaryDetailClient) Delete() *DictionaryDetailDelete {
mutation := newDictionaryDetailMutation(c.config, OpDelete)
return &DictionaryDetailDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// DeleteOne returns a builder for deleting the given entity.
func (c *DictionaryDetailClient) DeleteOne(dd *DictionaryDetail) *DictionaryDetailDeleteOne {
return c.DeleteOneID(dd.ID)
}
// DeleteOneID returns a builder for deleting the given entity by its id.
func (c *DictionaryDetailClient) DeleteOneID(id uint64) *DictionaryDetailDeleteOne {
builder := c.Delete().Where(dictionarydetail.ID(id))
builder.mutation.id = &id
builder.mutation.op = OpDeleteOne
return &DictionaryDetailDeleteOne{builder}
}
// Query returns a query builder for DictionaryDetail.
func (c *DictionaryDetailClient) Query() *DictionaryDetailQuery {
return &DictionaryDetailQuery{
config: c.config,
ctx: &QueryContext{Type: TypeDictionaryDetail},
inters: c.Interceptors(),
}
}
// Get returns a DictionaryDetail entity by its id.
func (c *DictionaryDetailClient) Get(ctx context.Context, id uint64) (*DictionaryDetail, error) {
return c.Query().Where(dictionarydetail.ID(id)).Only(ctx)
}
// GetX is like Get, but panics if an error occurs.
func (c *DictionaryDetailClient) GetX(ctx context.Context, id uint64) *DictionaryDetail {
obj, err := c.Get(ctx, id)
if err != nil {
panic(err)
}
return obj
}
// QueryDictionaries queries the dictionaries edge of a DictionaryDetail.
func (c *DictionaryDetailClient) QueryDictionaries(dd *DictionaryDetail) *DictionaryQuery {
query := (&DictionaryClient{config: c.config}).Query()
query.path = func(context.Context) (fromV *sql.Selector, _ error) {
id := dd.ID
step := sqlgraph.NewStep(
sqlgraph.From(dictionarydetail.Table, dictionarydetail.FieldID, id),
sqlgraph.To(dictionary.Table, dictionary.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, dictionarydetail.DictionariesTable, dictionarydetail.DictionariesColumn),
)
fromV = sqlgraph.Neighbors(dd.driver.Dialect(), step)
return fromV, nil
}
return query
}
// Hooks returns the client hooks.
func (c *DictionaryDetailClient) Hooks() []Hook {
return c.hooks.DictionaryDetail
}
// Interceptors returns the client interceptors.
func (c *DictionaryDetailClient) Interceptors() []Interceptor {
return c.inters.DictionaryDetail
}
func (c *DictionaryDetailClient) mutate(ctx context.Context, m *DictionaryDetailMutation) (Value, error) {
switch m.Op() {
case OpCreate:
return (&DictionaryDetailCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdate:
return (&DictionaryDetailUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdateOne:
return (&DictionaryDetailUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpDelete, OpDeleteOne:
return (&DictionaryDetailDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
default:
return nil, fmt.Errorf("ent: unknown DictionaryDetail mutation op: %q", m.Op())
}
}
// MenuClient is a client for the Menu schema.
type MenuClient struct {
config
}
// NewMenuClient returns a client for the Menu from the given config.
func NewMenuClient(c config) *MenuClient {
return &MenuClient{config: c}
}
// Use adds a list of mutation hooks to the hooks stack.
// A call to `Use(f, g, h)` equals to `menu.Hooks(f(g(h())))`.
func (c *MenuClient) Use(hooks ...Hook) {
c.hooks.Menu = append(c.hooks.Menu, hooks...)
}
// Intercept adds a list of query interceptors to the interceptors stack.
// A call to `Intercept(f, g, h)` equals to `menu.Intercept(f(g(h())))`.
func (c *MenuClient) Intercept(interceptors ...Interceptor) {
c.inters.Menu = append(c.inters.Menu, interceptors...)
}
// Create returns a builder for creating a Menu entity.
func (c *MenuClient) Create() *MenuCreate {
mutation := newMenuMutation(c.config, OpCreate)
return &MenuCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// CreateBulk returns a builder for creating a bulk of Menu entities.
func (c *MenuClient) CreateBulk(builders ...*MenuCreate) *MenuCreateBulk {
return &MenuCreateBulk{config: c.config, builders: builders}
}
// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates
// a builder and applies setFunc on it.
func (c *MenuClient) MapCreateBulk(slice any, setFunc func(*MenuCreate, int)) *MenuCreateBulk {
rv := reflect.ValueOf(slice)
if rv.Kind() != reflect.Slice {
return &MenuCreateBulk{err: fmt.Errorf("calling to MenuClient.MapCreateBulk with wrong type %T, need slice", slice)}
}
builders := make([]*MenuCreate, rv.Len())
for i := 0; i < rv.Len(); i++ {
builders[i] = c.Create()
setFunc(builders[i], i)
}
return &MenuCreateBulk{config: c.config, builders: builders}
}
// Update returns an update builder for Menu.
func (c *MenuClient) Update() *MenuUpdate {
mutation := newMenuMutation(c.config, OpUpdate)
return &MenuUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// UpdateOne returns an update builder for the given entity.
func (c *MenuClient) UpdateOne(m *Menu) *MenuUpdateOne {
mutation := newMenuMutation(c.config, OpUpdateOne, withMenu(m))
return &MenuUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// UpdateOneID returns an update builder for the given id.
func (c *MenuClient) UpdateOneID(id uint64) *MenuUpdateOne {
mutation := newMenuMutation(c.config, OpUpdateOne, withMenuID(id))
return &MenuUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// Delete returns a delete builder for Menu.
func (c *MenuClient) Delete() *MenuDelete {
mutation := newMenuMutation(c.config, OpDelete)
return &MenuDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// DeleteOne returns a builder for deleting the given entity.
func (c *MenuClient) DeleteOne(m *Menu) *MenuDeleteOne {
return c.DeleteOneID(m.ID)
}
// DeleteOneID returns a builder for deleting the given entity by its id.
func (c *MenuClient) DeleteOneID(id uint64) *MenuDeleteOne {
builder := c.Delete().Where(menu.ID(id))
builder.mutation.id = &id
builder.mutation.op = OpDeleteOne
return &MenuDeleteOne{builder}
}
// Query returns a query builder for Menu.
func (c *MenuClient) Query() *MenuQuery {
return &MenuQuery{
config: c.config,
ctx: &QueryContext{Type: TypeMenu},
inters: c.Interceptors(),
}
}
// Get returns a Menu entity by its id.
func (c *MenuClient) Get(ctx context.Context, id uint64) (*Menu, error) {
return c.Query().Where(menu.ID(id)).Only(ctx)
}
// GetX is like Get, but panics if an error occurs.
func (c *MenuClient) GetX(ctx context.Context, id uint64) *Menu {
obj, err := c.Get(ctx, id)
if err != nil {
panic(err)
}
return obj
}
// QueryRoles queries the roles edge of a Menu.