Clients
This page describes how to communicate with BaseX from other programming languages.
To address a server from within XQuery, use the Client Module instead.
You can use the following light-weight language bindings to connect to a running BaseX server instance, execute database commands and evaluate XQuery expressions (see Usage below). The underlying protocol is described in the Server Protocol.
Available Bindings
- Java: The default implementation
- C++: contributed by Karim Salama (based on SDL2_net)
- C++: contributed by Jean-Marc Mercier (based on libboost)
- C++: contributed by Ben Engbers (built with CMake)
- C#, contributed by the BaseX Team and Martín Ferrari
- C, contributed by the BaseX Team
- Golang: contributed by Christian Baune
- Erlang: contributed by Zachary Dean
- node.js: contributed by Andy Bunce
- Perl, contributed by the BaseX Team
- PHP: updated by James Ball
- Python: contributed by Hiroaki Itoh
- Python, using BaseX REST services: contributed by Luca Lianas
- R: contributed by Ben Engbers
- R: package by Ben Engbers
- Raku: contributed by Wayland
- Rust
- Ruby, contributed by the BaseX Team
- V, contributed by Erik Peterson
The bindings in the BaseX repository support the authentication of Version 13. Other bindings may need to be updated (see below).
Usage
Commands
Database commands are sent to the server with the execute() function of a session:
- Create a session with hostname, port, username and password.
- Call
execute()with a database command. The result is returned as a string. If an error occurs, an exception is thrown. - Optionally, call
info()to get information on the executed command. - Continue with step 2, or close the session with
close().
Queries
The query() function of a session returns a query instance, which can be used to bind external variables and evaluate a query iteratively:
- Create a session, and call
query()with an XQuery expression. - Optionally, bind variables with
bind(), and a context value withcontext(). - Iterate through the results with
more()andnext(), or callexecute()to get the whole result at once. - Optionally, call
info()to get information on query evaluation, oroptions()to get the serialization parameters. - Close the query with
close().
The following PHP example is taken from our repository:
<?php
include_once 'load.php';
use BaseXClient\BaseXException;
use BaseXClient\Session;
try {
// create session
$session = new Session("localhost", 1984, "admin", "...");
try {
// create query instance, bind variable, print result
$query = $session->query('declare variable $name external; for $i in 1 to 10 return element { $name } { $i }');
$query->bind("name", "number");
print $query->execute()."\n";
$query->close();
} catch (BaseXException $e) {
print $e->getMessage();
}
// close session
$session->close();
} catch (BaseXException $e) {
print $e->getMessage();
}
Authentication
Updated: Digest replaced with salted authentication.
Removed: Support for digest and cram-md5 authentication, and the outdated clients.
With Version 13.0, the login handshake has changed (see here for more details). The bindings in the BaseX repository have been updated.
Clients of versions 8 to 12 can no longer connect to the server, and clients of Version 13 cannot connect to older servers.
Implementation Files
Many implementations contain the following files:
BaseXClientcontains the code for creating a session, sending and executing commands and receiving results. An innerQueryclass facilitates the binding of external variables and iterative query evaluation.Exampledemonstrates how to send database commands.QueryExampleshows you how to evaluate queries iteratively.QueryBindExampleshows you how to bind a variable to your query and evaluate the query iteratively.CreateExampleshows how new databases can be created by using streams.AddExampleshows how documents can be added to a database by using streams.
Writing a New Binding
A new binding can be written in a few hundred lines of code:
- The byte exchange is described in the Server Protocol. The Java client serves as reference implementation.
\00and\FFbytes must be escaped in both directions. To check this, store a binary resource with all 256 byte values and compare it with the returned result.- The state of a query (id, cached results) must be kept in the query instance, so that multiple queries can be evaluated at the same time.
- Commands and queries return errors in different formats.
You are welcome to contribute your binding: Create a pull request, or send us a link to your repository.
Changelog
Version 13.0- Updated: Digest replaced with salted authentication.
- Removed: Support for digest and cram-md5 authentication, and the outdated clients.
- Updated: The
replaceandstorefunctions have been renamed toputandputBinary.
- Updated: cram-md5 replaced with digest authentication.
- Added:
context()function for queries.