native_db_32bit/transaction/query/len.rs
1use crate::db_type::{DatabaseSecondaryKeyOptions, 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 the number of values in the database.
7pub struct RLen<'db, 'txn> {
8 pub(crate) internal: &'txn InternalRTransaction<'db>,
9}
10
11impl RLen<'_, '_> {
12 /// Get the number of values.
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 all values
37 /// let _number:u64 = r.len().primary::<Data>()?;
38 /// Ok(())
39 /// }
40 /// ```
41 pub fn primary<T: Input>(&self) -> Result<u64> {
42 let model = T::native_db_model();
43 let result = self.internal.primary_len(model)?;
44 Ok(result)
45 }
46
47 /// **TODO: needs to be implemented**
48 ///
49 /// Get the number of values by secondary key.
50 ///
51 /// Anatomy of a secondary key it is a `enum` with the following structure: `<table_name>Key::<name>`.
52 ///
53 /// If the secondary key is [`optional`](struct.DatabaseBuilder.html#optional) you will
54 /// get all values that have the secondary key set.
55 pub fn secondary<T: Input>(
56 &self,
57 _key_def: impl KeyDefinition<DatabaseSecondaryKeyOptions>,
58 ) -> Result<Option<T>> {
59 todo!()
60 }
61}
62
63pub struct RwLen<'db, 'txn> {
64 pub(crate) internal: &'txn InternalRwTransaction<'db>,
65}
66
67impl RwLen<'_, '_> {
68 /// Get the number of values.
69 ///
70 /// Same as [`RLen::primary()`](struct.RLen.html#method.primary).
71 pub fn primary<T: Input>(&self) -> Result<u64> {
72 let model = T::native_db_model();
73 let result = self.internal.primary_len(model)?;
74 Ok(result)
75 }
76
77 /// Get the number of values by secondary key.
78 ///
79 /// Same as [`RLen::secondary()`](struct.RLen.html#method.secondary).
80 pub fn secondary<T: Input>(
81 &self,
82 _key_def: impl KeyDefinition<DatabaseSecondaryKeyOptions>,
83 ) -> Result<Option<T>> {
84 todo!()
85 }
86}