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

@@ -38,3 +38,18 @@ var Columns = []string{
var ForeignKeys = []string{
"user_pets",
}
// 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
}
}
for i := range ForeignKeys {
if column == ForeignKeys[i] {
return true
}
}
return false
}

View File

@@ -693,6 +693,11 @@ func (pgb *PetGroupBy) BoolX(ctx context.Context) bool {
}
func (pgb *PetGroupBy) sqlScan(ctx context.Context, v interface{}) error {
for _, f := range pgb.fields {
if !pet.ValidColumn(f) {
return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for group-by", f)}
}
}
rows := &sql.Rows{}
query, args := pgb.sqlQuery().Query()
if err := pgb.driver.Query(ctx, query, args, rows); err != nil {
@@ -927,6 +932,11 @@ func (ps *PetSelect) BoolX(ctx context.Context) bool {
}
func (ps *PetSelect) sqlScan(ctx context.Context, v interface{}) error {
for _, f := range ps.fields {
if !pet.ValidColumn(f) {
return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for selection", f)}
}
}
rows := &sql.Rows{}
query, args := ps.sqlQuery().Query()
if err := ps.driver.Query(ctx, query, args, rows); err != nil {

View File

@@ -36,3 +36,13 @@ var Columns = []string{
FieldAge,
FieldName,
}
// 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

@@ -686,6 +686,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 {
@@ -920,6 +925,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 {