use pyo3::prelude::*;
#[cfg(feature = "database")]
use crate::database::{DatabaseConfig, analyze_database, create_connector};
use crate::python::types::PyProfileReport;
#[pyfunction]
#[pyo3(signature = (connection_string, query, batch_size=10000, calculate_quality=false))]
pub fn analyze_database_async<'py>(
py: Python<'py>,
connection_string: String,
query: String,
batch_size: usize,
calculate_quality: bool,
) -> PyResult<Bound<'py, PyAny>> {
#[cfg(feature = "python-async")]
{
pyo3_async_runtimes::tokio::future_into_py(py, async move {
analyze_database_internal(connection_string, query, batch_size, calculate_quality)
.await
.map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(e.to_string()))
})
}
#[cfg(not(feature = "python-async"))]
{
Err(pyo3::exceptions::PyRuntimeError::new_err(
"Async support not enabled. Please compile with --features python-async",
))
}
}
#[cfg(all(feature = "database", feature = "python-async"))]
async fn analyze_database_internal(
connection_string: String,
query: String,
batch_size: usize,
calculate_quality: bool,
) -> Result<PyProfileReport, crate::core::errors::DataProfilerError> {
let config = DatabaseConfig {
connection_string,
batch_size,
..Default::default()
};
let report = analyze_database(config, &query, calculate_quality, None).await?;
Ok(PyProfileReport::new(report))
}
#[pyfunction]
pub fn test_connection_async<'py>(
py: Python<'py>,
connection_string: String,
) -> PyResult<Bound<'py, PyAny>> {
#[cfg(feature = "python-async")]
{
pyo3_async_runtimes::tokio::future_into_py(py, async move {
test_connection_internal(connection_string)
.await
.map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(e.to_string()))
})
}
#[cfg(not(feature = "python-async"))]
{
Err(pyo3::exceptions::PyRuntimeError::new_err(
"Async support not enabled. Please compile with --features python-async",
))
}
}
#[cfg(all(feature = "database", feature = "python-async"))]
async fn test_connection_internal(
connection_string: String,
) -> Result<bool, crate::core::errors::DataProfilerError> {
let config = DatabaseConfig {
connection_string,
..Default::default()
};
let mut connector = create_connector(config)?;
connector.connect().await?;
let result = connector.test_connection().await;
connector.disconnect().await?;
result
}
#[pyfunction]
pub fn get_table_schema_async<'py>(
py: Python<'py>,
connection_string: String,
table_name: String,
) -> PyResult<Bound<'py, PyAny>> {
#[cfg(feature = "python-async")]
{
pyo3_async_runtimes::tokio::future_into_py(py, async move {
get_table_schema_internal(connection_string, table_name)
.await
.map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(e.to_string()))
})
}
#[cfg(not(feature = "python-async"))]
{
Err(pyo3::exceptions::PyRuntimeError::new_err(
"Async support not enabled. Please compile with --features python-async",
))
}
}
#[cfg(all(feature = "database", feature = "python-async"))]
async fn get_table_schema_internal(
connection_string: String,
table_name: String,
) -> Result<Vec<String>, crate::core::errors::DataProfilerError> {
let config = DatabaseConfig {
connection_string,
..Default::default()
};
let mut connector = create_connector(config)?;
connector.connect().await?;
let schema = connector.get_table_schema(&table_name).await?;
connector.disconnect().await?;
Ok(schema)
}
#[pyfunction]
pub fn count_table_rows_async<'py>(
py: Python<'py>,
connection_string: String,
table_name: String,
) -> PyResult<Bound<'py, PyAny>> {
#[cfg(feature = "python-async")]
{
pyo3_async_runtimes::tokio::future_into_py(py, async move {
count_table_rows_internal(connection_string, table_name)
.await
.map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(e.to_string()))
})
}
#[cfg(not(feature = "python-async"))]
{
Err(pyo3::exceptions::PyRuntimeError::new_err(
"Async support not enabled. Please compile with --features python-async",
))
}
}
#[cfg(all(feature = "database", feature = "python-async"))]
async fn count_table_rows_internal(
connection_string: String,
table_name: String,
) -> Result<u64, crate::core::errors::DataProfilerError> {
let config = DatabaseConfig {
connection_string,
..Default::default()
};
let mut connector = create_connector(config)?;
connector.connect().await?;
let count = connector.count_table_rows(&table_name).await?;
connector.disconnect().await?;
Ok(count)
}