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:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user