# Database queries

After your migrations create the tables, the daily loop is simple: build a query, run it
against the database your project configures in `configs/database`, then read the resulting
`Operation`.

Use the **[Query Builder](/manual/ADI/Databases/SQL/Builder/overview/)** when query shape
comes from application code. Use raw SQL only when the builder is not the right fit.

In `HTTP_Server_CLI` routes, do not build `SQL` yourself: use the per-worker
`$Response->Database`
**[Response Resource](/manual/WPI/HTTP/HTTP_Server_CLI/Response/Resources/overview/)**. The
**[Database DBAL](/guide/database-dbal/overview/)** guide shows how it waits for operations
through the response instead of blocking the worker.

## The flow

```php
use Bootgly\ADI\Databases\SQL;
use Bootgly\ADI\Databases\SQL\Builder\Auxiliaries\Operators;
use Bootgly\API\Environment\Configs\DatabaseConfig;

enum Tables: string { case Users = 'users'; }
enum Columns: string { case Id = 'id'; case Name = 'name'; case Active = 'active'; }

$Database = new SQL(new DatabaseConfig(BOOTGLY_PROJECT->Configs->get('database'))->configure());

$Users = $Database
   ->table(Tables::Users)
   ->select(Columns::Id, Columns::Name)
   ->filter(Columns::Active, Operators::Equal, true);

$Operation = $Database->query($Users);
$Database->await($Operation);

$rows = $Operation->Result->rows;
```

