native_db_32bit/transaction/query/
get.rs

1use crate::db_type::{DatabaseSecondaryKeyOptions, InnerKeyValue, Input, KeyDefinition, Result};
2use crate::transaction::internal::private_readable_transaction::PrivateReadableTransaction;
3use crate::transaction::internal::r_transaction::InternalRTransaction;
4use crate::transaction::internal::rw_transaction::InternalRwTransaction;
5
6/// Get a value from the database.
7pub struct RGet<'db, 'txn> {
8    pub(crate) internal: &'txn InternalRTransaction<'db>,
9}
10
11impl RGet<'_, '_> {
12    /// Get a value from the database by primary key.
13    ///
14    /// # Example
15    /// ```rust
16    /// use native_db::*;
17    /// use native_model::{native_model, Model};
18    /// use serde::{Deserialize, Serialize};
19    ///
20    /// #[derive(Serialize, Deserialize)]
21    /// #[native_model(id=1, version=1)]
22    /// #[native_db]
23    /// struct Data {
24    ///     #[primary_key]
25    ///     id: u64,
26    /// }
27    ///
28    /// fn main() -> Result<(), db_type::Error> {
29    ///     let mut builder = DatabaseBuilder::new();
30    ///     builder.define::<Data>()?;
31    ///     let db = builder.create_in_memory()?;
32    ///     
33    ///     // Open a read transaction
34    ///     let r = db.r_transaction()?;
35    ///     
36    ///     // Get a value by primary key
37    ///     let _value: Option<Data> = r.get().primary(1u64)?;
38    ///     Ok(())
39    /// }
40    /// ```
41    pub fn primary<T: Input>(&self, key: impl InnerKeyValue) -> Result<Option<T>> {
42        let model = T::native_db_model();
43        let result = self.internal.get_by_primary_key(model, key)?;
44        Ok(result.map(|value| value.inner()))
45    }
46
47    /// Get a value from the database by secondary key.
48    ///
49    /// /!\ The secondary key **must** be [`unique`](crate::DatabaseBuilder#unique) else this method will return an error [`SecondaryKeyConstraintMismatch`](crate::db_type::Error::SecondaryKeyConstraintMismatch).
50    ///     If the secondary key is not unique, use [`scan()`](crate::transaction::RTransaction::scan) instead.
51    ///
52    /// Anatomy of a secondary key it is a `enum` with the following structure: `<table_name>Key::<name>`.
53    ///
54    /// # Example
55    /// ```rust
56    /// use native_db::*;
57    /// use native_model::{native_model, Model};
58    /// use serde::{Deserialize, Serialize};
59    ///
60    /// #[derive(Serialize, Deserialize)]
61    /// #[native_model(id=1, version=1)]
62    /// #[native_db]
63    /// struct Data {
64    ///     #[primary_key]
65    ///     id: u64,
66    ///     #[secondary_key(unique)] // Must be unique to use get()
67    ///     name: String,
68    /// }
69    ///
70    /// fn main() -> Result<(), db_type::Error> {
71    ///     let mut builder = DatabaseBuilder::new();
72    ///     builder.define::<Data>()?;
73    ///     let db = builder.create_in_memory()?;
74    ///     
75    ///     // Open a read transaction
76    ///     let r = db.r_transaction()?;
77    ///     
78    ///     // Get a value by primary key
79    ///     let _value: Option<Data> = r.get().secondary(DataKey::name, "test")?;
80    ///     Ok(())
81    /// }
82    /// ```
83    pub fn secondary<T: Input>(
84        &self,
85        key_def: impl KeyDefinition<DatabaseSecondaryKeyOptions>,
86        key: impl InnerKeyValue,
87    ) -> Result<Option<T>> {
88        let model = T::native_db_model();
89        let result = self.internal.get_by_secondary_key(model, key_def, key)?;
90        Ok(result.map(|value| value.inner()))
91    }
92}
93
94pub struct RwGet<'db, 'txn> {
95    pub(crate) internal: &'txn InternalRwTransaction<'db>,
96}
97
98impl RwGet<'_, '_> {
99    /// Get a value from the database by primary key.
100    ///
101    /// Same as [`RGet::primary()`](struct.RGet.html#method.primary).
102    pub fn primary<T: Input>(&self, key: impl InnerKeyValue) -> Result<Option<T>> {
103        let model = T::native_db_model();
104        let result = self.internal.get_by_primary_key(model, key)?;
105        Ok(result.map(|value| value.inner()))
106    }
107
108    /// Get a value from the database by secondary key.
109    ///
110    /// Same as [`RGet::secondary()`](struct.RGet.html#method.secondary).
111    pub fn secondary<T: Input>(
112        &self,
113        key_def: impl KeyDefinition<DatabaseSecondaryKeyOptions>,
114        key: impl InnerKeyValue,
115    ) -> Result<Option<T>> {
116        let model = T::native_db_model();
117        let result = self.internal.get_by_secondary_key(model, key_def, key)?;
118        Ok(result.map(|value| value.inner()))
119    }
120}