# E-LINT-007: SQL is not a constant

SQL built from values lets a viewer's input change the statement, so the SQL that an app passes to `Query`, `QueryContext`, `QueryRow`, `QueryRowContext`, `Exec`, `ExecContext`, `Prepare` or `PrepareContext` of `*sql.DB`, `*sql.Tx` or `*sql.Conn` must be a constant expression: a string literal, a constant, or constants joined with `+`, never a variable, `fmt.Sprintf` or a string built at run time. Every value goes in with a `?` placeholder. A list of values for `IN` goes in as one JSON array through one placeholder, which `JSON_TABLE` turns into rows, as `aicoded explain guides/sql-database` shows. The SQL is one argument of the call, never the results of another call, such as `db.QueryContext(statement())`, and it holds no executable comment, `/*!` or MariaDB's `/*M!`, whose text the database runs as SQL; optimizer hints, `/*+ ... */`, carry no statement and stay allowed. A method of an interface, or of a type parameter's constraint, whose name is one of these is checked as that method when its signature matches the `database/sql` one once every type parameter in it is allowed to match any type argument, such as the `R` of `QueryContext(ctx context.Context, query string, args ...any) (R, error)`. Such a method may only be called directly, never passed around as a value, so that its SQL can be checked. A method of an interface with one of these names whose signature has a type parameter and still does not match is refused as well, since lint cannot tell whether it takes SQL: give it another name. A constant statement must also be a query or a change of rows: its first keyword is one of `SELECT`, `INSERT`, `UPDATE`, `DELETE`, `REPLACE` or `WITH`. A schema change, `CREATE`, `ALTER`, `DROP`, `RENAME` or `TRUNCATE`, is E-LINT-013 and goes in `.aicoded/migration.sql`. A statement of any other kind is refused, because `SET` can change how SQL is lexed, `PREPARE` and `EXECUTE` run a string held in a variable, and `CALL` runs a stored routine, so lint cannot see what they would do. A constant holds one statement: a `;` outside strings and comments may only end it, so `DELETE FROM drafts; DROP TABLE notes` is refused. Nor may it read or write a file of the database server: `INTO OUTFILE`, `INTO DUMPFILE` and the name `LOAD_FILE`, in any case, also in backquotes, are refused. Generated files are checked too, since they hold the code of the templates' expressions, such as `{{ store.DB.QueryRow('SELECT ' + q) }}`, and a problem there points to the template line that the code was written from.

SQL chosen by the viewer's role is a common case: a list that shows each viewer their own records, and a viewer with a role every record. Never build one statement whose text depends on the role, such as a variable that gets `WHERE author = ?` added for everyone without the role. Write one constant statement for each case and choose one in Go, as `aicoded explain tasks/list-by-role` shows.

**Fix:** write the SQL as a constant and pass every value with a ? placeholder; when the viewer's role decides which rows to read, write one constant statement for each case and choose one in Go, as tasks/list-by-role shows; for a list in IN, use JSON_TABLE(?, ...) as guides/sql-database shows.
