Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add DuckDB data directory #61

Merged
Merged
Show file tree
Hide file tree
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
30 changes: 17 additions & 13 deletions src/duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,24 @@ type Result<T, E = Error> = std::result::Result<T, E>;

pub struct DuckDBTableProviderFactory {
access_mode: AccessMode,
db_path_param: String,
attach_databases_param: String,
}

const DUCKDB_DB_PATH_PARAM: &str = "open";
const DUCKDB_DB_BASE_FOLDER_PARAM: &str = "data_directory";
const DUCKDB_ATTACH_DATABASES_PARAM: &str = "attach_databases";

impl DuckDBTableProviderFactory {
#[must_use]
pub fn new() -> Self {
Self {
access_mode: AccessMode::ReadOnly,
db_path_param: "open".to_string(),
attach_databases_param: "attach_databases".to_string(),
}
}

#[must_use]
pub fn attach_databases(&self, options: &HashMap<String, String>) -> Vec<Arc<str>> {
options
.get(&self.attach_databases_param)
.get(DUCKDB_ATTACH_DATABASES_PARAM)
.map(|attach_databases| {
attach_databases
.split(';')
Expand All @@ -147,16 +147,20 @@ impl DuckDBTableProviderFactory {
self
}

#[must_use]
pub fn db_path_param(mut self, db_path_param: &str) -> Self {
self.db_path_param = db_path_param.to_string();
self
}

#[must_use]
pub fn duckdb_file_path(&self, name: &str, options: &mut HashMap<String, String>) -> String {
let db_path = remove_option(options, &self.db_path_param);
db_path.unwrap_or_else(|| format!("{name}.db"))
let options = util::remove_prefix_from_hashmap_keys(options.clone(), "duckdb_");

let db_base_folder = options
.get(DUCKDB_DB_BASE_FOLDER_PARAM)
.cloned()
.unwrap_or(".".to_string()); // default to the current directory
let default_filepath = format!("{db_base_folder}/{name}.db");

options
.get(DUCKDB_DB_PATH_PARAM)
.cloned()
.unwrap_or(default_filepath)
}
}

Expand Down
22 changes: 9 additions & 13 deletions src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,21 @@ pub enum Error {

type Result<T, E = Error> = std::result::Result<T, E>;

pub struct SqliteTableProviderFactory {
db_path_param: String,
db_base_folder_param: String,
attach_databases_param: String,
}
pub struct SqliteTableProviderFactory {}

const SQLITE_DB_PATH_PARAM: &str = "file";
const SQLITE_DB_BASE_FOLDER_PARAM: &str = "data_directory";
const SQLITE_ATTACH_DATABASES_PARAM: &str = "attach_databases";

impl SqliteTableProviderFactory {
#[must_use]
pub fn new() -> Self {
Self {
db_path_param: "file".to_string(),
db_base_folder_param: "data_directory".to_string(),
attach_databases_param: "attach_databases".to_string(),
}
Self {}
}

#[must_use]
pub fn attach_databases(&self, options: &HashMap<String, String>) -> Option<Vec<Arc<str>>> {
options.get(&self.attach_databases_param).map(|databases| {
options.get(SQLITE_ATTACH_DATABASES_PARAM).map(|databases| {
databases
.split(';')
.map(Arc::from)
Expand All @@ -115,13 +111,13 @@ impl SqliteTableProviderFactory {
let options = util::remove_prefix_from_hashmap_keys(options.clone(), "sqlite_");

let db_base_folder = options
.get(&self.db_base_folder_param)
.get(SQLITE_DB_BASE_FOLDER_PARAM)
.cloned()
.unwrap_or(".".to_string()); // default to the current directory
let default_filepath = format!("{db_base_folder}/{name}_sqlite.db");

options
.get(&self.db_path_param)
.get(SQLITE_DB_PATH_PARAM)
.cloned()
.unwrap_or(default_filepath)
}
Expand Down