mod arrow_values;
pub mod avro;
pub mod csv;
pub mod duckdb;
pub mod excel;
pub mod json;
pub mod mssql;
pub mod mysql;
pub mod odbc;
mod output_file;
pub mod parquet;
pub mod postgres;
mod row_stream;
pub mod snowflake;
pub mod sqlite;
use crate::{
schema::{Row, Schema},
Result,
};
use async_trait::async_trait;
#[async_trait]
pub trait Source: Send + Sync {
async fn connect(&mut self) -> Result<()>;
async fn infer_schema(&mut self, sample_size: usize) -> Result<Schema>;
async fn read_batch(&mut self, batch_size: usize) -> Result<Vec<Row>>;
async fn estimated_row_count(&self) -> Result<Option<usize>>;
async fn reset(&mut self) -> Result<()>;
fn has_more(&self) -> bool;
}
#[async_trait]
pub trait Target: Send + Sync {
async fn connect(&mut self) -> Result<()>;
async fn create_table(&mut self, table_name: &str, schema: &Schema) -> Result<()>;
async fn write_batch(&mut self, rows: &[Row]) -> Result<usize>;
async fn finalize(&mut self) -> Result<()>;
async fn exists(&self, table_name: &str) -> Result<bool>;
async fn truncate(&mut self, table_name: &str) -> Result<()>;
fn supports_append(&self) -> bool;
}
pub fn create_source(connection_string: &str) -> Result<Box<dyn Source>> {
if connection_string.ends_with(".csv") {
Ok(Box::new(csv::CsvSource::new(connection_string)?))
} else if connection_string.ends_with(".json") || json::is_json_lines(connection_string) {
Ok(Box::new(json::JsonSource::new(connection_string)?))
} else if connection_string.ends_with(".parquet") {
Ok(Box::new(parquet::ParquetSource::new(connection_string)?))
} else if connection_string.ends_with(".avro") {
Ok(Box::new(avro::AvroSource::new(connection_string)?))
} else if excel::is_spreadsheet(connection_string) {
Ok(Box::new(excel::ExcelSource::new(connection_string)?))
} else if (connection_string.contains(".duckdb#") || connection_string.ends_with(".duckdb"))
|| connection_string.starts_with("duckdb:")
{
Ok(Box::new(duckdb::DuckdbSource::new(connection_string)?))
} else if (connection_string.contains(".db#") || connection_string.ends_with(".db"))
|| connection_string.starts_with("sqlite:")
{
Ok(Box::new(sqlite::SqliteSource::new(connection_string)?))
} else if connection_string.starts_with("postgres://")
|| connection_string.starts_with("postgresql://")
{
Ok(Box::new(postgres::PostgresSource::new(connection_string)?))
} else if connection_string.starts_with("mysql://") {
Ok(Box::new(mysql::MysqlSource::new(connection_string)?))
} else if connection_string.starts_with("mssql://")
|| connection_string.starts_with("sqlserver://")
{
Ok(Box::new(mssql::MssqlSource::new(connection_string)?))
} else if connection_string.starts_with("odbc://") {
Ok(Box::new(odbc::OdbcSource::new(connection_string)?))
} else if connection_string.starts_with("snowflake://") {
Ok(Box::new(snowflake::SnowflakeSource::new(
connection_string,
&std::collections::HashMap::new(),
)?))
} else {
Err(crate::TinyEtlError::Configuration(
format!("Unsupported source type: {}. Supported formats: file.csv, file.json, file.jsonl, file.parquet, file.avro, file.xlsx[#sheet] (also .xlsm, .xlsb, .xls, .ods), file.db#table, file.duckdb#table, postgres://user:pass@host:port/db#table, mysql://user:pass@host:port/db#table, mssql://user:pass@host:port/db#table, odbc://connection_string#table, snowflake://user@account/db/schema#table", connection_string)
))
}
}
pub fn create_target(connection_string: &str) -> Result<Box<dyn Target>> {
if connection_string.contains("://") {
if connection_string.starts_with("duckdb://") {
return Ok(Box::new(duckdb::DuckdbTarget::new(connection_string)?));
} else if connection_string.starts_with("sqlite://") {
return Ok(Box::new(sqlite::SqliteTarget::new(connection_string)?));
} else if connection_string.starts_with("postgres://")
|| connection_string.starts_with("postgresql://")
{
return Ok(Box::new(postgres::PostgresTarget::new(connection_string)?));
} else if connection_string.starts_with("mysql://") {
return Ok(Box::new(mysql::MysqlTarget::new(connection_string)?));
} else if connection_string.starts_with("mssql://")
|| connection_string.starts_with("sqlserver://")
{
return Ok(Box::new(mssql::MssqlTarget::new(connection_string)?));
} else if connection_string.starts_with("odbc://") {
return Ok(Box::new(odbc::OdbcTarget::new(connection_string)?));
} else if connection_string.starts_with("snowflake://") {
return Ok(Box::new(snowflake::SnowflakeTarget::new(
connection_string,
&std::collections::HashMap::new(),
)?));
} else {
return Err(crate::TinyEtlError::Configuration(format!(
"Unsupported protocol in: {}. Supported protocols: duckdb://, sqlite://, postgres://, mysql://, mssql://, odbc://, snowflake://",
connection_string
)));
}
}
if connection_string.ends_with(".csv") {
Ok(Box::new(csv::CsvTarget::new(connection_string)?))
} else if connection_string.ends_with(".json") || json::is_json_lines(connection_string) {
Ok(Box::new(json::JsonTarget::new(connection_string)?))
} else if connection_string.ends_with(".parquet") {
Ok(Box::new(parquet::ParquetTarget::new(connection_string)?))
} else if connection_string.ends_with(".avro") {
Ok(Box::new(avro::AvroTarget::new(connection_string)?))
} else if excel::is_spreadsheet(connection_string) {
Ok(Box::new(excel::ExcelTarget::new(connection_string)?))
} else if connection_string.contains(".duckdb#")
|| connection_string.ends_with(".duckdb")
|| connection_string.starts_with("duckdb:")
{
Ok(Box::new(duckdb::DuckdbTarget::new(connection_string)?))
} else if connection_string.contains(".db#")
|| connection_string.ends_with(".db")
|| connection_string.starts_with("sqlite:")
{
Ok(Box::new(sqlite::SqliteTarget::new(connection_string)?))
} else {
Err(crate::TinyEtlError::Configuration(format!(
"Unsupported target type: {}. Supported formats: \
file.csv, file.json, file.jsonl, file.parquet, file.avro, file.xlsx[#sheet], file.db, file.db#table, file.duckdb, file.duckdb#table, \
duckdb://path/file.duckdb#table, sqlite://path/file.db#table, postgres://user:pass@host:port/db#table, mysql://user:pass@host:port/db#table, mssql://user:pass@host:port/db#table, odbc://connection_string#table, snowflake://user@account/db/schema#table",
connection_string
)))
}
}
pub async fn create_source_from_url(connection_string: &str) -> Result<Box<dyn Source>> {
create_source_from_url_with_type_and_options(
connection_string,
None,
&std::collections::HashMap::new(),
)
.await
}
pub async fn create_source_from_url_with_type(
connection_string: &str,
source_type: Option<&str>,
) -> Result<Box<dyn Source>> {
create_source_from_url_with_type_and_options(
connection_string,
source_type,
&std::collections::HashMap::new(),
)
.await
}
pub async fn create_source_from_url_with_type_and_options(
connection_string: &str,
source_type: Option<&str>,
options: &std::collections::HashMap<String, String>,
) -> Result<Box<dyn Source>> {
if connection_string.starts_with("snowflake://") {
return Ok(Box::new(snowflake::SnowflakeSource::new(
connection_string,
options,
)?));
}
if connection_string.contains("://") {
if connection_string.starts_with("duckdb://")
|| connection_string.starts_with("sqlite://")
|| connection_string.starts_with("postgres://")
|| connection_string.starts_with("postgresql://")
|| connection_string.starts_with("mysql://")
|| connection_string.starts_with("mssql://")
|| connection_string.starts_with("sqlserver://")
|| connection_string.starts_with("odbc://")
{
create_source(connection_string)
} else {
crate::protocols::create_source_from_url_with_type(
connection_string,
source_type,
options,
)
.await
}
} else {
create_source(connection_string)
}
}
pub async fn create_target_from_url(connection_string: &str) -> Result<Box<dyn Target>> {
create_target_from_url_with_options(connection_string, &std::collections::HashMap::new()).await
}
pub async fn create_target_from_url_with_options(
connection_string: &str,
options: &std::collections::HashMap<String, String>,
) -> Result<Box<dyn Target>> {
if connection_string.starts_with("snowflake://") {
return Ok(Box::new(snowflake::SnowflakeTarget::new(
connection_string,
options,
)?));
}
if connection_string.contains("://") {
if connection_string.starts_with("sqlite://")
|| connection_string.starts_with("postgres://")
|| connection_string.starts_with("postgresql://")
|| connection_string.starts_with("mysql://")
|| connection_string.starts_with("mssql://")
|| connection_string.starts_with("sqlserver://")
|| connection_string.starts_with("odbc://")
{
create_target(connection_string)
} else {
crate::protocols::create_target_from_url_with_options(connection_string, options).await
}
} else {
create_target(connection_string)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_csv_source() {
let source = create_source("test.csv");
assert!(source.is_ok());
}
#[test]
fn test_create_json_source() {
let source = create_source("test.json");
assert!(source.is_ok());
}
#[test]
fn test_create_parquet_source() {
let source = create_source("test.parquet");
assert!(source.is_ok());
}
#[test]
fn test_create_sqlite_source() {
let result = create_source("test.db#table");
if let Err(ref e) = result {
println!("SQLite source creation failed: {}", e);
}
assert!(result.is_ok());
}
#[test]
fn test_create_sqlite_source_without_table() {
let result = create_source("test.db");
assert!(result.is_err());
if let Err(err) = result {
assert!(err
.to_string()
.contains("SQLite source requires table specification"));
}
}
#[test]
fn test_create_sqlite_with_sqlite_prefix() {
let source = create_source("sqlite:test.db#table");
assert!(source.is_ok());
}
#[test]
fn test_create_postgres_source() {
let source = create_source("postgres://user:pass@localhost:5432/db#table");
assert!(source.is_ok());
}
#[test]
fn test_create_postgres_source_without_table() {
let result = create_source("postgres://user:pass@localhost:5432/db");
assert!(result.is_err());
}
#[test]
fn test_create_postgresql_source() {
let source = create_source("postgresql://user:pass@localhost:5432/db#table");
assert!(source.is_ok());
}
#[test]
fn test_create_unsupported_source() {
let source = create_source("test.unsupported");
assert!(source.is_err());
}
#[test]
fn test_create_csv_target() {
let target = create_target("output.csv");
assert!(target.is_ok());
}
#[test]
fn test_create_json_target() {
let target = create_target("output.json");
assert!(target.is_ok());
}
#[test]
fn test_create_parquet_target() {
let target = create_target("output.parquet");
assert!(target.is_ok());
}
#[test]
fn test_create_sqlite_target() {
let target = create_target("output.db");
assert!(target.is_ok());
}
#[test]
fn test_create_sqlite_target_with_table() {
let target = create_target("output.db#mytable");
assert!(target.is_ok());
}
#[test]
fn test_create_sqlite_protocol_target() {
let target = create_target("sqlite://output.db#mytable");
assert!(target.is_ok());
}
#[test]
fn test_create_postgres_target() {
let target = create_target("postgres://user:pass@localhost:5432/db");
assert!(target.is_ok());
}
#[test]
fn test_create_postgres_target_with_table() {
let target = create_target("postgres://user:pass@localhost:5432/db#mytable");
assert!(target.is_ok());
}
#[test]
fn test_create_postgresql_target() {
let target = create_target("postgresql://user:pass@localhost:5432/db");
assert!(target.is_ok());
}
#[test]
fn test_create_mysql_target() {
let target = create_target("mysql://user:pass@localhost:3306/db");
assert!(target.is_ok());
}
#[test]
fn test_create_mysql_target_with_table() {
let target = create_target("mysql://user:pass@localhost:3306/db#mytable");
assert!(target.is_ok());
}
#[test]
fn test_create_unsupported_target() {
let target = create_target("output.unsupported");
assert!(target.is_err());
}
#[test]
fn test_create_unsupported_protocol() {
let target = create_target("redis://localhost:6379");
assert!(target.is_err());
}
}