Documentation
¶
Overview ¶
Package sqlgraph provides graph abstraction capabilities on top of sql-based databases for ent codegen.
Index ¶
- func BatchCreate(ctx context.Context, drv dialect.Driver, spec *BatchCreateSpec) error
- func CountNodes(ctx context.Context, drv dialect.Driver, spec *QuerySpec) (int, error)
- func CreateNode(ctx context.Context, drv dialect.Driver, spec *CreateSpec) error
- func DeleteNodes(ctx context.Context, drv dialect.Driver, spec *DeleteSpec) (int, error)
- func HasNeighbors(q *sql.Selector, s *Step)
- func HasNeighborsWith(q *sql.Selector, s *Step, pred func(*sql.Selector))
- func IsCheckConstraintError(err error) bool
- func IsConstraintError(err error) bool
- func IsForeignKeyConstraintError(err error) bool
- func IsUniqueConstraintError(err error) bool
- func LimitNeighbors(partitionBy string, limit int, orderBy ...sql.Querier) func(*sql.Selector)
- func Neighbors[T ~string](dialect T, s *Step) (q *sql.Selector)
- func OrderByNeighborTerms(q *sql.Selector, s *Step, opts ...sql.OrderTerm)
- func OrderByNeighborsCount(q *sql.Selector, s *Step, opts ...sql.OrderTermOption)
- func QueryEdges(ctx context.Context, drv dialect.Driver, spec *EdgeQuerySpec) error
- func QueryNodes(ctx context.Context, drv dialect.Driver, spec *QuerySpec) error
- func QuerySelector(ctx context.Context, driver dialect.Driver, selector *sql.Selector) (dialect.Rows, error)
- func SetNeighbors[T ~string](dialect T, s *Step) (q *sql.Selector)
- func UpdateNode(ctx context.Context, drv dialect.Driver, spec *UpdateSpec) error
- func UpdateNodes(ctx context.Context, drv dialect.Driver, spec *UpdateSpec) (int, error)
- type BatchCreateSpec
- type ConstraintError
- type CreateSpec
- type DeleteSpec
- type EdgeMut
- type EdgeQuerySpec
- type EdgeSpec
- type EdgeSpecs
- type EdgeTarget
- type FieldMut
- type FieldSpec
- type NeighborsLimit
- type NodeSpec
- type NotFoundError
- type QuerySpec
- type Rel
- type Returning
- type Step
- type StepOption
- type UpdateSpec
- func (u *UpdateSpec) AddField(column string, t field.Type, value driver.Value)
- func (u *UpdateSpec) AddModifier(m func(*sql.UpdateBuilder))
- func (u *UpdateSpec) AddModifiers(m ...func(*sql.UpdateBuilder))
- func (u *UpdateSpec) ClearField(column string, t field.Type)
- func (u *UpdateSpec) SetField(column string, t field.Type, value driver.Value)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BatchCreate ¶
BatchCreate applies the BatchCreateSpec on the graph.
func CountNodes ¶
CountNodes counts the nodes in the given graph query.
func CreateNode ¶
CreateNode applies the CreateSpec on the graph. The operation creates a new record in the database, and connects it to other nodes specified in spec.Edges.
func DeleteNodes ¶
DeleteNodes applies the DeleteSpec on the graph.
func HasNeighbors ¶
HasNeighbors applies on the given Selector a neighbors check.
func HasNeighborsWith ¶
HasNeighborsWith applies on the given Selector a neighbors check. The given predicate applies its filtering on the selector.
func IsCheckConstraintError ¶
func IsConstraintError ¶
func IsUniqueConstraintError ¶
func LimitNeighbors ¶
LimitNeighbors returns a modifier that limits the number of neighbors (rows) loaded per parent row (node). The "partitionBy" is the foreign-key column (edge) to partition the window function by, the "limit" is the maximum number of rows per parent, and the "orderBy" defines the order of how neighbors (connected by the edge) are returned.
This function is useful for non-unique edges, such as O2M and M2M, where the same parent can have multiple children.
func Neighbors ¶
Neighbors returns a Selector for evaluating the path-step and getting the neighbors of one vertex.
func OrderByNeighborTerms ¶
OrderByNeighborTerms appends ordering based on the number of neighbors. For example, order users by their number of posts.
func OrderByNeighborsCount ¶
func OrderByNeighborsCount(q *sql.Selector, s *Step, opts ...sql.OrderTermOption)
OrderByNeighborsCount appends ordering based on the number of neighbors. For example, order users by their number of posts.
func QueryEdges ¶
QueryEdges queries the edges in the graph and scans the result with the given dest function.
func QueryNodes ¶
QueryNodes queries the nodes in the graph query and scans them to the given values.
func QuerySelector ¶ added in v0.3.1
func SetNeighbors ¶
SetNeighbors returns a Selector for evaluating the path-step and getting the neighbors of set of vertices.
func UpdateNode ¶
UpdateNode applies the UpdateSpec on one node in the graph.
func UpdateNodes ¶
UpdateNodes applies the UpdateSpec on a set of nodes in the graph.
Types ¶
type BatchCreateSpec ¶
type BatchCreateSpec struct {
Nodes []*CreateSpec
// The OnConflict option allows providing on-conflict
// options to the INSERT statement.
//
// sqlgraph.CreateSpec{
// OnConflict: []sql.ConflictOption{
// sql.ResolveWithNewValues(),
// },
// }
//
OnConflict []sql.ConflictOption
}
BatchCreateSpec holds the information for creating multiple nodes in the graph.
type ConstraintError ¶
type ConstraintError struct {
Err error
// contains filtered or unexported fields
}
A ConstraintError represents an error from mutation that violates a specific constraint.
func (*ConstraintError) Error ¶
func (e *ConstraintError) Error() string
func (*ConstraintError) Unwrap ¶ added in v0.3.0
func (e *ConstraintError) Unwrap() error
type CreateSpec ¶
type CreateSpec struct {
Expressions map[string]func(*sql.Builder)
Skipped bool
Returning *Returning
Table string
Schema string
ID *FieldSpec
Fields []*FieldSpec
Edges []*EdgeSpec
// The OnConflict option allows providing on-conflict
// options to the INSERT statement.
//
// sqlgraph.CreateSpec{
// OnConflict: []sql.ConflictOption{
// sql.ResolveWithNewValues(),
// },
// }
//
OnConflict []sql.ConflictOption
}
CreateSpec holds the information for creating a node in the graph.
func NewCreateSpec ¶
func NewCreateSpec(table string, id *FieldSpec) *CreateSpec
NewCreateSpec creates a new node creation spec.
type DeleteSpec ¶
DeleteSpec holds the information for delete one or more nodes in the graph.
func NewDeleteSpec ¶
func NewDeleteSpec(table string, id *FieldSpec) *DeleteSpec
NewDeleteSpec creates a new node deletion spec.
type EdgeQuerySpec ¶
type EdgeQuerySpec struct {
Edge *EdgeSpec
Predicate func(*sql.Selector)
ScanValues func() [2]any
Assign func(out, in any) error
}
EdgeQuerySpec holds the information for querying edges in the graph.
type EdgeSpec ¶
type EdgeSpec struct {
Rel Rel
Inverse bool
Table string
Schema string
Columns []string
Bidi bool // bidirectional edge.
Target *EdgeTarget // target nodes.
}
EdgeSpec holds the information for updating a field column in the database.
type EdgeSpecs ¶
type EdgeSpecs []*EdgeSpec
EdgeSpecs used for perform common operations on list of edges.
func (EdgeSpecs) GroupTable ¶
GroupTable groups edges by their table name.
type EdgeTarget ¶
type EdgeTarget struct {
Nodes []driver.Value
IDSpec *FieldSpec
// Additional fields can be set on the
// edge join table. Valid for M2M edges.
Fields []*FieldSpec
}
EdgeTarget holds the information for the target nodes of an edge.
func (*EdgeTarget) FieldValues ¶
func (e *EdgeTarget) FieldValues() []any
FieldValues returns the values of additional fields that were set on the join-table.
type FieldMut ¶
type FieldMut struct {
Set []*FieldSpec // field = ?
Add []*FieldSpec // field = field + ?
Clear []*FieldSpec // field = NULL
}
FieldMut defines field mutations.
type NeighborsLimit ¶
type NeighborsLimit struct {
// RowNumber is the window's output column name.
RowNumber string
// DefaultOrderField sets the default ordering for
// sub-queries in case no order terms were provided.
DefaultOrderField string
// Offset skips this many neighbors in each partition.
Offset int
}
NeighborsLimit provides a modifier function that limits the number of neighbors (rows) loaded per parent row (node).
type NodeSpec ¶
type NodeSpec struct {
Table string
Schema string
Columns []string
ID *FieldSpec // primary key.
CompositeID []*FieldSpec // composite id (edge schema).
// Unique contains unique column sets besides the primary key, in database
// column names; generated code fills it.
Unique [][]string
}
NodeSpec defines the information for querying and decoding nodes in the graph.
func (*NodeSpec) AddColumnOnce ¶
AddColumnOnce adds the given column to the spec if it is not already present.
type NotFoundError ¶
type NotFoundError struct {
// contains filtered or unexported fields
}
NotFoundError returns when trying to update an entity, and it was not found in the database.
func (*NotFoundError) Error ¶
func (e *NotFoundError) Error() string
type QuerySpec ¶
type QuerySpec struct {
Node *NodeSpec // Nodes info.
From *sql.Selector // Optional query source (from path).
Limit int
Offset int
Unique bool
Order func(*sql.Selector)
Predicate func(*sql.Selector)
Modifiers []func(*sql.Selector)
ScanValues func(columns []string) ([]any, error)
Assign func(columns []string, values []any) error
}
QuerySpec holds the information for querying nodes in the graph.
type Rel ¶
type Rel int
Rel is an edge relation type.
const ( O2O Rel // One to one / has one. O2M // One to many / has many. M2O // Many to one (inverse perspective for O2M). M2M // Many to many. )
Relation types.
type Returning ¶ added in v0.3.0
Returning scans each row returned by a mutation. Create callbacks must include the ID column and assign CreateSpec.ID.Value before returning for edge writes.
type Step ¶
type Step struct {
// From is the source of the step.
From struct {
// V can be either one vertex or set of vertices.
// It can be a pre-processed step (sql.Query) or a simple Go type (integer or string).
V any
// Table holds the table name of V (from).
Table string
// Column to join with. Usually the "id" column.
Column string
}
// Edge holds the edge information for getting the neighbors.
Edge struct {
// Rel of the edge.
Rel Rel
// Schema is an optional name of the database
// where the table is defined.
Schema string
// Table name of where this edge columns reside.
Table string
// Columns of the edge.
// In O2O and M2O, it holds the foreign-key column. Hence, len == 1.
// In M2M, it holds the primary-key columns of the join table. Hence, len == 2.
Columns []string
// Inverse indicates if the edge is an inverse edge.
Inverse bool
}
// To is the dest of the path (the neighbors).
To struct {
// Table holds the table name of the neighbors (to).
Table string
// Schema is an optional name of the database
// where the table is defined.
Schema string
// Column to join with. Usually the "id" column.
Column string
}
}
A Step provides a path-step information to the traversal functions.
func NewStep ¶
func NewStep(opts ...StepOption) *Step
NewStep gets list of options and returns a configured step.
NewStep(
From("table", "pk", V),
To("table", "pk"),
Edge("name", O2M, "fk"),
)
func (*Step) FromEdgeOwner ¶
FromEdgeOwner returns true if the step is from an edge owner. i.e., from the table that holds the foreign-key.
func (*Step) ThroughEdgeTable ¶
ThroughEdgeTable returns true if the step is through a join-table.
func (*Step) ToEdgeOwner ¶
ToEdgeOwner returns true if the step is to an edge owner. i.e., to the table that holds the foreign-key.
type StepOption ¶
type StepOption func(*Step)
StepOption allows configuring Steps using functional options.
type UpdateSpec ¶
type UpdateSpec struct {
Returning *Returning
Node *NodeSpec
Edges EdgeMut
Fields FieldMut
Predicate func(*sql.Selector)
Modifiers []func(*sql.UpdateBuilder)
ScanValues func(columns []string) ([]any, error)
Assign func(columns []string, values []any) error
OldScanValues func(columns []string) ([]any, error)
OldAssign func(columns []string, values []any) error
}
UpdateSpec holds the information for updating one or more nodes in the graph.
func NewUpdateSpec ¶
func NewUpdateSpec(table string, columns []string, id ...*FieldSpec) *UpdateSpec
NewUpdateSpec creates a new node update spec.
func (*UpdateSpec) AddModifier ¶
func (u *UpdateSpec) AddModifier(m func(*sql.UpdateBuilder))
AddModifier adds a new statement modifier to the spec.
func (*UpdateSpec) AddModifiers ¶
func (u *UpdateSpec) AddModifiers(m ...func(*sql.UpdateBuilder))
AddModifiers adds a list of statement modifiers to the spec.
func (*UpdateSpec) ClearField ¶
func (u *UpdateSpec) ClearField(column string, t field.Type)
ClearField appends a new field cleaner (set to NULL) to the update spec.