mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: add column-check for selection and grouping (#744)
This commit is contained in:
@@ -37,3 +37,13 @@ var (
|
||||
// primary key for the users relation (M2M).
|
||||
UsersPrimaryKey = []string{"group_id", "user_id"}
|
||||
)
|
||||
|
||||
// ValidColumn reports if the column name is valid (part of the table columns).
|
||||
func ValidColumn(column string) bool {
|
||||
for i := range Columns {
|
||||
if column == Columns[i] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -721,6 +721,11 @@ func (ggb *GroupGroupBy) BoolX(ctx context.Context) bool {
|
||||
}
|
||||
|
||||
func (ggb *GroupGroupBy) sqlScan(ctx context.Context, v interface{}) error {
|
||||
for _, f := range ggb.fields {
|
||||
if !group.ValidColumn(f) {
|
||||
return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for group-by", f)}
|
||||
}
|
||||
}
|
||||
rows := &sql.Rows{}
|
||||
query, args := ggb.sqlQuery().Query()
|
||||
if err := ggb.driver.Query(ctx, query, args, rows); err != nil {
|
||||
@@ -955,6 +960,11 @@ func (gs *GroupSelect) BoolX(ctx context.Context) bool {
|
||||
}
|
||||
|
||||
func (gs *GroupSelect) sqlScan(ctx context.Context, v interface{}) error {
|
||||
for _, f := range gs.fields {
|
||||
if !group.ValidColumn(f) {
|
||||
return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for selection", f)}
|
||||
}
|
||||
}
|
||||
rows := &sql.Rows{}
|
||||
query, args := gs.sqlQuery().Query()
|
||||
if err := gs.driver.Query(ctx, query, args, rows); err != nil {
|
||||
|
||||
@@ -40,3 +40,13 @@ var (
|
||||
// primary key for the groups relation (M2M).
|
||||
GroupsPrimaryKey = []string{"group_id", "user_id"}
|
||||
)
|
||||
|
||||
// ValidColumn reports if the column name is valid (part of the table columns).
|
||||
func ValidColumn(column string) bool {
|
||||
for i := range Columns {
|
||||
if column == Columns[i] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -721,6 +721,11 @@ func (ugb *UserGroupBy) BoolX(ctx context.Context) bool {
|
||||
}
|
||||
|
||||
func (ugb *UserGroupBy) sqlScan(ctx context.Context, v interface{}) error {
|
||||
for _, f := range ugb.fields {
|
||||
if !user.ValidColumn(f) {
|
||||
return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for group-by", f)}
|
||||
}
|
||||
}
|
||||
rows := &sql.Rows{}
|
||||
query, args := ugb.sqlQuery().Query()
|
||||
if err := ugb.driver.Query(ctx, query, args, rows); err != nil {
|
||||
@@ -955,6 +960,11 @@ func (us *UserSelect) BoolX(ctx context.Context) bool {
|
||||
}
|
||||
|
||||
func (us *UserSelect) sqlScan(ctx context.Context, v interface{}) error {
|
||||
for _, f := range us.fields {
|
||||
if !user.ValidColumn(f) {
|
||||
return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for selection", f)}
|
||||
}
|
||||
}
|
||||
rows := &sql.Rows{}
|
||||
query, args := us.sqlQuery().Query()
|
||||
if err := us.driver.Query(ctx, query, args, rows); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user