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 15)
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        ("CASE with floats", "get stats fields case when x > 0 then 100.0 else 0.0 end as rate"),
11    ];
12    
13    for (name, test) in tests {
14        println!("{}:", name);
15        match parse(test) {
16            Ok(cmd) => println!("  ✅ {}\n", cmd.to_sql()),
17            Err(e) => println!("  ❌ {}\n", e),
18        }
19    }
20}
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.sql.contains("coalesce(uc.unread_count, 0) AS \"unread_count\"") || res.sql.contains("COALESCE(uc.unread_count, 0) AS \"unread_count\"") {
11                println!("SUCCESS: Alias found correctly.");
12            } else {
13                println!("FAILURE: Alias missing in parameterized SQL.");
14                std::process::exit(1);
15            }
16        }
17        Err(e) => {
18            eprintln!("Parse error: {}", e);
19            std::process::exit(1);
20        }
21    }
22}
examples/test_joins.rs (line 7)
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!("2 JOINs: {}", parse(q2).map(|_| "OK".to_string()).unwrap_or_else(|e| e.to_string()));
8    
9    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";
10    println!("3 JOINs: {}", parse(q3).map(|_| "OK".to_string()).unwrap_or_else(|e| e.to_string()));
11    
12    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";
13    println!("7 JOINs: {}", parse(q7).map(|c| format!("OK - {} joins", c.joins.len())).unwrap_or_else(|e| e.to_string()));
14}
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_subquery.rs (line 12)
6fn main() {
7    println!("=== Testing INSERT...SELECT ===\n");
8    
9    // Test 1: Basic INSERT SELECT
10    let query1 = "add archive fields id, name from (get users fields id, name where active = false)";
11    println!("Query 1: {}", query1);
12    match parse(query1) {
13        Ok(cmd) => {
14            println!("  source_query: {:?}", cmd.source_query.is_some());
15            println!("  SQL: {}\n", cmd.to_sql());
16        }
17        Err(e) => println!("  Parse error: {:?}\n", e),
18    }
19    
20    println!("=== Testing Subquery in SET ===\n");
21    
22    // Test 2: Subquery in UPDATE
23    let query2 = "set orders values total = (get items fields sum(price) where order_id = orders.id) where id = :id";
24    println!("Query 2: {}", query2);
25    match parse(query2) {
26        Ok(cmd) => println!("  SQL: {}\n", cmd.to_sql()),
27        Err(e) => println!("  Parse error: {:?}\n", e),
28    }
29    
30    // Test 3: Simple subquery value
31    let query3 = "set users values role_id = (get roles fields id where name = 'admin') where user_id = :id";
32    println!("Query 3: {}", query3);
33    match parse(query3) {
34        Ok(cmd) => println!("  SQL: {}\n", cmd.to_sql()),
35        Err(e) => println!("  Parse error: {:?}\n", e),
36    }
37}
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}