mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
311 lines
9.5 KiB
Go
311 lines
9.5 KiB
Go
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
|
// This source code is licensed under the Apache 2.0 license found
|
|
// in the LICENSE file in the root directory of this source tree.
|
|
|
|
// Code generated by entc, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/facebookincubator/ent/entc/integration/privacy/ent/migrate"
|
|
|
|
"github.com/facebookincubator/ent/entc/integration/privacy/ent/galaxy"
|
|
"github.com/facebookincubator/ent/entc/integration/privacy/ent/planet"
|
|
|
|
"github.com/facebookincubator/ent/dialect"
|
|
"github.com/facebookincubator/ent/dialect/sql"
|
|
"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
|
|
)
|
|
|
|
// 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
|
|
// Galaxy is the client for interacting with the Galaxy builders.
|
|
Galaxy *GalaxyClient
|
|
// Planet is the client for interacting with the Planet builders.
|
|
Planet *PlanetClient
|
|
}
|
|
|
|
// NewClient creates a new client configured with the given options.
|
|
func NewClient(opts ...Option) *Client {
|
|
cfg := config{log: log.Println, hooks: &hooks{}}
|
|
cfg.options(opts...)
|
|
client := &Client{config: cfg}
|
|
client.init()
|
|
return client
|
|
}
|
|
|
|
func (c *Client) init() {
|
|
c.Schema = migrate.NewSchema(c.driver)
|
|
c.Galaxy = NewGalaxyClient(c.config)
|
|
c.Planet = NewPlanetClient(c.config)
|
|
}
|
|
|
|
// Open opens a connection to the database specified by the driver name and a
|
|
// driver-specific 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)
|
|
}
|
|
}
|
|
|
|
// Tx returns a new transactional client.
|
|
func (c *Client) Tx(ctx context.Context) (*Tx, error) {
|
|
if _, ok := c.driver.(*txDriver); ok {
|
|
return nil, fmt.Errorf("ent: cannot start a transaction within a transaction")
|
|
}
|
|
tx, err := newTx(ctx, c.driver)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ent: starting a transaction: %v", err)
|
|
}
|
|
cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks}
|
|
return &Tx{
|
|
config: cfg,
|
|
Galaxy: NewGalaxyClient(cfg),
|
|
Planet: NewPlanetClient(cfg),
|
|
}, nil
|
|
}
|
|
|
|
// Debug returns a new debug-client. It's used to get verbose logging on specific operations.
|
|
//
|
|
// client.Debug().
|
|
// Galaxy.
|
|
// Query().
|
|
// Count(ctx)
|
|
//
|
|
func (c *Client) Debug() *Client {
|
|
if c.debug {
|
|
return c
|
|
}
|
|
cfg := config{driver: dialect.Debug(c.driver, c.log), log: c.log, debug: true, hooks: c.hooks}
|
|
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) {
|
|
c.Galaxy.Use(hooks...)
|
|
c.Planet.Use(hooks...)
|
|
}
|
|
|
|
// GalaxyClient is a client for the Galaxy schema.
|
|
type GalaxyClient struct {
|
|
config
|
|
}
|
|
|
|
// NewGalaxyClient returns a client for the Galaxy from the given config.
|
|
func NewGalaxyClient(c config) *GalaxyClient {
|
|
return &GalaxyClient{config: c}
|
|
}
|
|
|
|
// Use adds a list of mutation hooks to the hooks stack.
|
|
// A call to `Use(f, g, h)` equals to `galaxy.Hooks(f(g(h())))`.
|
|
func (c *GalaxyClient) Use(hooks ...Hook) {
|
|
c.hooks.Galaxy = append(c.hooks.Galaxy, hooks...)
|
|
}
|
|
|
|
// Create returns a create builder for Galaxy.
|
|
func (c *GalaxyClient) Create() *GalaxyCreate {
|
|
mutation := newGalaxyMutation(c.config, OpCreate)
|
|
return &GalaxyCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
|
}
|
|
|
|
// Update returns an update builder for Galaxy.
|
|
func (c *GalaxyClient) Update() *GalaxyUpdate {
|
|
mutation := newGalaxyMutation(c.config, OpUpdate)
|
|
return &GalaxyUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
|
}
|
|
|
|
// UpdateOne returns an update builder for the given entity.
|
|
func (c *GalaxyClient) UpdateOne(ga *Galaxy) *GalaxyUpdateOne {
|
|
return c.UpdateOneID(ga.ID)
|
|
}
|
|
|
|
// UpdateOneID returns an update builder for the given id.
|
|
func (c *GalaxyClient) UpdateOneID(id int) *GalaxyUpdateOne {
|
|
mutation := newGalaxyMutation(c.config, OpUpdateOne)
|
|
mutation.id = &id
|
|
return &GalaxyUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
|
}
|
|
|
|
// Delete returns a delete builder for Galaxy.
|
|
func (c *GalaxyClient) Delete() *GalaxyDelete {
|
|
mutation := newGalaxyMutation(c.config, OpDelete)
|
|
return &GalaxyDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
|
}
|
|
|
|
// DeleteOne returns a delete builder for the given entity.
|
|
func (c *GalaxyClient) DeleteOne(ga *Galaxy) *GalaxyDeleteOne {
|
|
return c.DeleteOneID(ga.ID)
|
|
}
|
|
|
|
// DeleteOneID returns a delete builder for the given id.
|
|
func (c *GalaxyClient) DeleteOneID(id int) *GalaxyDeleteOne {
|
|
builder := c.Delete().Where(galaxy.ID(id))
|
|
builder.mutation.id = &id
|
|
builder.mutation.op = OpDeleteOne
|
|
return &GalaxyDeleteOne{builder}
|
|
}
|
|
|
|
// Create returns a query builder for Galaxy.
|
|
func (c *GalaxyClient) Query() *GalaxyQuery {
|
|
return &GalaxyQuery{config: c.config}
|
|
}
|
|
|
|
// Get returns a Galaxy entity by its id.
|
|
func (c *GalaxyClient) Get(ctx context.Context, id int) (*Galaxy, error) {
|
|
return c.Query().Where(galaxy.ID(id)).Only(ctx)
|
|
}
|
|
|
|
// GetX is like Get, but panics if an error occurs.
|
|
func (c *GalaxyClient) GetX(ctx context.Context, id int) *Galaxy {
|
|
ga, err := c.Get(ctx, id)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return ga
|
|
}
|
|
|
|
// QueryPlanets queries the planets edge of a Galaxy.
|
|
func (c *GalaxyClient) QueryPlanets(ga *Galaxy) *PlanetQuery {
|
|
query := &PlanetQuery{config: c.config}
|
|
query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) {
|
|
id := ga.ID
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(galaxy.Table, galaxy.FieldID, id),
|
|
sqlgraph.To(planet.Table, planet.FieldID),
|
|
sqlgraph.Edge(sqlgraph.O2M, false, galaxy.PlanetsTable, galaxy.PlanetsColumn),
|
|
)
|
|
fromV = sqlgraph.Neighbors(ga.driver.Dialect(), step)
|
|
return fromV, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// Hooks returns the client hooks.
|
|
func (c *GalaxyClient) Hooks() []Hook {
|
|
hooks := c.hooks.Galaxy
|
|
return append(hooks[:len(hooks):len(hooks)], galaxy.Hooks[:]...)
|
|
}
|
|
|
|
// PlanetClient is a client for the Planet schema.
|
|
type PlanetClient struct {
|
|
config
|
|
}
|
|
|
|
// NewPlanetClient returns a client for the Planet from the given config.
|
|
func NewPlanetClient(c config) *PlanetClient {
|
|
return &PlanetClient{config: c}
|
|
}
|
|
|
|
// Use adds a list of mutation hooks to the hooks stack.
|
|
// A call to `Use(f, g, h)` equals to `planet.Hooks(f(g(h())))`.
|
|
func (c *PlanetClient) Use(hooks ...Hook) {
|
|
c.hooks.Planet = append(c.hooks.Planet, hooks...)
|
|
}
|
|
|
|
// Create returns a create builder for Planet.
|
|
func (c *PlanetClient) Create() *PlanetCreate {
|
|
mutation := newPlanetMutation(c.config, OpCreate)
|
|
return &PlanetCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
|
}
|
|
|
|
// Update returns an update builder for Planet.
|
|
func (c *PlanetClient) Update() *PlanetUpdate {
|
|
mutation := newPlanetMutation(c.config, OpUpdate)
|
|
return &PlanetUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
|
}
|
|
|
|
// UpdateOne returns an update builder for the given entity.
|
|
func (c *PlanetClient) UpdateOne(pl *Planet) *PlanetUpdateOne {
|
|
return c.UpdateOneID(pl.ID)
|
|
}
|
|
|
|
// UpdateOneID returns an update builder for the given id.
|
|
func (c *PlanetClient) UpdateOneID(id int) *PlanetUpdateOne {
|
|
mutation := newPlanetMutation(c.config, OpUpdateOne)
|
|
mutation.id = &id
|
|
return &PlanetUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
|
}
|
|
|
|
// Delete returns a delete builder for Planet.
|
|
func (c *PlanetClient) Delete() *PlanetDelete {
|
|
mutation := newPlanetMutation(c.config, OpDelete)
|
|
return &PlanetDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
|
}
|
|
|
|
// DeleteOne returns a delete builder for the given entity.
|
|
func (c *PlanetClient) DeleteOne(pl *Planet) *PlanetDeleteOne {
|
|
return c.DeleteOneID(pl.ID)
|
|
}
|
|
|
|
// DeleteOneID returns a delete builder for the given id.
|
|
func (c *PlanetClient) DeleteOneID(id int) *PlanetDeleteOne {
|
|
builder := c.Delete().Where(planet.ID(id))
|
|
builder.mutation.id = &id
|
|
builder.mutation.op = OpDeleteOne
|
|
return &PlanetDeleteOne{builder}
|
|
}
|
|
|
|
// Create returns a query builder for Planet.
|
|
func (c *PlanetClient) Query() *PlanetQuery {
|
|
return &PlanetQuery{config: c.config}
|
|
}
|
|
|
|
// Get returns a Planet entity by its id.
|
|
func (c *PlanetClient) Get(ctx context.Context, id int) (*Planet, error) {
|
|
return c.Query().Where(planet.ID(id)).Only(ctx)
|
|
}
|
|
|
|
// GetX is like Get, but panics if an error occurs.
|
|
func (c *PlanetClient) GetX(ctx context.Context, id int) *Planet {
|
|
pl, err := c.Get(ctx, id)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return pl
|
|
}
|
|
|
|
// QueryNeighbors queries the neighbors edge of a Planet.
|
|
func (c *PlanetClient) QueryNeighbors(pl *Planet) *PlanetQuery {
|
|
query := &PlanetQuery{config: c.config}
|
|
query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) {
|
|
id := pl.ID
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(planet.Table, planet.FieldID, id),
|
|
sqlgraph.To(planet.Table, planet.FieldID),
|
|
sqlgraph.Edge(sqlgraph.M2M, false, planet.NeighborsTable, planet.NeighborsPrimaryKey...),
|
|
)
|
|
fromV = sqlgraph.Neighbors(pl.driver.Dialect(), step)
|
|
return fromV, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// Hooks returns the client hooks.
|
|
func (c *PlanetClient) Hooks() []Hook {
|
|
hooks := c.hooks.Planet
|
|
return append(hooks[:len(hooks):len(hooks)], planet.Hooks[:]...)
|
|
}
|