# SQL task

An SQL task runs a statement against a database: it writes a row for every file a job delivered, or reads rows that say what the job shall do. The rows it reads become items with a variable per column.

The database is somebody else's: the task connects to it with that database's JDBC driver.

> [!NOTE]
> CrushMFT 27 itself keeps nothing in a database. This task is for talking to the databases of other systems.

## Fields

| Field | What it does |
|---|---|
| **DB Driver** | the class name of the JDBC driver: `org.postgresql.Driver`, `com.mysql.cj.jdbc.Driver`, `com.microsoft.sqlserver.jdbc.SQLServerDriver`, `oracle.jdbc.OracleDriver` |
| **DB Driver File** | the driver's jar file. **Browse**. |
| **DB URL** | where the database is, in the driver's own form: `jdbc:postgresql://db.example.com:5432/orders` |
| **DB User**, **DB Pasword** | the account. Give it the rights the statement needs and no more. |
| **Query** | the statement, or several. With [variables](Jobs-Variables.md). |
| **Rollback if a transaction fails.** | everything the task does is one transaction: if any statement fails, none of it is kept |
| **Test DB Settings** | connects and says whether it worked |

### The driver

> DB driver jar files are no longer dynamically loaded. They must be present in the CrushMFT folder/plugins/lib folder at startup time to be available for usage. if you see a ClassNotFound error, please ensure you have the driver jar files copied to that folder and restart CrushMFT.

So: copy the driver's jar into `plugins/lib`, restart the server once, and the driver is there for every job. Drivers are not shipped with the server; they come from the maker of the database.

The Derby driver is refused: "Derby driver is not allowed for SQLTask due to security risks."

## How often it runs

Once for each item the task's filter matches, and once when the list is empty.

So a statement with `{name}` in it, behind a Copy, writes one row per file:

```sql
INSERT INTO deliveries (file_name, bytes, delivered_at)
VALUES ('{name}', {size}, CURRENT_TIMESTAMP)
```

and a `SELECT` at the start of a job, with nothing on the list, runs once.

## Values in a statement are made safe

What a variable brings into the statement is cleaned before it is put in: quotes and other characters that could end the value and start a statement of their own are taken out. A file named `x'; DROP TABLE deliveries; --` cannot do what its name hopes. You still write the quotes around text values yourself, as in the example.

## What a SELECT gives

Every row becomes a new item. For

```sql
SELECT id, target, mail FROM routes WHERE active = 1
```

each item has `{id}`, `{target}` and `{mail}`, and `{row}` as its number. Column names are also there in small letters, whatever the database calls them: `{TARGET}` and `{target}`.

- A date and time column is written the way the server's log writes dates.
- An empty (NULL) value is the text `null`.
- The item the statement ran for gets `{result_count}`: the number of rows read, or changed by an UPDATE, INSERT or DELETE.

## What it hands on

The items it was given, and behind them the rows. The rows are not files: their address begins with `sql://`. Give the tasks behind a filter, or use the rows in a [Loop](Task-Loop.md) of their own.

## Uses

**A delivery log in the partner portal's database**: the INSERT above, behind the Copy.

**Routing from a table.** SELECT the routes, then a Loop for each item with a Find and a Copy that use `{source}` and `{target}`. New partners are added in the table, not in the job.

**Marking as done.** `UPDATE orders SET exported = 1 WHERE id = {id}` at the end of each pass.

## Things to know

- **Who may use it.** A task that loads a driver by name can load program code. It is for full administrators, unless it uses a database connection that an administrator set up by name: see [Who may see, change and run a job](Jobs-Access.md).
- **In FIPS mode** the task is not available: a database driver brings cryptography of its own. See [FIPS mode](FIPS-Mode.md).
- **The password** is stored encrypted in the job and is not written to the log. The statement is: "Executing SQL: …", with the values filled in. Do not put secrets into statements.
- **One connection per run of the task**, closed at its end. For a thousand items that is a thousand statements on one connection, which is fine; for a million, write a file and let the database load it.
- **Time-outs** are the driver's. Most take them in the **DB URL**, such as `?connectTimeout=10`.
