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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//! # Select Operations and Query Building
//!
//! This module provides the core querying functionality for retrieving data from Supabase tables.
//! It implements a fluent query builder pattern that allows for intuitive, chainable operations
//! with comprehensive filtering, sorting, and pagination capabilities.
//!
//! ## 🎯 Core Concepts
//!
//! ### Query Builder Pattern
//! The select operations use a fluent API that allows you to chain multiple operations:
//! ```text
//! client.select("table") -> .eq("column", "value") -> .limit(10) -> .execute()
//! ```
//!
//! ### Performance Considerations
//! - **Column Selection**: Use `.columns()` to fetch only needed fields
//! - **Pagination**: Prefer `.range()` over `.offset()` for better performance
//! - **Filtering**: Apply filters early to reduce data transfer
//! - **Counting**: Use `.count()` sparingly as it's expensive on large tables
//!
//! ## 🔍 Available Filter Operations
//!
//! | Operator | Method | Description | Example |
//! |----------|--------|-------------|---------|
//! | `=` | `eq(column, value)` | Equal to | `.eq("status", "active")` |
//! | `!=` | `neq(column, value)` | Not equal to | `.neq("deleted", "true")` |
//! | `>` | `gt(column, value)` | Greater than | `.gt("age", "18")` |
//! | `<` | `lt(column, value)` | Less than | `.lt("score", "100")` |
//! | `>=` | `gte(column, value)` | Greater than or equal | `.gte("created_at", "2024-01-01")` |
//! | `<=` | `lte(column, value)` | Less than or equal | `.lte("price", "50.00")` |
//! | `IN` | `in_(column, values)` | Value in list | `.in_("category", &["tech", "science"])` |
//! | `FTS` | `text_search(column, query)` | Full-text search | `.text_search("content", "rust")` |
//!
//! ## 📄 Pagination Methods
//!
//! | Method | Description | Performance | Use Case |
//! |--------|-------------|-------------|----------|
//! | `range(from, to)` | PostgREST range header | ✅ Fast | Recommended for pagination |
//! | `limit(n)` | Limit number of results | ✅ Fast | Simple result limiting |
//! | `offset(n)` | Skip n records | ⚠️ Slower | Use sparingly, prefer range |
//! | `count()` | Count matching records | ❌ Expensive | Use only when necessary |
//!
//! ## 📖 Usage Examples
//!
//! ### Basic Querying
//!
//! ```rust,no_run
//! use supabase_rs::SupabaseClient;
//! use serde_json::Value;
//!
//! # async fn example() -> Result<(), String> {
//! # let client = SupabaseClient::new("url".to_string(), "key".to_string()).unwrap();
//! // Simple select with filtering
//! let users: Vec<Value> = client
//! .select("users")
//! .eq("status", "active")
//! .execute()
//! .await?;
//!
//! println!("Found {} active users", users.len());
//! # Ok(())
//! # }
//! ```
//!
//! ### Advanced Filtering
//!
//! ```rust,no_run
//! # use supabase_rs::SupabaseClient;
//! # use serde_json::Value;
//! # async fn example() -> Result<(), String> {
//! # let client = SupabaseClient::new("url".to_string(), "key".to_string()).unwrap();
//! // Complex filtering with multiple conditions
//! let filtered_products: Vec<Value> = client
//! .select("products")
//! .gte("price", "10.00") // Price >= $10
//! .lte("price", "100.00") // Price <= $100
//! .neq("category", "discontinued") // Not discontinued
//! .in_("brand", &["apple", "samsung", "google"]) // Specific brands
//! .text_search("description", "smartphone") // Full-text search
//! .order("price", true) // Sort by price ascending
//! .limit(50) // Limit results
//! .execute()
//! .await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Column Selection and Pagination
//!
//! ```rust,no_run
//! # use supabase_rs::SupabaseClient;
//! # use serde_json::Value;
//! # async fn example() -> Result<(), String> {
//! # let client = SupabaseClient::new("url".to_string(), "key".to_string()).unwrap();
//! // Select specific columns for efficiency
//! let user_profiles: Vec<Value> = client
//! .from("users")
//! .columns(vec!["id", "name", "email", "avatar_url"])
//! .eq("verified", "true")
//! .range(0, 24) // Get first 25 records (0-24 inclusive)
//! .order("created_at", false) // Newest first
//! .execute()
//! .await?;
//!
//! // Offset-based pagination (less efficient but sometimes needed)
//! let page_2: Vec<Value> = client
//! .from("posts")
//! .columns(vec!["id", "title", "excerpt"])
//! .eq("published", "true")
//! .limit(10)
//! .offset(10) // Skip first 10 records
//! .execute()
//! .await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Count Operations
//!
//! > **⚠️ Performance Warning**: Count operations can be expensive on large tables. Use judiciously.
//!
//! ```rust,no_run
//! # use supabase_rs::SupabaseClient;
//! # use serde_json::Value;
//! # async fn example() -> Result<(), String> {
//! # let client = SupabaseClient::new("url".to_string(), "key".to_string()).unwrap();
//! // Count with filters (recommended)
//! let active_user_count: Vec<Value> = client
//! .select("users")
//! .eq("status", "active")
//! .count()
//! .execute()
//! .await?;
//!
//! // Count all records (expensive on large tables)
//! let total_users: Vec<Value> = client
//! .select("users")
//! .count()
//! .execute()
//! .await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## ⚡ Performance Tips
//!
//! 1. **Use Column Selection**: Only fetch columns you need
//! 2. **Apply Filters Early**: Reduce data transfer with specific filters
//! 3. **Prefer Range Over Offset**: Range-based pagination is more efficient
//! 4. **Limit Results**: Always use reasonable limits to prevent large responses
//! 5. **Index Your Filters**: Ensure filtered columns are indexed in your database
//!
//! ## 🔧 Error Handling
//!
//! All select operations return `Result<Vec<Value>, String>` for consistent error handling:
//!
//! ```rust,no_run
//! # use supabase_rs::SupabaseClient;
//! # use serde_json::Value;
//! # async fn example() -> Result<(), String> {
//! # let client = SupabaseClient::new("url".to_string(), "key".to_string()).unwrap();
//! match client.select("users").eq("id", "123").execute().await {
//! Ok(users) => {
//! if users.is_empty() {
//! println!("No users found");
//! } else {
//! println!("Found {} users", users.len());
//! }
//! },
//! Err(error) => {
//! eprintln!("Query failed: {}", error);
//! // Handle specific error cases
//! if error.contains("401") {
//! eprintln!("Authentication failed");
//! }
//! }
//! }
//! # Ok(())
//! # }
//! ```
use crate;
use crateHeadersTypes;
use crateHeaders;
use cratehandle_response;
use crateSupabaseClient;
use HeaderMap;
use ;
use Response;
use Value;