Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions src/reader/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,50 +27,35 @@ pub enum ConnectionInfo {
/// # Supported Formats
///
/// - `duckdb://memory` - DuckDB in-memory database
/// - `duckdb:///absolute/path/file.db` - DuckDB file (absolute path)
/// - `duckdb://relative/file.db` - DuckDB file (relative path)
/// - `duckdb://...` - DuckDB path
/// - `postgres://...` - PostgreSQL connection string
/// - `sqlite://...` - SQLite file path
///
/// # Examples
///
/// ```
/// use ggsql::reader::connection::{parse_connection_string, ConnectionInfo};
///
/// let info = parse_connection_string("duckdb://memory").unwrap();
/// assert_eq!(info, ConnectionInfo::DuckDBMemory);
///
/// let info = parse_connection_string("duckdb://data.db").unwrap();
/// assert_eq!(info, ConnectionInfo::DuckDBFile("data.db".to_string()));
/// ```
pub fn parse_connection_string(uri: &str) -> Result<ConnectionInfo> {
if uri == "duckdb://memory" {
return Ok(ConnectionInfo::DuckDBMemory);
}

if let Some(path) = uri.strip_prefix("duckdb://") {
// Remove leading slashes for file paths
let cleaned_path = path.trim_start_matches('/');
if cleaned_path.is_empty() {
if path.is_empty() {
return Err(GgsqlError::ReaderError(
"DuckDB file path cannot be empty".to_string(),
));
}
return Ok(ConnectionInfo::DuckDBFile(cleaned_path.to_string()));
return Ok(ConnectionInfo::DuckDBFile(path.to_string()));
}

if uri.starts_with("postgres://") || uri.starts_with("postgresql://") {
return Ok(ConnectionInfo::PostgreSQL(uri.to_string()));
}

if let Some(path) = uri.strip_prefix("sqlite://") {
let cleaned_path = path.trim_start_matches('/');
if cleaned_path.is_empty() {
if path.is_empty() {
return Err(GgsqlError::ReaderError(
"SQLite file path cannot be empty".to_string(),
));
}
return Ok(ConnectionInfo::SQLite(cleaned_path.to_string()));
return Ok(ConnectionInfo::SQLite(path.to_string()));
}

if let Some(conn_str) = uri.strip_prefix("odbc://") {
Expand Down Expand Up @@ -107,7 +92,7 @@ mod tests {
#[test]
fn test_duckdb_file_absolute() {
let info = parse_connection_string("duckdb:///tmp/data.db").unwrap();
assert_eq!(info, ConnectionInfo::DuckDBFile("tmp/data.db".to_string()));
assert_eq!(info, ConnectionInfo::DuckDBFile("/tmp/data.db".to_string()));
}

#[test]
Expand Down Expand Up @@ -139,6 +124,12 @@ mod tests {
assert_eq!(info, ConnectionInfo::SQLite("data.db".to_string()));
}

#[test]
fn test_sqlite_absolute() {
let info = parse_connection_string("sqlite:///tmp/data.db").unwrap();
assert_eq!(info, ConnectionInfo::SQLite("/tmp/data.db".to_string()));
}

#[test]
fn test_empty_duckdb_path() {
let result = parse_connection_string("duckdb://");
Expand Down
Loading