Skip to content

Core\Db\Driver

Names which database a connection speaks to.

It is the first thing a connection's settings say, and it decides what the rest of those settings have to be: the four server backends are reached at a host, while SQLite is a file you give a path to. There is one case for each backend Novis can connect to — MySQL, MariaDB, PostgreSQL, SQLite and SQL Server. MariaDB is its own case rather than a setting on MySQL, because its logins and its error codes are its own.

Good to know: there is no case meaning "whichever database is configured". A connection can be asked which driver it is on, and a program only asks where the answer changes what it does — the handful of places where one backend wants different SQL from another. Everything else is written once and runs on all five.

Examples

Five backends and what each one needs

<?nvs
// The driver is the first setting of a connection. It decides which other
// settings the connection needs. Four of the five drivers connect to a server
// over the network. SQLite uses a file on the machine your program runs on.
class Backend
{
public static function label(Core\Db\Driver $driver): string
{
return match ($driver) {
Core\Db\Driver::MySql => 'MySQL',
Core\Db\Driver::MariaDb => 'MariaDB',
Core\Db\Driver::Postgres => 'PostgreSQL',
Core\Db\Driver::Sqlite => 'SQLite',
Core\Db\Driver::SqlServer => 'Microsoft SQL Server',
};
}
public static function needs(Core\Db\Driver $driver): string
{
return match ($driver) {
Core\Db\Driver::Sqlite => 'a path to a file',
default => 'a host, and a user to log in as',
};
}
}
array<Core\Db\Driver> $everything = [
Core\Db\Driver::MySql,
Core\Db\Driver::MariaDb,
Core\Db\Driver::Postgres,
Core\Db\Driver::Sqlite,
Core\Db\Driver::SqlServer,
];
foreach ($everything as Core\Db\Driver $driver) {
echo Backend::label($driver), ' takes ', Backend::needs($driver), "\n";
}
// MariaDB has its own case, because it logs in and reports errors differently
// from MySQL. The two cases are not equal, so this prints `no`.
echo 'MySQL and MariaDB are one case: ',
Core\Db\Driver::MySql == Core\Db\Driver::MariaDb ? 'yes' : 'no',
"\n";
Output
MySQL takes a host, and a user to log in as
MariaDB takes a host, and a user to log in as
PostgreSQL takes a host, and a user to log in as
SQLite takes a path to a file
Microsoft SQL Server takes a host, and a user to log in as
MySQL and MariaDB are one case: no

The one statement that is not the same everywhere

<?nvs
// Most SQL statements run unchanged on all five drivers. "Insert this row, or
// update it if it already exists" is not the same on all of them. `Upsert::statement`
// returns the right SQL for each driver, and the loop prints all five.
class Upsert
{
public static function statement(Core\Db\Driver $driver): string
{
return match ($driver) {
Core\Db\Driver::MySql => 'insert into contacts (email, name) values (?, ?) '
. 'on duplicate key update name = values(name)',
Core\Db\Driver::MariaDb => 'insert into contacts (email, name) values (?, ?) '
. 'on duplicate key update name = values(name)',
Core\Db\Driver::Postgres => 'insert into contacts (email, name) values (?, ?) '
. 'on conflict (email) do update set name = excluded.name',
Core\Db\Driver::Sqlite => 'insert into contacts (email, name) values (?, ?) '
. 'on conflict (email) do update set name = excluded.name',
Core\Db\Driver::SqlServer => 'merge contacts as target using (values (?, ?)) '
. 'as source (email, name) on target.email = source.email '
. 'when matched then update set name = source.name '
. 'when not matched then insert (email, name) values (source.email, source.name);',
};
}
public static function label(Core\Db\Driver $driver): string
{
return match ($driver) {
Core\Db\Driver::MySql => 'MySQL',
Core\Db\Driver::MariaDb => 'MariaDB',
Core\Db\Driver::Postgres => 'PostgreSQL',
Core\Db\Driver::Sqlite => 'SQLite',
Core\Db\Driver::SqlServer => 'SQL Server',
};
}
}
array<Core\Db\Driver> $everything = [
Core\Db\Driver::MySql,
Core\Db\Driver::MariaDb,
Core\Db\Driver::Postgres,
Core\Db\Driver::Sqlite,
Core\Db\Driver::SqlServer,
];
foreach ($everything as Core\Db\Driver $driver) {
echo Upsert::label($driver), "\n ", Upsert::statement($driver), "\n";
}
// Every statement above uses `?` for its values, so the values are sent as
// parameters. Only the SQL text changes with the driver.
Output
MySQL
insert into contacts (email, name) values (?, ?) on duplicate key update name = values(name)
MariaDB
insert into contacts (email, name) values (?, ?) on duplicate key update name = values(name)
PostgreSQL
insert into contacts (email, name) values (?, ?) on conflict (email) do update set name = excluded.name
SQLite
insert into contacts (email, name) values (?, ?) on conflict (email) do update set name = excluded.name
SQL Server
merge contacts as target using (values (?, ?)) as source (email, name) on target.email = source.email when matched then update set name = source.name when not matched then insert (email, name) values (source.email, source.name);

The same program on a laptop and in production

<?nvs
// The tests use a SQLite file. Staging and production use a PostgreSQL server.
// `Deployment::driverFor` picks the driver from the name of the environment.
// The SQL that differs between databases is then chosen from the driver.
class Deployment
{
public static function driverFor(string $environment): ?Core\Db\Driver
{
return match ($environment) {
'tests' => Core\Db\Driver::Sqlite,
'staging' => Core\Db\Driver::Postgres,
'production' => Core\Db\Driver::Postgres,
'reporting' => Core\Db\Driver::SqlServer,
default => null,
};
}
// `page` returns the SQL that reads one page of rows. SQL Server uses a
// different syntax from the other four drivers.
public static function page(Core\Db\Driver $driver): string
{
return match ($driver) {
Core\Db\Driver::SqlServer => 'offset ? rows fetch next ? rows only',
default => 'limit ? offset ?',
};
}
}
array<string> $environments = ['tests', 'production', 'reporting', 'sandbox'];
foreach ($environments as string $environment) {
?Core\Db\Driver $driver = Deployment::driverFor($environment);
// `driverFor` returns `null` for an environment with no database.
// The program prints a message for it and does not pick a driver.
if ($driver == null) {
echo $environment, ": no database is configured for that name\n";
} else {
echo $environment, ': pages with ', Deployment::page($driver), "\n";
}
}
Output
tests: pages with limit ? offset ?
production: pages with limit ? offset ?
reporting: pages with offset ? rows fetch next ? rows only
sandbox: no database is configured for that name