use std::error::Error;
use std::sync::Arc;
use vlorql::{SchemaSnapshot, SqlDialect, VlorQl};
use vlorql_core::policy::PolicyConfig;
use vlorql_core::schema::{
ColumnSchema, ComparisonOperator, DataType, Expression, FromClause, Predicate, Projection,
QueryPlan, SchemaMetadata, TableSchema,
};
use vlorql_llm::{LlmClient, LlmConfig, LlmProvider, MockLlmClient, OpenAIClient};
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: Some("User identifier".to_owned()),
is_primary_key: true,
foreign_key: None,
},
ColumnSchema {
name: "email".to_owned(),
data_type: DataType::String,
nullable: false,
description: Some("Primary email address".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 sample_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: "email".to_owned(),
alias: None,
},
],
from: FromClause {
table: "users".to_owned(),
alias: None,
},
r#where: Some(Predicate::Comparison {
left: Expression::ColumnRef {
table: Some("users".to_owned()),
column: "id".to_owned(),
},
op: ComparisonOperator::Gt,
right: Expression::Literal {
value: serde_json::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 select_llm_client() -> Box<dyn LlmClient> {
if let Ok(api_key) = std::env::var("OPENAI_API_KEY")
&& !api_key.trim().is_empty()
{
let model = std::env::var("OPENAI_MODEL").unwrap_or_else(|_| "gpt-4o-mini".to_owned());
let api_base = std::env::var("OPENAI_API_BASE").ok();
let config = LlmConfig {
provider: LlmProvider::OpenAi,
api_key: Some(api_key),
api_base,
model,
..LlmConfig::default()
};
println!("[basic_query] using OpenAI-compatible client: {config:?}");
return Box::new(OpenAIClient::from_config(config));
}
println!("[basic_query] OPENAI_API_KEY not set; using MockLlmClient");
Box::new(MockLlmClient::success(sample_plan()))
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let schema = build_schema();
let vlorql = VlorQl::builder()
.with_schema(schema)
.with_dialect_name("postgres")
.with_policy(PolicyConfig::default())
.with_llm_client(select_llm_client())
.with_max_retries(2)
.build()?;
let compiled = vlorql.query("List users with id greater than 10").await?;
println!("dialect: {:?}", SqlDialect::Postgres);
println!("sql: {}", compiled.sql);
if compiled.parameters.is_empty() {
println!("parameters: []");
} else {
println!("parameters:");
for (index, parameter) in compiled.parameters.iter().enumerate() {
println!(
" ${}: value={}, type={:?}",
index + 1,
parameter.value,
parameter.data_type
);
}
}
Ok(())
}