[−][src]Struct vdb::Db
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_row(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(&mut self) -> Result<(), Box<dyn Error>>
[src]
Save database under the subdirectory save/
with the same name it was open
ed or create
d
with. The subdirectory save/
must exist.
pub fn get_name(&self) -> String
[src]
Returns the filename of the database
pub fn db_string(v: &str) -> Data
[src]
Returns a new Data::DbString
pub fn db_i32(v: i32) -> Data
[src]
Returns a new Data::DbI32
pub fn find_first_i32(&self, name: &str) -> Option<i32>
[src]
Find a i32 by name
use vdb::{Db, Entry}; let mut db = Db::new("test-db"); let name = "yoyo"; let value = 7; let _row_id = db.add_row(vec![Entry::new_i32(name, value)]); assert_eq!(db.find_first_i32(name), Some(value));
pub fn find_first_string(&self, name: &str) -> Option<String>
[src]
Find a string by name
use vdb::{Db, Entry}; let mut db = Db::new("test-db"); let name = "yoyo"; let value = "lila"; let _row_id = db.add_row(vec![Entry::new_string(name, value)]); assert_eq!(db.find_first_string(name), Some(value.to_string()));
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_i32(&mut self, name: &str, value: i32) -> RowId
[src]
Add a new row with one i32
pub fn add_string(&mut self, name: &str, value: &str) -> RowId
[src]
Add a new row with one string
pub fn add_row(&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, new_entry: Entry)
[src]
Add a single entry to an existing row. An existing entry with the same name is overwritten. If multiple entries with the same name exist, they will be overwritten.
pub fn remove_by_name(&mut self, row_id: RowId, name: &str)
[src]
Removes all entries with name 'name' and row 'row_id'. Does not delete the whole row and leaves entries with other names.
pub fn remove_by_row_id(&mut self, row_id: RowId)
[src]
Removes all entries with row 'row_id'
pub fn add_row_id_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_row(vec![ Entry::new_string("word", "cocina"), Entry::new_string("translation", "cuisine"), Entry::new_string("translation", "kitchen"), ]); let row_2 = db.add_row(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. Does not delete all rows. Deletes matching entries in the row. The row will be kept if there are entries left, otherwise deleted.
pub fn find_row_ids_by_name(&self, name: &str) -> Vec<RowId>
[src]
Return row_ids of entries where an entry with name "name" exists.
pub fn find_row_ids_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]
&self,
name: &str,
value: &Data
) -> Option<RowId>
Return reference to first entry found in a given row.
pub fn find_first_entry_by_name(
&self,
row_id: RowId,
name: &str
) -> Option<Entry>
[src]
&self,
row_id: RowId,
name: &str
) -> Option<Entry>
Return reference to first entry found in a given row.
pub fn find_by_predicate(&self, predicate: &Predicate) -> Vec<RowId>
[src]
pub fn find_row_ids_by_predicate(
&self,
predicates: &[Predicate],
max_results: Option<usize>
) -> Vec<RowId>
[src]
&self,
predicates: &[Predicate],
max_results: Option<usize>
) -> Vec<RowId>
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_row(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 row_ids = db.find_row_ids_by_predicate(&predicates, Some(15)); assert_eq!(row_ids, [RowId(1)]); assert_eq!(db.entries_from_row_ids(&row_ids, &["name", "value"])[0][0], Entry::new_string("name", "coche"));
See also find_entries_by_predicate()
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: &[&str]
) -> Vec<Vec<Entry>>
[src]
&self,
row_ids: &[RowId],
names: &[&str]
) -> Vec<Vec<Entry>>
Returns entries for given row_ids.
Trait Implementations
impl Clone for Db
[src]
fn clone(&self) -> Db
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl PartialEq<Db> for Db
[src]
impl Debug for Db
[src]
impl Serialize for Db
[src]
fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error> where
__S: Serializer,
[src]
__S: Serializer,
impl<'de> Deserialize<'de> for Db
[src]
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
[src]
__D: Deserializer<'de>,
Auto Trait Implementations
Blanket Implementations
impl<T> From<T> for T
[src]
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> DeserializeOwned for T where
T: Deserialize<'de>,
[src]
T: Deserialize<'de>,