#![forbid(unsafe_code)]
#![warn(missing_docs)]
mod ast;
mod fold;
mod fold_parse;
mod lex;
mod parse;
mod parse_dump;
mod parse_view;
mod plan;
mod render;
mod schema;
mod typemap;
mod viewplan;
mod viewplan_norm;
mod viewplan_view;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SqlError {
pub line: u32,
pub col: u32,
pub message: String,
}
impl SqlError {
pub(crate) fn at(line: u32, col: u32, message: impl Into<String>) -> SqlError {
SqlError { line, col, message: message.into() }
}
}
impl std::fmt::Display for SqlError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "line {}, col {}: {}", self.line, self.col, self.message)
}
}
impl std::error::Error for SqlError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KevyType {
I64,
F64,
Str,
}
impl KevyType {
pub fn tag(self) -> &'static str {
match self {
KevyType::I64 => "i64",
KevyType::F64 => "f64",
KevyType::Str => "str",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CardParam {
pub n: u32,
pub column: String,
pub ty: KevyType,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct QueryCard {
pub name: String,
pub argv: Vec<String>,
pub params: Vec<CardParam>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Compilation {
pub commands: Vec<Vec<String>>,
pub query_cards: Vec<QueryCard>,
pub notes: Vec<String>,
}
impl Compilation {
pub fn render_script(&self) -> String {
render::render(self)
}
}
pub use fold::{Folded, fold_select};
pub use kevy_scalar::Scalar;
pub use plan::{Plan, PlanEntry, Served, plan};
pub fn compile(sql: &str) -> Result<Compilation, SqlError> {
let toks = lex::lex(sql)?;
let stmts = parse::parse_script(&toks)?;
let (tables, views, mut notes) = schema::build(&stmts)?;
let mut commands: Vec<Vec<String>> = tables.iter().map(schema::declare_argv).collect();
let mut query_cards = Vec::new();
for v in &views {
let Some(t) = tables.iter().find(|t| t.name == v.table) else {
return Err(SqlError::at(
v.line,
v.col,
format!(
"view '{}': FROM unknown table '{}' — CREATE TABLE it first (this compiler is whole-file: declare, then view)",
v.name, v.table
),
));
};
match viewplan::plan_view(v, t, &mut notes)? {
viewplan::Planned::View(argv) => commands.push(argv),
viewplan::Planned::Card(card) => query_cards.push(card),
}
}
Ok(Compilation { commands, query_cards, notes })
}