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    /// If the associated handle authenticated as an Instance Principal, this value must be an OCID.
56    /// In all other cases, the value may be specified as either a name (or path for nested compartments) or as an OCID.
57    ///
58    /// If no compartment is given, the default compartment id for the handle is used. If that value was
59    /// not specified, the root compartment of the tenancy will be used.
60    pub fn compartment_id(mut self, compartment_id: &str) -> Self {
61        self.compartment_id = compartment_id.to_string();
62        self
63    }
64
65    pub fn namespace(mut self, namespace: &str) -> ListTablesRequest {
66        self.namespace = namespace.to_string();
67        self
68    }
69
70    pub fn limit(mut self, limit: i32) -> ListTablesRequest {
71        self.limit = limit;
72        self
73    }
74
75    pub fn start_index(mut self, start_index: i32) -> ListTablesRequest {
76        self.start_index = start_index;
77        self
78    }
79
80    pub async fn execute(&self, h: &Handle) -> Result<ListTablesResult, NoSQLError> {
81        let mut w: Writer = Writer::new();
82        w.write_i16(h.inner.serial_version);
83        let timeout = h.get_timeout(&self.timeout);
84        self.nson_serialize(&mut w, &timeout);
85        let mut opts = SendOptions {
86            timeout: timeout,
87            retryable: true,
88            compartment_id: self.compartment_id.clone(),
89            ..Default::default()
90        };
91        let mut r = h.send_and_receive(w, &mut opts).await?;
92        let resp = ListTablesRequest::nson_deserialize(&mut r)?;
93        Ok(resp)
94    }
95
96    pub(crate) fn nson_serialize(&self, w: &mut Writer, timeout: &Duration) {
97        let mut ns = NsonSerializer::start_request(w);
98        ns.start_header();
99        ns.write_header(OpCode::ListTables, timeout, "");
100        ns.end_header();
101
102        // payload
103        ns.start_payload();
104        ns.write_nonempty_string_field(NAMESPACE, &self.namespace);
105        ns.write_nonzero_i32_field(LIST_START_INDEX, self.start_index);
106        ns.write_nonzero_i32_field(LIST_MAX_TO_READ, self.limit);
107        // TODO: this is currently only in http headers. Add to NSON?
108        //ns.write_string_field(COMPARTMENT_OCID, &self.compartment_id);
109        ns.end_payload();
110
111        ns.end_request();
112    }
113
114    pub(crate) fn nson_deserialize(r: &mut Reader) -> Result<ListTablesResult, NoSQLError> {
115        let mut walker = MapWalker::new(r)?;
116        let mut res: ListTablesResult = Default::default();
117        while walker.has_next() {
118            walker.next()?;
119            let name = walker.current_name();
120            match name.as_str() {
121                ERROR_CODE => {
122                    walker.handle_error_code()?;
123                }
124                TABLES => {
125                    res.table_names = walker.read_nson_string_array()?;
126                    //println!(" table_names={:?}", res.table_names);
127                }
128                LAST_INDEX => {
129                    res.last_table_index = walker.read_nson_i32()?;
130                    //println!(" last_index={:?}", res.last_table_index);
131                }
132                _ => {
133                    //println!("   list_tables_result: skipping field '{}'", name);
134                    walker.skip_nson_field()?;
135                }
136            }
137        }
138        Ok(res)
139    }
140}
141
142impl NsonRequest for ListTablesRequest {
143    fn serialize(&self, w: &mut Writer, timeout: &Duration) {
144        self.nson_serialize(w, timeout);
145    }
146}