postrust-core 0.2.0

Core library for Postrust - PostgREST-compatible REST API for PostgreSQL
Documentation
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Query builder implementation.

use crate::error::Result;
use crate::plan::{
    CallPlan, CallParams, CoercibleFilter, CoercibleLogicTree, CoercibleOrderTerm,
    CoercibleSelectField, MutatePlan, ReadPlan, ReadPlanTree,
};
use postrust_sql::{
    escape_ident, from_qi, DeleteBuilder, InsertBuilder, OrderExpr, SelectBuilder,
    SqlFragment, SqlParam, UpdateBuilder,
};

/// Query builder for converting plans to SQL.
pub struct QueryBuilder;

impl QueryBuilder {
    /// Build a SELECT query from a read plan tree.
    pub fn build_read(tree: &ReadPlanTree) -> Result<SqlFragment> {
        Self::build_read_plan(&tree.root)
    }

    /// Build a SELECT query from a read plan.
    fn build_read_plan(plan: &ReadPlan) -> Result<SqlFragment> {
        let mut builder = SelectBuilder::new();

        // FROM clause
        let qi = &plan.from;
        if let Some(alias) = &plan.from_alias {
            builder = builder.from_table_as(
                &postrust_sql::identifier::QualifiedIdentifier::new(&qi.schema, &qi.name),
                alias,
            );
        } else {
            builder = builder.from_table(
                &postrust_sql::identifier::QualifiedIdentifier::new(&qi.schema, &qi.name),
            );
        }

        // SELECT columns
        for field in &plan.select {
            let col_frag = Self::build_select_field(field)?;
            builder = builder.column_raw(col_frag);
        }

        // WHERE clauses
        for clause in &plan.where_clauses {
            let expr = Self::build_logic_tree(clause)?;
            builder = builder.where_raw(expr);
        }

        // ORDER BY
        for term in &plan.order {
            let order = Self::build_order_term(term);
            builder = builder.order_by(order);
        }

        // LIMIT/OFFSET
        if let Some(limit) = plan.range.limit {
            builder = builder.limit(limit);
        }
        if plan.range.offset > 0 {
            builder = builder.offset(plan.range.offset);
        }

        Ok(builder.build())
    }

    /// Build a SELECT field.
    fn build_select_field(field: &CoercibleSelectField) -> Result<SqlFragment> {
        let mut frag = SqlFragment::new();

        // Aggregate function
        if let Some(agg) = &field.aggregate {
            frag.push(agg.to_sql());
            frag.push("(");
        }

        // Column name with JSON path
        frag.push(&escape_ident(&field.field.name));

        // Close aggregate
        if field.aggregate.is_some() {
            frag.push(")");
        }

        // Cast
        if let Some(cast) = &field.cast {
            frag.push("::");
            frag.push(cast);
        }

        // Alias
        if let Some(alias) = &field.alias {
            frag.push(" AS ");
            frag.push(&escape_ident(alias));
        }

        Ok(frag)
    }

    /// Build a logic tree.
    fn build_logic_tree(tree: &CoercibleLogicTree) -> Result<SqlFragment> {
        match tree {
            CoercibleLogicTree::Expr { negated, op, children } => {
                let sep = match op {
                    crate::api_request::LogicOperator::And => " AND ",
                    crate::api_request::LogicOperator::Or => " OR ",
                };

                let child_frags: Result<Vec<_>> = children
                    .iter()
                    .map(|c| Self::build_logic_tree(c))
                    .collect();

                let mut combined = SqlFragment::join(sep, child_frags?).parens();

                if *negated {
                    let mut neg = SqlFragment::raw("NOT ");
                    neg.append(combined);
                    combined = neg;
                }

                Ok(combined)
            }
            CoercibleLogicTree::Stmt(filter) => Self::build_filter(filter),
            CoercibleLogicTree::NullEmbed { negated, field_name } => {
                let mut frag = SqlFragment::new();
                frag.push(&escape_ident(field_name));
                if *negated {
                    frag.push(" IS NOT NULL");
                } else {
                    frag.push(" IS NULL");
                }
                Ok(frag)
            }
        }
    }

