Crates.io | basex |
lib.rs | basex |
version | 0.7.0 |
source | src |
created_at | 2021-08-23 12:43:35.90829 |
updated_at | 2021-12-16 20:10:34.749968 |
description | A client library for BaseX XQuery databases. |
homepage | |
repository | https://github.com/RomanHodulak/basex-rs |
max_upload_size | |
id | 441165 |
size | 131,254 |
This library is a client implementation of the open-source XML database server and XQuery processor BaseX.
Compatible with versions 8.x and 9.x.
Add the library to the list of dependencies in your Cargo.toml
like so:
[dependencies]
basex = "0.7.0"
First, you need to have BaseX server up and running. If you want to try it out, you can do it right away using docker.
docker run -p 1984:1984 basex/basexhttp:9.5.2
Every example can be run with this server configuration.
Before you can do anything with the database server, you need to establish connection and authorize. Typically, you do this by calling Client::connect
. If you get Ok result, you get the instance of the Client
. Having its instance guarantees to have an open session with the server.
let client = Client::connect("localhost", 1984, "admin", "admin")?;
You can now send commands.
To run a query, you need to open a database.
Creating a database also opens it. Follow the create call with either without_input
or with_input
to optionally specify initial XML resource.
let info = client.create("coolbase")?.with_input(&mut xml)?;
Use Client::execute
with command OPEN [name]
.
let (client, info) = client.execute("OPEN coolbase")?.close()?;
Aside from running commands, you can run queries using XQuery syntax which is the most important use-case.
Client::query
. This puts the session into query mode.Query::bind
.Query::execute
.Query::close
.The following example creates database "lambada" with initial XML resource and counts all first-level child nodes of the Root
node.
use basex::{Client, ClientError};
use std::io::Read;
fn main() -> Result<(), ClientError> {
let mut client = Client::connect("localhost", 1984, "admin", "admin")?;
let info = client.create("lambada")?
.with_input("<Root><Text/><Lala/><Papa/></Root>")?;
assert!(info.starts_with("Database 'lambada' created"));
let query = client.query("count(/Root/*)")?;
let mut result = String::new();
let mut response = query.execute()?;
response.read_to_string(&mut result)?;
assert_eq!(result, "3");
let mut query = response.close()?;
query.close()?;
Ok(())
}
The library is licensed under ISC.