Core\Db\DbError
The error a database throws when it does not accept a statement.
A key that is already taken, an empty required column and a deadlock all throw this one error. It works the same with every database. Its kind property names the reason from one fixed list, so the same code handles PostgreSQL, MySQL and SQLite. Your program can then act on the reason. A duplicate email address needs a message for the user, and a deadlock can be tried again. The sql property is the statement your program wrote.
The server's own text: message never contains text from the database server, because that text can include a value your program sent. The server's text is in serverMessage. It is null when the statement may have bound a secret value. serverMessage and constraint are a tainted string. Tainted means the text came from outside your program, so check or escape it before you use it.
Good to know: a mistake in the call itself is a LogicError. An example is a value the driver cannot send.
Examples
The examples below run with this configuration file.
# The `schema` database is kept in memory. It is deleted when the program ends.[db.schema]driver = "sqlite"path = ":memory:"
# The program may open the `schema` database.[capabilities.db]connect = ["schema"]A row that is already there
<?nvs// Two people sign up with the same email address. The database is the thing// that knows the second one is a duplicate, and it says no. That refusal// arrives as this error, and the program decides what to tell the person.
use Core\Db;use Core\Db\DbError;
var $db = Db::connect('schema');$db->execute('create table people (email text not null primary key)', []);
array<string> $signups = ['ada@example.test', 'grace@example.test', 'ada@example.test'];foreach ($signups as string $email) { try { $db->execute('insert into people (email) values (?)', [$email]); echo 'welcome ', $email, "\n"; } catch (DbError $refused) { if ($refused->kind == DbError::Kind::UniqueViolation) { echo 'already signed up: ', $email, "\n"; } else { echo 'could not sign up ', $email, "\n"; } }}
$db->execute('drop table people', []);welcome ada@example.testwelcome grace@example.testalready signed up: ada@example.testWhat the refusal tells you
<?nvs// When the database rejects a statement, it throws a `DbError`. Its `kind` says// which rule the statement broke, and its `sql` is the statement itself.// So one `catch` block can log both.
use Core\Db;use Core\Db\DbError;
var $db = Db::connect('schema');$db->execute('create table notes (id integer primary key, title text not null)', []);$db->execute('insert into notes (id, title) values (?, ?)', [1, 'first']);
try { $db->execute('insert into notes (id, title) values (?, ?)', [1, 'again']);} catch (DbError $refused) { if ($refused->kind == DbError::Kind::UniqueViolation) { echo 'refused: that key is already taken', "\n"; } echo 'the statement it refused: ', $refused->sql, "\n";}
try { $db->execute('insert into notes (id, title) values (?, ?)', [2, null]);} catch (DbError $refused) { if ($refused->kind == DbError::Kind::NotNullViolation) { echo 'refused: that column may not be left empty', "\n"; } echo 'the statement it refused: ', $refused->sql, "\n";}
$db->execute('drop table notes', []);refused: that key is already takenthe statement it refused: insert into notes (id, title) values (?, ?)refused: that column may not be left emptythe statement it refused: insert into notes (id, title) values (?, ?)A signup that says what went wrong
<?nvs// A signup that tells the person what went wrong. If the address is already// registered, `Signup::add` returns a message the person can act on.// It throws every other `DbError` again, for the caller to handle.
use Core\Db;use Core\Db\Connection;use Core\Db\DbError;
final class Signup{ public static function add(Connection $db, string $email): string { try { $db->execute('insert into accounts (email) values (?)', [$email]); return 'welcome'; } catch (DbError $refused) { if ($refused->kind == DbError::Kind::UniqueViolation) { return 'that address is already registered'; } throw $refused; } }}
var $db = Db::connect('schema');$db->execute('create table accounts (email text not null primary key)', []);
echo Signup::add($db, 'ada@example.test'), "\n";echo Signup::add($db, 'ada@example.test'), "\n";
$db->execute('drop table accounts', []);welcomethat address is already registered