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/o2m2types/ent/pet"
|
||||
)
|
||||
|
||||
// Pet is the model entity for the Pet schema.
|
||||
@@ -22,21 +23,31 @@ type Pet struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
}
|
||||
|
||||
// FromRows scans the sql response data into Pet.
|
||||
func (pe *Pet) FromRows(rows *sql.Rows) error {
|
||||
var scanpe struct {
|
||||
ID int
|
||||
Name sql.NullString
|
||||
// scanValues returns the types for scanning values from sql.Rows.
|
||||
func (*Pet) scanValues() []interface{} {
|
||||
return []interface{}{
|
||||
&sql.NullInt64{},
|
||||
&sql.NullString{},
|
||||
}
|
||||
// the order here should be the same as in the `pet.Columns`.
|
||||
if err := rows.Scan(
|
||||
&scanpe.ID,
|
||||
&scanpe.Name,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// assignValues assigns the values that were returned from sql.Rows (after scanning)
|
||||
// to the Pet fields.
|
||||
func (pe *Pet) assignValues(values ...interface{}) error {
|
||||
if m, n := len(values), len(pet.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)
|
||||
}
|
||||
pe.ID = int(value.Int64)
|
||||
values = values[1:]
|
||||
if value, ok := values[0].(*sql.NullString); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field name", values[0])
|
||||
} else if value.Valid {
|
||||
pe.Name = value.String
|
||||
}
|
||||
pe.ID = scanpe.ID
|
||||
pe.Name = scanpe.Name.String
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -77,18 +88,6 @@ func (pe *Pet) String() string {
|
||||
// Pets is a parsable slice of Pet.
|
||||
type Pets []*Pet
|
||||
|
||||
// FromRows scans the sql response data into Pets.
|
||||
func (pe *Pets) FromRows(rows *sql.Rows) error {
|
||||
for rows.Next() {
|
||||
scanpe := &Pet{}
|
||||
if err := scanpe.FromRows(rows); err != nil {
|
||||
return err
|
||||
}
|
||||
*pe = append(*pe, scanpe)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (pe Pets) config(cfg config) {
|
||||
for _i := range pe {
|
||||
pe[_i].config = cfg
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/facebookincubator/ent/examples/o2m2types/ent/pet"
|
||||
"github.com/facebookincubator/ent/examples/o2m2types/ent/predicate"
|
||||
"github.com/facebookincubator/ent/examples/o2m2types/ent/user"
|
||||
"github.com/facebookincubator/ent/schema/field"
|
||||
)
|
||||
|
||||
// PetQuery is the builder for querying Pet entities.
|
||||
@@ -278,45 +279,31 @@ func (pq *PetQuery) Select(field string, fields ...string) *PetSelect {
|
||||
}
|
||||
|
||||
func (pq *PetQuery) sqlAll(ctx context.Context) ([]*Pet, error) {
|
||||
rows := &sql.Rows{}
|
||||
selector := pq.sqlQuery()
|
||||
if unique := pq.unique; len(unique) == 0 {
|
||||
selector.Distinct()
|
||||
var (
|
||||
nodes []*Pet
|
||||
spec = pq.querySpec()
|
||||
)
|
||||
spec.ScanValues = func() []interface{} {
|
||||
node := &Pet{config: pq.config}
|
||||
nodes = append(nodes, node)
|
||||
return node.scanValues()
|
||||
}
|
||||
query, args := selector.Query()
|
||||
if err := pq.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, pq.driver, spec); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var pes Pets
|
||||
if err := pes.FromRows(rows); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pes.config(pq.config)
|
||||
return pes, nil
|
||||
return nodes, nil
|
||||
}
|
||||
|
||||
func (pq *PetQuery) sqlCount(ctx context.Context) (int, error) {
|
||||
rows := &sql.Rows{}
|
||||
selector := pq.sqlQuery()
|
||||
unique := []string{pet.FieldID}
|
||||
if len(pq.unique) > 0 {
|
||||
unique = pq.unique
|
||||
}
|
||||
selector.Count(sql.Distinct(selector.Columns(unique...)...))
|
||||
query, args := selector.Query()
|
||||
if err := pq.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 := pq.querySpec()
|
||||
return sqlgraph.CountNodes(ctx, pq.driver, spec)
|
||||
}
|
||||
|
||||
func (pq *PetQuery) sqlExist(ctx context.Context) (bool, error) {
|
||||
@@ -327,6 +314,42 @@ func (pq *PetQuery) sqlExist(ctx context.Context) (bool, error) {
|
||||
return n > 0, nil
|
||||
}
|
||||
|
||||
func (pq *PetQuery) querySpec() *sqlgraph.QuerySpec {
|
||||
spec := &sqlgraph.QuerySpec{
|
||||
Node: &sqlgraph.NodeSpec{
|
||||
Table: pet.Table,
|
||||
Columns: pet.Columns,
|
||||
ID: &sqlgraph.FieldSpec{
|
||||
Type: field.TypeInt,
|
||||
Column: pet.FieldID,
|
||||
},
|
||||
},
|
||||
From: pq.sql,
|
||||
Unique: true,
|
||||
}
|
||||
if ps := pq.predicates; len(ps) > 0 {
|
||||
spec.Predicate = func(selector *sql.Selector) {
|
||||
for i := range ps {
|
||||
ps[i](selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
if limit := pq.limit; limit != nil {
|
||||
spec.Limit = *limit
|
||||
}
|
||||
if offset := pq.offset; offset != nil {
|
||||
spec.Offset = *offset
|
||||
}
|
||||
if ps := pq.order; len(ps) > 0 {
|
||||
spec.Order = func(selector *sql.Selector) {
|
||||
for i := range ps {
|
||||
ps[i](selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
return spec
|
||||
}
|
||||
|
||||
func (pq *PetQuery) sqlQuery() *sql.Selector {
|
||||
builder := sql.Dialect(pq.driver.Dialect())
|
||||
t1 := builder.Table(pet.Table)
|
||||
@@ -598,7 +621,7 @@ func (ps *PetSelect) sqlScan(ctx context.Context, v interface{}) error {
|
||||
}
|
||||
|
||||
func (ps *PetSelect) sqlQuery() sql.Querier {
|
||||
view := "pet_view"
|
||||
return sql.Dialect(ps.driver.Dialect()).
|
||||
Select(ps.fields...).From(ps.sql.As(view))
|
||||
selector := ps.sql
|
||||
selector.Select(selector.Columns(ps.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"
|
||||
@@ -295,27 +294,8 @@ func (puo *PetUpdateOne) sqlSave(ctx context.Context) (pe *Pet, err error) {
|
||||
spec.Edges.Add = append(spec.Edges.Add, edge)
|
||||
}
|
||||
pe = &Pet{config: puo.config}
|
||||
spec.ScanTypes = []interface{}{
|
||||
&sql.NullInt64{},
|
||||
&sql.NullString{},
|
||||
}
|
||||
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)
|
||||
}
|
||||
pe.ID = int(value.Int64)
|
||||
values = values[1:]
|
||||
if value, ok := values[0].(*sql.NullString); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field name", values[0])
|
||||
} else if value.Valid {
|
||||
pe.Name = value.String
|
||||
}
|
||||
return nil
|
||||
}
|
||||
spec.Assign = pe.assignValues
|
||||
spec.ScanValues = pe.scanValues()
|
||||
if err = sqlgraph.UpdateNode(ctx, puo.driver, spec); err != nil {
|
||||
if cerr, ok := isSQLConstraintError(err); ok {
|
||||
err = cerr
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/facebookincubator/ent/dialect/sql"
|
||||
"github.com/facebookincubator/ent/examples/o2m2types/ent/user"
|
||||
)
|
||||
|
||||
// User is the model entity for the User schema.
|
||||
@@ -24,24 +25,37 @@ type User struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
}
|
||||
|
||||
// FromRows scans the sql response data into User.
|
||||
func (u *User) FromRows(rows *sql.Rows) error {
|
||||
var scanu struct {
|
||||
ID int
|
||||
Age sql.NullInt64
|
||||
Name sql.NullString
|
||||
// scanValues returns the types for scanning values from sql.Rows.
|
||||
func (*User) scanValues() []interface{} {
|
||||
return []interface{}{
|
||||
&sql.NullInt64{},
|
||||
&sql.NullInt64{},
|
||||
&sql.NullString{},
|
||||
}
|
||||
// the order here should be the same as in the `user.Columns`.
|
||||
if err := rows.Scan(
|
||||
&scanu.ID,
|
||||
&scanu.Age,
|
||||
&scanu.Name,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// assignValues assigns the values that were returned from sql.Rows (after scanning)
|
||||
// to the User fields.
|
||||
func (u *User) assignValues(values ...interface{}) error {
|
||||
if m, n := len(values), len(user.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)
|
||||
}
|
||||
u.ID = int(value.Int64)
|
||||
values = values[1:]
|
||||
if value, ok := values[0].(*sql.NullInt64); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field age", values[0])
|
||||
} else if value.Valid {
|
||||
u.Age = int(value.Int64)
|
||||
}
|
||||
if value, ok := values[1].(*sql.NullString); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field name", values[1])
|
||||
} else if value.Valid {
|
||||
u.Name = value.String
|
||||
}
|
||||
u.ID = scanu.ID
|
||||
u.Age = int(scanu.Age.Int64)
|
||||
u.Name = scanu.Name.String
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -84,18 +98,6 @@ func (u *User) String() string {
|
||||
// Users is a parsable slice of User.
|
||||
type Users []*User
|
||||
|
||||
// FromRows scans the sql response data into Users.
|
||||
func (u *Users) FromRows(rows *sql.Rows) error {
|
||||
for rows.Next() {
|
||||
scanu := &User{}
|
||||
if err := scanu.FromRows(rows); err != nil {
|
||||
return err
|
||||
}
|
||||
*u = append(*u, scanu)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u Users) config(cfg config) {
|
||||
for _i := range u {
|
||||
u[_i].config = cfg
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/facebookincubator/ent/examples/o2m2types/ent/pet"
|
||||
"github.com/facebookincubator/ent/examples/o2m2types/ent/predicate"
|
||||
"github.com/facebookincubator/ent/examples/o2m2types/ent/user"
|
||||
"github.com/facebookincubator/ent/schema/field"
|
||||
)
|
||||
|
||||
// UserQuery is the builder for querying User entities.
|
||||
@@ -278,45 +279,31 @@ func (uq *UserQuery) Select(field string, fields ...string) *UserSelect {
|
||||
}
|
||||
|
||||
func (uq *UserQuery) sqlAll(ctx context.Context) ([]*User, error) {
|
||||
rows := &sql.Rows{}
|
||||
selector := uq.sqlQuery()
|
||||
if unique := uq.unique; len(unique) == 0 {
|
||||
selector.Distinct()
|
||||
var (
|
||||
nodes []*User
|
||||
spec = uq.querySpec()
|
||||
)
|
||||
spec.ScanValues = func() []interface{} {
|
||||
node := &User{config: uq.config}
|
||||
nodes = append(nodes, node)
|
||||
return node.scanValues()
|
||||
}
|
||||
query, args := selector.Query()
|
||||
if err := uq.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, uq.driver, spec); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var us Users
|
||||
if err := us.FromRows(rows); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
us.config(uq.config)
|
||||
return us, nil
|
||||
return nodes, nil
|
||||
}
|
||||
|
||||
func (uq *UserQuery) sqlCount(ctx context.Context) (int, error) {
|
||||
rows := &sql.Rows{}
|
||||
selector := uq.sqlQuery()
|
||||
unique := []string{user.FieldID}
|
||||
if len(uq.unique) > 0 {
|
||||
unique = uq.unique
|
||||
}
|
||||
selector.Count(sql.Distinct(selector.Columns(unique...)...))
|
||||
query, args := selector.Query()
|
||||
if err := uq.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 := uq.querySpec()
|
||||
return sqlgraph.CountNodes(ctx, uq.driver, spec)
|
||||
}
|
||||
|
||||
func (uq *UserQuery) sqlExist(ctx context.Context) (bool, error) {
|
||||
@@ -327,6 +314,42 @@ func (uq *UserQuery) sqlExist(ctx context.Context) (bool, error) {
|
||||
return n > 0, nil
|
||||
}
|
||||
|
||||
func (uq *UserQuery) querySpec() *sqlgraph.QuerySpec {
|
||||
spec := &sqlgraph.QuerySpec{
|
||||
Node: &sqlgraph.NodeSpec{
|
||||
Table: user.Table,
|
||||
Columns: user.Columns,
|
||||
ID: &sqlgraph.FieldSpec{
|
||||
Type: field.TypeInt,
|
||||
Column: user.FieldID,
|
||||
},
|
||||
},
|
||||
From: uq.sql,
|
||||
Unique: true,
|
||||
}
|
||||
if ps := uq.predicates; len(ps) > 0 {
|
||||
spec.Predicate = func(selector *sql.Selector) {
|
||||
for i := range ps {
|
||||
ps[i](selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
if limit := uq.limit; limit != nil {
|
||||
spec.Limit = *limit
|
||||
}
|
||||
if offset := uq.offset; offset != nil {
|
||||
spec.Offset = *offset
|
||||
}
|
||||
if ps := uq.order; len(ps) > 0 {
|
||||
spec.Order = func(selector *sql.Selector) {
|
||||
for i := range ps {
|
||||
ps[i](selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
return spec
|
||||
}
|
||||
|
||||
func (uq *UserQuery) sqlQuery() *sql.Selector {
|
||||
builder := sql.Dialect(uq.driver.Dialect())
|
||||
t1 := builder.Table(user.Table)
|
||||
@@ -598,7 +621,7 @@ func (us *UserSelect) sqlScan(ctx context.Context, v interface{}) error {
|
||||
}
|
||||
|
||||
func (us *UserSelect) sqlQuery() sql.Querier {
|
||||
view := "user_view"
|
||||
return sql.Dialect(us.driver.Dialect()).
|
||||
Select(us.fields...).From(us.sql.As(view))
|
||||
selector := us.sql
|
||||
selector.Select(selector.Columns(us.fields...)...)
|
||||
return selector
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ package ent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/facebookincubator/ent/dialect/sql"
|
||||
"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
|
||||
@@ -384,33 +383,8 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (u *User, err error) {
|
||||
spec.Edges.Add = append(spec.Edges.Add, edge)
|
||||
}
|
||||
u = &User{config: uuo.config}
|
||||
spec.ScanTypes = []interface{}{
|
||||
&sql.NullInt64{},
|
||||
&sql.NullInt64{},
|
||||
&sql.NullString{},
|
||||
}
|
||||
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)
|
||||
}
|
||||
u.ID = int(value.Int64)
|
||||
values = values[1:]
|
||||
if value, ok := values[0].(*sql.NullInt64); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field age", values[0])
|
||||
} else if value.Valid {
|
||||
u.Age = int(value.Int64)
|
||||
}
|
||||
if value, ok := values[1].(*sql.NullString); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field name", values[1])
|
||||
} else if value.Valid {
|
||||
u.Name = value.String
|
||||
}
|
||||
return nil
|
||||
}
|
||||
spec.Assign = u.assignValues
|
||||
spec.ScanValues = u.scanValues()
|
||||
if err = sqlgraph.UpdateNode(ctx, uuo.driver, spec); err != nil {
|
||||
if cerr, ok := isSQLConstraintError(err); ok {
|
||||
err = cerr
|
||||
|
||||
Reference in New Issue
Block a user