Struct basex::client::client::Client [−][src]
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
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);
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()?;
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>")?;
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[..])?;
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())?;
pub 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()?;
Trait Implementations
fn borrow(&self) -> &Connection<T, Authenticated>ⓘNotable traits for Connection<T, State>impl<T, State> Read for Connection<T, State> where
T: DatabaseStream,
fn borrow(&self) -> &Connection<T, Authenticated>ⓘNotable traits for Connection<T, State>impl<T, State> Read for Connection<T, State> where
T: DatabaseStream,
impl<T, State> Read for Connection<T, State> where
T: DatabaseStream,
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
fn borrow_mut(&mut self) -> &mut Connection<T, Authenticated>ⓘNotable traits for Connection<T, State>impl<T, State> Read for Connection<T, State> where
T: DatabaseStream,
fn borrow_mut(&mut self) -> &mut Connection<T, Authenticated>ⓘNotable traits for Connection<T, State>impl<T, State> Read for Connection<T, State> where
T: DatabaseStream,
impl<T, State> Read for Connection<T, State> where
T: DatabaseStream,
Mutably borrows from an owned value. Read more
Auto Trait Implementations
impl<T> RefUnwindSafe for Client<T> where
T: RefUnwindSafe,
impl<T> UnwindSafe for Client<T> where
T: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more