Skip to content

Core\Db\Row::uuid

$row->uuid(string $name): ?Core\Uuid$row->uuid(
    string $name,
): ?Core\Uuid

Reads one column of a row as an id.

Core\Db\Row::uuid takes the name of a column and returns a Core\Uuid. That is a 128-bit identifier, the kind a table carries instead of a counted number. The column has to be declared as one: UUID in PostgreSQL and in MariaDB 10.7 and later, uniqueidentifier in SQL Server, and uuid in SQLite.

The result is null when the column has no value in this row, so the type you get back is ?Core\Uuid. Test it for null before you use it.

The method throws a LogicError when the row has no column with that name, and when the column is not one of those types. A text column holding the same thirty-six characters is still text, and is read with Core\Db\Row::string. MySQL stores an id as BINARY(16), which is read with Core\Db\Row::bytes.

The examples below read the id of an order, read a column that may have no value, and look up rows by an id that arrived as text.

Parameters

  • $namestringThe column label, as the server described it.

Return value

Returns ?Core\Uuid. The identifier, or null for a NULL column.

Errors

  • LogicErrorThe row has no column with that name, or the column is not a UUID type. A BINARY(16) is bytes, and a UUID stored as text is a string.

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"]

Read the id of an order

<?nvs
// Shows how to read an id column out of a row.
var $db = Core\Db::connect('notes');
$db->execute('create table orders (id uuid not null, label text not null)', []);
$db->execute(
'insert into orders (id, label) values (?, ?), (?, ?)',
['3f2504e0-4f89-41d3-9a0c-0305e82c3301', 'one tea', 'b8c1a0de-2f7a-4d3b-8a91-6f1f2c7d4455', 'two coffees'],
);
// `uuid` reads the named column as a `Core\Uuid`, and `toString` writes that id
// out in the usual form. This prints one line for each order.
foreach ($db->query('select id, label from orders order by label', []) as Core\Db\Row $row) {
?Core\Uuid $id = $row->uuid('id');
if ($id != null) {
echo $row->string('label') ?? '', ': ', $id->toString(), "\n";
}
}
$db->close();
Output
one tea: 3f2504e0-4f89-41d3-9a0c-0305e82c3301
two coffees: b8c1a0de-2f7a-4d3b-8a91-6f1f2c7d4455

A column that has no value

<?nvs
// Shows what you get when the column has no value in this row.
var $db = Core\Db::connect('notes');
$db->execute('create table orders (label text not null, paid_by uuid)', []);
$db->execute(
'insert into orders (label, paid_by) values (?, ?), (?, ?)',
['one tea', '3f2504e0-4f89-41d3-9a0c-0305e82c3301', 'two coffees', null],
);
// The result is `null` when the column has no value in this row. The type you
// get back is `?Core\Uuid`, so you test it for `null` before you use it.
foreach ($db->query('select label, paid_by from orders order by label', []) as Core\Db\Row $row) {
?Core\Uuid $payer = $row->uuid('paid_by');
if ($payer == null) {
echo $row->string('label') ?? '', ': nobody has paid', "\n";
} else {
echo $row->string('label') ?? '', ': paid by ', $payer->toString(), "\n";
}
}
$db->close();
Output
one tea: paid by 3f2504e0-4f89-41d3-9a0c-0305e82c3301
two coffees: nobody has paid

Look up a row by the id a request carried

<?nvs
// Shows how to look up rows by an id that arrived as text, and how to read the
// id of every row that was found.
var $db = Core\Db::connect('notes');
$db->execute('create table orders (id uuid not null, customer uuid not null, label text not null)', []);
$db->execute(
'insert into orders (id, customer, label) values (?, ?, ?), (?, ?, ?), (?, ?, ?)',
[
'3f2504e0-4f89-41d3-9a0c-0305e82c3301',
'9f8e7d6c-5b4a-4938-8271-6a5b4c3d2e1f',
'one tea',
'b8c1a0de-2f7a-4d3b-8a91-6f1f2c7d4455',
'9f8e7d6c-5b4a-4938-8271-6a5b4c3d2e1f',
'two coffees',
'c4d5e6f7-1a2b-4c3d-9e8f-7a6b5c4d3e2f',
'11112222-3333-4444-8555-666677778888',
'a sandwich',
],
);
// `Core\Uuid::tryParse` returns `null` when the text is not an id. Checking it
// here means the query below only ever sees an id.
string $asked = '9f8e7d6c-5b4a-4938-8271-6a5b4c3d2e1f';
?Core\Uuid $customer = Core\Uuid::tryParse($asked);
if ($customer == null) {
echo "that is not an id\n";
} else {
// This prints the two orders of that customer, and not the sandwich.
foreach ($db->query('select id, label from orders where customer = ? order by label', [$customer->toString()]) as Core\Db\Row $row) {
?Core\Uuid $id = $row->uuid('id');
if ($id != null) {
echo $row->string('label') ?? '', ' is order ', $id->toString(), "\n";
}
}
}
$db->close();
Output
one tea is order 3f2504e0-4f89-41d3-9a0c-0305e82c3301
two coffees is order b8c1a0de-2f7a-4d3b-8a91-6f1f2c7d4455