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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//! # Eloquent
//!
//! **Eloquent** is a fluent, type-safe query builder for Rust, designed to make SQL query construction intuitive. It provides an expressive API, allowing developers to craft complex SQL queries through method chaining.
//!
//! ## Features
//!
//! - Fluent API for building SQL queries.
//! - Type-safe query construction with method chaining.
//! - Support for:
//! - `SELECT`, `JOIN`, `WHERE`, `GROUP BY`, `HAVING`, etc.
//! - Conditional queries with `AND`, `OR`, `NOT`, `LIKE`, `IN`, `NOT IN`, `IS NULL`, etc.
//! - Aggregation functions: `AVG`, `SUM`, `MIN`, `MAX`, and `COUNT`.
//! - Date functions: `DATE`, `TIME`, `YEAR`, `MONTH`, `DAY`, etc.
//! - Function aliases and raw expressions.
//! - CRUD operations: `INSERT`, `UPDATE`, and `DELETE`.
//! - Subqueries and nested conditions using closures.
//! - Cursor-based pagination support via `paginate()`.
//! - SQL query generation as raw `sql()` or formatted output `pretty_sql()`.
//! - Query validation and error handling (can be skipped with `skip_validation()`).
//!
//! Use your IDE to explore the available methods, or refer to the [docs.rs/eloquent - QueryBuilder](https://docs.rs/eloquent/latest/eloquent/struct.QueryBuilder.html).
//!
//! ## Installation
//!
//! To use Eloquent, add the following to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! eloquent = "2.0"
//! ```
//!
//! ## Usage
//!
//! ### Simple query example
//!
//! This example demonstrates a basic SQL query using Eloquent's fluent API.
//!
//! ```rust
//! use eloquent::Eloquent;
//!
//! let query = Eloquent::query()
//! .table("users")
//! .select(vec!["name", "email"])
//! .where_not_null("verified_at")
//! .where_like("email", "%@gmail.com")
//! .limit(100);
//!
//! println!("{}", query.pretty_sql()?);
//! ```
//!
//! ```sql
//! SELECT
//! name,
//! email
//! FROM
//! users
//! WHERE
//! verified_at IS NOT NULL
//! AND email LIKE '%@gmail.com'
//! LIMIT
//! 100
//! ```
//!
//! ### Complex query example
//!
//! This example demonstrates a more complex SQL query using Eloquent's fluent API.
//!
//! ```rust
//! use eloquent::Eloquent;
//!
//! let query = Eloquent::query()
//! .table("flights")
//! .select("origin_airport")
//! .select_avg("startup_time_in_minutes", "startup_time_in_minutes_avg")
//! .select_as("airports.city", "destination_city")
//! .join(
//! "airports",
//! "flights.destination_airport",
//! "airports.iata_code",
//! )
//! .r#where("origin_airport", "AMS")
//! .where_not_in("flight_number", vec!["KL123", "KL456"])
//! .where_not_null("gate_number")
//! .where_closure(|q| {
//! q.where_gte("flight_duration", 120)
//! .or_where_like("airports.city", "%NY%")
//! })
//! .group_by(vec!["origin_airport", "airports.city"])
//! .having_gt("startup_time_in_minutes_avg", 120)
//! .order_by_asc("startup_time_in_minutes_avg")
//! .limit(20);
//!
//! println!("{}", query.pretty_sql()?);
//! ```
//!
//! ```sql
//! SELECT
//! origin_airport,
//! AVG(startup_time_in_minutes) AS startup_time_in_minutes_avg,
//! airports.city AS destination_city
//! FROM
//! flights
//! JOIN airports ON flights.destination_airport = airports.iata_code
//! WHERE
//! origin_airport = 'AMS'
//! AND flight_number NOT IN ('KL123', 'KL456')
//! AND gate_number IS NOT NULL
//! AND (
//! flight_duration >= 120
//! OR airports.city LIKE '%NY%'
//! )
//! GROUP BY
//! origin_airport,
//! airports.city
//! HAVING
//! startup_time_in_minutes_avg > 120
//! ORDER BY
//! startup_time_in_minutes_avg ASC
//! LIMIT
//! 20
//! ```
//!
//! ### Subquery example
//!
//! This example demonstrates a subquery using Eloquent's fluent API.
//!
//! ```rust
//! use eloquent::Eloquent;
//!
//! let subquery = Eloquent::subquery()
//! .table("tickets")
//! .select("event_id")
//! .select_avg("price", "price_avg")
//! .group_by("event_id")
//! .order_by_desc("price_avg")
//! .limit(1);
//!
//! let query = Eloquent::query()
//! .table("events")
//! .select(vec!["event_name", "event_date"])
//! .r#where("event_id", subquery)
//! .pretty_sql()?;
//! ```
//!
//! ```sql
//! SELECT
//! event_name,
//! event_date
//! FROM
//! EVENTS
//! WHERE
//! event_id = (
//! SELECT
//! event_id,
//! AVG(price) AS price_avg
//! FROM
//! tickets
//! GROUP BY
//! event_id
//! ORDER BY
//! price_avg DESC
//! LIMIT
//! 1
//! )
//! ```
//!
//! ### Pagination example
//!
//! This example demonstrates cursor-based pagination using Eloquent's `paginate()` method.
//!
//! ```rust
//! use eloquent::Eloquent;
//!
//! let last_id = None; // initial query
//!
//! let query = Eloquent::query()
//! .table("departures")
//! .select("flight_number")
//! .paginate::<u64>("id", last_id, 25)
//! .sql()?;
//! ```
//!
//! ```sql
//! SELECT flight_number FROM departures LIMIT 25
//! ```
//!
//! ```rust
//! use eloquent::Eloquent;
//!
//! let last_id = Some(40); // last id from previous query
//!
//! let query = Eloquent::query()
//! .table("departures")
//! .select("flight_number")
//! .paginate("id", last_id, 25)
//! .sql()?;
//! ```
//!
//! ```sql
//! SELECT flight_number FROM departures WHERE id > 40 LIMIT 25
//! ```
pub use *;
/// The main struct for building queries.
;