use std::collections::HashMap;
use std::error::Error;
use std::sync::Arc;
use serde_json::json;
use vlorql::{SchemaSnapshot, VlorQl};
use vlorql_core::policy::{PolicyConfig, TablePolicy};
use vlorql_core::schema::{
ColumnSchema, ComparisonOperator, DataType, Expression, FromClause, Predicate, Projection,
QueryPlan, SchemaMetadata, TableSchema,
};
use vlorql_llm::{LlmClient, MockLlmClient};
fn build_schema() -> Arc<SchemaSnapshot> {
Arc::new(SchemaSnapshot::new(
vec![TableSchema {
name: "users".to_owned(),
columns: vec![
ColumnSchema {
name: "id".to_owned(),
data_type: DataType::Int,
nullable: false,
description: None,
is_primary_key: true,
foreign_key: None,
},
ColumnSchema {
name: "name".to_owned(),
data_type: DataType::String,
nullable: false,
description: None,
is_primary_key: false,
foreign_key: None,
},
ColumnSchema {
name: "email".to_owned(),
data_type: DataType::String,
nullable: false,
description: Some("Personally identifiable information".to_owned()),
is_primary_key: false,
foreign_key: None,
},
],
description: Some("Application users".to_owned()),
primary_key: Some(vec!["id".to_owned()]),
}],
SchemaMetadata::default(),
))
}
fn build_policy() -> PolicyConfig {
let mut table_policies = HashMap::new();
table_policies.insert(
"users".to_owned(),
TablePolicy {
allowed: true,
allowed_columns: Some(vec!["id".to_owned(), "name".to_owned()]),
denied_columns: vec!["email".to_owned()],
row_filter: None,
},
);
PolicyConfig {
table_policies,
global_denied_columns: Vec::new(),
row_filters: Vec::new(),
}
}
fn allowed_plan() -> QueryPlan {
QueryPlan {
select: vec![
Projection::Column {
table: Some("users".to_owned()),
column: "id".to_owned(),
alias: None,
},
Projection::Column {
table: Some("users".to_owned()),
column: "name".to_owned(),
alias: None,
},
],
from: FromClause::table("users".to_owned(), None),
r#where: Some(Predicate::Comparison {
left: Box::new(Expression::ColumnRef {
table: Some("users".to_owned()),
column: "id".to_owned(),
}),
op: ComparisonOperator::Gt,
right: Box::new(Expression::Literal {
value: json!(10),
data_type: DataType::Int,
}),
}),
group_by: None,
having: None,
order_by: None,
limit: None,
offset: None,
joins: None,
ctes: None,
distinct: false,
distinct_on: None,
set_operation: None,
}
}
fn denied_plan() -> QueryPlan {
QueryPlan {
select: vec![Projection::Column {
table: Some("users".to_owned()),
column: "email".to_owned(),
alias: None,
}],
from: FromClause::table("users".to_owned(), None),
r#where: None,
group_by: None,
having: None,
order_by: None,
limit: None,
offset: None,
joins: None,
ctes: None,
distinct: false,
distinct_on: None,
set_operation: None,
}
}
async fn run_scenario(label: &str, client: Box<dyn LlmClient>) -> Result<(), Box<dyn Error>> {
let vlorql = VlorQl::builder()
.with_schema(build_schema())
.with_dialect_name("postgres")
.with_policy(build_policy())
.with_llm_client(client)
.with_max_retries(0)
.build()?;
println!("--- {label} ---");
match vlorql.query("policy demo").await {
Ok((compiled, _usage)) => {
println!("PASS sql: {}", compiled.sql);
println!(" params: {}", compiled.parameters.len());
}
Err(error) => {
println!("DENY code: {}", error.error_code());
println!(" error: {error}");
if matches!(error, vlorql_core::errors::VlorQLError::Validation { .. }) {
println!(" kind: Validation (policy/schema/dialect)");
}
}
}
println!();
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
run_scenario(
"allowed: SELECT id, name FROM users WHERE id > 10",
Box::new(MockLlmClient::success(allowed_plan())),
)
.await?;
run_scenario(
"denied: SELECT email FROM users",
Box::new(MockLlmClient::success(denied_plan())),
)
.await?;
let vlorql = VlorQl::builder()
.with_schema(build_schema())
.with_dialect_name("postgres")
.with_policy(build_policy())
.build()?;
let errors = vlorql
.validate_only(&denied_plan())
.expect_err("policy should deny email");
println!("--- validate_only summary for denied plan ---");
println!("error_count: {}", errors.len());
for error in errors.as_slice() {
let variant = match error {
vlorql_core::errors::VlorQLError::Policy { .. } => "Policy",
vlorql_core::errors::VlorQLError::Schema { .. } => "Schema",
vlorql_core::errors::VlorQLError::Validation { .. } => "Validation",
_ => "Other",
};
println!("- variant: {variant}, code: {}", error.error_code());
}
Ok(())
}