sql_tools/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#![doc = include_str!("../README.md")]

use create::CreateProps;
use data_types::ToSQLData;
use insert::InsertProps;
use sql_variations::OracleConnect;
use select::SelectProps;
use update::UpdateProps;

pub mod errors;
pub mod sql_variations;
pub mod select;
pub mod update;
pub mod utils;
pub mod where_clause;
pub mod insert;
pub mod data_types;
pub mod create;

/// Trait used for the SQL Database types found in [`SQLVariation`] to implement basic SQL queries.
pub trait QueryBuilder {
    /// Creates a new [`SelectProps`] to start building out a select query.
    /// 
    /// For the select method, you add the table you want to select from, then the columns in a vector. 
    /// If you want to select all, simply input `vec!["*"]`. 
    /// You can add a [`where_clause::WhereSelect`] to filter out the rows you want, just like writing a SQL query.
    /// ```no_run
    /// let conn = OracleConnect::new(connection_string, username, password);
    /// let data: Vec<Vec<SQLDataTypes>> = conn
    ///     .select("regional_sales", vec!["product_id", "revenue", "sale_date"])
    ///     .where_in("product_id", vec!["1001", "4567"])
    ///     .and_not("city", vec!["Austin", "Dallas"])
    ///     .build()?;
    /// data.iter().for_each(|row: &Vec<SQLDataTypes>| { println!("{:?}", row) });
    /// ```
    /// Is the same as:
    /// ```sql
    /// SELECT product_id, revenue, sale_date FROM regional_sales
    /// WHERE product_id IN ('1001', '4567')
    /// AND city NOT IN ('Austin', 'Dallas');
    /// ```
    fn select(&self, table: &str, columns: Vec<&str>) -> SelectProps;

    /// Creates a new [`UpdateProps`] to start building out an update query.
    /// 
    /// Updates a table's column(s) based on criteria set in the optional [where clauses](#where). 
    /// Updates can return Ok() or the number of rows that were updated.
    /// ```no_run
    /// let conn = OracleConnect::new(connection_string, username, password);
    /// conn.update("global_sales")
    ///     .set("continent", "North America")
    ///     .where_in("country", vec!["Canada", "United States", "Mexico"])
    ///     .build()?;
    /// // If you want to get the number of rows that were updated
    /// let count: usize = conn
    ///     .update("global_sales")
    ///     .set("continent", "North America")
    ///     .where_in("country", vec!["Canada", "United States", "Mexico"])
    ///     .build_return_count()?;
    /// ```
    fn update(&self, table: &str) -> UpdateProps;

    /// Creates a new [`InsertProps`] to start building out an insert query.
    /// 
    /// Inserts a grid (two-dimensional vector) of data into your database. 
    /// Can take any type that has the [`ToSQLData`](#tosqldata) trait implemented. 
    /// If the table does not exist, it will automatically create a new table (will have an abort option in a future update).
    /// ```no_run
    /// let conn = OracleConnect::new(connection_string, username, password);
    /// let data: Vec<Vec<&str>> = vec![
    ///     vec!["Column_A", "Column_B", "Column_C"],
    ///     vec!["a1", "b1", "c1"],
    ///     vec!["a2", "b2", "c2"],
    ///     vec!["a3", "b3", "c3"],
    /// ];
    /// conn.insert("my_table", data).build()?;
    /// ```
    fn insert<T: ToSQLData>(&self, table: &str, data: Vec<Vec<T>>) -> InsertProps;

    /// Creates a new [`CreateProps`] to start building a create query.
    /// 
    /// Creates a table using a vector of the `CreateColumns` struct and the `CreateDataTypes` to apply the correct types to the new columns.
    /// ```no_run
    /// let conn = OracleConnect::new(connection_string, username, password);
    /// let columns = vec![
    ///         CreateColumns{ name: "Column_A".to_string(), data_type: CreateDataTypes::VARCHAR(20 as usize) },
    ///         CreateColumns{ name: "Column_B".to_string(), data_type: CreateDataTypes::NUMBER },
    ///         CreateColumns{ name: "Column_C".to_string(), data_type: CreateDataTypes::FLOAT },
    ///     ];
    ///
    ///     conn.create()
    ///         .table("my_table", columns)
    ///         .build()?;
    /// ```
    ///
    /// You can add a column after you initiated the create table process.
    /// ```no_run
    /// let my_table: CreateTable = conn.create()
    ///     .table("my_table", columns);
    ///
    /// if add_date_col == true {
    ///     my_table.add_column("Column_D".to_string(), CreateDataTypes::DATE);
    /// }
    ///
    /// my_table.build()?;
    /// ```
    fn create(&self) -> CreateProps;
}

#[derive(Debug)]
/// The various types of SQL connections 
pub enum SQLVariation {
    Oracle(OracleConnect),
}