Struct Client

Source
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 Client<TcpStream>

Source

pub fn connect( host: &str, port: u16, user: &str, password: &str, ) -> Result<Client<TcpStream>>

Connects and authenticates to BaseX server using TCP stream.

§Example
let client = Client::connect("localhost", 1984, "admin", "admin")?;
Source§

impl<T> Client<T>
where T: DatabaseStream,

Source

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);
Source

pub fn execute(self, command: &str) -> Result<Response<T>>

Executes a server command including arguments.

Returns response which can be read using the Read trait.

§Example
let mut client = Client::connect("localhost", 1984, "admin", "admin")?;
let mut list = String::new();
client.execute("LIST")?.read_to_string(&mut list)?;
println!("{}", list);
Source

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()?;
Source

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>")?;
Source

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[..])?;
Source

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, use Client::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())?;
Source

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§

Source§

impl<T, HasInfo> Borrow<Client<T>> for Query<T, HasInfo>
where T: DatabaseStream,

Source§

fn borrow(&self) -> &Client<T>

Immutably borrows from an owned value. Read more
Source§

impl<T: DatabaseStream> Borrow<Connection<T, Authenticated>> for Client<T>

Source§

fn borrow(&self) -> &Connection<T, Authenticated>

Immutably borrows from an owned value. Read more
Source§

impl<T, HasInfo> BorrowMut<Client<T>> for Query<T, HasInfo>
where T: DatabaseStream,

Source§

fn borrow_mut(&mut self) -> &mut Client<T>

Mutably borrows from an owned value. Read more
Source§

impl<T: DatabaseStream> BorrowMut<Connection<T, Authenticated>> for Client<T>

Source§

fn borrow_mut(&mut self) -> &mut Connection<T, Authenticated>

Mutably borrows from an owned value. Read more
Source§

impl<T: DatabaseStream> Clone for Client<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Client<T>
where T: DatabaseStream + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Client<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Client<T>
where T: RefUnwindSafe,

§

impl<T> Send for Client<T>
where T: Send,

§

impl<T> Sync for Client<T>
where T: Sync,

§

impl<T> Unpin for Client<T>
where T: Unpin,

§

impl<T> UnwindSafe for Client<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.