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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use crate::{Error, SQLImplementation, data_types::SQLDataTypes};
pub mod implement;
pub mod sql_implementations;
#[derive(Debug)]
pub struct SelectProps {
pub connect: SQLImplementation,
pub columns: Vec<Column>,
pub table: String,
pub joins: Vec<Joins>,
pub clause: Option<String>,
pub order_by: Option<Vec<OrderBy>>,
pub group_by: Option<Vec<Column>>,
pub limit: Limit,
pub return_header: bool,
}
/// The column value used in SELECT statements, WHERE clauses, and conjunctions.
#[derive(Debug, Clone)]
pub enum Column {
/// Column name and table.
///
/// ```sql
/// SELECT my_table.my_column FROM my_table;
/// ```
Name(ColumnProps),
/// Used to return columns passed into functions and procedures.
///
/// ```sql
/// SELECT COUNT(my_column) FROM my_table;
/// ```
Function(String),
/// Use a Varchar as a column.
///
/// ```sql
/// SELECT 'My String' FROM dual;
/// ```
Varchar(String),
/// Selects all columns from the input table.
/// The equivalent to:
///
/// ```sql
/// SELECT my_table.* FROM my_table;
/// ```
ALL(String),
}
#[derive(Debug, Clone)]
pub struct ColumnProps {
pub name: String,
pub table: String,
}
#[derive(Debug)]
pub struct OrderBy {
pub column: Column,
pub by: Direction,
}
#[derive(Debug)]
pub enum Direction {
ASC,
DESC,
}
#[derive(Debug)]
pub struct Limit {
pub limit: Option<usize>,
pub offset: Option<usize>,
}
#[derive(Debug)]
pub struct Joins {
pub table: String,
pub primary_column: String,
pub foreign_column: String,
pub join_type: JoinType,
}
#[derive(Debug)]
pub enum JoinType {
Inner,
Outer,
Right,
Left,
}
pub trait SelectBuilder {
/// Inner joins another table to your query.
///
/// The table from the [`select method`](`crate::QueryBuilder::select`) is the primary table and will auto-generate an ID.
/// The column you want associated with the primary table will be the `primary_column` that's passed into this method and vice versa.
/// ```no_run
/// let data = conn.select("quarterly_earnings", vec![
/// "yearly.year", // Adding the joined table's id to specify which table the column comes from
/// "yearly.revenue as yearly_rev",
/// "revenue as quarterly_rev", // If you don't add an id, then it will be associated with the primary table
/// "profit",
/// ])
/// .inner_join("yearly_earnings", "year", "year")
/// .where_in("quarter", vec!["Q2", "Q3"])
/// .and("yearly.year", vec!["2025", "2026"]) // Same concept from above applies in a WHERE/AND/OR clause
/// .build()?;
/// ```
fn inner_join(self, foreign_table: &str, primary_column: &str, foreign_column: &str) -> Self;
/// Outer joins another table to your query.
///
/// The table from the [`select method`](`crate::QueryBuilder::select`) is the primary table and will auto-generate an ID.
/// The column you want associated with the primary table will be the `primary_column` that's passed into this method and vice versa.
/// ```no_run
/// let data = conn.select("quarterly_earnings", vec![
/// "yearly.year", // Adding the joined table's id to specify which table the column comes from
/// "yearly.revenue as yearly_rev",
/// "revenue as quarterly_rev", // If you don't add an id, then it will be associated with the primary table
/// "profit",
/// ])
/// .outer_join("yearly_earnings", "year", "year")
/// .where_in("quarter", vec!["Q2", "Q3"])
/// .and("yearly.year", vec!["2025", "2026"]) // Same concept from above applies in a WHERE/AND/OR clause
/// .build()?;
/// ```
fn outer_join(self, foreign_table: &str, primary_column: &str, foreign_column: &str) -> Self;
/// Right joins another table to your query.
///
/// The table from the [`select method`](`crate::QueryBuilder::select`) is the primary table and will auto-generate an ID.
/// The column you want associated with the primary table will be the `primary_column` that's passed into this method and vice versa.
/// ```no_run
/// let data = conn.select("quarterly_earnings", vec![
/// "yearly.year", // Adding the joined table's id to specify which table the column comes from
/// "yearly.revenue as yearly_rev",
/// "revenue as quarterly_rev", // If you don't add an id, then it will be associated with the primary table
/// "profit",
/// ])
/// .right_join("yearly_earnings", "year", "year")
/// .where_in("quarter", vec!["Q2", "Q3"])
/// .and("yearly.year", vec!["2025", "2026"]) // Same concept from above applies in a WHERE/AND/OR clause
/// .build()?;
/// ```
fn right_join(self, foreign_table: &str, primary_column: &str, foreign_column: &str) -> Self;
/// Left joins another table to your query.
///
/// The table from the [`select method`](`crate::QueryBuilder::select`) is the primary table and will auto-generate an ID.
/// The column you want associated with the primary table will be the `primary_column` that's passed into this method and vice versa.
/// ```no_run
/// let data = conn.select("quarterly_earnings", vec![
/// "yearly.year", // Adding the joined table's id to specify which table the column comes from
/// "yearly.revenue as yearly_rev",
/// "revenue as quarterly_rev", // If you don't add an id, then it will be associated with the primary table
/// "profit",
/// ])
/// .left_join("yearly_earnings", "year", "year")
/// .where_in("quarter", vec!["Q2", "Q3"])
/// .and("yearly.year", vec!["2025", "2026"]) // Same concept from above applies in a WHERE/AND/OR clause
/// .build()?;
/// ```
fn left_join(self, foreign_table: &str, primary_column: &str, foreign_column: &str) -> Self;
/// Order By column(s)
fn order_by(self, columns: Vec<OrderBy>) -> Self;
/// Group By column(s)
fn group_by(self, columns: Vec<&Column>) -> Self;
/// Sets limit and offset of query
fn limit(self, limit: usize, offset: Option<usize>) -> Self;
/// Returns column names
fn return_header(self) -> Self;
/// Builds the query.
/// This is multi-threaded by default, dividing the number of rows by the number of CPU cores.
/// If you're using a single core machine, it's recommended to use [`build_single_thread`](`SelectBuilder::build_single_thread`).
/// [`SQLite`](`SQLImplementation::SQLite`) runs better using [`build_single_thread`](`SelectBuilder::build_single_thread`)
/// (will either fix or remove it as an option in a future update).
fn build(self) -> Result<Vec<Vec<Box<SQLDataTypes>>>, Error>;
/// Builds the query only using one thread.
fn build_single_thread(self) -> Result<Vec<Vec<Box<SQLDataTypes>>>, Error>;
}
impl Column {
pub fn fmt_to_string(&self) -> String {
match self {
Column::Name(column_props) => format!("{}.{}", column_props.table, column_props.name),
Column::Function(func) => format!("{}", func),
Column::Varchar(varchar) => format!("'{}'", varchar),
Column::ALL(all) => format!("{}.*", all),
}
}
}