Core\Db\Column::isNullable
$column->isNullable(): boolSays whether a column may hold SQL NULL, which means the column has no value in that row.
A query result describes its columns before you read any rows. isNullable() returns true when the column may be empty, and false when it may not.
Every database driver returns true for every column. A query result does not include the not null setting of the table. Finding it needs a second query on the table, and the driver does not run one. So your program should treat every column as one that may be empty.
In plain words: read every value with a ? type, such as ?string or ?int, and choose what to show when there is nothing there.
The examples below print the description of a query first. Then they read a column that may be empty, and count the empty values in a table.
Return value
Returns bool. true for every column this driver describes, so treat every column as nullable. The typed methods of Core\Db\Row already do this, and each returns ?T. A database that sends the flag gives its value here.
Changelog
| Version | Change |
|---|---|
0.0.6 | queryEach and queryEachAs run a query and read its rows one at a time. runTransaction runs a function in a transaction, and Core\Db\Column->isNullable() checks a column. The old names were stream, streamAs, transaction and nullable. |
Examples
The examples below run with this configuration file.
# The `notes` database is kept in memory. It is deleted when the program ends.[db.notes]driver = "sqlite"path = ":memory:"
# The program may open the `notes` database.[capabilities.db]connect = ["notes"]Ask whether a column may be empty
<?nvs// Shows how to read the description of a query: the label of each column, and// whether that column may be empty.
var $db = Core\Db::connect('notes');
$db->execute('create table notes (id integer primary key, title text not null, note text)', []);
var $rows = $db->query('select id, title, note from notes', []);
// This prints `id may be empty`, `title may be empty` and `note may be empty`.// A query result carries no `not null` information, so every answer is `true`.foreach ($rows->columns() as Core\Db\Column $column) { if ($column->isNullable()) { echo $column->name(), ' may be empty', "\n"; } else { echo $column->name(), ' always has a value', "\n"; }}
$db->close();id may be emptytitle may be emptynote may be emptyPrint a record without its empty fields
<?nvs// Shows how to print one record and leave out the fields that have no value.// `isNullable()` says which columns need that check before any row is read.
var $db = Core\Db::connect('notes');
$db->execute('create table notes (title text not null, note text, due text)', []);$db->execute( 'insert into notes (title, note, due) values (?, ?, ?)', ['buy milk', null, '2026-01-09'],);
var $rows = $db->query('select title, note, due from notes', []);array<Core\Db\Column> $columns = $rows->columns();
// This prints `title: buy milk` and `due: 2026-01-09`. The `note` column has no// value in this row, so the line for it is left out.foreach ($rows as Core\Db\Row $row) { foreach ($columns as Core\Db\Column $column) { // `string()` reads the column as text and gives `null` when the column // has no value. A column that may not be empty needs no such check. ?tainted string $cell = $row->string($column->name()); if ($column->isNullable() && $cell == null) { continue; } echo $column->name(), ': ', $cell ?? '', "\n"; }}
$db->close();title: buy milkdue: 2026-01-09Count the missing values in a table
<?nvs// Shows how to find the gaps in a table. For every column that may be empty,// this counts the rows that have no value in it.
var $db = Core\Db::connect('notes');
$db->execute( 'create table notes (id integer primary key, title text not null, note text, due text)', [],);$db->execute('insert into notes (id, title, note, due) values (?, ?, ?, ?)', [1, 'buy milk', null, null]);$db->execute('insert into notes (id, title, note, due) values (?, ?, ?, ?)', [2, 'call back', 'after nine', null]);$db->execute('insert into notes (id, title, note, due) values (?, ?, ?, ?)', [3, 'book a table', null, '2026-01-09']);
var $rows = $db->query('select id, title, note, due from notes', []);array<Core\Db\Column> $columns = $rows->columns();
// Every key of this array is a column label, and every value is a count.array<uint> $missing = [];foreach ($columns as Core\Db\Column $start) { $missing[$start->name()] = 0;}
foreach ($rows as Core\Db\Row $row) { foreach ($columns as Core\Db\Column $column) { // A column that always has a value is not worth checking. This driver // says every column may be empty, so every column is checked here. if (!$column->isNullable()) { continue; } // `get()` gives `null` when the column has no value in this row. if ($row->get($column->name()) == null) { $missing[$column->name()] = $missing[$column->name()] + 1; } }}
// This prints `id 0`, `title 0`, `note 2` and `due 2`.foreach ($columns as Core\Db\Column $each) { echo $each->name(), ' ', $missing[$each->name()], "\n";}
$db->close();id 0title 0note 2due 2