This builds `SQL` from the project's `database` config scope, so it runs inside a project (its
boot function, a command or a script) whose `configs/database` defines that scope; see
**[Configure the database](/guide/database-dbal/overview/#configure-the-database)**. In a
project without a `configs/` directory, `BOOTGLY_PROJECT->Configs` is `null`. Outside a
project, pass the connection settings as an array, for example
`new SQL(['driver' => 'pgsql', 'host' => '127.0.0.1', 'port' => 5432, 'database' => 'app', 'username' => 'app', 'password' => 'secret'])`.
A bare `new SQL` never reads your project config: it uses the built-in local defaults
(PostgreSQL at `127.0.0.1:5432`, database `bootgly`, user `postgres`, empty password), and an
array falls back to them for every key it omits, whatever its `driver`, so a MySQL array must
set `port` and `username` too.

`SQL::table()` starts the builder with the database dialect already selected. Passing the
builder to `SQL::query()` compiles it into SQL and ordered parameters before the operation
is assigned to the pool. `await()` drives the operation to its end and returns it; the rows are
in `$Operation->Result->rows`, keyed by column name. Everything else a finished operation holds
is in **[Read the outcome](/manual/ADI/Databases/SQL/overview/#read-the-outcome)**.

## Read rows

```php
$Users = $Database
   ->table(Tables::Users)
   ->select(Columns::Id, Columns::Name)
   ->filter(Columns::Name, Operators::Equal, 'Ada')
   ->limit(10);

$Operation = $Database->query($Users);
$Database->await($Operation);
```

Compiled for PostgreSQL:

```sql
SELECT "id", "name" FROM "users" WHERE "name" = $1 LIMIT 10
```

Parameters:

```php
['Ada']
```

Each `filter()` is joined to the one before it with `AND`. Write `->or` before a `filter()` to
join it with `OR` instead:

```php
$Users = $Database
   ->table(Tables::Users)
   ->select(Columns::Id, Columns::Name)
   ->filter(Columns::Name, Operators::Equal, 'Ada')
   ->or
   ->filter(Columns::Name, Operators::Equal, 'Grace');
```

Compiled for PostgreSQL:

```sql
SELECT "id", "name" FROM "users" WHERE "name" = $1 OR "name" = $2
```

`->or` applies to the next filter only. To group conditions, as in `A AND (B OR C)`, use `nest()`
— see **[OR and AND](/manual/ADI/Databases/SQL/Builder/Reading/overview/#or-and-and)**.

When read replicas are configured, read builders like these can route to a replica unless the
current scope is sticky after a write. See
**[Database read replicas](/guide/database-read-replicas/overview/)** for the routing rules.

## Write rows

```php
$Insert = $Database
   ->table(Tables::Users)
   ->insert()
   ->set(Columns::Name, 'Ada')
   ->set(Columns::Active, true)
   ->output(Columns::Id);

$Operation = $Database->query($Insert);
$Database->await($Operation);
```

Only PostgreSQL supports `output()`, which compiles to `RETURNING`; the returned columns
arrive in `$Operation->Result->rows`. MySQL and SQLite reject it (`output()` throws
`InvalidArgumentException` with `SQL dialect does not support RETURNING output.`), so omit it
there and read the generated key from `$Operation->Result->inserted` after awaiting the
operation; PostgreSQL leaves that field at `0`. See
**[Returning output](/manual/ADI/Databases/SQL/Builder/Writing/overview/#returning-output)**
and
**[Generated keys without RETURNING](/manual/ADI/Databases/SQL/Drivers/overview/#generated-keys-without-returning)**.

`update()` and `delete()` require at least one `filter()`. The builder stops global mutation
queries before they can run: `query()` throws instead of sending them.

## Use a transaction when writes must stay together

When several statements must share one connection and commit or roll back as a unit, use
`SQL::begin()` and run builders through `Transaction::query()`. See
**[Database transactions](/guide/database-transactions/overview/)** for the full flow,
rollback pattern and savepoints.

## Dynamic names and raw expressions

Prefer backed enums for table and column names. When a name is only known at runtime, wrap
it in `Identifier` so the dialect quotes it, segment by segment —
`SELECT "users"."id" FROM "users"` on PostgreSQL:

```php
use Bootgly\ADI\Databases\SQL\Builder\Identifier;

$Users = $Database
   ->table(new Identifier('users'))
   ->select(new Identifier('users.id'));
```

Quoting stops injection, not access: check a name that comes from a request against the names you
allow first — see
**[Dynamic identifiers](/manual/ADI/Databases/SQL/Builder/Composing/overview/#dynamic-identifiers)**.

Use `Expression` only for trusted SQL fragments:

```php
use Bootgly\ADI\Databases\SQL\Builder\Expression;

$Now = new Expression('CURRENT_TIMESTAMP');

$Query = $Database
   ->table(Tables::Users)
   ->select($Now);
```

`Expression` does not carry bindings. Values that come from users should go through
`filter()` or `set()` so they become parameters. Both wrappers, and the subqueries and CTEs
you can build with them, are in
**[Composing queries](/manual/ADI/Databases/SQL/Builder/Composing/overview/)**.

## When a query throws

A query can fail in two places. A builder that cannot become a valid statement throws
`InvalidArgumentException` before anything is sent: a builder call throws at once for bad input,
and `query()` throws for a statement it cannot compile, as below. A statement the database refuses
fails the operation instead: `await()` throws `RuntimeException` with the server's message, and the
driver's error code stays in `$Operation->code`.

```php
use InvalidArgumentException;
use RuntimeException;

try {
   $Operation = $Database->query($Users);
   $Database->await($Operation);
}
catch (InvalidArgumentException $Exception) {
   echo "Invalid query: {$Exception->getMessage()}\n";
}
catch (RuntimeException $Exception) {
   echo "Query failed ({$Operation->code}): {$Exception->getMessage()}\n";
}
```

A `delete()` without a filter prints `Invalid query: SQL update/delete requires at least one filter.`;
an insert of an id that already exists prints, on PostgreSQL,
`Query failed (23505): duplicate key value violates unique constraint "users_pkey"`. Every
builder error, and when it is thrown, is listed in
**[Errors](/manual/ADI/Databases/SQL/Builder/overview/#errors)**; the failure fields of an
operation are in **[Read the outcome](/manual/ADI/Databases/SQL/overview/#read-the-outcome)**.

## Next references

- **[Query Builder](/manual/ADI/Databases/SQL/Builder/overview/)** - entry points, compile and execution, modes and errors.
- **[Database DBAL](/guide/database-dbal/overview/)** - async DBAL usage in HTTP responses.
- **[Database ORM](/guide/database-orm/overview/)** - Data Mapper entities over SQL operations.
- **[Database read replicas](/guide/database-read-replicas/overview/)** - route safe reads to replicas and keep writes on primary.
- **[Reading rows](/manual/ADI/Databases/SQL/Builder/Reading/overview/)** - select, filters, joins, grouping and limits.
- **[Writing rows](/manual/ADI/Databases/SQL/Builder/Writing/overview/)** - insert, update, delete, output and upsert.
- **[Composing queries](/manual/ADI/Databases/SQL/Builder/Composing/overview/)** - identifiers, expressions, subqueries and CTEs.
- **[Query dialects](/manual/ADI/Databases/SQL/Builder/Dialects/overview/)** - PostgreSQL, MySQL and SQLite differences.
- **[Transactions](/manual/ADI/Databases/SQL/Transaction/overview/)** - commit, rollback and savepoints.
