mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
@@ -11,6 +11,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/facebookincubator/ent/dialect/sql"
|
||||
"github.com/facebookincubator/ent/examples/o2orecur/ent/node"
|
||||
)
|
||||
|
||||
// Node is the model entity for the Node schema.
|
||||
@@ -22,21 +23,31 @@ type Node struct {
|
||||
Value int `json:"value,omitempty"`
|
||||
}
|
||||
|
||||
// FromRows scans the sql response data into Node.
|
||||
func (n *Node) FromRows(rows *sql.Rows) error {
|
||||
var scann struct {
|
||||
ID int
|
||||
Value sql.NullInt64
|
||||
// scanValues returns the types for scanning values from sql.Rows.
|
||||
func (*Node) scanValues() []interface{} {
|
||||
return []interface{}{
|
||||
&sql.NullInt64{},
|
||||
&sql.NullInt64{},
|
||||
}
|
||||
// the order here should be the same as in the `node.Columns`.
|
||||
if err := rows.Scan(
|
||||
&scann.ID,
|
||||
&scann.Value,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// assignValues assigns the values that were returned from sql.Rows (after scanning)
|
||||
// to the Node fields.
|
||||
func (n *Node) assignValues(values ...interface{}) error {
|
||||
if m, n := len(values), len(node.Columns); m != n {
|
||||
return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
|
||||
}
|
||||
value, ok := values[0].(*sql.NullInt64)
|
||||
if !ok {
|
||||
return fmt.Errorf("unexpected type %T for field id", value)
|
||||
}
|
||||
n.ID = int(value.Int64)
|
||||
values = values[1:]
|
||||
if value, ok := values[0].(*sql.NullInt64); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field value", values[0])
|
||||
} else if value.Valid {
|
||||
n.Value = int(value.Int64)
|
||||
}
|
||||
n.ID = scann.ID
|
||||
n.Value = int(scann.Value.Int64)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -82,18 +93,6 @@ func (n *Node) String() string {
|
||||
// Nodes is a parsable slice of Node.
|
||||
type Nodes []*Node
|
||||
|
||||
// FromRows scans the sql response data into Nodes.
|
||||
func (n *Nodes) FromRows(rows *sql.Rows) error {
|
||||
for rows.Next() {
|
||||
scann := &Node{}
|
||||
if err := scann.FromRows(rows); err != nil {
|
||||
return err
|
||||
}
|
||||
*n = append(*n, scann)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n Nodes) config(cfg config) {
|
||||
for _i := range n {
|
||||
n[_i].config = cfg
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
|
||||
"github.com/facebookincubator/ent/examples/o2orecur/ent/node"
|
||||
"github.com/facebookincubator/ent/examples/o2orecur/ent/predicate"
|
||||
"github.com/facebookincubator/ent/schema/field"
|
||||
)
|
||||
|
||||
// NodeQuery is the builder for querying Node entities.
|
||||
@@ -289,45 +290,31 @@ func (nq *NodeQuery) Select(field string, fields ...string) *NodeSelect {
|
||||
}
|
||||
|
||||
func (nq *NodeQuery) sqlAll(ctx context.Context) ([]*Node, error) {
|
||||
rows := &sql.Rows{}
|
||||
selector := nq.sqlQuery()
|
||||
if unique := nq.unique; len(unique) == 0 {
|
||||
selector.Distinct()
|
||||
var (
|
||||
nodes []*Node
|
||||
spec = nq.querySpec()
|
||||
)
|
||||
spec.ScanValues = func() []interface{} {
|
||||
node := &Node{config: nq.config}
|
||||
nodes = append(nodes, node)
|
||||
return node.scanValues()
|
||||
}
|
||||
query, args := selector.Query()
|
||||
if err := nq.driver.Query(ctx, query, args, rows); err != nil {
|
||||
spec.Assign = func(values ...interface{}) error {
|
||||
if len(nodes) == 0 {
|
||||
return fmt.Errorf("ent: Assign called without calling ScanValues")
|
||||
}
|
||||
node := nodes[len(nodes)-1]
|
||||
return node.assignValues(values...)
|
||||
}
|
||||
if err := sqlgraph.QueryNodes(ctx, nq.driver, spec); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var ns Nodes
|
||||
if err := ns.FromRows(rows); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ns.config(nq.config)
|
||||
return ns, nil
|
||||
return nodes, nil
|
||||
}
|
||||
|
||||
func (nq *NodeQuery) sqlCount(ctx context.Context) (int, error) {
|
||||
rows := &sql.Rows{}
|
||||
selector := nq.sqlQuery()
|
||||
unique := []string{node.FieldID}
|
||||
if len(nq.unique) > 0 {
|
||||
unique = nq.unique
|
||||
}
|
||||
selector.Count(sql.Distinct(selector.Columns(unique...)...))
|
||||
query, args := selector.Query()
|
||||
if err := nq.driver.Query(ctx, query, args, rows); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
if !rows.Next() {
|
||||
return 0, errors.New("ent: no rows found")
|
||||
}
|
||||
var n int
|
||||
if err := rows.Scan(&n); err != nil {
|
||||
return 0, fmt.Errorf("ent: failed reading count: %v", err)
|
||||
}
|
||||
return n, nil
|
||||
spec := nq.querySpec()
|
||||
return sqlgraph.CountNodes(ctx, nq.driver, spec)
|
||||
}
|
||||
|
||||
func (nq *NodeQuery) sqlExist(ctx context.Context) (bool, error) {
|
||||
@@ -338,6 +325,42 @@ func (nq *NodeQuery) sqlExist(ctx context.Context) (bool, error) {
|
||||
return n > 0, nil
|
||||
}
|
||||
|
||||
func (nq *NodeQuery) querySpec() *sqlgraph.QuerySpec {
|
||||
spec := &sqlgraph.QuerySpec{
|
||||
Node: &sqlgraph.NodeSpec{
|
||||
Table: node.Table,
|
||||
Columns: node.Columns,
|
||||
ID: &sqlgraph.FieldSpec{
|
||||
Type: field.TypeInt,
|
||||
Column: node.FieldID,
|
||||
},
|
||||
},
|
||||
From: nq.sql,
|
||||
Unique: true,
|
||||
}
|
||||
if ps := nq.predicates; len(ps) > 0 {
|
||||
spec.Predicate = func(selector *sql.Selector) {
|
||||
for i := range ps {
|
||||
ps[i](selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
if limit := nq.limit; limit != nil {
|
||||
spec.Limit = *limit
|
||||
}
|
||||
if offset := nq.offset; offset != nil {
|
||||
spec.Offset = *offset
|
||||
}
|
||||
if ps := nq.order; len(ps) > 0 {
|
||||
spec.Order = func(selector *sql.Selector) {
|
||||
for i := range ps {
|
||||
ps[i](selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
return spec
|
||||
}
|
||||
|
||||
func (nq *NodeQuery) sqlQuery() *sql.Selector {
|
||||
builder := sql.Dialect(nq.driver.Dialect())
|
||||
t1 := builder.Table(node.Table)
|
||||
@@ -609,7 +632,7 @@ func (ns *NodeSelect) sqlScan(ctx context.Context, v interface{}) error {
|
||||
}
|
||||
|
||||
func (ns *NodeSelect) sqlQuery() sql.Querier {
|
||||
view := "node_view"
|
||||
return sql.Dialect(ns.driver.Dialect()).
|
||||
Select(ns.fields...).From(ns.sql.As(view))
|
||||
selector := ns.sql
|
||||
selector.Select(selector.Columns(ns.fields...)...)
|
||||
return selector
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ package ent
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/facebookincubator/ent/dialect/sql"
|
||||
"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
|
||||
@@ -468,27 +467,8 @@ func (nuo *NodeUpdateOne) sqlSave(ctx context.Context) (n *Node, err error) {
|
||||
spec.Edges.Add = append(spec.Edges.Add, edge)
|
||||
}
|
||||
n = &Node{config: nuo.config}
|
||||
spec.ScanTypes = []interface{}{
|
||||
&sql.NullInt64{},
|
||||
&sql.NullInt64{},
|
||||
}
|
||||
spec.Assign = func(values ...interface{}) error {
|
||||
if m, n := len(values), len(spec.ScanTypes); m != n {
|
||||
return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
|
||||
}
|
||||
value, ok := values[0].(*sql.NullInt64)
|
||||
if !ok {
|
||||
return fmt.Errorf("unexpected type %T for field id", value)
|
||||
}
|
||||
n.ID = int(value.Int64)
|
||||
values = values[1:]
|
||||
if value, ok := values[0].(*sql.NullInt64); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field value", values[0])
|
||||
} else if value.Valid {
|
||||
n.Value = int(value.Int64)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
spec.Assign = n.assignValues
|
||||
spec.ScanValues = n.scanValues()
|
||||
if err = sqlgraph.UpdateNode(ctx, nuo.driver, spec); err != nil {
|
||||
if cerr, ok := isSQLConstraintError(err); ok {
|
||||
err = cerr
|
||||
|
||||
Reference in New Issue
Block a user