Skip to content

Core\Db\Row::bytes

$row->bytes(string $name): ?tainted bytes$row->bytes(
    string $name,
): ?tainted bytes

Reads one column of a row as bytes.

Core\Db\Row::bytes takes the name of a column and returns the octets that are stored in it. It is the reader for a BINARY, BLOB or BYTEA column, which is where a picture, a document or a hash is kept. That data has no encoding, so it is not text.

bytes and string are separate types in Novis, and this method never converts between them. Read a text column with Core\Db\Row::string.

The result is null when the column has no value in this row, so the type you get back is ?bytes. The method throws a LogicError when the row has no column with that name, and when the column holds text rather than bytes.

The examples below measure a picture that is stored in a row, read a column that may have no value, and find out which file type a stored picture is.

Parameters

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

Return value

Returns ?tainted bytes. The bytes as tainted bytes, or null for a NULL column.

Errors

  • LogicErrorThe row has no column with that name, or the column is text. Text and bytes are separate types, and this method reads only bytes.

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 a picture out of a row

<?nvs
// Shows how to read binary data, such as a picture, out of a row.
var $db = Core\Db::connect('notes');
$db->execute('create table pictures (name text not null, body blob not null)', []);
$db->execute('insert into pictures (name, body) values (?, ?), (?, ?)', ['logo', 'GIF89a' as bytes, 'banner', 'GIF89a-wide' as bytes]);
// `bytes` reads the named column as octets, and `Core\Bytes::length` counts
// them. This prints `banner: 11` and `logo: 6`.
foreach ($db->query('select name, body from pictures order by name', []) as Core\Db\Row $row) {
tainted bytes $body = $row->bytes('body') ?? '' as bytes;
echo $row->string('name') ?? '', ': ', Core\Bytes::length($body), "\n";
}
$db->close();
Output
banner: 11
logo: 6

A column that has no value

<?nvs
// Shows what you get when the binary column has no value in this row.
var $db = Core\Db::connect('notes');
$db->execute('create table people (name text not null, picture blob)', []);
$db->execute('insert into people (name, picture) values (?, ?), (?, ?)', ['Ana', 'GIF89a' as bytes, 'Bo', null]);
// `bytes` returns `null` for a column with no value, so your program can
// choose what to show instead. This prints `Ana: 6` and `Bo: no picture`.
foreach ($db->query('select name, picture from people order by name', []) as Core\Db\Row $row) {
?tainted bytes $picture = $row->bytes('picture');
echo $row->string('name') ?? '', ': ';
if ($picture != null) {
echo Core\Bytes::length($picture), "\n";
} else {
echo "no picture\n";
}
}
$db->close();
Output
Ana: 6
Bo: no picture

Find out which file type a picture is

<?nvs
// Shows how to name the file type of a stored picture. A program does this
// before it sends the file, because the type is not kept in the table.
var $db = Core\Db::connect('notes');
$db->execute('create table uploads (name text not null, body blob not null)', []);
// Every file type starts with a few fixed bytes. `Core\Encoding::fromHex`
// writes those bytes, because most of them are not text.
bytes $png = Core\Encoding::fromHex('89504e470d0a1a0a');
bytes $gif = 'GIF89a' as bytes;
$db->execute('insert into uploads (name, body) values (?, ?), (?, ?), (?, ?)', ['a', $png, 'b', $gif, 'c', 'hello' as bytes]);
// `bytes` reads the column as octets, and `Core\Bytes::startsWith` tests the
// first of them. This prints `a: png`, `b: gif` and `c: unknown`.
foreach ($db->query('select name, body from uploads order by name', []) as Core\Db\Row $row) {
tainted bytes $body = $row->bytes('body') ?? '' as bytes;
string $type = 'unknown';
if (Core\Bytes::startsWith($body, $png)) {
$type = 'png';
}
if (Core\Bytes::startsWith($body, $gif)) {
$type = 'gif';
}
echo $row->string('name') ?? '', ': ', $type, "\n";
}
$db->close();
Output
a: png
b: gif
c: unknown