pub struct Database { /* private fields */ }
Expand description
A Database
Implementations§
Source§impl Database
impl Database
Sourcepub fn open(path: impl AsRef<Path>) -> Result<Self>
pub fn open(path: impl AsRef<Path>) -> Result<Self>
Opens a file at the given path and uses it as the database.
If the file doesn’t exist, it will be created.
let db = Database::open("my_database.db")?;
Sourcepub fn in_memory() -> Result<Self>
pub fn in_memory() -> Result<Self>
Opens an in-memory database.
Useful for tests and as a stub for a database that doesn’t need to be saved to disk.
let db = Database::in_memory()?;
Sourcepub fn close(self)
pub fn close(self)
Closes the databas
let db = Database::open("my_database.db")?;
db.close();
// why ?
Sourcepub fn table<'a>(&'a self, name: &'a str) -> Table<'a>
pub fn table<'a>(&'a self, name: &'a str) -> Table<'a>
Get a read-only handle to a table with the given name.
let db = Database::open("my_database.db")?;
let value = db.table("my_table").get("key")?;
Sourcepub fn table_mut<'a>(&'a mut self, name: &'a str) -> TableMut<'a>
pub fn table_mut<'a>(&'a mut self, name: &'a str) -> TableMut<'a>
Get a read-write handle to a table with the given name.
let mut db = Database::open("my_database.db")?;
db.table_mut("my_table").set("key", &"value")?;
Sourcepub fn list_tables(&self) -> Result<Vec<String>>
pub fn list_tables(&self) -> Result<Vec<String>>
Returns a list of the names of all tables in the database.
let db = Database::open("my_database.db")?;
let tables = db.list_tables()?;
for table in tables {
println!("{}", table);
}
Sourcepub fn delete_table(&mut self, name: &str) -> Result<()>
pub fn delete_table(&mut self, name: &str) -> Result<()>
Deletes a table from the database.
let mut db = Database::open("my_database.db")?;
db.delete_table("my_table")?;
Sourcepub fn len_all_tables(&self) -> Result<usize>
pub fn len_all_tables(&self) -> Result<usize>
Returns the number of entries in all tables in the database.
aliases: size_all_tables()
let db = Database::open("my_database.db")?;
let len = db.len_all_tables()?;
println!("the database has {} entries across all tables", len);
Sourcepub fn size_all_tables(&self) -> Result<usize>
pub fn size_all_tables(&self) -> Result<usize>
Returns the number of entries in all tables in the database.
aliases: len_all_tables()
let db = Database::open("my_database.db")?;
let size = db.size_all_tables()?;
println!("the database has {} entries across all tables", size);
Sourcepub fn delete_all_tables(&mut self) -> Result<()>
pub fn delete_all_tables(&mut self) -> Result<()>
Deletes all tables in the database. \
let mut db = Database::open("my_database.db")?;
db.delete_all_tables()?;
assert!(db.list_tables()?.is_empty());
Trait Implementations§
Source§impl TableReadInterface for Database
impl TableReadInterface for Database
Source§fn get<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>>
fn get<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>>
Gets the value associated with the given key. Read more
Source§fn values<T: DeserializeOwned>(&self) -> Result<Vec<T>>
fn values<T: DeserializeOwned>(&self) -> Result<Vec<T>>
Gets a list of all values in the table (that can be deserialized into the given type). Read more
Source§fn entries<T: DeserializeOwned>(&self) -> Result<Vec<(String, T)>>
fn entries<T: DeserializeOwned>(&self) -> Result<Vec<(String, T)>>
Gets a list of all entries in the table (that can be deserialized into the given type). Read more
Source§fn get_or<T: DeserializeOwned>(&self, key: &str, default: T) -> Result<T>
fn get_or<T: DeserializeOwned>(&self, key: &str, default: T) -> Result<T>
Gets the value associated with the given key,
if an error occurs, or no value is found, returns the given default value. Read more
if an error occurs, or no value is found, returns the given default value. Read more
Source§fn get_or_default<T: DeserializeOwned + Default>(&self, key: &str) -> Result<T>
fn get_or_default<T: DeserializeOwned + Default>(&self, key: &str) -> Result<T>
Gets the value associated with the given key,
if an error occurs, or no value is found, returns the default value for the given type. if no value is found, returns the default value for the given type. Read more
if an error occurs, or no value is found, returns the default value for the given type. if no value is found, returns the default value for the given type. Read more
Source§fn get_or_else<T: DeserializeOwned, F: FnOnce() -> T>(
&self,
key: &str,
default: F,
) -> Result<T>
fn get_or_else<T: DeserializeOwned, F: FnOnce() -> T>( &self, key: &str, default: F, ) -> Result<T>
Gets the value associated with the given key,
if an error occurs, or no value is found, calls the given closure and returns the result. Read more
if an error occurs, or no value is found, calls the given closure and returns the result. Read more
Source§impl TableWriteInterface for Database
impl TableWriteInterface for Database
Source§fn get_or_insert<T: Serialize + DeserializeOwned>(
&mut self,
key: &str,
default: T,
) -> Result<T>
fn get_or_insert<T: Serialize + DeserializeOwned>( &mut self, key: &str, default: T, ) -> Result<T>
Gets the value associated with the given key,
if the no value is found, inserts the given default value into the table and returns it. Read more
if the no value is found, inserts the given default value into the table and returns it. Read more
Source§fn get_or_insert_default<T: Serialize + DeserializeOwned + Default>(
&mut self,
key: &str,
) -> Result<T>
fn get_or_insert_default<T: Serialize + DeserializeOwned + Default>( &mut self, key: &str, ) -> Result<T>
Gets the value associated with the given key,
if the no value is found, inserts the default value for the given type into the table and returns it. Read more
if the no value is found, inserts the default value for the given type into the table and returns it. Read more
Source§fn get_or_insert_with<T: Serialize + DeserializeOwned, F: FnOnce() -> T>(
&mut self,
key: &str,
default: F,
) -> Result<T>
fn get_or_insert_with<T: Serialize + DeserializeOwned, F: FnOnce() -> T>( &mut self, key: &str, default: F, ) -> Result<T>
Gets the value associated with the given key,
if the no value is found, inserts the result of the given closure into the table and returns it. Read more
if the no value is found, inserts the result of the given closure into the table and returns it. Read more
Auto Trait Implementations§
impl Freeze for Database
impl !RefUnwindSafe for Database
impl Send for Database
impl Sync for Database
impl Unpin for Database
impl !UnwindSafe for Database
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more