at main 16 kB view raw
1// Code generated by ent, DO NOT EDIT. 2 3package ent 4 5import ( 6 "context" 7 "errors" 8 "fmt" 9 "log" 10 "reflect" 11 12 "tangled.sh/seiso.moe/aletheia.directory/ent/migrate" 13 14 "entgo.io/ent" 15 "entgo.io/ent/dialect" 16 "entgo.io/ent/dialect/sql" 17 "tangled.sh/seiso.moe/aletheia.directory/ent/operation" 18 "tangled.sh/seiso.moe/aletheia.directory/ent/syncstatus" 19) 20 21// Client is the client that holds all ent builders. 22type Client struct { 23 config 24 // Schema is the client for creating, migrating and dropping schema. 25 Schema *migrate.Schema 26 // Operation is the client for interacting with the Operation builders. 27 Operation *OperationClient 28 // SyncStatus is the client for interacting with the SyncStatus builders. 29 SyncStatus *SyncStatusClient 30} 31 32// NewClient creates a new client configured with the given options. 33func NewClient(opts ...Option) *Client { 34 client := &Client{config: newConfig(opts...)} 35 client.init() 36 return client 37} 38 39func (c *Client) init() { 40 c.Schema = migrate.NewSchema(c.driver) 41 c.Operation = NewOperationClient(c.config) 42 c.SyncStatus = NewSyncStatusClient(c.config) 43} 44 45type ( 46 // config is the configuration for the client and its builder. 47 config struct { 48 // driver used for executing database requests. 49 driver dialect.Driver 50 // debug enable a debug logging. 51 debug bool 52 // log used for logging on debug mode. 53 log func(...any) 54 // hooks to execute on mutations. 55 hooks *hooks 56 // interceptors to execute on queries. 57 inters *inters 58 } 59 // Option function to configure the client. 60 Option func(*config) 61) 62 63// newConfig creates a new config for the client. 64func newConfig(opts ...Option) config { 65 cfg := config{log: log.Println, hooks: &hooks{}, inters: &inters{}} 66 cfg.options(opts...) 67 return cfg 68} 69 70// options applies the options on the config object. 71func (c *config) options(opts ...Option) { 72 for _, opt := range opts { 73 opt(c) 74 } 75 if c.debug { 76 c.driver = dialect.Debug(c.driver, c.log) 77 } 78} 79 80// Debug enables debug logging on the ent.Driver. 81func Debug() Option { 82 return func(c *config) { 83 c.debug = true 84 } 85} 86 87// Log sets the logging function for debug mode. 88func Log(fn func(...any)) Option { 89 return func(c *config) { 90 c.log = fn 91 } 92} 93 94// Driver configures the client driver. 95func Driver(driver dialect.Driver) Option { 96 return func(c *config) { 97 c.driver = driver 98 } 99} 100 101// Open opens a database/sql.DB specified by the driver name and 102// the data source name, and returns a new client attached to it. 103// Optional parameters can be added for configuring the client. 104func Open(driverName, dataSourceName string, options ...Option) (*Client, error) { 105 switch driverName { 106 case dialect.MySQL, dialect.Postgres, dialect.SQLite: 107 drv, err := sql.Open(driverName, dataSourceName) 108 if err != nil { 109 return nil, err 110 } 111 return NewClient(append(options, Driver(drv))...), nil 112 default: 113 return nil, fmt.Errorf("unsupported driver: %q", driverName) 114 } 115} 116 117// ErrTxStarted is returned when trying to start a new transaction from a transactional client. 118var ErrTxStarted = errors.New("ent: cannot start a transaction within a transaction") 119 120// Tx returns a new transactional client. The provided context 121// is used until the transaction is committed or rolled back. 122func (c *Client) Tx(ctx context.Context) (*Tx, error) { 123 if _, ok := c.driver.(*txDriver); ok { 124 return nil, ErrTxStarted 125 } 126 tx, err := newTx(ctx, c.driver) 127 if err != nil { 128 return nil, fmt.Errorf("ent: starting a transaction: %w", err) 129 } 130 cfg := c.config 131 cfg.driver = tx 132 return &Tx{ 133 ctx: ctx, 134 config: cfg, 135 Operation: NewOperationClient(cfg), 136 SyncStatus: NewSyncStatusClient(cfg), 137 }, nil 138} 139 140// BeginTx returns a transactional client with specified options. 141func (c *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) { 142 if _, ok := c.driver.(*txDriver); ok { 143 return nil, errors.New("ent: cannot start a transaction within a transaction") 144 } 145 tx, err := c.driver.(interface { 146 BeginTx(context.Context, *sql.TxOptions) (dialect.Tx, error) 147 }).BeginTx(ctx, opts) 148 if err != nil { 149 return nil, fmt.Errorf("ent: starting a transaction: %w", err) 150 } 151 cfg := c.config 152 cfg.driver = &txDriver{tx: tx, drv: c.driver} 153 return &Tx{ 154 ctx: ctx, 155 config: cfg, 156 Operation: NewOperationClient(cfg), 157 SyncStatus: NewSyncStatusClient(cfg), 158 }, nil 159} 160 161// Debug returns a new debug-client. It's used to get verbose logging on specific operations. 162// 163// client.Debug(). 164// Operation. 165// Query(). 166// Count(ctx) 167func (c *Client) Debug() *Client { 168 if c.debug { 169 return c 170 } 171 cfg := c.config 172 cfg.driver = dialect.Debug(c.driver, c.log) 173 client := &Client{config: cfg} 174 client.init() 175 return client 176} 177 178// Close closes the database connection and prevents new queries from starting. 179func (c *Client) Close() error { 180 return c.driver.Close() 181} 182 183// Use adds the mutation hooks to all the entity clients. 184// In order to add hooks to a specific client, call: `client.Node.Use(...)`. 185func (c *Client) Use(hooks ...Hook) { 186 c.Operation.Use(hooks...) 187 c.SyncStatus.Use(hooks...) 188} 189 190// Intercept adds the query interceptors to all the entity clients. 191// In order to add interceptors to a specific client, call: `client.Node.Intercept(...)`. 192func (c *Client) Intercept(interceptors ...Interceptor) { 193 c.Operation.Intercept(interceptors...) 194 c.SyncStatus.Intercept(interceptors...) 195} 196 197// Mutate implements the ent.Mutator interface. 198func (c *Client) Mutate(ctx context.Context, m Mutation) (Value, error) { 199 switch m := m.(type) { 200 case *OperationMutation: 201 return c.Operation.mutate(ctx, m) 202 case *SyncStatusMutation: 203 return c.SyncStatus.mutate(ctx, m) 204 default: 205 return nil, fmt.Errorf("ent: unknown mutation type %T", m) 206 } 207} 208 209// OperationClient is a client for the Operation schema. 210type OperationClient struct { 211 config 212} 213 214// NewOperationClient returns a client for the Operation from the given config. 215func NewOperationClient(c config) *OperationClient { 216 return &OperationClient{config: c} 217} 218 219// Use adds a list of mutation hooks to the hooks stack. 220// A call to `Use(f, g, h)` equals to `operation.Hooks(f(g(h())))`. 221func (c *OperationClient) Use(hooks ...Hook) { 222 c.hooks.Operation = append(c.hooks.Operation, hooks...) 223} 224 225// Intercept adds a list of query interceptors to the interceptors stack. 226// A call to `Intercept(f, g, h)` equals to `operation.Intercept(f(g(h())))`. 227func (c *OperationClient) Intercept(interceptors ...Interceptor) { 228 c.inters.Operation = append(c.inters.Operation, interceptors...) 229} 230 231// Create returns a builder for creating a Operation entity. 232func (c *OperationClient) Create() *OperationCreate { 233 mutation := newOperationMutation(c.config, OpCreate) 234 return &OperationCreate{config: c.config, hooks: c.Hooks(), mutation: mutation} 235} 236 237// CreateBulk returns a builder for creating a bulk of Operation entities. 238func (c *OperationClient) CreateBulk(builders ...*OperationCreate) *OperationCreateBulk { 239 return &OperationCreateBulk{config: c.config, builders: builders} 240} 241 242// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates 243// a builder and applies setFunc on it. 244func (c *OperationClient) MapCreateBulk(slice any, setFunc func(*OperationCreate, int)) *OperationCreateBulk { 245 rv := reflect.ValueOf(slice) 246 if rv.Kind() != reflect.Slice { 247 return &OperationCreateBulk{err: fmt.Errorf("calling to OperationClient.MapCreateBulk with wrong type %T, need slice", slice)} 248 } 249 builders := make([]*OperationCreate, rv.Len()) 250 for i := 0; i < rv.Len(); i++ { 251 builders[i] = c.Create() 252 setFunc(builders[i], i) 253 } 254 return &OperationCreateBulk{config: c.config, builders: builders} 255} 256 257// Update returns an update builder for Operation. 258func (c *OperationClient) Update() *OperationUpdate { 259 mutation := newOperationMutation(c.config, OpUpdate) 260 return &OperationUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation} 261} 262 263// UpdateOne returns an update builder for the given entity. 264func (c *OperationClient) UpdateOne(o *Operation) *OperationUpdateOne { 265 mutation := newOperationMutation(c.config, OpUpdateOne, withOperation(o)) 266 return &OperationUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} 267} 268 269// UpdateOneID returns an update builder for the given id. 270func (c *OperationClient) UpdateOneID(id int) *OperationUpdateOne { 271 mutation := newOperationMutation(c.config, OpUpdateOne, withOperationID(id)) 272 return &OperationUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} 273} 274 275// Delete returns a delete builder for Operation. 276func (c *OperationClient) Delete() *OperationDelete { 277 mutation := newOperationMutation(c.config, OpDelete) 278 return &OperationDelete{config: c.config, hooks: c.Hooks(), mutation: mutation} 279} 280 281// DeleteOne returns a builder for deleting the given entity. 282func (c *OperationClient) DeleteOne(o *Operation) *OperationDeleteOne { 283 return c.DeleteOneID(o.ID) 284} 285 286// DeleteOneID returns a builder for deleting the given entity by its id. 287func (c *OperationClient) DeleteOneID(id int) *OperationDeleteOne { 288 builder := c.Delete().Where(operation.ID(id)) 289 builder.mutation.id = &id 290 builder.mutation.op = OpDeleteOne 291 return &OperationDeleteOne{builder} 292} 293 294// Query returns a query builder for Operation. 295func (c *OperationClient) Query() *OperationQuery { 296 return &OperationQuery{ 297 config: c.config, 298 ctx: &QueryContext{Type: TypeOperation}, 299 inters: c.Interceptors(), 300 } 301} 302 303// Get returns a Operation entity by its id. 304func (c *OperationClient) Get(ctx context.Context, id int) (*Operation, error) { 305 return c.Query().Where(operation.ID(id)).Only(ctx) 306} 307 308// GetX is like Get, but panics if an error occurs. 309func (c *OperationClient) GetX(ctx context.Context, id int) *Operation { 310 obj, err := c.Get(ctx, id) 311 if err != nil { 312 panic(err) 313 } 314 return obj 315} 316 317// Hooks returns the client hooks. 318func (c *OperationClient) Hooks() []Hook { 319 return c.hooks.Operation 320} 321 322// Interceptors returns the client interceptors. 323func (c *OperationClient) Interceptors() []Interceptor { 324 return c.inters.Operation 325} 326 327func (c *OperationClient) mutate(ctx context.Context, m *OperationMutation) (Value, error) { 328 switch m.Op() { 329 case OpCreate: 330 return (&OperationCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) 331 case OpUpdate: 332 return (&OperationUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) 333 case OpUpdateOne: 334 return (&OperationUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) 335 case OpDelete, OpDeleteOne: 336 return (&OperationDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx) 337 default: 338 return nil, fmt.Errorf("ent: unknown Operation mutation op: %q", m.Op()) 339 } 340} 341 342// SyncStatusClient is a client for the SyncStatus schema. 343type SyncStatusClient struct { 344 config 345} 346 347// NewSyncStatusClient returns a client for the SyncStatus from the given config. 348func NewSyncStatusClient(c config) *SyncStatusClient { 349 return &SyncStatusClient{config: c} 350} 351 352// Use adds a list of mutation hooks to the hooks stack. 353// A call to `Use(f, g, h)` equals to `syncstatus.Hooks(f(g(h())))`. 354func (c *SyncStatusClient) Use(hooks ...Hook) { 355 c.hooks.SyncStatus = append(c.hooks.SyncStatus, hooks...) 356} 357 358// Intercept adds a list of query interceptors to the interceptors stack. 359// A call to `Intercept(f, g, h)` equals to `syncstatus.Intercept(f(g(h())))`. 360func (c *SyncStatusClient) Intercept(interceptors ...Interceptor) { 361 c.inters.SyncStatus = append(c.inters.SyncStatus, interceptors...) 362} 363 364// Create returns a builder for creating a SyncStatus entity. 365func (c *SyncStatusClient) Create() *SyncStatusCreate { 366 mutation := newSyncStatusMutation(c.config, OpCreate) 367 return &SyncStatusCreate{config: c.config, hooks: c.Hooks(), mutation: mutation} 368} 369 370// CreateBulk returns a builder for creating a bulk of SyncStatus entities. 371func (c *SyncStatusClient) CreateBulk(builders ...*SyncStatusCreate) *SyncStatusCreateBulk { 372 return &SyncStatusCreateBulk{config: c.config, builders: builders} 373} 374 375// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates 376// a builder and applies setFunc on it. 377func (c *SyncStatusClient) MapCreateBulk(slice any, setFunc func(*SyncStatusCreate, int)) *SyncStatusCreateBulk { 378 rv := reflect.ValueOf(slice) 379 if rv.Kind() != reflect.Slice { 380 return &SyncStatusCreateBulk{err: fmt.Errorf("calling to SyncStatusClient.MapCreateBulk with wrong type %T, need slice", slice)} 381 } 382 builders := make([]*SyncStatusCreate, rv.Len()) 383 for i := 0; i < rv.Len(); i++ { 384 builders[i] = c.Create() 385 setFunc(builders[i], i) 386 } 387 return &SyncStatusCreateBulk{config: c.config, builders: builders} 388} 389 390// Update returns an update builder for SyncStatus. 391func (c *SyncStatusClient) Update() *SyncStatusUpdate { 392 mutation := newSyncStatusMutation(c.config, OpUpdate) 393 return &SyncStatusUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation} 394} 395 396// UpdateOne returns an update builder for the given entity. 397func (c *SyncStatusClient) UpdateOne(ss *SyncStatus) *SyncStatusUpdateOne { 398 mutation := newSyncStatusMutation(c.config, OpUpdateOne, withSyncStatus(ss)) 399 return &SyncStatusUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} 400} 401 402// UpdateOneID returns an update builder for the given id. 403func (c *SyncStatusClient) UpdateOneID(id int) *SyncStatusUpdateOne { 404 mutation := newSyncStatusMutation(c.config, OpUpdateOne, withSyncStatusID(id)) 405 return &SyncStatusUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} 406} 407 408// Delete returns a delete builder for SyncStatus. 409func (c *SyncStatusClient) Delete() *SyncStatusDelete { 410 mutation := newSyncStatusMutation(c.config, OpDelete) 411 return &SyncStatusDelete{config: c.config, hooks: c.Hooks(), mutation: mutation} 412} 413 414// DeleteOne returns a builder for deleting the given entity. 415func (c *SyncStatusClient) DeleteOne(ss *SyncStatus) *SyncStatusDeleteOne { 416 return c.DeleteOneID(ss.ID) 417} 418 419// DeleteOneID returns a builder for deleting the given entity by its id. 420func (c *SyncStatusClient) DeleteOneID(id int) *SyncStatusDeleteOne { 421 builder := c.Delete().Where(syncstatus.ID(id)) 422 builder.mutation.id = &id 423 builder.mutation.op = OpDeleteOne 424 return &SyncStatusDeleteOne{builder} 425} 426 427// Query returns a query builder for SyncStatus. 428func (c *SyncStatusClient) Query() *SyncStatusQuery { 429 return &SyncStatusQuery{ 430 config: c.config, 431 ctx: &QueryContext{Type: TypeSyncStatus}, 432 inters: c.Interceptors(), 433 } 434} 435 436// Get returns a SyncStatus entity by its id. 437func (c *SyncStatusClient) Get(ctx context.Context, id int) (*SyncStatus, error) { 438 return c.Query().Where(syncstatus.ID(id)).Only(ctx) 439} 440 441// GetX is like Get, but panics if an error occurs. 442func (c *SyncStatusClient) GetX(ctx context.Context, id int) *SyncStatus { 443 obj, err := c.Get(ctx, id) 444 if err != nil { 445 panic(err) 446 } 447 return obj 448} 449 450// Hooks returns the client hooks. 451func (c *SyncStatusClient) Hooks() []Hook { 452 return c.hooks.SyncStatus 453} 454 455// Interceptors returns the client interceptors. 456func (c *SyncStatusClient) Interceptors() []Interceptor { 457 return c.inters.SyncStatus 458} 459 460func (c *SyncStatusClient) mutate(ctx context.Context, m *SyncStatusMutation) (Value, error) { 461 switch m.Op() { 462 case OpCreate: 463 return (&SyncStatusCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) 464 case OpUpdate: 465 return (&SyncStatusUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) 466 case OpUpdateOne: 467 return (&SyncStatusUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) 468 case OpDelete, OpDeleteOne: 469 return (&SyncStatusDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx) 470 default: 471 return nil, fmt.Errorf("ent: unknown SyncStatus mutation op: %q", m.Op()) 472 } 473} 474 475// hooks and interceptors per client, for fast access. 476type ( 477 hooks struct { 478 Operation, SyncStatus []ent.Hook 479 } 480 inters struct { 481 Operation, SyncStatus []ent.Interceptor 482 } 483)