# `Lotus.SQL.SortInjector`
[🔗](https://github.com/typhoonworks/lotus/blob/v0.16.6/lib/lotus/sql/sort_injector.ex#L1)

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)

# `apply`

```elixir
@spec apply(String.t(), [Lotus.Query.Sort.t()], (String.t() -&gt; 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.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
