Core\Db\Connection::serverVersion
$connection->serverVersion(): stringSays which version the database is running.
Core\Db\Connection::serverVersion returns the version as text, in the database's own wording. PostgreSQL gives its server_version setting. MySQL and MariaDB give the banner they send when the connection opens. SQL Server gives three numbers with dots between them. SQLite has no server, so it gives the version of the database library this program is built with.
The database reports its version while the connection is opening, and the connection keeps that text. Reading it therefore runs no statement, and the result is the same every time. You can also read it while queryEach reads rows on the connection and no other statement can run.
The result is tainted string, because the server sends this text. Write tainted string on the variable that keeps it.
The text is never empty, and Novis never changes it. A version such as 14.10 (Debian 14.10-1) keeps the part in brackets, because that is what the server sent. A closed connection throws a LogicError.
The examples read the version, and take the main number out of it to see what the database can do. The last one stores the version beside a change to the schema.
Return value
Returns string. The version, as a tainted string, because the server sent it. It is never empty and never changed. A suffix that a distribution adds is part of it.
Errors
LogicErrorThe connection has been closed.
Changelog
| Version | Change |
|---|---|
0.0.6 | The output of a child process is tainted bytes. The version text of a database server and the name an LDAP server returns are tainted string. Another program or a server writes this text, so it may contain input from outside. |
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"]Read the version the database reported
<?nvs// Shows how to read the version of the database a connection is talking to.// The connection was told the version while it opened, so this costs no// statement.
var $db = Core\Db::connect('notes');
// The text is the database's own wording, for example `3.45.1`. Every database// reports a version, so it is never empty.var $version = $db->serverVersion();
// This prints `the database reported a version`.if (Core\Str::isEmpty($version)) { echo "the database reported nothing\n";} else { echo "the database reported a version\n";}
$db->close();the database reported a versionSee what the database can do
<?nvs// Shows how to read the main version number out of the text. A program tests// it before it uses something that older databases do not have.
var $db = Core\Db::connect('notes');
// The version begins with the main number and then a dot. `Core\Str::before`// gives the text in front of the first dot.tainted string $main = Core\Str::before($db->serverVersion(), '.') ?? '0';
// `as ?int` converts that text to a whole number. If that is not possible, the// result is `null`, and `?? 0` turns it into `0`.int $number = $main as ?int ?? 0;
// SQLite has been at version 3 for many years. This prints `the database is// new enough`.if ($number >= 3) { echo "the database is new enough\n";} else { echo "this program needs a newer database\n";}
$db->close();the database is new enoughRecord the version a change was made on
<?nvs// Shows a program that changes the schema and writes down which database// version it ran on. Later you can see where every change was made.
var $db = Core\Db::connect('notes');$db->execute('create table changes (id integer primary key, name text not null, server text not null)', []);
// The version goes into the row beside the name of the change.$db->execute('insert into changes (name, server) values (?, ?)', ['add the notes table', $db->serverVersion()]);$db->execute('create table notes (id integer primary key, text text not null)', []);
var $made = $db->query('select name from changes order by id', []);
// This prints `add the notes table`. The version is stored in the same row.foreach ($made->all() as Core\Db\Row $row) { echo $row->string('name') ?? '', "\n";}
$db->close();add the notes table