parse

Function parse 

Source
pub fn parse(input: &str) -> QailResult<QailCmd>
Expand description

Parse a complete QAIL query string (v2 syntax only).

Uses keyword-based syntax: get table fields * where col = value

Examples found in repository?
examples/test_floats.rs (line 18)
5fn main() {
6    let tests = [
7        ("Simple float", "get stats fields 100.0 as val"),
8        ("Zero float", "get stats fields 0.0 as val"),
9        ("Pi", "get stats fields 3.14 as val"),
10        (
11            "CASE with floats",
12            "get stats fields case when x > 0 then 100.0 else 0.0 end as rate",
13        ),
14    ];
15
16    for (name, test) in tests {
17        println!("{}:", name);
18        match parse(test) {
19            Ok(cmd) => println!("  ✅ {}\n", cmd.to_sql()),
20            Err(e) => println!("  ❌ {}\n", e),
21        }
22    }
23}
More examples
Hide additional examples
examples/test_param_repro.rs (line 6)
3fn main() {
4    let query = "get::t 'coalesce(uc.unread_count, 0)@unread_count";
5
6    match parse(query) {
7        Ok(cmd) => {
8            let res = cmd.to_sql_parameterized();
9            println!("SQL: {}", res.sql);
10            if res
11                .sql
12                .contains("coalesce(uc.unread_count, 0) AS \"unread_count\"")
13                || res
14                    .sql
15                    .contains("COALESCE(uc.unread_count, 0) AS \"unread_count\"")
16            {
17                println!("SUCCESS: Alias found correctly.");
18            } else {
19                println!("FAILURE: Alias missing in parameterized SQL.");
20                std::process::exit(1);
21            }
22        }
23        Err(e) => {
24            eprintln!("Parse error: {}", e);
25            std::process::exit(1);
26        }
27    }
28}
examples/test_cte_parse.rs (line 10)
5fn main() {
6    // Complete WhatsApp insights query with all 6 columns
7    let query = "with stats as (get whatsapp_messages fields count(distinct phone_number) as total_contacts, count(*) as total_messages, count(*) filter (where direction = 'outbound' and created_at > now() - 24h) as messages_sent_24h, count(*) filter (where direction = 'inbound' and created_at > now() - 24h) as messages_received_24h, count(*) filter (where direction = 'inbound' and status = 'received') as unread_messages, count(*) filter (where direction = 'outbound' and created_at > now() - 24h and status in ('delivered', 'read')) as successful_deliveries_24h) get stats";
8
9    println!("Complete WhatsApp Insights Query (6 columns):");
10    match parse(query) {
11        Ok(cmd) => println!("✅\n{}", cmd.to_sql()),
12        Err(e) => println!("❌ {}", e),
13    }
14}
examples/test_joins.rs (line 9)
5fn main() {
6    let q2 = "get a left join b on b.x = a.x left join c on c.y = a.y fields a.id";
7    println!(
8        "2 JOINs: {}",
9        parse(q2)
10            .map(|_| "OK".to_string())
11            .unwrap_or_else(|e| e.to_string())
12    );
13
14    let q3 = "get a left join b on b.x = a.x left join c on c.y = a.y left join d on d.z = a.z fields a.id";
15    println!(
16        "3 JOINs: {}",
17        parse(q3)
18            .map(|_| "OK".to_string())
19            .unwrap_or_else(|e| e.to_string())
20    );
21
22    let q7 = "get a left join b on b.x = a.x left join c on c.y = a.y left join d on d.z = a.z left join e on e.w = a.w left join f on f.v = a.v left join g on g.u = a.u left join h on h.t = a.t fields a.id";
23    println!(
24        "7 JOINs: {}",
25        parse(q7)
26            .map(|c| format!("OK - {} joins", c.joins.len()))
27            .unwrap_or_else(|e| e.to_string())
28    );
29}
examples/test_subquery.rs (line 13)
6fn main() {
7    println!("=== Testing INSERT...SELECT ===\n");
8
9    // Test 1: Basic INSERT SELECT
10    let query1 =
11        "add archive fields id, name from (get users fields id, name where active = false)";
12    println!("Query 1: {}", query1);
13    match parse(query1) {
14        Ok(cmd) => {
15            println!("  source_query: {:?}", cmd.source_query.is_some());
16            println!("  SQL: {}\n", cmd.to_sql());
17        }
18        Err(e) => println!("  Parse error: {:?}\n", e),
19    }
20
21    println!("=== Testing Subquery in SET ===\n");
22
23    // Test 2: Subquery in UPDATE
24    let query2 = "set orders values total = (get items fields sum(price) where order_id = orders.id) where id = :id";
25    println!("Query 2: {}", query2);
26    match parse(query2) {
27        Ok(cmd) => println!("  SQL: {}\n", cmd.to_sql()),
28        Err(e) => println!("  Parse error: {:?}\n", e),
29    }
30
31    // Test 3: Simple subquery value
32    let query3 =
33        "set users values role_id = (get roles fields id where name = 'admin') where user_id = :id";
34    println!("Query 3: {}", query3);
35    match parse(query3) {
36        Ok(cmd) => println!("  SQL: {}\n", cmd.to_sql()),
37        Err(e) => println!("  Parse error: {:?}\n", e),
38    }
39}
examples/audit_syntax.rs (line 12)
6fn main() {
7    println!("=== Testing DISTINCT ON with Expressions ===\n");
8
9    // Test 1: Simple DISTINCT ON (column)
10    let q1 = "get distinct on (phone_number) messages fields phone_number, content order by phone_number, created_at desc";
11    println!("Test 1 (DISTINCT ON column):");
12    match parse(q1) {
13        Ok(cmd) => println!("  OK: {}\n", cmd.to_sql()),
14        Err(e) => println!("  ERR: {}\n", e),
15    }
16
17    // Test 2: DISTINCT ON with CASE WHEN expression
18    let q2 = r#"get distinct on (case when phone like '0%' then '62' || substring(phone from 2) else phone end) orders 
19        fields phone, name order by case when phone like '0%' then '62' || substring(phone from 2) else phone end, created_at desc"#;
20    println!("Test 2 (DISTINCT ON CASE WHEN):");
21    match parse(q2) {
22        Ok(cmd) => println!("  OK: {}\n", cmd.to_sql()),
23        Err(e) => println!("  ERR: {}\n", e),
24    }
25
26    // Test 3: DISTINCT ON with function
27    let q3 = "get distinct on (lower(email)) users fields email, name order by lower(email), id";
28    println!("Test 3 (DISTINCT ON function):");
29    match parse(q3) {
30        Ok(cmd) => println!("  OK: {}\n", cmd.to_sql()),
31        Err(e) => println!("  ERR: {}\n", e),
32    }
33}