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
use std::{
  convert::TryFrom,
  ffi::{CStr, CString},
};

use libpg_query::{pg_query_free_parse_result, pg_query_parse};
use litrs::StringLit;
use proc_macro::TokenStream;
use quote::quote;

#[proc_macro]
pub fn sql(input: TokenStream) -> TokenStream {
  let input = input.into_iter().collect::<Vec<_>>();
  if input.len() != 1 {
    let msg = format!(
      "expected exactly one string literal as input, got {} tokens",
      input.len()
    );
    return quote! { compile_error!(#msg) }.into();
  }
  let string_lit = match StringLit::try_from(&input[0]) {
    Ok(lit) => lit,
    Err(e) => return e.to_compile_error(),
  };

  if let Err(e) = check_sql(string_lit.value()) {
    let msg = format!("failed to parse SQL query: {}", e);
    return quote! { compile_error!(#msg) }.into();
  }

  input.into_iter().collect()
}

fn check_sql(sql: &str) -> Result<(), String> {
  let c_str = CString::new(sql).unwrap();
  let pg_parse_result = unsafe { pg_query_parse(c_str.as_ptr()) };

  let result = if pg_parse_result.error.is_null() {
    Ok(())
  }
  else if let Ok(message) = unsafe { CStr::from_ptr((*pg_parse_result.error).message) }.to_str() {
    Err(message.to_owned())
  }
  else {
    Err("parse failed, and error message wasn't valid UTF-8".to_owned())
  };

  unsafe {
    pg_query_free_parse_result(pg_parse_result);
  }

  result
}