use crate::bind::BoundExpr;
use crate::catalog_view::ColumnInfo;
use crate::directive::PragmaArgument;
use crate::vtab::Declaration;
pub fn declared_columns(declaration: &Declaration) -> Vec<ColumnInfo> {
declaration
.columns
.iter()
.map(|column| ColumnInfo {
folded: column.name.to_ascii_lowercase(),
name: column.name.clone(),
declared_type: column.declared_type.clone(),
affinity: column.affinity,
collation: column.collation.clone(),
not_null: false,
not_null_conflict: None,
primary_key_conflict: None,
default_sql: None,
primary_key_position: None,
hidden: column.hidden,
generated: false,
stored: true,
generated_sql: None,
})
.collect()
}
pub fn argument_text(argument: &PragmaArgument) -> String {
match argument {
PragmaArgument::Name(name) => String::from_utf8_lossy(name).into_owned(),
PragmaArgument::Value(expr) => expression_text(expr),
}
}
fn expression_text(expr: &BoundExpr) -> String {
match expr {
BoundExpr::Text(text) => String::from_utf8_lossy(text).into_owned(),
BoundExpr::Integer(value) => value.to_string(),
BoundExpr::Real(value) => value.to_string(),
BoundExpr::Unary { op, operand } => match op {
crate::ast::UnaryOp::Negate => format!("-{}", expression_text(operand)),
crate::ast::UnaryOp::Identity => expression_text(operand),
_ => String::new(),
},
_ => String::new(),
}
}
pub fn argument_boolean(argument: &PragmaArgument) -> bool {
let text = argument_text(argument);
let folded = text.trim().to_ascii_lowercase();
match folded.as_str() {
"on" | "yes" | "true" => true,
"off" | "no" | "false" => false,
_ => folded
.parse::<i64>()
.map(|value| value != 0)
.unwrap_or(false),
}
}
pub fn argument_integer(argument: &PragmaArgument) -> i64 {
argument_text(argument).trim().parse().unwrap_or(0)
}