oracle_nosql_rust_sdk/
list_tables_request.rs

1//
2// Copyright (c) 2024, 2025 Oracle and/or its affiliates. All rights reserved.
3//
4// Licensed under the Universal Permissive License v 1.0 as shown at
5//  https://oss.oracle.com/licenses/upl/
6//
7use crate::error::NoSQLError;
8use crate::handle::Handle;
9use crate::handle::SendOptions;
10use crate::nson::*;
11use crate::reader::Reader;
12use crate::types::OpCode;
13use crate::writer::Writer;
14use std::result::Result;
15use std::time::Duration;
16
17/// Struct used for listing tables in the NoSQL Database.
18#[derive(Default, Debug)]
19pub struct ListTablesRequest {
20    pub(crate) compartment_id: String,
21    pub(crate) namespace: String,
22    pub(crate) start_index: i32,
23    pub(crate) limit: i32,
24    pub(crate) timeout: Option<Duration>,
25    // TODO: tags?
26}
27
28/// Struct representing the result of a [`ListTablesRequest`] operation.
29#[derive(Default, Debug)]
30pub struct ListTablesResult {
31    pub table_names: Vec<String>,
32    pub last_table_index: i32,
33}
34
35impl ListTablesRequest {
36    pub fn new() -> ListTablesRequest {
37        ListTablesRequest {
38            ..Default::default()
39        }
40    }
41
42    /// Specify the timeout value for the request.
43    ///
44    /// This is optional.
45    /// If set, it must be greater than or equal to 1 millisecond, otherwise an
46    /// IllegalArgument error will be returned.
47    /// If not set, the default timeout value configured for the [`Handle`](crate::HandleBuilder::timeout()) is used.
48    pub fn timeout(mut self, t: &Duration) -> Self {
49        self.timeout = Some(t.clone());
50        self
51    }
52
53    /// Cloud Service only: set the name or id of a compartment to be used for this operation.
54    ///
55    /// The compartment may be specified as either a name (or path for nested compartments) or as an id (OCID).
56    /// A name (vs id) can only be used when authenticated using a specific user identity. It is not available if
57    /// the associated handle authenticated as an Instance Principal (which can be done when calling the service from
58    /// a compute instance in the Oracle Cloud Infrastructure: see [`HandleBuilder::cloud_auth_from_instance()`](crate::HandleBuilder::cloud_auth_from_instance()).)
59    ///
60    /// If no compartment is given, the root compartment of the tenancy will be used.
61    pub fn compartment_id(mut self, compartment_id: &str) -> Self {
62        self.compartment_id = compartment_id.to_string();
63        self
64    }
65
66    pub fn namespace(mut self, namespace: &str) -> ListTablesRequest {
67        self.namespace = namespace.to_string();
68        self
69    }
70
71    pub fn limit(mut self, limit: i32) -> ListTablesRequest {
72        self.limit = limit;
73        self
74    }
75
76    pub fn start_index(mut self, start_index: i32) -> ListTablesRequest {
77        self.start_index = start_index;
78        self
79    }
80
81    pub async fn execute(&self, h: &Handle) -> Result<ListTablesResult, NoSQLError> {
82        let mut w: Writer = Writer::new();
83        w.write_i16(h.inner.serial_version);
84        let timeout = h.get_timeout(&self.timeout);
85        self.nson_serialize(&mut w, &timeout);
86        let mut opts = SendOptions {
87            timeout: timeout,
88            retryable: true,
89            compartment_id: self.compartment_id.clone(),
90            ..Default::default()
91        };
92        let mut r = h.send_and_receive(w, &mut opts).await?;
93        let resp = ListTablesRequest::nson_deserialize(&mut r)?;
94        Ok(resp)
95    }
96
97    pub(crate) fn nson_serialize(&self, w: &mut Writer, timeout: &Duration) {
98        let mut ns = NsonSerializer::start_request(w);
99        ns.start_header();
100        ns.write_header(OpCode::ListTables, timeout, "");
101        ns.end_header();
102
103        // payload
104        ns.start_payload();
105        ns.write_nonempty_string_field(NAMESPACE, &self.namespace);
106        ns.write_nonzero_i32_field(LIST_START_INDEX, self.start_index);
107        ns.write_nonzero_i32_field(LIST_MAX_TO_READ, self.limit);
108        // TODO: this is currently only in http headers. Add to NSON?
109        //ns.write_string_field(COMPARTMENT_OCID, &self.compartment_id);
110        ns.end_payload();
111
112        ns.end_request();
113    }
114
115    pub(crate) fn nson_deserialize(r: &mut Reader) -> Result<ListTablesResult, NoSQLError> {
116        let mut walker = MapWalker::new(r)?;
117        let mut res: ListTablesResult = Default::default();
118        while walker.has_next() {
119            walker.next()?;
120            let name = walker.current_name();
121            match name.as_str() {
122                ERROR_CODE => {
123                    walker.handle_error_code()?;
124                }
125                TABLES => {
126                    res.table_names = walker.read_nson_string_array()?;
127                    //println!(" table_names={:?}", res.table_names);
128                }
129                LAST_INDEX => {
130                    res.last_table_index = walker.read_nson_i32()?;
131                    //println!(" last_index={:?}", res.last_table_index);
132                }
133                _ => {
134                    //println!("   list_tables_result: skipping field '{}'", name);
135                    walker.skip_nson_field()?;
136                }
137            }
138        }
139        Ok(res)
140    }
141}
142
143impl NsonRequest for ListTablesRequest {
144    fn serialize(&self, w: &mut Writer, timeout: &Duration) {
145        self.nson_serialize(w, timeout);
146    }
147}