sqlload 0.38.0

SQL data loading tooling.
//! Load one table: read its source (a Python table through the host) and commit its rows batch by batch.

use crate::loader::_helpers::publish::publish_table;
use crate::loader::_helpers::sources::{open_source, source_readers};
use crate::loader::constants::NO_PYTHON_HOST;
use crate::loader::errors::LoaderError;
use crate::loader::main::_load_python::load_python;
use crate::loader::models::{LoadRun, PythonLoad, TableLoaded, TablePlan};

/// Load one table; returns its name as reported and what the load did.
pub(in crate::loader) async fn load_one(
    run: &LoadRun<'_>,
    planned: &TablePlan,
) -> Result<(String, TableLoaded), LoaderError> {
    let project = run.project;
    let source = project
        .sources
        .get(&planned.spec.source_name)
        .ok_or_else(|| {
            LoaderError::Invalid(format!(
                "source {:?} does not exist",
                planned.spec.source_name
            ))
        })?;
    let destination = run.destinations.get(&planned.spec.target).ok_or_else(|| {
        LoaderError::Invalid(format!("target {:?} is not connected", planned.spec.target))
    })?;
    let name = planned.outcome_name();
    let loaded = match (&planned.spec.python, &run.host) {
        (Some(_), Some(host)) => {
            let load = PythonLoad {
                project,
                table: planned,
                destination,
                host: host.clone(),
                options: run.options,
            };
            load_python(&load).await?
        }
        (Some(_), None) => return Err(LoaderError::Invalid(NO_PYTHON_HOST.to_owned())),
        (None, _) => {
            let reader = open_source(project, source, source_readers(planned)).await?;
            TableLoaded {
                rows: publish_table(&reader, planned, (destination, run)).await?,
                changes: Vec::new(),
                note: None,
            }
        }
    };
    Ok((name, loaded))
}