Database Operations

Learn about Zephyr's database operations

Zephyr currently supports the following types of database interactions:

  • Write.

  • Read.

  • Update.

On the guest level, there is no auth to handle so this is pretty straightforward as you don't have to grant any special permissions.

Write

In the Zephyr VM, or better in the Mercury abstraction of the VM implementation, a write operation translates to a SQL insert statement and it is performed by providing a constructed DatabaseDerive object to the EnvClient::put() function:

#[derive(DatabaseDerive, Clone)]
#[with_name("test")]
struct TestTable {
    hello: ScVal,
}

#[no_mangle]
pub extern "C" fn on_close() {
    let env = EnvClient::new();
    // define the message
    let table = TestTable {
        hello: message.clone(),
    };
    
    table.put(&env);
}

Read

Reading means returning an iterator (a vec currently) over DatabaseDerive elements, as many as the rows present in the requested table. It is done by simply calling EnvClient::read() while inferring the requested table type (which will automatically translate to reading from the corresponding table):

let rows = env.read::<TestTable>();
for row in rows {
    // ...
}

Update

Updating lets you update a selection of rows with a given row given the provided conditions:

let table = TestTable {
    hello: message1.clone(),
};

// note: message1 is an ScVal
env.update().column_equal_to_xdr("hello", &message).execute(&table)

In this snippet, all rows where the hello column equals to the message scval will be updated with the row defined by table.

Last updated