# unchecked-query

Default severity: **warn**

## What it does

Flags `--sql` queries that are not plain string literals: a raw or bytes string, alone or implicitly concatenated with others, or a `SQL[T](...)` argument that holds anything besides string literals. `SQL[Any](...)`, with `typing.Any` under any import name, is not flagged, nor is a psycopg composition passed to a helper whose row type is `Any`.

A psycopg composition passed to a helper is flagged too when a part is not a literal. With [`preview`](<https://sqlty.kuncar.dev/guide/query-variants/index.md>), one built from names bound in branches is checked along every path instead, unless it has more variants than `max-query-variants`.

## Why is this bad?

sqlty reads queries from the source text, so it neither rewrites nor checks these: the row type is never inferred or compared.

## Example

```python
from sqlty import SQL

# error: [unchecked-query] "--sql query is not plain string literals, so it is not checked or rewritten"
q = SQL[tuple[int]]("--sql\nSELECT id::INTEGER FROM " + table)
# error: [unchecked-query] "--sql query is not plain string literals, so it is not checked or rewritten"
r = r"--sql\nSELECT id::INTEGER FROM users WHERE name ~ '\d'"
```

## Fix

Write the query as one plain literal, or as implicitly concatenated plain literals, and pass values as query parameters. A query that must stay dynamic can opt out with `SQL[Any](...)`, or `Any` as the row type of a helper called with a psycopg composition.
