use super::CommandResult;
use oxirs_core::rdf_store::RdfStore;
use std::fs;
use std::path::PathBuf;
use std::time::Instant;
pub async fn run(dataset: String, update: String, file: bool) -> CommandResult {
println!("Executing SPARQL update on dataset '{dataset}'");
let sparql_update = if file {
let update_path = PathBuf::from(&update);
if !update_path.exists() {
return Err(format!("Update file '{}' does not exist", update_path.display()).into());
}
fs::read_to_string(update_path)?
} else {
update
};
println!("Update:");
println!("---");
println!("{sparql_update}");
println!("---");
let dataset_path = if PathBuf::from(&dataset).join("oxirs.toml").exists() {
load_dataset_from_config(&dataset)?
} else {
PathBuf::from(&dataset)
};
println!("Opening dataset at: {}", dataset_path.display());
let store = if dataset_path.exists() {
RdfStore::open(&dataset_path).map_err(|e| format!("Failed to open dataset: {e}"))?
} else {
println!("Creating new dataset at: {}", dataset_path.display());
RdfStore::open(&dataset_path).map_err(|e| format!("Failed to create dataset: {e}"))?
};
let start_time = Instant::now();
println!("\nExecuting SPARQL update...");
execute_update(&store, &sparql_update)?;
let duration = start_time.elapsed();
println!(
"Update executed successfully in {:.3} seconds",
duration.as_secs_f64()
);
Ok(())
}
fn load_dataset_from_config(dataset: &str) -> Result<PathBuf, Box<dyn std::error::Error>> {
crate::config::load_dataset_from_config(dataset)
.map_err(|e| Box::new(e) as Box<dyn std::error::Error>)
}
fn execute_update(store: &RdfStore, update: &str) -> Result<(), Box<dyn std::error::Error>> {
use oxirs_core::query::update::{UpdateExecutor, UpdateParser};
println!(" [1/2] Parsing SPARQL UPDATE...");
let parser = UpdateParser::new();
let parsed_update = parser
.parse(update)
.map_err(|e| format!("SPARQL UPDATE parse error: {e}"))?;
println!(" ✓ Parsed successfully");
println!(" Operations: {}", parsed_update.operations.len());
println!(" [2/2] Executing update operations...");
let executor = UpdateExecutor::new(store);
executor
.execute(&parsed_update)
.map_err(|e| format!("SPARQL UPDATE execution error: {e}"))?;
println!(" ✓ All operations completed successfully");
Ok(())
}