oracle_sql_tools/statements/
select.rs

1use crate::{types::errors::OracleSqlToolsError, utils::remove_invalid_chars};
2use super::PreppedRowData;
3
4impl PreppedRowData {
5        /// Selects the columns (via the input vector) from the specified table.
6        /// 
7        /// # Usage
8        /// 
9        /// It's recommended to open a [`Connection`](oracle::Connection) first so if there's an issue connecting to the database, it'll error faster.
10        /// 
11        /// You'll also need to initialize your column names as a Vector (recommended to use &str or String, but can accept any datatype that implements [`crate::format_data::FormattedData`]).
12        /// 
13        /// Once the previous two things are done, use the [`.prep_data()`](crate::PrepData::prep_data) method on your vector, then 
14        /// ```no_run
15        /// let conn: oracle::Connection = match Connection::connect("<USERNAME>", "<PASSWORD>", "<IP ADDRESS>")?; 
16        ///
17        /// let col_names: Vec<&str> = vec!["Employee ID", "Name", "Job Title", "Department", "Business Unit"];
18        ///
19        /// let table_data: Vec<Vec<Option<String>>> = col_names.prep_data(conn).select("MY_TABLE")?;
20        /// ```
21    pub fn select(self, table_name: &str) -> Result<Vec<Vec<Option<String>>>, OracleSqlToolsError> {
22        let header = self.data.iter().map(|cell|
23            remove_invalid_chars(cell)
24        ).collect::<Vec<String>>();
25        let sql = format!("SELECT {} FROM {}", &header.join(", "), table_name);
26
27        let query = self.conn.query(&sql, &[])?;
28        let mut outer_vec = Vec::new();
29        for v in query {
30            let p = v?;
31            let mut inner_vec = Vec::new();
32            for colindx in 0..header.len() {
33                let a = p.get::<usize, Option<String>>(colindx)?;
34                inner_vec.push(a)
35            }
36            outer_vec.push(inner_vec)
37        }
38
39        Ok(outer_vec)
40    }
41}