|
| 1 | +//! Direct scanning that bypasses protobuf serialization/deserialization. |
| 2 | +//! |
| 3 | +//! This module provides a faster alternative to the protobuf-based scanning by |
| 4 | +//! directly reading the scanner's token output and converting it to Rust protobuf types. |
| 5 | +
|
| 6 | +use crate::bindings; |
| 7 | +use crate::bindings_raw; |
| 8 | +use crate::protobuf; |
| 9 | +use crate::{Error, Result}; |
| 10 | +use std::ffi::{CStr, CString}; |
| 11 | + |
| 12 | +/// Scans a SQL statement directly into protobuf types without going through protobuf serialization. |
| 13 | +/// |
| 14 | +/// This function is faster than `scan` because it skips the protobuf encode/decode step. |
| 15 | +/// The tokens are read directly from the C scanner output. |
| 16 | +/// |
| 17 | +/// # Example |
| 18 | +/// |
| 19 | +/// ```rust |
| 20 | +/// let result = pg_query::scan_raw("SELECT * FROM users").unwrap(); |
| 21 | +/// assert!(!result.tokens.is_empty()); |
| 22 | +/// ``` |
| 23 | +pub fn scan_raw(sql: &str) -> Result<protobuf::ScanResult> { |
| 24 | + let input = CString::new(sql)?; |
| 25 | + let result = unsafe { bindings_raw::pg_query_scan_raw(input.as_ptr()) }; |
| 26 | + |
| 27 | + let scan_result = if !result.error.is_null() { |
| 28 | + let message = unsafe { CStr::from_ptr((*result.error).message) }.to_string_lossy().to_string(); |
| 29 | + Err(Error::Scan(message)) |
| 30 | + } else { |
| 31 | + // Convert the C tokens to protobuf types |
| 32 | + let tokens = unsafe { convert_tokens(result.tokens, result.n_tokens) }; |
| 33 | + Ok(protobuf::ScanResult { version: bindings::PG_VERSION_NUM as i32, tokens }) |
| 34 | + }; |
| 35 | + |
| 36 | + unsafe { bindings_raw::pg_query_free_raw_scan_result(result) }; |
| 37 | + scan_result |
| 38 | +} |
| 39 | + |
| 40 | +/// Converts C scan tokens to protobuf ScanToken vector. |
| 41 | +unsafe fn convert_tokens(tokens: *mut bindings_raw::PgQueryRawScanToken, n_tokens: usize) -> Vec<protobuf::ScanToken> { |
| 42 | + if tokens.is_null() || n_tokens == 0 { |
| 43 | + return Vec::new(); |
| 44 | + } |
| 45 | + |
| 46 | + let mut result = Vec::with_capacity(n_tokens); |
| 47 | + |
| 48 | + for i in 0..n_tokens { |
| 49 | + let token = &*tokens.add(i); |
| 50 | + result.push(protobuf::ScanToken { start: token.start, end: token.end, token: token.token, keyword_kind: token.keyword_kind }); |
| 51 | + } |
| 52 | + |
| 53 | + result |
| 54 | +} |
| 55 | + |
| 56 | +#[cfg(test)] |
| 57 | +mod tests { |
| 58 | + use super::*; |
| 59 | + |
| 60 | + #[test] |
| 61 | + fn test_scan_raw_basic() { |
| 62 | + let result = scan_raw("SELECT * FROM users").unwrap(); |
| 63 | + assert!(!result.tokens.is_empty()); |
| 64 | + // First token should be SELECT |
| 65 | + assert_eq!(result.tokens[0].start, 0); |
| 66 | + assert_eq!(result.tokens[0].end, 6); |
| 67 | + } |
| 68 | + |
| 69 | + #[test] |
| 70 | + fn test_scan_raw_matches_scan() { |
| 71 | + let sql = "SELECT id, name FROM users WHERE active = true"; |
| 72 | + let raw_result = scan_raw(sql).unwrap(); |
| 73 | + let prost_result = crate::scan(sql).unwrap(); |
| 74 | + |
| 75 | + assert_eq!(raw_result.version, prost_result.version); |
| 76 | + assert_eq!(raw_result.tokens.len(), prost_result.tokens.len()); |
| 77 | + |
| 78 | + for (raw_token, prost_token) in raw_result.tokens.iter().zip(prost_result.tokens.iter()) { |
| 79 | + assert_eq!(raw_token.start, prost_token.start); |
| 80 | + assert_eq!(raw_token.end, prost_token.end); |
| 81 | + assert_eq!(raw_token.token, prost_token.token); |
| 82 | + assert_eq!(raw_token.keyword_kind, prost_token.keyword_kind); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn test_scan_raw_empty() { |
| 88 | + let result = scan_raw("").unwrap(); |
| 89 | + assert!(result.tokens.is_empty()); |
| 90 | + } |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn test_scan_raw_complex() { |
| 94 | + let sql = r#"SELECT "column" AS left /* comment */ FROM between"#; |
| 95 | + let result = scan_raw(sql).unwrap(); |
| 96 | + assert!(!result.tokens.is_empty()); |
| 97 | + } |
| 98 | +} |
0 commit comments