SqlValidator
A utility object that provides functionality for SQL validation and schema handling. This includes methods for checking SQL query syntax, validating against schemas, managing schemas from definition files, and ensuring data type consistency during validation.
Types
Functions
Removes an ORDER BY clause that has no effect on the result of a scalar method (exists*, count*) — ordering rows the query never returns individually is pure overhead for the database. Returns the SQL unchanged when there is no ORDER BY or the statement is not a plain select.
Rewrites a bare RETURNING * on a write over the entity's own table into the entity's explicit columns, mirroring expandSelectStar. Returns the SQL unchanged for anything else (a non-write, a write against a different table, or an explicit RETURNING column list).
Loads SQL schema definitions from the specified directory, processes each .sql file encountered, and constructs a representation of the database schema consisting of tables and their columns.
Rejects a GROUP BY / HAVING clause in a method that reads a single scalar row (count*, findOne*, exists*). Such a query returns one row per group, but these methods only read the first row — so grouping would silently yield the wrong result. Non-plain selects are skipped.
Rejects a @Query value that contains more than one SQL statement (a stacked-query safety guard). Malformed SQL is ignored here — it is reported by validateQuerySyntax.
Wraps a plain SELECT ... FROM <entity table> WHERE ... into SELECT EXISTS(SELECT 1 FROM ...) for exists* methods. If the query is not a plain select over the entity table (e.g. the user already wrote SELECT EXISTS(...), which has no FROM), it is returned unchanged.
Validates that every column referenced by a single-table @Query exists on the entity. Multi-table queries (joins, subselects, or a different FROM table) are skipped to avoid false positives.
Validates that a count* method selects a single count(...) aggregate (rather than, say, *).
Validates that a find* method whose result is bound by the generated row mapper selects every entity column (or SELECT *). A partial projection would leave the mapper without a column at runtime. Multi-table queries and non-plain selects are skipped.
Validates the provided SQL query schema against Calcite's SQL parser and validator. Optionally applies custom literal type checks if the VALIDATE_LITERAL_TYPES flag is enabled.
Parses sql and returns the parsed statement(s), so the result can be reused by the structural checks (rejectStackedStatements, validateColumnsExist, expandSelectStar) instead of re-parsing.
Validates that a RETURNING clause references only columns that exist on the entity.
Validates that the statement kind matches the one implied by the method prefix (e.g. find* must be a SELECT, delete* a DELETE, execute* a write).
Validates that a single-table @Query targets the repository entity's own table. A FROM that is a subselect (or a non-CRUD statement) is skipped.
Appends a LIMIT [rowCount] to a SELECT that has none, so findOne* methods fetch at most a couple of rows while still detecting a multi-row result. Returns the SQL unchanged for anything that already has a limit or is not a plain select. LIMIT n is portable across all supported dialects (PostgreSQL, MySQL, SQLite).