    /// Build a filter expression.
    fn build_filter(filter: &CoercibleFilter) -> Result<SqlFragment> {
        let mut frag = SqlFragment::new();

        // Column name
        frag.push(&escape_ident(&filter.field.name));

        // Handle negation
        if filter.op_expr.negated {
            frag.push(" NOT");
        }

        // Operation
        match &filter.op_expr.operation {
            crate::api_request::Operation::Simple { op, value } => {
                frag.push(" ");
                frag.push(op.to_sql());
                frag.push(" ");
                frag.push_param(value.clone());
            }
            crate::api_request::Operation::Quant { op, quantifier, value } => {
                frag.push(" ");
                frag.push(op.to_sql());
                frag.push(" ");
                if let Some(q) = quantifier {
                    match q {
                        crate::api_request::OpQuantifier::Any => frag.push("ANY("),
                        crate::api_request::OpQuantifier::All => frag.push("ALL("),
                    };
                    frag.push_param(value.clone());
                    frag.push(")");
                } else {
                    frag.push_param(value.clone());
                }
            }
            crate::api_request::Operation::In(values) => {
                frag.push(" IN (");
                for (i, v) in values.iter().enumerate() {
                    if i > 0 {
                        frag.push(", ");
                    }
                    frag.push_param(v.clone());
                }
                frag.push(")");
            }
            crate::api_request::Operation::Is(is_val) => {
                frag.push(" IS ");
                frag.push(is_val.to_sql());
            }
            crate::api_request::Operation::IsDistinctFrom(value) => {
                frag.push(" IS DISTINCT FROM ");
                frag.push_param(value.clone());
            }
            crate::api_request::Operation::Fts { op, language, value } => {
                frag.push(" @@ ");
                frag.push(op.to_function());
                frag.push("(");
                if let Some(lang) = language {
                    frag.push_param(lang.clone());
                    frag.push(", ");
                }
                frag.push_param(value.clone());
                frag.push(")");
            }
        }

        Ok(frag)
    }

    /// Build an ORDER BY term.
    fn build_order_term(term: &CoercibleOrderTerm) -> OrderExpr {
        let mut order = OrderExpr::new(&term.field.name);

        if let Some(dir) = &term.direction {
            order = match dir {
                crate::api_request::OrderDirection::Asc => order.asc(),
                crate::api_request::OrderDirection::Desc => order.desc(),
            };
        }

        if let Some(nulls) = &term.nulls {
            order = match nulls {
                crate::api_request::OrderNulls::First => order.nulls_first(),
                crate::api_request::OrderNulls::Last => order.nulls_last(),
            };
        }

        order
    }

