Skip to main content

octofhir_sof/
lib.rs

1//! SQL on FHIR implementation for OctoFHIR.
2//!
3//! This crate provides support for the SQL on FHIR specification, which enables
4//! transformation of FHIR resources into tabular data using ViewDefinition resources.
5//!
6//! # Overview
7//!
8//! SQL on FHIR allows you to define views over FHIR resources that can be materialized
9//! as relational tables. This is useful for analytics, reporting, and integration with
10//! SQL-based tools.
11//!
12//! # Components
13//!
14//! - [`ViewDefinition`] - Parsed representation of a FHIR ViewDefinition resource
15//! - [`SqlGenerator`] - Generates SQL from ViewDefinitions
16//! - [`ViewRunner`] - Executes views against a PostgreSQL database
17//!
18//! # Example
19//!
20//! ```ignore
21//! use octofhir_sof::{ViewDefinition, SqlGenerator, ViewRunner};
22//!
23//! // Parse a ViewDefinition
24//! let view_def = ViewDefinition::from_json(&json)?;
25//!
26//! // Generate SQL
27//! let generator = SqlGenerator::new();
28//! let sql = generator.generate(&view_def)?;
29//!
30//! // Execute the view
31//! let runner = ViewRunner::new(pool, generator);
32//! let result = runner.run(&view_def).await?;
33//! ```
34//!
35//! # SQL on FHIR Specification
36//!
37//! See: <https://build.fhir.org/ig/FHIR/sql-on-fhir-v2/>
38
39mod column;
40mod eval;
41pub mod output;
42mod runner;
43mod sql_generator;
44mod view_definition;
45
46pub use column::{ColumnInfo, ColumnType};
47pub use eval::{CompiledView, execute, execute_blocking};
48pub use runner::{SqlExecutor, ViewResult, ViewRunner, run_with};
49pub use sql_generator::{Dialect, GeneratedColumn, GeneratedSql, SqlGenerator, create_table};
50pub use view_definition::{Column, Constant, SelectColumn, ViewDefinition, WhereClause};
51
52use thiserror::Error;
53
54/// Errors that can occur during SQL on FHIR operations.
55#[derive(Debug, Error)]
56pub enum Error {
57    /// The ViewDefinition JSON is invalid or missing required fields.
58    #[error("Invalid ViewDefinition: {0}")]
59    InvalidViewDefinition(String),
60
61    /// A FHIRPath expression is invalid or cannot be converted to SQL.
62    #[error("Invalid path: {0}")]
63    InvalidPath(String),
64
65    /// An error occurred while executing SQL.
66    #[error("SQL execution error: {0}")]
67    Sql(#[from] sqlx_core::error::Error),
68
69    /// An error occurred during FHIRPath evaluation or compilation.
70    #[error("FHIRPath error: {0}")]
71    FhirPath(String),
72
73    /// An error occurred while generating output.
74    #[error("Output error: {0}")]
75    Output(String),
76
77    /// JSON serialization/deserialization error.
78    #[error("JSON error: {0}")]
79    Json(#[from] serde_json::Error),
80}
81
82/// Result type alias using the crate's Error type.
83pub type Result<T> = std::result::Result<T, Error>;