mirror of
https://github.com/ent/ent.git
synced 2026-05-28 09:49:08 +03:00
entc/gen: allow selecting specific fields (#1075)
This commit is contained in:
@@ -45,29 +45,42 @@ func (e CityEdges) StreetsOrErr() ([]*Street, error) {
|
||||
}
|
||||
|
||||
// scanValues returns the types for scanning values from sql.Rows.
|
||||
func (*City) scanValues() []interface{} {
|
||||
return []interface{}{
|
||||
&sql.NullInt64{}, // id
|
||||
&sql.NullString{}, // name
|
||||
func (*City) scanValues(columns []string) ([]interface{}, error) {
|
||||
values := make([]interface{}, len(columns))
|
||||
for i := range columns {
|
||||
switch columns[i] {
|
||||
case city.FieldID:
|
||||
values[i] = &sql.NullInt64{}
|
||||
case city.FieldName:
|
||||
values[i] = &sql.NullString{}
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected column %q for type City", columns[i])
|
||||
}
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
// assignValues assigns the values that were returned from sql.Rows (after scanning)
|
||||
// to the City fields.
|
||||
func (c *City) assignValues(values ...interface{}) error {
|
||||
if m, n := len(values), len(city.Columns); m < n {
|
||||
func (c *City) assignValues(columns []string, values []interface{}) error {
|
||||
if m, n := len(values), len(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 name", values[0])
|
||||
} else if value.Valid {
|
||||
c.Name = value.String
|
||||
for i := range columns {
|
||||
switch columns[i] {
|
||||
case city.FieldID:
|
||||
value, ok := values[i].(*sql.NullInt64)
|
||||
if !ok {
|
||||
return fmt.Errorf("unexpected type %T for field id", value)
|
||||
}
|
||||
c.ID = int(value.Int64)
|
||||
case city.FieldName:
|
||||
if value, ok := values[i].(*sql.NullString); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field name", values[i])
|
||||
} else if value.Valid {
|
||||
c.Name = value.String
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -344,19 +344,18 @@ func (cq *CityQuery) sqlAll(ctx context.Context) ([]*City, error) {
|
||||
cq.withStreets != nil,
|
||||
}
|
||||
)
|
||||
_spec.ScanValues = func() []interface{} {
|
||||
_spec.ScanValues = func(columns []string) ([]interface{}, error) {
|
||||
node := &City{config: cq.config}
|
||||
nodes = append(nodes, node)
|
||||
values := node.scanValues()
|
||||
return values
|
||||
return node.scanValues(columns)
|
||||
}
|
||||
_spec.Assign = func(values ...interface{}) error {
|
||||
_spec.Assign = func(columns []string, values []interface{}) error {
|
||||
if len(nodes) == 0 {
|
||||
return fmt.Errorf("ent: Assign called without calling ScanValues")
|
||||
}
|
||||
node := nodes[len(nodes)-1]
|
||||
node.Edges.loadedTypes = loadedTypes
|
||||
return node.assignValues(values...)
|
||||
return node.assignValues(columns, values)
|
||||
}
|
||||
if err := sqlgraph.QueryNodes(ctx, cq.driver, _spec); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -403,7 +403,7 @@ func (cuo *CityUpdateOne) sqlSave(ctx context.Context) (_node *City, err error)
|
||||
}
|
||||
_node = &City{config: cuo.config}
|
||||
_spec.Assign = _node.assignValues
|
||||
_spec.ScanValues = _node.scanValues()
|
||||
_spec.ScanValues = _node.scanValues
|
||||
if err = sqlgraph.UpdateNode(ctx, cuo.driver, _spec); err != nil {
|
||||
if _, ok := err.(*sqlgraph.NotFoundError); ok {
|
||||
err = &NotFoundError{city.Label}
|
||||
|
||||
@@ -52,44 +52,50 @@ func (e StreetEdges) CityOrErr() (*City, error) {
|
||||
}
|
||||
|
||||
// scanValues returns the types for scanning values from sql.Rows.
|
||||
func (*Street) scanValues() []interface{} {
|
||||
return []interface{}{
|
||||
&sql.NullInt64{}, // id
|
||||
&sql.NullString{}, // name
|
||||
}
|
||||
}
|
||||
|
||||
// fkValues returns the types for scanning foreign-keys values from sql.Rows.
|
||||
func (*Street) fkValues() []interface{} {
|
||||
return []interface{}{
|
||||
&sql.NullInt64{}, // city_streets
|
||||
func (*Street) scanValues(columns []string) ([]interface{}, error) {
|
||||
values := make([]interface{}, len(columns))
|
||||
for i := range columns {
|
||||
switch columns[i] {
|
||||
case street.FieldID:
|
||||
values[i] = &sql.NullInt64{}
|
||||
case street.FieldName:
|
||||
values[i] = &sql.NullString{}
|
||||
case street.ForeignKeys[0]: // city_streets
|
||||
values[i] = &sql.NullInt64{}
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected column %q for type Street", columns[i])
|
||||
}
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
// assignValues assigns the values that were returned from sql.Rows (after scanning)
|
||||
// to the Street fields.
|
||||
func (s *Street) assignValues(values ...interface{}) error {
|
||||
if m, n := len(values), len(street.Columns); m < n {
|
||||
func (s *Street) assignValues(columns []string, values []interface{}) error {
|
||||
if m, n := len(values), len(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)
|
||||
}
|
||||
s.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 {
|
||||
s.Name = value.String
|
||||
}
|
||||
values = values[1:]
|
||||
if len(values) == len(street.ForeignKeys) {
|
||||
if value, ok := values[0].(*sql.NullInt64); !ok {
|
||||
return fmt.Errorf("unexpected type %T for edge-field city_streets", value)
|
||||
} else if value.Valid {
|
||||
s.city_streets = new(int)
|
||||
*s.city_streets = int(value.Int64)
|
||||
for i := range columns {
|
||||
switch columns[i] {
|
||||
case street.FieldID:
|
||||
value, ok := values[i].(*sql.NullInt64)
|
||||
if !ok {
|
||||
return fmt.Errorf("unexpected type %T for field id", value)
|
||||
}
|
||||
s.ID = int(value.Int64)
|
||||
case street.FieldName:
|
||||
if value, ok := values[i].(*sql.NullString); !ok {
|
||||
return fmt.Errorf("unexpected type %T for field name", values[i])
|
||||
} else if value.Valid {
|
||||
s.Name = value.String
|
||||
}
|
||||
case street.ForeignKeys[0]:
|
||||
if value, ok := values[i].(*sql.NullInt64); !ok {
|
||||
return fmt.Errorf("unexpected type %T for edge-field city_streets", value)
|
||||
} else if value.Valid {
|
||||
s.city_streets = new(int)
|
||||
*s.city_streets = int(value.Int64)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -351,22 +351,18 @@ func (sq *StreetQuery) sqlAll(ctx context.Context) ([]*Street, error) {
|
||||
if withFKs {
|
||||
_spec.Node.Columns = append(_spec.Node.Columns, street.ForeignKeys...)
|
||||
}
|
||||
_spec.ScanValues = func() []interface{} {
|
||||
_spec.ScanValues = func(columns []string) ([]interface{}, error) {
|
||||
node := &Street{config: sq.config}
|
||||
nodes = append(nodes, node)
|
||||
values := node.scanValues()
|
||||
if withFKs {
|
||||
values = append(values, node.fkValues()...)
|
||||
}
|
||||
return values
|
||||
return node.scanValues(columns)
|
||||
}
|
||||
_spec.Assign = func(values ...interface{}) error {
|
||||
_spec.Assign = func(columns []string, values []interface{}) error {
|
||||
if len(nodes) == 0 {
|
||||
return fmt.Errorf("ent: Assign called without calling ScanValues")
|
||||
}
|
||||
node := nodes[len(nodes)-1]
|
||||
node.Edges.loadedTypes = loadedTypes
|
||||
return node.assignValues(values...)
|
||||
return node.assignValues(columns, values)
|
||||
}
|
||||
if err := sqlgraph.QueryNodes(ctx, sq.driver, _spec); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -343,7 +343,7 @@ func (suo *StreetUpdateOne) sqlSave(ctx context.Context) (_node *Street, err err
|
||||
}
|
||||
_node = &Street{config: suo.config}
|
||||
_spec.Assign = _node.assignValues
|
||||
_spec.ScanValues = _node.scanValues()
|
||||
_spec.ScanValues = _node.scanValues
|
||||
if err = sqlgraph.UpdateNode(ctx, suo.driver, _spec); err != nil {
|
||||
if _, ok := err.(*sqlgraph.NotFoundError); ok {
|
||||
err = &NotFoundError{street.Label}
|
||||
|
||||
Reference in New Issue
Block a user