dialect/sql: move columns check from codegen to sql package (#3431)

This commit is contained in:
Ariel Mashraki
2023-04-02 13:57:21 +03:00
committed by GitHub
parent 90289b7494
commit abafd91b2d
37 changed files with 815 additions and 1102 deletions

View File

@@ -4,6 +4,10 @@
package sql
import (
"fmt"
)
// The following helpers exist to simplify the way raw predicates
// are defined and used in both ent/schema and generated code. For
// full predicates API, check out the sql.P in builder.go.
@@ -162,3 +166,23 @@ func FieldContainsFold(name string, substr string) func(*Selector) {
s.Where(ContainsFold(s.C(name), substr))
}
}
// ColumnCheck is a function that verifies whether the
// specified column exists within the given table.
type ColumnCheck func(table, column string) error
// NewColumnCheck returns a function that verifies whether the specified column exists
// within the given table. This function is utilized by the generated code to validate
// column names in ordering functions.
func NewColumnCheck(checks map[string]func(string) bool) ColumnCheck {
return func(table, column string) error {
check, ok := checks[table]
if !ok {
return fmt.Errorf("unknown table %q", table)
}
if !check(column) {
return fmt.Errorf("unknown column %q for table %q", column, table)
}
return nil
}
}