# Dialects

You write **one** table definition; Bootgly turns it into the right SQL for your database.
PostgreSQL is the default; MySQL and SQLite also work. You usually don't think about this —
this page is for when it matters.

## The short version

- **PostgreSQL** — lacks none of the capabilities below. A column rename goes in an `alter()` of
  its own, and an index on a schema-qualified table needs a `name:` (see
  **[Generated names](/manual/ADI/Databases/SQL/Schema/overview/#generated-names)**).
- **MySQL** — works, with differences: no `USING` cast; nullability changes only together with a
  type change, which restates the whole column (`MODIFY COLUMN`); an inline foreign key becomes a
  named table-level constraint; `unindex()` and `unconstrain()` ignore `exists`.
- **SQLite** — great for tests and local dev, but its `ALTER TABLE` is limited: one action per
  `alter()`, no `change()` of any kind (type, default or nullability), no table-level
  `reference()` in `alter()` — an inline one on a column you `add()` works —, no `unconstrain()`.
  Adding, renaming and dropping columns work.

If you write a migration that an engine can't perform, compiling it **stops with a clear error**
before anything is sent — for example
`InvalidArgumentException: SQLite schema dialect lacks capability: AlterColumnNullability.` The
builder does not check everything the server checks, though: a definition the server rejects — a
default the column type cannot hold, a column SQLite cannot add — compiles, and fails when it runs.

## Same code, different SQL

```php
$Schema->create('users', function (Blueprint $Table): void {
   $Table->add('id', Types::BigInteger)->generate()->constrain(Keys::Primary);
   $Table->add('email', Types::String)->limit(190)->constrain(Keys::Unique);
}, exists: true);
```

PostgreSQL:
```sql
CREATE TABLE IF NOT EXISTS "users" ("id" BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY, "email" VARCHAR(190) NOT NULL UNIQUE)
```
MySQL:
```sql
CREATE TABLE IF NOT EXISTS `users` (`id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, `email` VARCHAR(190) NOT NULL UNIQUE)
```
SQLite:
```sql
CREATE TABLE IF NOT EXISTS "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "email" TEXT NOT NULL UNIQUE)
```

Same migration, three correct results — that's the point. You only pick the driver: in a project,
through `DB_CONNECTION` in its `database` config (see
**[From the project config](/manual/ADI/Database/Config/overview/#from-the-project-config)**);
outside one, in the connection array:

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

new SQL(['driver' => 'pgsql', 'host' => '127.0.0.1', 'port' => 5432, 'database' => 'app', 'username' => 'app', 'password' => 'secret']);
// MySQL: set its port and user too — the defaults are PostgreSQL's (5432, postgres)
new SQL(['driver' => 'mysql', 'host' => '127.0.0.1', 'port' => 3306, 'database' => 'app', 'username' => 'app', 'password' => 'secret']);
new SQL(['driver' => 'sqlite', 'database' => ':memory:']);
```

## What each engine can do

Each `Capabilities` case names one thing a dialect may lack. `$Schema->Dialect->check()` answers
for the engine in use (see [Reference](#reference)):

| Capability | Lets you | PostgreSQL | MySQL | SQLite |
|------------|----------|:----------:|:-----:|:------:|
| `AlterColumnType` | change a column's type | ✅ | ✅ restating the whole column | ❌ |
| `AlterColumnUsing` | convert the values with `cast()` (`USING`) | ✅ | ❌ | ❌ |
| `AlterColumnDefault` | set or drop a column's default | ✅ | ✅ | ❌ |
| `AlterColumnNullability` | change only a column's nullability | ✅ | ❌ only inside a type change | ❌ |
| `AddConstraint` | add a foreign key with the table-level `reference()` in `alter()` | ✅ | ✅ | ❌ |
| `DropConstraint` | drop a constraint with `unconstrain()` | ✅ | ✅ foreign keys only | ❌ |
| `MultiActionAlter` | put several actions in one `alter()` | ✅ except a rename | ✅ | ❌ |
| `RenameColumn` | rename a column | ✅ in its own `alter()` | ✅ | ✅ |
| `DropColumn` | drop a column | ✅ | ✅ | ✅ |

An action the engine lacks throws
`InvalidArgumentException: <Dialect> schema dialect lacks capability: <Capability>.` — `<Dialect>`
is `MySQL` or `SQLite`, since PostgreSQL lacks none. PostgreSQL's rename rule has its own message
(see **[Change a table](/manual/ADI/Databases/SQL/Schema/overview/#change-a-table)**), and so do the
MySQL type-change rules (see
**[On MySQL, a type change carries the whole column](/manual/ADI/Databases/SQL/Schema/Blueprint/overview/#on-mysql-a-type-change-carries-the-whole-column)**).

Prefer this table over assumptions; if you target multiple engines, stick to the rows that are ✅
everywhere — add, rename and drop columns, one action per `alter()` — and declare indexes and
foreign keys with the table.

## Column types per engine

| `Types` case | PostgreSQL | MySQL | SQLite |
|--------------|------------|-------|--------|
| `BigInteger` | `BIGINT` | `BIGINT` | `INTEGER` |
| `Boolean` | `BOOLEAN` | `BOOLEAN` (stored as `tinyint(1)`) | `INTEGER` |
| `Date` | `DATE` | `DATE` | `TEXT` |
| `Decimal` | `NUMERIC(p, s)`, or `NUMERIC` without `size()` | `DECIMAL(p, s)`, or `DECIMAL` without `size()` | `NUMERIC` |
| `Float` | `DOUBLE PRECISION` | `DOUBLE` | `REAL` |
| `Integer` | `INTEGER` | `INT` | `INTEGER` |
| `Json` | `JSON` | `JSON` | `TEXT` |
| `JsonB` | `JSONB` | `JSON` | `TEXT` |
| `String` | `VARCHAR(n)` | `VARCHAR(n)` | `TEXT` |
| `Text` | `TEXT` | `LONGTEXT` | `TEXT` |
| `Time` | `TIME` | `TIME` | `TEXT` |
| `Timestamp` | `TIMESTAMP` | `TIMESTAMP` | `TEXT` |
| `Timestamptz` | `TIMESTAMPTZ` | `TIMESTAMP` | `TEXT` |
| `Uuid` | `UUID` | `CHAR(36)` | `TEXT` |

- **`String`** takes its `n` from `limit()`, `255` without it. SQLite ignores the length: a longer
  value is stored whole.
- **`Decimal`** takes `p` and `s` from `size()`. Without them, PostgreSQL's `NUMERIC` holds any
  number, but MySQL's `DECIMAL` is `DECIMAL(10,0)` — 12.75 is stored as 13. SQLite ignores the size
  and stores a value with a fraction as a floating-point `REAL` (`0.1 + 0.2` reads back as
  `0.30000000000000004`): keep money in integer cents there.
- **`Timestamp` and `Timestamptz`** differ on PostgreSQL only. MySQL's `TIMESTAMP` converts a value
  from the session time zone to UTC and back, and holds `1970-01-01 00:00:01` to
  `2038-01-19 03:14:07` UTC — a value outside fails with `1292: Incorrect datetime value`.
- **`Json` and `JsonB`** stay distinct on PostgreSQL; the other engines map both to their JSON type.
- A column added without a type is `Types::Text`.

## Foreign keys per engine

| Declaration | PostgreSQL | MySQL | SQLite |
|-------------|------------|-------|--------|
| `add('team_id', Types::BigInteger)->reference('teams')` | `"team_id" BIGINT NOT NULL REFERENCES "teams" ("id")`, named `members_team_id_fkey` by the engine | ``CONSTRAINT `members_team_id_teams_fk` FOREIGN KEY (`team_id`) REFERENCES `teams` (`id`)``, after the columns | `"team_id" INTEGER NOT NULL REFERENCES "teams" ("id")`, unnamed |
| `reference('team_id', 'teams')` | `CONSTRAINT "members_team_id_teams_fk" FOREIGN KEY ("team_id") REFERENCES "teams" ("id")` | ``CONSTRAINT `members_team_id_teams_fk` FOREIGN KEY (`team_id`) REFERENCES `teams` (`id`)`` | `CONSTRAINT "members_team_id_teams_fk" FOREIGN KEY ("team_id") REFERENCES "teams" ("id")` |

- In `alter()`, MySQL puts an `ADD CONSTRAINT` for the same key right after the `ADD COLUMN` of an
  inline reference. SQLite takes the inline form there, but refuses the table-level one (`AddConstraint`).
- The `name:` of an inline `reference()` is written on MySQL only. Use the table-level
  `reference()` for a key a later migration may drop: its name is the same on every engine.
- On MySQL a foreign key blocks `remove()` of its column (`1828`) and `unindex()` of the index
  that enforces it (`1553`), and needs both columns of the same type (`3780`) — see
  **[Linking to another table](/manual/ADI/Databases/SQL/Schema/Blueprint/overview/#linking-to-another-table-foreign-key)**.

## Dropping indexes and constraints

| Call | PostgreSQL | MySQL | SQLite |
|------|------------|-------|--------|
| `drop('users')` | `DROP TABLE IF EXISTS "users"` | ``DROP TABLE IF EXISTS `users` `` | `DROP TABLE IF EXISTS "users"` |
| `unindex('users', 'users_email_index')` | `DROP INDEX IF EXISTS "users_email_index"` | ``DROP INDEX `users_email_index` ON `users` `` | `DROP INDEX IF EXISTS "users_email_index"` |
| `unconstrain('users', 'users_team_id_teams_fk')` | `ALTER TABLE "users" DROP CONSTRAINT IF EXISTS "users_team_id_teams_fk"` | ``ALTER TABLE `users` DROP FOREIGN KEY `users_team_id_teams_fk` `` | throws (`DropConstraint`) |

- `exists: false` takes `IF EXISTS` out of every statement that has it.
- For now, MySQL ignores `exists` in `unindex()` and `unconstrain()`: dropping an index or a foreign
  key that is already gone fails with `1091: Can't DROP '…'; check that column/key exists` — see
  **[Drop and re-run](/manual/ADI/Databases/SQL/Schema/overview/#drop-and-re-run)**.
- MySQL's `unconstrain()` drops foreign keys only. A `UNIQUE` constraint is an index there, which
  `unindex()` drops; a `CHECK` needs a raw statement.
- PostgreSQL qualifies the index with the schema of a schema-qualified table:
  `unindex('audit.users', 'users_email_index')` compiles
  `DROP INDEX IF EXISTS "audit"."users_email_index"`. SQLite does not use the table.

## Adding columns on SQLite

SQLite's `ALTER TABLE … ADD COLUMN` accepts less than a `CREATE TABLE`. A column you `add()` in
`alter()`:

- can be `NOT NULL` only with a default other than `NULL` — on a table with rows, SQLite answers
  `Cannot add a NOT NULL column with default value NULL`;
- cannot be the primary key (`Cannot add a PRIMARY KEY column`) or `UNIQUE`
  (`Cannot add a UNIQUE column`);
- cannot default to a non-constant expression such as `CURRENT_TIMESTAMP`:
  `Cannot add a column with non-constant default`.

`remove()` also refuses a column an index still uses (`error in index … after drop column`):
`unindex()` it first. These shapes work on every engine:

```php
$Schema->alter('users', function (Blueprint $Table): void {
   $Table->add('score', Types::Integer)->default = 0;               // NOT NULL, constant default
});
$Schema->alter('users', function (Blueprint $Table): void {
   $Table->add('nickname', Types::String)->limit(40)->nullable = true;
});
$Schema->index('users', 'nickname', unique: true);                 // the UNIQUE part, as an index
```

> [!WARNING]
> For now, adding a column that carries a `check()`, or that is `NOT NULL` without a default, fails
> on SQLite even when the table is empty, with `duplicate column name: <column>` — and outside a
> transaction the column has been added anyway. Inside a migration, SQLite's migration transaction
> rolls it back. Give a `NOT NULL` column a default, and declare checks in the `create()`.

## Explicit keys and identity sequences

| Engine | After an INSERT with explicit ids | `resync()` |
|--------|-----------------------------------|------------|
| PostgreSQL | the identity (or serial) sequence stays behind | `setval()`, only when the sequence is behind |
| MySQL/MariaDB | `AUTO_INCREMENT` moves past the highest id | `null` |
| SQLite | `AUTOINCREMENT` moves past the highest id | `null` |

On PostgreSQL the role that runs the statement needs `USAGE` (or `SELECT`) and `UPDATE` on the
sequence — the owner of the table has them. Without those privileges it fails with
`permission denied for sequence …`.

The statement never lowers a sequence that is already ahead when it runs. Its limits: a key
above an identity's own `MAXVALUE` fails with `setval: value … is out of bounds`; a key at the
column type's maximum leaves no ids for generated rows; and an insert into the same table while
it runs can slip between its read and its move — seed while nothing else writes.

## Transactional DDL and advisory locks

| Engine | `transactions` (DDL in a transaction) | Advisory lock |
|--------|---------------------------------------|---------------|
| PostgreSQL | yes | `pg_try_advisory_lock` / `pg_advisory_unlock` |
| MySQL/MariaDB | no — DDL causes implicit commits | `GET_LOCK('bootgly_<key>', 0)` / `RELEASE_LOCK` |
| SQLite | yes | — (local file lock only) |

Where `transactions` is true, the migration runner wraps each migration in a transaction, and the
seed runner the queries each seeder returns. Both runners take the advisory lock beside their
local file lock, without waiting: when another run holds either one, the run fails at once instead
of queueing.

An advisory lock belongs to the session that took it: a release sent from another session
releases nothing — `pg_advisory_unlock` answers `false`, `RELEASE_LOCK` `0` — and the lock stays
until its session ends. Which handle to give a runner, so that its release reaches that session,
is in **[Locking](/manual/ADI/Databases/SQL/Schema/Migrations/overview/#locking)**.

## Writing a dialect

A schema dialect extends `Bootgly\ADI\Databases\SQL\Schema\Dialect`. It implements the seven
compile methods and overrides `check()`, `lock()`/`unlock()`, `resync()` and `$transactions` where
its engine differs. It reads the `Blueprint`, `Column`, `Change`, `Reference`, `Rename` and `Index`
the builder fills — their properties are in the
**[Defining tables](/manual/ADI/Databases/SQL/Schema/Blueprint/overview/#reference)** Reference — and
can use the protected helpers of the base class: `guard()` and `fail()` throw the capability error,
`quote()` quotes an identifier through the Query Builder dialect, `escape()` writes a default
literal and `refer()` writes a referential action.

The registry behind `new Schema()` and `SQL::structure()` maps the three built-in Query Builder
dialects to their schema dialects and refuses any other. A dialect of your own therefore reaches a
Schema Builder only through `new Schema($Dialect)`; the migration and seed runners always use the
built-in dialect of their database's driver.

## Reference

### Dialect

`Bootgly\ADI\Databases\SQL\Schema\Dialect`, the abstract base of the `PostgreSQL`, `MySQL` and
`SQLite` classes in `Bootgly\ADI\Databases\SQL\Schema\Dialects`. A Schema Builder holds one in
`$Schema->Dialect`.

```php
__construct (QueryDialect $Dialect)
```

Creates the dialect around the Query Builder dialect of the same engine
(`Bootgly\ADI\Databases\SQL\Builder\Dialect`), which quotes identifiers and marks parameters.

```php
public QueryDialect $Dialect
```

That Query Builder dialect.

```php
public bool $transactions = false
```

Whether the engine runs DDL inside a transaction, so the runners wrap each migration, and the
queries each seeder returns, in one: `true` on PostgreSQL and SQLite, `false` on MySQL.

```php
check (Capabilities $Capability): bool
```

Whether the dialect can compile what `$Capability` names — see
[What each engine can do](#what-each-engine-can-do). The base class answers `true` for every case.

```php
create (Blueprint $Blueprint, bool $exists = false): Query
```

Compiles `CREATE TABLE` from the blueprint's columns and foreign keys. `Schema::create()` calls it.

```php
alter (Blueprint $Blueprint): Query
```

Compiles `ALTER TABLE` from the blueprint's actions. `Schema::alter()` calls it.

```php
drop (BackedEnum|Stringable|string $Table, bool $exists = true): Query
```

Compiles `DROP TABLE`.

```php
rename (BackedEnum|Stringable|string $From, BackedEnum|Stringable|string $To): Query
```

Compiles a table rename.

```php
index (Index $Index): Query
```

Compiles `CREATE INDEX` for an `Index` value.

```php
unindex (BackedEnum|Stringable|string $Table, BackedEnum|Stringable|string $Name, bool $exists = true): Query
```

Compiles `DROP INDEX` — see [Dropping indexes and constraints](#dropping-indexes-and-constraints).

```php
unconstrain (BackedEnum|Stringable|string $Table, BackedEnum|Stringable|string $Name, bool $exists = true): Query
```

Compiles the drop of a constraint, or throws where the engine has none.

```php
lock (int $key): null|Query
```

Compiles the advisory lock for `$key`, which answers in a `locked` column — or returns `null` when
the engine has none, as on SQLite.

```php
unlock (int $key): null|Query
```

Compiles the release of that lock, which answers in an `unlocked` column, or returns `null`.

```php
resync (string $table, array $assignments): null|Query
```

Compiles the statement that moves identity sequences past the explicit integer keys one INSERT
wrote. It takes what the Builder compiled for that INSERT — the quoted table and
`Builder::$assignments` — and the seed **[Runner](/manual/ADI/Databases/SQL/Seed/overview/)**
calls it after every `Builder` INSERT a seeder returns. PostgreSQL returns a forward-only
`setval()` statement, or `null` when the INSERT wrote no integer value; a sequence already past
the keys when the statement runs is left untouched. MySQL and SQLite return `null`, since their
auto-increment counters already move past explicit values.

### Dialects

```php
fetch (QueryDialect $Dialect): Dialect
```

`Bootgly\ADI\Databases\SQL\Schema\Dialects` returns a new schema dialect for a built-in Query
Builder dialect — PostgreSQL, MySQL or SQLite — and throws
`InvalidArgumentException: Schema Builder dialect is not registered.` for any other. `new Schema()`
uses it.
