entc/gen: add column-check for selection and grouping (#744)

This commit is contained in:
Ariel Mashraki
2020-09-09 12:27:28 +03:00
committed by GitHub
parent d16e78e686
commit 8b8744022e
114 changed files with 1272 additions and 8 deletions

View File

@@ -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
}

View File

@@ -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 {

View File

@@ -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
}

View File

@@ -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 {