[][src]Struct vdb::Db

pub struct Db { /* fields omitted */ }

Container for the database. Usually only one is used per application.

Examples

use vdb::{Db, Entry};
let mut db = Db::new("test-db");
let _row_id = db.add(vec![Entry::new_string("mundo", "world")]);

Methods

impl Db[src]

pub fn new(filename: &str) -> Db[src]

Create new database in memory. The file is not created until save() is called.

pub fn load(filename: &str) -> Result<Db, Box<dyn Error>>[src]

Load a database file from the filesystem under the subdirectory save/.

Errors

May return errors from external modules while opening the file or parsing the contents.

pub fn save(&self) -> Result<(), Box<dyn Error>>[src]

Save database under the subdirectory save/ with the same name it was opened or created with. The subdirectory save/ must exist.

pub fn db_string(v: &str) -> Data[src]

pub fn db_int(v: i32) -> Data[src]

pub fn db_datetime(v: &str) -> Result<Data, Box<dyn Error>>[src]

Parse &str into a DbDateTime. The format string is %Y-%m-%d %H:%M:%S.

pub fn add(&mut self, entries: Vec<Entry>) -> RowId[src]

Add a new row with multiple entries.

pub fn add_or_update_entry(&mut self, row_id: RowId, entry: Entry)[src]

Add a single entry to an existing row. An existing entry with the same name is overwritten.

pub fn add_entry(&mut self, row_id: RowId, entry: Entry)[src]

Add a single entry to an existing row. Does not check if entry exists.

pub fn delete_rows(&mut self, row_ids: &[RowId])[src]

Delete rows in the database

Examples

use vdb::{Db, Entry};
let mut db = Db::new("test-db");
let row_1 = db.add(vec![
        Entry::new_string("word", "cocina"),
        Entry::new_string("translation", "cuisine"),
        Entry::new_string("translation", "kitchen"),
]);
let row_2 = db.add(vec![
        Entry::new_string("word", "coche"),
        Entry::new_string("translation", "car"),
]);
let coche = db.find_first_row_id_by_value("word", &Db::db_string("coche"));
assert_eq!(coche, Some(row_2));
db.delete_rows(&[row_1, row_2]);
let no_coche = db.find_first_row_id_by_value("word", &Db::db_string("coche"));
assert_eq!(no_coche, None);

pub fn delete_entry_all(&mut self, name: &str)[src]

Delete all entries with this name in the whole database.

pub fn find_by_name(&self, name: &str) -> Vec<RowId>[src]

Return row_ids of entries where an entry with name "name" exists.

pub fn find_by_value(&self, name: &str, value: &Data) -> Vec<RowId>[src]

Return row_ids of entries that are exactly "value". For partial string matches, use Predicates.

pub fn find_first_row_id_by_name(&self, name: &str) -> Option<RowId>[src]

Return reference to first entry found in a given row.

pub fn find_first_row_id_by_value(
    &self,
    name: &str,
    value: &Data
) -> Option<RowId>
[src]

Return reference to first entry found in a given row.

pub fn get_first_entry(&self, row_id: RowId, name: &str) -> Option<&Entry>[src]

Return reference to first entry found in a given row.

pub fn get_first_entry_mut(
    &mut self,
    row_id: RowId,
    name: &str
) -> Option<&mut Entry>
[src]

Return mutable reference to an entry in a given row.

pub fn select_row_ids(
    &self,
    predicates: &[Predicate],
    max_results: Option<usize>
) -> Vec<RowId>
[src]

Returns all rows if no predicates are given. The first predicate is evaluated first and should have high selectivity, i. e. evaluate to a small number of rows, to improve execution time. The number of results can be limited with Some(max_results)

Examples

// Like SQL "select name, value from testdb where name='coche' limit 15"
use vdb::{Data, Db, Entry, Predicate, RowId};
let mut db = Db::new("test-db");
let _id = db.add(vec![
    Entry {
        name: String::from("set"),
        value: Db::db_string("es-en"),
    },
    Entry {
        name: String::from("name"),
        value: Db::db_string("coche"),
    },
    Entry {
        name: String::from("value"),
        value: Db::db_string("car"),
    },
]);
let predicates = vec![Predicate::new_equal_string("name", "coche")];
let entries = vec![String::from("name"), String::from("value")];
let row_ids = db.select_row_ids(&predicates, Some(15));
assert_eq!(row_ids, [RowId(1)]);
assert_eq!(db.entries_from_row_ids(&row_ids, entries)[0][0], Entry::new_string("name", "coche"));

See also select()

pub fn find_all_row_ids(&self) -> Vec<RowId>[src]

Returns all rows in the database

pub fn entries_from_row_ids(
    &self,
    row_ids: &[RowId],
    names: Vec<String>
) -> Vec<Vec<Entry>>
[src]

Returns entries for given row_ids. The order of the entries in each row is not guaranteed.

Trait Implementations

impl PartialEq<Db> for Db[src]

impl Clone for Db[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for Db[src]

impl Serialize for Db[src]

impl<'de> Deserialize<'de> for Db[src]

Auto Trait Implementations

impl Unpin for Db

impl Sync for Db

impl Send for Db

impl RefUnwindSafe for Db

impl UnwindSafe for Db

Blanket Implementations

impl<T> From<T> for T[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: Deserialize<'de>, 
[src]