pub struct Client<T>where
T: DatabaseStream,{
connection: Connection<T, Authenticated>,
}
Expand description
Represents an interface to communicate with the BaseX server. Its main purpose is to send database commands and create queries.
Start by connecting to the database using Client::connect
.
§Example
let mut client = Client::connect("localhost", 1984, "admin", "admin")?;
let info = client.create("a45d766")?
.with_input("<Root><Text></Text><Lala></Lala><Papa></Papa></Root>")?;
assert!(info.starts_with("Database 'a45d766' created"));
let query = client.query("count(/Root/*)")?.without_info()?;
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()?;
Fields§
§connection: Connection<T, Authenticated>
Implementations§
Source§impl<T> Client<T>where
T: DatabaseStream,
impl<T> Client<T>where
T: DatabaseStream,
Sourcepub fn new(connection: Connection<T, Authenticated>) -> Self
pub fn new(connection: Connection<T, Authenticated>) -> Self
Returns new client instance with the given connection bound to it. Works only with authenticated connections.
Typically, you only need to use this method when using a custom connection. It is used heavily in tests, for
example. For regular usage, refer to the Client::connect
method.
§Example
let stream = TcpStream::connect("localhost:1984")?;
let connection = Connection::new(stream).authenticate("admin", "admin")?;
let client = Client::new(connection);
Sourcepub fn create(&mut self, name: &str) -> Result<CommandWithOptionalInput<'_, T>>
pub fn create(&mut self, name: &str) -> Result<CommandWithOptionalInput<'_, T>>
Creates a new database with the specified name
and, optionally, an initial input
and opens it.
- Overwrites existing database with the same
name
. - The
name
must be valid database name. - The
input
is a stream with valid XML. - More options can be controlled by setting Create Options
§Example
let mut client = Client::connect("localhost", 1984, "admin", "admin")?;
client.create("boy_sminem")?.with_input("<wojak pink_index=\"69\"></wojak>")?;
client.create("bogdanoff")?.without_input()?;
Sourcepub fn replace<'a>(
&mut self,
path: &str,
input: impl AsResource<'a>,
) -> Result<String>
pub fn replace<'a>( &mut self, path: &str, input: impl AsResource<'a>, ) -> Result<String>
Replaces resources in the currently opened database, addressed by path
, with the XML document read from
input
, or adds new documents if no resource exists at the specified path.
§Example
let mut client = Client::connect("localhost", 1984, "admin", "admin")?;
client.create("bell")?.without_input()?;
client.replace("bogdanoff", "<wojak pink_index=\"69\"></wojak>")?;
Sourcepub fn store<'a>(
&mut self,
path: &str,
input: impl AsResource<'a>,
) -> Result<String>
pub fn store<'a>( &mut self, path: &str, input: impl AsResource<'a>, ) -> Result<String>
Stores a binary file from input
in the currently opened database under path
. Overwrites existing resource.
§Example
let mut client = Client::connect("localhost", 1984, "admin", "admin")?;
let mut blob = [0 as u8, 1, 2, 3];
client.create("asylum")?.without_input()?;
client.store("bogdanoff", &mut &blob[..])?;
Sourcepub fn add<'a>(
&mut self,
path: &str,
input: impl AsResource<'a>,
) -> Result<String>
pub fn add<'a>( &mut self, path: &str, input: impl AsResource<'a>, ) -> Result<String>
Adds an XML resource to the currently opened database under the specified path
.
- Keeps multiple documents with the same
path
. If this is unwanted, useClient::replace
. - On the server-side if the stream is too large to be added in one go, its data structures will be cached to
disk first. Caching can be enforced by turning the
ADDCACHE
option on. - The
input
is a stream with valid XML.
§Example
let mut client = Client::connect("localhost", 1984, "admin", "admin")?;
client.create("taurus")?.without_input()?;
client.add("bogdanoff", &mut "<wojak pink_index=\"69\"></wojak>".as_bytes())?;
Sourcepub fn query<'a, R: AsResource<'a>>(
self,
query: R,
) -> Result<QueryWithOptionalInfo<'a, T, R>>
pub fn query<'a, R: AsResource<'a>>( self, query: R, ) -> Result<QueryWithOptionalInfo<'a, T, R>>
Creates a new query
from given XQuery code.
You then need to make a statement about collecting compiler info using either with_info
or without_info
.
§Example
let mut client = Client::connect("localhost", 1984, "admin", "admin")?;
let info = client.create("triangle")?
.with_input("<polygon><line></line><line></line><line></line></polygon>")?;
assert!(info.starts_with("Database 'triangle' created"));
let query = client.query("count(/polygon/*)")?.without_info()?;
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()?;