use chdb_rust::{
session::SessionBuilder,
arg::Arg,
format::OutputFormat,
log_level::LogLevel
};
use tempdir::TempDir;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a temporary directory for database storage
let tmp = TempDir::new("chdb-rust")?;
// Build session with configuration
let session = SessionBuilder::new()
.with_data_path(tmp.path())
.with_arg(Arg::LogLevel(LogLevel::Debug))
.with_auto_cleanup(true) // Cleanup on drop
.build()?;
// Create database and table
session.execute(
"CREATE DATABASE demo; USE demo",
Some(&[Arg::MultiQuery])
)?;
session.execute(
"CREATE TABLE logs (id UInt64, msg String) ENGINE = MergeTree() ORDER BY id",
None,
)?;
// Insert data
session.execute(
"INSERT INTO logs (id, msg) VALUES (1, 'Hello'), (2, 'World')",
None,
)?;
// Query data
let result = session.execute(
"SELECT * FROM logs ORDER BY id",
Some(&[Arg::OutputFormat(OutputFormat::JSONEachRow)]),
)?;
println!("Query results:\n{}", result.data_utf8()?);
// Get query statistics
println!("Rows read: {}", result.rows_read());
println!("Bytes read: {}", result.bytes_read());
println!("Query time: {:?}", result.elapsed());
Ok(())
}