mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
105 lines
2.7 KiB
Go
105 lines
2.7 KiB
Go
// Copyright 2019-present Facebook Inc. 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"
|
|
stdsql "database/sql"
|
|
"fmt"
|
|
|
|
"entgo.io/ent"
|
|
"entgo.io/ent/dialect"
|
|
)
|
|
|
|
// Option function to configure the client.
|
|
type Option func(*config)
|
|
|
|
// Config is the configuration for the client and its builder.
|
|
type 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(...interface{})
|
|
// hooks to execute on mutations.
|
|
hooks *hooks
|
|
}
|
|
|
|
// hooks per client, for fast access.
|
|
type hooks struct {
|
|
Card []ent.Hook
|
|
Comment []ent.Hook
|
|
FieldType []ent.Hook
|
|
File []ent.Hook
|
|
FileType []ent.Hook
|
|
Goods []ent.Hook
|
|
Group []ent.Hook
|
|
GroupInfo []ent.Hook
|
|
Item []ent.Hook
|
|
Node []ent.Hook
|
|
Pet []ent.Hook
|
|
Spec []ent.Hook
|
|
Task []ent.Hook
|
|
User []ent.Hook
|
|
}
|
|
|
|
// 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(...interface{})) 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
|
|
}
|
|
}
|
|
|
|
// ExecContext allows calling the underlying ExecContext method of the driver if it is supported by it.
|
|
// See, database/sql#DB.ExecContext for more information.
|
|
func (c *config) ExecContext(ctx context.Context, query string, args ...interface{}) (stdsql.Result, error) {
|
|
ex, ok := c.driver.(interface {
|
|
ExecContext(context.Context, string, ...interface{}) (stdsql.Result, error)
|
|
})
|
|
if !ok {
|
|
return nil, fmt.Errorf("Driver.ExecContext is not supported")
|
|
}
|
|
return ex.ExecContext(ctx, query, args...)
|
|
}
|
|
|
|
// QueryContext allows calling the underlying QueryContext method of the driver if it is supported by it.
|
|
// See, database/sql#DB.QueryContext for more information.
|
|
func (c *config) QueryContext(ctx context.Context, query string, args ...interface{}) (*stdsql.Rows, error) {
|
|
q, ok := c.driver.(interface {
|
|
QueryContext(context.Context, string, ...interface{}) (*stdsql.Rows, error)
|
|
})
|
|
if !ok {
|
|
return nil, fmt.Errorf("Driver.QueryContext is not supported")
|
|
}
|
|
return q.QueryContext(ctx, query, args...)
|
|
}
|