|
| 1 | +//! Binding printed SQL in DuckDB, when the crate is built with the engine. |
| 2 | +
|
| 3 | +#[cfg(feature = "engine")] |
| 4 | +use crate::checking::_helpers::engine::with_engine; |
| 5 | +#[cfg(feature = "engine")] |
| 6 | +use crate::checking::_helpers::tables::{function_definitions, table_definitions}; |
| 7 | +#[cfg(feature = "engine")] |
| 8 | +use crate::checking::constants::PARSER_ERROR; |
| 9 | +#[cfg(feature = "engine")] |
| 10 | +use crate::checking::errors::CheckStage; |
| 11 | +use crate::checking::errors::PrintedSqlFailure; |
| 12 | +use crate::checking::types::{FunctionLookup, RelationLookup}; |
| 13 | + |
| 14 | +/// DuckDB parses `sql` and binds it over the relations and functions `names` reads. |
| 15 | +#[cfg(feature = "engine")] |
| 16 | +pub(crate) fn bound( |
| 17 | + sql: &str, |
| 18 | + names: &[String], |
| 19 | + (lookup, functions): (RelationLookup<'_>, FunctionLookup<'_>), |
| 20 | +) -> Result<(), PrintedSqlFailure> { |
| 21 | + with_engine(|engine| { |
| 22 | + let mut definitions = table_definitions(names, lookup); |
| 23 | + definitions.extend(function_definitions(names, functions)); |
| 24 | + engine.keep_only(&definitions.iter().map(|(name, _)| name.as_str()).collect())?; |
| 25 | + for (name, statement) in &definitions { |
| 26 | + engine.define_table(name, statement)?; |
| 27 | + } |
| 28 | + engine.connection.prepare(sql).map(drop).map_err(|error| { |
| 29 | + let message = error.to_string(); |
| 30 | + let stage = match message.starts_with(PARSER_ERROR) { |
| 31 | + true => CheckStage::Parse, |
| 32 | + false => CheckStage::Bind, |
| 33 | + }; |
| 34 | + PrintedSqlFailure { stage, message } |
| 35 | + }) |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +/// Without the engine the caller binds the SQL itself, so only the lexical checks apply. |
| 40 | +#[cfg(not(feature = "engine"))] |
| 41 | +pub(crate) fn bound( |
| 42 | + _sql: &str, |
| 43 | + _names: &[String], |
| 44 | + _lookups: (RelationLookup<'_>, FunctionLookup<'_>), |
| 45 | +) -> Result<(), PrintedSqlFailure> { |
| 46 | + Ok(()) |
| 47 | +} |
0 commit comments