use std::error::Error;
use std::sync::Arc;
use vlorql::VlorQl;
use vlorql_core::observability::{VlorqMetrics, init_telemetry, shutdown_telemetry};
use vlorql_core::policy::PolicyConfig;
use vlorql_core::schema::{ColumnSchema, DataType, SchemaMetadata, SchemaSnapshot, TableSchema};
use vlorql_llm::{LlmConfig, LlmProvider, 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: 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(),
))
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let otlp_endpoint = std::env::var("OTEL_EXPORTER_OTLP_ENDPOINT")
.unwrap_or_else(|_| "http://localhost:4317".to_owned());
let _guard = init_telemetry("vlorql-example", &otlp_endpoint)?;
let metrics = Arc::new(VlorqMetrics::new());
let vlorql = VlorQl::builder()
.with_schema(build_schema())
.with_dialect_name("postgres")
.with_policy(PolicyConfig::default())
.with_llm_config(LlmConfig {
provider: LlmProvider::Ollama,
model: "mock-model".to_owned(),
..LlmConfig::default()
})
.with_llm_client(MockLlmClient::success({
use vlorql_core::schema::{
ComparisonOperator, Expression, FromClause, Predicate, Projection, 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(), 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: 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,
}
}))
.with_metrics(metrics)
.with_max_retries(0)
.build()?;
println!("Sending query…");
let (compiled, _usage) = vlorql.query("Show me active users with id > 10").await?;
println!("SQL: {}", compiled.sql);
if !compiled.parameters.is_empty() {
println!("Params:");
for (i, param) in compiled.parameters.iter().enumerate() {
println!(" ${}: {} ({:?})", i + 1, param.value, param.data_type);
}
}
println!("Shutting down telemetry…");
shutdown_telemetry(_guard);
println!("Done. Open http://localhost:16686 to view the trace.");
Ok(())
}