Skip to content

Core\Db\Column::type

$column->type(): Core\Db\Column::Type

Says what kind of value a column holds, as one of the Core\Db\Column::Type cases.

A query result describes its columns before you read any rows. type() returns the case for one column: Int, Text, Float, Date and eleven more. The case comes from what the column was declared as in the table. It does not come from the value in any one row.

Every database gives you the same set of cases, so your program can compare against Core\Db\Column::Type::Int instead of against a name such as int4, INTEGER or bigint. Those are three databases' words for the same thing.

Good to know: a column the query works out, such as price * 2, was never declared anywhere, so its case is Other.

Return value

Returns Core\Db\Column::Type. The case. It describes the column, not the value you get when you read it. For example, a JSON column and a TEXT column both read as tainted string, and this case tells them apart. A type with no matching Novis type is Other. This includes every array and, on PostgreSQL, every ENUM.

Examples

The examples below run with this configuration file.

nvs.toml
# 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"]

See what each column holds

<?nvs
// Shows what kind of value each column of a query holds. `type()` returns one
// of the `Core\Db\Column::Type` cases, and the same case comes back on every
// database.
var $db = Core\Db::connect('notes');
$db->execute(
'create table notes (id integer primary key, title text not null, weight real, paidAt datetime)',
[],
);
var $rows = $db->query('select id, title, weight, paidAt from notes', []);
// This prints `id Int`, `title Text`, `weight Float` and `paidAt DateTime`.
foreach ($rows->columns() as Core\Db\Column $column) {
string $kind = match ($column->type()) {
Core\Db\Column::Type::Int => 'Int',
Core\Db\Column::Type::Text => 'Text',
Core\Db\Column::Type::Float => 'Float',
Core\Db\Column::Type::DateTime => 'DateTime',
default => 'something else',
};
echo $column->name(), ' ', $kind, "\n";
}
$db->close();
Output
id Int
title Text
weight Float
paidAt DateTime

A column the query works out

<?nvs
// Shows the case a computed column gets. A column the query works out was
// never declared in a table, so `type()` returns `Other`.
var $db = Core\Db::connect('notes');
$db->execute('create table notes (id integer primary key, price real not null)', []);
var $rows = $db->query('select price, price * 2 as total from notes', []);
// This prints `price is a column of the table` and `total is worked out`.
foreach ($rows->columns() as Core\Db\Column $column) {
if ($column->type() == Core\Db\Column::Type::Other) {
echo $column->name(), " is worked out\n";
} else {
echo $column->name(), " is a column of the table\n";
}
}
$db->close();
Output
price is a column of the table
total is worked out

Line up the numbers in a report

<?nvs
// Shows how a report decides how to print each column before it reads a single
// row. Numbers go to the right of their space, and text goes to the left.
var $db = Core\Db::connect('notes');
$db->execute('create table notes (title text not null, amount integer not null)', []);
$db->execute('insert into notes (title, amount) values (?, ?)', ['buy milk', 3]);
$db->execute('insert into notes (title, amount) values (?, ?)', ['call the plumber', 12]);
var $rows = $db->query('select title, amount from notes order by amount', []);
array<Core\Db\Column> $columns = $rows->columns();
// This prints the title in 20 places and the amount in 5 places after it.
foreach ($rows as Core\Db\Row $row) {
tainted string $line = '';
foreach ($columns as Core\Db\Column $column) {
tainted string $cell = $row->get($column->name()) as ?string ?? '';
if ($column->type() == Core\Db\Column::Type::Int) {
$line = $line . Core\Str::padStart($cell, 5);
} else {
$line = $line . Core\Str::padEnd($cell, 20);
}
}
echo $line, "|\n";
}
$db->close();
Output
buy milk 3|
call the plumber 12|