Shared helpers for SQL-based sources to inject sort directives into queries.
Wraps the original query in a CTE and appends an ORDER BY clause. This ensures sorting works even when the original query already contains ORDER BY, GROUP BY, or other trailing clauses.
Each SQL source calls this with its own quote_fn for identifier quoting.
Example
quote_fn = fn id -> ~s("#{id}") end
Lotus.SQL.SortInjector.apply("SELECT * FROM users ORDER BY id", [
%Lotus.Query.Sort{column: "created_at", direction: :desc}
], quote_fn)
#=> ~s(WITH _sorted AS (SELECT * FROM users ORDER BY id) SELECT * FROM _sorted ORDER BY "created_at" DESC)
Summary
Functions
Wraps the given SQL in a CTE and appends ORDER BY for each sort directive.
Functions
@spec apply(String.t(), [Lotus.Query.Sort.t()], (String.t() -> String.t())) :: String.t()
Wraps the given SQL in a CTE and appends ORDER BY for each sort directive.
quote_fn is a 1-arity function that quotes an identifier for the target database.
Returns the original SQL unchanged if sorts is empty.