Example Project
Let's say we need to write a program to generate a report for the demo HR schema. This report shall list median salaries for each country of the specified region. The name of the region and the connection parameters shall be specified as environment variables (maybe because our program will be executed in a container).
Project Setup
cargo new median-salary
Then edit Cargo.toml
and include Sibyl as a dependency:
[dependencies]
sibyl = { version="0.6", features=["blocking"] }
We will be writing a single threaded program, thus we will use Sibyl's blocking
API.
Implement the Solution
fn main() -> sibyl::Result<()> {
let oracle = sibyl::env()?;
let dbname = std::env::var("DBNAME").expect("database name");
let dbuser = std::env::var("DBUSER").expect("user name");
let dbpass = std::env::var("DBPASS").expect("password");
let region = std::env::var("REGION").expect("HR region");
let session = oracle.connect(&dbname, &dbuser, &dbpass)?;
let stmt = session.prepare("
SELECT c.country_name, Median(e.salary)
FROM hr.employees e
JOIN hr.departments d ON d.department_id = e.department_id
JOIN hr.locations l ON l.location_id = d.location_id
JOIN hr.countries c ON c.country_id = l.country_id
JOIN hr.regions r ON r.region_id = c.region_id
WHERE r.region_name = :REGION_NAME
GROUP BY c.country_name
")?;
let rows = stmt.query(®ion)?;
while let Some(row) = rows.next()? {
let country_name : &str = row.get(0)?;
let median_salary : u16 = row.get(1)?;
println!("{:25}: {:>5}", country_name, median_salary);
}
Ok(())
}
Build and Run
cargo build
And then run it:
DBNAME=localhost/orcl DBUSER=sibyl DBPASS=Or4cl3 REGION=Americas cargo run
⚠ Note that depending on the Oracle client installed you might also need to adjust
PATH
on Windows or specifyLD_LIBRARY_PATH
on Linux:
LD_LIBRARY_PATH=/opt/instantclient_19_24 DBNAME=localhost/orcl DBUSER=sibyl DBPASS=Or4cl3 REGION=Americas cargo run
Expected Output
Canada : 9500
United States of America : 3250