// Code generated by entc, DO NOT EDIT. package ent import ( "context" "fmt" "log" "github.com/go-kratos/examples/blog/internal/data/ent/migrate" "github.com/go-kratos/examples/blog/internal/data/ent/article" "github.com/go-kratos/examples/blog/internal/data/ent/comment" "github.com/go-kratos/examples/blog/internal/data/ent/tag" "entgo.io/ent/dialect" "entgo.io/ent/dialect/sql" "entgo.io/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 // Article is the client for interacting with the Article builders. Article *ArticleClient // Comment is the client for interacting with the Comment builders. Comment *CommentClient // Tag is the client for interacting with the Tag builders. Tag *TagClient } // 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.Article = NewArticleClient(c.config) c.Comment = NewCommentClient(c.config) c.Tag = NewTagClient(c.config) } // 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) } } // 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, 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 := c.config cfg.driver = tx return &Tx{ ctx: ctx, config: cfg, Article: NewArticleClient(cfg), Comment: NewCommentClient(cfg), Tag: NewTagClient(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, fmt.Errorf("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: %v", err) } cfg := c.config cfg.driver = &txDriver{tx: tx, drv: c.driver} return &Tx{ config: cfg, Article: NewArticleClient(cfg), Comment: NewCommentClient(cfg), Tag: NewTagClient(cfg), }, nil } // Debug returns a new debug-client. It's used to get verbose logging on specific operations. // // client.Debug(). // Article. // 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) { c.Article.Use(hooks...) c.Comment.Use(hooks...) c.Tag.Use(hooks...) } // ArticleClient is a client for the Article schema. type ArticleClient struct { config } // NewArticleClient returns a client for the Article from the given config. func NewArticleClient(c config) *ArticleClient { return &ArticleClient{config: c} } // Use adds a list of mutation hooks to the hooks stack. // A call to `Use(f, g, h)` equals to `article.Hooks(f(g(h())))`. func (c *ArticleClient) Use(hooks ...Hook) { c.hooks.Article = append(c.hooks.Article, hooks...) } // Create returns a create builder for Article. func (c *ArticleClient) Create() *ArticleCreate { mutation := newArticleMutation(c.config, OpCreate) return &ArticleCreate{config: c.config, hooks: c.Hooks(), mutation: mutation} } // CreateBulk returns a builder for creating a bulk of Article entities. func (c *ArticleClient) CreateBulk(builders ...*ArticleCreate) *ArticleCreateBulk { return &ArticleCreateBulk{config: c.config, builders: builders} } // Update returns an update builder for Article. func (c *ArticleClient) Update() *ArticleUpdate { mutation := newArticleMutation(c.config, OpUpdate) return &ArticleUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation} } // UpdateOne returns an update builder for the given entity. func (c *ArticleClient) UpdateOne(a *Article) *ArticleUpdateOne { mutation := newArticleMutation(c.config, OpUpdateOne, withArticle(a)) return &ArticleUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} } // UpdateOneID returns an update builder for the given id. func (c *ArticleClient) UpdateOneID(id int64) *ArticleUpdateOne { mutation := newArticleMutation(c.config, OpUpdateOne, withArticleID(id)) return &ArticleUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} } // Delete returns a delete builder for Article. func (c *ArticleClient) Delete() *ArticleDelete { mutation := newArticleMutation(c.config, OpDelete) return &ArticleDelete{config: c.config, hooks: c.Hooks(), mutation: mutation} } // DeleteOne returns a delete builder for the given entity. func (c *ArticleClient) DeleteOne(a *Article) *ArticleDeleteOne { return c.DeleteOneID(a.ID) } // DeleteOneID returns a delete builder for the given id. func (c *ArticleClient) DeleteOneID(id int64) *ArticleDeleteOne { builder := c.Delete().Where(article.ID(id)) builder.mutation.id = &id builder.mutation.op = OpDeleteOne return &ArticleDeleteOne{builder} } // Query returns a query builder for Article. func (c *ArticleClient) Query() *ArticleQuery { return &ArticleQuery{config: c.config} } // Get returns a Article entity by its id. func (c *ArticleClient) Get(ctx context.Context, id int64) (*Article, error) { return c.Query().Where(article.ID(id)).Only(ctx) } // GetX is like Get, but panics if an error occurs. func (c *ArticleClient) GetX(ctx context.Context, id int64) *Article { obj, err := c.Get(ctx, id) if err != nil { panic(err) } return obj } // QueryComments queries the comments edge of a Article. func (c *ArticleClient) QueryComments(a *Article) *CommentQuery { query := &CommentQuery{config: c.config} query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) { id := a.ID step := sqlgraph.NewStep( sqlgraph.From(article.Table, article.FieldID, id), sqlgraph.To(comment.Table, comment.FieldID), sqlgraph.Edge(sqlgraph.O2M, false, article.CommentsTable, article.CommentsColumn), ) fromV = sqlgraph.Neighbors(a.driver.Dialect(), step) return fromV, nil } return query } // QueryTags queries the tags edge of a Article. func (c *ArticleClient) QueryTags(a *Article) *TagQuery { query := &TagQuery{config: c.config} query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) { id := a.ID step := sqlgraph.NewStep( sqlgraph.From(article.Table, article.FieldID, id), sqlgraph.To(tag.Table, tag.FieldID), sqlgraph.Edge(sqlgraph.M2M, true, article.TagsTable, article.TagsPrimaryKey...), ) fromV = sqlgraph.Neighbors(a.driver.Dialect(), step) return fromV, nil } return query } // Hooks returns the client hooks. func (c *ArticleClient) Hooks() []Hook { return c.hooks.Article } // CommentClient is a client for the Comment schema. type CommentClient struct { config } // NewCommentClient returns a client for the Comment from the given config. func NewCommentClient(c config) *CommentClient { return &CommentClient{config: c} } // Use adds a list of mutation hooks to the hooks stack. // A call to `Use(f, g, h)` equals to `comment.Hooks(f(g(h())))`. func (c *CommentClient) Use(hooks ...Hook) { c.hooks.Comment = append(c.hooks.Comment, hooks...) } // Create returns a create builder for Comment. func (c *CommentClient) Create() *CommentCreate { mutation := newCommentMutation(c.config, OpCreate) return &CommentCreate{config: c.config, hooks: c.Hooks(), mutation: mutation} } // CreateBulk returns a builder for creating a bulk of Comment entities. func (c *CommentClient) CreateBulk(builders ...*CommentCreate) *CommentCreateBulk { return &CommentCreateBulk{config: c.config, builders: builders} } // Update returns an update builder for Comment. func (c *CommentClient) Update() *CommentUpdate { mutation := newCommentMutation(c.config, OpUpdate) return &CommentUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation} } // UpdateOne returns an update builder for the given entity. func (c *CommentClient) UpdateOne(co *Comment) *CommentUpdateOne { mutation := newCommentMutation(c.config, OpUpdateOne, withComment(co)) return &CommentUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} } // UpdateOneID returns an update builder for the given id. func (c *CommentClient) UpdateOneID(id int64) *CommentUpdateOne { mutation := newCommentMutation(c.config, OpUpdateOne, withCommentID(id)) return &CommentUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} } // Delete returns a delete builder for Comment. func (c *CommentClient) Delete() *CommentDelete { mutation := newCommentMutation(c.config, OpDelete) return &CommentDelete{config: c.config, hooks: c.Hooks(), mutation: mutation} } // DeleteOne returns a delete builder for the given entity. func (c *CommentClient) DeleteOne(co *Comment) *CommentDeleteOne { return c.DeleteOneID(co.ID) } // DeleteOneID returns a delete builder for the given id. func (c *CommentClient) DeleteOneID(id int64) *CommentDeleteOne { builder := c.Delete().Where(comment.ID(id)) builder.mutation.id = &id builder.mutation.op = OpDeleteOne return &CommentDeleteOne{builder} } // Query returns a query builder for Comment. func (c *CommentClient) Query() *CommentQuery { return &CommentQuery{config: c.config} } // Get returns a Comment entity by its id. func (c *CommentClient) Get(ctx context.Context, id int64) (*Comment, error) { return c.Query().Where(comment.ID(id)).Only(ctx) } // GetX is like Get, but panics if an error occurs. func (c *CommentClient) GetX(ctx context.Context, id int64) *Comment { obj, err := c.Get(ctx, id) if err != nil { panic(err) } return obj } // QueryPost queries the post edge of a Comment. func (c *CommentClient) QueryPost(co *Comment) *ArticleQuery { query := &ArticleQuery{config: c.config} query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) { id := co.ID step := sqlgraph.NewStep( sqlgraph.From(comment.Table, comment.FieldID, id), sqlgraph.To(article.Table, article.FieldID), sqlgraph.Edge(sqlgraph.M2O, true, comment.PostTable, comment.PostColumn), ) fromV = sqlgraph.Neighbors(co.driver.Dialect(), step) return fromV, nil } return query } // Hooks returns the client hooks. func (c *CommentClient) Hooks() []Hook { return c.hooks.Comment } // TagClient is a client for the Tag schema. type TagClient struct { config } // NewTagClient returns a client for the Tag from the given config. func NewTagClient(c config) *TagClient { return &TagClient{config: c} } // Use adds a list of mutation hooks to the hooks stack. // A call to `Use(f, g, h)` equals to `tag.Hooks(f(g(h())))`. func (c *TagClient) Use(hooks ...Hook) { c.hooks.Tag = append(c.hooks.Tag, hooks...) } // Create returns a create builder for Tag. func (c *TagClient) Create() *TagCreate { mutation := newTagMutation(c.config, OpCreate) return &TagCreate{config: c.config, hooks: c.Hooks(), mutation: mutation} } // CreateBulk returns a builder for creating a bulk of Tag entities. func (c *TagClient) CreateBulk(builders ...*TagCreate) *TagCreateBulk { return &TagCreateBulk{config: c.config, builders: builders} } // Update returns an update builder for Tag. func (c *TagClient) Update() *TagUpdate { mutation := newTagMutation(c.config, OpUpdate) return &TagUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation} } // UpdateOne returns an update builder for the given entity. func (c *TagClient) UpdateOne(t *Tag) *TagUpdateOne { mutation := newTagMutation(c.config, OpUpdateOne, withTag(t)) return &TagUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} } // UpdateOneID returns an update builder for the given id. func (c *TagClient) UpdateOneID(id int64) *TagUpdateOne { mutation := newTagMutation(c.config, OpUpdateOne, withTagID(id)) return &TagUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} } // Delete returns a delete builder for Tag. func (c *TagClient) Delete() *TagDelete { mutation := newTagMutation(c.config, OpDelete) return &TagDelete{config: c.config, hooks: c.Hooks(), mutation: mutation} } // DeleteOne returns a delete builder for the given entity. func (c *TagClient) DeleteOne(t *Tag) *TagDeleteOne { return c.DeleteOneID(t.ID) } // DeleteOneID returns a delete builder for the given id. func (c *TagClient) DeleteOneID(id int64) *TagDeleteOne { builder := c.Delete().Where(tag.ID(id)) builder.mutation.id = &id builder.mutation.op = OpDeleteOne return &TagDeleteOne{builder} } // Query returns a query builder for Tag. func (c *TagClient) Query() *TagQuery { return &TagQuery{config: c.config} } // Get returns a Tag entity by its id. func (c *TagClient) Get(ctx context.Context, id int64) (*Tag, error) { return c.Query().Where(tag.ID(id)).Only(ctx) } // GetX is like Get, but panics if an error occurs. func (c *TagClient) GetX(ctx context.Context, id int64) *Tag { obj, err := c.Get(ctx, id) if err != nil { panic(err) } return obj } // QueryPosts queries the posts edge of a Tag. func (c *TagClient) QueryPosts(t *Tag) *ArticleQuery { query := &ArticleQuery{config: c.config} query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) { id := t.ID step := sqlgraph.NewStep( sqlgraph.From(tag.Table, tag.FieldID, id), sqlgraph.To(article.Table, article.FieldID), sqlgraph.Edge(sqlgraph.M2M, false, tag.PostsTable, tag.PostsPrimaryKey...), ) fromV = sqlgraph.Neighbors(t.driver.Dialect(), step) return fromV, nil } return query } // Hooks returns the client hooks. func (c *TagClient) Hooks() []Hook { return c.hooks.Tag }