    /// Build a mutation query.
    pub fn build_mutate(plan: &MutatePlan) -> Result<SqlFragment> {
        match plan {
            MutatePlan::Insert {
                target,
                columns,
                body,
                on_conflict,
                returning,
                ..
            } => {
                let qi = postrust_sql::identifier::QualifiedIdentifier::new(
                    &target.schema,
                    &target.name,
                );

                let mut builder = InsertBuilder::new().into_table(&qi);

                // Column names
                let col_names: Vec<String> = columns.iter().map(|c| c.name.clone()).collect();
                builder = builder.columns(col_names);

                // For bulk insert, we'd use json_populate_recordset
                // For now, simplified single-row insert
                if let Some(body_bytes) = body {
                    // This would be expanded with proper JSON handling
                    let body_str = String::from_utf8_lossy(body_bytes);
                    let mut frag = SqlFragment::new();
                    frag.push("SELECT * FROM json_populate_recordset(NULL::");
                    frag.push(&from_qi(&qi));
                    frag.push(", ");
                    frag.push_param(body_str.to_string());
                    frag.push("::json)");
                    return Ok(frag);
                }

                // ON CONFLICT
                if let Some((resolution, conflict_cols)) = on_conflict {
                    match resolution {
                        crate::api_request::PreferResolution::IgnoreDuplicates => {
                            builder = builder.on_conflict_do_nothing();
                        }
                        crate::api_request::PreferResolution::MergeDuplicates => {
                            let set_cols: Vec<(String, SqlFragment)> = columns
                                .iter()
                                .map(|c| {
                                    let mut frag = SqlFragment::new();
                                    frag.push("EXCLUDED.");
                                    frag.push(&escape_ident(&c.name));
                                    (c.name.clone(), frag)
                                })
                                .collect();
                            builder = builder.on_conflict_do_update(conflict_cols.clone(), set_cols);
                        }
                    }
                }

                // RETURNING
                for col in returning {
                    builder = builder.returning(col);
                }

                Ok(builder.build())
            }

            MutatePlan::Update {
                target,
                columns,
                body,
                where_clauses,
                returning,
                ..
            } => {
                let qi = postrust_sql::identifier::QualifiedIdentifier::new(
                    &target.schema,
                    &target.name,
                );

                let builder = UpdateBuilder::new().table(&qi);

                // SET columns from body
                if let Some(body_bytes) = body {
                    let body_str = String::from_utf8_lossy(body_bytes);
                    // Simplified: would properly parse JSON and set columns
                    let mut frag = SqlFragment::new();
                    frag.push("UPDATE ");
                    frag.push(&from_qi(&qi));
                    frag.push(" SET ");

                    for (i, col) in columns.iter().enumerate() {
                        if i > 0 {
                            frag.push(", ");
                        }
                        frag.push(&escape_ident(&col.name));
                        frag.push(" = (");
                        frag.push_param(body_str.to_string());
                        frag.push("::json->>");
                        frag.push_param(col.name.clone());
                        frag.push(")::");
                        frag.push(&col.ir_type);
                    }

                    // WHERE
                    if !where_clauses.is_empty() {
                        frag.push(" WHERE ");
                        for (i, clause) in where_clauses.iter().enumerate() {
                            if i > 0 {
                                frag.push(" AND ");
                            }
                            frag.append(Self::build_logic_tree(clause)?);
                        }
                    }

                    // RETURNING
                    if !returning.is_empty() {
                        frag.push(" RETURNING ");
                        for (i, col) in returning.iter().enumerate() {
                            if i > 0 {
                                frag.push(", ");
                            }
                            frag.push(&escape_ident(col));
                        }
                    }

                    return Ok(frag);
                }

                Ok(builder.build())
            }

            MutatePlan::Delete {
                target,
                where_clauses,
                returning,
            } => {
                let qi = postrust_sql::identifier::QualifiedIdentifier::new(
                    &target.schema,
                    &target.name,
                );

                let mut builder = DeleteBuilder::new().from_table(&qi);

                // WHERE
                for clause in where_clauses {
                    let expr = Self::build_logic_tree(clause)?;
                    builder = builder.where_raw(expr);
                }

                // RETURNING
                for col in returning {
                    builder = builder.returning(col);
                }

                Ok(builder.build())
            }
        }
    }

    /// Build an RPC call query.
    pub fn build_call(plan: &CallPlan) -> Result<SqlFragment> {
        let qi = postrust_sql::identifier::QualifiedIdentifier::new(
            &plan.function.schema,
            &plan.function.name,
        );

        let mut frag = SqlFragment::new();
        frag.push("SELECT * FROM ");
        frag.push(&from_qi(&qi));
        frag.push("(");

        match &plan.params {
            CallParams::Named(params) => {
                for (i, (name, value)) in params.iter().enumerate() {
                    if i > 0 {
                        frag.push(", ");
                    }
                    frag.push(&escape_ident(name));
                    frag.push(" => ");
                    frag.push_param(SqlParam::Text(value.clone()));
                }
            }
            CallParams::Positional(values) => {
                for (i, value) in values.iter().enumerate() {
                    if i > 0 {
                        frag.push(", ");
                    }
                    frag.push_param(SqlParam::Text(value.clone()));
                }
            }
            CallParams::SingleObject(body) => {
                let body_str = String::from_utf8_lossy(body);
                frag.push_param(SqlParam::Text(body_str.to_string()));
            }
            CallParams::None => {}
        }

        frag.push(")");

        Ok(frag)
    }
}