examples: regenerate assets

Closes #261
This commit is contained in:
Ariel Mashraki
2019-12-25 11:21:04 +02:00
parent d586492147
commit 1f2cb87cee
60 changed files with 1755 additions and 1739 deletions

View File

@@ -12,6 +12,7 @@ import (
"time"
"github.com/facebookincubator/ent/dialect/sql"
"github.com/facebookincubator/ent/examples/start/ent/car"
)
// Car is the model entity for the Car schema.
@@ -25,24 +26,37 @@ type Car struct {
RegisteredAt time.Time `json:"registered_at,omitempty"`
}
// FromRows scans the sql response data into Car.
func (c *Car) FromRows(rows *sql.Rows) error {
var scanc struct {
ID int
Model sql.NullString
RegisteredAt sql.NullTime
// scanValues returns the types for scanning values from sql.Rows.
func (*Car) scanValues() []interface{} {
return []interface{}{
&sql.NullInt64{},
&sql.NullString{},
&sql.NullTime{},
}
// the order here should be the same as in the `car.Columns`.
if err := rows.Scan(
&scanc.ID,
&scanc.Model,
&scanc.RegisteredAt,
); err != nil {
return err
}
// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Car fields.
func (c *Car) assignValues(values ...interface{}) error {
if m, n := len(values), len(car.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)
}
c.ID = int(value.Int64)
values = values[1:]
if value, ok := values[0].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field model", values[0])
} else if value.Valid {
c.Model = value.String
}
if value, ok := values[1].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field registered_at", values[1])
} else if value.Valid {
c.RegisteredAt = value.Time
}
c.ID = scanc.ID
c.Model = scanc.Model.String
c.RegisteredAt = scanc.RegisteredAt.Time
return nil
}
@@ -85,18 +99,6 @@ func (c *Car) String() string {
// Cars is a parsable slice of Car.
type Cars []*Car
// FromRows scans the sql response data into Cars.
func (c *Cars) FromRows(rows *sql.Rows) error {
for rows.Next() {
scanc := &Car{}
if err := scanc.FromRows(rows); err != nil {
return err
}
*c = append(*c, scanc)
}
return nil
}
func (c Cars) config(cfg config) {
for _i := range c {
c[_i].config = cfg

View File

@@ -17,6 +17,7 @@ import (
"github.com/facebookincubator/ent/examples/start/ent/car"
"github.com/facebookincubator/ent/examples/start/ent/predicate"
"github.com/facebookincubator/ent/examples/start/ent/user"
"github.com/facebookincubator/ent/schema/field"
)
// CarQuery is the builder for querying Car entities.
@@ -278,45 +279,31 @@ func (cq *CarQuery) Select(field string, fields ...string) *CarSelect {
}
func (cq *CarQuery) sqlAll(ctx context.Context) ([]*Car, error) {
rows := &sql.Rows{}
selector := cq.sqlQuery()
if unique := cq.unique; len(unique) == 0 {
selector.Distinct()
var (
nodes []*Car
spec = cq.querySpec()
)
spec.ScanValues = func() []interface{} {
node := &Car{config: cq.config}
nodes = append(nodes, node)
return node.scanValues()
}
query, args := selector.Query()
if err := cq.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, cq.driver, spec); err != nil {
return nil, err
}
defer rows.Close()
var cs Cars
if err := cs.FromRows(rows); err != nil {
return nil, err
}
cs.config(cq.config)
return cs, nil
return nodes, nil
}
func (cq *CarQuery) sqlCount(ctx context.Context) (int, error) {
rows := &sql.Rows{}
selector := cq.sqlQuery()
unique := []string{car.FieldID}
if len(cq.unique) > 0 {
unique = cq.unique
}
selector.Count(sql.Distinct(selector.Columns(unique...)...))
query, args := selector.Query()
if err := cq.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 := cq.querySpec()
return sqlgraph.CountNodes(ctx, cq.driver, spec)
}
func (cq *CarQuery) sqlExist(ctx context.Context) (bool, error) {
@@ -327,6 +314,42 @@ func (cq *CarQuery) sqlExist(ctx context.Context) (bool, error) {
return n > 0, nil
}
func (cq *CarQuery) querySpec() *sqlgraph.QuerySpec {
spec := &sqlgraph.QuerySpec{
Node: &sqlgraph.NodeSpec{
Table: car.Table,
Columns: car.Columns,
ID: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: car.FieldID,
},
},
From: cq.sql,
Unique: true,
}
if ps := cq.predicates; len(ps) > 0 {
spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if limit := cq.limit; limit != nil {
spec.Limit = *limit
}
if offset := cq.offset; offset != nil {
spec.Offset = *offset
}
if ps := cq.order; len(ps) > 0 {
spec.Order = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
return spec
}
func (cq *CarQuery) sqlQuery() *sql.Selector {
builder := sql.Dialect(cq.driver.Dialect())
t1 := builder.Table(car.Table)
@@ -598,7 +621,7 @@ func (cs *CarSelect) sqlScan(ctx context.Context, v interface{}) error {
}
func (cs *CarSelect) sqlQuery() sql.Querier {
view := "car_view"
return sql.Dialect(cs.driver.Dialect()).
Select(cs.fields...).From(cs.sql.As(view))
selector := cs.sql
selector.Select(selector.Columns(cs.fields...)...)
return selector
}

View File

@@ -9,7 +9,6 @@ package ent
import (
"context"
"errors"
"fmt"
"time"
"github.com/facebookincubator/ent/dialect/sql"
@@ -324,33 +323,8 @@ func (cuo *CarUpdateOne) sqlSave(ctx context.Context) (c *Car, err error) {
spec.Edges.Add = append(spec.Edges.Add, edge)
}
c = &Car{config: cuo.config}
spec.ScanTypes = []interface{}{
&sql.NullInt64{},
&sql.NullString{},
&sql.NullTime{},
}
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)
}
c.ID = int(value.Int64)
values = values[1:]
if value, ok := values[0].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field model", values[0])
} else if value.Valid {
c.Model = value.String
}
if value, ok := values[1].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field registered_at", values[1])
} else if value.Valid {
c.RegisteredAt = value.Time
}
return nil
}
spec.Assign = c.assignValues
spec.ScanValues = c.scanValues()
if err = sqlgraph.UpdateNode(ctx, cuo.driver, spec); err != nil {
if cerr, ok := isSQLConstraintError(err); ok {
err = cerr

View File

@@ -11,6 +11,7 @@ import (
"strings"
"github.com/facebookincubator/ent/dialect/sql"
"github.com/facebookincubator/ent/examples/start/ent/group"
)
// Group is the model entity for the Group schema.
@@ -22,21 +23,31 @@ type Group struct {
Name string `json:"name,omitempty"`
}
// FromRows scans the sql response data into Group.
func (gr *Group) FromRows(rows *sql.Rows) error {
var scangr struct {
ID int
Name sql.NullString
// scanValues returns the types for scanning values from sql.Rows.
func (*Group) scanValues() []interface{} {
return []interface{}{
&sql.NullInt64{},
&sql.NullString{},
}
// the order here should be the same as in the `group.Columns`.
if err := rows.Scan(
&scangr.ID,
&scangr.Name,
); err != nil {
return err
}
// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Group fields.
func (gr *Group) assignValues(values ...interface{}) error {
if m, n := len(values), len(group.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)
}
gr.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 {
gr.Name = value.String
}
gr.ID = scangr.ID
gr.Name = scangr.Name.String
return nil
}
@@ -77,18 +88,6 @@ func (gr *Group) String() string {
// Groups is a parsable slice of Group.
type Groups []*Group
// FromRows scans the sql response data into Groups.
func (gr *Groups) FromRows(rows *sql.Rows) error {
for rows.Next() {
scangr := &Group{}
if err := scangr.FromRows(rows); err != nil {
return err
}
*gr = append(*gr, scangr)
}
return nil
}
func (gr Groups) config(cfg config) {
for _i := range gr {
gr[_i].config = cfg

View File

@@ -17,6 +17,7 @@ import (
"github.com/facebookincubator/ent/examples/start/ent/group"
"github.com/facebookincubator/ent/examples/start/ent/predicate"
"github.com/facebookincubator/ent/examples/start/ent/user"
"github.com/facebookincubator/ent/schema/field"
)
// GroupQuery is the builder for querying Group entities.
@@ -278,45 +279,31 @@ func (gq *GroupQuery) Select(field string, fields ...string) *GroupSelect {
}
func (gq *GroupQuery) sqlAll(ctx context.Context) ([]*Group, error) {
rows := &sql.Rows{}
selector := gq.sqlQuery()
if unique := gq.unique; len(unique) == 0 {
selector.Distinct()
var (
nodes []*Group
spec = gq.querySpec()
)
spec.ScanValues = func() []interface{} {
node := &Group{config: gq.config}
nodes = append(nodes, node)
return node.scanValues()
}
query, args := selector.Query()
if err := gq.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, gq.driver, spec); err != nil {
return nil, err
}
defer rows.Close()
var grs Groups
if err := grs.FromRows(rows); err != nil {
return nil, err
}
grs.config(gq.config)
return grs, nil
return nodes, nil
}
func (gq *GroupQuery) sqlCount(ctx context.Context) (int, error) {
rows := &sql.Rows{}
selector := gq.sqlQuery()
unique := []string{group.FieldID}
if len(gq.unique) > 0 {
unique = gq.unique
}
selector.Count(sql.Distinct(selector.Columns(unique...)...))
query, args := selector.Query()
if err := gq.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 := gq.querySpec()
return sqlgraph.CountNodes(ctx, gq.driver, spec)
}
func (gq *GroupQuery) sqlExist(ctx context.Context) (bool, error) {
@@ -327,6 +314,42 @@ func (gq *GroupQuery) sqlExist(ctx context.Context) (bool, error) {
return n > 0, nil
}
func (gq *GroupQuery) querySpec() *sqlgraph.QuerySpec {
spec := &sqlgraph.QuerySpec{
Node: &sqlgraph.NodeSpec{
Table: group.Table,
Columns: group.Columns,
ID: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: group.FieldID,
},
},
From: gq.sql,
Unique: true,
}
if ps := gq.predicates; len(ps) > 0 {
spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if limit := gq.limit; limit != nil {
spec.Limit = *limit
}
if offset := gq.offset; offset != nil {
spec.Offset = *offset
}
if ps := gq.order; len(ps) > 0 {
spec.Order = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
return spec
}
func (gq *GroupQuery) sqlQuery() *sql.Selector {
builder := sql.Dialect(gq.driver.Dialect())
t1 := builder.Table(group.Table)
@@ -598,7 +621,7 @@ func (gs *GroupSelect) sqlScan(ctx context.Context, v interface{}) error {
}
func (gs *GroupSelect) sqlQuery() sql.Querier {
view := "group_view"
return sql.Dialect(gs.driver.Dialect()).
Select(gs.fields...).From(gs.sql.As(view))
selector := gs.sql
selector.Select(selector.Columns(gs.fields...)...)
return selector
}

View File

@@ -328,27 +328,8 @@ func (guo *GroupUpdateOne) sqlSave(ctx context.Context) (gr *Group, err error) {
spec.Edges.Add = append(spec.Edges.Add, edge)
}
gr = &Group{config: guo.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)
}
gr.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 {
gr.Name = value.String
}
return nil
}
spec.Assign = gr.assignValues
spec.ScanValues = gr.scanValues()
if err = sqlgraph.UpdateNode(ctx, guo.driver, spec); err != nil {
if cerr, ok := isSQLConstraintError(err); ok {
err = cerr

View File

@@ -11,6 +11,7 @@ import (
"strings"
"github.com/facebookincubator/ent/dialect/sql"
"github.com/facebookincubator/ent/examples/start/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
}
@@ -89,18 +103,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

View File

@@ -18,6 +18,7 @@ import (
"github.com/facebookincubator/ent/examples/start/ent/group"
"github.com/facebookincubator/ent/examples/start/ent/predicate"
"github.com/facebookincubator/ent/examples/start/ent/user"
"github.com/facebookincubator/ent/schema/field"
)
// UserQuery is the builder for querying User entities.
@@ -291,45 +292,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) {
@@ -340,6 +327,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)
@@ -611,7 +634,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
}

View File

@@ -571,33 +571,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